Use GDB to debug the currently running program

Source: Internet
Author: User
Tags sigint signal
Http://wiki.ubuntu.org.cn/index.php? Title = % E7 % 94% a8gdb % E8 % B0 % 83% E8 % af % 95% E7 % A8 % 8B % E5 % Ba % 8f & variant = ZH-Hans

 

Http://blog.csdn.net/wfing/archive/2010/09/17/5890382.aspx

Http://blog.csdn.net/chenglian_999/archive/2009/11/15/4813469.aspx

You can use GDB to debug the current Program And read out its parameters.
The following uses a simple program as an example to illustrate GDB debugging.

The first step is to compile an endless loop program. 

/* File name malloc. C */

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>

Void getmem (void ** P, int num ){
* P = (void *) malloc (Num );
}

Void test (void ){
Char * STR = NULL;
Getmem (void ***) & STR, 100 );
Strcpy (STR, "hello ");
Printf ("% s \ n", STR );
}

Int main (void ){
Int I = 0;
While (1 ){
If (I = 1 ){
Test ();
Return 1;
}
}
Return 0;
}

We can see that this program is malloc memory space for use by strcpy. As it is only for debugging, some correctness judgment statements about strcpy are not added to the test program.
The normal exit of the function is I = 1, But I = 1 cannot be enabled during the program running. The value of the I variable will be used when GDB is used.

Start Compilation
$ Gcc-G malloc. c 

You must use GDB and add-G. The generated executable file is a. Out.

Step 2 connect GDB to the running process 
Run the program first.
$./A. Out 
Obviously, it is an endless loop.

Re-open a shell
$ PS-u 
The running status of my machine is as follows:
Warning: Bad PS syntax, perhaps a bogu '-'? See http://procps.sf.net/faq.html
User PID % CPU % mem vsz RSS tty stat Start Time Command
WYC 7712 0.0 0.1 6092 3644 pts/8 SS bash
WYC 7880 0.0 0.1 6092 3608 pts/9 SS bash
WYC 7929 0.0 0.3 10848 6468 pts/9 S + GDB
WYC 8347 93.0 0.0 1652 284 pts/8 R +./A. Out
...

No? The process number of./A. Out is 8347.

Start GDB now
$ GDB 

Because it is a debugging and running process, not an executable file, no parameters need to be followed. When you use GDB to debug a program in the running state, the core is the attach command in GDB.
Usage:
(GDB) Attach

This is an example on my machine:
$ GDB 
Gnu gdb (GDB) 7.1.50.20100621
Copyright (c) 2010 Free Software Foundation, Inc.
License gplv3 +: gnu gpl Version 3 or later
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 "i686-pc-linux-gnu ".
For bug reporting instructions, please see:
.
(GDB) Attach 8347.
Attaching to process 8347
Reading symbols from/home/WYC/desktop/my_program/review/a. Out... done.
Reading symbols from/lib/tls/i686/cmov/libc. so.6... (no debugging symbols found)... done.
Loaded symbols for/lib/tls/i686/cmov/libc. so.6
Reading symbols from/lib/ld-linux.so.2... (no debugging symbols found)... done.
Loaded symbols for/lib/ld-linux.so.2
Main () at malloc. C: 19
19 if (I = 1 ){
(GDB) p I
$1 = 0
(GDB) set I = 1
Ambiguous set command "I = 1 ":.
(GDB) I = 1
Undefined info command: "= 1". Try "Help Info ".
(GDB) set I = 1
Ambiguous set command "I = 1 ":.
(GDB) set var I = 1
(GDB) L
14}
15
16 int main (void ){
17 int I = 0;
18 while (1 ){
19 if (I = 1 ){
20 test ();
21 return 1;
22}
23}
(GDB) N
20 test ();
(GDB)
21 return 1;
(GDB)
25}
(GDB)
0xb7f47775 in _ libc_start_main () from/lib/tls/i686/cmov/libc. so.6
(GDB)
Single stepping until exit from function _ libc_start_main,
Which has no line number information.

Program exited with code 01.
(GDB)

When running the 20th-line command, you can see the shell that runs./A. Out. The Hello string should be on the standard output. When GDB shows that the process exits, the./A. out shell should end the current process.
In GDBSet var I = 1 To modify the value of variable I (set I = 1 cannot recognize the command), so that the program can exit normally.

During debugging, all the databases called by the current program are also released. In this example
Reading symbols from/home/WYC/desktop/my_program/review/a. Out... done.
Reading symbols from/lib/tls/i686/cmov/libc. so.6... (no debugging symbols found)... done.
Loaded symbols for/lib/tls/i686/cmov/libc. so.6
Reading symbols from/lib/ld-linux.so.2... (no debugging symbols found)... done.
Loaded symbols for/lib/ld-linux.so.2
Is all the databases called by the. Out Program. You can use this method to analyze the calling status of the library of the currently running program.

Do not turn off GDB. The following debugging is more exciting:

Step 3 restart the program in GDB 
I already know that the program has exited normally, but GDB has not exited. How is the running effect in GDB?

(GDB) Run 
Starting program:/home/WYC/desktop/my_program/review/a. Out
The following is an endless loop...
Press Ctrl + C to send a SIGINT signal to GDB.

^ C
Program received signal SIGINT, interrupt.
Main () at malloc. C: 19
19 if (I = 1 ){
(GDB) p I
$2 = 0
(GDB) set var I = 1
(GDB) N
20 test ();
(GDB) N
Hello
21 return 1;
(GDB) N
25}
(GDB) N
0xb7e7b775 in _ libc_start_main () from/lib/tls/i686/cmov/libc. so.6
(GDB) N
Single stepping until exit from function _ libc_start_main,
Which has no line number information.

Program exited with code 01.

It can be seen that after a process is connected with GDB, it will find all the files required to run the process. After the current process is closed, the program can still be started in GDB.

I have to admire the powerful debugging functions of GDB.

Other commands in GDB depend on whether the program is used during analysis. For example, some simple commands are as follows:

Common BT, p, p/X, setp, info registers, break, jump ...... 

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.