Spe*_*987 2 c memory gdb segmentation-fault argv
我有一个用 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
}
Run Code Online (Sandbox Code Playgroud)
我正在用 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
Run Code Online (Sandbox Code Playgroud)
显然它会出现分段错误,因为程序需要三个 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>
Run Code Online (Sandbox Code Playgroud)
从理论上讲,使用“x/s”我应该在第一个地址中看到命令行,在第二个地址中看到“test”,在第三个地址中看到空。但没什么。如果我将该地址复制粘贴到 ascii 到字符串转换器,它会毫无意义地给我数据。我究竟做错了什么?
您的平台使用 64 位指针,因此请尝试:
(gdb) x/3xg 0x7fffffffe958
Run Code Online (Sandbox Code Playgroud)
显示argv数组中的 64 位指针,然后:
(gdb) x/s 0x00007fffffffebfe
Run Code Online (Sandbox Code Playgroud)
要不就 :
(gdb) p argv[0]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
701 次 |
| 最近记录: |