Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting an std::string variable value from gdb?

Tags:

Is it possible... when the debugger is stopped at a breakpoint, to modify the value of a std::string variable without resorting to hacks like tweaking the memory image of the current buffer?

e.g. something like "set var mystring="hello world"

?

like image 810
Stabledog Avatar asked Mar 23 '10 17:03

Stabledog


People also ask

How do you set a variable value in gdb?

Use the set variable (gdb) and the assign (dbx) commands to change the value associated with a variable, memory address, or expression that is accessible according to the scope and visibility rules of the language. The expression can be any expression that is valid in the current context.

Which command is used to determine the variable in gdb?

The ptype [ARG] command will print the type. Show activity on this post. This question may be related: vtable in polymorphic class of C++ using gdb: (gdb) help set print object Set printing of object's derived type based on vtable info.

What is ESP in gdb?

The following registers are mentioned in the article: ESP (points to the top of the stack) EBP (is used as a reference when accessing local variables and arguments of the function) EIP (points to the address of the next instruction)


1 Answers

Try this (tested and works for me):

call mystring.assign("hello world") 

The key is that instead of modifying memory directly, you call the object's functions to change its state. It so happens that std::basic_string has a member function called assign which does the job.

like image 86
Chris Jester-Young Avatar answered Sep 28 '22 10:09

Chris Jester-Young