[Linux] Add support for GDB JIT interface to display source lines on break
The recent versions of both GDB and LLDB support JIT compilation interface. In essence it's an in memory representation of object file with corresponding debugging information. When such information contains source line mapping to jitted code address, the debugger displays the source on breakpoint hit instead of disassembled instructions. There is good overview of this feature in LLVM documentation Debugging JIT-ed Code With GDB . So all we need on Linux is the VM to generate in memory representation of ELF covering the jitted code with in memory DWARF structures (describing at least source line mapping) for the same code. As long as VM generates jitted code on by method basis so these structures also should be generated on by method basis. We plan to implement this feature, however there are couple of open questions:
- As far as I understand currently JIT notification (
DACNotify::DoJITNotification) is only generated when debugger subscribed to it. How can I generate GDB JIT notification regardless of the presence of debugger? It's very cheap (one call to empty function). - How can I obtain generated code size from MethodDesc (something similar to MethodDesc::GetNativeCode which returns address of generated code)?
@mikem8361 @janvorli
cc @lucenticus @chunseoklee @seanshpark
I'm working with @Dmitri-Botcharnikov on this feature (displaying of managed source code in lldb+sosplugin on breakpoints) and trying to read portable PDB for correct mapping from native offsets to source lines. It seems that we need to read symbols inside debuggee process for correct DWARF generation. I'm not sure that approach which we used in https://github.com/dotnet/coreclr/issues/4798 is applicable for this. Could you suggest the best way to load System.Diagnostics.Debug.SymbolReader.dll into debuggee process?
I successfully load SymbolReader to debuggee process by using coreclr_create_delegate in src/coreclr/hosts/unixcoreruncommon/coreruncommon.cpp, but I have no idea how to get mapping from native to IL offsets inside debuggee process.
/cc @jkotas
I'm not sure if this is a good idea in general. This approach does change/affect the debuggee process quite a bit and we usually try not to alter the debuggee state. All of the services that SOS and the managed debuggers you need are available from the DAC interfaces but only out of context in another process. From what I understand from the JIT'ed code links above, the JIT would have to generate an ELF compatible object file, etc. in memory for each method which seems like a lot of work and would increase the size of target/debuggee process's memory footprint.
That being said all the functions to do what you need to do are there in the VM code in the coreclr (even what the hosting apis call to implement coreclr_create_delegate). I couldn't help with the details; I really don't know the JIT and VM core that well.
I agree that lldb/gdb jit interface would have too much overhead and/or it would be too invasive. It is not something that can be enabled by default, enabling it optionally under switch may be ok. Either way, it is still a lot of work.
gdb has alternative mechanism for this: https://sourceware.org/gdb/current/onlinedocs/gdb/Custom-Debug-Info.html#Custom-Debug-Info It has problems too: it is gdb specific solution, and the debug info readers have to be released under gpl.
FYI: https://github.com/v8/v8/wiki/GDB-JIT-Interface#known-limitations Nevertheless, it is worth a try before we get the better solution. :)
@Dmitri-Botcharnikov I have one question. LLDB supports jit interface ? I cannot find any reference which describes such feature of LLDB. If LLDB does not support jit interface, we need to execute gdb client ?
@chunseoklee LLDB supports this feature, you can reproduce an example http://llvm.org/docs/DebuggingJITedCode.html with LLDB 3.8 at least on Linux/x64
@Dmitri-Botcharnikov http://reviews.llvm.org/rL202956 this patch is what I look for. Thank you.
We have prepared initial implementation (see PR above) and tested on Linux/x64 and Linux/ARM. The feature is enabled by providing -DFEATURE_GDBJIT argument to cmake. Now we generate only source line info: it's about 1k on x64 and about 700 bytes on ARM per method. It also take several seconds on ODROID board.
The GDB JIT provides handler to discard unnecessary debug information if jitted code has been dropped. Is it right that jit dropping is not supported in current version of CoreCLR?
@Dmitri-Botcharnikov Have you tested this feature on arm/linux(softfp) ?
@chunseoklee Yes, we've tested on arm softfp. There is issue with bpmd filename:line on this platform at the moment, but otherwise it works.
What kind of issue ? Actually, on my arm32 linux(softfp) bpmd with line number does not work now. Do you have any idea?
For this kind of bpmd notification from VM to Strike that method is jitted is either not generated or lost.
@Dmitri-Botcharnikov It happens at random or we can reproduce explitcitly ? In my setting, it seems not working always.
Yes, it's not working always.
In the current implementation of this feature we need set up managed breakpoint to enable JIT notification. I found that bpmd command set up notification CLRDATA_METHNOTIFY_GENERATED. To get rid of setting managed breakpoint we have to set CLRDATA_METHNOTIFY_GENERATED for specific methods in debuggee assembly. We don't need setting JIT notification for system libraries, we need user code only. Is it possible to distinguish debuggee assembly and system libraries?
There is no distinction between system libraries and user code.
@chunseoklee @seanshpark @lucenticus I have found the cause of issue with bpmd file:line on Linux/ARM. Please see PR dotnet/corefx#10145