admin管理员组

文章数量:1122846

I have the following code:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s{"qwe"};
    while (true){} // in order to attach
    cout << s << endl;
}

I run gdbserver with the command:

sudo gdbserver --attach localhost:2345 `pidof my_proj`

Then I run gdb and attach to gdbserver:

gdb
(gdb) target remote localhost:2345

As expected, it freezes on while (true){}, and when I'm trying to read the s variable I get the following output:

(gdb) print s
$1 = {
  _M_dataplus = {<std::allocator<char>> = {<std::__new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x7ffea7445190 "qwe"}, 
  _M_string_length = 3, {_M_local_buf = "qwe", '\000' <repeats 12 times>, 
    _M_allocated_capacity = 6649713}}

It's not as pretty as I'd like it to be. But when I attach to my program directly with gdb and print the same variable, it looks quite pretty:

gdb -p `pidof my_proj`

(gdb) print s
$1 = "qwe"

What is the reason of such a behavior and how can I make the pretty printing work with gdbserver?

本文标签: cSTL containers don39t prettyprint when connected to gdbserverStack Overflow