std::string 是全局变量时在 gdb 中无法打印,为什么?
std::string cannot be printed in gdb when it's global variable, why?
我有一个teststring.cpp文件,我想看看gdb能不能输出std::string值:
#include<string>
#include<iostream>
using namespace std;
string sHello="hello world";
int main()
{
cout<<sHello.c_str()<<endl;
return 0;
}
使用 g++ teststring.cpp -g 和 gdb a.out
(gdb) b main
Breakpoint 1 at 0x400bfa: file teststring.cpp, line 7.
(gdb) r
Starting program: /home/x/a.out
Breakpoint 1, main () at teststring.cpp:7
7 cout<<sHello.c_str()<<endl;
(gdb) p sHello
No symbol "sHello" in current context.
这很奇怪。如果我移动
string sHello="hello world";
作为main的第一行,像这样:
#include<string>
#include<iostream>
using namespace std;
int main()
{
string sHello="hello world";
cout<<sHello.c_str()<<endl;
return 0;
}
然后 "p sHello" 如果我调试它就会工作。
嗯,这对我来说太奇怪了。
Why it says no symbol?
更奇怪的是,我用“$ readelf -s a.out|grep sHello”检查了a.out的两个版本,都没有给我任何结果。
我想当有符号命名为 "sHello":
sHello should appear in symbol table of ELF file, right?
More strange is, I used "$ readelf -s a.out|grep sHello" to check both versions of a.out, neither of them give me any result.
你应该试试:
$ readelf -w a.out | fgrep sHello
<2fd3> DW_AT_name : (indirect string, offset: 0xce6): sHello
-w
开关显示文件中调试部分的内容(如果存在)。
Why it says no symbol?
罪魁祸首可能是编译器和调试器之间的不匹配。
我无法重现问题(使用 g++ v5.4.0 和 gdb 7.11.1):
(gdb) p sHello
= {static npos = <optimized out>,
_M_dataplus = {<std::allocator<char>> =
{<__gnu_cxx::new_allocator<char>> = {<No data fields>},
<No data fields>},
_M_p = 0x614c38 "hello world"}}
也许在更高版本的 GDB 上您会得到预期的行为。
我有一个teststring.cpp文件,我想看看gdb能不能输出std::string值:
#include<string>
#include<iostream>
using namespace std;
string sHello="hello world";
int main()
{
cout<<sHello.c_str()<<endl;
return 0;
}
使用 g++ teststring.cpp -g 和 gdb a.out
(gdb) b main
Breakpoint 1 at 0x400bfa: file teststring.cpp, line 7.
(gdb) r
Starting program: /home/x/a.out
Breakpoint 1, main () at teststring.cpp:7
7 cout<<sHello.c_str()<<endl;
(gdb) p sHello
No symbol "sHello" in current context.
这很奇怪。如果我移动
string sHello="hello world";
作为main的第一行,像这样:
#include<string>
#include<iostream>
using namespace std;
int main()
{
string sHello="hello world";
cout<<sHello.c_str()<<endl;
return 0;
}
然后 "p sHello" 如果我调试它就会工作。 嗯,这对我来说太奇怪了。
Why it says no symbol?
更奇怪的是,我用“$ readelf -s a.out|grep sHello”检查了a.out的两个版本,都没有给我任何结果。
我想当有符号命名为 "sHello":
sHello should appear in symbol table of ELF file, right?
More strange is, I used "$ readelf -s a.out|grep sHello" to check both versions of a.out, neither of them give me any result.
你应该试试:
$ readelf -w a.out | fgrep sHello
<2fd3> DW_AT_name : (indirect string, offset: 0xce6): sHello
-w
开关显示文件中调试部分的内容(如果存在)。
Why it says no symbol?
罪魁祸首可能是编译器和调试器之间的不匹配。
我无法重现问题(使用 g++ v5.4.0 和 gdb 7.11.1):
(gdb) p sHello
= {static npos = <optimized out>,
_M_dataplus = {<std::allocator<char>> =
{<__gnu_cxx::new_allocator<char>> = {<No data fields>},
<No data fields>},
_M_p = 0x614c38 "hello world"}}
也许在更高版本的 GDB 上您会得到预期的行为。