gpt4 book ai didi

c - GDB 不允许我读取 argv 内存段

转载 作者:太空宇宙 更新时间:2023-11-04 06:13:16 24 4
gpt4 key购买 nike

我有这个用 C 语言编写的简单脚本:

#include <stdio.h>

void usage(char *program_name) {
printf("Usage: %s <message> <# of times to repeat>\n", program_name);
exit(1);
}

int main(int argc, char *argv[]) {
int i, count;

// if(argc < 3) // If less than 3 arguments are used,
// usage(argv[0]); // display usage message and exit.

count = atoi(argv[2]); // convert the 2nd arg into an integer
printf("Repeating %d times..\n", count);

for(i=0; i < count; i++)
printf("%3d - %s\n", i, argv[1]); // print the 1st arg
}

我正在用 GDB 做一些测试。

我是这样做的:

(gdb) run test
Starting program: /home/user/Desktop/booksrc/convert2 test

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a56e56 in ____strtoll_l_internal () from /usr/lib/libc.so.6

显然它进入了段错误,因为程序需要三个 argv 才能工作。我评论了控制的行。所以它出错了。

(gdb) where
#0 0x00007ffff7a56e56 in ____strtoll_l_internal () from /usr/lib/libc.so.6
#1 0x00007ffff7a53a80 in atoi () from /usr/lib/libc.so.6
#2 0x00005555555546ea in main (argc=2, argv=0x7fffffffe958) at convert2.c:14
(gdb) break main
Breakpoint 1 at 0x5555555546d2: file convert2.c, line 14.
(gdb) run test
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/user/Desktop/booksrc/convert2 test

Breakpoint 1, main (argc=2, argv=0x7fffffffe958) at convert2.c:14
14 count = atoi(argv[2]); // convert the 2nd arg into an integer
(gdb) cont
Continuing.

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a56e56 in ____strtoll_l_internal () from /usr/lib/libc.so.6
(gdb) x/3xw 0x7fffffffe958 // this is memory of the "argv" some line before
0x7fffffffe958: 0xffffebfe 0x00007fff 0xffffec22
(gdb) x/s 0xffffebfe
0xffffebfe: <error: Cannot access memory at address 0xffffebfe>
(gdb) x/s 0x00007fff
0x7fff: <error: Cannot access memory at address 0x7fff>
(gdb) x/s 0xffffec22
0xffffec22: <error: Cannot access memory at address 0xffffec22>

理论上,使用“x/s”我应该在第一个地址中看到命令行,在第二个地址中看到“test”,在第三个地址中看到空值。但是什么也没有。如果我将该地址复制粘贴到 ascii 到字符串转换器,它会给我没有任何意义的数据。我做错了什么?

最佳答案

您的平台使用 64 位指针,所以尝试:

(gdb) x/3xg 0x7fffffffe958

显示argv数组中的64位指针,然后:

(gdb) x/s 0x00007fffffffebfe

或者只是:

(gdb) p argv[0]

关于c - GDB 不允许我读取 argv 内存段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51725073/

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