所以,我正在学习如何使用C语言进行编程,而且我正在(或者正在尝试)与GDB一起玩.
所以我写了这个简单的代码:
#include <stdio.h>
int main (int argc, char *argv[]){
int i;
int n = atoi(argv[2]);
for (i=0; i<n ; i++){
printf("%s \n",i+1,argv[1]); // prints the string provided in
} // the arguments for n times
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我试图让GDB获得一些信息.所以我用它来尝试从内存地址中获取参数,但这就是我得到的:
(gdb) break main
Breakpoint 1 at 0x4005d7: file repeat2.c, line 14.
(gdb) break 17
Breakpoint 2 at 0x40062c: file repeat2.c, line 17.
(gdb) run hello 5
Starting program: /root/Scrivania/Programmazione/repeat2 hello 5
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000
Breakpoint 1, main (argc=3, argv=0x7fffffffe948) at repeat2.c:14
14 int n = atoi(argv[2]);
(gdb) cont
Continuing.
1 ------> hello
2 ------> hello
3 ------> hello
4 ------> hello
5 ------> hello
Breakpoint 2, main (argc=3, argv=0x7fffffffe948) at repeat2.c:18
18 return 0;
(gdb) x/3xw 0x7fffffffe948 (I try to read what argv contains)
0x7fffffffe948: 0xffffebbc 0x00007fff 0xffffebe3
(gdb) x/s 0xffffebbc (I try to read one of the argoments in the array)
0xffffebbc: <Address 0xffffebbc out of bounds>
Run Code Online (Sandbox Code Playgroud)
为什么我一直收到这个错误?我是64位,我正在使用Kali Linux
该程序如果编译好了,只是我无法理解为什么我不能用GDB读取这些值.
@DrakaSAN 在您的程序中发现了错误。至于你的 gdb 问题:
x/3xw打印出 3 个 4 字节字。argv是一个char *指针数组。由于您使用的是 64 位系统,指针为 8 个字节,因此w您不想使用g(giant, 8 bytes) 或a(address),它将自动选择正确的大小:
(gdb) break 7
Breakpoint 1 at 0x40058c: file repeat2.c, line 7.
(gdb) run hello 5
Starting program: /tmp/repeat2 hello 5
Breakpoint 1, main (argc=3, argv=0x7fffffffdfe8) at repeat2.c:7
7 int n = atoi(argv[2]);
(gdb) x/3xg 0x7fffffffdfe8
0x7fffffffdfe8: 0x00007fffffffe365 0x00007fffffffe372
0x7fffffffdff8: 0x00007fffffffe378
(gdb) x/3xa 0x7fffffffdfe8
0x7fffffffdfe8: 0x7fffffffe365 0x7fffffffe372
0x7fffffffdff8: 0x7fffffffe378
(gdb) x/s 0x7fffffffe365
0x7fffffffe365: "/tmp/repeat2"
(gdb) x/s 0x7fffffffe372
0x7fffffffe372: "hello"
(gdb) x/s 0x7fffffffe378
0x7fffffffe378: "5"
Run Code Online (Sandbox Code Playgroud)
感谢@adpeace 建议a修改器。
| 归档时间: |
|
| 查看次数: |
7880 次 |
| 最近记录: |