evaluate variable/function in gdb

混江龙づ霸主 提交于 2019-12-07 19:25:40

问题


I have the following code,

...
char* seg = mmap(0, ...)
printf("seg=%x\n", seg);
...

The program prints with seg=b7ffd000. While in gdb (for the same execution), when using p/x seg, it prints $2 = 0x0. I am confused here. Isn't it the same var seg? why are the values different.

PS: in mmap, the first argument is the preferably address of mapped memory and the return value is the actual address of mapped memory.


回答1:


Now that you've answered my question in the comments, I can answer!

The value that you see as the result of the printf is the real address. You are seeing 0 as the value of seg in the debugger because when optimizations are enabled, the compiler is free to do all sorts of weird things (which generally makes step-by-step debugging tricky). The "observable" behaviour should always be correct, though (assuming you're not relying on any undefined behaviour).



来源:https://stackoverflow.com/questions/5823843/evaluate-variable-function-in-gdb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!