gdb 调试正在运行的记录

源程序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.




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值