llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.13k stars 12.02k forks source link

Regression: JITed Code Debugging Broken on LLDB 6.0 Release Branch #35557

Closed weliveindetail closed 3 years ago

weliveindetail commented 6 years ago
Bugzilla Link 36209
Resolution FIXED
Resolved on Jan 14, 2021 07:17
Version 8.0
OS All
Attachments Full logs from LLDB 6.0, LLDB 5.0 and LLDB 4.0
CC @dwblaikie,@lhames

Extended Description

With LLDB built from branch release_60 it is no longer possible to set a breakpoint in JIT-compiled code. This worked well for release_50. LLDB 6.0 fails with error message: warning: failed to set breakpoint site at 0x4 for breakpoint 1.1: error: 2 sending the breakpoint request

Repro with a default configuration of LLVM/Clang/LLDB on Linux Mint 18:

$ cat hellow.cpp

include

void jitbp() { printf("Hello World!\n"); }

int main() { jitbp(); return 0; }

$ clang -g -c -S -emit-llvm hellow.cpp $ ./lli hellow.ll Hello World! $ ./lldb -- lli (lldb) target create "lli" Current executable set to 'lli' (x86_64). (lldb) b jitbp Breakpoint 1: no locations (pending). WARNING: Unable to resolve breakpoint to any actual locations. (lldb) log enable lldb jit (lldb) r hellow.ll

Actual output with LLDB 6.0:

lldb JITLoaderGDB::SetJITBreakpoint looking for JIT register hook lldb JITLoaderGDB::SetJITBreakpoint looking for JIT register hook Process 17664 launched: '/media/LLVM/llvm60/build-debug-clang-lld-lldb/bin/lli' (x86_64) intern-state JITLoaderGDB::SetJITBreakpoint looking for JIT register hook ... intern-state JITLoaderGDB::SetJITBreakpoint looking for JIT register hook intern-state JITLoaderGDB::SetJITBreakpoint setting JIT breakpoint intern-state JITLoaderGDB::JITDebugBreakpointHit hit JIT breakpoint intern-state JITLoaderGDB::ReadJITDescriptorImpl registering JIT entry at 0x781230 (3432 bytes) warning: failed to set breakpoint site at 0x4 for breakpoint 1.1: error: 2 sending the breakpoint request 1 location added to breakpoint 1 warning: failed to set breakpoint site at 0x4 for breakpoint 1.1: error: 2 sending the breakpoint request Hello World! intern-state JITLoaderGDB::JITDebugBreakpointHit hit JIT breakpoint intern-state JITLoaderGDB::ReadJITDescriptorImpl unregistering JIT entry at 0x781230 Process 17664 exited with status = 0 (0x00000000) (lldb) q

Expected output, tested with LLDB 5.0 and LLDB 4.0:

JITLoaderGDB::SetJITBreakpoint looking for JIT register hook JITLoaderGDB::SetJITBreakpoint looking for JIT register hook Process 16807 launched: '/media/LLVM/llvm50/build-debug-clang-lld-lldb/bin/lli' (x86_64) JITLoaderGDB::SetJITBreakpoint looking for JIT register hook ... JITLoaderGDB::SetJITBreakpoint looking for JIT register hook JITLoaderGDB::SetJITBreakpoint setting JIT breakpoint JITLoaderGDB::JITDebugBreakpointHit hit JIT breakpoint JITLoaderGDB::ReadJITDescriptorImpl registering JIT entry at 0x754850 (3432 bytes) 1 location added to breakpoint 1 Process 16807 stopped

The same issue can be reproduced with a slightly modified version of LLDB that allows debugging JITed code on Mac OS X. Please find attached the full logs.

weliveindetail commented 3 years ago

The documentation update landed and should be in the LLVM 12 release. A orc-greedy mode was considered a good addition for LLI in https://reviews.llvm.org/D90789#2491473

weliveindetail commented 4 years ago

Just confirmed this to work with RuntimeDyld-based LLJIT on current tip-of-tree:

(lldb) run tools/lldb/test/Breakpoint/Output/jitbp_elf.test.tmp.ll Process 89272 launched: '/workspaces/llvm-lldb/build/bin/LLJITWithGDBRegistrationListener' (x86_64) 1 location added to breakpoint 1 Process 89272 stopped

I guess we cannot add a test for it based on the OrcV2 example executable, so we'd have to add something like an "orc-eager" kind to lli.

@​Lang: Do you think that's worth it? Anything special I should consider in case I decided to do that?

weliveindetail commented 4 years ago

Part 2 of the fix has landed. It will ship in the upcoming release 12 of LLDB. I also updated the documentation and set up a review: https://reviews.llvm.org/D90789

weliveindetail commented 4 years ago

Yes, I want to do that. For the LLDB test case, however, it doesn't matter so much as it currently uses the same RuntimeDyld functionality under the hood as MCJIT does.

Also (note to myself) I have to patch lli first, as it currently makes the distinction whether or not to add the GDB listener based on the triple's isOSBinFormatELF(). That disqualifies JITing ELF on macOS or Windows even though it's quite a popular workaround.

lhames commented 4 years ago

Hi Stefan,

FWIW OrcV2 now supports listeners when using RTDyldObjectLinkingLayer if you want to test this under OrcV2. (I believe there is an example in llvm/examples/OrcV2Examples).

I'm on the road the next few days, but will try to check that review soon. Please ping me if you don't hear anything by next week.

-- Lang.

weliveindetail commented 4 years ago

https://reviews.llvm.org/D90769 aims to fix the relocation issue and bring back source-level debugging.

weliveindetail commented 4 years ago

These fields are empty because the object that we get from MCJIT doesn't have relocations applied. There's multiple reasons for it on the MCJIT side:

I can think of two ways that this broke since 4.0:

(1) Either LLDB used to resolve relocations for objects it received via the GDB JIT Interface itself and it stopped doing so between releases 5 and 6.

(2) Or otherwise this has always been lli's responsibility and it broke silently because the feature wasn't used widely anyway.

I think version (1) is more likely, so I will go find a lldb-5.0 and test debugging with top-of-tree lli.

weliveindetail commented 4 years ago

Some more investigation showed that the DWARF has some corrupt data. All fields of type DW_FORM_strp have a 0 offset. That prevents functions and compile units from being registered correctly. Fields with other data types look fine (e.g. 32 bit unsigned for DW_AT_language).

In a debug session for the regression test I created in D61611 (https://github.com/llvm/llvm-project/blob/87aa9c9e4d41ed88/lldb/test/Shell/Breakpoint/jitbp_elf.test) I put a breakpoint in DWARFFormValue::AsCString() once the JIT descriptor was registered and managed to twist the relevant fields to their correct values:

As a result the jitbp function was added as a base-name and Symtab parsed of the compile unit's DIEs in detail in order to figure out the source code location.

It looked promising, until ResolveAddressUsingFileSections() failed to map the jitbp's AddressRange to one of the compile unit's sections. The range is DW_AT_low/high_pc == 0/3, which looked reasonable for a section offset. I did not go into more detail here as this is all standard LLDB logic. I would be a little surprised if there is a bug here that kills us. I had expected something like an incorrect type setting similar to D61611.

So, I wonder whether it's really the debugger side that's broken here or if it might be MCJIT. Not sure it's worth the time to investigate that. Maybe I can check again with ORCv2 once we have support for listeners.

lhames commented 5 years ago

This is exciting. Thanks Stefan!

I tried to fix source-level debugging in RuntimeDyldMachO a while back but was not able to wrap my head around the LLDB side of the change (MacOS had been explicitly disabled there) in the time I had. If/when you get this running I would love to go over the protocol with you: hopefully I can write a jit-debugging plugin for JITLink.

weliveindetail commented 5 years ago

https://reviews.llvm.org/D61611 did a first step by fixing the "sending the breakpoint request" error. Now LLDB again manages to implement breakpoint sites in JITed code.

The next part of the regression is that we only see disassembly, while LLDB 5.0 was successfully loading and showing the respective source lines.

In a last step we should update the documentation: http://llvm.org/docs/DebuggingJITedCode.html

weliveindetail commented 5 years ago

Reproducible with current LLDB master, which will be 9.0, e.g. running this docker container (with extra params to allow debugging):

$ docker run --cap-add=SYS_PTRACE --security-opt seccomp=unconfined --security-opt apparmor=unconfined weliveindetail/lldb-bug36209:1dbd42ab

Image runs Ubuntu 18.04 and LLDB built from https://github.com/llvm/llvm-project/commit/1dbd42ab.