“N”的另一个印刷机将执行该行,但随后也将移动 外,如果环路和b现在将超出范围
的问题是,next
执行过多的说明在b
变量变为不可用。您可以使用step
和finish
命令替换此单个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"
我不认为这可以用gcc或其他编译器,只在行的粒度发出源到编译代码映射。作为一种解决方法,您可以在'string b ='行设置一个断点,并附带一个“watch b”命令,然后继续。在写入'b'后应该停止gdb,尽管这可能在构造函数或其他字符串类代码的中间,而不是在你的代码中。 –
'手表'看起来像一个合理的答案,似乎尽可能接近我想要的。如果你把这个作为答案,我会接受它 – rbennett485