如何在调试C程序时将gdb值转换为python数字对象 [英] How to convert a gdb Value to a python numeral object while debugging C program

查看:155
本文介绍了如何在调试C程序时将gdb值转换为python数字对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在调试C程序时使用了python2.6的gdb模块,并且希望将gdb.Value实例转换为基于实例'.Type'的python数字对象(变量)。

例如,通过 sfv = gdb.parse_and_eval('SomeStruct-> ;. > some_double_val'),但THEN把它变成一个双精度浮点python变量 - 知道 str(sfv.type.strip_typedefs())=='double' 并且它的大小是8B - 无需仅仅通过使用 dbl = float(str(sfv)) Value.string(),而是像使用 struct 解开字节以获得正确的double值一样。



从我的搜索中返回的每个链接都指向 https://sourceware.org/gdb/onlinedocs/gdb/Values-From-Inferior.html#Values-From-Inferior ,但我看不到如何将Value实例转换为python变量干净地说,价值甚至不是在C内存中,但代表了一个gdb.Value.address(所以不能使用 Inferior.read_memory()),如何将它变成一个Python int而不用强制字符串值?

解决方案

您可以使用 int 或<$ c直接从 Value

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

(gdb)python print float(gdb.Value(0.0))
0.0

系统中似乎至少有一个故障,因为 float(gdb.Value(0))不起作用。 p>

I'm using python2.6's gdb module while debugging a C program, and would like to convert a gdb.Value instance into a python numeral object (variable) based off the instance's '.Type'.

E.g. turn my C program's SomeStruct->some_float_val = 1./6; to a Python gdb.Value via sfv=gdb.parse_and_eval('SomeStruct->some_double_val'), but THEN turn this into a double precision floating point python variable -- knowing that str(sfv.type.strip_typedefs())=='double' and its size is 8B -- WITHOUT just converting through a string using dbl=float(str(sfv)) or Value.string() but rather something like unpacking the bytes using struct to get the correct double value.

Every link returned from my searches points https://sourceware.org/gdb/onlinedocs/gdb/Values-From-Inferior.html#Values-From-Inferior, but I can't see how to convert a Value instance into a python variable cleanly, say the Value wasn't even in C memory but represented a gdb.Value.address (so can't use Inferior.read_memory()), how would one turn this into a Python int without casting string values?

解决方案

You can convert it directly from the Value using int or float:

(gdb) python print int(gdb.Value(0))
0
(gdb) python print float(gdb.Value(0.0))
0.0

There seems to be at least one glitch in the system, though, as float(gdb.Value(0)) does not work.

这篇关于如何在调试C程序时将gdb值转换为python数字对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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