Creating C++ string in GDB

后端 未结 3 1889
我寻月下人不归
我寻月下人不归 2020-12-09 02:55

I\'m having trouble creating an std::string (or any C++ object, I guess) in GDB. I tried lots of variations to the following and none of them seem to work:

3条回答
  •  一向
    一向 (楼主)
    2020-12-09 03:07

    You should be able to construct a new std::string within the GDB. You want to allocate space on the heap to hold the std::string object, invoke the default constructor, and assign your string value. Here is an example:

    (gdb) call malloc(sizeof(std::string))
    $1 = (void *) 0x91a6a0
    (gdb) call ((std::string*)0x91a6a0)->basic_string()
    (gdb) call ((std::string*)0x91a6a0)->assign("Hello, World")
    $2 = (std::basic_string, std::allocator > &) @0x91a6a0: {static npos = , _M_dataplus = {> = {<__gnu_cxx::new_allocator> = {}, }, _M_p = 0x91a6f8 "Hello, World"}}
    (gdb) call SomeFunctionThatTakesAConstStringRef(*(const std::string*)0x91a6a0)
    

提交回复
热议问题