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

HP-UX Handbook Rev 13.00 Page 56 (of 101)
Chapter 11 Software Development
October 29, 2013
gdb program [ core | PID ]
It will print a startup message load the specified program. If a core file name is added, it will be
loaded too, and the location of the program abort is printed. When adding a process ID, gdb(1)
will attach to this process, stop it and print the location where it was stopped. Then the (gdb)
prompt is printed to wait for user input.
When debugging a core file, it is a good practice to give the core file another name than “core”.
Else, whenever some other process aborts with a core, the core file to debug is lost.
Programs should be built with the -g compiler option if there is a need for debugging. This
causes the compilers to add source line and file references to the objects that make debugging
much easier. The debugger will then load the source files and show the appropriate code. If no
debug information is in the executable, there is no way to find out which machine instruction
belongs to which line of the source code. While this makes debugging harder, there is still a lot
of helpful information available to track down a problem.
It becomes even more difficult if the program has been stripped (see man strip(1)). This will
remove even symbol information, so an address in the program cannot be associated to a
function name anymore, thus making debugging nearly impossible.
A list of the most common debugger commands can be found in the man page. gdb(1) also
provides an online help facility. Simply type help at the (gdb) prompt and follow the
instructions. Further documentation including a tutorial can be found on the HP WDB website
[1]..
Debugging is a very complex work and cannot be discussed in too much detail here. However, a
few simple things shall be explained. Let's debug our HelloWorld program (don't forget to
recompile/relink with -g):
$ gdb HelloWorld
Copyright 1986 - 2001 Free Software Foundation, Inc. Hewlett-Packard
Wildebeest 3.0.01 (based on GDB) is covered by the GNU General Public
License. Type "show copying" to see the conditions to change it and/or
distribute copies. Type "show warranty" for warranty/support.
..
(gdb) list
1 #include <stdio.h>
2
3 int main() {
4 printf("Hello World!\n");
5 }
(gdb) break main
Breakpoint 1 at 0x2754: file HelloWorld.c, line 4.
(gdb) run