本次调试,用到了GDB修改内存的命令。先将断点加到函数处,然后检查传入的参数放在何处,再将传入的参数修改,然后继续运行。
结果,按修改后的参数进行了执行。
代码如下,myfunc将入参变成2倍返回:
cat test.c
#include <stdio.h>
int myfunc(int i)
{
return i*2;
}
int main()
{
int x = myfunc(10);
printf("x=%d\n",x);
return 0;
}
1、为支持在MIPS上,进行交叉编译
/opt/toolschain/cavium2/bin/mips64-octeon-linux-gnu-gcc -mabi=n32 -g -o test2 test.c
2、上传到板子,用GDBSERVER运行
侦听1234端口等待连接
/var/tmp # ./gdbserver 10.12.2.240:1234 ./test2
Process ./test2 created; pid = 1692
Listening on port 1234 - 执行时停留在这
Remote debugging from host 10.12.0.254 -- 有连接时提示
[ERHMG2V100D005:3555s][ERHMG2V100D005:3555s][ERHMG2V100D005:3555s][ERHMG2V100D005:3556s][ERHMG2V100D005:3556s][ERHMG2V100D005:3944s]x=2 - 执行结果
Child exited with status 0
GDBserver exiting
4、在LINUX主机,用GDB调试。
x/1xw 0xnnnnnnnn
查看结果现在是a
set *0xnnnnnnnn=1
x/1xw 0xnnnnnnnn
结果修改成了1
再执行,发现运行结果已经变成了2
完整调试日志如下:
info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x10000670 in myfunc at test.c:5
breakpoint already hit 1 time
(gdb) c
Continuing.
Breakpoint 1, myfunc (i=10) at test.c:5
5 return i*2;
(gdb) x/5i $pc
=> 0x10000670 <myfunc+16>: lw v0,0(s8)
0x10000674 <myfunc+20>: sll v0,v0,0x1
0x10000678 <myfunc+24>: move sp,s8
0x1000067c <myfunc+28>: ld s8,24(sp)
0x10000680 <myfunc+32>: addiu sp,sp,32
(gdb) x/5i myfunc
0x10000660 <myfunc>: addiu sp,sp,-32
0x10000664 <myfunc+4>: sd s8,24(sp)
0x10000668 <myfunc+8>: move s8,sp
0x1000066c <myfunc+12>: sw a0,0(s8)
=> 0x10000670 <myfunc+16>: lw v0,0(s8)
(gdb) x/10i myfunc
0x10000660 <myfunc>: addiu sp,sp,-32
0x10000664 <myfunc+4>: sd s8,24(sp)
0x10000668 <myfunc+8>: move s8,sp
0x1000066c <myfunc+12>: sw a0,0(s8)
=> 0x10000670 <myfunc+16>: lw v0,0(s8)
0x10000674 <myfunc+20>: sll v0,v0,0x1
0x10000678 <myfunc+24>: move sp,s8
0x1000067c <myfunc+28>: ld s8,24(sp)
0x10000680 <myfunc+32>: addiu sp,sp,32
0x10000684 <myfunc+36>: jr ra
0x10000688 <myfunc+40>: nop
(gdb) info $s8
Undefined info command: "$s8". Try "help info".
(gdb) p $s8
$1 = 2145385424
(gdb) info reg
zero at v0 v1
R0 0000000000000000 0000000000000001 0000000000000000 0000000077fb44e0
a0 a1 a2 a3
R4 000000000000000a 000000007fdffd14 000000007fdffd1c 000000007fda5a90
a4 a5 a6 a7
R8 0000000077fb9e40 0000000077fb3788 000000007fdffd10 000000007fdffe41
t0 t1 t2 t3
R12 0000000077e24678 0000000077ff5000 0000000010010964 000000007fdffaa0
s0 s1 s2 s3
R16 0000000000000000 0000000000000000 0000000000000000 0000000010052641
s4 s5 s6 s7
R20 000000001006458c 0000000010060000 0000000000000008 ffffffffffffffff
t8 t9 k0 k1
R24 0000000000000001 000000001000068c 0000000077fb7148 0000000000000000
gp sp s8 ra
R28 0000000077fb9e40 000000007fdffbd0 000000007fdffbd0 00000000100006a8
status lo hi badvaddr
0000000004109cf3 0000000000005e17 00000000000001a5 0000000077e56490
cause pc
0000000000800024 0000000010000670
fcsr fir restart
0000000000000000 0000000000739600 0000000000000000
(gdb) x/1xw 7fdffbd0
Invalid number "7fdffbd0".
(gdb) x/1xw 0x7fdffbd0
0x7fdffbd0: 0x0000000a
(gdb) set 0x7fdffbd0=1
Left operand of assignment is not an lvalue.
(gdb) set *0x7fdffbd0=1
(gdb) x/1xw 0x7fdffbd0
0x7fdffbd0: 0x00000001
(gdb) c
Continuing.
[Inferior 1 (process 1692) exited normally]
(gdb)