在gdb中,我如何写一个字符串到内存? [英] In gdb, how can I write a string to memory?
问题描述
(gdb)set {int}使用gdb写入整数或十六进制的内存地址非常简单: 0x08040000 = 42
(gdb)set {int} 0x08040000 = 0xffffffff
以类似简单的方式将字符或整个字符串写入内存?现在我不得不求助于将字符串转换为十六进制,然后输入,这很耗时。
解释方案下面的程序:
int main(void){
char [] person =Bob;
char [] p2 =Alice;
printf(Hello%s \\\
);
$ / code>
通过GDB,你可以在main中设置一个断点,并通过改变这个人的名字:
(gdb)set main :: person = {'S','a','m',0x00}
或更可疑
(gdb)set main :: person =Sam
如果你想直接使用内存设置:
set {char [4]} 0x08040000 =Ace
$ c $我假设既然你用gdb戳记了你知道你在做什么,所以你知道设置空字节为字符串等。请记住,如果您尝试更改数组的值,并且尝试放入比最初分配的字符串长的字符串,那么您很可能会损坏内存。 (例如试图将main :: person设置为Dilbert会导致问题
It is quite straightforward to write ints or hexadecimals to a memory address with gdb:
(gdb) set {int}0x08040000 = 42
(gdb) set {int}0x08040000 = 0xffffffff
But how can I write chars or entire strings in a similarly simple fashion to memory? Right now I have to resort to translating the string to hex and then entering that, which is time consuming.
解决方案 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
这篇关于在gdb中,我如何写一个字符串到内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!