gdb - 执行当前行而不继续

时间:2015-11-25 11:20:35

标签: gdb cgdb

这可能已经在其他地方被问过了,但对谷歌来说有点棘手。

我正在调试gdb(或更具体的cgdb)中的以下代码:

if(something) {
  string a = stringMaker();
  string b = stringMaker();
}

当我逐步使用' n'时,光标将到达'字符串b'线。此时我可以检查a的值,但由于该行尚未执行,因此尚未填充b。另一则媒体报道' n'将执行该行,但是也将移出if循环,b现在将超出范围。有没有办法在不继续前进的情况下执行当前行,以便在它超出范围之前检查其结果?

3 个答案:

答案 0 :(得分:0)

之后只需添加代码b;。即。

if(something) {
  string a = stringMaker();
  string b = stringMaker();
  b; // Break point here
}

答案 1 :(得分:0)

嗯,你总是可以

(gdb) p stringMaker(); 

无论你在哪个行,stringMaker()都可以访问。如果这些变量在当前范围内,您可以执行任何类型的语句,甚至涉及变量。对于更高级的用法,您可以使用gdb的内部变量($1,$2等)来存储某些结果,以便在以前计算中涉及的变量超出范围时使用它。

最后,上帝(无论可能是什么)向我们发送gdb Python API。只需输入py并拆除您的代码,以至于您将忘记您在第一时间所做的事情。

答案 2 :(得分:0)

  

另一次按' n'将执行该行,但随后也会移动   在if循环之外,b现在将超出范围

问题是next执行b变量中的过多指令变得不可用。您可以使用多个nextstep命令替换此单个finish,以便在调试时获得更多粒度,并在构建b后立即停止。以下是测试程序的示例gdb会话:

[ks@localhost ~]$ cat ttt.cpp 
#include <string>

int main()
{
  if (true)
  {
    std::string a = "aaa";
    std::string b = "bbb";
  }
  return 0;
}
[ks@localhost ~]$ gdb -q a.out 
Reading symbols from a.out...done.
(gdb) start 
Temporary breakpoint 1 at 0x40081f: file ttt.cpp, line 7.
Starting program: /home/ks/a.out 

Temporary breakpoint 1, main () at ttt.cpp:7
7       std::string a = "aaa";
(gdb) n
8       std::string b = "bbb";
(gdb) p b
$1 = ""
(gdb) s
std::allocator<char>::allocator (this=0x7fffffffde8f) at /usr/src/debug/gcc-5.1.1-20150618/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/allocator.h:113
113       allocator() throw() { }
(gdb) fin
Run till exit from #0  std::allocator<char>::allocator (this=0x7fffffffde8f) at /usr/src/debug/gcc-5.1.1-20150618/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/allocator.h:113
0x0000000000400858 in main () at ttt.cpp:8
8       std::string b = "bbb";
(gdb) s
std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string (this=0x7fffffffde70, __s=0x400984 "bbb", __a=...) at /usr/src/debug/gcc-5.1.1-20150618/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.tcc:656
656     basic_string<_CharT, _Traits, _Alloc>::
(gdb) fin
Run till exit from #0  std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string (this=0x7fffffffde70, __s=0x400984 "bbb", __a=...) at /usr/src/debug/gcc-5.1.1-20150618/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.tcc:656
0x000000000040086d in main () at ttt.cpp:8
8       std::string b = "bbb";
(gdb) p b
$2 = "bbb"