The GDB command uses attach

Source: Internet
Author: User
Tags diff gdb debugger

[test program]
Let's take a look at our test procedure:
/* in EG1.C */

int wib (int no1, int NO2)
{
int result, diff;
diff = No1-no2;
result = No1/diff;
return result;
}

int main ()
{
pid_t pid;

PID = fork ();
if (PID <0) {
printf ("fork err\n");
Exit (-1);
} else if (PID = = 0) {
/* in child process */
Sleep (60); ------------------ (!)

int value = 10;
int div = 6;
int total = 0;
int i = 0;
int result = 0;

for (i = 0; i < i++) {
result = Wib (value, div);
Total + = result;
div++;
value--;
}

printf ("%d wibed by%d equals%d\n", value, Div, total);
Exit (0);
} else {
/* In parent process */
Sleep (4);
Wait (-1);
Exit (0);
}
}
A ' except 0 ' exception appears in the WIB function during the process of running a neutron process in the test program. Now we need to debug the subprocess.

[debugging principle]
I don't know if we found out, in (!) After our test program was fork in the parent process, the child process called sleep for 60 seconds. This is the key, this sleep should not exist in the sub-process code, but rather the use of GDB debugging after adding, it is a key point of our debugging. Why let the child process just run to start sleep? Because we want to use the shell command to get its process ID during the child process sleep, and then debug the process by using GdB's method of debugging the external process attach to the process ID.

[debugging process]
I think the idea of the above debugging principle is very clear, the rest is how to operate the problem. Let's practice it once!
I'm using environment Yes solaris OS 9.0/gcc 3.2/gdb 6.1 . The prerequisite for the

GDB Debugger is that you must compile the program with debug symbol information, which is the '-G ' compilation option. First compile our source program ' Gcc-g-o eg1 eg1.c '. After compiling, we have our debug target eg1. Since we need multiple tools in the debugging process, so you'd better open several terminal windows, another point to note is the best in EG1 working directory to execute the GDB program, or gdb back to the hint ' No symbol table is loaded '. You also have to load the symbol table manually. OK, so let's start debugging our EG1 ' Step by step '.

Execute EG1:
EG1 &  ---Let EG1 run the background.

Find Process ID:
Ps-fu your_user_name

Run GDB:
Gdb
(GDB) attach xxxxx ---xxxxx the subprocess process ID obtained with the PS command
(GDB) Stop ---This is important, you need to pause that subprocess first, then set some breakpoints and some watch
(GDB) Break PNS --in result = WIB (value, div); This line sets a breakpoint, you can use the List command to view the source code
Breakpoint 1 at 0x10808:file eg1.c, line 37.
(GDB)continue
Continuing.

Breakpoint 1, Main () at eg1.c:37
PNS result = Wib (value, div);
(GDB)Step
WIB (no1=10, no2=6) at eg1.c:13
diff = No1-no2;
(GDB)Continue
Continuing.

Breakpoint 1, Main () at eg1.c:37
PNS result = Wib (value, div);
(GDB)Step
WIB (no1=9, no2=7) at eg1.c:13
diff = No1-no2;
(GDB)Continue
Continuing.

Breakpoint 1, Main () at eg1.c:37
PNS result = Wib (value, div);
(GDB)Step
WIB (no1=8, no2=8) at eg1.c:13
diff = No1-no2;
(GDB)Next
result = No1/diff;
(GDB)Print diff
$6 = 0-------Divisor is 0, we found the culprit.
(GDB) Next
Program received signal SIGFPE, arithmetic exception.
0xff29d830 in. Div () from/usr/lib/libc.so.1

At this point, we have finished debugging.

The GDB command uses attach

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.