Debug the program with GDB

Source: Internet
Author: User
Article title: use GDB to debug programs. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
GDB is a powerful UNIX program debugging tool released by the GNU open-source organization. Maybe you prefer the GUI, such as VC, BCB, and other IDE debugging methods. but if you are running software on a UNIX platform, you will find that the GDB debugging tool has more powerful functions than the visual debugger of VC and BCB. This is the so-called "have an inch, have a small size and have a short size.
  
In general, GDB helps you complete the following four functions:
  
1. start your program and run it as needed according to your custom requirements.
2. the program to be debugged can be stopped at the breakpoint you specified. (The breakpoint can be a conditional expression)
3. when the program is stopped, you can check what happens in your program.
4. dynamically change the execution environment of your program.
  
From the above point of view, GDB is similar to a general debugging tool and basically completes these functions. However, in details, you will find that GDB is a powerful debugging tool, you may be used to graphical debugging tools, but sometimes command line debugging tools have functions that cannot be completed by graphical tools. Let's look at it one by one.
  
   A debugging example
SOURCE Program: tst. c
  
1 # include
2
3 int func (int n)
4 {
5 int sum = 0, I;
6 for (I = 0; I 7 {
8 sum + = I;
9}
10 return sum;
11}
12
13
14 main ()
15 {
16 int I;
17 long result = 0;
18 for (I = 1; I <= 100; I ++)
19 {
20 result + = I;
21}
22
23 printf ("result [1-100] = % d", result );
24 printf ("result [1-250] = % d", func (250 ));
25}
  
Compile and generate the execution File: (in Linux)
Hchen/test> cc-g tst. c-o tst
  
Debug with GDB:
  
Hchen/test> gdb tst <---------- start GDB
GNU gdb 5.1.1
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
Welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-suse-linux "...
(Gdb) l <-------------------- l command is equivalent to list, the original code is obtained from the first line.
1 # include
2
3 int func (int n)
4 {
5 int sum = 0, I;
6 for (I = 0; i7 {
8 sum + = I;
9}
10 return sum;
(Gdb) <-------------------- press enter to repeat the previous command
11}
12
13
14 main ()
15 {
16 int I;
17 long result = 0;
18 for (I = 1; I <= 100; I ++)
19 {
20 result + = I;
(Gdb) break 16 <-------------------- set the breakpoint, at the source program line 16th.
Breakpoint 1 at 0x8048496: file tst. c, line 16.
(Gdb) break func <-------------------- sets the breakpoint at the entry of the function func.
Breakpoint 2 at 0x8048456: file tst. c, line 5.
(Gdb) info break <-------------------- view the breakpoint information.
Num Type Disp Enb Address What
1 breakpoint keep y 0x08048496 in main at tst. c: 16
2 breakpoint keep y 0x08048456 in func at tst. c: 5
(Gdb) r <--------------------- run the program, short for the run Command
Starting program:/home/hchen/test/tst
  
Breakpoint 1, main () at tst. c: 17 <---------- stop at the Breakpoint.
17 long result = 0;
(Gdb) n <--------------------- execute a single statement. The next command is short for execution.
18 for (I = 1; I <= 100; I ++)
(Gdb) n
20 result + = I;
(Gdb) n
18 for (I = 1; I <= 100; I ++)
(Gdb) n
20 result + = I;
(Gdb) c <--------------------- to continue running the program, the continue command is abbreviated.
Continuing.
Result [1-100] = 5050 <---------- program output.
  
Breakpoint 2, func (n = 250) at tst. c: 5
5 int sum = 0, I;
(Gdb) n
6 for (I = 1; I <= n; I ++)
(Gdb) p I <--------------------- print the value of variable I. The print command is short.
$1 = 134513808
(Gdb) n
8 sum + = I;
(Gdb) n
6 for (I = 1; I <= n; I ++)
(Gdb) p sum
$2 = 1
(Gdb) n
8 sum + = I;
(Gdb) p I
$3 = 2
(Gdb) n
6 for (I = 1; I <= n; I ++)
(Gdb) p sum
$4 = 3
(Gdb) bt <--------------------- view the function stack.
#0 func (n = 250) at tst. c: 5
#1 0x080484e4 in main () at tst. c: 24
#2 0x400409ed in _ libc_start_main () from/lib/libc. so.6
(Gdb) finish <--------------------- exit the function.
Run till exit from #0 func (n = 250) at tst. c: 5
0x080484e4 in main () at tst. c: 24
24 printf ("result [1-250] = % d", func (250 ));
Value returned is $6 = 31375
(Gdb) c <--------------------- continue to run.
Continuing.
Result [1-250] = 31375 <---------- program output.
  
Program exited with code 027. <-------- The Program exits and debugging is complete.
(Gdb) q <--------------------- exit gdb.
Hchen/test>
  
Well, with the above perceptual knowledge, let's get to know gdb systematically.
  
   Use GDB
Generally, GDB mainly debugs C/C ++ programs. To debug a C/C ++ program, we must add the debugging information to the executable file during compilation. This can be done using the-g parameter of the compiler (cc/gcc/g ++. For example:
  
> Cc-g hello. c-o hello
> G ++-g hello. cpp-o hello
  
Without-g, you will not be able to see the function name and variable name of the program, instead of the runtime memory address. After you add the debugging information with-g and successfully compile the target code, let's see how to debug it with gdb.
  
The following methods are used to start GDB:
  
1. gdb
Program is your execution file, which is usually in the directory of course.
  
2. gdb core
Debug a running program and core file with gdb. The core is the file generated after the core is dumped after the program is illegally executed.
  
3. gdb
If your program is a service program, you can specify the process ID when the service program runs. Gdb automatically attach and debug it. The program should be searched in the PATH environment variable.
  
When GDB is started, you can add some GDB start switches. for details about the switches, you can use gdb-help to view them. Here are some common parameters:
  
-Symbols
-S
Read the symbol table from a specified file.
  
-Se file
Read the symbol table information from the specified file and use it in the executable file.
  
-Core
-C
Core dump core file during debugging.
  
-Directory
-D
Add the search path of a source file. The default search PATH is the PATH defined by PATH in the environment variable.
  
   GDB command overview
After starting gdb, you will be brought into the debugging environment of gdb. you can use the gdb command to start debugging the program. the command of gdb can be viewed using the help command, as shown below:
  
/Home/hchen> gdb
GNU gdb 5.1.1
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
Welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-suse-linux ".
(Gdb) help
List of classes of commands:
  
Aliases -- Aliases of other commands
Breakpoints -- Making program stop at certain points
Data -- Examining data
Files -- Specifying and examining files
Internals -- Maintenance commands
Obscure -- Obscure features
Running -- Running the program
Stack -- Examining the stack
Status -- Status inquiries
Support -- Support facilities
Tracepoints -- Tracing of program execution without stopping the program
User-defined -- User-defined commands
  
Type "help" followed by a class name for a list of commands in that class.
Type "help" followed by command name for full documentation.
Command name abbreviations are allowed if unambiguous.
(Gdb)
  
Gdb has many commands, which are divided into many types by gdb. The help command is just an example of the command type of gdb. if you want to view the commands in the command type, you can use the help command, for example, help breakpoints, to view all the commands for setting breakpoints. You can also directly help to view the command help.
  
In gdb, when you enter a command, you do not need to execute a full command. only the first few characters of the command can be used. of course, the first few characters of the command should be marked

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.