Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与lldb中的python gdb.execute(';…';)等效的_Python_Lldb - Fatal编程技术网

与lldb中的python gdb.execute(';…';)等效的

与lldb中的python gdb.execute(';…';)等效的,python,lldb,Python,Lldb,如果我有这样一个结构(在C中) 还有它的一个实例,比如说a和a.x=1和a.y={2,3} 要从python脚本中访问a.y[1],我真的必须执行这个非常健谈的命令吗 script print lldb.frame.FindVariable('a').GetChildMemberWithName('y').GetChildAtIndex(1) 我编写此函数是为了帮助我在C中打印结构变量的成员: # lldb_cmds.py import lldb def get_nested_val_c(

如果我有这样一个结构(在C中)

还有它的一个实例,比如说
a
a.x=1
a.y={2,3}

要从python脚本中访问
a.y[1]
,我真的必须执行这个非常健谈的命令吗

script print lldb.frame.FindVariable('a').GetChildMemberWithName('y').GetChildAtIndex(1)
我编写此函数是为了帮助我在C中打印结构变量的成员:

# lldb_cmds.py

import lldb

def get_nested_val_c(vnames,idx=None):
    """
        vnames is a list of strings ['a','b','c',...] that will be evaluated as if
        you called

            a.b.c.,...

        So for [a,b,c] you get:

            a.b.c

        If idx is given, it is evaluated as

            a.b.c[idx]

    """

    try:
        x=lldb.frame.FindVariable(vnames[0])
        for v_ in vnames[1:]:
            x=x.GetChildMemberWithName(v_)
    except TypeError:
        x=lldb.frame.FindVariable(vnames)

    if idx == None:
        return x

    try:
        x=x.GetChildAtIndex(idx[0])
        for i_ in idx[1:]:
            x=x.GetChildAtIndex(i_,False,True)
    except TypeError:
        x=x.GetChildAtIndex(idx)
然后可以加载

command script import lldb_cmds.py
和我一起打电话(如上所述)

但是有没有一条更短的路?是的,我知道你会写作

p a.y[1]
但是,既然lldb中似乎没有任何while循环,我怎么能用一个变量索引打印它,而不使用这么长的语句呢


(是的,我知道你可以为这个例子写:
p*(int(*)[2])a.y
,但我一般都在问。)

我不完全确定你想在这里做什么。我将回答几个潜在的问题,你可以告诉我其中一个是否正确

如果您试图找到一种更好的方法来访问已知的嵌套结构,这可能会更方便:

var = lldb.frame.FindVariable("a").GetValueForExpressionPath("y[0]")
如果您尝试在Python脚本中运行命令行命令,则有两种方法:

lldb.debugger.HandleCommand("command")
只需运行命令,并将结果打印到lldb的标准输出,或:

ret_val = lldb.SBCommandReturnObject()
lldb.debugger.GetCommandInterpreter().HandleCommand("command", ret_val)
它将运行该命令,并将该命令的结果(即在命令运行时在lldb驱动程序中打印的结果)返回到
ret_val
对象中。如果您想检查命令的结果,这很方便。ReturnObject还告诉您命令是否成功,如果没有成功,则保留错误

如果您试图打印静态分配的数组(如示例中所示),则不需要执行上面显示的强制转换,只需执行以下操作:

(lldb)每年

这样就可以打印出所有的元素。如果您处理的是动态大小的数组(例如,指向malloc'ed数组的int*),那么Xcode 8.0中的lldb有一个新命令
parray
,它可以让您说:

(lldb)帕雷10年

打印y指向的整数数组的10个元素

lldb.debugger.HandleCommand("command")
ret_val = lldb.SBCommandReturnObject()
lldb.debugger.GetCommandInterpreter().HandleCommand("command", ret_val)