在没有优化但当前上下文中仍然没有局部变量符号的程序上使用 gdb 进行调试

2022-01-20 00:00:00 gcc debugging gdb c++

我一直有这个问题,但似乎总是推迟问这个问题,因为我好像做错了什么......但现在我觉得不是......拿了这个代码:

I've been having this problem for while now, but always seem to put off asking this question because it seems like I am doing something wrong... but right now I feel otherwise... taken this code:

#include <string>
#include <iostream>
#include <algorithm>
int main(int argc, char** argv)
{
  if(argc < 2)
  {
    std::cout << "usage: " << argv[0] << " <string>" << std::endl;
    return 1;
  }
  std::string str = argv[1];
  std::reverse(str.begin(), str.end());
  std::cout << str << std::endl;
  return 0;
}

用命令编译:

g++ -std=c++11 -Wall main.cpp -o main -O0 -ggdb3

我使用的是最新的主干版本的 gcc,我的主干是在 9 月 23 日左右拍摄的……另外请注意,我没有进行优化编译!

I am using a very recent trunk version of gcc, I the trunk was taken around September 23rd ish... Also note that I am not compiling with optimization!

不管怎样,我现在开始调试,像这样:

Now anyway, I start debugging, like this:

gdb --quiet --args ./main string

我在第 12 行设置了一个断点(反向算法)

I set a break point at line 12 (the reverse algorithm)

b 12

然后我运行程序

run

然后我尝试打印出字符串,看看它是什么

then I try to print out the string, to see what it is

print str

亲爱的朋友们,我觉得这很奇怪:上一条命令的输出是:

And this, my dear friends, is what seems strange to me: The output of that previous command is:

No symbol "str" in current context.

快速检查局部变量也不显示字符串

a quick check to the local variables doesn't show the string either

info locals

我得到的只是

std::__ioinit = {static _S_refcount = 2, static _S_synced_with_stdio = true}

所以我想知道,是我有错,还是编译器或调试器有错...长期以来,这对我来说一直是一个非常痛苦的问题...所以感谢您阅读本文问题... :)

so I'm wondering, am I at fault, or is the compiler or debugger at fault... this has been a pretty pain in the ass problem for a long time for me... so thanks for even reading this question... :)

现在很明显我的 gcc 构建有问题,我想知道是否有人遇到过错误报告或任何其他似乎存在类似问题的情况... 我还将尝试检查最近构建的 gdb,以确保我当前的调试器(ubuntu 附带)绝对没有问题......这有意义吗?

now that it has become clear that there is something wrong with my gcc build, I'm wondering if anyone has come across a bug report or any other case where there seems to a be a problem similar to this... I will also try checking with a recent build of gdb to make sure that there is definitely not a problem with my current debugger (that comes with ubuntu)... does that make sense?

所以在编译 gdb v7.5 之后,我得到了相对相同的结果,除了根本没有本地人存在......我想这意味着这是一个 gcc 问题,谢谢大家......

So after compiling gdb v7.5, I got relatively the same result except there were no locals present at all... I guess that means it's a gcc issue, thanks everyone...

推荐答案

不,即使使用 --quiet 它也对我有用.可能你的设置有问题.

No, even with --quiet it works for me. Maybe there's something wrong with your setup.

~/tmp $ g++ -Wall tmp.cpp -o tmp -O0 -ggdb3
~/tmp $ gdb --quiet --args ./tmp string
Reading symbols from /xxxxxxxx/tmp...done.
(gdb) b 12
Breakpoint 1 at 0x400c95: file tmp.cpp, line 12.
(gdb) run
Starting program: /xxxxxxxx/tmp string
Breakpoint 1, main (argc=2, argv=0x7fffffffdc58) at tmp.cpp:12
12        std::reverse(str.begin(), str.end());
(gdb) print str
$1 = "string"
(gdb) info locals
str = "string"
(gdb)

相关文章