在GDB中,您可以像设置为char数组一样设置内存吗?

在GDB中,您可以像设置为char数组一样设置内存吗?

问题描述:

例如,我在地址0xdeadbeef处有一个32元素的无符号char数组.我想覆盖内存中数组的内容.我没有使用-g进行编译,因此不能仅执行设置[变量名] = [我的值]".

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?

我见过有人尝试set *((unsigned char*) 0xdeadbeef) = "abcdefghijklmnop",但这似乎不起作用.

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

或者,如果不可能(例如,因为gdb如何知道将其转换为十六进制ascii表示形式?),是否可以一次提供多个字节,字等?例如,我可以只计算要数组表示的十六进制值,但是我可以一次输入所有值吗?类似于:set 0xdeadbeef = 0x4142434445464748495051

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

(发布此信息只是为了使问题有一个官方"答案)

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

卡尔在评论中的陈述是完全正确的.您可以在gdb中执行以下操作:

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

call strcpy(0xdeadbeef, "mystring")

这适用于静态链接的C库中包含的任何功能(memset,strncpy等).

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