Let’s start with an example.
#include <stdio.h>
void my_itoa(int n,char s[])
{
int i,j,sign;
if((sign=n)<0) // record sign
n=-n; // make n positive
i=0;
do{
s[i++]=n%10+'0'; // get the next digit
}while((n/=10)>0); // loop through division
if(sign<0)
s[i++]='-';
s[i]='\0';
for(j=i-1;j>=0;j--) // the generated numbers are in reverse order, so output them in reverse
printf("%c",s[j]);
}
void main()
{
int n;
char str[100];
my_itoa(-123,str);
printf("\n");
printf("%d\n",my_atoi("123"));
system("pause");
}
Save the above file to test.cpp, then compile and run it.
Now let’s debug it and see how to debug specifically. Debug the code following the method below.
linan@linan-PC:~$ gdb test
GNU gdb (Debian 7.12-6) 7.12.0.20161007-git
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...done.
(gdb) l
2
3 int func(int n)
4 {
5 int sum=0,i;
6 for(i=0; i<n; i++)
7 {
8 sum+=i;
9 }
10 return sum;
11 }
(gdb)
12
13 main()
14 {
15 int i;
16 long result = 0;
17 for(i=1; i<=100; i++)
18 {
19 result += i;
20 }
21
(gdb)
22 printf("result[1-100] = %d \n", result );
23 printf("result[1-250] = %d \n", func(250) );
24 }
(gdb)
Line number 25 out of range; test.cpp has 24 lines.
(gdb) break 15
Breakpoint 1 at 0x706: file test.cpp, line 15.
(gdb) break func
Breakpoint 2 at 0x6d7: file test.cpp, line 5.
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000000706 in main() at test.cpp:15
2 breakpoint keep y 0x00000000000006d7 in func(int) at test.cpp:5
(gdb) run
Starting program: /home/linan/test
Breakpoint 1, main () at test.cpp:16
16 long result = 0;
(gdb) next
17 for(i=1; i<=100; i++)
(gdb) next
19 result += i;
(gdb) next
17 for(i=1; i<=100; i++)
(gdb) continue
Continuing.
result[1-100] = 5050
Breakpoint 2, func (n=250) at test.cpp:5
5 int sum=0,i;
(gdb) p sum
$1 = 32767
(gdb) n
6 for(i=0; i<n; i++)
(gdb) p sum
$2 = 0
(gdb) p i
$3 = -134225552
(gdb) print i
$4 = -134225552
(gdb) n
8 sum+=i;
(gdb) print i
$5 = 0
(gdb) bt
#0 func (n=250) at test.cpp:8
#1 0x000055555555474c in main () at test.cpp:23
(gdb) help bt
Print backtrace of all stack frames, or innermost COUNT frames.
With a negative argument, print outermost -COUNT frames.
Use of the 'full' qualifier also prints the values of the local variables.
Use of the 'no-filters' qualifier prohibits frame filters from executing
on this backtrace.
(gdb) bt
#0 func (n=250) at test.cpp:8
#1 0x000055555555474c in main () at test.cpp:23
(gdb) finish
Run till exit from #0 func (n=250) at test.cpp:8
0x000055555555474c in main () at test.cpp:23
23 printf("result[1-250] = %d \n", func(250) );
Value returned is $6 = 31125
(gdb) c
Continuing.
result[1-250] = 31125
[Inferior 1 (process 11480) exited normally]
(gdb)
Before Using GDB
First, make sure your program was compiled with the -g parameter. This will insert relevant information into the executable program. This information doesn’t help the program but assists with debugging.
Starting Debugging with GDB
There are several ways to launch GDB:
-
gdb program is your executable file, usually in the current directory.
-
gdb core Use gdb to simultaneously debug a running program and core file. The core file is generated after a program illegally executes and core dumps.
-
gdb If your program is a service program, you can specify the process ID of the running service program. gdb will automatically attach to it and debug it. The program should be found in the PATH environment variable.
Notes for GDB Beginners
When debugging with GDB, you need to know where the source code files are located. In the example above, the source code and the running program are together. But this is not possible in practice. Add the source code directory using the -d parameter in the GDB command or using the directory command during runtime.
Other notes:
-symbols <file>
-s <file>
Read symbols from the specified file.
-se file
Read symbol table information from the specified file and use it in the executable file.
-core <file>
-c <file>
The core file from a core dump during debugging.