C语言 可以从指针字符串创建SBType吗?

q8l4jmvw  于 2023-01-01  发布在  其他
关注(0)|答案(1)|浏览(190)

我想从一个字符串创建一个SBType,目标是使用SBType来计算指针类型,这是当前代码:

# `typ` is a string representing a C type (int, int32_t, char*).
typ_sbtype : lldb.SBType = target.FindFirstType (typ)
if not typ_sbtype.IsValid ():
    raise Exception ("type: {} is not valid".format(typ))
print (typ_sbtype.GetPointerType())

它适用于char,但不适用于char*

(lldb) target create "./a.out"
Current executable set to '/home/kevin/code/python_toolkit/lldb_scripting/debugme2/a.out' (x86_64).
(lldb) command script import print_ptrtype
print_ptrtype has been imported. Try `print_ptrtype <type>`.
(lldb) print_ptrtype "char"
char *
(lldb) print_ptrtype "char *"
Traceback (most recent call last):
  File "/home/kevin/code/python_toolkit/lldb_scripting/debugme2/./print_ptrtype.py", line 13, in print_ptrtype
    raise Exception ("type: {} is not valid".format(typ))
Exception: type: char * is not valid

Find function pointer SBType in LLDB when DWARF info contains only typedef提出了类似的问题,但没有解决方案。
编辑:link @atl shared提示我使用EvaluateExpression来转换表达式。下面是一个更新的示例:

# `typ` is a string representing a C type (int, int32_t, char*).
typ_sbtype : lldb.SBType = target.FindFirstType (typ)
if not typ_sbtype.IsValid ():
    # Try to cast 0 as an expression to obtain the type.
    ptr : lldb.SBValue = frame.EvaluateExpression("({}) 0".format(typ))
    typ_sbtype : lldb.SBType = ptr.GetType()
    if not typ_sbtype.IsValid():
        raise Exception ("type: {} is not valid".format(typ))
kknvjkwl

kknvjkwl1#

这一系列命令可能会有所帮助,其思想来自这些lldb注解

(lldb) script ptr_type = lldb.target.FindFirstType("int")
(lldb) script print(ptr_type)
int
(lldb) script ptr_type = lldb.target.FindFirstType("int").GetPointerType()
(lldb) script print(ptr_type)
int *
(lldb) script print(type(ptr_type))
<class 'lldb.SBType'>

相关问题