在gdb中,我如何写一个字符串到内存? [英] In gdb, how can I write a string to memory?

查看:1126
本文介绍了在gdb中,我如何写一个字符串到内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 (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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆