Creating C++ String in Gdb

Creating C++ string in GDB

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)

In gdb, how can I write a string to memory?

Say you have the following program:

int main(void){
char[] person = "Bob";
char[] p2 = "Alice";

printf("Hello %s\n");
}

With GDB you could set a breakpoint in main, and change the person's name via:

(gdb) set main::person = { 'S', 'a', 'm', 0x00 }

or more susinctly

(gdb) set main::person = "Sam"

If you want to set memory directly use:

set {char [4]} 0x08040000 = "Ace"

I'm assuming that since you're poking memory with gdb you know what you're doing, so you know about setting the null bytes for strings etc. Keep in mind if you are trying to change values for an array and you try to put in a string that is longer than what was originally allocated, you have a really good chance that you're going to corrupt memory. (example trying to set main::person to "Dilbert" is going to cause problems

setting strings in gdb

You can do this:

call a.assign("ok")

This way, gdb knows right away that it needs to call a function (rather than what you tried using operator=), it knows what function to call (std::string::assign), and it doesn't need to convert types at all (since there's an overload of assign which matches exactly).

How do I print the full value of a long string in gdb?

set print elements 0

From the GDB manual:

set print elements number-of-elements
Set a limit on how many elements of an array GDB will print. If GDB is printing a large array, it stops printing after it has printed the number of elements set by the set print elements command. This limit also applies to the display of strings. When GDB starts, this limit is set to 200. Setting number-of-elements to zero means that the printing is unlimited.

How to print the string a pointer points to while debugging using GDB?

Here printf is not a function, but a gdb command. Omit the parentheses.

Better yet, just use the print command, or the x command with format /s

(You can actually call the C function printf() with the call command.)`

gdb has voluminous help available with the help command. Try it.

Print string starting with NUL in gdb

Well, you should be able to print it as an array:

print *str@length

How to print a null-terminated string with newlines without showing backslash escapes in gdb?

Update:
Why not just use the gdb printf command?

(gdb) printf "%s", x
asd
qwe
...
(gdb)

Old answer:
From within the debugger you can execute commands. Just call printf

(gdb) call printf("%s", x)
asd
qwe
...
(gdb)


Related Topics



Leave a reply



Submit