Examples of using GDB program Debugging Tools

Source: Internet
Author: User


1, start the debugger program.
2. Let the program being debugged stop at the specified position.
3, when the program is stopped, you can check the state of the program (such as variable values).

Then practice, Vim test.c

#include <studio.h>
int main ()
{
int i;
Long result = 0;
For (i=0 i<100; i++) {
Result+=i;
}
printf ("result=%d\n", result);
return 0;
}
You can follow these steps to debug:

1. Compile the build executable file:
Gcc-g Test.c-o Test

2. Start GDB
GDB test

3. Set a breakpoint at the main function
Break Main

4. Run the program
Run

5. Single Step operation
Next

6. Continue to operate
Continue

1, start GDB

Two ways:

(1) GDB Debug program Name

Example: GDB test

(2) GDB

File Debugger Name

2, GDB command

List (l) Viewer program

Break (b) Function name add breakpoint at the entrance of a function

Break (b) Line number add breakpoint at specified line

Break (b) FileName: line number adds a breakpoint to the specified line in the specified file

Break (b) Line number if condition when the condition is true, the breakpoint at the specified line number takes effect, example B 5 if i=10, when I equals 10 o'clock the 5th line breakpoint takes effect

Info Break view breakpoints for all settings

Delete Breakpoint Number Remove Breakpoint

Run (R) starts running the program

Next (N) Step into the program (do not enter the child function)

Step (s) steps to run the program (enter the child function)

Continue (c) continue to run the program

Print (p) Variable name view specified variable value

Finish run the program until the current function ends

Watch variable name to monitor a specified variable

Quit (q) Exit GDB

The above commands are very basic simple, and then knock once to master the use of methods

A debugging example
——————

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}
return sum;
11}
12
13
Main ()
15 {
int i;
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 GDB:

hchen/test> gdb TST <----------start GDB
GNU gdb 5.1.1
Copyright 2002 Free Software Foundation, Inc.
The GDB is free software, covered by the GNU general public License, and your are
Welcome to change it and/or distribute copies of it under certain conditions.
Type ' show copying ' to the conditions.
There is absolutely no warranty for GDB. Type ' show warranty ' for details.
This is GDB was configured as "I386-suse-linux" ...
(GDB) L <--------------------l command is equivalent to the list, starting with 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}
return sum;
(GDB) <--------------------Direct carriage return, repeat the last command
11}
12
13
Main ()
15 {
int i;
Long result = 0;
for (I=1; i<=100; i++)
19 {
result = i;
(GDB) Break <--------------------set breakpoints at the 16th line of the source program.
Breakpoint 1 at 0x8048496:file tst.c, line 16.
(GDB) Break Func <--------------------set a breakpoint at the func () entrance of the function.
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 programs, Run command shorthand
Starting program:/HOME/HCHEN/TEST/TST

Breakpoint 1, Main () at Tst.c:17 <----------stop at the breakpoint.
Long result = 0;
(GDB) n <---------------------A single statement is executed, and the next command is abbreviated.
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 <---------------------the value of the print variable i and the Print command is abbreviated.
$ = 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
$ = 3
(GDB) bt <---------------------view the function stack.
#0 func (n=250) at Tst.c:5
#1 0x080484e4 in Main () 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 $ = 31375
(GDB) C <---------------------continue to run.
Continuing.
RESULT[1-250] = 31375 <----------program output.

Program exited with code 027. <--------program exits and debugging ends.
(GDB) Q <---------------------exit gdb.
Hchen/test>

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


Using GDB
————

In general, GDB is primarily debugging C + + programs. To debug a C + + program, first at compile time, we have to add debug information to the executable file. You can do this with 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 all the run-time memory address. When you add the debug information with-G and successfully compile the target code, let's see how to debug it with GDB.

There are several ways to start gdb:

1, GDB <program>
Program is your executable file, usually in the course of the directory.

2. GDB <program> Core
Using GDB to debug both a running program and a core file, the core is the file that was generated after the program was illegally executed after the core dump.

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

When GDB starts, you can add some gdb startup switches, and the detailed switches 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 dump core file 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.

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.