C语言 gdb 无法访问内存地址错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30139356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:56:01  来源:igfitidea点击:

gdb can't access memory address error

cgdb

提问by Ojs

here is my disas code:

这是我的disas代码:

   0x0804844d <+0>:     push   %ebp
   0x0804844e <+1>:     mov    %esp,%ebp
   0x08048450 <+3>:     and    
puts("some string");
int i;
scanf("%d", &i);  // I don't know what the actual format string is.
                  // You can find out with x/s 0x8048555
if (i == 0x208c) { ... } else { ... }
xfffffff0,%esp 0x08048453 <+6>: sub
int foo = 42;
int *pfoo = &foo;
x20,%esp 0x08048456 <+9>: movl
mmap(0, size, PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
x8048540,(%esp) 0x0804845d <+16>: call 0x8048310 <puts@plt> 0x08048462 <+21>: lea 0x1c(%esp),%eax 0x08048466 <+25>: mov %eax,0x4(%esp) 0x0804846a <+29>: movl
mmap(0, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
x8048555,(%esp) 0x08048471 <+36>: call 0x8048320 <scanf@plt> 0x08048476 <+41>: mov 0x1c(%esp),%eax 0x0804847a <+45>: cmp
#include <errno.h>
#include <sys/mman.h>
#include <stdio.h>
#include <string.h>
int main(){
    size_t length_alloc = 10354688;
    size_t length_unmap = 5917171456;
    void *v = mmap(0, 10354688, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); 
    if (v == MAP_FAILED) {
            printf("mmap of %lu bytes failed with error: %s", 10354688, strerror(errno));
    }else{
            printf("mmaped %p\n", v); 
            munmap(v, length_unmap);
    }   

}
x208c,%eax 0x0804847f <+50>: jne 0x804848f <main+66> 0x08048481 <+52>: movl ##代码##x8048558,(%esp) 0x08048488 <+59>: call 0x8048310 <puts@plt> 0x0804848d <+64>: jmp 0x804849b <main+78> => 0x0804848f <+66>: movl ##代码##x8048569,(%esp) 0x08048496 <+73>: call 0x8048310 <puts@plt> 0x0804849b <+78>: mov ##代码##x0,%eax 0x080484a0 <+83>: leave 0x080484a1 <+84>: ret

what i'm tring to examine is $0x208c. When I type x/xw 0x208c it gives me back error which says Cannot access memory at address 0x208c. When i type Info registers and look at eax it says the value which i provided. So basically this program compares two values and depending on that prints something out.The problem is that this is homework from university and I have not got code. Hope you can help. Thank you.

我要检查的是 $0x208c。当我输入 x/xw 0x208c 时,它给我返回错误,提示无法访问地址 0x208c 处的内存。当我输入 Info registers 并查看 eax 时,它会显示我提供的值。所以基本上这个程序比较两个值,并根据它打印出一些东西。问题是这是大学的作业,我没有得到代码。希望你能帮忙。谢谢你。

回答by Employed Russian

When I type x/xw 0x208cit gives me back error which says Cannot access memory at address 0x208c

当我输入时,x/xw 0x208c它给了我一个错误,上面写着Cannot access memory at address 0x208c

The disassembly for your program says that it does something like this:

你的程序的反汇编说它做这样的事情:

##代码##

In other words, the 0x208cis a value (8332) that your program has hard-coded in it, and is nota pointer. Therefore, GDB is entirely correct in telling you that if you interpret 0x208cas a pointer, that pointer does not point to readable memory.

换句话说,0x208c8332你的程序硬编码在其中的值 ( ),而不是指针。因此,GDB 完全正确地告诉您,如果您将其解释0x208c为指针,则该指针不指向可读内存。

i finally figured out to use print statement instead of x/xw

我终于想出使用打印语句而不是 x/xw

You appear to not understand the difference between printand examinecommands. Consider this example:

您似乎不明白printexamine命令之间的区别。考虑这个例子:

##代码##

With above, print pfoowill give you the addressof foo, and x pfoowill give you the valuestored at that address (i.e. the value of foo).

通过以上,print pfoo会给你的地址foo,并且x pfoo会给你的存储在该地址(即的值foo)。

回答by user7610

I found out that it is impossible to examine mmaped memory that does not have PROT_READflag. This is not the OPs problem, but it was mine, and the error message is the same.

我发现不可能检查mmap没有PROT_READ标志的ed 内存。这不是 OP 的问题,而是我的问题,错误信息是一样的。

Instead of

代替

##代码##

do

##代码##

and voila, the memory can be examined.

瞧,可以检查内存。

回答by Suresh Babu

If GDB says memory address not found that means the symbol is not available in the executable file opened by gdb or through file exefilename. OR you have not compiled the exefile with -g option. What happens when you are a newbie for gdb you may have given the command file argfile instead of run argfile. Pls check.

如果 GDB 显示未找到内存地址,则表示该符号在 gdb 打开的可执行文件中或通过文件 exefilename 不可用。或者您没有使用 -g 选项编译 exefile。当您是 gdb 的新手时会发生什么,您可能已经给出了命令文件 argfile 而不是 run argfile。请检查。

回答by mahdi Lotfi

I experienced same error. I solved my case with increasing swap space with Gparted software. 1- First install Gparted with "sudo apt-get install gparted" 2- Open Gparted and right click on swap then select Resize/Move (Note: you be able to increase swap size only if you have unallocated memory before or after swap memory)

我遇到了同样的错误。我通过使用 Gparted 软件增加交换空间解决了我的问题。1- 首先使用“sudo apt-get install gparted”安装 Gparted 2- 打开 Gparted 并右键单击交换,然后选择调整大小/移动(注意:只有在交换内存之前或之后您有未分配的内存时,才能增加交换大小)

回答by Jacek Tomaka

In my case the problem was caused by calling munmap with length bigger than mmap:

在我的情况下,问题是由于调用长度大于 mmap 的 munmap 引起的:

##代码##

So the unmap unmapped also mappings for stacks of a few threads. Pretty nasty one because it rendered the core dump impossible to use with my current skill level. Especially that in the original problem, the size passed to munmap was somewhat random. And it crashed only sometimes and the end of a very lengthy process.

所以 unmap unmapped 也映射了几个线程的堆栈。非常讨厌,因为它使我目前的技能水平无法使用核心转储。特别是在原始问题中,传递给 munmap 的大小有些随机。它只是偶尔崩溃,并结束了一个非常漫长的过程。