32

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:

(gdb) p std::string("hello")
A syntax error in expression, near `"hello")'.

Is there a way to do it?

(I'm surprised I couldn't find anything about this on the Web. I'm starting to think if my GDB is buggy or I'm doing something very wrong.)

Johannes Sasongko
  • 4,178
  • 23
  • 34

3 Answers3

42

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<char, std::char_traits<char>, std::allocator<char> > &) @0x91a6a0: {static npos = <optimized out>, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x91a6f8 "Hello, World"}}
(gdb) call SomeFunctionThatTakesAConstStringRef(*(const std::string*)0x91a6a0)
Jason Dillaman
  • 436
  • 5
  • 2
  • 13
    This can be done much simpler with "convenience variables." Something like: (gdb) set $mystr = (std::string*) malloc(sizeof(std::string)), (gdb) call $mystr->basic_string(), (gdb) call $mystr->assign("foo"), (gdb) call some_function(*$mystr) – EvanED Aug 28 '15 at 17:18
  • 2
    `(gdb) call malloc(sizeof(std::string))` results in `No symbol "string" in namespace "std".` What am I doing wrong? – Moberg Nov 24 '20 at 09:42
  • 1
    @Moberg you need `call (void *)malloc(sizeof(std::string))` – fghj Apr 27 '21 at 12:53
  • I get `Converting character sets: Invalid argument.` for assign() –  Feb 13 '22 at 04:25
3

GDB cannot really do what you describe. Your case involves:

  1. instantiating a basic_string template and generating code for the class
  2. generate a call to constructor

This means it must do the work of the same complexity as a compiler. This is not the job of the debugger.

With that said, GDB is capable of evaluating a limited subset of statements, like calling an existing function with existing data and retrieving its result, since this won't involve generating a lot of code.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
Alex B
  • 82,554
  • 44
  • 203
  • 280
  • I think "too complex" is a perfectly valid reason. However, I disagree with your opinion that it's "not the job of the debugger"; maybe I'm too used to Python's debugger allowing me to run any Python code in the environment. – Johannes Sasongko Sep 16 '11 at 14:24
  • 5
    Of course it's not the job of the debugger to instantiate templates, but since I already have the basic_string functions compiled into my program, I see no reason why it would in principle be unable to find and call them, without resorting to the hilarious workaround given by Jason Dillaman. – mhsmith Oct 18 '12 at 14:28
  • 2
    I don't see what's so hilarious about Jason's answer. It seems perfectly reasonable to me. – inetknght Jun 17 '14 at 18:41
-2

What do you mean by "creating"? GDB doesn't persist C++ objects (your application does that), so you can't create a C++ object in GDB itself.

However, you should be able to call specific function of your application from GDB.

Kos
  • 70,399
  • 25
  • 169
  • 233