Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Nsc/segfault doc #93

Open
wants to merge 22 commits into
base: bl/stack-trace
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5184e3a
bugfix: pick path under project root when possible (#28)
gbrik Nov 16, 2020
12aa875
Added check for invalid utf8 doc string and cleaning
Strum355 Nov 18, 2020
0f8305e
Merge pull request #31 from sourcegraph/nsc/fixutf8
Strum355 Nov 18, 2020
234bb81
Added .gitignore and updated linux kernel instructions
Strum355 Nov 30, 2020
5bd7e30
Dockerized with Github Action
Strum355 Dec 2, 2020
166f328
Merge pull request #33 from sourcegraph/nsc/dockerized
Strum355 Dec 2, 2020
a350b92
test commit for workflow
Strum355 Dec 2, 2020
85a2181
Fixed branch name
Strum355 Dec 2, 2020
91bf298
auto push not supported OOTB
Strum355 Dec 2, 2020
ba900cc
Smaller image by only shipping libllvm10
Strum355 Dec 2, 2020
684b21b
Bump dockerfile to ubuntu 20.10
Strum355 Jan 4, 2021
2099933
Added env to point cmake to clang-10 in Dockerfile
Strum355 Jan 4, 2021
44ca8aa
Transition to beta stage
Strum355 Jan 4, 2021
cffed4a
Exit 1 when 0 entries in compile_commands.json/file not found
Strum355 Jan 13, 2021
048cebd
Added DEBIAN_FRONTEND=noninteractive to Dockerfile and clang-10 to pa…
Strum355 Jan 14, 2021
55bf7ff
--debug-files
shrouxm Feb 3, 2021
75e801d
introduces backward-cpp based backtraces on segfaults
Strum355 Nov 19, 2020
9e26e13
Merge pull request #32 from sourcegraph/nsc/epic-backtrace-avec-backw…
Strum355 Feb 4, 2021
3d5a13c
ci: Use https:// rather than ssh for clone
tjdevries Feb 4, 2021
f660a58
ci: pull_request workflow
tjdevries Feb 4, 2021
aa1bcc8
escape the project root we compare paths against (#42)
gbrik Feb 9, 2021
935aafe
introduce documentation to aid users in the case of a segfault
Strum355 Jan 18, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
massif.out.*
.vscode
.devcontainer
build/
bin/
25 changes: 25 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: pull_request

on: [pull_request]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Unshallow
run: git fetch --prune --unshallow && git submodule update --init --recursive
- uses: docker/setup-buildx-action@v1
- name: Docker login
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Just build (no push)
id: docker_build
uses: docker/build-push-action@v2
with:
push: false
- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
29 changes: 29 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: release

on:
push:
branches:
- llvmorg-10.0.0-lsif-clang

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Unshallow
run: git fetch --prune --unshallow && git submodule update --init --recursive
- uses: docker/setup-buildx-action@v1
- name: Docker login
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
push: true
tags: sourcegraph/lsif-clang:latest
- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
massif.out.*
vgcore.*
valgrind*
.clangd
.cache
bin
build
cmake-build-debug*
.clangd
compile_commands.json
dump.lsif
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "src/indexer/backward-cpp"]
path = src/indexer/backward-cpp
url = https://github.com/bombela/backward-cpp.git
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_BUILD_TYPE RelWithDebInfo)
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O3 -DNDEBUG -g")

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
Expand Down
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM ubuntu:20.10 as build

RUN apt update && apt install -y llvm-10 clang-10 libclang-10-dev cmake

WORKDIR /lsif-clang

COPY . .

RUN CC=clang-10 CXX=clang-10 cmake -B build && make -C build -j$(nproc)

FROM ubuntu:20.10

RUN apt update && apt install -y libllvm10 cmake clang-10

# Might as well set this, for auto-index purposes
ENV DEBIAN_FRONTEND=noninteractive

COPY --from=build /lsif-clang/bin/lsif-clang /usr/local/bin/lsif-clang

ENTRYPOINT [ "lsif-clang" ]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# lsif-clang indexer ![Status: Development](https://img.shields.io/badge/status-development-yellow?style=flat)
# lsif-clang indexer ![Status: Development](https://img.shields.io/badge/status-beta-yellow?style=flat)

![GIF displaying usage on the linux kernel.](docs/gifs/torvalds-linux.gif)
![GIF displaying usage on the linux kernel.](docs/images/torvalds-linux.gif)

This project is a fork of [clangd](https://clangd.llvm.org/) with patches to add support for outputting [LSIF indexes](https://microsoft.github.io/language-server-protocol/specifications/lsif/0.5.0/specification/). Specifically, a fork of the `clang-tools-extra/clangd` subdirectory of the [llvm-project repo](https://github.com/llvm/llvm-project/).

Expand Down
6 changes: 3 additions & 3 deletions docs/compdb.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ http_archive(
```

Then you can run:
```
```bash
bazel build \
--aspects=@bazel_compdb//:aspects.bzl%compilation_database_aspect \
--output_groups=compdb_files,header_files `# this should include any generated outputs needed for cpp compilation` \
$(bazel query 'kind("cc_(library|binary|test|inc_library|proto_library)", //...)')'
--output_groups=compdb_files,header_files \ # this should include any generated outputs needed for cpp compilation
$(bazel query 'kind("cc_(library|binary|test|inc_library|proto_library)", //...)')
```
The bazel query might look different for your project.

Expand Down
10 changes: 5 additions & 5 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ This page provides examples of generating compile_commands.json files for differ

## [github.com/torvalds/linux](https://github.com/torvalds/linux)

![GIF displaying usage on the linux kernel.](gifs/torvalds-linux.gif)
![GIF displaying usage on the linux kernel.](images/torvalds-linux.gif)

Once you've installed the kernel dependencies (you can use the table in `Documentation/process/changes.rst`), run the following commands from the repository root:
```sh
make allyesconfig
make CC=clang-10 HOSTCC=clang-10 `# replace with your clang version`
scripts/gen_compile_commands.py
make CC=clang-10 HOSTCC=clang-10 # replace with your clang version
scripts/clang-tools/gen_compile_commands.py
lsif-clang compile_commands.json
```

## [github.com/envoyproxy/envoy](https://github.com/envoyproxy/envoy)

Use the following steps:
```sh
./bazel/setup_clang.sh /usr/lib/llvm-10 `# or wherever your llvm installation lives`
./bazel/setup_clang.sh /usr/lib/llvm-10 # or wherever your llvm installation lives
echo 'build --config=clang' > user.bazelrc
TEST_TMPDIR=/tmp tools/gen_compilation_database.py --include_headers --run_bazel_build
lsif-clang compile_commands.json
Expand All @@ -28,6 +28,6 @@ lsif-clang compile_commands.json
Install the [Ninja build tool](https://ninja-build.org/), and run the following commands from the repository root:
```sh
cmake -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -Dgrpc_BUILD_TESTS=ON -G Ninja
ninja -C build $(egrep -e '^build[^:]+.pb.(cc|h|c|cpp|inc|hpp)[: ]' build/build.ninja | awk '{print $2}') `# generate pb`
ninja -C build $(egrep -e '^build[^:]+.pb.(cc|h|c|cpp|inc|hpp)[: ]' build/build.ninja | awk '{print $2}') # generate pb
lsif-clang build/compile_commands.json
```
Binary file added docs/images/stacktrace.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This project depends on LLVM and Clang. lsif-clang itself should be built agains
### Ubuntu (20.04)

```sh
apt install llvm-10 clang clang-10 libclang-10-dev cmake
apt install llvm-10 clang clang-10 libclang-10-dev cmake binutils-dev
```

#### Older versions of Ubuntu
Expand All @@ -19,13 +19,15 @@ don't exist in the `apt` package repository.
### MacOS

```sh
brew install cmake sourcegraph/brew/llvm@10
brew install cmake sourcegraph/brew/llvm@10 binutils
```

> Note: lsif-clang must currently be built using LLVM 10

# Installation

Make sure to checkout any submodules, either with `git clone --recurse-submodules ...` or `git submodule update --init --recursive`

### Ubuntu

```sh
Expand Down
48 changes: 48 additions & 0 deletions docs/segfault.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# How to troubleshoot a segfault

This document outlines how to configure your system to provide enough information to debug lsif-clang in the case of a segfault.

## Information to be provided

Along with the information gathered by reading the section below, please also provide:

1. lsif-clang version (commit/tag/etc)
1. Output of `ldd <path to lsif-clang>`
1. lsif-clang source (self-compiled/docker/prebuilt binary)
- In the case of self-compiled, it may be useful to attach the binary in the bug report
1. One or both of the below (stacktrace/core dump)

## Stacktrace

The lsif-clang indexer has the ability to emit a detailed stacktrace (when possible) as shown in the screenshot below if a segfault is encountered. In the case of the stacktrace not being annotated with source fragments as shown below, we may also ask for a core dump. To shorten the feedback loop, it can be beneficial to include the core dump with the original bug report.

![stacktrace in terminal](images/stacktrace.png)

## Core dump

If there's no stacktrace, then the next option is to configure the OS to save a core dump on segfault.

As the process is a bit more involved, we will outline general guidelines for coercing the system to save a core dump.

### Linux

1. Run `ulimit -c unlimited`
1. Check the core-dump handler
- You can find the configured handler by running `cat /proc/sys/kernel/core_pattern`
- The output can be either a path with format verbs or a path prefixed by `|` followed by format verbs
- If the latter, then the binary at that path will be invoked with the core dump and you should read the manual for the given program as to where it stores core dumps and how to proper configure it to do so
- Else if the former, assuming the aforementioned `ulimit` command was invoked, then it should save the file at that location with the filename based on the format verbs passed
- e.g `/tmp/core-%e.%p.%h.%t` vs `|/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %h`
1. Configure the core-dump handler (if necessary)
- This can be done by running `sysctl -w kernel.core_pattern=<your desired config>` as root

### Docker

If invoking `lsif-clang` from a docker container, the core dump settings are inherited from the host OS (or VM if Mac/Windows).
In this case, it is highly recommended to configure the core pattern to save to a file that can be copied to the host from the container.

### Mac OS

1. Run `ulimit -c unlimited`
1. Make sure `/cores` directory is writeable by the current user
1. And that should be it : )
15 changes: 12 additions & 3 deletions src/index/LSIFSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ template <> struct DenseMapInfo<clang::clangd::SymbolLocation> {
namespace {
struct LSIFMeta {
LSIFMeta(llvm::raw_ostream &OS, const clang::clangd::IndexFileOut &O)
: OS(OS), ProjectRoot(O.ProjectRoot), Debug(O.Debug) {}
: OS(OS), ProjectRoot(O.ProjectRoot), Debug(O.Debug), DebugFiles(O.DebugFiles) {}
llvm::raw_ostream &OS;
const std::string ProjectRoot;
const bool Debug;
const bool DebugFiles;
int IDCounter = 0;
llvm::StringMap<int> DocumentIDs;
llvm::DenseMap<clang::clangd::SymbolID, int> ReferenceResultIDs;
Expand All @@ -76,6 +77,9 @@ struct LSIFMeta {

bool contains(const char *FileURI) {
bool contains = FileURI && llvm::StringRef(FileURI).startswith(ProjectRoot);
if (!contains && DebugFiles) {
llvm::errs() << "Ignoring file: " << llvm::StringRef(FileURI) << "\n";
}
return contains;
}
};
Expand Down Expand Up @@ -254,8 +258,13 @@ void writeHoverResult(LSIFMeta &Meta, const clang::clangd::Symbol &Sym,

JSONOut.attribute("value", OHover.str());
});
if (Sym.Documentation.data() && !Sym.Documentation.empty())
JSONOut.value(Sym.Documentation);
if (Sym.Documentation.data() && !Sym.Documentation.empty()) {
auto docString = Sym.Documentation;
if(!llvm::json::isUTF8(docString)) {
docString = llvm::StringRef(llvm::json::fixUTF8(Sym.Documentation));
}
JSONOut.value(docString);
}
});
});
});
Expand Down
Loading