源程序test.c
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char *argv[])
{
struct stat sb;
if (argc != 2) {
fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
exit(EXIT_FAILURE);
}
while (1) {
if (stat("/tmp/tmp", &sb) == 0)
{
if (stat(argv[1], &sb) == -1) {
int exitCode = 33;
perror("stat");
printf("%s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
sleep(5);
}
return 0;
}
编译运行:
$ gcc -g test.c -o test && ./test /tmp/iojjooo
目标,通过gdb查看 exitCode的值.
1. 查看./test进程的pid
ps -ef |grep test
得知进程IP 3429
2. 启动gdb 并attach 该进程.
</pre><pre name="code" class="plain">$gdb --pid 3249
GNU gdb (Ubuntu/Linaro 7.4-2012.02-0ubuntu2) 7.4-2012.02
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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 "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>.
Attaching to process 3249
Reading symbols from /home/taoxie/test...done.
Reading symbols from /lib/x86_64-linux-gnu/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/x86_64-linux-gnu/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00007f61e94c5020 in nanosleep () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) list test.c:21 # 查看flow.c文件第23行周围的10行
16 }
17
18 while (1) {
19 if (stat("/tmp/tmp", &sb) == 0)
20 {
21 if (stat(argv[1], &sb) == -1) {
22 int exitCode = 33;
23 perror("stat");
24 printf("%s\n", strerror(errno));
25 exit(EXIT_FAILURE);
(gdb) b 21
Breakpoint 1 at 0x4007b0: file test.c, line 21.
(gdb) info b #查看断点信卢
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000004007b0 in main at test.c:21
(gdb) delete 1 #删除断点 , 1为断点号
(gdb) backtrace #查看堆栈信息
#0 main (argc=2, argv=0x7fff0fbf3d68) at test.c:23
(gdb) c
Continuing.