英文:
Assign string stored in memory into a GDB variable
问题
当调试C程序时,如何将存储在某个已知内存位置上的字符串(以\0
字节结尾的字芍数组)分配给GDB方便变量?
例如:
有一个字符串"hello_world"
存储在内存位置0xAAAAAAAA
,如何使用该内存位置将字符串存储到GDB变量string_variable
中?使用(gdb) set $string_Variable = (char *) 0xAAAAAAAA
会存储地址而不是字符串本身。
英文:
When debugging a C program, how can I assign a string (array of bytes terminated by \0
byte) stored at some known memory location into a GDB convenience variable?
E.g.:
There is a string "hello_world"
stored at memory location 0xAAAAAAAA
, how can I store the string into a GDB variable string_variable
using that memory location? Using (gdb) set $string_Variable = (char *) 0xAAAAAAAA
stores the address and not the string itself.
答案1
得分: 2
在GDB中,字符串方便变量是一个char
数组:
(gdb) set $foo = "bbb"
(gdb) ptype $foo
type = char [4]
使用GDB的CLI,我找不到一种直接从调试目标的NUL结尾字节字符串地址创建字符串方便变量的方法。
有效的方法是使用GDB的Python扩展从调试目标获取gdb.Value,然后将其转换为字符串:
(gdb) python gdb.set_convenience_variable("string_variable", gdb.parse_and_eval("(char *)0x555555556011").string())
(gdb) ptype $string_variable
type = char [12]
(gdb) p $string_variable
$3 = "hello_world"
英文:
A string convenience variable in GDB is an array of char
:
(gdb) set $foo = "bbb"
(gdb) ptype $foo
type = char [4]
Using GDB's CLI, I can't find a straightforward way to create a string convenience variable from an address of a NUL-terminated string of bytes in the debuggee.
What does work is to use GDB's Python extension to get a gdb.Value from the debuggee, then convert it to a string:
(gdb) python gdb.set_convenience_variable("string_variable", gdb.parse_and_eval("(char *)0x555555556011").string())
(gdb) ptype $string_variable
type = char [12]
(gdb) p $string_variable
$3 = "hello_world"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论