LLDB Error: Unable to resolve breakpoint to any actual locations - macos

I'm trying to use LLDB (because I apparently can't use gdb anymore) to debug som of my code and each time I try to...
(lldb) breakpoint set -f file.c -l 65
I get...
Breakpoint 1: no locations (pending)
WARNING: Unable to resolve breakpoint to any actual locations.
I've tried different things like assigning the breakpoint to a function and such but I always get the same error. When running there's no break. Please help!

lldb: resolving breakpoints to locations
If your out file doesn't have debugging symbols enabled for Code Generation Options then breakpoints likely can't be resolved to locations within your .c source file.
When you create your out file enable debug information:
$ clang -g -O0 file.c -o file
$ lldb file
(lldb) target create "file"
Current executable set to 'file' (x86_64).
(lldb) b file.c:13
Breakpoint 1: where = file`main + 29 at file.c:13, address = 0x0000000100000f4d
Using the -g option adds the necessary debug information to your file for lldb. It should now resolve when you breakpoint set -f file.c -l n (which can be abbreviated as b file.c:n).
-g Generate debug information. Note that Clang debug information
works best at -O0.

Related

Why is it that assembling/linking in one step loses debug info for my assembly source?

When I build my source code using two steps:
localhost % clang -g -c factorial.s
localhost % clang -o factorial factorial.o
I get debug info about the assembly source.
localhost % lldb factorial
(lldb) target create "factorial"
Current executable set to '/Users/chris/Dev/assembly/learning-assembly/chapter11/factorial' (x86_64).
(lldb) source info -f factorial.s
Lines found for file factorial.s in compilation unit factorial.s in `factorial
[0x0000000100003f89-0x0000000100003f8b): /Users/chris/Dev/assembly/learning-assembly/chapter11/factorial.s:10
[0x0000000100003f8b-0x0000000100003f8d): /Users/chris/Dev/assembly/learning-assembly/chapter11/factorial.s:13
[0x0000000100003f92-0x0000000100003f93): /Users/chris/Dev/assembly/learning-assembly/chapter11/factorial.s:15
[0x0000000100003f93-0x0000000100003f95): /Users/chris/Dev/assembly/learning-assembly/chapter11/factorial.s:16
[0x0000000100003f95-0x0000000100003f9c): /Users/chris/Dev/assembly/learning-assembly/chapter11/factorial.s:19
[0x0000000100003f9c-0x0000000100003f9d): /Users/chris/Dev/assembly/learning-assembly/chapter11/factorial.s:22
[0x0000000100003f9d-0x0000000100003fa1): /Users/chris/Dev/assembly/learning-assembly/chapter11/factorial.s:25
[0x0000000100003fa1-0x0000000100003fa7): /Users/chris/Dev/assembly/learning-assembly/chapter11/factorial.s:26
[0x0000000100003fa7-0x0000000100003faa): /Users/chris/Dev/assembly/learning-assembly/chapter11/factorial.s:29
[0x0000000100003faa-0x0000000100003fac): /Users/chris/Dev/assembly/learning-assembly/chapter11/factorial.s:30
[0x0000000100003fac-0x0000000100003faf): /Users/chris/Dev/assembly/learning-assembly/chapter11/factorial.s:33
[0x0000000100003faf-0x0000000100003fb6): /Users/chris/Dev/assembly/learning-assembly/chapter11/factorial.s:34
[0x0000000100003fb6-0x0000000100003fb8): /Users/chris/Dev/assembly/learning-assembly/chapter11/factorial.s:35
(lldb)
If I do this in one step I don't:
localhost % clang -g -o factorial factorial.s
localhost % lldb factorial
(lldb) target create "factorial"
Current executable set to '/Users/chris/Dev/assembly/learning-assembly/chapter11/factorial' (x86_64).
(lldb) source info -f factorial.s
error: No source filenames matched 'factorial.s'.
(lldb)
In another example, I did the one step build from source of a main.c along with function.s. In that case lldb knew about the main.c file, but not the assembly file.
Is there an option when building in one step with clang to get it to give me dwarf info for the assembly too?
This answer is the same as the one buried in a comment, but I thought it would be easier to find it here.
TL;DR — .o debug info + executable debug map is supposed to be the same as dSYM bundle, but in the case of assembly it turns out it isn't.
The complete discussion can be found at Why is LLDB not showing debug info for my assembly file?

GDB symbols work for "break" and "print", but "list" fails with "No debugging symbols found"

There are tons of questions and answers about GDB and the "No debugging symbols found" warning, but all of those assume that the debugging symbols are not part of the .elf file.
I'm dealing with the following:
I have a .elf file with debugging symbols. I can verify this by doing objdump, and seeing a disassembly with the subroutine labels being present.
When I load the .elf file, it loads file correctly.
When I then do list to list the C code, I get No symbol table is loaded. Use the "file" command.
However, I can still do things like break main or p/x global_cntr!
When I do file progmem.elf, there's no difference in behavior: I get (No debugging symbols found in progmem.elf), but breakpoints etc still work.
GCC and GDB are using the same version of the GCC toolchain
I tried using -gdwarf-3 instead of -ggdb. No difference.
I'm lost...
I'm using a RISC-V toolchain, if that matters.
Here's an excerpt of my Makefile:
TARGET = $(TOOLS_PREFIX)/riscv32-unknown-elf
AS = $(TARGET)-as
ASFLAGS = -march=$(MARCH) -mabi=ilp32
LD = $(TARGET)-gcc
LDFLAGS = -march=$(MARCH) -g -mabi=ilp32 -Wl,-Tsections.lds,-Map,progmem.map -ffreestanding -nostartfiles -Wl,--no-relax
CC = $(TARGET)-gcc
CFLAGS = -march=$(MARCH) -g -ggdb -mno-div -mabi=ilp32 -Wall -Wextra -pedantic -DCPU_FREQ=$(CPU_FREQ_MHZ)000000 $(CC_OPT)
...
progmem.elf: $(OBJ_FILES) top_defines.h sections.lds Makefile
$(LD) $(LDFLAGS) -o $# $(OBJ_FILES) -lm
And here's a log of my GDB session:
/opt/riscv32im/bin//riscv32-unknown-elf-gdb progmem.elf \
-ex "target remote localhost:3333"
...
Remote debugging using localhost:3333
0x0000002e in rdcycle64 ()
(gdb)
(gdb)
(gdb) monitor soft_reset_halt
requesting target halt and executing a soft reset
(gdb) file progmem.elf
A program is being debugged already.
Are you sure you want to change the file? (y or n) y
Reading symbols from progmem.elf...
(No debugging symbols found in progmem.elf)
(gdb) br main
Breakpoint 1 at 0x5d6
(gdb) load
Loading section .memory, size 0x5790 lma 0x0
Start address 0x0, load size 22416
Transfer rate: 23 KB/sec, 11208 bytes/write.
(gdb) c
Continuing.
Program stopped.
0x000005d6 in main ()
(gdb) p/x global_cntr
$1 = 0x0
(gdb) l
No symbol table is loaded. Use the "file" command.
(gdb)
I have a .elf file with debugging symbols. I can verify this by doing objdump, and seeing a disassembly with the subroutine labels being present.
Debugging symbols are not the same as symbols. For disassembly, you only need the latter. For source listing you need the former.
I can still do things like break main or p/x global_cntr!
These also require only the symbol table.
You can confirm that you don't have debug symbols using objdump -g progmem.elf or readelf -wi progmem.elf.
Your command lines look like debug symbols should be included, but there is no telling what you do with .debug_* sections in your sections.lds linker script. Probably you discard them, which would explain why they aren't there.
Update:
Do you by any chance has an example sections.lds file that has them included?
ld --verbose should print the default linker script. Here is one example.
My original linker script was the following:
SECTIONS {
.memory : {
. = 0x00000;
start*(.text);
*(.text);
*(*);
end = .;
}
}
I suspect that my issue was caused by the catchall *(*); which moved all sections into the .text section.
I replaced it with the following script:
MEMORY
{
ram (ax) : ORIGIN = 0x00000000, LENGTH = 16K
}
SECTIONS {
}
After this, .debug_* symbols are included.
This script should be refined with more precise placement of various sections, but it's good enough to unblock me.

unrecognized -a option `ck_size'

I am using GCC ver-4.6.4 (both in Mac and Linux Mint 15) to compile a code I do for research.
The command I am using is :
gfortran -O2 -fopenmp -Wl,-stack_size,1000000 <...Lots of files...> -o a.out
, where I omit the actual file names.
This code compiles OK in Mac, however I get the following error in Mint:
/usr/bin/ld: unrecognized -a option `ck_size'
collect2: error: ld returned 1 exit status
make[1]: *** [a.out] Error 1
In Mint, this will compile if I do not use any flags at all, therefore this problem is related to OpenMP.
However, I do need OpenMP and do not understand what it says in the error, because I do not have 'ck_size'. BTW, deleting -O2 doesn't help.
The problem is not related to OpenMP, it is related to your different OS's.
-stack_size is specific to Macintosh and refers to the maximal size of arrays on the stack. Linux changes the stack size via the terminal command ulimit (to check your Mint settings type, ulimit -a to see everything, the stack size can be seen with ulimit -s, see the ulimit man page for more information).
Thus, you need to scrap that whole -stack_size,100000 portion from your compiler flag, it means nothing in Linux.
You passed -Wl,-stack_size,1000000 to gfortran, which is passing on the option "-stack_size 1000000" to the linker ld. It is interpreting "st" as single letter options "-s" and "-t", then reading the next letter as an option "-a", and the rest of the word ("ck_size") as its parameter.
I could find no reference to a -stack_size option for ld. It looks like the option is --stack, so you need to put something like -Wl,--stack,1000000 instead.

LLDB equivalent of gdb "directory" command for specifying source search path?

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!
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..
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).

Details on gdb memory access complaint

I have an object file compiled using as (from assembler code).
If I link it using ld, when I try to stepi (or nexti) gdb complains about memory access at address 0x0. If I link it using gcc, all is fine.
I am guessing the problem is caused by ld, which produces fewer sections when compared to the linking result of gcc.
Is there a way to configure gdb to be more verbose so I can maybe figure out what's wrong with the executable?
(gdb) b main
Breakpoint 1 at 0x100000f8e
(gdb) r
Breakpoint 1, 0x0000000100000f8e in main ()
(gdb) x/10i $pc
0x100000f8e <main>: fbld 0x6c(%rip) # 0x100001000 <data1>
0x100000f94 <main+6>: fimul 0x7a(%rip) # 0x100001014 <data2>
0x100000f9a <main+12>: fbstp 0x60(%rip) # 0x100001000 <data1>
0x100000fa0 <main+18>: mov0x0 $0x2000001,%rax
0x100000fa7 <main+25>: mov $,%rdi
0x100000fae <main+32>: syscall
(gdb) si
Cannot access memory at address 0x0
0x0000000100000f94 in main ()
PS: The executable itself runs as expected in both versions.
Later edit: commands i've used to compile:
as -arch x86_64 src.s -o src.o
ld -e _main -arch x86_64 src.o -o src
gcc -o src src.o
gdb has a "show debug" command, giving various internal debug settings. E.g. "set debug target 1" will turn on tracing for gdb's interaction with the target process. You might want to experiment with every flag they have (there aren't that many).
GCC doesn't actually do the linking, it just calls ld on your behalf. The options it's providing must be different from the ones you are using.
Per this thread:
How to get GCC linker command?
You should be able to see the ld invocation's command line by running gcc -v.
That should tell you how to modify your ld command line so things work for you.

Resources