I want to create an SBType
from a string. The goal is to use the SBType
to compute the pointer type. This is the current code:
# `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())
It works for char
, but not 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 asks a similar question, but has no solution.
Edit: The link @atl shared clued me in to using EvaluateExpression
to cast an expression. Here is an updated example:
# `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))
CodePudding user response:
Maybe this sequence of commands will help, the ideas come from these lldb notes
(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'>