In GDB, can you set memory as if it were a char array?

1

Say for instance I have a 32 element unsigned char array at address 0xdeadbeef. I would like to overwrite the contents of the array in memory. I am not compiled with -g, and so cannot just do a "set [variable name] = [my value]".

Is it possible to set the contents of the memory all at once?

I've seen someone try set *((unsigned char*) 0xdeadbeef) = "abcdefghijklmnop", but this doesn't appear to work.

Alternatively, if it isn't possible (for instance, because how would gdb know to convert that to the hex ascii representation?), is it possible to give multiple bytes, words, etc all at once? For example, I could just calculate the value in hex that I want the array to represent, but can I feed it all at once? Something like: set 0xdeadbeef = 0x4142434445464748495051

c
arrays
gdb
asked on Stack Overflow Sep 26, 2014 by Maxthecat

2 Answers

3

(Posting this just so the question has an "official" answer)

Carl's statements in the comments are entirely correct. You can do the following in gdb:

call strcpy(0xdeadbeef, "mystring")

This works for any of the functions included in the statically linked C library (memset, strncpy, etc).

answered on Stack Overflow Sep 29, 2014 by Maxthecat
1

There's alternative of writing char array in one command, without standard functions like strcpy().

set *(char [CHAR_ARRAY_SIZE] *) <WRITE_ADDRESS> = "YOUR_CHAR_ARRAY"

where CHAR_ARRAY_SIZE is the size of YOUR_CHAR_ARRAY, plus extra NULL byte.

e.g.

set *(char [15] *) 0x20018000 = "Write a string"
answered on Stack Overflow Jan 4, 2020 by Han

User contributions licensed under CC BY-SA 3.0