I have 3 answers (so far) and analysis of a specific case when I needed to know how to do this
addr2line can do this if the compiler generates debugging information compatible with the tool.
~ cat asdf.txt /var/tmp/ccITB4j2.o: In function `main': /var/tmp/ccITB4j2.o(.text+0x4): undefined reference to `myFunction(void)' ~ cat asdf.txt | addr2line -e /var/tmp/ccITB4j2.o ... it should print src:line info here
Then there is objdump :
objdump --dwarf=decodedline test.o test.o: file format elf64-x86-64 Decoded dump of debug contents of section .debug_line: CU: test.cc: File name Line number Starting address test.cc 2 0x23 test.cc 3 0x84
In response to the question "how does the debugger do this", here is a good article on the topic: How debuggers work: Part 3 - Debugging information , where the objdump option comes from.
I will apologize in advance for future generations if the connection breaks.
I ran into a problem on these lines with Solaris Studio 12.3 on Linux. It seems that the information it generates (in .debug_line or some other section) debugs information that is incompatible with addr2line , but only when building with optimization. The following code will cause a similar link error:
~ cat test.cc struct Test {int x; Test(); }; inline void test() { Test *t = new Test(); } void blah() { test(); } ~ CC -g -Kpic test.cc -shared -o libtest.so -Wl,--unresolved-symbols=ignore-in-shared-libs (...) test.cc:2: undefined reference to `void operator delete(void*)' (...) ~ CC -g -Kpic test.cc -shared -o libtest.so -Wl,--unresolved-symbols=ignore-in-shared-libs -O0 (...) test.cc:(.text+0x45): undefined reference to `void operator delete(void*)' (...) ~ CC -g -Kpic test.cc -c ~ addr2line -e test.o +0x45 test.cc:2 ~ CC -g -Kpic test.cc -O0 -c ~ addr2line -e test.o +0x45 ??:?
Resolving a communication error for this case requires linking to the libCrun compiler libCrun . As a counter-example, for the employer's comments that it is useless to know the line numbers: I, of course, had to scratch my head about where delete referenced. As it turned out, the compiler inserts additional code that selects the material and removes it. If he correctly printed the line number (tight binding of the function), it would be much more obvious that the compiler was doing something unusual.
source share