GDB Debug Program One (Overview)

Source: Internet
Author: User
Tags aliases

Disclaimer: The blog content refers to Mr. Chen's blog post, the original link http://blog.csdn.net/haoel/article/details/2879. Since the original blog post does not have a directory, inconvenient to query, so the original blog post is organized into a directory version.


GDB Overview————
GDB is a powerful UNIX program debugging tool released by the GNU Open source organization. Perhaps, you prefer that graphical interface, such as VC, BCB and other IDE debugging, but if you are in the UNIX platform to do software, you will find GDB this debugging tool than VC, BCB graphical debugger more powerful features. The so-called "inch, the ruler is short" is this truth.
In general, GDB is the main help you complete the following four aspects of the function:
1, start your program, you can follow your custom requirements to run the program at will.
2. Allow the program to be debugged to stop at the breakpoint where you have specified the adjustment. (Breakpoint can be a conditional expression)
3. When the program is stopped, you can check what happened in your program at this time.
4. Dynamically change the execution environment of your program.
From the above, GDB and the General debugging tool is not the same, basically is to complete these functions, but in the details, you will find GDB this debugging tool is powerful, you may be more accustomed to the graphical debugging tools, but sometimes, the command line debugging tools have a graphical tool can not be done. Let's see.

A debugging ExampleSOURCE program: TST.C


1 #include <stdio.h>
2
3 int func (int n)
4 {
5 int sum=0,i;
6 for (i=0; i<n; i++)
7 {
8 sum+=i;
9}
Sum of ten return;
11}
12
13
Main ()
15 {
int i;
A long result = 0;
for (I=1; i<=100; i++)
19 {
result + = i;
21}
22
printf ("result[1-100] =%d/n", result);
printf ("result[1-250] =%d/n", func (250));
25}


Compile Build Execution file: (under Linux)
hchen/test> cc-g Tst.c-o TST


Debugging with GdBhchen/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.
Type "Show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "Show warranty" for details.
This GDB is configured as "I386-suse-linux" ...
(GDB) L <--------------------l command is equivalent to list, starting from the first line of the original code.
1 #include <stdio.h>
2
3 int func (int n)
4 {
5 int sum=0,i;
6 for (i=0; i<n; i++)
7 {
8 sum+=i;
9}
Sum of ten return;
(GDB) <--------------------direct return, repeat the last command
11}
12
13
Main ()
15 {
int i;
A long result = 0;
for (I=1; i<=100; i++)
19 {
result + = i;
(GDB) Break <--------------------set breakpoints at line 16th of the source program.
Breakpoint 1 at 0x8048496:file tst.c, line 16.
(GDB) Break Func <--------------------set breakpoints at the entrance of function func ().
Breakpoint 2 at 0x8048456:file tst.c, line 5.
(GDB) Info Break <--------------------view 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 <---------------------running program, shorthand for Run command
Starting program:/HOME/HCHEN/TEST/TST


Breakpoint 1, Main () at Tst.c:17 <----------stop at the breakpoint.
A long result = 0;
(GDB) n <---------------------single statement execution, next command shorthand.
for (I=1; i<=100; i++)
(GDB) n
result + = i;
(GDB) n
for (I=1; i<=100; i++)
(GDB) n
result + = i;
(GDB) C <---------------------continue to run the program, continue command shorthand.
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 the variable i, the Print command shorthand.
$ = 134513808
(GDB) n
8 sum+=i;
(GDB) n
6 for (i=1; i<=n; i++)
(GDB) P sum
$ = 1
(GDB) n
8 sum+=i;
(GDB) P I
$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 function.
Run till exit from #0 func (n=250) at Tst.c:5
0x080484e4 in Main () at tst.c:24
printf ("result[1-250] =%d/n", func (250));
Value returned is $6 = 31375
(GDB) C <---------------------continue to run.
Continuing.
RESULT[1-250] = 31375 <----------program output.


Program exited with code 027. <--------program exit, Debug end.
(GDB) Q <---------------------exit gdb.
Hchen/test>


Well, with the above perceptual knowledge, or let us have a systematic understanding of gdb it.

using GDB————

In General, GDB is mainly debugging the C + + program. In order to debug a C + + program, first at compile time, we have to add debug information to the executable file. This can be done using the-g parameter of the compiler (cc/gcc/g++). Such as:


> cc-g hello.c-o Hello
> g++-G hello.cpp-o Hello


without-G, you will not see the program's function name, variable name, instead of the memory address of the runtime. After you have added the debug information with-G and successfully compiled the target code, let's look at how to debug it with GDB.


There are several ways to start gdb:


1. GdB <program>
Program is your execution file, usually under the catalogue of course.


2. GDB <program> Core
Using GDB to debug a running program and core file at the same time, the core is the file generated after core dump is executed illegally.


3. GDB <program> <PID>
If your program is a service program, you can specify the process ID at which the service program runs. GDB will automatically attach up and debug him. The program should be searched in the PATH environment variable.


GDB start, you can add some gdb start switch, the detailed switch can be viewed with gdb-help. I'll just cite some of the more commonly used parameters below:


-symbols <file>
-S <file>
Reads the symbol table from the specified file.


-se file
Reads the symbol table information from the specified file and uses it in the executable file.


-core <file>
-C <file>
Core file of core dump when debugging.


-directory <directory>
-D <directory>
Add a search path to a source file. The default search path is the path defined by path in the environment variable.


Overview of GDB commands———————


Once GDB is started, you can start debugging with GDB's commands, as soon as you are brought into GDB's debug environment, and GDB commands 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.
Type "Show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "Show warranty" for details.
This GDB is 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 is allowed if unambiguous.
(GDB)


GDB has a lot of commands, and gdb divides it into many different categories. The help command is just an example of the type of GDB command, and if you want to see the commands in the category, you can see all the commands that set a breakpoint by using the helps <class> command, such as breakpoints. You can also go directly to help <command> to see the commands.




GDB, the input command, you can not play the full command, only the first few characters to play the command can be, of course, the first few characters of the command should be marked a unique command, under Linux, you can hit two times tab to fill the command, if there are duplicates, then GDB will take its example.

Example one: When entering the function func, set a breakpoint. You can either knock the break func, or directly be B func.
(GDB) b func
Breakpoint 1 at 0x8048458:file hello.c, line 10.

Example two: Typing B by pressing two times tab, you will see all the B commands:
(GDB) b
BackTrace Break BT
(GDB)


Example three: Just remember the prefix of the function, so you can:
(GDB) B make_ < Press the TAB key >
(Press the TAB key again, and you'll see:)
Make_a_section_from_file Make_environ
Make_abs_section Make_function_type
Make_blockvector Make_pointer_type
Make_cleanup Make_reference_type
Make_command make_symbol_completion_list
(GDB) B make_
GDB gives you an example of all the functions that start with make.


Example four: When you debug a C + + program, you can have the same function name. Such as:
(gdb) b ' bubble (M-?
Bubble (double,double) Bubble (int,int)
(gdb) b ' Bubble (
You can see all the overloaded functions and parameters in C + +. (Note: M-and "Press two tab" is a meaning)

To exit GDB, simply send a quit or a command called Q.



a shell program that runs Unix in gdb————————————


In the GDB environment, you can execute the UNIX shell command, using GDB's shell command to complete:


Shell <command string>
Call the Unix shell to execute <command string> the shell of Unix defined in the environment variable shell will be used to execute <command string> If the shell is not defined, Then use Unix's standard shell:/bin/sh. (Use Command.com or cmd.exe in Windows)


There is also a GDB command that is make:
Make <make-args>
You can build your own program by executing the Make command in GDB. This command is equivalent to "shell make <make-args>".

run the program in GDB ————————


When GDB is started with gdb <program>, GDB searches the path path and the current directory source files for <program>. To confirm that GDB is reading the source file, you can use the L or List command to see if GDB can list the source code.


in GDB, run the program using the R or Run command. program runs, you may need to set up the following four things.


1, program run parameters.
   
    Show args command to view the set of running parameters.


2, the operating environment.
    Path <dir> can set the run path of the program.
    Show paths to view the running path of the program.
    set environment varname [=value] Sets environment variables. For example: Set env user=hchen
    show environment [varname] View environment variables.


3, working directory.
    CD <dir> the equivalent of the Shell's CD command.
    PWD Displays the current directory.


4, the input and output of the program. The
    Info terminal shows the terminal mode used by your program.
    Use redirection control program output. For example, the Run > outfile
    TTY command can refer to an end device that writes input and output. such as: Tty/dev/ttyb
to debug a program that is already running————————

Two methods:
1. Under UNIX, use PS to see the PID (Process ID) of the running program, and then use GDB <program> PID format to hook up the running program (the PID context is the program specified by program).
2, first use GDB <program> associated with the source code, and GDB, in gdb with the attach command to hook up the process PID. Use the detach to cancel the hook process.


Pause/Resume program run—————————


In the debugger, it is necessary to pause the program, and GDB can conveniently pause the program's operation. You can set up which line of the program stops, under what conditions, stop when you receive a signal, and so on. To make it easier for you to see the variables at run time, as well as the process at run time.

When the process is stopped by GDB, you can use the Info program to see if it is running, the process number, and why it was paused.

In gdb, we can pause in the following ways: Breakpoints (breakpoint), Observation points (watchpoint), Snap points (Catchpoint), Signals (signals: CTRL + Z), Thread stops (threads Stops). If you want to restore the program to run, you can use the C or Continue command.

GDB Debug Program One (Overview)

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.