jdagz
jdagz

Reputation: 43

In debugging with lldb: how do you access current variable values in a linked list?

I am trying to debug my code and want to follow the progression of my code line by line.

How do I view the values inside the linked list and the current value in an integer array?
I have tried the frame variable but it will only give me the memory location.

(t_stack *) stack_a = 0x0000000100504080 stack_a
(int *) temp = 0x00000001005040e0

Thank you very much in advance.

Upvotes: 0

Views: 495

Answers (1)

Jim Ingham
Jim Ingham

Reputation: 27110

There are two different scenarios mentioned here.

  1. A (type *) pointer to a memory area that you know is a pointer to an array of N elements of that type.

In that case you have to tell the debugger that it is an array and how many elements, since it can't infer that from type *. In frame var and in expr you do that by using the --element-count (short form -Z) option. There's a convenience alias parray expr count as well.

  1. A linked list

This one you are going to have to unroll by hand. There's no way for the debugger to know which member is the next element pointer, or what your termination condition is. So you'll have to do a little scripting to tell lldb how to present that type.

For instance, if the linked list's next pointer is called next and the termination condition is next == NULL, then you can do something like:

(lldb) script
>>> var = lldb.frame.FindVariable("LINKED_LIST_VARNAME")
>>> while 1:
...     # Print out whatever you want here - print(var) is the easiest
...     var = var.GetChildMemberWithName("next")
...     if var.GetValueAsUnsigned() == 0:
...         break
... 

If you do this a lot, then you can wrap this in a "python backed lldb command":

https://lldb.llvm.org/use/python-reference.html#create-a-new-lldb-command-using-a-python-function

that takes the name of the variable you want to print, and prints it as described above. Do remember, if you get that far, not to use the lldb.frame construct I used here. Instead, when you define your command, use the signature that takes an SBExecutionContext, and then you can get the current frame from that.

Upvotes: 2

Related Questions