利用gdb远程调试程序
利用客户机/服务器的工作机制,通过stub方法来实现远程调试。具体操作如下:
1 为方便调试时看源代码,首先保持调试机(Client)和目标机(Server)的可执行程序、源代码保持一致。
设客户机的IP: 192.168.1.244
服务器的IP: 192.168.1.241
通信端口 : 1100
将要调试的程序名为: test1
test1.c
2 在目标机(server)先启动程序 gdb 192.168.1.244:1100 test1
Process test1 created; pid = 5834
Listening on port 1100
3 在调试机(client) [abs20@LX test]$ gdb
(gdb) target remote 192.168.1.241:1100
Remote debugging using 192.168.1.241:1100
0x49c988d0 in ?? ()
(gdb) list
warning: Source file is more recent than executable.
1
#include
2
3
int main( void )
4
{
5
int i=2;
6
int x, y;
7
8
x=(++i);
9
printf(" %d %d/n", i,x);
10
x+=(++i);
(gdb) b 9
Breakpoint 1 at 0x8048376: file test1.c, line 9.
(gdb) list 9,16
9
printf(" %d %d/n", i,x);
10
x+=(++i);
11
printf(" %d %d/n", i,x);
12
x+=(++i);
13
printf(" %d %d/n", i,x);
14
i=2;
15
y=(i++)+(i++)+(i++);
16
printf(" %d %d/n", i,y);
(gdb) b 11
Breakpoint 2 at 0x804839a: file test1.c, line 11.
(gdb) break 13
Breakpoint 3 at 0x80483be: file test1.c, line 13.
(gdb) b 16
Breakpoint 4 at 0x80483f7: file test1.c, line 16.
4 每步操作
Remote debugging from host 192.168.1.244
3 3
4 7
5 12
5 6
Child terminated with signal = 4
Child terminated with signal = 0x4
GDBserver exiting
源程序如下:
#include
int main( void )
{
int i=2;
int x, y;
x=(++i);
printf(" %d %d/n", i,x);
x+=(++i);
printf(" %d %d/n", i,x);
x+=(++i);
printf(" %d %d/n", i,x);
i=2;
y=(i++)+(i++)+(i++);
printf(" %d %d/n", i,y);
return 0;
}