HP-UX HB v13.00 Ch-11 - Software Development

HP-UX Handbook Rev 13.00 Page 58 (of 101)
Chapter 11 Software Development
October 29, 2013
Breakpoint 2, 0x7f7a5378 in printf () from /usr/lib/libc.2
(gdb)
Remember that printf(3S) is in libc which has no debug info. That's why gdb can only
determine the code address of the breakpoint, and not the source file and line number. When re-
running the program, it stops again in main() because gdb keeps breakpoints until they are
unset, another program is loaded or gdb exits. After continuing, gdb stops the program at the first
instruction of printf(). Although we have no sources for this function we could debug it, but
only at assembly level, which is beyond the scope of this workshop. We could do a disas now to
take a look at the assembly instructions of printf().
If the program has stopped somewhere and we want to know the current location, the procedure
backtrace gives an orientation. If the program is multithreaded, make sure to get the stack trace
from the correct thread. Therefore, before getting a stacktrace, always verify which thread you
look at, and switch to the right one if necessary:
(gdb) info threads
* 1 system thread 3785 0x7f7a5378 in printf () from /usr/lib/libc.2
(gdb) thread 1
[Current thread is already 1 (system thread 3785)]
(gdb) bt
#0 0x7f7a5378 in printf () from /usr/lib/libc.2
#1 0x2768 in main () at HelloWorld.c:4
(gdb)
In the above case we have only one thread. If there are multiple threads, the "info threads"
command lists them all and marks the active thread with an asterisk.
Each line of the trace represents a procedure frame, which tells you the current address within
the function, and in which part of the process it is. With debug information it also prints source
file and line number. The address in frame #0 is the location where the program stopped, or in
case of a core file, where it aborted. For every other frame it shows the address of the instruction
to be executed next, if the program returned to this frame.
The backtrace from the HelloWorld program reads as follows:
Frame #0: The program stopped at address 0x7f7a5378 in printf(3S), which is in
/usr/lib/libc.2.
Frame #1: printf(3S) in has been called from main() on line 4 in HelloWorld.c.
When returning from printf(3S), the program would continue with function main() at
address 0x2768.