所以,我正在学习如何用c语言编程,我正在(或者在elast,尝试)和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;
}
我想用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>
为什么我老是犯这个错误?我是64位的,我用的是kali linux
这个程序,如果编译的话,可以工作,只是我不明白为什么我不能用gdb读取这些值。
最佳答案
@德拉卡森在你的程序中发现了这个错误。至于你的gdb问题:x/3xw
打印出3个4字节的单词。argv
是指针数组。
由于您在64位系统上,指针是8字节,因此您不想使用char *
(巨大的,8字节)或w
(地址),它将自动选择正确的大小:
(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"
感谢@adpeace建议使用
g
修饰符。