如何访问Python GDB Value的键或值

发布于 2024-12-28 04:20:40 字数 504 浏览 0 评论 0原文

我在 GDB 中有一个结构体,想要运行一个检查该结构体的脚本。在 Python GDB 中,您可以通过以下方式轻松访问该结构。

(gdb) python mystruct = gdb.parse_and_eval("mystruct")

现在我得到了这个名为 mystruct 的变量,它是一个 GDB.Value 对象。我只需将此对象用作字典即可访问该结构的所有成员(例如mystruct['member'])。

问题是,我的脚本不知道某个结构有哪些成员。所以我想从这个 GDB.Value 对象中获取键(甚至是值)。但 mystruct.values() 和 mystruct.keys() 在这里都不起作用。

是否无法访问此信息?我认为您不太可能无法访问此信息,但我没有在任何地方找到它。 dir(mystruct) 告诉我也没有键或值函数。我可以通过打印 mystruct 来查看所有成员,但是没有办法在 python 中获取成员吗?

I have a struct in GDB and want to run a script which examines this struct. In Python GDB you can easily access the struct via

(gdb) python mystruct = gdb.parse_and_eval("mystruct")

Now I got this variable called mystruct which is a GDB.Value object. And I can access all the members of the struct by simply using this object as a dictionary (likemystruct['member']).

The problem is, that my script doesn't know which members a certain struct has. So I wanted to get the keys (or even the values) from this GDB.Value object. But neither mystruct.values() nor mystruct.keys() is working here.

Is there no possibility to access this information? I think it's highly unlikely that you can't access this information, but I didn't found it anywhere. A dir(mystruct) showed me that there also is no keys or values function. I can see all the members by printing the mystruct, but isn't there a way to get the members in python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

方圜几里 2025-01-04 04:20:40

从 GDB 文档

您可以获取mystruct 的类型如下所示:

tp = mystruct.type

并迭代 字段 通过 tp.fields( )

不需要邪恶的解决方法;-)

更新:
GDB 7.4 刚刚发布。来自公告

结构和联合类型的类型对象现在允许访问
使用标准 Python 字典(映射)方法的字段。

From GDB documentation:

You can get the type of mystruct like so:

tp = mystruct.type

and iterate over the fields via tp.fields()

No evil workarounds required ;-)

Update:
GDB 7.4 has just been released. From the announcement:

Type objects for struct and union types now allow access to
the fields using standard Python dictionary (mapping) methods.

凉薄对峙 2025-01-04 04:20:40

邪恶的解决方法:

python print eval("dict(" + str(mystruct)[1:-2] + ")")

我不知道这是否具有普遍性。作为演示,我编写了一个最小的示例 test.cpp

#include <iostream>

struct mystruct
{
  int i;
  double x;
} mystruct_1;

int main ()
{
  mystruct_1.i = 2;
  mystruct_1.x = 1.242;
  std::cout << "Blarz";
  std::cout << std::endl;
}

现在我像往常一样运行 g++ -g test.cpp -o test 并启动 gdb test.以下是会话记录示例:

(gdb) break main
Breakpoint 1 at 0x400898: file test.cpp, line 11.
(gdb) run
Starting program: ...

Breakpoint 1, main () at test.cpp:11
11        mystruct_1.i = 2;
(gdb) step
12        mystruct_1.x = 1.242;
(gdb) step
13        std::cout << "Blarz";
(gdb) python mystruct = gdb.parse_and_eval("mystruct_1")
(gdb) python print mystruct
{i = 2, x = 1.242}
(gdb) python print eval("dict(" + str(mystruct)[1:-2] + ")")
{'i': 2, 'x': 1.24}
(gdb) python print eval("dict(" + str(mystruct)[1:-2] + ")").keys()
['i', 'x']

Evil workaround:

python print eval("dict(" + str(mystruct)[1:-2] + ")")

I don't know if this is generalisable. As a demo, I wrote a minimal example test.cpp

#include <iostream>

struct mystruct
{
  int i;
  double x;
} mystruct_1;

int main ()
{
  mystruct_1.i = 2;
  mystruct_1.x = 1.242;
  std::cout << "Blarz";
  std::cout << std::endl;
}

Now I run g++ -g test.cpp -o test as usual and fire up gdb test. Here is a example session transcript:

(gdb) break main
Breakpoint 1 at 0x400898: file test.cpp, line 11.
(gdb) run
Starting program: ...

Breakpoint 1, main () at test.cpp:11
11        mystruct_1.i = 2;
(gdb) step
12        mystruct_1.x = 1.242;
(gdb) step
13        std::cout << "Blarz";
(gdb) python mystruct = gdb.parse_and_eval("mystruct_1")
(gdb) python print mystruct
{i = 2, x = 1.242}
(gdb) python print eval("dict(" + str(mystruct)[1:-2] + ")")
{'i': 2, 'x': 1.24}
(gdb) python print eval("dict(" + str(mystruct)[1:-2] + ")").keys()
['i', 'x']
情深已缘浅 2025-01-04 04:20:40

这些天:

(gdb) python import sys; print(sys.version)
3.10.8 (main, Oct 15 2022, 19:00:40)  [GCC 12.2.0 64 bit (AMD64)]

...它变得容易多了,属性是 dict 中的键 - 这是一个示例:

test_struct.c

#include <stdint.h>
#include <stdio.h>

struct mystruct_s {
  uint8_t member;
  uint8_t list[5];
};
typedef struct mystruct_s mystruct_t;

mystruct_t mystruct = {
  .member = 0,
  .list = { 10, 20, 30, 40, 50 },
};

int main(void) {
  printf("mystruct.member %d\n", mystruct.member);
  for(uint8_t ix=0; ix<sizeof(mystruct.list); ix++) {
    printf("mystruct.list[%d]: %d\n", ix, mystruct.list[ix]);
  }
}

然后编译并输入 gdb:

gcc -g -o test_struct.exe test_struct.c
gdb --args ./test_struct.exe

...然后在 gdb 中:

(gdb) b main
Breakpoint 1 at 0x140001591: file test_struct.c, line 16.

(gdb) python ms = gdb.parse_and_eval("mystruct")
(gdb) python print(ms)
{member = 0 '\000', list = "\n\024\036(2"}
(gdb) python print(ms['member'])
0 '\000'
(gdb) python print(ms['list'])
"\n\024\036(2"

(gdb) python for ix in range(0,5): print("mystruct.list[{}]: {}".format(ix, ms['list'][ix]))
mystruct.list[0]: 10 '\n'
mystruct.list[1]: 20 '\024'
mystruct.list[2]: 30 '\036'
mystruct.list[3]: 40 '('
mystruct.list[4]: 50 '2'

These days:

(gdb) python import sys; print(sys.version)
3.10.8 (main, Oct 15 2022, 19:00:40)  [GCC 12.2.0 64 bit (AMD64)]

... it got a lot easier, properties are keys in dict - here is an example:

test_struct.c:

#include <stdint.h>
#include <stdio.h>

struct mystruct_s {
  uint8_t member;
  uint8_t list[5];
};
typedef struct mystruct_s mystruct_t;

mystruct_t mystruct = {
  .member = 0,
  .list = { 10, 20, 30, 40, 50 },
};

int main(void) {
  printf("mystruct.member %d\n", mystruct.member);
  for(uint8_t ix=0; ix<sizeof(mystruct.list); ix++) {
    printf("mystruct.list[%d]: %d\n", ix, mystruct.list[ix]);
  }
}

Then compile and enter gdb:

gcc -g -o test_struct.exe test_struct.c
gdb --args ./test_struct.exe

... then in gdb:

(gdb) b main
Breakpoint 1 at 0x140001591: file test_struct.c, line 16.

(gdb) python ms = gdb.parse_and_eval("mystruct")
(gdb) python print(ms)
{member = 0 '\000', list = "\n\024\036(2"}
(gdb) python print(ms['member'])
0 '\000'
(gdb) python print(ms['list'])
"\n\024\036(2"

(gdb) python for ix in range(0,5): print("mystruct.list[{}]: {}".format(ix, ms['list'][ix]))
mystruct.list[0]: 10 '\n'
mystruct.list[1]: 20 '\024'
mystruct.list[2]: 30 '\036'
mystruct.list[3]: 40 '('
mystruct.list[4]: 50 '2'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文