gpt4 book ai didi

c - GDB
错误

转载 作者:太空狗 更新时间:2023-10-29 15:04:01 33 4
gpt4 key购买 nike

因此,我正在学习如何用 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;
}

我正尝试使用 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 读取这些值。

最佳答案

@DrakaSAN 发现了您程序中的错误。至于你的 gdb 问题:

x/3xw 打印出 3 个 4 字节的单词。 argv 是一个 char * 指针数组。由于您使用的是 64 位系统,指针是 8 个字节,因此您要使用 g(巨型,8 个字节)或 a 而不是 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 建议使用 a 修饰符。

关于c - GDB <Address 0xblablabla out of bounds> 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24866919/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com