0

I am trying to run the following function in the debugger.

I have an Annotated syntax tree created by a parser. I can explore the tree using LLDB but I want to use a function to explore the tree. I am not sure how I could do this in 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;
}
1
  • And, what is your question? What did you try, what do you expect, and what did you get? Commented Aug 12, 2019 at 17:42

1 Answer 1

1

I'm not sure exactly what you're asking, but given a C program like

#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;
}

I can implement an lldb command in Python, like this:

# 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__)

and then use it:

* 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.