The GNU debugger (gdb) allows you to examine the internals of another program while the program executes or retrospectively to see what a program was doing at the moment that it crashed.
The gdb allows you to examine and control the execution of code and is useful for evaluating the causes of crashes or general incorrect behavior. gdb does not handle Java™ processes, so it is of limited use on a pure Java program. It is useful for debugging native libraries and the JVM itself.
You can run gdb in three ways:
export IBM_JVM_DEBUG_PROG=gdb javaThen you receive a gdb prompt, and you supply the run command and the Java arguments:
r <java_arguments>
gdb <Java Executable> <PID>
When gdb is attached to a running program, this program is halted and its position in the code is displayed for the viewer. The program is then under the control of gdb and you can start to issue commands to set and view the variables and generally control the execution of the code.
gdb <Java Executable> <system dump>
When you run gdb against a system dump, it initially shows information such as the termination signal the program received, the function that was executing at the time, and even the line of code that generated the fault.
When a program comes under the control of gdb, a welcome message is displayed followed by a prompt (gdb). The program is now waiting for you to enter instructions. For each instruction, the program continues in whichever way you choose.
Breakpoints can be set for a particular line or function using the command:
break linenumber
or
break functionName
After you have set a breakpoint, use the continue command to allow the program to execute until it reaches a breakpoint.
Set breakpoints using conditionals so that the program halts only when the specified condition is reached. For example, using breakpoint 39 if var == value causes the program to halt when it reaches line 39, but only if the variable is equal to the specified value.
If you want to know where as well as when a variable became a certain value you can use a watchpoint. Set the watchpoint when the variable in question is in scope. After doing so, you will be alerted whenever this variable attains the specified value. The syntax of the command is: watch var == value.
To see which breakpoints and watchpoints are set, use the info command:
info break info watch
handle sig32 pass nostop noprint handle sigusr2 pass nostop noprint
#6 0x804c4d8 in myFunction () at myApplication.c
To view more detailed information about a function frame, use the frame command along with a parameter specifying the frame number. After you have selected a frame, you can display its variables using the command print var.
Use the print command to change the value of a variable; for example, print var = newValue.
The info locals command displays the values of all local variables in the selected function.
To follow the exact sequence of execution of your program, use the step and next commands. Both commands take an optional parameter specifying the number of lines to execute. However, next treats function calls as a single line of execution, while step progresses through each line of the called function, one step at a time.
When you have finished debugging your code, the run command causes the program to run through to its end or its crash point. The quit command is used to exit gdb.
Other useful commands are: