Development/GDB: Difference between revisions
No edit summary |
S Richling (talk | contribs) |
||
(49 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
{{Softwarepage|devel/gdb}} |
|||
{| style="border-style: solid; border-width: 1px" |
|||
! Navigation: [[BwHPC_Best_Practices_Repository|bwHPC BPR]] |
|||
|} |
|||
{| width=600px class="wikitable" |
|||
== Parallel Debugger ddt == |
|||
{| style="width:100%; vertical-align:top; background:#f5fffa;border:1px solid #e7aa01;" |
|||
| style="padding:2px;" | <div style="margin:3px; background:#cef2e0; font-size:120%; font-weight:bold; border:1px solid #e7aa01; text-align:left; color:#000; padding:0.2em 0.4em;">Currently only for employees of KIT</div> |
|||
|- |
|- |
||
! Description !! Content |
|||
| style="color:#000;" | <div style="padding:2px 5px">On bwUniCluster the GUI based distributed debugging tool (ddt) may be used to debug serial as |
|||
|- |
|||
well as parallel applications. For serial applications also the GNU gdb or Intel idb debugger |
|||
| module load |
|||
may be used. The Intel idb comes with the compiler and information on this tool is available |
|||
| devel/gdb |
|||
together with the compiler documentation. In order to debug your program it must be |
|||
|- |
|||
compiled and linked using the -g compiler option. This will force the compiler to add additional information to the object code which is used by the debugger at runtime. |
|||
| License |
|||
</div> |
|||
| [http://www.gnu.org/licenses/ GPL] |
|||
|- |
|||
|Citing |
|||
| n/a |
|||
|- |
|||
| Links |
|||
| [http://www.gnu.org/software/gdb/ Homepage] | [http://www.gnu.org/software/gdb/documentation/ Documentation] | [https://sourceware.org/gdb/wiki/ Wiki] | [http://www.gnu.org/software/gdb/mailing-lists/ Mailinglists] |
|||
|- |
|||
| Graphical Interface |
|||
| No |
|||
|- |
|||
| Included modules |
|||
| icc | icpc | ifort | idb |
|||
|} |
|} |
||
<br> |
|||
= Description = |
|||
The '''GNU Debugger (GDB)''' is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. In the past Intel supported their own '''idb''' debugger, however this has been deprecated in favor of their own port called <kbd>gdb-ia</kbd>. |
|||
<br> |
|||
= Basic commands = |
|||
The code you want to debug should be compiled with the <kbd><font color=green>-g</font></kbd> option. If the optimization flag is not set, GCC will still do some basic optimization, like dead-code elimination or reorder instruction execution obfuscating the order when debugging. Therefore, it is recommended to turn off optimization explicitly with the <font color=green>-O0</font> parameter for debugging. To start a debug session for a program execute GDB with the program path as parameter: |
|||
<pre>$ gdb ./example</pre> |
|||
Inside GDB is a prompt where you can enter commands. Important commands are listed below. |
|||
{| width=600px class="wikitable" |
|||
ddt consists of a graphical frontend and a backend serial debugger which controls the |
|||
|- |
|||
application program. One instance of the serial debugger controls one MPI process. Via the |
|||
! Command |
|||
frontend the user interacts with the debugger to select the program that will be debugged, |
|||
! Description |
|||
to specify different options and to monitor the execution of the program. Debugging |
|||
|- |
|||
commands may be sent to one, all or a subset of the MPI processes. |
|||
| help ''cmd'' |
|||
| Show help for command ''cmd''. |
|||
Before the parallel debugger ddt can be used, it is necessary to load the corresponding |
|||
|- |
|||
module file: |
|||
| break ''func'' |
|||
<pre> |
|||
| Set a breakpoint at function ''func''. |
|||
$ module use /opt/bwhpc/ka/modulefiles (only available for employees of KIT) |
|||
|- |
|||
$ module add debugger/ddt |
|||
| run |
|||
</pre> |
|||
| Start program. |
|||
|- |
|||
Now ddt may be started with the command |
|||
| next |
|||
<pre> |
|||
| Go to next program line. Do not enter functions. |
|||
$ ddt program |
|||
|- |
|||
</pre> |
|||
| step |
|||
| Go to next program line. Enter functions. |
|||
|- |
|||
| list |
|||
[[File:ddt1_750.jpg]] |
|||
| Show the surrounding source code of the currently processed line. |
|||
|- |
|||
Figure: DDT startup window |
|||
| print ''expr'' |
|||
| Print the value of the expression ''expr''. |
|||
|- |
|||
| display ''expr'' |
|||
| Display the value of the expression ''expr'' every time the program stops. |
|||
|- |
|||
| watch ''expr'' |
|||
| Stop when value of the expression ''expr'' changes. |
|||
|- |
|||
| continue |
|||
| Continue execution until a breakpoint or a watchpoint appears. |
|||
|- |
|||
| backtrace |
|||
| Print a list of functions that are currently active. |
|||
|- |
|||
| quit |
|||
| Exit GDB. |
|||
|} |
|||
The above figure shows ddt’s startup window. Before actually starting the debugging session |
|||
you should check the contents of several fields in this window: |
|||
1. The top line shows the executable file that will be run under control of the debugger. In |
|||
the following lines you may input some options that are passed to your program or to the |
|||
MPI environment. |
|||
= Branch record tracing = |
|||
2. If your program reads data from stdin you can specify an input file in the startup window. |
|||
Starting with GBD-10.1, the debugger has been installed with Intel Processor Trace [https://github.com/intel/libipt libipt], allowing recording and replaying of process state. |
|||
This allows disassembling previously executed instructions, checking for previously called functions and branch tracing. |
|||
Honestly, Segmentation Violations are better caught using [[Development/Valgrind|Valgrind]]. However in this case, |
|||
3. Before starting an MPI program you should check that "Open MPI (Compatability)" or |
|||
<kbd>valgrind</kbd> would ''not'' have helped: this loops overwrites <kbd>v</kbd> an |
|||
"Intel MPI" is the MPI implementation that has been selected. If this is not the case, you |
|||
array of 2 ints on the stack and the return address leading to the execution of IP <kbd>0x07</kbd>. |
|||
have to change this. Otherwise ddt may not be able to run your program. In order to debug |
|||
serial programs, the selected MPI implementation should be "none". You may also change |
|||
the underlying serial debugger using the "change" button. By default ddt uses its own serial debugger, but it may also use the Intel idb debugger. |
|||
More information is available in [https://sourceware.org/gdb/current/onlinedocs/gdb/Process-Record-and-Replay.html#Process-Record-and-Replay gdb's feature documentation] |
|||
4. Select the number of MPI processes that will be started by ddt. If you are using ddt within |
|||
a batch job, replace mpirun by ddt in the command line of ????? and make sure that the |
|||
chosen number of MPI processes is identical to the number of MPI tasks (-p option ???) that |
|||
you selected with the ?????? command. When you debug a serial program, select 1. |
|||
<br> |
|||
5. After you have checked all inputs in the ddt startup window, you can start the debugging |
|||
session by pressing the "run" button. |
|||
= Core dumps = |
|||
When the program crashes, a log file (called core dump) can be created which contains the state of the program when it crashed. This is turned off by default because these core dumps can get quite large. If you want to turn it on you have to change your ulimits, for example: |
|||
<pre>$ ulimit -c unlimited</pre> |
|||
Every time your program crashes a new file called core.xxx (where xxx is a number) will be created in the directory from which you started the executable. You can call gdb to examine your core dump using the following command (assuming your program is called ex): |
|||
<pre>$ gdb ./ex core.xxx</pre> |
|||
Now you can print a backtrace to check in which function the error happened and what values the parameters had. Additionally you can examine the values of your variables to reproduce the error. |
|||
<br> |
|||
The ddt window now shows the source code of the program that is being debugged and breakpoints can be set by just pointing to the corresponding line and pressing the right |
|||
mouse button. So you may step through your program, display the values of variables |
|||
and arrays and look at the message queues. |
|||
= Multithreaded debugging = |
|||
[[File:ddt2_750.jpg]] |
|||
GDB can also be useful for multithreaded applications for example when OpenMP was used. By going through each thread separately you can better see what is really going on and you can check the computation step by step. |
|||
The following commands are useful for multithreaded debugging: |
Latest revision as of 00:49, 9 December 2022
The main documentation is available via |
Description | Content |
---|---|
module load | devel/gdb |
License | GPL |
Citing | n/a |
Links | Homepage | Documentation | Wiki | Mailinglists |
Graphical Interface | No |
Included modules | icc | icpc | ifort | idb |
Description
The GNU Debugger (GDB) is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. In the past Intel supported their own idb debugger, however this has been deprecated in favor of their own port called gdb-ia.
Basic commands
The code you want to debug should be compiled with the -g option. If the optimization flag is not set, GCC will still do some basic optimization, like dead-code elimination or reorder instruction execution obfuscating the order when debugging. Therefore, it is recommended to turn off optimization explicitly with the -O0 parameter for debugging. To start a debug session for a program execute GDB with the program path as parameter:
$ gdb ./example
Inside GDB is a prompt where you can enter commands. Important commands are listed below.
Command | Description |
---|---|
help cmd | Show help for command cmd. |
break func | Set a breakpoint at function func. |
run | Start program. |
next | Go to next program line. Do not enter functions. |
step | Go to next program line. Enter functions. |
list | Show the surrounding source code of the currently processed line. |
print expr | Print the value of the expression expr. |
display expr | Display the value of the expression expr every time the program stops. |
watch expr | Stop when value of the expression expr changes. |
continue | Continue execution until a breakpoint or a watchpoint appears. |
backtrace | Print a list of functions that are currently active. |
quit | Exit GDB. |
Branch record tracing
Starting with GBD-10.1, the debugger has been installed with Intel Processor Trace libipt, allowing recording and replaying of process state. This allows disassembling previously executed instructions, checking for previously called functions and branch tracing.
Honestly, Segmentation Violations are better caught using Valgrind. However in this case, valgrind would not have helped: this loops overwrites v an array of 2 ints on the stack and the return address leading to the execution of IP 0x07.
More information is available in gdb's feature documentation
Core dumps
When the program crashes, a log file (called core dump) can be created which contains the state of the program when it crashed. This is turned off by default because these core dumps can get quite large. If you want to turn it on you have to change your ulimits, for example:
$ ulimit -c unlimited
Every time your program crashes a new file called core.xxx (where xxx is a number) will be created in the directory from which you started the executable. You can call gdb to examine your core dump using the following command (assuming your program is called ex):
$ gdb ./ex core.xxx
Now you can print a backtrace to check in which function the error happened and what values the parameters had. Additionally you can examine the values of your variables to reproduce the error.
Multithreaded debugging
GDB can also be useful for multithreaded applications for example when OpenMP was used. By going through each thread separately you can better see what is really going on and you can check the computation step by step. The following commands are useful for multithreaded debugging: