将存储在内存中的字符串分配给 GDB 变量。

huangapple go评论67阅读模式
英文:

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"

huangapple
  • 本文由 发表于 2023年2月13日 23:17:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75437796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定