「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