<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.bwhpc.de/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=R+Doros</id>
	<title>bwHPC Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.bwhpc.de/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=R+Doros"/>
	<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/e/Special:Contributions/R_Doros"/>
	<updated>2026-04-15T23:55:30Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.17</generator>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=3789</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=3789"/>
		<updated>2016-02-24T08:49:12Z</updated>

		<summary type="html">&lt;p&gt;R Doros: /* Disassembling */ Loop disassembling&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Description !! Content&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] &amp;amp;#124; [[BwForCluster_Chemistry]]&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| n/a&lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [http://www.gnu.org/software/gdb/ Homepage] &amp;amp;#124; [http://www.gnu.org/software/gdb/documentation/  Documentation] &amp;amp;#124; [https://sourceware.org/gdb/wiki/ Wiki] &amp;amp;#124; [http://www.gnu.org/software/gdb/mailing-lists/ Mailinglists]&lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Included modules&lt;br /&gt;
|  icc &amp;amp;#124; icpc &amp;amp;#124; ifort &amp;amp;#124; idb&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Introduction =&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Versions and Availability =&lt;br /&gt;
A list of versions currently available on all bwHPC-C5-Clusters can be obtained from the&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://cis-hpc.uni-konstanz.de/prod.cis/ Cluster Information System CIS]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/big&amp;gt;&lt;br /&gt;
{{#widget:Iframe&lt;br /&gt;
|url=https://cis-hpc.uni-konstanz.de/prod.cis/bwUniCluster/devel/gdb&lt;br /&gt;
|width=99%&lt;br /&gt;
|height=120&lt;br /&gt;
}}&lt;br /&gt;
On the command line interface of any bwHPC cluster you&#039;ll get a list of available versions &lt;br /&gt;
by using the command &#039;&#039;&#039;&#039;module avail devel/gdb&#039;&#039;&#039;&#039;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ : bwUniCluster&lt;br /&gt;
$ module avail devel/gdb&lt;br /&gt;
------------------------ /opt/bwhpc/common/modulefiles -------------------------&lt;br /&gt;
devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font color=red&amp;gt;Attention!&amp;lt;/font&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The default Linux GDB may be a different version.&lt;br /&gt;
Check the &amp;lt;font color=green&amp;gt;default&amp;lt;/font&amp;gt; version by: &#039;&#039;&#039;&#039;gdb --version&#039;&#039;&#039;&#039; &amp;lt;font color=red&amp;gt;without loaded GDB-module&amp;lt;/font&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ module clear                             # clear all loaded modules&lt;br /&gt;
Are you sure you want to clear all loaded modules!? [n] y&lt;br /&gt;
$ gdb --version                            # this is the default version&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-83.el6)&lt;br /&gt;
[...]&lt;br /&gt;
$ module avail devel/gdb                   # available GDB-module&lt;br /&gt;
------------------------ /opt/bwhpc/common/modulefiles -------------------------&lt;br /&gt;
devel/gdb/7.7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&#039;&#039;It is not recommended (or impossible) the debug MPI-based binaries with GDB. You can&#039;t attach to MPI&#039;ed jobs, too.&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Loading =&lt;br /&gt;
It is possible to choose between several versions of GDB. By default both programs are installed. You can check the version which is currently used with:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To use a different version of GCC or GDB, you have to load it through the module system. For example you need GDB 7.7 or higher for GCC version 4.8 and above. In this case load the devel/gdb module.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
How to load the desired version (e.g. GDB version 7.7):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Documentation =&lt;br /&gt;
For &#039;&#039;&#039;online documentation&#039;&#039;&#039; see the links section in the summary table at the top of this page. For &#039;&#039;&#039;local documentation&#039;&#039;&#039; consult the man page.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Basic commands =&lt;br /&gt;
The code you want to debug should be compiled with the &amp;lt;font color=green&amp;gt;-g&amp;lt;/font&amp;gt; option. If the optimization flag is not set, the GCC will still do some basic optimization. Therefore, it is recommended to turn off the optmization explicitly with the &amp;lt;font color=green&amp;gt;-O0&amp;lt;/font&amp;gt; parameter for debugging. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Core dumps =&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Multithreaded debugging =&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called thread_bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pthread_t thread;&lt;br /&gt;
&lt;br /&gt;
void* thread3 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int w[2];&lt;br /&gt;
    int c, l;&lt;br /&gt;
&lt;br /&gt;
    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
        w[l] = c;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void* thread2 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        sleep(4);&lt;br /&gt;
        k = i/(2*2);    /* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main (){&lt;br /&gt;
&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
  &lt;br /&gt;
    //Thread 1&lt;br /&gt;
    int count1 = 0;&lt;br /&gt;
&lt;br /&gt;
    while(count1 &amp;lt; 4000){&lt;br /&gt;
      printf(&amp;quot;Thread 1: %d\n&amp;quot;, count1++);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    pthread_join(thread, NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB thread session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g thread_bug.c -o thread_bug -lpthread&lt;br /&gt;
$ gdb ./thread_bug&lt;br /&gt;
[...]&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break thread3&lt;br /&gt;
Breakpoint 1 at 0x40060c: file thread_bug.c, line 11.&lt;br /&gt;
(gdb) break thread2&lt;br /&gt;
Breakpoint 2 at 0x400650: file thread_bug.c, line 24.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 3 at 0x40069e: file thread_bug.c, line 35.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /tank/home/doros/.t/thread_bug &lt;br /&gt;
[Thread debugging using libthread_db enabled]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 3, main () at thread_bug.c:35&lt;br /&gt;
35	  pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 28260)  main () at thread_bug.c:35&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff7fe3700 (LWP 28303)]&lt;br /&gt;
36	  pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 28303)  thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 28260)  main () at thread_bug.c:36&lt;br /&gt;
(gdb) next&lt;br /&gt;
[Switching to Thread 0x7ffff7fe3700 (LWP 28303)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 2, thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
24	    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
(gdb) next&lt;br /&gt;
25		sleep(4);&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 3 Thread 0x7ffff77e2700 (LWP 28344)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 28303)  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 28260)  0x000000362f8725db in _IO_new_file_overflow () from /lib64/libc.so.6&lt;br /&gt;
(gdb) thread 2&lt;br /&gt;
[Switching to thread 2 (Thread 0x7ffff7fe3700 (LWP 28303))]#0  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
Single stepping until exit from function nanosleep,&lt;br /&gt;
which has no line number information.&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) thread 2&lt;br /&gt;
[Switching to thread 2 (Thread 0x7ffff7fe3700 (LWP 28303))]#0  0x000000362f8acce9 in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
Single stepping until exit from function nanosleep,&lt;br /&gt;
which has no line number information.&lt;br /&gt;
0x000000362f8acb50 in sleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
  3 Thread 0x7ffff77e2700 (LWP 28344)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
* 2 Thread 0x7ffff7fe3700 (LWP 28303)  0x000000362f8acb50 in sleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 28260)  0x000000362f8476f0 in vfprintf () from /lib64/libc.so.6&lt;br /&gt;
(gdb) thread 3&lt;br /&gt;
[Switching to thread 3 (Thread 0x7ffff77e2700 (LWP 28344))]#0  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) next&lt;br /&gt;
12	        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
No symbol &amp;quot;k&amp;quot; in current context.&lt;br /&gt;
(gdb) watch (l &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 4: (l &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Thread 1: 0&lt;br /&gt;
Thread 1: 1&lt;br /&gt;
Thread 1: 2&lt;br /&gt;
Thread 1: 3&lt;br /&gt;
Thread 1: 4&lt;br /&gt;
[...]&lt;br /&gt;
Hardware watchpoint 4: (l &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
thread3 (d=0x0) at thread_bug.c:13&lt;br /&gt;
13	        w[l] = c;&lt;br /&gt;
(gdb) print l&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print c&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Disassembling =&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info functions&lt;br /&gt;
| Shows names and data types of all defined functions.&lt;br /&gt;
|-&lt;br /&gt;
| info line &amp;quot;function&amp;quot;&lt;br /&gt;
| Map source lines to memory adresses (and back).&lt;br /&gt;
|-&lt;br /&gt;
| disassemble &#039;&#039;function&#039;&#039;&lt;br /&gt;
| Disassembles &amp;quot;function&amp;quot; (or a function fragment).&lt;br /&gt;
|}&lt;br /&gt;
&#039;&#039;&#039;Sample GDB disassembling session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
[...]&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;.&lt;br /&gt;
Find the GDB manual and other documentation resources online at:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/documentation/&amp;gt;.&lt;br /&gt;
For help, type &amp;quot;help&amp;quot;.&lt;br /&gt;
Type &amp;quot;apropos word&amp;quot; to search for commands related to &amp;quot;word&amp;quot;...&lt;br /&gt;
Reading symbols from bug...done.&lt;br /&gt;
(gdb) info functions&lt;br /&gt;
All defined functions:&lt;br /&gt;
&lt;br /&gt;
File bug.c:&lt;br /&gt;
void begin();&lt;br /&gt;
void end();&lt;br /&gt;
void loop();&lt;br /&gt;
int main();&lt;br /&gt;
&lt;br /&gt;
Non-debugging symbols:&lt;br /&gt;
0x0000000000400370  _init&lt;br /&gt;
0x00000000004003a0  __libc_start_main@plt&lt;br /&gt;
0x00000000004003b0  __gmon_start__@plt&lt;br /&gt;
0x00000000004003c0  _start&lt;br /&gt;
0x00000000004003f0  deregister_tm_clones&lt;br /&gt;
0x0000000000400430  register_tm_clones&lt;br /&gt;
0x0000000000400470  __do_global_dtors_aux&lt;br /&gt;
0x0000000000400490  frame_dummy&lt;br /&gt;
0x0000000000400540  __libc_csu_init&lt;br /&gt;
0x00000000004005b0  __libc_csu_fini&lt;br /&gt;
0x00000000004005b4  _fini&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Sample GDB disassembling session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(gdb) disassemble main&lt;br /&gt;
Dump of assembler code for function main:&lt;br /&gt;
   0x000000000040050f &amp;lt;+0&amp;gt;:     push   %rbp&lt;br /&gt;
   0x0000000000400510 &amp;lt;+1&amp;gt;:     mov    %rsp,%rbp&lt;br /&gt;
   0x0000000000400513 &amp;lt;+4&amp;gt;:     mov    $0x0,%eax&lt;br /&gt;
   0x0000000000400518 &amp;lt;+9&amp;gt;:     callq  0x4004b6 &amp;lt;begin&amp;gt;&lt;br /&gt;
   0x000000000040051d &amp;lt;+14&amp;gt;:    mov    $0x0,%eax&lt;br /&gt;
   0x0000000000400522 &amp;lt;+19&amp;gt;:    callq  0x4004c7 &amp;lt;loop&amp;gt;&lt;br /&gt;
   0x0000000000400527 &amp;lt;+24&amp;gt;:    mov    $0x0,%eax&lt;br /&gt;
   0x000000000040052c &amp;lt;+29&amp;gt;:    callq  0x4004fe &amp;lt;end&amp;gt;&lt;br /&gt;
   0x0000000000400531 &amp;lt;+34&amp;gt;:    mov    $0x0,%eax&lt;br /&gt;
   0x0000000000400536 &amp;lt;+39&amp;gt;:    pop    %rbp&lt;br /&gt;
   0x0000000000400537 &amp;lt;+40&amp;gt;:    retq&lt;br /&gt;
End of assembler dump.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Sample GDB disassembling session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(gdb) disassemble /m main&lt;br /&gt;
Dump of assembler code for function main:&lt;br /&gt;
23      int main() {&lt;br /&gt;
   0x000000000040050f &amp;lt;+0&amp;gt;:     push   %rbp&lt;br /&gt;
   0x0000000000400510 &amp;lt;+1&amp;gt;:     mov    %rsp,%rbp&lt;br /&gt;
&lt;br /&gt;
24          begin();&lt;br /&gt;
   0x0000000000400513 &amp;lt;+4&amp;gt;:     mov    $0x0,%eax&lt;br /&gt;
   0x0000000000400518 &amp;lt;+9&amp;gt;:     callq  0x4004b6 &amp;lt;begin&amp;gt;&lt;br /&gt;
&lt;br /&gt;
25          loop();&lt;br /&gt;
   0x000000000040051d &amp;lt;+14&amp;gt;:    mov    $0x0,%eax&lt;br /&gt;
   0x0000000000400522 &amp;lt;+19&amp;gt;:    callq  0x4004c7 &amp;lt;loop&amp;gt;&lt;br /&gt;
&lt;br /&gt;
26          end();&lt;br /&gt;
   0x0000000000400527 &amp;lt;+24&amp;gt;:    mov    $0x0,%eax&lt;br /&gt;
   0x000000000040052c &amp;lt;+29&amp;gt;:    callq  0x4004fe &amp;lt;end&amp;gt;&lt;br /&gt;
&lt;br /&gt;
27&lt;br /&gt;
28          return 0;&lt;br /&gt;
   0x0000000000400531 &amp;lt;+34&amp;gt;:    mov    $0x0,%eax&lt;br /&gt;
&lt;br /&gt;
29      }&lt;br /&gt;
   0x0000000000400536 &amp;lt;+39&amp;gt;:    pop    %rbp&lt;br /&gt;
   0x0000000000400537 &amp;lt;+40&amp;gt;:    retq&lt;br /&gt;
&lt;br /&gt;
End of assembler dump.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Sample GDB disassembling session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(gdb) disassemble /m loop&lt;br /&gt;
Dump of assembler code for function loop:&lt;br /&gt;
9       void loop() {&lt;br /&gt;
   0x00000000004004c7 &amp;lt;+0&amp;gt;:     push   %rbp&lt;br /&gt;
   0x00000000004004c8 &amp;lt;+1&amp;gt;:     mov    %rsp,%rbp&lt;br /&gt;
&lt;br /&gt;
10          int v[2];&lt;br /&gt;
11          int i, k;&lt;br /&gt;
12&lt;br /&gt;
13          for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
   0x00000000004004cb &amp;lt;+4&amp;gt;:     movl   $0x0,-0x4(%rbp)&lt;br /&gt;
   0x00000000004004d2 &amp;lt;+11&amp;gt;:    jmp    0x4004f5 &amp;lt;loop+46&amp;gt;&lt;br /&gt;
   0x00000000004004f1 &amp;lt;+42&amp;gt;:    addl   $0x1,-0x4(%rbp)&lt;br /&gt;
   0x00000000004004f5 &amp;lt;+46&amp;gt;:    cmpl   $0x7,-0x4(%rbp)&lt;br /&gt;
   0x00000000004004f9 &amp;lt;+50&amp;gt;:    jle    0x4004d4 &amp;lt;loop+13&amp;gt;&lt;br /&gt;
&lt;br /&gt;
14              k = i/2*2;      /* should have been k = i/(2*2); */&lt;br /&gt;
   0x00000000004004d4 &amp;lt;+13&amp;gt;:    mov    -0x4(%rbp),%eax&lt;br /&gt;
   0x00000000004004d7 &amp;lt;+16&amp;gt;:    mov    %eax,%edx&lt;br /&gt;
   0x00000000004004d9 &amp;lt;+18&amp;gt;:    shr    $0x1f,%edx&lt;br /&gt;
   0x00000000004004dc &amp;lt;+21&amp;gt;:    add    %edx,%eax&lt;br /&gt;
   0x00000000004004de &amp;lt;+23&amp;gt;:    sar    %eax&lt;br /&gt;
   0x00000000004004e0 &amp;lt;+25&amp;gt;:    add    %eax,%eax&lt;br /&gt;
   0x00000000004004e2 &amp;lt;+27&amp;gt;:    mov    %eax,-0x8(%rbp)&lt;br /&gt;
&lt;br /&gt;
15              v[k] = i;&lt;br /&gt;
   0x00000000004004e5 &amp;lt;+30&amp;gt;:    mov    -0x8(%rbp),%eax&lt;br /&gt;
   0x00000000004004e8 &amp;lt;+33&amp;gt;:    cltq&lt;br /&gt;
   0x00000000004004ea &amp;lt;+35&amp;gt;:    mov    -0x4(%rbp),%edx&lt;br /&gt;
   0x00000000004004ed &amp;lt;+38&amp;gt;:    mov    %edx,-0x10(%rbp,%rax,4)&lt;br /&gt;
&lt;br /&gt;
16          }&lt;br /&gt;
17      }&lt;br /&gt;
   0x00000000004004fb &amp;lt;+52&amp;gt;:    nop&lt;br /&gt;
   0x00000000004004fc &amp;lt;+53&amp;gt;:    pop    %rbp&lt;br /&gt;
   0x00000000004004fd &amp;lt;+54&amp;gt;:    retq&lt;br /&gt;
&lt;br /&gt;
End of assembler dump.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Sample objdump disassembling session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ objdump -S -D bug&lt;br /&gt;
[...]&lt;br /&gt;
00000000004004c7 &amp;lt;loop&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
  4004c7:       55                      push   %rbp&lt;br /&gt;
  4004c8:       48 89 e5                mov    %rsp,%rbp&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
  4004cb:       c7 45 fc 00 00 00 00    movl   $0x0,-0x4(%rbp)&lt;br /&gt;
  4004d2:       eb 21                   jmp    4004f5 &amp;lt;loop+0x2e&amp;gt;&lt;br /&gt;
        k = i/2*2;      /* should have been k = i/(2*2); */&lt;br /&gt;
  4004d4:       8b 45 fc                mov    -0x4(%rbp),%eax&lt;br /&gt;
  4004d7:       89 c2                   mov    %eax,%edx&lt;br /&gt;
  4004d9:       c1 ea 1f                shr    $0x1f,%edx&lt;br /&gt;
  4004dc:       01 d0                   add    %edx,%eax&lt;br /&gt;
  4004de:       d1 f8                   sar    %eax&lt;br /&gt;
  4004e0:       01 c0                   add    %eax,%eax&lt;br /&gt;
  4004e2:       89 45 f8                mov    %eax,-0x8(%rbp)&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
  4004e5:       8b 45 f8                mov    -0x8(%rbp),%eax&lt;br /&gt;
  4004e8:       48 98                   cltq&lt;br /&gt;
  4004ea:       8b 55 fc                mov    -0x4(%rbp),%edx&lt;br /&gt;
  4004ed:       89 54 85 f0             mov    %edx,-0x10(%rbp,%rax,4)&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
  4004f1:       83 45 fc 01             addl   $0x1,-0x4(%rbp)&lt;br /&gt;
  4004f5:       83 7d fc 07             cmpl   $0x7,-0x4(%rbp)&lt;br /&gt;
  4004f9:       7e d9                   jle    4004d4 &amp;lt;loop+0xd&amp;gt;&lt;br /&gt;
        k = i/2*2;      /* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
  4004fb:       90                      nop&lt;br /&gt;
  4004fc:       5d                      pop    %rbp&lt;br /&gt;
  4004fd:       c3                      retq&lt;br /&gt;
[...]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:debugger software]][[Category:bwUniCluster]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=3788</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=3788"/>
		<updated>2016-02-24T08:42:03Z</updated>

		<summary type="html">&lt;p&gt;R Doros: /* Disassembling */ More Examples&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Description !! Content&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] &amp;amp;#124; [[BwForCluster_Chemistry]]&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| n/a&lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [http://www.gnu.org/software/gdb/ Homepage] &amp;amp;#124; [http://www.gnu.org/software/gdb/documentation/  Documentation] &amp;amp;#124; [https://sourceware.org/gdb/wiki/ Wiki] &amp;amp;#124; [http://www.gnu.org/software/gdb/mailing-lists/ Mailinglists]&lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Included modules&lt;br /&gt;
|  icc &amp;amp;#124; icpc &amp;amp;#124; ifort &amp;amp;#124; idb&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Introduction =&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Versions and Availability =&lt;br /&gt;
A list of versions currently available on all bwHPC-C5-Clusters can be obtained from the&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://cis-hpc.uni-konstanz.de/prod.cis/ Cluster Information System CIS]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/big&amp;gt;&lt;br /&gt;
{{#widget:Iframe&lt;br /&gt;
|url=https://cis-hpc.uni-konstanz.de/prod.cis/bwUniCluster/devel/gdb&lt;br /&gt;
|width=99%&lt;br /&gt;
|height=120&lt;br /&gt;
}}&lt;br /&gt;
On the command line interface of any bwHPC cluster you&#039;ll get a list of available versions &lt;br /&gt;
by using the command &#039;&#039;&#039;&#039;module avail devel/gdb&#039;&#039;&#039;&#039;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ : bwUniCluster&lt;br /&gt;
$ module avail devel/gdb&lt;br /&gt;
------------------------ /opt/bwhpc/common/modulefiles -------------------------&lt;br /&gt;
devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font color=red&amp;gt;Attention!&amp;lt;/font&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The default Linux GDB may be a different version.&lt;br /&gt;
Check the &amp;lt;font color=green&amp;gt;default&amp;lt;/font&amp;gt; version by: &#039;&#039;&#039;&#039;gdb --version&#039;&#039;&#039;&#039; &amp;lt;font color=red&amp;gt;without loaded GDB-module&amp;lt;/font&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ module clear                             # clear all loaded modules&lt;br /&gt;
Are you sure you want to clear all loaded modules!? [n] y&lt;br /&gt;
$ gdb --version                            # this is the default version&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-83.el6)&lt;br /&gt;
[...]&lt;br /&gt;
$ module avail devel/gdb                   # available GDB-module&lt;br /&gt;
------------------------ /opt/bwhpc/common/modulefiles -------------------------&lt;br /&gt;
devel/gdb/7.7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&#039;&#039;It is not recommended (or impossible) the debug MPI-based binaries with GDB. You can&#039;t attach to MPI&#039;ed jobs, too.&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Loading =&lt;br /&gt;
It is possible to choose between several versions of GDB. By default both programs are installed. You can check the version which is currently used with:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To use a different version of GCC or GDB, you have to load it through the module system. For example you need GDB 7.7 or higher for GCC version 4.8 and above. In this case load the devel/gdb module.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
How to load the desired version (e.g. GDB version 7.7):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Documentation =&lt;br /&gt;
For &#039;&#039;&#039;online documentation&#039;&#039;&#039; see the links section in the summary table at the top of this page. For &#039;&#039;&#039;local documentation&#039;&#039;&#039; consult the man page.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Basic commands =&lt;br /&gt;
The code you want to debug should be compiled with the &amp;lt;font color=green&amp;gt;-g&amp;lt;/font&amp;gt; option. If the optimization flag is not set, the GCC will still do some basic optimization. Therefore, it is recommended to turn off the optmization explicitly with the &amp;lt;font color=green&amp;gt;-O0&amp;lt;/font&amp;gt; parameter for debugging. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Core dumps =&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Multithreaded debugging =&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called thread_bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pthread_t thread;&lt;br /&gt;
&lt;br /&gt;
void* thread3 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int w[2];&lt;br /&gt;
    int c, l;&lt;br /&gt;
&lt;br /&gt;
    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
        w[l] = c;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void* thread2 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        sleep(4);&lt;br /&gt;
        k = i/(2*2);    /* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main (){&lt;br /&gt;
&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
  &lt;br /&gt;
    //Thread 1&lt;br /&gt;
    int count1 = 0;&lt;br /&gt;
&lt;br /&gt;
    while(count1 &amp;lt; 4000){&lt;br /&gt;
      printf(&amp;quot;Thread 1: %d\n&amp;quot;, count1++);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    pthread_join(thread, NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB thread session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g thread_bug.c -o thread_bug -lpthread&lt;br /&gt;
$ gdb ./thread_bug&lt;br /&gt;
[...]&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break thread3&lt;br /&gt;
Breakpoint 1 at 0x40060c: file thread_bug.c, line 11.&lt;br /&gt;
(gdb) break thread2&lt;br /&gt;
Breakpoint 2 at 0x400650: file thread_bug.c, line 24.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 3 at 0x40069e: file thread_bug.c, line 35.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /tank/home/doros/.t/thread_bug &lt;br /&gt;
[Thread debugging using libthread_db enabled]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 3, main () at thread_bug.c:35&lt;br /&gt;
35	  pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 28260)  main () at thread_bug.c:35&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff7fe3700 (LWP 28303)]&lt;br /&gt;
36	  pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 28303)  thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 28260)  main () at thread_bug.c:36&lt;br /&gt;
(gdb) next&lt;br /&gt;
[Switching to Thread 0x7ffff7fe3700 (LWP 28303)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 2, thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
24	    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
(gdb) next&lt;br /&gt;
25		sleep(4);&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 3 Thread 0x7ffff77e2700 (LWP 28344)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 28303)  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 28260)  0x000000362f8725db in _IO_new_file_overflow () from /lib64/libc.so.6&lt;br /&gt;
(gdb) thread 2&lt;br /&gt;
[Switching to thread 2 (Thread 0x7ffff7fe3700 (LWP 28303))]#0  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
Single stepping until exit from function nanosleep,&lt;br /&gt;
which has no line number information.&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) thread 2&lt;br /&gt;
[Switching to thread 2 (Thread 0x7ffff7fe3700 (LWP 28303))]#0  0x000000362f8acce9 in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
Single stepping until exit from function nanosleep,&lt;br /&gt;
which has no line number information.&lt;br /&gt;
0x000000362f8acb50 in sleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
  3 Thread 0x7ffff77e2700 (LWP 28344)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
* 2 Thread 0x7ffff7fe3700 (LWP 28303)  0x000000362f8acb50 in sleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 28260)  0x000000362f8476f0 in vfprintf () from /lib64/libc.so.6&lt;br /&gt;
(gdb) thread 3&lt;br /&gt;
[Switching to thread 3 (Thread 0x7ffff77e2700 (LWP 28344))]#0  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) next&lt;br /&gt;
12	        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
No symbol &amp;quot;k&amp;quot; in current context.&lt;br /&gt;
(gdb) watch (l &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 4: (l &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Thread 1: 0&lt;br /&gt;
Thread 1: 1&lt;br /&gt;
Thread 1: 2&lt;br /&gt;
Thread 1: 3&lt;br /&gt;
Thread 1: 4&lt;br /&gt;
[...]&lt;br /&gt;
Hardware watchpoint 4: (l &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
thread3 (d=0x0) at thread_bug.c:13&lt;br /&gt;
13	        w[l] = c;&lt;br /&gt;
(gdb) print l&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print c&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Disassembling =&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info functions&lt;br /&gt;
| Shows names and data types of all defined functions.&lt;br /&gt;
|-&lt;br /&gt;
| info line &amp;quot;function&amp;quot;&lt;br /&gt;
| Map source lines to memory adresses (and back).&lt;br /&gt;
|-&lt;br /&gt;
| disassemble &#039;&#039;function&#039;&#039;&lt;br /&gt;
| Disassembles &amp;quot;function&amp;quot; (or a function fragment).&lt;br /&gt;
|}&lt;br /&gt;
&#039;&#039;&#039;Sample GDB disassembling session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
[...]&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;.&lt;br /&gt;
Find the GDB manual and other documentation resources online at:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/documentation/&amp;gt;.&lt;br /&gt;
For help, type &amp;quot;help&amp;quot;.&lt;br /&gt;
Type &amp;quot;apropos word&amp;quot; to search for commands related to &amp;quot;word&amp;quot;...&lt;br /&gt;
Reading symbols from bug...done.&lt;br /&gt;
(gdb) info functions&lt;br /&gt;
All defined functions:&lt;br /&gt;
&lt;br /&gt;
File bug.c:&lt;br /&gt;
void begin();&lt;br /&gt;
void end();&lt;br /&gt;
void loop();&lt;br /&gt;
int main();&lt;br /&gt;
&lt;br /&gt;
Non-debugging symbols:&lt;br /&gt;
0x0000000000400370  _init&lt;br /&gt;
0x00000000004003a0  __libc_start_main@plt&lt;br /&gt;
0x00000000004003b0  __gmon_start__@plt&lt;br /&gt;
0x00000000004003c0  _start&lt;br /&gt;
0x00000000004003f0  deregister_tm_clones&lt;br /&gt;
0x0000000000400430  register_tm_clones&lt;br /&gt;
0x0000000000400470  __do_global_dtors_aux&lt;br /&gt;
0x0000000000400490  frame_dummy&lt;br /&gt;
0x0000000000400540  __libc_csu_init&lt;br /&gt;
0x00000000004005b0  __libc_csu_fini&lt;br /&gt;
0x00000000004005b4  _fini&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Sample GDB disassembling session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(gdb) disassemble main&lt;br /&gt;
Dump of assembler code for function main:&lt;br /&gt;
   0x000000000040050f &amp;lt;+0&amp;gt;:     push   %rbp&lt;br /&gt;
   0x0000000000400510 &amp;lt;+1&amp;gt;:     mov    %rsp,%rbp&lt;br /&gt;
   0x0000000000400513 &amp;lt;+4&amp;gt;:     mov    $0x0,%eax&lt;br /&gt;
   0x0000000000400518 &amp;lt;+9&amp;gt;:     callq  0x4004b6 &amp;lt;begin&amp;gt;&lt;br /&gt;
   0x000000000040051d &amp;lt;+14&amp;gt;:    mov    $0x0,%eax&lt;br /&gt;
   0x0000000000400522 &amp;lt;+19&amp;gt;:    callq  0x4004c7 &amp;lt;loop&amp;gt;&lt;br /&gt;
   0x0000000000400527 &amp;lt;+24&amp;gt;:    mov    $0x0,%eax&lt;br /&gt;
   0x000000000040052c &amp;lt;+29&amp;gt;:    callq  0x4004fe &amp;lt;end&amp;gt;&lt;br /&gt;
   0x0000000000400531 &amp;lt;+34&amp;gt;:    mov    $0x0,%eax&lt;br /&gt;
   0x0000000000400536 &amp;lt;+39&amp;gt;:    pop    %rbp&lt;br /&gt;
   0x0000000000400537 &amp;lt;+40&amp;gt;:    retq&lt;br /&gt;
End of assembler dump.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Sample GDB disassembling session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(gdb) disassemble /m main&lt;br /&gt;
Dump of assembler code for function main:&lt;br /&gt;
23      int main() {&lt;br /&gt;
   0x000000000040050f &amp;lt;+0&amp;gt;:     push   %rbp&lt;br /&gt;
   0x0000000000400510 &amp;lt;+1&amp;gt;:     mov    %rsp,%rbp&lt;br /&gt;
&lt;br /&gt;
24          begin();&lt;br /&gt;
   0x0000000000400513 &amp;lt;+4&amp;gt;:     mov    $0x0,%eax&lt;br /&gt;
   0x0000000000400518 &amp;lt;+9&amp;gt;:     callq  0x4004b6 &amp;lt;begin&amp;gt;&lt;br /&gt;
&lt;br /&gt;
25          loop();&lt;br /&gt;
   0x000000000040051d &amp;lt;+14&amp;gt;:    mov    $0x0,%eax&lt;br /&gt;
   0x0000000000400522 &amp;lt;+19&amp;gt;:    callq  0x4004c7 &amp;lt;loop&amp;gt;&lt;br /&gt;
&lt;br /&gt;
26          end();&lt;br /&gt;
   0x0000000000400527 &amp;lt;+24&amp;gt;:    mov    $0x0,%eax&lt;br /&gt;
   0x000000000040052c &amp;lt;+29&amp;gt;:    callq  0x4004fe &amp;lt;end&amp;gt;&lt;br /&gt;
&lt;br /&gt;
27&lt;br /&gt;
28          return 0;&lt;br /&gt;
   0x0000000000400531 &amp;lt;+34&amp;gt;:    mov    $0x0,%eax&lt;br /&gt;
&lt;br /&gt;
29      }&lt;br /&gt;
   0x0000000000400536 &amp;lt;+39&amp;gt;:    pop    %rbp&lt;br /&gt;
   0x0000000000400537 &amp;lt;+40&amp;gt;:    retq&lt;br /&gt;
&lt;br /&gt;
End of assembler dump.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:debugger software]][[Category:bwUniCluster]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=3787</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=3787"/>
		<updated>2016-02-24T08:36:38Z</updated>

		<summary type="html">&lt;p&gt;R Doros: First disassembling session&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Description !! Content&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] &amp;amp;#124; [[BwForCluster_Chemistry]]&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| n/a&lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [http://www.gnu.org/software/gdb/ Homepage] &amp;amp;#124; [http://www.gnu.org/software/gdb/documentation/  Documentation] &amp;amp;#124; [https://sourceware.org/gdb/wiki/ Wiki] &amp;amp;#124; [http://www.gnu.org/software/gdb/mailing-lists/ Mailinglists]&lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Included modules&lt;br /&gt;
|  icc &amp;amp;#124; icpc &amp;amp;#124; ifort &amp;amp;#124; idb&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Introduction =&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Versions and Availability =&lt;br /&gt;
A list of versions currently available on all bwHPC-C5-Clusters can be obtained from the&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://cis-hpc.uni-konstanz.de/prod.cis/ Cluster Information System CIS]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/big&amp;gt;&lt;br /&gt;
{{#widget:Iframe&lt;br /&gt;
|url=https://cis-hpc.uni-konstanz.de/prod.cis/bwUniCluster/devel/gdb&lt;br /&gt;
|width=99%&lt;br /&gt;
|height=120&lt;br /&gt;
}}&lt;br /&gt;
On the command line interface of any bwHPC cluster you&#039;ll get a list of available versions &lt;br /&gt;
by using the command &#039;&#039;&#039;&#039;module avail devel/gdb&#039;&#039;&#039;&#039;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ : bwUniCluster&lt;br /&gt;
$ module avail devel/gdb&lt;br /&gt;
------------------------ /opt/bwhpc/common/modulefiles -------------------------&lt;br /&gt;
devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font color=red&amp;gt;Attention!&amp;lt;/font&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The default Linux GDB may be a different version.&lt;br /&gt;
Check the &amp;lt;font color=green&amp;gt;default&amp;lt;/font&amp;gt; version by: &#039;&#039;&#039;&#039;gdb --version&#039;&#039;&#039;&#039; &amp;lt;font color=red&amp;gt;without loaded GDB-module&amp;lt;/font&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ module clear                             # clear all loaded modules&lt;br /&gt;
Are you sure you want to clear all loaded modules!? [n] y&lt;br /&gt;
$ gdb --version                            # this is the default version&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-83.el6)&lt;br /&gt;
[...]&lt;br /&gt;
$ module avail devel/gdb                   # available GDB-module&lt;br /&gt;
------------------------ /opt/bwhpc/common/modulefiles -------------------------&lt;br /&gt;
devel/gdb/7.7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&#039;&#039;It is not recommended (or impossible) the debug MPI-based binaries with GDB. You can&#039;t attach to MPI&#039;ed jobs, too.&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Loading =&lt;br /&gt;
It is possible to choose between several versions of GDB. By default both programs are installed. You can check the version which is currently used with:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To use a different version of GCC or GDB, you have to load it through the module system. For example you need GDB 7.7 or higher for GCC version 4.8 and above. In this case load the devel/gdb module.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
How to load the desired version (e.g. GDB version 7.7):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Documentation =&lt;br /&gt;
For &#039;&#039;&#039;online documentation&#039;&#039;&#039; see the links section in the summary table at the top of this page. For &#039;&#039;&#039;local documentation&#039;&#039;&#039; consult the man page.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Basic commands =&lt;br /&gt;
The code you want to debug should be compiled with the &amp;lt;font color=green&amp;gt;-g&amp;lt;/font&amp;gt; option. If the optimization flag is not set, the GCC will still do some basic optimization. Therefore, it is recommended to turn off the optmization explicitly with the &amp;lt;font color=green&amp;gt;-O0&amp;lt;/font&amp;gt; parameter for debugging. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Core dumps =&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Multithreaded debugging =&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called thread_bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pthread_t thread;&lt;br /&gt;
&lt;br /&gt;
void* thread3 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int w[2];&lt;br /&gt;
    int c, l;&lt;br /&gt;
&lt;br /&gt;
    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
        w[l] = c;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void* thread2 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        sleep(4);&lt;br /&gt;
        k = i/(2*2);    /* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main (){&lt;br /&gt;
&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
  &lt;br /&gt;
    //Thread 1&lt;br /&gt;
    int count1 = 0;&lt;br /&gt;
&lt;br /&gt;
    while(count1 &amp;lt; 4000){&lt;br /&gt;
      printf(&amp;quot;Thread 1: %d\n&amp;quot;, count1++);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    pthread_join(thread, NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB thread session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g thread_bug.c -o thread_bug -lpthread&lt;br /&gt;
$ gdb ./thread_bug&lt;br /&gt;
[...]&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break thread3&lt;br /&gt;
Breakpoint 1 at 0x40060c: file thread_bug.c, line 11.&lt;br /&gt;
(gdb) break thread2&lt;br /&gt;
Breakpoint 2 at 0x400650: file thread_bug.c, line 24.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 3 at 0x40069e: file thread_bug.c, line 35.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /tank/home/doros/.t/thread_bug &lt;br /&gt;
[Thread debugging using libthread_db enabled]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 3, main () at thread_bug.c:35&lt;br /&gt;
35	  pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 28260)  main () at thread_bug.c:35&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff7fe3700 (LWP 28303)]&lt;br /&gt;
36	  pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 28303)  thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 28260)  main () at thread_bug.c:36&lt;br /&gt;
(gdb) next&lt;br /&gt;
[Switching to Thread 0x7ffff7fe3700 (LWP 28303)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 2, thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
24	    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
(gdb) next&lt;br /&gt;
25		sleep(4);&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 3 Thread 0x7ffff77e2700 (LWP 28344)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 28303)  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 28260)  0x000000362f8725db in _IO_new_file_overflow () from /lib64/libc.so.6&lt;br /&gt;
(gdb) thread 2&lt;br /&gt;
[Switching to thread 2 (Thread 0x7ffff7fe3700 (LWP 28303))]#0  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
Single stepping until exit from function nanosleep,&lt;br /&gt;
which has no line number information.&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) thread 2&lt;br /&gt;
[Switching to thread 2 (Thread 0x7ffff7fe3700 (LWP 28303))]#0  0x000000362f8acce9 in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
Single stepping until exit from function nanosleep,&lt;br /&gt;
which has no line number information.&lt;br /&gt;
0x000000362f8acb50 in sleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
  3 Thread 0x7ffff77e2700 (LWP 28344)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
* 2 Thread 0x7ffff7fe3700 (LWP 28303)  0x000000362f8acb50 in sleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 28260)  0x000000362f8476f0 in vfprintf () from /lib64/libc.so.6&lt;br /&gt;
(gdb) thread 3&lt;br /&gt;
[Switching to thread 3 (Thread 0x7ffff77e2700 (LWP 28344))]#0  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) next&lt;br /&gt;
12	        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
No symbol &amp;quot;k&amp;quot; in current context.&lt;br /&gt;
(gdb) watch (l &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 4: (l &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Thread 1: 0&lt;br /&gt;
Thread 1: 1&lt;br /&gt;
Thread 1: 2&lt;br /&gt;
Thread 1: 3&lt;br /&gt;
Thread 1: 4&lt;br /&gt;
[...]&lt;br /&gt;
Hardware watchpoint 4: (l &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
thread3 (d=0x0) at thread_bug.c:13&lt;br /&gt;
13	        w[l] = c;&lt;br /&gt;
(gdb) print l&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print c&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Disassembling =&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info functions&lt;br /&gt;
| Shows names and data types of all defined functions.&lt;br /&gt;
|-&lt;br /&gt;
| info line &amp;quot;function&amp;quot;&lt;br /&gt;
| Map source lines to memory adresses (and back).&lt;br /&gt;
|-&lt;br /&gt;
| disassemble &#039;&#039;function&#039;&#039;&lt;br /&gt;
| Disassembles &amp;quot;function&amp;quot; (or a function fragment).&lt;br /&gt;
|}&lt;br /&gt;
&#039;&#039;&#039;Sample GDB disassembling session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
[...]&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;.&lt;br /&gt;
Find the GDB manual and other documentation resources online at:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/documentation/&amp;gt;.&lt;br /&gt;
For help, type &amp;quot;help&amp;quot;.&lt;br /&gt;
Type &amp;quot;apropos word&amp;quot; to search for commands related to &amp;quot;word&amp;quot;...&lt;br /&gt;
Reading symbols from bug...done.&lt;br /&gt;
(gdb) info functions&lt;br /&gt;
All defined functions:&lt;br /&gt;
&lt;br /&gt;
File bug.c:&lt;br /&gt;
void begin();&lt;br /&gt;
void end();&lt;br /&gt;
void loop();&lt;br /&gt;
int main();&lt;br /&gt;
&lt;br /&gt;
Non-debugging symbols:&lt;br /&gt;
0x0000000000400370  _init&lt;br /&gt;
0x00000000004003a0  __libc_start_main@plt&lt;br /&gt;
0x00000000004003b0  __gmon_start__@plt&lt;br /&gt;
0x00000000004003c0  _start&lt;br /&gt;
0x00000000004003f0  deregister_tm_clones&lt;br /&gt;
0x0000000000400430  register_tm_clones&lt;br /&gt;
0x0000000000400470  __do_global_dtors_aux&lt;br /&gt;
0x0000000000400490  frame_dummy&lt;br /&gt;
0x0000000000400540  __libc_csu_init&lt;br /&gt;
0x00000000004005b0  __libc_csu_fini&lt;br /&gt;
0x00000000004005b4  _fini&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
[[Category:debugger software]][[Category:bwUniCluster]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=3786</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=3786"/>
		<updated>2016-02-24T08:30:48Z</updated>

		<summary type="html">&lt;p&gt;R Doros: /* Disassembling */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Description !! Content&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] &amp;amp;#124; [[BwForCluster_Chemistry]]&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| n/a&lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [http://www.gnu.org/software/gdb/ Homepage] &amp;amp;#124; [http://www.gnu.org/software/gdb/documentation/  Documentation] &amp;amp;#124; [https://sourceware.org/gdb/wiki/ Wiki] &amp;amp;#124; [http://www.gnu.org/software/gdb/mailing-lists/ Mailinglists]&lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Included modules&lt;br /&gt;
|  icc &amp;amp;#124; icpc &amp;amp;#124; ifort &amp;amp;#124; idb&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Introduction =&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Versions and Availability =&lt;br /&gt;
A list of versions currently available on all bwHPC-C5-Clusters can be obtained from the&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://cis-hpc.uni-konstanz.de/prod.cis/ Cluster Information System CIS]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/big&amp;gt;&lt;br /&gt;
{{#widget:Iframe&lt;br /&gt;
|url=https://cis-hpc.uni-konstanz.de/prod.cis/bwUniCluster/devel/gdb&lt;br /&gt;
|width=99%&lt;br /&gt;
|height=120&lt;br /&gt;
}}&lt;br /&gt;
On the command line interface of any bwHPC cluster you&#039;ll get a list of available versions &lt;br /&gt;
by using the command &#039;&#039;&#039;&#039;module avail devel/gdb&#039;&#039;&#039;&#039;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ : bwUniCluster&lt;br /&gt;
$ module avail devel/gdb&lt;br /&gt;
------------------------ /opt/bwhpc/common/modulefiles -------------------------&lt;br /&gt;
devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font color=red&amp;gt;Attention!&amp;lt;/font&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The default Linux GDB may be a different version.&lt;br /&gt;
Check the &amp;lt;font color=green&amp;gt;default&amp;lt;/font&amp;gt; version by: &#039;&#039;&#039;&#039;gdb --version&#039;&#039;&#039;&#039; &amp;lt;font color=red&amp;gt;without loaded GDB-module&amp;lt;/font&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ module clear                             # clear all loaded modules&lt;br /&gt;
Are you sure you want to clear all loaded modules!? [n] y&lt;br /&gt;
$ gdb --version                            # this is the default version&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-83.el6)&lt;br /&gt;
[...]&lt;br /&gt;
$ module avail devel/gdb                   # available GDB-module&lt;br /&gt;
------------------------ /opt/bwhpc/common/modulefiles -------------------------&lt;br /&gt;
devel/gdb/7.7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&#039;&#039;It is not recommended (or impossible) the debug MPI-based binaries with GDB. You can&#039;t attach to MPI&#039;ed jobs, too.&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Loading =&lt;br /&gt;
It is possible to choose between several versions of GDB. By default both programs are installed. You can check the version which is currently used with:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To use a different version of GCC or GDB, you have to load it through the module system. For example you need GDB 7.7 or higher for GCC version 4.8 and above. In this case load the devel/gdb module.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
How to load the desired version (e.g. GDB version 7.7):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Documentation =&lt;br /&gt;
For &#039;&#039;&#039;online documentation&#039;&#039;&#039; see the links section in the summary table at the top of this page. For &#039;&#039;&#039;local documentation&#039;&#039;&#039; consult the man page.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Basic commands =&lt;br /&gt;
The code you want to debug should be compiled with the &amp;lt;font color=green&amp;gt;-g&amp;lt;/font&amp;gt; option. If the optimization flag is not set, the GCC will still do some basic optimization. Therefore, it is recommended to turn off the optmization explicitly with the &amp;lt;font color=green&amp;gt;-O0&amp;lt;/font&amp;gt; parameter for debugging. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Core dumps =&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Multithreaded debugging =&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called thread_bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pthread_t thread;&lt;br /&gt;
&lt;br /&gt;
void* thread3 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int w[2];&lt;br /&gt;
    int c, l;&lt;br /&gt;
&lt;br /&gt;
    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
        w[l] = c;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void* thread2 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        sleep(4);&lt;br /&gt;
        k = i/(2*2);    /* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main (){&lt;br /&gt;
&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
  &lt;br /&gt;
    //Thread 1&lt;br /&gt;
    int count1 = 0;&lt;br /&gt;
&lt;br /&gt;
    while(count1 &amp;lt; 4000){&lt;br /&gt;
      printf(&amp;quot;Thread 1: %d\n&amp;quot;, count1++);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    pthread_join(thread, NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB thread session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g thread_bug.c -o thread_bug -lpthread&lt;br /&gt;
$ gdb ./thread_bug&lt;br /&gt;
[...]&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break thread3&lt;br /&gt;
Breakpoint 1 at 0x40060c: file thread_bug.c, line 11.&lt;br /&gt;
(gdb) break thread2&lt;br /&gt;
Breakpoint 2 at 0x400650: file thread_bug.c, line 24.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 3 at 0x40069e: file thread_bug.c, line 35.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /tank/home/doros/.t/thread_bug &lt;br /&gt;
[Thread debugging using libthread_db enabled]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 3, main () at thread_bug.c:35&lt;br /&gt;
35	  pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 28260)  main () at thread_bug.c:35&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff7fe3700 (LWP 28303)]&lt;br /&gt;
36	  pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 28303)  thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 28260)  main () at thread_bug.c:36&lt;br /&gt;
(gdb) next&lt;br /&gt;
[Switching to Thread 0x7ffff7fe3700 (LWP 28303)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 2, thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
24	    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
(gdb) next&lt;br /&gt;
25		sleep(4);&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 3 Thread 0x7ffff77e2700 (LWP 28344)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 28303)  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 28260)  0x000000362f8725db in _IO_new_file_overflow () from /lib64/libc.so.6&lt;br /&gt;
(gdb) thread 2&lt;br /&gt;
[Switching to thread 2 (Thread 0x7ffff7fe3700 (LWP 28303))]#0  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
Single stepping until exit from function nanosleep,&lt;br /&gt;
which has no line number information.&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) thread 2&lt;br /&gt;
[Switching to thread 2 (Thread 0x7ffff7fe3700 (LWP 28303))]#0  0x000000362f8acce9 in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
Single stepping until exit from function nanosleep,&lt;br /&gt;
which has no line number information.&lt;br /&gt;
0x000000362f8acb50 in sleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
  3 Thread 0x7ffff77e2700 (LWP 28344)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
* 2 Thread 0x7ffff7fe3700 (LWP 28303)  0x000000362f8acb50 in sleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 28260)  0x000000362f8476f0 in vfprintf () from /lib64/libc.so.6&lt;br /&gt;
(gdb) thread 3&lt;br /&gt;
[Switching to thread 3 (Thread 0x7ffff77e2700 (LWP 28344))]#0  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) next&lt;br /&gt;
12	        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
No symbol &amp;quot;k&amp;quot; in current context.&lt;br /&gt;
(gdb) watch (l &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 4: (l &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Thread 1: 0&lt;br /&gt;
Thread 1: 1&lt;br /&gt;
Thread 1: 2&lt;br /&gt;
Thread 1: 3&lt;br /&gt;
Thread 1: 4&lt;br /&gt;
[...]&lt;br /&gt;
Hardware watchpoint 4: (l &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
thread3 (d=0x0) at thread_bug.c:13&lt;br /&gt;
13	        w[l] = c;&lt;br /&gt;
(gdb) print l&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print c&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Disassembling =&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info functions&lt;br /&gt;
| Shows names and data types of all defined functions.&lt;br /&gt;
|-&lt;br /&gt;
| info line &amp;quot;function&amp;quot;&lt;br /&gt;
| Map source lines to memory adresses (and back).&lt;br /&gt;
|-&lt;br /&gt;
| disassemble &#039;&#039;function&#039;&#039;&lt;br /&gt;
| Disassembles &amp;quot;function&amp;quot; (or a function fragment).&lt;br /&gt;
|}&lt;br /&gt;
&#039;&#039;&#039;Sample GDB disassembling session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
[[Category:debugger software]][[Category:bwUniCluster]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=3785</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=3785"/>
		<updated>2016-02-23T15:40:08Z</updated>

		<summary type="html">&lt;p&gt;R Doros: /* Disassembling */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Description !! Content&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] &amp;amp;#124; [[BwForCluster_Chemistry]]&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| n/a&lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [http://www.gnu.org/software/gdb/ Homepage] &amp;amp;#124; [http://www.gnu.org/software/gdb/documentation/  Documentation] &amp;amp;#124; [https://sourceware.org/gdb/wiki/ Wiki] &amp;amp;#124; [http://www.gnu.org/software/gdb/mailing-lists/ Mailinglists]&lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Included modules&lt;br /&gt;
|  icc &amp;amp;#124; icpc &amp;amp;#124; ifort &amp;amp;#124; idb&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Introduction =&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Versions and Availability =&lt;br /&gt;
A list of versions currently available on all bwHPC-C5-Clusters can be obtained from the&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://cis-hpc.uni-konstanz.de/prod.cis/ Cluster Information System CIS]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/big&amp;gt;&lt;br /&gt;
{{#widget:Iframe&lt;br /&gt;
|url=https://cis-hpc.uni-konstanz.de/prod.cis/bwUniCluster/devel/gdb&lt;br /&gt;
|width=99%&lt;br /&gt;
|height=120&lt;br /&gt;
}}&lt;br /&gt;
On the command line interface of any bwHPC cluster you&#039;ll get a list of available versions &lt;br /&gt;
by using the command &#039;&#039;&#039;&#039;module avail devel/gdb&#039;&#039;&#039;&#039;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ : bwUniCluster&lt;br /&gt;
$ module avail devel/gdb&lt;br /&gt;
------------------------ /opt/bwhpc/common/modulefiles -------------------------&lt;br /&gt;
devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font color=red&amp;gt;Attention!&amp;lt;/font&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The default Linux GDB may be a different version.&lt;br /&gt;
Check the &amp;lt;font color=green&amp;gt;default&amp;lt;/font&amp;gt; version by: &#039;&#039;&#039;&#039;gdb --version&#039;&#039;&#039;&#039; &amp;lt;font color=red&amp;gt;without loaded GDB-module&amp;lt;/font&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ module clear                             # clear all loaded modules&lt;br /&gt;
Are you sure you want to clear all loaded modules!? [n] y&lt;br /&gt;
$ gdb --version                            # this is the default version&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-83.el6)&lt;br /&gt;
[...]&lt;br /&gt;
$ module avail devel/gdb                   # available GDB-module&lt;br /&gt;
------------------------ /opt/bwhpc/common/modulefiles -------------------------&lt;br /&gt;
devel/gdb/7.7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&#039;&#039;It is not recommended (or impossible) the debug MPI-based binaries with GDB. You can&#039;t attach to MPI&#039;ed jobs, too.&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Loading =&lt;br /&gt;
It is possible to choose between several versions of GDB. By default both programs are installed. You can check the version which is currently used with:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To use a different version of GCC or GDB, you have to load it through the module system. For example you need GDB 7.7 or higher for GCC version 4.8 and above. In this case load the devel/gdb module.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
How to load the desired version (e.g. GDB version 7.7):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Documentation =&lt;br /&gt;
For &#039;&#039;&#039;online documentation&#039;&#039;&#039; see the links section in the summary table at the top of this page. For &#039;&#039;&#039;local documentation&#039;&#039;&#039; consult the man page.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Basic commands =&lt;br /&gt;
The code you want to debug should be compiled with the &amp;lt;font color=green&amp;gt;-g&amp;lt;/font&amp;gt; option. If the optimization flag is not set, the GCC will still do some basic optimization. Therefore, it is recommended to turn off the optmization explicitly with the &amp;lt;font color=green&amp;gt;-O0&amp;lt;/font&amp;gt; parameter for debugging. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Core dumps =&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Multithreaded debugging =&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called thread_bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pthread_t thread;&lt;br /&gt;
&lt;br /&gt;
void* thread3 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int w[2];&lt;br /&gt;
    int c, l;&lt;br /&gt;
&lt;br /&gt;
    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
        w[l] = c;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void* thread2 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        sleep(4);&lt;br /&gt;
        k = i/(2*2);    /* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main (){&lt;br /&gt;
&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
  &lt;br /&gt;
    //Thread 1&lt;br /&gt;
    int count1 = 0;&lt;br /&gt;
&lt;br /&gt;
    while(count1 &amp;lt; 4000){&lt;br /&gt;
      printf(&amp;quot;Thread 1: %d\n&amp;quot;, count1++);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    pthread_join(thread, NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB thread session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g thread_bug.c -o thread_bug -lpthread&lt;br /&gt;
$ gdb ./thread_bug&lt;br /&gt;
[...]&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break thread3&lt;br /&gt;
Breakpoint 1 at 0x40060c: file thread_bug.c, line 11.&lt;br /&gt;
(gdb) break thread2&lt;br /&gt;
Breakpoint 2 at 0x400650: file thread_bug.c, line 24.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 3 at 0x40069e: file thread_bug.c, line 35.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /tank/home/doros/.t/thread_bug &lt;br /&gt;
[Thread debugging using libthread_db enabled]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 3, main () at thread_bug.c:35&lt;br /&gt;
35	  pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 28260)  main () at thread_bug.c:35&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff7fe3700 (LWP 28303)]&lt;br /&gt;
36	  pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 28303)  thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 28260)  main () at thread_bug.c:36&lt;br /&gt;
(gdb) next&lt;br /&gt;
[Switching to Thread 0x7ffff7fe3700 (LWP 28303)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 2, thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
24	    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
(gdb) next&lt;br /&gt;
25		sleep(4);&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 3 Thread 0x7ffff77e2700 (LWP 28344)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 28303)  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 28260)  0x000000362f8725db in _IO_new_file_overflow () from /lib64/libc.so.6&lt;br /&gt;
(gdb) thread 2&lt;br /&gt;
[Switching to thread 2 (Thread 0x7ffff7fe3700 (LWP 28303))]#0  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
Single stepping until exit from function nanosleep,&lt;br /&gt;
which has no line number information.&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) thread 2&lt;br /&gt;
[Switching to thread 2 (Thread 0x7ffff7fe3700 (LWP 28303))]#0  0x000000362f8acce9 in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
Single stepping until exit from function nanosleep,&lt;br /&gt;
which has no line number information.&lt;br /&gt;
0x000000362f8acb50 in sleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
  3 Thread 0x7ffff77e2700 (LWP 28344)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
* 2 Thread 0x7ffff7fe3700 (LWP 28303)  0x000000362f8acb50 in sleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 28260)  0x000000362f8476f0 in vfprintf () from /lib64/libc.so.6&lt;br /&gt;
(gdb) thread 3&lt;br /&gt;
[Switching to thread 3 (Thread 0x7ffff77e2700 (LWP 28344))]#0  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) next&lt;br /&gt;
12	        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
No symbol &amp;quot;k&amp;quot; in current context.&lt;br /&gt;
(gdb) watch (l &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 4: (l &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Thread 1: 0&lt;br /&gt;
Thread 1: 1&lt;br /&gt;
Thread 1: 2&lt;br /&gt;
Thread 1: 3&lt;br /&gt;
Thread 1: 4&lt;br /&gt;
[...]&lt;br /&gt;
Hardware watchpoint 4: (l &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
thread3 (d=0x0) at thread_bug.c:13&lt;br /&gt;
13	        w[l] = c;&lt;br /&gt;
(gdb) print l&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print c&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Disassembling =&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info functions&lt;br /&gt;
| Shows names and data types of all defined functions.&lt;br /&gt;
|-&lt;br /&gt;
| info line &amp;quot;function&amp;quot;&lt;br /&gt;
| Map source lines to memory adresses (and back).&lt;br /&gt;
|-&lt;br /&gt;
| disassemble &#039;&#039;function&#039;&#039;&lt;br /&gt;
| Disassembles &amp;quot;function&amp;quot; (or a function fragment).&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
[[Category:debugger software]][[Category:bwUniCluster]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=3784</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=3784"/>
		<updated>2016-02-23T15:25:16Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Start new chapter&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Description !! Content&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] &amp;amp;#124; [[BwForCluster_Chemistry]]&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| n/a&lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [http://www.gnu.org/software/gdb/ Homepage] &amp;amp;#124; [http://www.gnu.org/software/gdb/documentation/  Documentation] &amp;amp;#124; [https://sourceware.org/gdb/wiki/ Wiki] &amp;amp;#124; [http://www.gnu.org/software/gdb/mailing-lists/ Mailinglists]&lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Included modules&lt;br /&gt;
|  icc &amp;amp;#124; icpc &amp;amp;#124; ifort &amp;amp;#124; idb&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Introduction =&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Versions and Availability =&lt;br /&gt;
A list of versions currently available on all bwHPC-C5-Clusters can be obtained from the&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://cis-hpc.uni-konstanz.de/prod.cis/ Cluster Information System CIS]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/big&amp;gt;&lt;br /&gt;
{{#widget:Iframe&lt;br /&gt;
|url=https://cis-hpc.uni-konstanz.de/prod.cis/bwUniCluster/devel/gdb&lt;br /&gt;
|width=99%&lt;br /&gt;
|height=120&lt;br /&gt;
}}&lt;br /&gt;
On the command line interface of any bwHPC cluster you&#039;ll get a list of available versions &lt;br /&gt;
by using the command &#039;&#039;&#039;&#039;module avail devel/gdb&#039;&#039;&#039;&#039;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ : bwUniCluster&lt;br /&gt;
$ module avail devel/gdb&lt;br /&gt;
------------------------ /opt/bwhpc/common/modulefiles -------------------------&lt;br /&gt;
devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font color=red&amp;gt;Attention!&amp;lt;/font&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The default Linux GDB may be a different version.&lt;br /&gt;
Check the &amp;lt;font color=green&amp;gt;default&amp;lt;/font&amp;gt; version by: &#039;&#039;&#039;&#039;gdb --version&#039;&#039;&#039;&#039; &amp;lt;font color=red&amp;gt;without loaded GDB-module&amp;lt;/font&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ module clear                             # clear all loaded modules&lt;br /&gt;
Are you sure you want to clear all loaded modules!? [n] y&lt;br /&gt;
$ gdb --version                            # this is the default version&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-83.el6)&lt;br /&gt;
[...]&lt;br /&gt;
$ module avail devel/gdb                   # available GDB-module&lt;br /&gt;
------------------------ /opt/bwhpc/common/modulefiles -------------------------&lt;br /&gt;
devel/gdb/7.7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&#039;&#039;It is not recommended (or impossible) the debug MPI-based binaries with GDB. You can&#039;t attach to MPI&#039;ed jobs, too.&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Loading =&lt;br /&gt;
It is possible to choose between several versions of GDB. By default both programs are installed. You can check the version which is currently used with:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To use a different version of GCC or GDB, you have to load it through the module system. For example you need GDB 7.7 or higher for GCC version 4.8 and above. In this case load the devel/gdb module.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
How to load the desired version (e.g. GDB version 7.7):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Documentation =&lt;br /&gt;
For &#039;&#039;&#039;online documentation&#039;&#039;&#039; see the links section in the summary table at the top of this page. For &#039;&#039;&#039;local documentation&#039;&#039;&#039; consult the man page.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
= Basic commands =&lt;br /&gt;
The code you want to debug should be compiled with the &amp;lt;font color=green&amp;gt;-g&amp;lt;/font&amp;gt; option. If the optimization flag is not set, the GCC will still do some basic optimization. Therefore, it is recommended to turn off the optmization explicitly with the &amp;lt;font color=green&amp;gt;-O0&amp;lt;/font&amp;gt; parameter for debugging. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Core dumps =&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Multithreaded debugging =&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| width=600px class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called thread_bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pthread_t thread;&lt;br /&gt;
&lt;br /&gt;
void* thread3 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int w[2];&lt;br /&gt;
    int c, l;&lt;br /&gt;
&lt;br /&gt;
    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
        w[l] = c;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void* thread2 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        sleep(4);&lt;br /&gt;
        k = i/(2*2);    /* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main (){&lt;br /&gt;
&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
  &lt;br /&gt;
    //Thread 1&lt;br /&gt;
    int count1 = 0;&lt;br /&gt;
&lt;br /&gt;
    while(count1 &amp;lt; 4000){&lt;br /&gt;
      printf(&amp;quot;Thread 1: %d\n&amp;quot;, count1++);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    pthread_join(thread, NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB thread session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g thread_bug.c -o thread_bug -lpthread&lt;br /&gt;
$ gdb ./thread_bug&lt;br /&gt;
[...]&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break thread3&lt;br /&gt;
Breakpoint 1 at 0x40060c: file thread_bug.c, line 11.&lt;br /&gt;
(gdb) break thread2&lt;br /&gt;
Breakpoint 2 at 0x400650: file thread_bug.c, line 24.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 3 at 0x40069e: file thread_bug.c, line 35.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /tank/home/doros/.t/thread_bug &lt;br /&gt;
[Thread debugging using libthread_db enabled]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 3, main () at thread_bug.c:35&lt;br /&gt;
35	  pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 28260)  main () at thread_bug.c:35&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff7fe3700 (LWP 28303)]&lt;br /&gt;
36	  pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 28303)  thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 28260)  main () at thread_bug.c:36&lt;br /&gt;
(gdb) next&lt;br /&gt;
[Switching to Thread 0x7ffff7fe3700 (LWP 28303)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 2, thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
24	    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
(gdb) next&lt;br /&gt;
25		sleep(4);&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 3 Thread 0x7ffff77e2700 (LWP 28344)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 28303)  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 28260)  0x000000362f8725db in _IO_new_file_overflow () from /lib64/libc.so.6&lt;br /&gt;
(gdb) thread 2&lt;br /&gt;
[Switching to thread 2 (Thread 0x7ffff7fe3700 (LWP 28303))]#0  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
Single stepping until exit from function nanosleep,&lt;br /&gt;
which has no line number information.&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) thread 2&lt;br /&gt;
[Switching to thread 2 (Thread 0x7ffff7fe3700 (LWP 28303))]#0  0x000000362f8acce9 in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
Single stepping until exit from function nanosleep,&lt;br /&gt;
which has no line number information.&lt;br /&gt;
0x000000362f8acb50 in sleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
  3 Thread 0x7ffff77e2700 (LWP 28344)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
* 2 Thread 0x7ffff7fe3700 (LWP 28303)  0x000000362f8acb50 in sleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 28260)  0x000000362f8476f0 in vfprintf () from /lib64/libc.so.6&lt;br /&gt;
(gdb) thread 3&lt;br /&gt;
[Switching to thread 3 (Thread 0x7ffff77e2700 (LWP 28344))]#0  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) next&lt;br /&gt;
12	        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
No symbol &amp;quot;k&amp;quot; in current context.&lt;br /&gt;
(gdb) watch (l &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 4: (l &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Thread 1: 0&lt;br /&gt;
Thread 1: 1&lt;br /&gt;
Thread 1: 2&lt;br /&gt;
Thread 1: 3&lt;br /&gt;
Thread 1: 4&lt;br /&gt;
[...]&lt;br /&gt;
Hardware watchpoint 4: (l &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
thread3 (d=0x0) at thread_bug.c:13&lt;br /&gt;
13	        w[l] = c;&lt;br /&gt;
(gdb) print l&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print c&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Disassembling =&lt;br /&gt;
[[Category:debugger software]][[Category:bwUniCluster]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_overview.jpg&amp;diff=3734</id>
		<title>File:Map mainwin overview.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_overview.jpg&amp;diff=3734"/>
		<updated>2016-01-20T14:53:30Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Allinea map tool main window time bars&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Allinea map tool main window time bars&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_source.jpg&amp;diff=3731</id>
		<title>File:Map mainwin source.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_source.jpg&amp;diff=3731"/>
		<updated>2016-01-20T11:58:09Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Allinea map tool main window source code view&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Allinea map tool main window source code view&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_selection_source.jpg&amp;diff=3730</id>
		<title>File:Map mainwin selection source.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_selection_source.jpg&amp;diff=3730"/>
		<updated>2016-01-20T11:57:28Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Allinea map tool main window source code view (with selected part)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Allinea map tool main window source code view (with selected part)&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_selection_overview.jpg&amp;diff=3729</id>
		<title>File:Map mainwin selection overview.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_selection_overview.jpg&amp;diff=3729"/>
		<updated>2016-01-20T11:56:23Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Allinea map tool main window (with selected part)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Allinea map tool main window (with selected part)&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_selection_function.jpg&amp;diff=3728</id>
		<title>File:Map mainwin selection function.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_selection_function.jpg&amp;diff=3728"/>
		<updated>2016-01-20T11:55:13Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Allinea map tool main window parallel stack view (selected part)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Allinea map tool main window parallel stack view (selected part)&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_presets.jpg&amp;diff=3727</id>
		<title>File:Map mainwin presets.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_presets.jpg&amp;diff=3727"/>
		<updated>2016-01-20T11:53:39Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Allinea map tool main window presets overview&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Allinea map tool main window presets overview&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_output.jpg&amp;diff=3726</id>
		<title>File:Map mainwin output.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_output.jpg&amp;diff=3726"/>
		<updated>2016-01-20T11:52:41Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Allinea map tool main window tool output overview&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Allinea map tool main window tool output overview&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_mpi.jpg&amp;diff=3725</id>
		<title>File:Map mainwin mpi.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_mpi.jpg&amp;diff=3725"/>
		<updated>2016-01-20T11:51:26Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Allinea map tool main window mpi overview&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Allinea map tool main window mpi overview&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_memory.jpg&amp;diff=3724</id>
		<title>File:Map mainwin memory.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_memory.jpg&amp;diff=3724"/>
		<updated>2016-01-20T11:50:12Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Allinea map tool main window memory overview&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Allinea map tool main window memory overview&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_ins.jpg&amp;diff=3723</id>
		<title>File:Map mainwin ins.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_ins.jpg&amp;diff=3723"/>
		<updated>2016-01-20T11:49:21Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Allinea map tool main window instruction overview&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Allinea map tool main window instruction overview&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_function.jpg&amp;diff=3722</id>
		<title>File:Map mainwin function.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_function.jpg&amp;diff=3722"/>
		<updated>2016-01-20T11:47:48Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Allinea map tool main window parallel stack view&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Allinea map tool main window parallel stack view&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_disk.jpg&amp;diff=3721</id>
		<title>File:Map mainwin disk.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_disk.jpg&amp;diff=3721"/>
		<updated>2016-01-20T11:46:46Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Allinea map tool main window disc overview&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Allinea map tool main window disc overview&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_cpu.jpg&amp;diff=3720</id>
		<title>File:Map mainwin cpu.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin_cpu.jpg&amp;diff=3720"/>
		<updated>2016-01-20T11:44:39Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Allinea map tool main window cpu overview&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Allinea map tool main window cpu overview&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin.png&amp;diff=3719</id>
		<title>File:Map mainwin.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=File:Map_mainwin.png&amp;diff=3719"/>
		<updated>2016-01-20T11:00:41Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Allinea map tool main window&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Allinea map tool main window&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=File:Map_startwin.jpg&amp;diff=3718</id>
		<title>File:Map startwin.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=File:Map_startwin.jpg&amp;diff=3718"/>
		<updated>2016-01-20T10:59:36Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Allinea map tool start window&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Allinea map tool start window&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/Parallel_Programming&amp;diff=2362</id>
		<title>Development/Parallel Programming</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/Parallel_Programming&amp;diff=2362"/>
		<updated>2015-07-02T08:39:09Z</updated>

		<summary type="html">&lt;p&gt;R Doros: /* Usage */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{| style=&amp;quot;border-style: solid; border-width: 1px&amp;quot;&lt;br /&gt;
! Navigation: [[BwHPC_Best_Practices_Repository|bwHPC BPR]] &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
This page will provide information regarding the supported parallel programming paradigms and specific hints on their usage.&lt;br /&gt;
Please refer to the [[BwUniCluster_Environment_Modules|Modules Documentation]] how to setup your environment on bwUniCluster to load a specific software installation.&lt;br /&gt;
&lt;br /&gt;
= OpenMP =&lt;br /&gt;
== General Information ==&lt;br /&gt;
OpenMP is a mature specification [http://openmp.org/wp/openmp-specifications/] to allow easy, portable, and most importantly incremental node-level parallelisation of code.&lt;br /&gt;
Being a thread-based approach, OpenMP is aimed at more fine-grained parallelism than [[BwHPC_Best_Practices_Repository#MPI|MPI]].&lt;br /&gt;
Although there have been extensions to extend OpenMP for inter-node parallelisation, it is a node-level approach aimed to make best usage of a node&#039;s cores&amp;lt;!-- -- the section [[#Hybrid Parallelisation|Hybrid Parallelisation]] will explain how to parallelise utilizing MPI plus a thread-based parallelization paradigm like OpenMP--&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
With regard to ease-of-use, OpenMP is ahead of any other common approach: the source-code is annotated using &amp;lt;tt&amp;gt;#pragma omp&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;!$omp&amp;lt;/tt&amp;gt; statements, in C/C++ and Fortran respectively.&lt;br /&gt;
Whenever the compiler encompasses a semantic block of code encapsulated in a parallel region, this block of code is transparently compiled into a function, which is passed to a so-called team-of-threads upon entering this semantic block. This fork-join model of execution eases a lot of the programmer&#039;s pain involved with Threads.&lt;br /&gt;
Being a loop-centric approach, OpenMP is aimed at codes with long/time-consuming loops.&lt;br /&gt;
A single combined directive &amp;lt;tt&amp;gt;pragma omp parallel for&amp;lt;/tt&amp;gt; will tell the compiler to automatically parallelize the ensuing for-loop.&lt;br /&gt;
&lt;br /&gt;
The following example is a bit more advanced in that even reductions of variables over multiple threads are easily parallelizable:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
   for (int i=0, sum = 0.0; i &amp;lt; VECTOR_LEN; i++)&lt;br /&gt;
     norm2 += (v[i]*v[i]);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
is parallelized by just adding a single line as in:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#  pragma omp parallel for reduction(+:norm2)&lt;br /&gt;
   for (int i=0, sum = 0.0; i &amp;lt; VECTOR_LEN; i++)&lt;br /&gt;
     norm2 += (v[i]*v[i]);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
With &amp;lt;tt&amp;gt;VECTOR_LENGTH&amp;lt;/tt&amp;gt; being large enough, this piece of code compiled with OpenMP will run in parallel, exhibiting very nice speedup.&lt;br /&gt;
Compiled without, the code remains as is. Developpers may therefore incrementally parallelize their application based on the profile derived from performance analysis tools, starting with the most time-consuming loops.&lt;br /&gt;
Using OpenMP&#039;s concise API, one may query the number of running threads, the number of processors, a time to calculate runtime, and even set parameters such as the number of threads to execute a parallel region.&lt;br /&gt;
&lt;br /&gt;
The OpenMP-4.0 specification added support for the SIMD-directive to better utilize SIMD-vectorization, as well as integrating directives to offload computation to accelerators using the &amp;lt;tt&amp;gt;target&amp;lt;/tt&amp;gt; directive: these are integrated into the Intel Compiler and are actively being worked on for the GNU compiler, some restrictions may apply.&lt;br /&gt;
&lt;br /&gt;
== OpenMP Best Practice Guide ==&lt;br /&gt;
The following silly example to calculate the squared Euklidian Norm shows some techniques:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;omp.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define VECTOR_LENGTH 5&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char * argv[])&lt;br /&gt;
{&lt;br /&gt;
    int len = VECTOR_LENGTH;&lt;br /&gt;
    int i;&lt;br /&gt;
    double * v;&lt;br /&gt;
    double norm2 = 0.0;&lt;br /&gt;
    double t1, tdiff;&lt;br /&gt;
&lt;br /&gt;
    if (argc &amp;gt; 1)&lt;br /&gt;
        len = atoi (argv[1]);&lt;br /&gt;
    v = malloc (len * sizeof(double));&lt;br /&gt;
&lt;br /&gt;
    t1 = omp_get_wtime();&lt;br /&gt;
    // Initialization already with (the same number of) threads&lt;br /&gt;
#pragma omp parallel for&lt;br /&gt;
    for (i=0; i &amp;lt; len; i++) {&lt;br /&gt;
        v[i] = i;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Now aggregate the sum-of-squares by specifying a reduction&lt;br /&gt;
#pragma omp parallel for reduction(+:norm2)&lt;br /&gt;
    for(i=0; i &amp;lt; len; i++) {&lt;br /&gt;
        norm2 += (v[i]*v[i]);&lt;br /&gt;
    }&lt;br /&gt;
    tdiff = omp_get_wtime() - t1;&lt;br /&gt;
&lt;br /&gt;
    printf (&amp;quot;norm2: %f Time:%f\n&amp;quot;, norm2, tdiff);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;!-- Specific OpenMP hints: default(none), reproducability, thread-safey --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Group independent parallel sections together: in the above example, You may combine those two sections into one larger parallel block. This will just once enter the parallel region (in the fork-join model) instead of twice. Especially in inner loops, this will considerably decrease overhead.&lt;br /&gt;
* Compile with the Intel compiler&#039;s option &amp;lt;tt&amp;gt;-diag-enable sc-parallel3&amp;lt;/tt&amp;gt; to get the further warnings on thread-safety, performance, etc. The following code with loop-carried dependency will e.g. compile fine (aka without warning):&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#pragma omp parallel for reduction(+:norm2)&lt;br /&gt;
    for(i=1; i &amp;lt; len-1; i++) {&lt;br /&gt;
        v[i] = v[i-1]+v[i+1];&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
However the Intel compiler with &amp;lt;tt&amp;gt;-diag-enable sc-parallel3&amp;lt;/tt&amp;gt; will produce the following warning:&lt;br /&gt;
&amp;lt;tt&amp;gt;warning #12246: variable &amp;quot;v&amp;quot; has loop carried data dependency that may lead to incorrect program execution in parallel mode; see (file:omp_norm2.c line:32)&amp;lt;/tt&amp;gt;&lt;br /&gt;
* Always specify &amp;lt;tt&amp;gt;default(none)&amp;lt;/tt&amp;gt; on larger parallel regions in order to specifically set the visibility of variables to either &amp;lt;tt&amp;gt;shared&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;private&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* Try to restructure code to allow for &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt;: OpenMP defines synchronization points (implied barriers) at the end of worksharing constructs such as the &amp;lt;tt&amp;gt;pragma omp for&amp;lt;tt&amp;gt; directive. If the ensuing section of code does not depend on data being generated inside the parallel section, adding the &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; clause to the worksharing directive allows the compiler to eliminate this synchronization point. This reduces overhead, allows for better overlap and better utilization of the processor&#039;s ressources. This might imply however to restructure the code (move portions of independent code in between dependent works-sharing constructs).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Usage ==&lt;br /&gt;
OpenMP is supported by various compilers, here the usage for two main compilers [[BwHPC_BPG_Compiler#GCC|GCC]] and [[BwHPC_BPG_Compiler#Intel Suite|Intel Suite]] are introduced.&lt;br /&gt;
For both compilers, You first need to turn on OpenMP support by specifying a parameter on the compiler&#039;s command-line.&lt;br /&gt;
In case You make function calls to OpenMP&#039;s API, You also need to include the header-file &amp;lt;tt&amp;gt;omp.h&amp;lt;/tt&amp;gt;.&lt;br /&gt;
OpenMP&#039;s API allows to query or set the number of threads, query the number of processors, get a wall-clock time to measure execution times, etc.&lt;br /&gt;
&lt;br /&gt;
=== OpenMP with GNU Compiler Collection ===&lt;br /&gt;
Starting with version 4.2 the gcc compiler supports OpenMP-2.5.&lt;br /&gt;
Since then the analysis capabilities of the GNU compiler have steadily improved.&lt;br /&gt;
The installed compilers support OpenMP-3.1.&lt;br /&gt;
&amp;lt;!-- Starting with gcc-4.9 OpenMP-4.0 is supported, however the &amp;lt;tt&amp;gt;target&amp;lt;/tt&amp;gt; directive will only offload to the host processor. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To use OpenMP with the gcc-compiler, pass &amp;lt;tt&amp;gt;-fopenmp&amp;lt;/tt&amp;gt; as parameter.&lt;br /&gt;
&lt;br /&gt;
=== OpenMP with Intel Compiler ===&lt;br /&gt;
The Intel Compiler&#039;s support for OpenMP is more advanced than gcc&#039;s -- especially in term of programmer support.&lt;br /&gt;
To use OpenMP with the Intel compiler, pass &amp;lt;tt&amp;gt;-openmp&amp;lt;/tt&amp;gt; as command-line parameter.&lt;br /&gt;
&lt;br /&gt;
One may get very insightful information about OpenMP, when compiling with&lt;br /&gt;
* Compiling with &amp;lt;tt&amp;gt;-openmp-report2&amp;lt;/tt&amp;gt; to get information, which loops were parallelized and a reason why not.&lt;br /&gt;
* Compiling with &amp;lt;tt&amp;gt;-diag-enable sc-parallel3&amp;lt;/tt&amp;gt; to get errors and warnings about your sources weaknesses with regard to parallelization (see example below).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= MPI =&lt;br /&gt;
In this section, You will find information regarding the supported installations of the Message-Passing Interface libraries and their usage.&lt;br /&gt;
Due to the Fortran interface ABI, all MPI-libraries are normally bound to a specific compiler-vendor and even the specific compiler version.&lt;br /&gt;
Therefore, as listed in [[BwHPC_BPG_Compiler]] two compilers are supported on bwUniCluster: [[BwHPC_BPG_Compiler#GCC|GCC]] and [[BwHPC_BPG_Compiler#Intel Suite|Intel Suite]].&lt;br /&gt;
As both compilers are continously improving, the communication libraries will be adopted in lock-step.&lt;br /&gt;
&lt;br /&gt;
With a set of different implementations, there comes the problem of choice. These pages should inform the user of the communication libraries, what considerations should be done with regard to performance, maintainability and debugging -- in general tool support -- of the various implementations.&lt;br /&gt;
&lt;br /&gt;
== MPI Introduction ==&lt;br /&gt;
The Message-Passing Interface is a standard provided by the [http://www.mpi-forum.org MPI-Forum] which regularly convenes for the [http://meetings.mpi-forum.org MPI-Forum Meetings] to update this standard. The current version is MPI-3.0 available as [http://mpi-forum.org/docs/mpi-3.0/mpi30-report.pdf PDF].&lt;br /&gt;
This document defines the API of over 300 functions for the C- and the Fortran-language -- however, You will certainly not need all of them to begin with.&lt;br /&gt;
&lt;br /&gt;
Every MPI-conforming program needs to call &amp;lt;tt&amp;gt;MPI_Init()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;MPI_Finalize()&amp;lt;/tt&amp;gt; upon start and shutdown -- or &amp;lt;tt&amp;gt;MPI_Abort()&amp;lt;/tt&amp;gt; in case of an abnormal termination.&lt;br /&gt;
After initialization the programm may call any other MPI-function, specifically communication functions.&lt;br /&gt;
However to do so, it is required to find out how many processes the program has been started with, using &amp;lt;tt&amp;gt;MPI_Comm_size()&amp;lt;/tt&amp;gt; and what number (here called a rank= this particular process has using &amp;lt;tt&amp;gt;MPI_Comm_rank()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
Communication is always relative to a so-called communicator -- the default one after initialization being called &amp;lt;tt&amp;gt;MPI_COMM_WORLD&amp;lt;/tt&amp;gt;- &lt;br /&gt;
&lt;br /&gt;
There&#039;s basically three ways of communication:&lt;br /&gt;
* two-sided communication using point-to-point (often abbreviated P2P) functions, such as &amp;lt;tt&amp;gt;MPI_Send()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;MPI_Recv()&amp;lt;/tt&amp;gt;, which always involves two participating processes,&lt;br /&gt;
* collectice communcation functions (often abbreviated as colls) involve multiple processes, examples are &amp;lt;tt&amp;gt;MPI_Bcast()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;MPI_Reduce()&amp;lt;/tt&amp;gt;, &lt;br /&gt;
* one-sided communication, where communication between two processes is initiated by one-process, only. With proper RMA-hardware support and careful programming, this may allow higher performance or scalibility.&lt;br /&gt;
&lt;br /&gt;
All parts of the programm, which reference MPI functionality need to be compiled with the &#039;&#039;&#039;same&#039;&#039;&#039; compiler settings/include files and linked to the same MPI-Library. This is stressed here, since without taking pre-cautions, a different MPI&#039;s header may be included, resulting in funny errors: consider that Intel MPI is derived from MPIch, with MPI-datatypes being C &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;s, while Open MPI uses pointers to structures (the former being 4, the latter being 8 bytes on bwUniCluster).&lt;br /&gt;
To ease the programmer&#039;s life, MPI implementations offer compiler-wrappers, e.g. &amp;lt;tt&amp;gt;mpicc&amp;lt;/tt&amp;gt; for C and &amp;lt;tt&amp;gt;mpif90&amp;lt;/tt&amp;gt; for Fortran90 for compilation and linking, taking care to include all required libraries.&lt;br /&gt;
 &lt;br /&gt;
All programs must be started using the &amp;lt;tt&amp;gt;mpirun&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;mpiexec&amp;lt;/tt&amp;gt; command. Depending on the actual implementation, it uses different arguments, however the following works with any MPI:&lt;br /&gt;
* &amp;lt;tt&amp;gt;mpirun -np 128 ./app&amp;lt;/tt&amp;gt; starts 128 processes (with ranks 0 to 127)&lt;br /&gt;
* &amp;lt;tt&amp;gt;mpiexec -n 128 -hostfile mynodes.txt ./app&amp;lt;/tt&amp;gt; starts 128 processes on only the nodes listed line-by-line in the provided text-file &amp;lt;tt&amp;gt;mynodes.txt&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* &amp;lt;tt&amp;gt;mpiexec -n 64 ./app1 : -n 64 ./app2&amp;lt;/tt&amp;gt; starts 128 processes, 64 of which execute &amp;lt;tt&amp;gt;app1&amp;lt;/tt&amp;gt;, the other 64 execute &amp;lt;tt&amp;gt;app2&amp;lt;/tt&amp;gt;. All processes however participate in the same &amp;lt;tt&amp;gt;MPI_COMM_WORLD&amp;lt;/tt&amp;gt; and therefore must accordingly take care about their respective ranks.&lt;br /&gt;
Please note, that process placement (e.g. a round-robin scheme), and specifically process-binding to sockets is MPI-implementation dependant.&lt;br /&gt;
&lt;br /&gt;
== MPI Best Practice Guide ==&lt;br /&gt;
Specific performance considerations with regard to MPI (independent of the implementation):&lt;br /&gt;
* No communication at all is best: Only communicate between processes if at all necessary. Consider that file-access is &amp;quot;communication&amp;quot; as well.&lt;br /&gt;
* If communication is done with multiple processes, try to involve as many processes in just one call: MPI optimizes the communication pattern for so-called &amp;quot;collective communication&amp;quot; to take advantage of the underlying network (with regard to network topology, message sizes, queueing capabilities of the network interconnect, etc.). Therefore try to always think in collective communication, if a communication pattern involves a group of processes.&lt;br /&gt;
* Try to group processes together: Function calls like &amp;lt;tt&amp;gt;MPI_Cart_create&amp;lt;/tt&amp;gt; will come in handy for applications with cartesian domains but also general communicators derived from &amp;lt;tt&amp;gt;MPI_COMM_WORLD&amp;lt;/tt&amp;gt; using &amp;lt;tt&amp;gt;MPI_Comm_split()&amp;lt;/tt&amp;gt; may benefit by MPI&#039;s knowing the underlying network topology. Use MPI3&#039;s &amp;lt;tt&amp;gt;MPI_Comm_split_type()&amp;lt;/tt&amp;gt; with &amp;lt;tt&amp;gt;MPI_COMM_TYPE_SHARED&amp;lt;/tt&amp;gt; for a sub-communicator with processes having access to the same shared memory region (aka on bwUniCluster the same node).&lt;br /&gt;
* File-accesses to load / store data &#039;&#039;&#039;must&#039;&#039;&#039; be done collectively: Writing to storage, or even reading the initialization data -- all of which involves getting data from/to all MPI processes -- must be done collectively. MPI&#039;s Parallel IO offers a rich API to read and distribute the data access -- in order to take advantage of parallel filesystems like Lustre. A many-fold performance improvement may be seen by writing data in large chunks in collective fashion -- and at the same time being nice to other users and applications. &lt;br /&gt;
* Try to hide the communication by computation: Try to hide (some) of the cost of communication of Point-to-point communication by using non-blocking / immediate P2P-calls (&amp;lt;tt&amp;gt;MPI_Isend&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;MPI_Irecv&amp;lt;/tt&amp;gt; et al, followed by &amp;lt;tt&amp;gt;MPI_Wait&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;MPI_Test&amp;lt;/tt&amp;gt; et al). This may allow the MPI-implementation to initiate or even offload communication to the network interconnect and resume executing your application, while data is being transferred. MPI-3 adds non-blocking collectives, e.g. &amp;lt;tt&amp;gt;MPI_Ibcast()&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;MPI_Iallreduce()&amp;lt;/tt&amp;gt;. For extra credit, explain the use-cases of &amp;lt;tt&amp;gt;MPI_Ibarrier()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* Every call to MPI may trigger an access to physical hardware -- limit it: When calling communication-related functions such as &amp;lt;tt&amp;gt;MPI_Test&amp;lt;/tt&amp;gt; to check whether a specific communication has finished, the queue of the network adapter may need to be queried. This memory access or even physical hardware access to query the state will cost cycles. Therefore, the programmer should combine multiple requests with functions such as &amp;lt;tt&amp;gt;MPI_Waitall()&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;MPI_Waitany()&amp;lt;/tt&amp;gt; or their Test*-counterparts.&lt;br /&gt;
* Make usage of derived datatypes: instead of manually copying data into temporary, even newly allocated memory, describe the data-layout to MPI -- and let the implementation, or even the network HCA&#039;s hardware do the data fetching.&lt;br /&gt;
* Bind Your processes to sockets: Operating Systems are good in making best use of the ressources -- which sometimes involves moving tasks from one core to another, or even (though more unlikely since the OS&#039; heuristics try to avoid it) to another socket, with the obvious effects: Caches are cold, every memory access to memory allocated on the previous socket &amp;quot;has to travel the bus&amp;quot;. This is particularly happening if You have multiple OpenMP parallel regions which are separated by code that does IO -- and threads are sleeping -- the processes doing IO may wander to a different socket... Bind Your processes to at least the socket. All major MPIs support this binding (see below).&lt;br /&gt;
* Do not use the C++ interface: First of all, it has been marked as deprecated in the MPI-3.0 standard, since it added little benefit to C++ programmers over the C-interface. Moreover, since MPI implementations are written in C, the interface adds another level of indirection and therefore a bit of overhead in terms of instructions and Cache misses.&lt;br /&gt;
&lt;br /&gt;
== Open MPI ==&lt;br /&gt;
The [http://www.open-mpi.org Open MPI] library is an open, flexible and nevertheless performant implementation of MPI-2 and MPI-3. Licensed under BSD, it is being actively developed by an open community of industry and research institutions.&lt;br /&gt;
The flexibility comes in handy: using the concept of a [http://www.open-mpi.org/faq/?category=tuning#mca-def MCA] (aka a plugin) Open MPI supports many different network interconnects (Infinband, TCP, Cray, etc.) , on the other hand, a installation may be tailored to suite an installation, e.g. the network (Infiniband with specific settings), the main startup-mechanism, etc.&lt;br /&gt;
Furthermore, the [http://www.open-mpi.org/faq/ FAQ] offers hints on [http://www.open-mpi.org/faq/?category=tuning performance tuning].&lt;br /&gt;
&lt;br /&gt;
=== Usage ===&lt;br /&gt;
Like other MPI implementations, after loading the [[BwUniCluster_Environment_Modules|module]], Open MPI provides the compiler-wrappers &amp;lt;tt&amp;gt;mpicc&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;mpicxx&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;mpifort&amp;lt;/tt&amp;gt;  (or for&lt;br /&gt;
versions lower than 1.7 &amp;lt;tt&amp;gt;mpif77&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;mpif90&amp;lt;/tt&amp;gt;) for the C-, C++ and Fortran compilers respectively. Albeit their usage is not required, these wrappers are handy to not have to use the command-line options for header- or library directories, aka &amp;lt;tt&amp;gt;-I&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;-L&amp;lt;/tt&amp;gt;, as well as the actual needed MPI-libraries itselve.&lt;br /&gt;
&lt;br /&gt;
=== Further information === &lt;br /&gt;
&lt;br /&gt;
Open MPI also features a few specific functionalities that will help users and developpers, alike:&lt;br /&gt;
* Open MPI&#039;s tool &amp;lt;tt&amp;gt;ompi_info&amp;lt;/tt&amp;gt; allows seeing all of Open MPI&#039;s installed MCA components and their specific options.&lt;br /&gt;
Without any option the user gets a list of flags, the Open MPI installation was compiled for (version of compilers, specific configure-flags, e.g. debugging, or profiling options). Furthermore, using &amp;lt;tt&amp;gt;ompi_info --param all all&amp;lt;/tt&amp;gt; one may see all of the MCA&#039;s options, e.g. that the default PML-MCA uses an initial free-list of 4 blocks (increased by 64 upon first encountering this limit):&lt;br /&gt;
&amp;lt;tt&amp;gt;ompi_info --param ob1 all&amp;lt;/tt&amp;gt; -- which may be increased for applications that are certain to benefit from a larger value upon startup.&lt;br /&gt;
* Open MPI allows adapting MCA parameters on the command-line: parameters may be supplied, e.g. the above-mentioned parameter &amp;lt;tt&amp;gt;mpirun -np 128 --mca mpirun -np 16 --mca pml_ob1_free_list_num 128 ./mpi_stub&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* Open MPI internally uses the tool [http://www.open-mpi.org/projects/hwloc/ hwloc] for node-local processor-information, as well as process- and memory-affinity. This tool also is a good tool to get information on the node&#039;s processor topology and Cache-information. This may be used to optimize and balance memory usage or for choosing a better ratio of MPI processes per node vs. OpenMP threads per core.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &lt;br /&gt;
== Intel MPI ==&lt;br /&gt;
&lt;br /&gt;
 General information &lt;br /&gt;
 Usage &lt;br /&gt;
 Further information &lt;br /&gt;
&lt;br /&gt;
= Hybrid Parallelization =&lt;br /&gt;
&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GCC&amp;diff=2278</id>
		<title>Development/GCC</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GCC&amp;diff=2278"/>
		<updated>2015-04-23T06:16:00Z</updated>

		<summary type="html">&lt;p&gt;R Doros: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;!--{|  align=&amp;quot;right&amp;quot; {{Table|width=40%}}   --&amp;gt;&lt;br /&gt;
{|{{Softwarebox}}&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;2&amp;quot;  style=&amp;quot;text-align:center&amp;quot; | GCC&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| compiler/gnu/xxx&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| GPL&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [http://gcc.gnu.org/ Homepage]&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Included in module&lt;br /&gt;
| gcc, g++, gfortran&lt;br /&gt;
|}&lt;br /&gt;
The &#039;&#039;&#039;GNU Compiler Collection (GCC)&#039;&#039;&#039; consists of tools to compile C, C++ and Fortran programs:&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; style=&amp;quot;margin:5px;border-collapse:collapse&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;padding:3px&amp;quot;| gcc&lt;br /&gt;
|style=&amp;quot;padding:3px&amp;quot;| GNU C compiler&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;padding:3px&amp;quot;| g++&lt;br /&gt;
|style=&amp;quot;padding:3px&amp;quot;| GNU C++ compiler&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;padding:3px&amp;quot;| gfortran&lt;br /&gt;
|style=&amp;quot;padding:3px&amp;quot;| GNU Fortran compiler&lt;br /&gt;
|}&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Loading:&#039;&#039;&#039; There is a version of GCC available on the system without loading a module but it may be outdated and it is recommended to load the GNU compiler module. To get a list of all the different versions installed on the system execute the following command:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module avail compiler/gnu&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a default version which will be loaded when no version is explicitly specified, so the command&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/gnu&amp;lt;/pre&amp;gt;&lt;br /&gt;
will load the default version.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Online documentation:&#039;&#039;&#039; http://gcc.gnu.org/onlinedocs/&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Local documentation:&#039;&#039;&#039; For version specific documentation see the help page of the module. For example&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module help compiler/gnu&amp;lt;/pre&amp;gt;&lt;br /&gt;
will show the information for the default version.&lt;br /&gt;
For detailed lists of the different program options consult the particular man page:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ man gcc&lt;br /&gt;
$ man g++&lt;br /&gt;
$ man gfortran&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Optimizations:&#039;&#039;&#039; You can turn on various optimization options to enhance the performance of your program. Which options are the best depends on the specific program and can be determined by benchmarking your code. A command which gives good performance and a decent file size is&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc -march=native -O2 ex.c -o ex&amp;lt;/pre&amp;gt;&lt;br /&gt;
There are more aggressive optimization flags but the compiled programs can get quite large and the compilation process will probably take much longer. Moreover it can happen that the so compiled program is even slower. Such a command would be for example&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc -march=native -O3 ex.c -o ex&amp;lt;/pre&amp;gt;&lt;br /&gt;
For a complete list of all the optimization options execute&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc --help=optimizers&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Profiling with gprof:&#039;&#039;&#039; Gprof is the profiler which belongs to the gcc compiler. Gprof is also installed on the system and can be used to profile your code. The profiler supports C, C++, Pascal and Fortran 77 program code. With this tool it is possible to analyse call times and time spent within program functions.&lt;br /&gt;
&lt;br /&gt;
The first required step is to compile your program with the profiling flag &amp;quot;-pg&amp;quot;.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc -pg ex.c -o ex&amp;lt;/pre&amp;gt;&lt;br /&gt;
Compiled in this way your program will generate profiling data during execution. By default a file named gmon.out can be found in the work directory after running the program.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ./ex&amp;lt;/pre&amp;gt;&lt;br /&gt;
The next step is to run the gprof program to analyse the gmon.out profiling data file.  This file contains profiling data concerning the program execution, like an overview, time information or the call graph, in human readable format.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gprof gmon.out ex &amp;gt; outputfile.txt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Compiler_software]][[Category:bwUniCluster]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=1062</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=1062"/>
		<updated>2014-05-21T09:48:10Z</updated>

		<summary type="html">&lt;p&gt;R Doros: mt session finished&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;!--{|  align=&amp;quot;right&amp;quot; {{Table|width=40%}}   --&amp;gt;&lt;br /&gt;
{|{{Softwarebox}}&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;2&amp;quot;  style=&amp;quot;text-align:center&amp;quot; | GDB: The GNU Project Debugger&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
&amp;lt;!-- Neben CIS auch bereits über Kategorien  abgedeckt&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] [[bwForFreiburg]],  [[bwForMannheim-Heidelberg]],  [[bwForUlm]],  [[bwForTübingen]]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [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]&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No &amp;lt;!-- Yes could link to some x11-forwarding/vnc instructions --&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Included in module&lt;br /&gt;
| &amp;lt;!-- mention if there is more than one software in the module, i.e. icc, ifort and debugger --&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&lt;br /&gt;
== Loading ==&lt;br /&gt;
It is possible to choose between several versions of GDB and GCC. By default both programms are installed. You can check the version which is currently used with:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To use a different version of GCC or GDB, you have to load it through the module system. For example you need GDB 7.7 or higher for GCC version 4.8 and above. In this case load the devel/gdb module.&lt;br /&gt;
&lt;br /&gt;
How to check which versions are available:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module avail&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
How to load the desired version (e.g. GDB version 7.7):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Documentation ==&lt;br /&gt;
For &#039;&#039;&#039;online documentation&#039;&#039;&#039; see the links section in the summary table at the top of this page. For &#039;&#039;&#039;local documentation&#039;&#039;&#039; consult the man page.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Basic commands ==&lt;br /&gt;
The code you want to debug should be compiled with the -g option. If the optimization flag is not set, the GCC will still do some basic optimization. Therefore, it is recommended to turn off the optmization explicitly with the -O0 parameter for debugging. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Core dumps ==&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Multithreaded debugging ==&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called thread_bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pthread_t thread;&lt;br /&gt;
&lt;br /&gt;
void* thread3 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int w[2];&lt;br /&gt;
    int c, l;&lt;br /&gt;
&lt;br /&gt;
    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
        w[l] = c;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void* thread2 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        sleep(4);&lt;br /&gt;
        k = i/(2*2);    /* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main (){&lt;br /&gt;
&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
  &lt;br /&gt;
    //Thread 1&lt;br /&gt;
    int count1 = 0;&lt;br /&gt;
&lt;br /&gt;
    while(count1 &amp;lt; 4000){&lt;br /&gt;
      printf(&amp;quot;Thread 1: %d\n&amp;quot;, count1++);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    pthread_join(thread, NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB thread session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g thread_bug.c -o thread_bug -lpthread&lt;br /&gt;
$ gdb ./thread_bug&lt;br /&gt;
[...]&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break thread3&lt;br /&gt;
Breakpoint 1 at 0x40060c: file thread_bug.c, line 11.&lt;br /&gt;
(gdb) break thread2&lt;br /&gt;
Breakpoint 2 at 0x400650: file thread_bug.c, line 24.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 3 at 0x40069e: file thread_bug.c, line 35.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /tank/home/doros/.t/thread_bug &lt;br /&gt;
[Thread debugging using libthread_db enabled]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 3, main () at thread_bug.c:35&lt;br /&gt;
35	  pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 28260)  main () at thread_bug.c:35&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff7fe3700 (LWP 28303)]&lt;br /&gt;
36	  pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 28303)  thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 28260)  main () at thread_bug.c:36&lt;br /&gt;
(gdb) next&lt;br /&gt;
[Switching to Thread 0x7ffff7fe3700 (LWP 28303)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 2, thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
24	    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
(gdb) next&lt;br /&gt;
25		sleep(4);&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 3 Thread 0x7ffff77e2700 (LWP 28344)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 28303)  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 28260)  0x000000362f8725db in _IO_new_file_overflow () from /lib64/libc.so.6&lt;br /&gt;
(gdb) thread 2&lt;br /&gt;
[Switching to thread 2 (Thread 0x7ffff7fe3700 (LWP 28303))]#0  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
Single stepping until exit from function nanosleep,&lt;br /&gt;
which has no line number information.&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 28344)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) thread 2&lt;br /&gt;
[Switching to thread 2 (Thread 0x7ffff7fe3700 (LWP 28303))]#0  0x000000362f8acce9 in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
Single stepping until exit from function nanosleep,&lt;br /&gt;
which has no line number information.&lt;br /&gt;
0x000000362f8acb50 in sleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
  3 Thread 0x7ffff77e2700 (LWP 28344)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
* 2 Thread 0x7ffff7fe3700 (LWP 28303)  0x000000362f8acb50 in sleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 28260)  0x000000362f8476f0 in vfprintf () from /lib64/libc.so.6&lt;br /&gt;
(gdb) thread 3&lt;br /&gt;
[Switching to thread 3 (Thread 0x7ffff77e2700 (LWP 28344))]#0  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) next&lt;br /&gt;
12	        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
No symbol &amp;quot;k&amp;quot; in current context.&lt;br /&gt;
(gdb) watch (l &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 4: (l &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Thread 1: 0&lt;br /&gt;
Thread 1: 1&lt;br /&gt;
Thread 1: 2&lt;br /&gt;
Thread 1: 3&lt;br /&gt;
Thread 1: 4&lt;br /&gt;
[...]&lt;br /&gt;
Hardware watchpoint 4: (l &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
thread3 (d=0x0) at thread_bug.c:13&lt;br /&gt;
13	        w[l] = c;&lt;br /&gt;
(gdb) print l&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print c&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:debugger software]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=1061</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=1061"/>
		<updated>2014-05-21T09:34:43Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Continue mt session&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;!--{|  align=&amp;quot;right&amp;quot; {{Table|width=40%}}   --&amp;gt;&lt;br /&gt;
{|{{Softwarebox}}&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;2&amp;quot;  style=&amp;quot;text-align:center&amp;quot; | GDB: The GNU Project Debugger&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
&amp;lt;!-- Neben CIS auch bereits über Kategorien  abgedeckt&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] [[bwForFreiburg]],  [[bwForMannheim-Heidelberg]],  [[bwForUlm]],  [[bwForTübingen]]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [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]&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No &amp;lt;!-- Yes could link to some x11-forwarding/vnc instructions --&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Included in module&lt;br /&gt;
| &amp;lt;!-- mention if there is more than one software in the module, i.e. icc, ifort and debugger --&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&lt;br /&gt;
== Loading ==&lt;br /&gt;
It is possible to choose between several versions of GDB and GCC. By default both programms are installed. You can check the version which is currently used with:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To use a different version of GCC or GDB, you have to load it through the module system. For example you need GDB 7.7 or higher for GCC version 4.8 and above. In this case load the devel/gdb module.&lt;br /&gt;
&lt;br /&gt;
How to check which versions are available:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module avail&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
How to load the desired version (e.g. GDB version 7.7):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Documentation ==&lt;br /&gt;
For &#039;&#039;&#039;online documentation&#039;&#039;&#039; see the links section in the summary table at the top of this page. For &#039;&#039;&#039;local documentation&#039;&#039;&#039; consult the man page.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Basic commands ==&lt;br /&gt;
The code you want to debug should be compiled with the -g option. If the optimization flag is not set, the GCC will still do some basic optimization. Therefore, it is recommended to turn off the optmization explicitly with the -O0 parameter for debugging. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Core dumps ==&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Multithreaded debugging ==&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called thread_bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pthread_t thread;&lt;br /&gt;
&lt;br /&gt;
void* thread3 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int w[2];&lt;br /&gt;
    int c, l;&lt;br /&gt;
&lt;br /&gt;
    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
        w[l] = c;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void* thread2 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        sleep(4);&lt;br /&gt;
        k = i/(2*2);    /* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main (){&lt;br /&gt;
&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
  &lt;br /&gt;
    //Thread 1&lt;br /&gt;
    int count1 = 0;&lt;br /&gt;
&lt;br /&gt;
    while(count1 &amp;lt; 4000){&lt;br /&gt;
      printf(&amp;quot;Thread 1: %d\n&amp;quot;, count1++);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    pthread_join(thread, NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB thread session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g thread_bug.c -o thread_bug -lpthread&lt;br /&gt;
$ gdb ./thread_bug&lt;br /&gt;
[...]&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break thread3&lt;br /&gt;
Breakpoint 1 at 0x40060c: file thread_bug.c, line 11.&lt;br /&gt;
(gdb) break thread2&lt;br /&gt;
Breakpoint 2 at 0x400650: file thread_bug.c, line 24.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 3 at 0x40069e: file thread_bug.c, line 35.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /tank/home/doros/.t/thread_bug &lt;br /&gt;
[Thread debugging using libthread_db enabled]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 3, main () at thread_bug.c:35&lt;br /&gt;
35	  pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 49274)  main () at thread_bug.c:35&lt;br /&gt;
(gdb) cont&lt;br /&gt;
Continuing.&lt;br /&gt;
[New Thread 0x7ffff7fe3700 (LWP 19724)]&lt;br /&gt;
[Switching to Thread 0x7ffff7fe3700 (LWP 19724)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 2, thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
24	    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 2 Thread 0x7ffff7fe3700 (LWP 19724)  thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 49274)  0x000000362f8e55a7 in mprotect () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
25		sleep(4);&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff77e2700 (LWP 19780)]&lt;br /&gt;
Thread 1: 0&lt;br /&gt;
Thread 1: 1&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 19780)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 3 Thread 0x7ffff77e2700 (LWP 19780)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 19724)  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 49274)  0x000000362f8db7ad in write () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
12	        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
(gdb) thread 2&lt;br /&gt;
[Switching to thread 2 (Thread 0x7ffff7fe3700 (LWP 19724))]#0  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
Single stepping until exit from function nanosleep,&lt;br /&gt;
which has no line number information.&lt;br /&gt;
Thread 1: 2&lt;br /&gt;
Thread 1: 3&lt;br /&gt;
Thread 1: 4&lt;br /&gt;
Thread 1: 5&lt;br /&gt;
Thread 1: 6&lt;br /&gt;
Thread 1: 7&lt;br /&gt;
Thread 1: 8&lt;br /&gt;
Thread 1: 9&lt;br /&gt;
Thread 1: 10&lt;br /&gt;
Thread 1: 11&lt;br /&gt;
Thread 1: 12&lt;br /&gt;
Thread 1: 13&lt;br /&gt;
0x000000362f8acb50 in sleep () from /lib64/libc.so.6&lt;br /&gt;
(gdb) thread 3&lt;br /&gt;
[Switching to thread 3 (Thread 0x7ffff77e2700 (LWP 19780))]#0  0x0000003600000007 in ?? ()&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 3 Thread 0x7ffff77e2700 (LWP 19780)  0x0000003600000007 in ?? ()&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 19724)  0x000000362f8acb50 in sleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 49274)  0x000000362f8db7ad in write () from /lib64/libc.so.6&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:debugger software]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=1060</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=1060"/>
		<updated>2014-05-20T14:50:29Z</updated>

		<summary type="html">&lt;p&gt;R Doros: minor edit on session&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;!--{|  align=&amp;quot;right&amp;quot; {{Table|width=40%}}   --&amp;gt;&lt;br /&gt;
{|{{Softwarebox}}&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;2&amp;quot;  style=&amp;quot;text-align:center&amp;quot; | GDB: The GNU Project Debugger&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
&amp;lt;!-- Neben CIS auch bereits über Kategorien  abgedeckt&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] [[bwForFreiburg]],  [[bwForMannheim-Heidelberg]],  [[bwForUlm]],  [[bwForTübingen]]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [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]&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No &amp;lt;!-- Yes could link to some x11-forwarding/vnc instructions --&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Included in module&lt;br /&gt;
| &amp;lt;!-- mention if there is more than one software in the module, i.e. icc, ifort and debugger --&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&lt;br /&gt;
== Loading ==&lt;br /&gt;
It is possible to choose between several versions of GDB and GCC. By default both programms are installed. You can check the version which is currently used with:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To use a different version of GCC or GDB, you have to load it through the module system. For example you need GDB 7.7 or higher for GCC version 4.8 and above. In this case load the devel/gdb module.&lt;br /&gt;
&lt;br /&gt;
How to check which versions are available:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module avail&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
How to load the desired version (e.g. GDB version 7.7):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Documentation ==&lt;br /&gt;
For &#039;&#039;&#039;online documentation&#039;&#039;&#039; see the links section in the summary table at the top of this page. For &#039;&#039;&#039;local documentation&#039;&#039;&#039; consult the man page.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Basic commands ==&lt;br /&gt;
The code you want to debug should be compiled with the -g option. If the optimization flag is not set, the GCC will still do some basic optimization. Therefore, it is recommended to turn off the optmization explicitly with the -O0 parameter for debugging. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Core dumps ==&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Multithreaded debugging ==&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called thread_bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pthread_t thread;&lt;br /&gt;
&lt;br /&gt;
void* thread3 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int w[2];&lt;br /&gt;
    int c, l;&lt;br /&gt;
&lt;br /&gt;
    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
        w[l] = c;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void* thread2 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        sleep(4);&lt;br /&gt;
        k = i/(2*2);    /* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main (){&lt;br /&gt;
&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
  &lt;br /&gt;
    //Thread 1&lt;br /&gt;
    int count1 = 0;&lt;br /&gt;
&lt;br /&gt;
    while(count1 &amp;lt; 4000){&lt;br /&gt;
      printf(&amp;quot;Thread 1: %d\n&amp;quot;, count1++);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    pthread_join(thread, NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB thread session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g thread_bug.c -o thread_bug -lpthread&lt;br /&gt;
$ gdb ./thread_bug&lt;br /&gt;
[...]&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break thread3&lt;br /&gt;
Breakpoint 1 at 0x40060c: file thread_bug.c, line 11.&lt;br /&gt;
(gdb) break thread2&lt;br /&gt;
Breakpoint 2 at 0x400650: file thread_bug.c, line 24.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 3 at 0x40069e: file thread_bug.c, line 35.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /tank/home/doros/.t/thread_bug &lt;br /&gt;
[Thread debugging using libthread_db enabled]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 3, main () at thread_bug.c:35&lt;br /&gt;
35	  pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 49049)  main () at thread_bug.c:35&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff7fe3700 (LWP 49077)]&lt;br /&gt;
36	  pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 49077)  thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 49049)  main () at thread_bug.c:36&lt;br /&gt;
(gdb) next&lt;br /&gt;
[Switching to Thread 0x7ffff7fe3700 (LWP 49077)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 2, thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
24	    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 2 Thread 0x7ffff7fe3700 (LWP 49077)  thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 49049)  0x000000362f8e55a7 in mprotect () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
25		sleep(4);&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff77e2700 (LWP 49086)]&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 49086)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 3 Thread 0x7ffff77e2700 (LWP 49086)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 49077)  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 49049)  0x000000362f874720 in _IO_setb_internal () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
12	        l = c/(2*2);	/* should have been l = c/(2*2); */&lt;br /&gt;
(gdb) next&lt;br /&gt;
13	        w[l] = c;&lt;br /&gt;
(gdb) next&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:debugger software]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=1059</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=1059"/>
		<updated>2014-05-20T14:49:15Z</updated>

		<summary type="html">&lt;p&gt;R Doros: minor edit on session&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;!--{|  align=&amp;quot;right&amp;quot; {{Table|width=40%}}   --&amp;gt;&lt;br /&gt;
{|{{Softwarebox}}&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;2&amp;quot;  style=&amp;quot;text-align:center&amp;quot; | GDB: The GNU Project Debugger&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
&amp;lt;!-- Neben CIS auch bereits über Kategorien  abgedeckt&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] [[bwForFreiburg]],  [[bwForMannheim-Heidelberg]],  [[bwForUlm]],  [[bwForTübingen]]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [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]&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No &amp;lt;!-- Yes could link to some x11-forwarding/vnc instructions --&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Included in module&lt;br /&gt;
| &amp;lt;!-- mention if there is more than one software in the module, i.e. icc, ifort and debugger --&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&lt;br /&gt;
== Loading ==&lt;br /&gt;
It is possible to choose between several versions of GDB and GCC. By default both programms are installed. You can check the version which is currently used with:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To use a different version of GCC or GDB, you have to load it through the module system. For example you need GDB 7.7 or higher for GCC version 4.8 and above. In this case load the devel/gdb module.&lt;br /&gt;
&lt;br /&gt;
How to check which versions are available:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module avail&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
How to load the desired version (e.g. GDB version 7.7):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Documentation ==&lt;br /&gt;
For &#039;&#039;&#039;online documentation&#039;&#039;&#039; see the links section in the summary table at the top of this page. For &#039;&#039;&#039;local documentation&#039;&#039;&#039; consult the man page.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Basic commands ==&lt;br /&gt;
The code you want to debug should be compiled with the -g option. If the optimization flag is not set, the GCC will still do some basic optimization. Therefore, it is recommended to turn off the optmization explicitly with the -O0 parameter for debugging. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Core dumps ==&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Multithreaded debugging ==&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called thread_bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pthread_t thread;&lt;br /&gt;
&lt;br /&gt;
void* thread3 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int w[2];&lt;br /&gt;
    int c, l;&lt;br /&gt;
&lt;br /&gt;
    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
        w[l] = c;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void* thread2 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        sleep(4);&lt;br /&gt;
        k = i/(2*2);    /* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main (){&lt;br /&gt;
&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
  &lt;br /&gt;
    //Thread 1&lt;br /&gt;
    int count1 = 0;&lt;br /&gt;
&lt;br /&gt;
    while(count1 &amp;lt; 4000){&lt;br /&gt;
      printf(&amp;quot;Thread 1: %d\n&amp;quot;, count1++);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    pthread_join(thread, NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB thread session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g thread_bug.c -o thread_bug -lpthread&lt;br /&gt;
$ gdb ./thread_bug&lt;br /&gt;
[...]&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break thread3&lt;br /&gt;
Breakpoint 1 at 0x40060c: file thread_bug.c, line 11.&lt;br /&gt;
(gdb) break thread2&lt;br /&gt;
Breakpoint 2 at 0x400650: file thread_bug.c, line 24.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 3 at 0x40069e: file thread_bug.c, line 35.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/thread_bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 3, main () at thread_bug.c:35&lt;br /&gt;
35	  pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 49049)  main () at thread_bug.c:35&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff7fe3700 (LWP 49077)]&lt;br /&gt;
36	  pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 49077)  thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 49049)  main () at thread_bug.c:36&lt;br /&gt;
(gdb) next&lt;br /&gt;
[Switching to Thread 0x7ffff7fe3700 (LWP 49077)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 2, thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
24	    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 2 Thread 0x7ffff7fe3700 (LWP 49077)  thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 49049)  0x000000362f8e55a7 in mprotect () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
25		sleep(4);&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff77e2700 (LWP 49086)]&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 49086)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 3 Thread 0x7ffff77e2700 (LWP 49086)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 49077)  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 49049)  0x000000362f874720 in _IO_setb_internal () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
12	        l = c/(2*2);	/* should have been l = c/(2*2); */&lt;br /&gt;
(gdb) next&lt;br /&gt;
13	        w[l] = c;&lt;br /&gt;
(gdb) next&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:debugger software]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=1058</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=1058"/>
		<updated>2014-05-20T14:47:13Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Sourcecode changed&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;!--{|  align=&amp;quot;right&amp;quot; {{Table|width=40%}}   --&amp;gt;&lt;br /&gt;
{|{{Softwarebox}}&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;2&amp;quot;  style=&amp;quot;text-align:center&amp;quot; | GDB: The GNU Project Debugger&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
&amp;lt;!-- Neben CIS auch bereits über Kategorien  abgedeckt&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] [[bwForFreiburg]],  [[bwForMannheim-Heidelberg]],  [[bwForUlm]],  [[bwForTübingen]]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [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]&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No &amp;lt;!-- Yes could link to some x11-forwarding/vnc instructions --&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Included in module&lt;br /&gt;
| &amp;lt;!-- mention if there is more than one software in the module, i.e. icc, ifort and debugger --&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&lt;br /&gt;
== Loading ==&lt;br /&gt;
It is possible to choose between several versions of GDB and GCC. By default both programms are installed. You can check the version which is currently used with:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To use a different version of GCC or GDB, you have to load it through the module system. For example you need GDB 7.7 or higher for GCC version 4.8 and above. In this case load the devel/gdb module.&lt;br /&gt;
&lt;br /&gt;
How to check which versions are available:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module avail&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
How to load the desired version (e.g. GDB version 7.7):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Documentation ==&lt;br /&gt;
For &#039;&#039;&#039;online documentation&#039;&#039;&#039; see the links section in the summary table at the top of this page. For &#039;&#039;&#039;local documentation&#039;&#039;&#039; consult the man page.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Basic commands ==&lt;br /&gt;
The code you want to debug should be compiled with the -g option. If the optimization flag is not set, the GCC will still do some basic optimization. Therefore, it is recommended to turn off the optmization explicitly with the -O0 parameter for debugging. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Core dumps ==&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Multithreaded debugging ==&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called thread_bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pthread_t thread;&lt;br /&gt;
&lt;br /&gt;
void* thread3 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int w[2];&lt;br /&gt;
    int c, l;&lt;br /&gt;
&lt;br /&gt;
    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
        l = c/2*2;	/* should have been l = c/(2*2); */&lt;br /&gt;
        w[l] = c;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void* thread2 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        sleep(4);&lt;br /&gt;
        k = i/(2*2);    /* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main (){&lt;br /&gt;
&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
  &lt;br /&gt;
    //Thread 1&lt;br /&gt;
    int count1 = 0;&lt;br /&gt;
&lt;br /&gt;
    while(count1 &amp;lt; 4000){&lt;br /&gt;
      printf(&amp;quot;Thread 1: %d\n&amp;quot;, count1++);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    pthread_join(thread, NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB thread session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g thread_bug.c -o thread_bug -lpthread&lt;br /&gt;
$ gdb ./thread_bug&lt;br /&gt;
[...]&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break thread3&lt;br /&gt;
(gdb) break thread2&lt;br /&gt;
(gdb) break main&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/thread_bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 3, main () at thread_bug.c:35&lt;br /&gt;
35	  pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 49049)  main () at thread_bug.c:35&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff7fe3700 (LWP 49077)]&lt;br /&gt;
36	  pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 49077)  thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 49049)  main () at thread_bug.c:36&lt;br /&gt;
(gdb) next&lt;br /&gt;
[Switching to Thread 0x7ffff7fe3700 (LWP 49077)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 2, thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
24	    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 2 Thread 0x7ffff7fe3700 (LWP 49077)  thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 49049)  0x000000362f8e55a7 in mprotect () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
25		sleep(4);&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff77e2700 (LWP 49086)]&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 49086)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 3 Thread 0x7ffff77e2700 (LWP 49086)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 49077)  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 49049)  0x000000362f874720 in _IO_setb_internal () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
12	        l = c/(2*2);	/* should have been l = c/(2*2); */&lt;br /&gt;
(gdb) next&lt;br /&gt;
13	        w[l] = c;&lt;br /&gt;
(gdb) next&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:debugger software]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=1056</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=1056"/>
		<updated>2014-05-20T14:40:51Z</updated>

		<summary type="html">&lt;p&gt;R Doros: First part of the gdb session&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;!--{|  align=&amp;quot;right&amp;quot; {{Table|width=40%}}   --&amp;gt;&lt;br /&gt;
{|{{Softwarebox}}&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;2&amp;quot;  style=&amp;quot;text-align:center&amp;quot; | GDB: The GNU Project Debugger&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
&amp;lt;!-- Neben CIS auch bereits über Kategorien  abgedeckt&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] [[bwForFreiburg]],  [[bwForMannheim-Heidelberg]],  [[bwForUlm]],  [[bwForTübingen]]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [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]&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No &amp;lt;!-- Yes could link to some x11-forwarding/vnc instructions --&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Included in module&lt;br /&gt;
| &amp;lt;!-- mention if there is more than one software in the module, i.e. icc, ifort and debugger --&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&lt;br /&gt;
== Loading ==&lt;br /&gt;
It is possible to choose between several versions of GDB and GCC. By default both programms are installed. You can check the version which is currently used with:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To use a different version of GCC or GDB, you have to load it through the module system. For example you need GDB 7.7 or higher for GCC version 4.8 and above. In this case load the devel/gdb module.&lt;br /&gt;
&lt;br /&gt;
How to check which versions are available:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module avail&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
How to load the desired version (e.g. GDB version 7.7):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Documentation ==&lt;br /&gt;
For &#039;&#039;&#039;online documentation&#039;&#039;&#039; see the links section in the summary table at the top of this page. For &#039;&#039;&#039;local documentation&#039;&#039;&#039; consult the man page.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Basic commands ==&lt;br /&gt;
The code you want to debug should be compiled with the -g option. If the optimization flag is not set, the GCC will still do some basic optimization. Therefore, it is recommended to turn off the optmization explicitly with the -O0 parameter for debugging. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Core dumps ==&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Multithreaded debugging ==&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called thread_bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pthread_t thread;&lt;br /&gt;
&lt;br /&gt;
void* thread3 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int w[2];&lt;br /&gt;
    int c, l;&lt;br /&gt;
&lt;br /&gt;
    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
        l = c/(2*2);	/* should have been l = c/(2*2); */&lt;br /&gt;
        w[l] = c;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void* thread2 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        sleep(4);&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main (){&lt;br /&gt;
&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
  &lt;br /&gt;
    //Thread 1&lt;br /&gt;
    int count1 = 0;&lt;br /&gt;
&lt;br /&gt;
    while(count1 &amp;lt; 4000){&lt;br /&gt;
      printf(&amp;quot;Thread 1: %d\n&amp;quot;, count1++);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    pthread_join(thread, NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB thread session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g thread_bug.c -o thread_bug -lpthread&lt;br /&gt;
$ gdb ./thread_bug&lt;br /&gt;
[...]&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break thread3&lt;br /&gt;
(gdb) break thread2&lt;br /&gt;
(gdb) break main&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/thread_bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 3, main () at thread_bug.c:35&lt;br /&gt;
35	  pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 49049)  main () at thread_bug.c:35&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff7fe3700 (LWP 49077)]&lt;br /&gt;
36	  pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 49077)  thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
* 1 Thread 0x7ffff7fe5700 (LWP 49049)  main () at thread_bug.c:36&lt;br /&gt;
(gdb) next&lt;br /&gt;
[Switching to Thread 0x7ffff7fe3700 (LWP 49077)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 2, thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
24	    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 2 Thread 0x7ffff7fe3700 (LWP 49077)  thread2 (d=0x0) at thread_bug.c:24&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 49049)  0x000000362f8e55a7 in mprotect () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
25		sleep(4);&lt;br /&gt;
(gdb) next&lt;br /&gt;
[New Thread 0x7ffff77e2700 (LWP 49086)]&lt;br /&gt;
[Switching to Thread 0x7ffff77e2700 (LWP 49086)]&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
(gdb) info threads&lt;br /&gt;
* 3 Thread 0x7ffff77e2700 (LWP 49086)  thread3 (d=0x0) at thread_bug.c:11&lt;br /&gt;
  2 Thread 0x7ffff7fe3700 (LWP 49077)  0x000000362f8accdd in nanosleep () from /lib64/libc.so.6&lt;br /&gt;
  1 Thread 0x7ffff7fe5700 (LWP 49049)  0x000000362f874720 in _IO_setb_internal () from /lib64/libc.so.6&lt;br /&gt;
(gdb) next&lt;br /&gt;
12	        l = c/(2*2);	/* should have been l = c/(2*2); */&lt;br /&gt;
(gdb) next&lt;br /&gt;
13	        w[l] = c;&lt;br /&gt;
(gdb) next&lt;br /&gt;
11	    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:debugger software]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=1055</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=1055"/>
		<updated>2014-05-20T14:30:59Z</updated>

		<summary type="html">&lt;p&gt;R Doros: One line source code&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;!--{|  align=&amp;quot;right&amp;quot; {{Table|width=40%}}   --&amp;gt;&lt;br /&gt;
{|{{Softwarebox}}&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;2&amp;quot;  style=&amp;quot;text-align:center&amp;quot; | GDB: The GNU Project Debugger&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
&amp;lt;!-- Neben CIS auch bereits über Kategorien  abgedeckt&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] [[bwForFreiburg]],  [[bwForMannheim-Heidelberg]],  [[bwForUlm]],  [[bwForTübingen]]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [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]&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No &amp;lt;!-- Yes could link to some x11-forwarding/vnc instructions --&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Included in module&lt;br /&gt;
| &amp;lt;!-- mention if there is more than one software in the module, i.e. icc, ifort and debugger --&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&lt;br /&gt;
== Loading ==&lt;br /&gt;
It is possible to choose between several versions of GDB and GCC. By default both programms are installed. You can check the version which is currently used with:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To use a different version of GCC or GDB, you have to load it through the module system. For example you need GDB 7.7 or higher for GCC version 4.8 and above. In this case load the devel/gdb module.&lt;br /&gt;
&lt;br /&gt;
How to check which versions are available:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module avail&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
How to load the desired version (e.g. GDB version 7.7):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Documentation ==&lt;br /&gt;
For &#039;&#039;&#039;online documentation&#039;&#039;&#039; see the links section in the summary table at the top of this page. For &#039;&#039;&#039;local documentation&#039;&#039;&#039; consult the man page.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Basic commands ==&lt;br /&gt;
The code you want to debug should be compiled with the -g option. If the optimization flag is not set, the GCC will still do some basic optimization. Therefore, it is recommended to turn off the optmization explicitly with the -O0 parameter for debugging. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Core dumps ==&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Multithreaded debugging ==&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called thread_bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pthread_t thread;&lt;br /&gt;
&lt;br /&gt;
void* thread3 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int w[2];&lt;br /&gt;
    int c, l;&lt;br /&gt;
&lt;br /&gt;
    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
        l = c/(2*2);	/* should have been l = c/(2*2); */&lt;br /&gt;
        w[l] = c;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void* thread2 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        sleep(4);&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main (){&lt;br /&gt;
&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
  &lt;br /&gt;
    //Thread 1&lt;br /&gt;
    int count1 = 0;&lt;br /&gt;
&lt;br /&gt;
    while(count1 &amp;lt; 4000){&lt;br /&gt;
      printf(&amp;quot;Thread 1: %d\n&amp;quot;, count1++);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    pthread_join(thread, NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB thread session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g thread_bug.c -o thread_bug -lpthread&lt;br /&gt;
$ gdb ./thread_bug&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:debugger software]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=1054</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=1054"/>
		<updated>2014-05-20T14:10:31Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Start session&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;!--{|  align=&amp;quot;right&amp;quot; {{Table|width=40%}}   --&amp;gt;&lt;br /&gt;
{|{{Softwarebox}}&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;2&amp;quot;  style=&amp;quot;text-align:center&amp;quot; | GDB: The GNU Project Debugger&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
&amp;lt;!-- Neben CIS auch bereits über Kategorien  abgedeckt&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] [[bwForFreiburg]],  [[bwForMannheim-Heidelberg]],  [[bwForUlm]],  [[bwForTübingen]]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [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]&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No &amp;lt;!-- Yes could link to some x11-forwarding/vnc instructions --&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Included in module&lt;br /&gt;
| &amp;lt;!-- mention if there is more than one software in the module, i.e. icc, ifort and debugger --&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&lt;br /&gt;
== Loading ==&lt;br /&gt;
It is possible to choose between several versions of GDB and GCC. By default both programms are installed. You can check the version which is currently used with:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To use a different version of GCC or GDB, you have to load it through the module system. For example you need GDB 7.7 or higher for GCC version 4.8 and above. In this case load the devel/gdb module.&lt;br /&gt;
&lt;br /&gt;
How to check which versions are available:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module avail&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
How to load the desired version (e.g. GDB version 7.7):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Documentation ==&lt;br /&gt;
For &#039;&#039;&#039;online documentation&#039;&#039;&#039; see the links section in the summary table at the top of this page. For &#039;&#039;&#039;local documentation&#039;&#039;&#039; consult the man page.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Basic commands ==&lt;br /&gt;
The code you want to debug should be compiled with the -g option. If the optimization flag is not set, the GCC will still do some basic optimization. Therefore, it is recommended to turn off the optmization explicitly with the -O0 parameter for debugging. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Core dumps ==&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Multithreaded debugging ==&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called thread_bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pthread_t thread;&lt;br /&gt;
&lt;br /&gt;
void* thread3 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int w[2];&lt;br /&gt;
    int c, l;&lt;br /&gt;
&lt;br /&gt;
    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
        l = c/(2*2);	/* should have been l = c/(2*2); */&lt;br /&gt;
        w[l] = c;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void* thread2 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main (){&lt;br /&gt;
&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
  &lt;br /&gt;
    //Thread 1&lt;br /&gt;
    int count1 = 0;&lt;br /&gt;
&lt;br /&gt;
    while(count1 &amp;lt; 4000){&lt;br /&gt;
      printf(&amp;quot;Thread 1: %d\n&amp;quot;, count1++);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    pthread_join(thread, NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB thread session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g thread_bug.c -o thread_bug -lpthread&lt;br /&gt;
$ gdb ./thread_bug&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:debugger software]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=1053</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=1053"/>
		<updated>2014-05-20T14:06:40Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Include thread example&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;!--{|  align=&amp;quot;right&amp;quot; {{Table|width=40%}}   --&amp;gt;&lt;br /&gt;
{|{{Softwarebox}}&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;2&amp;quot;  style=&amp;quot;text-align:center&amp;quot; | GDB: The GNU Project Debugger&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
&amp;lt;!-- Neben CIS auch bereits über Kategorien  abgedeckt&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] [[bwForFreiburg]],  [[bwForMannheim-Heidelberg]],  [[bwForUlm]],  [[bwForTübingen]]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [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]&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No &amp;lt;!-- Yes could link to some x11-forwarding/vnc instructions --&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Included in module&lt;br /&gt;
| &amp;lt;!-- mention if there is more than one software in the module, i.e. icc, ifort and debugger --&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&lt;br /&gt;
== Loading ==&lt;br /&gt;
It is possible to choose between several versions of GDB and GCC. By default both programms are installed. You can check the version which is currently used with:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To use a different version of GCC or GDB, you have to load it through the module system. For example you need GDB 7.7 or higher for GCC version 4.8 and above. In this case load the devel/gdb module.&lt;br /&gt;
&lt;br /&gt;
How to check which versions are available:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module avail&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
How to load the desired version (e.g. GDB version 7.7):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Documentation ==&lt;br /&gt;
For &#039;&#039;&#039;online documentation&#039;&#039;&#039; see the links section in the summary table at the top of this page. For &#039;&#039;&#039;local documentation&#039;&#039;&#039; consult the man page.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Basic commands ==&lt;br /&gt;
The code you want to debug should be compiled with the -g option. If the optimization flag is not set, the GCC will still do some basic optimization. Therefore, it is recommended to turn off the optmization explicitly with the -O0 parameter for debugging. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Core dumps ==&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Multithreaded debugging ==&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called thread_bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
pthread_t thread;&lt;br /&gt;
&lt;br /&gt;
void* thread3 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int w[2];&lt;br /&gt;
    int c, l;&lt;br /&gt;
&lt;br /&gt;
    for(c = 0; c &amp;lt; 8; c++) {&lt;br /&gt;
        l = c/(2*2);	/* should have been l = c/(2*2); */&lt;br /&gt;
        w[l] = c;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void* thread2 (void* d)&lt;br /&gt;
{&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main (){&lt;br /&gt;
&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread2, NULL);&lt;br /&gt;
    pthread_create (&amp;amp;thread, NULL, thread3, NULL);&lt;br /&gt;
  &lt;br /&gt;
    //Thread 1&lt;br /&gt;
    int count1 = 0;&lt;br /&gt;
&lt;br /&gt;
    while(count1 &amp;lt; 4000){&lt;br /&gt;
      printf(&amp;quot;Thread 1: %d\n&amp;quot;, count1++);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    pthread_join(thread, NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:debugger software]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=817</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=817"/>
		<updated>2014-03-20T08:37:21Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Versionnumbers removed, more infos for gdb debugging insert&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{| style=&amp;quot;border-style: solid; border-width: 1px&amp;quot;&lt;br /&gt;
! Navigation: [[BwHPC_Best_Practices_Repository|bwHPC BPR]] &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--{|  align=&amp;quot;right&amp;quot; {{Table|width=40%}}   --&amp;gt;&lt;br /&gt;
{|{{Softwarebox}}&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;2&amp;quot;  style=&amp;quot;text-align:center&amp;quot; | GDB: The GNU Project Debugger&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
&amp;lt;!-- Neben CIS auch bereits über Kategorien  abgedeckt&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] [[bwForFreiburg]],  [[bwForMannheim-Heidelberg]],  [[bwForUlm]],  [[bwForTübingen]]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [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]&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No &amp;lt;!-- Yes could link to some x11-forwarding/vnc instructions --&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Included in module&lt;br /&gt;
| &amp;lt;!-- mention if there is more than one software in the module, i.e. icc, ifort and debugger --&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Debugging =&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&lt;br /&gt;
== Loading ==&lt;br /&gt;
It is possible to choose between several versions of GDB and GCC. By default both programms are installed. You can check the version which is currently used with:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to use another version of GCC or GDB, you have to load it through the module system. For example you need GDB 7.7 or higher for GCC version 4.8 and above. In this case load the devel/gdb module.&lt;br /&gt;
&lt;br /&gt;
How to check which versions are available:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module avail&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
How to load the desired version (e.g. GDB version 7.7):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load devel/gdb/7.7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Documentation ==&lt;br /&gt;
For &#039;&#039;&#039;online documentation&#039;&#039;&#039; see the links section in the summary table at the top of this page. For &#039;&#039;&#039;local documentation&#039;&#039;&#039; consult the man page.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Basic commands ==&lt;br /&gt;
The code you want to debug should be compiled with the -g option. If the optimization flag is just not set, the GCC will still do some basic optimization. So it is recommended to turn off the optmization explicitly with the -O0 parameter. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Core dumps ==&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Multithreaded debugging ==&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Valgrind =&lt;br /&gt;
&lt;br /&gt;
Valgrind can be used for debugging and profiling programs. It ships with various tools, some of them are described in the following table.&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Tool&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| memcheck&lt;br /&gt;
| Detects heap array overruns, memory leaks and incorrect freeing of heap memory.&lt;br /&gt;
|-&lt;br /&gt;
| exp-sgcheck&lt;br /&gt;
| Detects stack and global array overruns.&lt;br /&gt;
|-&lt;br /&gt;
| callgrind&lt;br /&gt;
| Profiler. Helps finding bottlenecks.&lt;br /&gt;
|-&lt;br /&gt;
| helgrind&lt;br /&gt;
| Detects race conditions and deadlocks.&lt;br /&gt;
|-&lt;br /&gt;
| drd&lt;br /&gt;
| Detects race conditions. Does not detect deadlocks but needs less memory than helgrind.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Loading:&#039;&#039;&#039; There is no need for loading valgrind because it is available by default.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Online documentation:&#039;&#039;&#039; http://valgrind.org/docs/&lt;br /&gt;
&#039;&#039;&#039;Local documentation:&#039;&#039;&#039; &amp;lt;pre&amp;gt;man valgrind&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Usage:&#039;&#039;&#039;&lt;br /&gt;
To simulate your program and gather information about it using a specific tool, valgrind can be called like this&lt;br /&gt;
&amp;lt;pre&amp;gt;$ valgrind --tool=&amp;lt;tool&amp;gt; --log-file=&amp;lt;logfile&amp;gt; ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
For example to check for race conditions the command would look like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ valgrind --tool=drd --log-file=log.mem ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Beware that the simulation via valgrind can take much longer and can consume much more memory compared to a normal execution.&lt;br /&gt;
Furthermore the tools can give false positives, i.e. they report errors even though the are none.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example for helgrind and drd:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This small program demonstrates how to detect data races in parallel programs. Both helgrind and drd support POSIX Threads (Pthreads) but not OpenMP so prepare to get many false positives when using these tools with OpenMP.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* pthreads.c */&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;math.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define NUM_THREADS 2&lt;br /&gt;
&lt;br /&gt;
int v = 0;&lt;br /&gt;
&lt;br /&gt;
void* f(void* x)&lt;br /&gt;
{&lt;br /&gt;
    int j = 0;&lt;br /&gt;
    int k = *(int*)x;&lt;br /&gt;
    &lt;br /&gt;
    for(j = 0; j &amp;lt; 100; j++)&lt;br /&gt;
        v += sin(j) + 1 + k;&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    pthread_t threads[NUM_THREADS];&lt;br /&gt;
    int idx[NUM_THREADS];&lt;br /&gt;
    int t;&lt;br /&gt;
   &lt;br /&gt;
    for(t=0; t&amp;lt;NUM_THREADS; t++) {&lt;br /&gt;
        idx[t] = t;&lt;br /&gt;
        pthread_create(&amp;amp;threads[t], NULL, f, (void*)&amp;amp;idx[t]);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    for(t=0; t&amp;lt;NUM_THREADS; t++)&lt;br /&gt;
        pthread_join(threads[t], NULL);&lt;br /&gt;
&lt;br /&gt;
    printf(&amp;quot;%i\n&amp;quot;, v);&lt;br /&gt;
&lt;br /&gt;
    pthread_exit(NULL);&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The program can be compiled with&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc -Wall pthreads.c -o pthreads -pthread -lm -O2&amp;lt;/pre&amp;gt;&lt;br /&gt;
where -pthread tells the compiler to link against the Pthreads library and -lm is needed for linking against the math library.&lt;br /&gt;
This program has a race condition, consecutive executions can give different outputs. Such errors are hard to find because they can show up very rarely.&lt;br /&gt;
Helgrind can tell you the problematic part of your code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ valgrind --tool=helgrind ./pthreads&lt;br /&gt;
...&lt;br /&gt;
==29237== ----------------------------------------------------------------&lt;br /&gt;
==29237== &lt;br /&gt;
==29237== Possible data race during read of size 4 at 0x600C78 by thread #3&lt;br /&gt;
==29237== Locks held: none&lt;br /&gt;
==29237==    at 0x4007BC: f(void*) (in /pfs/data2/home/xx/xxxx/xxxx/pthreads)&lt;br /&gt;
==29237==    by 0x4A0C0D4: mythread_wrapper (hg_intercepts.c:219)&lt;br /&gt;
==29237==    by 0x3EDA6079D0: start_thread (in /lib64/libpthread-2.12.so)&lt;br /&gt;
==29237==    by 0x3ED9AE8B6C: clone (in /lib64/libc-2.12.so)&lt;br /&gt;
==29237== &lt;br /&gt;
==29237== This conflicts with a previous write of size 4 by thread #2&lt;br /&gt;
==29237== Locks held: none&lt;br /&gt;
==29237==    at 0x4007FB: f(void*) (in /pfs/data2/home/xx/xxxx/xxxx/pthreads)&lt;br /&gt;
==29237==    by 0x4A0C0D4: mythread_wrapper (hg_intercepts.c:219)&lt;br /&gt;
==29237==    by 0x3EDA6079D0: start_thread (in /lib64/libpthread-2.12.so)&lt;br /&gt;
==29237==    by 0x3ED9AE8B6C: clone (in /lib64/libc-2.12.so)&lt;br /&gt;
==29237== &lt;br /&gt;
==29237== ----------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now we know that we have to check the function f for a possible data race. A similar output can be achieved using drd with the following command:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ valgrind --tool=drd ./pthreads&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Valgrind can give even better hints when compiled with debug information (helgrind produces similar output):  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g -Wall pthreads.c -o pthreads -pthread -lm&lt;br /&gt;
$ valgrind --tool=drd ./pthreads&lt;br /&gt;
...&lt;br /&gt;
==604== Thread 3:&lt;br /&gt;
==604== Conflicting load by thread 3 at 0x00600cc0 size 4&lt;br /&gt;
==604==    at 0x400749: f(void*) (pthreads.c:17)&lt;br /&gt;
==604==    by 0x4A128B4: vgDrd_thread_wrapper (drd_pthread_intercepts.c:355)&lt;br /&gt;
==604==    by 0x3EDA6079D0: start_thread (in /lib64/libpthread-2.12.so)&lt;br /&gt;
==604==    by 0x3ED9AE8B6C: clone (in /lib64/libc-2.12.so)&lt;br /&gt;
==604== Allocation context: BSS section of fs/data2/home/xx/xxxx/xxxx/pthreads&lt;br /&gt;
==604== Other segment start (thread 2)&lt;br /&gt;
==604==    (thread finished, call stack no longer available)&lt;br /&gt;
==604== Other segment end (thread 2)&lt;br /&gt;
==604==    (thread finished, call stack no longer available)&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The execution can take longer but now we know that the problem is most likely in line 17 (which is correct).&lt;br /&gt;
&lt;br /&gt;
= Parallel Debugger ddt =&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;width:100%; vertical-align:top; background:#f5fffa;border:1px solid #e7aa01;&amp;quot;&lt;br /&gt;
| style=&amp;quot;padding:2px;&amp;quot; | &amp;lt;div style=&amp;quot;margin:3px; background:#cef2e0; font-size:120%; font-weight:bold; border:1px solid #e7aa01; text-align:left; color:#000; padding:0.2em 0.4em;&amp;quot;&amp;gt;Currently only for employees of KIT&amp;lt;/div&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;color:#000;&amp;quot; | &amp;lt;div style=&amp;quot;padding:2px 5px&amp;quot;&amp;gt;On bwUniCluster the GUI based distributed debugging tool (ddt) may be used to debug serial as&lt;br /&gt;
well as parallel applications. For serial applications also the GNU gdb or Intel idb debugger&lt;br /&gt;
may be used. The Intel idb comes with the compiler and information on this tool is available&lt;br /&gt;
together with the compiler documentation. In order to debug your program it must be&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ddt consists of a graphical frontend and a backend serial debugger which controls the&lt;br /&gt;
application program. One instance of the serial debugger controls one MPI process. Via the&lt;br /&gt;
frontend the user interacts with the debugger to select the program that will be debugged,&lt;br /&gt;
to specify different options and to monitor the execution of the program. Debugging&lt;br /&gt;
commands may be sent to one, all or a subset of the MPI processes.&lt;br /&gt;
&lt;br /&gt;
Before the parallel debugger ddt can be used, it is necessary to load the corresponding&lt;br /&gt;
module file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ module use /opt/bwhpc/ka/modulefiles      (only available for employees of KIT)&lt;br /&gt;
$ module add debugger/ddt   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now ddt may be started with the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ ddt program&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where program is the name of your program that you want to debug.&lt;br /&gt;
&lt;br /&gt;
[[File:ddt1_750.jpg]]&lt;br /&gt;
&lt;br /&gt;
Figure: DDT startup window&lt;br /&gt;
&lt;br /&gt;
The above figure shows ddt’s startup window. Before actually starting the debugging session&lt;br /&gt;
you should check the contents of several fields in this window:&lt;br /&gt;
&lt;br /&gt;
1. The top line shows the executable file that will be run under control of the debugger. In&lt;br /&gt;
the following lines you may input some options that are passed to your program or to the&lt;br /&gt;
MPI environment.&lt;br /&gt;
&lt;br /&gt;
2. If your program reads data from stdin you can specify an input file in the startup window.&lt;br /&gt;
&lt;br /&gt;
3. Before starting an MPI program you should check that &amp;quot;Open MPI (Compatability)&amp;quot; or&lt;br /&gt;
&amp;quot;Intel MPI&amp;quot; is the MPI implementation that has been selected. If this is not the case, you&lt;br /&gt;
have to change this. Otherwise ddt may not be able to run your program. In order to debug&lt;br /&gt;
serial programs, the selected MPI implementation should be &amp;quot;none&amp;quot;. You may also change&lt;br /&gt;
the underlying serial debugger using the &amp;quot;change&amp;quot; button. By default ddt uses its own serial debugger, but it may also use the Intel idb debugger.&lt;br /&gt;
&lt;br /&gt;
4. Select the number of MPI processes that will be started by ddt. If you are using ddt within&lt;br /&gt;
a batch job, replace mpirun by ddt in the command line of ????? and make sure that the&lt;br /&gt;
chosen number of MPI processes is identical to the number of MPI tasks (-p option ???) that&lt;br /&gt;
you selected with the ?????? command. When you debug a serial program, select 1.&lt;br /&gt;
&lt;br /&gt;
5. After you have checked all inputs in the ddt startup window, you can start the debugging&lt;br /&gt;
session by pressing the &amp;quot;run&amp;quot; button.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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&lt;br /&gt;
mouse button. So you may step through your program, display the values of variables&lt;br /&gt;
and arrays and look at the message queues.&lt;br /&gt;
&lt;br /&gt;
[[File:ddt2_750.jpg]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=797</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=797"/>
		<updated>2014-03-04T18:21:29Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Section documentation update&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{| style=&amp;quot;border-style: solid; border-width: 1px&amp;quot;&lt;br /&gt;
! Navigation: [[BwHPC_Best_Practices_Repository|bwHPC BPR]] &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--{|  align=&amp;quot;right&amp;quot; {{Table|width=40%}}   --&amp;gt;&lt;br /&gt;
{|{{Softwarebox}}&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;2&amp;quot;  style=&amp;quot;text-align:center&amp;quot; | GDB: The GNU Project Debugger&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
&amp;lt;!-- Neben CIS auch bereits über Kategorien  abgedeckt&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] [[bwForFreiburg]],  [[bwForMannheim-Heidelberg]],  [[bwForUlm]],  [[bwForTübingen]]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [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]&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No &amp;lt;!-- Yes could link to some x11-forwarding/vnc instructions --&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Included in module&lt;br /&gt;
| &amp;lt;!-- mention if there is more than one software in the module, i.e. icc, ifort and debugger --&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Debugging =&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&lt;br /&gt;
== Loading ==&lt;br /&gt;
By default GCC 4.4 and GDB 7.2 are available. If you want to use&lt;br /&gt;
another version of GCC, you have to load it through the module system. For GCC&lt;br /&gt;
version 4.8 and above&lt;br /&gt;
you need GDB 7.7 or higher. In this case load the devel/gdb module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load devel/gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can check the version which is currently used with:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Documentation ==&lt;br /&gt;
For &#039;&#039;&#039;online documentation&#039;&#039;&#039; see the links section in the summary table at the top of this page. For &#039;&#039;&#039;local documentation&#039;&#039;&#039; consult the man page.&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Basic commands ==&lt;br /&gt;
The code you want to debug should be compiled with the -g option and it is recommended that optimization flags are not set. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Core dumps ==&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Multithreaded debugging ==&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Valgrind =&lt;br /&gt;
&lt;br /&gt;
Valgrind can be used for debugging and profiling programs. It ships with various tools, some of them are described in the following table.&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Tool&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| memcheck&lt;br /&gt;
| Detects heap array overruns, memory leaks and incorrect freeing of heap memory.&lt;br /&gt;
|-&lt;br /&gt;
| exp-sgcheck&lt;br /&gt;
| Detects stack and global array overruns.&lt;br /&gt;
|-&lt;br /&gt;
| callgrind&lt;br /&gt;
| Profiler. Helps finding bottlenecks.&lt;br /&gt;
|-&lt;br /&gt;
| helgrind&lt;br /&gt;
| Detects race conditions and deadlocks.&lt;br /&gt;
|-&lt;br /&gt;
| drd&lt;br /&gt;
| Detects race conditions. Does not detect deadlocks but needs less memory than helgrind.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Loading:&#039;&#039;&#039; There is no need for loading valgrind because it is available by default.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Online documentation:&#039;&#039;&#039; http://valgrind.org/docs/&lt;br /&gt;
&#039;&#039;&#039;Local documentation:&#039;&#039;&#039; &amp;lt;pre&amp;gt;man valgrind&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Usage:&#039;&#039;&#039;&lt;br /&gt;
To simulate your program and gather information about it using a specific tool, valgrind can be called like this&lt;br /&gt;
&amp;lt;pre&amp;gt;$ valgrind --tool=&amp;lt;tool&amp;gt; --log-file=&amp;lt;logfile&amp;gt; ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
For example to check for race conditions the command would look like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ valgrind --tool=drd --log-file=log.mem ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Beware that the simulation via valgrind can take much longer and can consume much more memory compared to a normal execution.&lt;br /&gt;
Furthermore the tools can give false positives, i.e. they report errors even though the are none.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example for helgrind and drd:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This small program demonstrates how to detect data races in parallel programs. Both helgrind and drd support POSIX Threads (Pthreads) but not OpenMP so prepare to get many false positives when using these tools with OpenMP.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* pthreads.c */&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;math.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define NUM_THREADS 2&lt;br /&gt;
&lt;br /&gt;
int v = 0;&lt;br /&gt;
&lt;br /&gt;
void* f(void* x)&lt;br /&gt;
{&lt;br /&gt;
    int j = 0;&lt;br /&gt;
    int k = *(int*)x;&lt;br /&gt;
    &lt;br /&gt;
    for(j = 0; j &amp;lt; 100; j++)&lt;br /&gt;
        v += sin(j) + 1 + k;&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    pthread_t threads[NUM_THREADS];&lt;br /&gt;
    int idx[NUM_THREADS];&lt;br /&gt;
    int t;&lt;br /&gt;
   &lt;br /&gt;
    for(t=0; t&amp;lt;NUM_THREADS; t++) {&lt;br /&gt;
        idx[t] = t;&lt;br /&gt;
        pthread_create(&amp;amp;threads[t], NULL, f, (void*)&amp;amp;idx[t]);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    for(t=0; t&amp;lt;NUM_THREADS; t++)&lt;br /&gt;
        pthread_join(threads[t], NULL);&lt;br /&gt;
&lt;br /&gt;
    printf(&amp;quot;%i\n&amp;quot;, v);&lt;br /&gt;
&lt;br /&gt;
    pthread_exit(NULL);&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The program can be compiled with&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc -Wall pthreads.c -o pthreads -pthread -lm -O2&amp;lt;/pre&amp;gt;&lt;br /&gt;
where -pthread tells the compiler to link against the Pthreads library and -lm is needed for linking against the math library.&lt;br /&gt;
This program has a race condition, consecutive executions can give different outputs. Such errors are hard to find because they can show up very rarely.&lt;br /&gt;
Helgrind can tell you the problematic part of your code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ valgrind --tool=helgrind ./pthreads&lt;br /&gt;
...&lt;br /&gt;
==29237== ----------------------------------------------------------------&lt;br /&gt;
==29237== &lt;br /&gt;
==29237== Possible data race during read of size 4 at 0x600C78 by thread #3&lt;br /&gt;
==29237== Locks held: none&lt;br /&gt;
==29237==    at 0x4007BC: f(void*) (in /pfs/data2/home/xx/xxxx/xxxx/pthreads)&lt;br /&gt;
==29237==    by 0x4A0C0D4: mythread_wrapper (hg_intercepts.c:219)&lt;br /&gt;
==29237==    by 0x3EDA6079D0: start_thread (in /lib64/libpthread-2.12.so)&lt;br /&gt;
==29237==    by 0x3ED9AE8B6C: clone (in /lib64/libc-2.12.so)&lt;br /&gt;
==29237== &lt;br /&gt;
==29237== This conflicts with a previous write of size 4 by thread #2&lt;br /&gt;
==29237== Locks held: none&lt;br /&gt;
==29237==    at 0x4007FB: f(void*) (in /pfs/data2/home/xx/xxxx/xxxx/pthreads)&lt;br /&gt;
==29237==    by 0x4A0C0D4: mythread_wrapper (hg_intercepts.c:219)&lt;br /&gt;
==29237==    by 0x3EDA6079D0: start_thread (in /lib64/libpthread-2.12.so)&lt;br /&gt;
==29237==    by 0x3ED9AE8B6C: clone (in /lib64/libc-2.12.so)&lt;br /&gt;
==29237== &lt;br /&gt;
==29237== ----------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now we know that we have to check the function f for a possible data race. A similar output can be achieved using drd with the following command:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ valgrind --tool=drd ./pthreads&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Valgrind can give even better hints when compiled with debug information (helgrind produces similar output):  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g -Wall pthreads.c -o pthreads -pthread -lm&lt;br /&gt;
$ valgrind --tool=drd ./pthreads&lt;br /&gt;
...&lt;br /&gt;
==604== Thread 3:&lt;br /&gt;
==604== Conflicting load by thread 3 at 0x00600cc0 size 4&lt;br /&gt;
==604==    at 0x400749: f(void*) (pthreads.c:17)&lt;br /&gt;
==604==    by 0x4A128B4: vgDrd_thread_wrapper (drd_pthread_intercepts.c:355)&lt;br /&gt;
==604==    by 0x3EDA6079D0: start_thread (in /lib64/libpthread-2.12.so)&lt;br /&gt;
==604==    by 0x3ED9AE8B6C: clone (in /lib64/libc-2.12.so)&lt;br /&gt;
==604== Allocation context: BSS section of fs/data2/home/xx/xxxx/xxxx/pthreads&lt;br /&gt;
==604== Other segment start (thread 2)&lt;br /&gt;
==604==    (thread finished, call stack no longer available)&lt;br /&gt;
==604== Other segment end (thread 2)&lt;br /&gt;
==604==    (thread finished, call stack no longer available)&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The execution can take longer but now we know that the problem is most likely in line 17 (which is correct).&lt;br /&gt;
&lt;br /&gt;
= Parallel Debugger ddt =&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;width:100%; vertical-align:top; background:#f5fffa;border:1px solid #e7aa01;&amp;quot;&lt;br /&gt;
| style=&amp;quot;padding:2px;&amp;quot; | &amp;lt;div style=&amp;quot;margin:3px; background:#cef2e0; font-size:120%; font-weight:bold; border:1px solid #e7aa01; text-align:left; color:#000; padding:0.2em 0.4em;&amp;quot;&amp;gt;Currently only for employees of KIT&amp;lt;/div&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;color:#000;&amp;quot; | &amp;lt;div style=&amp;quot;padding:2px 5px&amp;quot;&amp;gt;On bwUniCluster the GUI based distributed debugging tool (ddt) may be used to debug serial as&lt;br /&gt;
well as parallel applications. For serial applications also the GNU gdb or Intel idb debugger&lt;br /&gt;
may be used. The Intel idb comes with the compiler and information on this tool is available&lt;br /&gt;
together with the compiler documentation. In order to debug your program it must be&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ddt consists of a graphical frontend and a backend serial debugger which controls the&lt;br /&gt;
application program. One instance of the serial debugger controls one MPI process. Via the&lt;br /&gt;
frontend the user interacts with the debugger to select the program that will be debugged,&lt;br /&gt;
to specify different options and to monitor the execution of the program. Debugging&lt;br /&gt;
commands may be sent to one, all or a subset of the MPI processes.&lt;br /&gt;
&lt;br /&gt;
Before the parallel debugger ddt can be used, it is necessary to load the corresponding&lt;br /&gt;
module file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ module use /opt/bwhpc/ka/modulefiles      (only available for employees of KIT)&lt;br /&gt;
$ module add debugger/ddt   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now ddt may be started with the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ ddt program&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where program is the name of your program that you want to debug.&lt;br /&gt;
&lt;br /&gt;
[[File:ddt1_750.jpg]]&lt;br /&gt;
&lt;br /&gt;
Figure: DDT startup window&lt;br /&gt;
&lt;br /&gt;
The above figure shows ddt’s startup window. Before actually starting the debugging session&lt;br /&gt;
you should check the contents of several fields in this window:&lt;br /&gt;
&lt;br /&gt;
1. The top line shows the executable file that will be run under control of the debugger. In&lt;br /&gt;
the following lines you may input some options that are passed to your program or to the&lt;br /&gt;
MPI environment.&lt;br /&gt;
&lt;br /&gt;
2. If your program reads data from stdin you can specify an input file in the startup window.&lt;br /&gt;
&lt;br /&gt;
3. Before starting an MPI program you should check that &amp;quot;Open MPI (Compatability)&amp;quot; or&lt;br /&gt;
&amp;quot;Intel MPI&amp;quot; is the MPI implementation that has been selected. If this is not the case, you&lt;br /&gt;
have to change this. Otherwise ddt may not be able to run your program. In order to debug&lt;br /&gt;
serial programs, the selected MPI implementation should be &amp;quot;none&amp;quot;. You may also change&lt;br /&gt;
the underlying serial debugger using the &amp;quot;change&amp;quot; button. By default ddt uses its own serial debugger, but it may also use the Intel idb debugger.&lt;br /&gt;
&lt;br /&gt;
4. Select the number of MPI processes that will be started by ddt. If you are using ddt within&lt;br /&gt;
a batch job, replace mpirun by ddt in the command line of ????? and make sure that the&lt;br /&gt;
chosen number of MPI processes is identical to the number of MPI tasks (-p option ???) that&lt;br /&gt;
you selected with the ?????? command. When you debug a serial program, select 1.&lt;br /&gt;
&lt;br /&gt;
5. After you have checked all inputs in the ddt startup window, you can start the debugging&lt;br /&gt;
session by pressing the &amp;quot;run&amp;quot; button.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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&lt;br /&gt;
mouse button. So you may step through your program, display the values of variables&lt;br /&gt;
and arrays and look at the message queues.&lt;br /&gt;
&lt;br /&gt;
[[File:ddt2_750.jpg]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=796</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=796"/>
		<updated>2014-03-04T18:12:52Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Load section update for gdb 7.7&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{| style=&amp;quot;border-style: solid; border-width: 1px&amp;quot;&lt;br /&gt;
! Navigation: [[BwHPC_Best_Practices_Repository|bwHPC BPR]] &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--{|  align=&amp;quot;right&amp;quot; {{Table|width=40%}}   --&amp;gt;&lt;br /&gt;
{|{{Softwarebox}}&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;2&amp;quot;  style=&amp;quot;text-align:center&amp;quot; | GDB: The GNU Project Debugger&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
&amp;lt;!-- Neben CIS auch bereits über Kategorien  abgedeckt&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] [[bwForFreiburg]],  [[bwForMannheim-Heidelberg]],  [[bwForUlm]],  [[bwForTübingen]]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [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]&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No &amp;lt;!-- Yes could link to some x11-forwarding/vnc instructions --&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Included in module&lt;br /&gt;
| &amp;lt;!-- mention if there is more than one software in the module, i.e. icc, ifort and debugger --&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Debugging =&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&lt;br /&gt;
== Loading ==&lt;br /&gt;
By default GCC 4.4 and GDB 7.2 are available. If you want to use&lt;br /&gt;
another version of GCC, you have to load it through the module system. For GCC&lt;br /&gt;
version 4.8 and above&lt;br /&gt;
you need GDB 7.7 or higher. In this case load the devel/gdb module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load devel/gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can check the version which is currently used with:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb --version&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Documentation ==&lt;br /&gt;
&#039;&#039;&#039;Online documentation:&#039;&#039;&#039; [http://www.gnu.org/software/gdb/documentation/ GDB documentation] and [http://software.intel.com/sites/products/documentation/doclib/stdxe/2013/composerxe/debugger/user_guide/index.htm IDB Documentation]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Local documentation:&#039;&#039;&#039;&lt;br /&gt;
For detailed lists of the different program options consult the man page:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Basic commands ==&lt;br /&gt;
The code you want to debug should be compiled with the -g option and it is recommended that optimization flags are not set. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Core dumps ==&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Multithreaded debugging ==&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Valgrind =&lt;br /&gt;
&lt;br /&gt;
Valgrind can be used for debugging and profiling programs. It ships with various tools, some of them are described in the following table.&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Tool&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| memcheck&lt;br /&gt;
| Detects heap array overruns, memory leaks and incorrect freeing of heap memory.&lt;br /&gt;
|-&lt;br /&gt;
| exp-sgcheck&lt;br /&gt;
| Detects stack and global array overruns.&lt;br /&gt;
|-&lt;br /&gt;
| callgrind&lt;br /&gt;
| Profiler. Helps finding bottlenecks.&lt;br /&gt;
|-&lt;br /&gt;
| helgrind&lt;br /&gt;
| Detects race conditions and deadlocks.&lt;br /&gt;
|-&lt;br /&gt;
| drd&lt;br /&gt;
| Detects race conditions. Does not detect deadlocks but needs less memory than helgrind.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Loading:&#039;&#039;&#039; There is no need for loading valgrind because it is available by default.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Online documentation:&#039;&#039;&#039; http://valgrind.org/docs/&lt;br /&gt;
&#039;&#039;&#039;Local documentation:&#039;&#039;&#039; &amp;lt;pre&amp;gt;man valgrind&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Usage:&#039;&#039;&#039;&lt;br /&gt;
To simulate your program and gather information about it using a specific tool, valgrind can be called like this&lt;br /&gt;
&amp;lt;pre&amp;gt;$ valgrind --tool=&amp;lt;tool&amp;gt; --log-file=&amp;lt;logfile&amp;gt; ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
For example to check for race conditions the command would look like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ valgrind --tool=drd --log-file=log.mem ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Beware that the simulation via valgrind can take much longer and can consume much more memory compared to a normal execution.&lt;br /&gt;
Furthermore the tools can give false positives, i.e. they report errors even though the are none.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example for helgrind and drd:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This small program demonstrates how to detect data races in parallel programs. Both helgrind and drd support POSIX Threads (Pthreads) but not OpenMP so prepare to get many false positives when using these tools with OpenMP.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* pthreads.c */&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;math.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define NUM_THREADS 2&lt;br /&gt;
&lt;br /&gt;
int v = 0;&lt;br /&gt;
&lt;br /&gt;
void* f(void* x)&lt;br /&gt;
{&lt;br /&gt;
    int j = 0;&lt;br /&gt;
    int k = *(int*)x;&lt;br /&gt;
    &lt;br /&gt;
    for(j = 0; j &amp;lt; 100; j++)&lt;br /&gt;
        v += sin(j) + 1 + k;&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    pthread_t threads[NUM_THREADS];&lt;br /&gt;
    int idx[NUM_THREADS];&lt;br /&gt;
    int t;&lt;br /&gt;
   &lt;br /&gt;
    for(t=0; t&amp;lt;NUM_THREADS; t++) {&lt;br /&gt;
        idx[t] = t;&lt;br /&gt;
        pthread_create(&amp;amp;threads[t], NULL, f, (void*)&amp;amp;idx[t]);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    for(t=0; t&amp;lt;NUM_THREADS; t++)&lt;br /&gt;
        pthread_join(threads[t], NULL);&lt;br /&gt;
&lt;br /&gt;
    printf(&amp;quot;%i\n&amp;quot;, v);&lt;br /&gt;
&lt;br /&gt;
    pthread_exit(NULL);&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The program can be compiled with&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc -Wall pthreads.c -o pthreads -pthread -lm -O2&amp;lt;/pre&amp;gt;&lt;br /&gt;
where -pthread tells the compiler to link against the Pthreads library and -lm is needed for linking against the math library.&lt;br /&gt;
This program has a race condition, consecutive executions can give different outputs. Such errors are hard to find because they can show up very rarely.&lt;br /&gt;
Helgrind can tell you the problematic part of your code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ valgrind --tool=helgrind ./pthreads&lt;br /&gt;
...&lt;br /&gt;
==29237== ----------------------------------------------------------------&lt;br /&gt;
==29237== &lt;br /&gt;
==29237== Possible data race during read of size 4 at 0x600C78 by thread #3&lt;br /&gt;
==29237== Locks held: none&lt;br /&gt;
==29237==    at 0x4007BC: f(void*) (in /pfs/data2/home/xx/xxxx/xxxx/pthreads)&lt;br /&gt;
==29237==    by 0x4A0C0D4: mythread_wrapper (hg_intercepts.c:219)&lt;br /&gt;
==29237==    by 0x3EDA6079D0: start_thread (in /lib64/libpthread-2.12.so)&lt;br /&gt;
==29237==    by 0x3ED9AE8B6C: clone (in /lib64/libc-2.12.so)&lt;br /&gt;
==29237== &lt;br /&gt;
==29237== This conflicts with a previous write of size 4 by thread #2&lt;br /&gt;
==29237== Locks held: none&lt;br /&gt;
==29237==    at 0x4007FB: f(void*) (in /pfs/data2/home/xx/xxxx/xxxx/pthreads)&lt;br /&gt;
==29237==    by 0x4A0C0D4: mythread_wrapper (hg_intercepts.c:219)&lt;br /&gt;
==29237==    by 0x3EDA6079D0: start_thread (in /lib64/libpthread-2.12.so)&lt;br /&gt;
==29237==    by 0x3ED9AE8B6C: clone (in /lib64/libc-2.12.so)&lt;br /&gt;
==29237== &lt;br /&gt;
==29237== ----------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now we know that we have to check the function f for a possible data race. A similar output can be achieved using drd with the following command:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ valgrind --tool=drd ./pthreads&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Valgrind can give even better hints when compiled with debug information (helgrind produces similar output):  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g -Wall pthreads.c -o pthreads -pthread -lm&lt;br /&gt;
$ valgrind --tool=drd ./pthreads&lt;br /&gt;
...&lt;br /&gt;
==604== Thread 3:&lt;br /&gt;
==604== Conflicting load by thread 3 at 0x00600cc0 size 4&lt;br /&gt;
==604==    at 0x400749: f(void*) (pthreads.c:17)&lt;br /&gt;
==604==    by 0x4A128B4: vgDrd_thread_wrapper (drd_pthread_intercepts.c:355)&lt;br /&gt;
==604==    by 0x3EDA6079D0: start_thread (in /lib64/libpthread-2.12.so)&lt;br /&gt;
==604==    by 0x3ED9AE8B6C: clone (in /lib64/libc-2.12.so)&lt;br /&gt;
==604== Allocation context: BSS section of fs/data2/home/xx/xxxx/xxxx/pthreads&lt;br /&gt;
==604== Other segment start (thread 2)&lt;br /&gt;
==604==    (thread finished, call stack no longer available)&lt;br /&gt;
==604== Other segment end (thread 2)&lt;br /&gt;
==604==    (thread finished, call stack no longer available)&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The execution can take longer but now we know that the problem is most likely in line 17 (which is correct).&lt;br /&gt;
&lt;br /&gt;
= Parallel Debugger ddt =&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;width:100%; vertical-align:top; background:#f5fffa;border:1px solid #e7aa01;&amp;quot;&lt;br /&gt;
| style=&amp;quot;padding:2px;&amp;quot; | &amp;lt;div style=&amp;quot;margin:3px; background:#cef2e0; font-size:120%; font-weight:bold; border:1px solid #e7aa01; text-align:left; color:#000; padding:0.2em 0.4em;&amp;quot;&amp;gt;Currently only for employees of KIT&amp;lt;/div&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;color:#000;&amp;quot; | &amp;lt;div style=&amp;quot;padding:2px 5px&amp;quot;&amp;gt;On bwUniCluster the GUI based distributed debugging tool (ddt) may be used to debug serial as&lt;br /&gt;
well as parallel applications. For serial applications also the GNU gdb or Intel idb debugger&lt;br /&gt;
may be used. The Intel idb comes with the compiler and information on this tool is available&lt;br /&gt;
together with the compiler documentation. In order to debug your program it must be&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ddt consists of a graphical frontend and a backend serial debugger which controls the&lt;br /&gt;
application program. One instance of the serial debugger controls one MPI process. Via the&lt;br /&gt;
frontend the user interacts with the debugger to select the program that will be debugged,&lt;br /&gt;
to specify different options and to monitor the execution of the program. Debugging&lt;br /&gt;
commands may be sent to one, all or a subset of the MPI processes.&lt;br /&gt;
&lt;br /&gt;
Before the parallel debugger ddt can be used, it is necessary to load the corresponding&lt;br /&gt;
module file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ module use /opt/bwhpc/ka/modulefiles      (only available for employees of KIT)&lt;br /&gt;
$ module add debugger/ddt   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now ddt may be started with the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ ddt program&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where program is the name of your program that you want to debug.&lt;br /&gt;
&lt;br /&gt;
[[File:ddt1_750.jpg]]&lt;br /&gt;
&lt;br /&gt;
Figure: DDT startup window&lt;br /&gt;
&lt;br /&gt;
The above figure shows ddt’s startup window. Before actually starting the debugging session&lt;br /&gt;
you should check the contents of several fields in this window:&lt;br /&gt;
&lt;br /&gt;
1. The top line shows the executable file that will be run under control of the debugger. In&lt;br /&gt;
the following lines you may input some options that are passed to your program or to the&lt;br /&gt;
MPI environment.&lt;br /&gt;
&lt;br /&gt;
2. If your program reads data from stdin you can specify an input file in the startup window.&lt;br /&gt;
&lt;br /&gt;
3. Before starting an MPI program you should check that &amp;quot;Open MPI (Compatability)&amp;quot; or&lt;br /&gt;
&amp;quot;Intel MPI&amp;quot; is the MPI implementation that has been selected. If this is not the case, you&lt;br /&gt;
have to change this. Otherwise ddt may not be able to run your program. In order to debug&lt;br /&gt;
serial programs, the selected MPI implementation should be &amp;quot;none&amp;quot;. You may also change&lt;br /&gt;
the underlying serial debugger using the &amp;quot;change&amp;quot; button. By default ddt uses its own serial debugger, but it may also use the Intel idb debugger.&lt;br /&gt;
&lt;br /&gt;
4. Select the number of MPI processes that will be started by ddt. If you are using ddt within&lt;br /&gt;
a batch job, replace mpirun by ddt in the command line of ????? and make sure that the&lt;br /&gt;
chosen number of MPI processes is identical to the number of MPI tasks (-p option ???) that&lt;br /&gt;
you selected with the ?????? command. When you debug a serial program, select 1.&lt;br /&gt;
&lt;br /&gt;
5. After you have checked all inputs in the ddt startup window, you can start the debugging&lt;br /&gt;
session by pressing the &amp;quot;run&amp;quot; button.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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&lt;br /&gt;
mouse button. So you may step through your program, display the values of variables&lt;br /&gt;
and arrays and look at the message queues.&lt;br /&gt;
&lt;br /&gt;
[[File:ddt2_750.jpg]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=795</id>
		<title>Development/GDB</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=Development/GDB&amp;diff=795"/>
		<updated>2014-03-04T13:13:52Z</updated>

		<summary type="html">&lt;p&gt;R Doros: Table Header included&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{| style=&amp;quot;border-style: solid; border-width: 1px&amp;quot;&lt;br /&gt;
! Navigation: [[BwHPC_Best_Practices_Repository|bwHPC BPR]] &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--{|  align=&amp;quot;right&amp;quot; {{Table|width=40%}}   --&amp;gt;&lt;br /&gt;
{|{{Softwarebox}}&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;2&amp;quot;  style=&amp;quot;text-align:center&amp;quot; | GDB: The GNU Project Debugger&lt;br /&gt;
|-&lt;br /&gt;
| module load&lt;br /&gt;
| devel/gdb&lt;br /&gt;
&amp;lt;!-- Neben CIS auch bereits über Kategorien  abgedeckt&lt;br /&gt;
|-&lt;br /&gt;
| Availability&lt;br /&gt;
| [[bwUniCluster]] [[bwForFreiburg]],  [[bwForMannheim-Heidelberg]],  [[bwForUlm]],  [[bwForTübingen]]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| License&lt;br /&gt;
| [http://www.gnu.org/licenses/ GPL]&lt;br /&gt;
|-&lt;br /&gt;
|Citing&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Links&lt;br /&gt;
| [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]&lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| Graphical Interface&lt;br /&gt;
| No &amp;lt;!-- Yes could link to some x11-forwarding/vnc instructions --&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Included in module&lt;br /&gt;
| &amp;lt;!-- mention if there is more than one software in the module, i.e. icc, ifort and debugger --&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Debugging =&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;GNU Debugger (GDB)&#039;&#039;&#039; is a standard debugger for serial programs although it can be used for parallel and even distributed programs with few processes too. The &#039;&#039;&#039;Intel Debugger (IDB)&#039;&#039;&#039; uses the same commands for basic debugging as GDB and hence can be used instead of GDB just by substituting idbc for gdb.&lt;br /&gt;
&lt;br /&gt;
== Loading ==&lt;br /&gt;
There is no need for loading GDB because it is available by default. If you want to use IDB load the Intel compiler module:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ module load compiler/intel&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Documentation ==&lt;br /&gt;
&#039;&#039;&#039;Online documentation:&#039;&#039;&#039; [http://www.gnu.org/software/gdb/documentation/ GDB documentation] and [http://software.intel.com/sites/products/documentation/doclib/stdxe/2013/composerxe/debugger/user_guide/index.htm IDB Documentation]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Local documentation:&#039;&#039;&#039;&lt;br /&gt;
For detailed lists of the different program options consult the man page:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man gdb&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;$ man idb&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Basic commands ==&lt;br /&gt;
The code you want to debug should be compiled with the -g option and it is recommended that optimization flags are not set. To start a debug session for a program execute GDB with the program path as parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside GDB is a prompt where you can enter commands. Important commands are listed below.&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| help &#039;&#039;cmd&#039;&#039;&lt;br /&gt;
| Show help for command &#039;&#039;cmd&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| break &#039;&#039;func&#039;&#039;&lt;br /&gt;
| Set a breakpoint at function &#039;&#039;func&#039;&#039;.	&lt;br /&gt;
|-&lt;br /&gt;
| run&lt;br /&gt;
| Start program.&lt;br /&gt;
|-&lt;br /&gt;
| next&lt;br /&gt;
| Go to next program line. Do not enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| step&lt;br /&gt;
| Go to next program line. Enter functions.&lt;br /&gt;
|-&lt;br /&gt;
| list&lt;br /&gt;
| Show the surrounding source code of the currently processed line.&lt;br /&gt;
|-&lt;br /&gt;
| print &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Print the value of the expression &#039;&#039;expr&#039;&#039;.&lt;br /&gt;
|-&lt;br /&gt;
| display &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Display the value of the expression &#039;&#039;expr&#039;&#039; every time the program stops.&lt;br /&gt;
|-&lt;br /&gt;
| watch &#039;&#039;expr&#039;&#039;&lt;br /&gt;
| Stop when value of the expression &#039;&#039;expr&#039;&#039; changes.&lt;br /&gt;
|-&lt;br /&gt;
| continue&lt;br /&gt;
| Continue execution until a breakpoint or a watchpoint appears.&lt;br /&gt;
|-&lt;br /&gt;
| backtrace&lt;br /&gt;
| Print a list of functions that are currently active.&lt;br /&gt;
|-&lt;br /&gt;
| quit&lt;br /&gt;
| Exit GDB.		&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
We debug the following program called bug.c which crashes on execution.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int global = 0;&lt;br /&gt;
&lt;br /&gt;
void begin() {&lt;br /&gt;
    global = 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
    int v[2];&lt;br /&gt;
    int i, k;&lt;br /&gt;
&lt;br /&gt;
    for(i = 0; i &amp;lt; 8; i++) {&lt;br /&gt;
        k = i/2*2;	/* should have been k = i/(2*2); */&lt;br /&gt;
        v[k] = i;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void end() {&lt;br /&gt;
    global = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    begin();&lt;br /&gt;
    loop();&lt;br /&gt;
    end();&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Sample GDB session:&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g bug.c -o bug&lt;br /&gt;
$ gdb ./bug&lt;br /&gt;
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)&lt;br /&gt;
Copyright (C) 2010 Free Software Foundation, Inc.&lt;br /&gt;
License GPLv3+: GNU GPL version 3 or later &amp;lt;http://gnu.org/licenses/gpl.html&amp;gt;&lt;br /&gt;
This is free software: you are free to change and redistribute it.&lt;br /&gt;
There is NO WARRANTY, to the extent permitted by law.  Type &amp;quot;show copying&amp;quot;&lt;br /&gt;
and &amp;quot;show warranty&amp;quot; for details.&lt;br /&gt;
This GDB was configured as &amp;quot;x86_64-redhat-linux-gnu&amp;quot;.&lt;br /&gt;
For bug reporting instructions, please see:&lt;br /&gt;
&amp;lt;http://www.gnu.org/software/gdb/bugs/&amp;gt;...&lt;br /&gt;
Reading symbols from /pfs/data2/home/xx/xxx/xxxx/bug...done.&lt;br /&gt;
(gdb) break main&lt;br /&gt;
Breakpoint 1 at 0x4005b2: file bug.c, line 26.&lt;br /&gt;
(gdb) run&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libgcc-4.4.7-4.el6.x86_64&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) next&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x0000000000000005 in ?? ()&lt;br /&gt;
(gdb) # now we know that the bug is in loop(). start again.&lt;br /&gt;
(gdb) run&lt;br /&gt;
The program being debugged has been started already.&lt;br /&gt;
Start it from the beginning? (y or n) y&lt;br /&gt;
Starting program: /pfs/data2/home/xx/xxx/xxxx/bug&lt;br /&gt;
&lt;br /&gt;
Breakpoint 1, main () at bug.c:26&lt;br /&gt;
26              begin();&lt;br /&gt;
(gdb) next&lt;br /&gt;
27              loop();&lt;br /&gt;
(gdb) step&lt;br /&gt;
loop () at bug.c:13&lt;br /&gt;
13              for(i = 0; i &amp;lt; 8; i++)&lt;br /&gt;
(gdb) next&lt;br /&gt;
15                      k = i/2*2;&lt;br /&gt;
(gdb) next&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # maybe k gets too big?&lt;br /&gt;
(gdb) watch (k &amp;gt;= 2)&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
(gdb) continue&lt;br /&gt;
Continuing.&lt;br /&gt;
Hardware watchpoint 2: (k &amp;gt;= 2)&lt;br /&gt;
&lt;br /&gt;
Old value = 0&lt;br /&gt;
New value = 1&lt;br /&gt;
loop () at bug.c:16&lt;br /&gt;
16                      v[k] = i;&lt;br /&gt;
(gdb) # k is too big&lt;br /&gt;
(gdb) print k&lt;br /&gt;
$1 = 2&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$2 = 2&lt;br /&gt;
(gdb) quit&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Core dumps ==&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ ulimit -c unlimited&amp;lt;/pre&amp;gt;&lt;br /&gt;
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):&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gdb ./ex core.xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Multithreaded debugging ==&lt;br /&gt;
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.&lt;br /&gt;
The following commands are useful for multithreaded debugging:&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| info threads&lt;br /&gt;
| Shows the status of all existing threads.&lt;br /&gt;
|-&lt;br /&gt;
| thread &#039;&#039;num&#039;&#039;&lt;br /&gt;
| Switches to the thread with the number &#039;&#039;num&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Valgrind =&lt;br /&gt;
&lt;br /&gt;
Valgrind can be used for debugging and profiling programs. It ships with various tools, some of them are described in the following table.&lt;br /&gt;
&lt;br /&gt;
{| {{Table}}&lt;br /&gt;
|-&lt;br /&gt;
! Tool&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| memcheck&lt;br /&gt;
| Detects heap array overruns, memory leaks and incorrect freeing of heap memory.&lt;br /&gt;
|-&lt;br /&gt;
| exp-sgcheck&lt;br /&gt;
| Detects stack and global array overruns.&lt;br /&gt;
|-&lt;br /&gt;
| callgrind&lt;br /&gt;
| Profiler. Helps finding bottlenecks.&lt;br /&gt;
|-&lt;br /&gt;
| helgrind&lt;br /&gt;
| Detects race conditions and deadlocks.&lt;br /&gt;
|-&lt;br /&gt;
| drd&lt;br /&gt;
| Detects race conditions. Does not detect deadlocks but needs less memory than helgrind.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Loading:&#039;&#039;&#039; There is no need for loading valgrind because it is available by default.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Online documentation:&#039;&#039;&#039; http://valgrind.org/docs/&lt;br /&gt;
&#039;&#039;&#039;Local documentation:&#039;&#039;&#039; &amp;lt;pre&amp;gt;man valgrind&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Usage:&#039;&#039;&#039;&lt;br /&gt;
To simulate your program and gather information about it using a specific tool, valgrind can be called like this&lt;br /&gt;
&amp;lt;pre&amp;gt;$ valgrind --tool=&amp;lt;tool&amp;gt; --log-file=&amp;lt;logfile&amp;gt; ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
For example to check for race conditions the command would look like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ valgrind --tool=drd --log-file=log.mem ./example&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Beware that the simulation via valgrind can take much longer and can consume much more memory compared to a normal execution.&lt;br /&gt;
Furthermore the tools can give false positives, i.e. they report errors even though the are none.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example for helgrind and drd:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This small program demonstrates how to detect data races in parallel programs. Both helgrind and drd support POSIX Threads (Pthreads) but not OpenMP so prepare to get many false positives when using these tools with OpenMP.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* pthreads.c */&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;math.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define NUM_THREADS 2&lt;br /&gt;
&lt;br /&gt;
int v = 0;&lt;br /&gt;
&lt;br /&gt;
void* f(void* x)&lt;br /&gt;
{&lt;br /&gt;
    int j = 0;&lt;br /&gt;
    int k = *(int*)x;&lt;br /&gt;
    &lt;br /&gt;
    for(j = 0; j &amp;lt; 100; j++)&lt;br /&gt;
        v += sin(j) + 1 + k;&lt;br /&gt;
&lt;br /&gt;
    return NULL;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    pthread_t threads[NUM_THREADS];&lt;br /&gt;
    int idx[NUM_THREADS];&lt;br /&gt;
    int t;&lt;br /&gt;
   &lt;br /&gt;
    for(t=0; t&amp;lt;NUM_THREADS; t++) {&lt;br /&gt;
        idx[t] = t;&lt;br /&gt;
        pthread_create(&amp;amp;threads[t], NULL, f, (void*)&amp;amp;idx[t]);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    for(t=0; t&amp;lt;NUM_THREADS; t++)&lt;br /&gt;
        pthread_join(threads[t], NULL);&lt;br /&gt;
&lt;br /&gt;
    printf(&amp;quot;%i\n&amp;quot;, v);&lt;br /&gt;
&lt;br /&gt;
    pthread_exit(NULL);&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The program can be compiled with&lt;br /&gt;
&amp;lt;pre&amp;gt;$ gcc -Wall pthreads.c -o pthreads -pthread -lm -O2&amp;lt;/pre&amp;gt;&lt;br /&gt;
where -pthread tells the compiler to link against the Pthreads library and -lm is needed for linking against the math library.&lt;br /&gt;
This program has a race condition, consecutive executions can give different outputs. Such errors are hard to find because they can show up very rarely.&lt;br /&gt;
Helgrind can tell you the problematic part of your code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ valgrind --tool=helgrind ./pthreads&lt;br /&gt;
...&lt;br /&gt;
==29237== ----------------------------------------------------------------&lt;br /&gt;
==29237== &lt;br /&gt;
==29237== Possible data race during read of size 4 at 0x600C78 by thread #3&lt;br /&gt;
==29237== Locks held: none&lt;br /&gt;
==29237==    at 0x4007BC: f(void*) (in /pfs/data2/home/xx/xxxx/xxxx/pthreads)&lt;br /&gt;
==29237==    by 0x4A0C0D4: mythread_wrapper (hg_intercepts.c:219)&lt;br /&gt;
==29237==    by 0x3EDA6079D0: start_thread (in /lib64/libpthread-2.12.so)&lt;br /&gt;
==29237==    by 0x3ED9AE8B6C: clone (in /lib64/libc-2.12.so)&lt;br /&gt;
==29237== &lt;br /&gt;
==29237== This conflicts with a previous write of size 4 by thread #2&lt;br /&gt;
==29237== Locks held: none&lt;br /&gt;
==29237==    at 0x4007FB: f(void*) (in /pfs/data2/home/xx/xxxx/xxxx/pthreads)&lt;br /&gt;
==29237==    by 0x4A0C0D4: mythread_wrapper (hg_intercepts.c:219)&lt;br /&gt;
==29237==    by 0x3EDA6079D0: start_thread (in /lib64/libpthread-2.12.so)&lt;br /&gt;
==29237==    by 0x3ED9AE8B6C: clone (in /lib64/libc-2.12.so)&lt;br /&gt;
==29237== &lt;br /&gt;
==29237== ----------------------------------------------------------------&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now we know that we have to check the function f for a possible data race. A similar output can be achieved using drd with the following command:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ valgrind --tool=drd ./pthreads&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Valgrind can give even better hints when compiled with debug information (helgrind produces similar output):  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gcc -g -Wall pthreads.c -o pthreads -pthread -lm&lt;br /&gt;
$ valgrind --tool=drd ./pthreads&lt;br /&gt;
...&lt;br /&gt;
==604== Thread 3:&lt;br /&gt;
==604== Conflicting load by thread 3 at 0x00600cc0 size 4&lt;br /&gt;
==604==    at 0x400749: f(void*) (pthreads.c:17)&lt;br /&gt;
==604==    by 0x4A128B4: vgDrd_thread_wrapper (drd_pthread_intercepts.c:355)&lt;br /&gt;
==604==    by 0x3EDA6079D0: start_thread (in /lib64/libpthread-2.12.so)&lt;br /&gt;
==604==    by 0x3ED9AE8B6C: clone (in /lib64/libc-2.12.so)&lt;br /&gt;
==604== Allocation context: BSS section of fs/data2/home/xx/xxxx/xxxx/pthreads&lt;br /&gt;
==604== Other segment start (thread 2)&lt;br /&gt;
==604==    (thread finished, call stack no longer available)&lt;br /&gt;
==604== Other segment end (thread 2)&lt;br /&gt;
==604==    (thread finished, call stack no longer available)&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The execution can take longer but now we know that the problem is most likely in line 17 (which is correct).&lt;br /&gt;
&lt;br /&gt;
= Parallel Debugger ddt =&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;width:100%; vertical-align:top; background:#f5fffa;border:1px solid #e7aa01;&amp;quot;&lt;br /&gt;
| style=&amp;quot;padding:2px;&amp;quot; | &amp;lt;div style=&amp;quot;margin:3px; background:#cef2e0; font-size:120%; font-weight:bold; border:1px solid #e7aa01; text-align:left; color:#000; padding:0.2em 0.4em;&amp;quot;&amp;gt;Currently only for employees of KIT&amp;lt;/div&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;color:#000;&amp;quot; | &amp;lt;div style=&amp;quot;padding:2px 5px&amp;quot;&amp;gt;On bwUniCluster the GUI based distributed debugging tool (ddt) may be used to debug serial as&lt;br /&gt;
well as parallel applications. For serial applications also the GNU gdb or Intel idb debugger&lt;br /&gt;
may be used. The Intel idb comes with the compiler and information on this tool is available&lt;br /&gt;
together with the compiler documentation. In order to debug your program it must be&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ddt consists of a graphical frontend and a backend serial debugger which controls the&lt;br /&gt;
application program. One instance of the serial debugger controls one MPI process. Via the&lt;br /&gt;
frontend the user interacts with the debugger to select the program that will be debugged,&lt;br /&gt;
to specify different options and to monitor the execution of the program. Debugging&lt;br /&gt;
commands may be sent to one, all or a subset of the MPI processes.&lt;br /&gt;
&lt;br /&gt;
Before the parallel debugger ddt can be used, it is necessary to load the corresponding&lt;br /&gt;
module file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ module use /opt/bwhpc/ka/modulefiles      (only available for employees of KIT)&lt;br /&gt;
$ module add debugger/ddt   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now ddt may be started with the command&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ ddt program&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where program is the name of your program that you want to debug.&lt;br /&gt;
&lt;br /&gt;
[[File:ddt1_750.jpg]]&lt;br /&gt;
&lt;br /&gt;
Figure: DDT startup window&lt;br /&gt;
&lt;br /&gt;
The above figure shows ddt’s startup window. Before actually starting the debugging session&lt;br /&gt;
you should check the contents of several fields in this window:&lt;br /&gt;
&lt;br /&gt;
1. The top line shows the executable file that will be run under control of the debugger. In&lt;br /&gt;
the following lines you may input some options that are passed to your program or to the&lt;br /&gt;
MPI environment.&lt;br /&gt;
&lt;br /&gt;
2. If your program reads data from stdin you can specify an input file in the startup window.&lt;br /&gt;
&lt;br /&gt;
3. Before starting an MPI program you should check that &amp;quot;Open MPI (Compatability)&amp;quot; or&lt;br /&gt;
&amp;quot;Intel MPI&amp;quot; is the MPI implementation that has been selected. If this is not the case, you&lt;br /&gt;
have to change this. Otherwise ddt may not be able to run your program. In order to debug&lt;br /&gt;
serial programs, the selected MPI implementation should be &amp;quot;none&amp;quot;. You may also change&lt;br /&gt;
the underlying serial debugger using the &amp;quot;change&amp;quot; button. By default ddt uses its own serial debugger, but it may also use the Intel idb debugger.&lt;br /&gt;
&lt;br /&gt;
4. Select the number of MPI processes that will be started by ddt. If you are using ddt within&lt;br /&gt;
a batch job, replace mpirun by ddt in the command line of ????? and make sure that the&lt;br /&gt;
chosen number of MPI processes is identical to the number of MPI tasks (-p option ???) that&lt;br /&gt;
you selected with the ?????? command. When you debug a serial program, select 1.&lt;br /&gt;
&lt;br /&gt;
5. After you have checked all inputs in the ddt startup window, you can start the debugging&lt;br /&gt;
session by pressing the &amp;quot;run&amp;quot; button.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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&lt;br /&gt;
mouse button. So you may step through your program, display the values of variables&lt;br /&gt;
and arrays and look at the message queues.&lt;br /&gt;
&lt;br /&gt;
[[File:ddt2_750.jpg]]&lt;/div&gt;</summary>
		<author><name>R Doros</name></author>
	</entry>
</feed>