23

Looking for the lldb equivalent of the gdb "directory" command to add search paths for finding missing source code (or possibly similar functionality within xcode)?

Thanks in advance!

IODEV
  • 1,706
  • 2
  • 17
  • 20

2 Answers2

34

The target.source-map setting allows you define a series of a => b path remappings in the debug session. It's not identical to the gdb dir command, which is a list of directories to search for source files by base name, but you can solve the same problems with source-map. Here's an example where I move a source file to a hidden directory after compiling:

% cd /tmp
% echo 'int main () { }' > a.c
% clang -g a.c
% mkdir hide
% mv a.c hide/
% xcrun lldb a.out
(lldb) settings set target.source-map /tmp /tmp/hide
(lldb) l -f a.c
   1    int main () { }
(lldb) br se -n main
Breakpoint created: 1: name = 'main', locations = 1
(lldb) r
Process 21674 launched: '/private/tmp/a.out' (x86_64)
Process 21674 stopped
* thread #1: tid = 0x1f03, 0x0000000100000f49 a.out`main + 9 at a.c:1, stop reason = breakpoint 1.1
    #0: 0x0000000100000f49 a.out`main + 9 at a.c:1
-> 1    int main () { }
(lldb) 

For more information about this setting, type set list target.source-map in lldb. fwiw you might have discovered this in lldb by doing apropos path which will list all commands/settings that have the word path in the name/description. Seeing that there was a setting by this name, you'd do settings list to see the list of settings and find out that it's filed under target..

Jason Molenda
  • 14,835
  • 1
  • 59
  • 61
  • Jason, thanks for clarify the usage and supplying a good self-explanatory example! ("A picture is worth a thousand words" ;-) Hope you don't mind using this example as a suggestion to update the "GDB TO LLDB COMMAND MAP" (http://lldb.llvm.org/lldb-gdb.html). /Regards, Lars. – IODEV Oct 20 '12 at 19:17
  • 1
    Yeah, the only problem is that target.source-map isn't really the same thing as dir; it's equivalent to gdb's pathname-substitutions setting. pathname-substitutions was used by Xcode behind the scenes but users were not normally aware of it. We'll need to add a command similar to gdb's dir command to lldb at some point. – Jason Molenda Oct 21 '12 at 21:41
  • 3
    About the only thing I miss from this answer, would be how to find what source path was used when building it. If I connect to a remote machine, I often don't know what path was used, I hope can ask lldb to show me in what path it wants to find the source files, so I can source-map it correctly. – lundman Dec 19 '14 at 02:49
  • 9
    Easiest way: use `image lookup --verbose --address $pc` (or `im loo -va $pc` to get all the information about the place where you're currently executing. You can give `image lookup` a function name or whatever, too. You can use the python scripting interface to get the current stack frame's filename too with a line like `script print lldb.frame.GetCompileUnit().GetFileSpec()`. – Jason Molenda Dec 21 '14 at 20:42
  • 4
    If lldb can't find the file I need (e.g., `l foo.cc:1` says `error: Could not find source file "foo.cc".`) then what? gdb's `directory` made it easy to say *look here!*, but in lldb there doesn't seem to be a solution unless you know where it was trying to find the file so you can give the proper argument to `target.source-map`, but it doesn't tell you that either. – George Oct 26 '16 at 19:53
  • 1
    Use `source info` – sourcedelica May 11 '17 at 01:17
  • That is, navigate to the frame with the file you want to map and run `source info`. It will give you the full pathname of the original source file. – sourcedelica May 11 '17 at 01:32
  • This is a bug because it doesn't work if the build files do no longer exist but a copy of the source is available. – rwst May 07 '18 at 06:58
  • ```(lldb) source info error: No debug info for the selected frame. ``` Where does one go from here? – Graham Leggett May 28 '20 at 00:51
  • There doesn't seem to be any way to make this work if the binary includes relative paths. – tbodt Jun 05 '20 at 20:24
-2

The problem with lldb not being able to find your source files may be caused by flawed compilation process - i just spent several hours in attempt to find a lldb command to set path to sources by force but ended up discovering that i performed both actual compiling and linking with identical set of flags (-Wall -Werror -Wextra -g) in my Makefile... So compiler worked without warning and error messages despite errors (or warning treated as errors) actually existed. Fixing them fixed lldb workflow. Maybe developers should consider adding some warning (for newbies like me) in case program wasn't able to find sources (they were located in the very same directory in src folder).

Kamil.S
  • 5,205
  • 2
  • 22
  • 51