C语言 在调试器中运行函数/方法



我正在尝试在调试器中运行以下函数。

我有一个由解析器创建的带注释的语法树。我可以使用 LLDB 浏览树,但我想使用函数来探索树。我不确定如何在LLDB中做到这一点。

IrNode*findNthIrNodeOfTypeHelper(State * N, IrNode * root, IrNodeType expectedType, int* nth)
{
if (root->type == expectedType)
{
if(*nth == 0)
{
return root;
}
*nth = *nth - 1;
}
IrNode * nthNode;
if (root->irLeftChild != NULL &&
(nthNode = findNthIrNodeOfTypeHelper(N, root->irLeftChild, expectedType, nth)) != NULL)
{
return nthNode;
}
if (root->irRightChild != NULL &&
(nthNode = findNthIrNodeOfTypeHelper(N, root->irRightChild, expectedType, nth)) != NULL)
{
return nthNode;
}
return NULL;
}

我不确定你在问什么,但给定一个像这样的 C 程序

#include <stdlib.h>
struct elem {
struct elem *left;
struct elem *right;
int datum;
};
int element_number = 0;
struct elem *new_elem ()
{
struct elem *e = (struct elem *) malloc (sizeof (struct elem));
e->left = NULL;
e->right = NULL;
e->datum = element_number++;
return e;
}
int main ()
{
struct elem *root = new_elem();
root->left = new_elem();
root->left->left = new_elem();
root->right = new_elem();
root->right->left = new_elem();
root->right->left->right = new_elem();
root->right->right = new_elem();
return root->datum;
}

我可以在 Python 中实现一个 lldb 命令,如下所示:

# import this into lldb with a command like
# command script import print_tree.py
from __future__ import print_function
import lldb
def print_node(result, value, indent_level):
indent_str = "  " * indent_level
datum = value.GetChildMemberWithName("datum")
left = value.GetChildMemberWithName("left")
right = value.GetChildMemberWithName("right")
if not datum.IsValid() or not left.IsValid() or not right.IsValid():
return
print(indent_str + ("datum: %d" % datum.GetValueAsUnsigned()), file=result)
if left.GetValueAsUnsigned() != 0:
print(indent_str + "left:", file=result)
print_node(result, left, indent_level + 1)
if right.GetValueAsUnsigned() != 0:
print(indent_str + "right:", file=result)
print_node(result, right, indent_level + 1)
def print_tree(debugger, command, *args):
"""Usage: print_tree
Print all the nodes in a tree which have left/right children to follow."""
exe_ctx = args[0]
result = args[1]
frame = exe_ctx.GetFrame()
if frame.IsValid() != True:
print("error: process is not paused.", file=result)
result.SetStatus (lldb.eReturnStatusFailed)
return
root = frame.FindVariable(command)
if not root.IsValid():
print("Could not find variable '%s'" % command, file=result)
print_node(result, root, 0)
def __lldb_init_module (debugger, dict):
debugger.HandleCommand('command script add -f %s.print_tree print_tree' % __name__)

然后使用它:

* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x0000000100000f85 a.out`main + 117 at a.c:29
26     root->right->left->right = new_elem();
27     root->right->right = new_elem();
28   
-> 29     return root->datum;
30   }
Target 0: (a.out) stopped.
Process 36029 launched: '/tmp/a.out' (x86_64)
(lldb) print_tree root
datum: 0
left:
datum: 1
left:
datum: 2
right:
datum: 3
left:
datum: 4
right:
datum: 5
right:
datum: 6
(lldb) q

最新更新