~ubuntu-branches/ubuntu/trusty/gsl-ref-html/trusty

« back to all changes in this revision

Viewing changes to Using-gdb.html

  • Committer: Bazaar Package Importer
  • Author(s): Dirk Eddelbuettel
  • Date: 2006-04-12 19:46:32 UTC
  • mfrom: (1.3.1 upstream) (3.1.1 dapper)
  • Revision ID: james.westby@ubuntu.com-20060412194632-c9lodpl075pv9si3
Tags: 1.8-1
* New upstream release 1.8
* As with previous releases, the sources were obtained from the FSF web 
  pages by means of a wget call (c.f. the debian/rules target 'upstream')

* debian/control: Standards-Version increased to 3.6.2
* debian/copyright: Updated FSF address
* debian/rules: Set DH_COMPAT=4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<html lang="en">
 
2
<head>
 
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">
 
11
<!--
 
12
Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 The GSL Team.
 
13
 
 
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
 
21
License''.
 
22
 
 
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; } 
 
36
--></style>
 
37
</head>
 
38
<body>
 
39
<div class="node">
 
40
<p>
 
41
<a name="Using-gdb"></a>
 
42
Next:&nbsp;<a rel="next" accesskey="n" href="Examining-floating-point-registers.html#Examining-floating-point-registers">Examining floating point registers</a>,
 
43
Up:&nbsp;<a rel="up" accesskey="u" href="Debugging-Numerical-Programs.html#Debugging-Numerical-Programs">Debugging Numerical Programs</a>
 
44
<hr>
 
45
</div>
 
46
 
 
47
<h3 class="section">A.1 Using gdb</h3>
 
48
 
 
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
 
53
 
 
54
<pre class="example">     break gsl_error
 
55
</pre>
 
56
   <p class="noindent">into your <samp><span class="file">.gdbinit</span></samp> file in the directory where your program is
 
57
started.
 
58
 
 
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
 
64
line,
 
65
 
 
66
<pre class="smallexample">     status = gsl_fft_complex_wavetable_alloc (0, &amp;complex_wavetable);
 
67
</pre>
 
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.
 
71
 
 
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>,
 
74
 
 
75
<pre class="smallexample">     $ gdb test_trap
 
76
     
 
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.
 
82
     
 
83
     Breakpoint 1 at 0x8050b1e: file error.c, line 14.
 
84
</pre>
 
85
   <p class="noindent">When we run the program this breakpoint catches the error and shows the
 
86
reason for it.
 
87
 
 
88
<pre class="smallexample">     (gdb) run
 
89
     Starting program: test_trap
 
90
     
 
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)
 
94
         at error.c:14
 
95
     14        if (gsl_error_handler)
 
96
</pre>
 
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,
 
99
 
 
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)
 
104
         at error.c:14
 
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)
 
108
         at test_trap.c:94
 
109
     #3  0x80488be in ___crt_dummy__ ()
 
110
</pre>
 
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>.
 
115
 
 
116
   <p>By moving up to the level of the original call we can find the line that
 
117
caused the error,
 
118
 
 
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);
 
123
     (gdb) up
 
124
     #2  0x8048a00 in main (argc=1, argv=0xbffff9bc)
 
125
         at test_trap.c:94
 
126
     94    status = gsl_fft_complex_wavetable_alloc (0,
 
127
             &amp;complex_wavetable);
 
128
</pre>
 
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>.
 
132
 
 
133
   </body></html>
 
134