c - 在调试器中运行函数/方法

标签 c lldb

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

我有一个由解析器创建的带注释的语法树。我可以使用 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

关于c - 在调试器中运行函数/方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57461558/

相关文章:

c++ - 如何在 lldb 上设置 std::string 类型?

android - C/Renderscript/Neon Intrinsics 之间的电池电量消耗 -- 视频过滤器(边缘检测)APK

c - 为什么 scanf() 无法过滤双引号,尽管将格式设置为 [A-Za-z]

c - fgets();这部分代码是如何工作的? C语言编程

objective-c - 将日志附加到文件中

xcode - 如何在 Xcode 中使用 LLDB 调试时更改变量值?

ios - LLDB 代表什么?

xcode - 在调试期间检查 Swift 中的结构不起作用

ios - LLDB( swift ): Casting Raw Address into Usable Type

C-win32 : AttachThreadInput & SetFocus, 64 位:无线索