2015-11-25 63 views
1

这可能是其他地方所要求的,但对谷歌来说有点棘手。gdb - 执行当前行而不移动

我调试像在gdb下面一些代码(或cgdb更具体地):

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

正如我通过使用“N”的步骤,光标将达到“串b”线。在这一点上,我可以检查a的值,但由于该行尚未执行,因此b尚未填充。另一个按'n'将执行该行,但是也会移出if循环,现在b将超出范围。有没有办法在不移动的情况下执行当前行,以便在结果超出范围之前对其结果进行检查?

+0

我不认为这可以用gcc或其他编译器,只在行的粒度发出源到编译代码映射。作为一种解决方法,您可以在'string b ='行设置一个断点,并附带一个“watch b”命令,然后继续。在写入'b'后应该停止gdb,尽管这可能在构造函数或其他字符串类代码的中间,而不是在你的代码中。 –

+0

'手表'看起来像一个合理的答案,似乎尽可能接近我想要的。如果你把这个作为答案,我会接受它 – rbennett485

回答

0

只需在代码后添加代码b;即可。即

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

这类作品 - 它显然不是理想的。我说过,因为我正在调试的文件可能不是我正在编辑的文件,而是从代码库中的其他位置编辑的,所以使用我自己的版本进行更改和重建并非易事 – rbennett485

0

那么你可以随时到

(gdb) p stringMaker(); 

,无论你是在考虑到stringMaker()是接近直线的。如果这些变量在当前范围内,则可以执行任何种类的语句,即使涉及变量。对于更高级的用法,可以使用gdb的内部变量($1,$2等)来存储某些结果,以便稍后在涉及先前计算的变量超出范围时使用它。

最后上帝(无论那可能)发送给我们gdb Python API。只需键入py并拆除你的代码,以至于你会忘记你在第一个地方做了什么。

0

“N”的另一个印刷机将执行该行,但随后也将移动 外,如果环路和b现在将超出范围

的问题是,next执行过多的说明在b变量变为不可用。您可以使用stepfinish命令替换此单个next以实现更多的调试粒度,并在构建b后立即停止。这里是测试程序的示例gdb会话:

[[email protected] ~]$ cat ttt.cpp 
#include <string> 

int main() 
{ 
    if (true) 
    { 
    std::string a = "aaa"; 
    std::string b = "bbb"; 
    } 
    return 0; 
} 
[[email protected] ~]$ 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" 
相关问题