Debug program with GDB (a)

Source: Internet
Author: User

Debugging a program with GDB

GDB Overview
————

GDB is a powerful UNIX program debugging tool published by the GNU Open source organization. Perhaps, you prefer that graphical interface, such as VC, BCB and other IDE debugging, but assume that 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 mainly helps you to complete the following four aspects of the function:

1, start your program, can follow your own definition of the requirements of the arbitrary execution of the program.
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 no different, basically is finished 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 complete the function. Let's see.


A sample Debug Demo
——————

SOURCE 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 the build Run File: (Linux)
hchen/test> cc-g Tst.c-o TST

Debugging 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.
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 to start with 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 carriage return, repeated 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 <---------------------execution program, Run command shorthand
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 execute 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 execution.
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, we need to add the debug information to the executable file at the first compile time. 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

Assuming that there is no-G, you will not see the program's function name, variable name, instead of the memory address at the time of execution. 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 running file, usually under the folder of course.

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

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

When GDB starts, it can add some gdb start-up switch, the specific switch can be viewed with gdb-help. I'm just using the following examples to illustrate some of the more frequently used parameters:

-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 the source file. The default search path is the path defined by path in the environment variable.

Next page,

(All rights reserved, please specify the author and source when reproduced)

Debug program with GDB (a)

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.