3
<title>Using gdb - GNU Scientific Library -- Reference Manual</title>
4
<meta http-equiv="Content-Type" content="text/html">
5
<meta name="description" content="GNU Scientific Library -- Reference Manual">
6
<meta name="generator" content="makeinfo 4.8">
7
<link title="Top" rel="start" href="index.html#Top">
8
<link rel="up" href="Debugging-Numerical-Programs.html#Debugging-Numerical-Programs" title="Debugging Numerical Programs">
9
<link rel="next" href="Examining-floating-point-registers.html#Examining-floating-point-registers" title="Examining floating point registers">
10
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
12
Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 The GSL Team.
14
Permission is granted to copy, distribute and/or modify this document
15
under the terms of the GNU Free Documentation License, Version 1.2 or
16
any later version published by the Free Software Foundation; with the
17
Invariant Sections being ``GNU General Public License'' and ``Free Software
18
Needs Free Documentation'', the Front-Cover text being ``A GNU Manual'',
19
and with the Back-Cover Text being (a) (see below). A copy of the
20
license is included in the section entitled ``GNU Free Documentation
23
(a) The Back-Cover Text is: ``You have freedom to copy and modify this
24
GNU Manual, like GNU software.''-->
25
<meta http-equiv="Content-Style-Type" content="text/css">
26
<style type="text/css"><!--
27
pre.display { font-family:inherit }
28
pre.format { font-family:inherit }
29
pre.smalldisplay { font-family:inherit; font-size:smaller }
30
pre.smallformat { font-family:inherit; font-size:smaller }
31
pre.smallexample { font-size:smaller }
32
pre.smalllisp { font-size:smaller }
33
span.sc { font-variant:small-caps }
34
span.roman { font-family:serif; font-weight:normal; }
35
span.sansserif { font-family:sans-serif; font-weight:normal; }
41
<a name="Using-gdb"></a>
42
Next: <a rel="next" accesskey="n" href="Examining-floating-point-registers.html#Examining-floating-point-registers">Examining floating point registers</a>,
43
Up: <a rel="up" accesskey="u" href="Debugging-Numerical-Programs.html#Debugging-Numerical-Programs">Debugging Numerical Programs</a>
47
<h3 class="section">A.1 Using gdb</h3>
49
<p><a name="index-gdb-2367"></a><a name="index-debugging-numerical-programs-2368"></a><a name="index-breakpoints-2369"></a>Any errors reported by the library are passed to the function
50
<code>gsl_error</code>. By running your programs under gdb and setting a
51
breakpoint in this function you can automatically catch any library
52
errors. You can add a breakpoint for every session by putting
54
<pre class="example"> break gsl_error
56
<p class="noindent">into your <samp><span class="file">.gdbinit</span></samp> file in the directory where your program is
59
<p>If the breakpoint catches an error then you can use a backtrace
60
(<code>bt</code>) to see the call-tree, and the arguments which possibly
61
caused the error. By moving up into the calling function you can
62
investigate the values of variables at that point. Here is an example
63
from the program <code>fft/test_trap</code>, which contains the following
66
<pre class="smallexample"> status = gsl_fft_complex_wavetable_alloc (0, &complex_wavetable);
68
<p class="noindent">The function <code>gsl_fft_complex_wavetable_alloc</code> takes the length of
69
an FFT as its first argument. When this line is executed an error will
70
be generated because the length of an FFT is not allowed to be zero.
72
<p>To debug this problem we start <code>gdb</code>, using the file
73
<samp><span class="file">.gdbinit</span></samp> to define a breakpoint in <code>gsl_error</code>,
75
<pre class="smallexample"> $ gdb test_trap
77
GDB is free software and you are welcome to distribute copies
78
of it under certain conditions; type "show copying" to see
79
the conditions. There is absolutely no warranty for GDB;
80
type "show warranty" for details. GDB 4.16 (i586-debian-linux),
81
Copyright 1996 Free Software Foundation, Inc.
83
Breakpoint 1 at 0x8050b1e: file error.c, line 14.
85
<p class="noindent">When we run the program this breakpoint catches the error and shows the
88
<pre class="smallexample"> (gdb) run
89
Starting program: test_trap
91
Breakpoint 1, gsl_error (reason=0x8052b0d
92
"length n must be positive integer",
93
file=0x8052b04 "c_init.c", line=108, gsl_errno=1)
95
14 if (gsl_error_handler)
97
<p class="noindent">The first argument of <code>gsl_error</code> is always a string describing the
98
error. Now we can look at the backtrace to see what caused the problem,
100
<pre class="smallexample"> (gdb) bt
101
#0 gsl_error (reason=0x8052b0d
102
"length n must be positive integer",
103
file=0x8052b04 "c_init.c", line=108, gsl_errno=1)
105
#1 0x8049376 in gsl_fft_complex_wavetable_alloc (n=0,
106
wavetable=0xbffff778) at c_init.c:108
107
#2 0x8048a00 in main (argc=1, argv=0xbffff9bc)
109
#3 0x80488be in ___crt_dummy__ ()
111
<p class="noindent">We can see that the error was generated in the function
112
<code>gsl_fft_complex_wavetable_alloc</code> when it was called with an
113
argument of <var>n=0</var>. The original call came from line 94 in the
114
file <samp><span class="file">test_trap.c</span></samp>.
116
<p>By moving up to the level of the original call we can find the line that
119
<pre class="smallexample"> (gdb) up
120
#1 0x8049376 in gsl_fft_complex_wavetable_alloc (n=0,
121
wavetable=0xbffff778) at c_init.c:108
122
108 GSL_ERROR ("length n must be positive integer", GSL_EDOM);
124
#2 0x8048a00 in main (argc=1, argv=0xbffff9bc)
126
94 status = gsl_fft_complex_wavetable_alloc (0,
127
&complex_wavetable);
129
<p class="noindent">Thus we have found the line that caused the problem. From this point we
130
could also print out the values of other variables such as
131
<code>complex_wavetable</code>.