View array in LLDB: equivalent of GDB's '@' operator in Xcode 4.1

38,841

Solution 1

There are two ways to do this in lldb.

Most commonly, you use the parray lldb command which takes a COUNT and an EXPRESSION; EXPRESSION is evaluated and should result in a pointer to memory. lldb will then print COUNT items of that type at that address. e.g.

parray 10 ptr

where ptr is of type int *.

Alternatively, it can be done by casting the pointer to a pointer-to-array.

For example, if you have a int* ptr, and you want to view it as an array of ten integers, you can do

p *(int(*)[10])ptr

Because it relies only on standard C features, this method works without any plugins or special settings. It likewise works with other debuggers like GDB or CDB, even though they also have specialized syntaxes for printing arrays.

Solution 2

Starting with the lldb in Xcode 8.0, there is a new built-in parray command. So you can say:

(lldb) parray <COUNT> <EXPRESSION>

to print the memory pointed to by the result of the EXPRESSION as an array of COUNT elements of the type pointed to by the expression.

If the count is stored in a variable available in the current frame, then remember you can do:

(lldb) parray `count_variable` pointer_to_malloced_array

That's a general lldb feature, any command-line argument in lldb surrounded in backticks gets evaluated as an expression that returns an integer, and then the integer gets substituted for the argument before command execution.

Solution 3

The only way I found was via a Python scripting module:

""" File: parray.py """
import lldb
import shlex

def parray(debugger, command, result, dict):
    args = shlex.split(command)
    va = lldb.frame.FindVariable(args[0])
    for i in range(0, int(args[1])):
        print va.GetChildAtIndex(i, 0, 1)

Define a command "parray" in lldb:

(lldb) command script import /path/to/parray.py
(lldb) command script add --function parray.parray parray

Now you can use "parray variable length":

(lldb) parray a 5
(double) *a = 0
(double) [1] = 0
(double) [2] = 1.14468
(double) [3] = 2.28936
(double) [4] = 3.43404

Solution 4

With Xcode 4.5.1 (which may or may not help you now), you can do this in the lldb console:

(lldb) type summary add -s "${var[0-63]}" "float *"
(lldb) frame variable pointer
  (float *) pointer = 0x000000010ba92950 [0.0,1.0,2.0,3.0, ... ,63.0]

This example assumes that 'pointer' is an array of 64 floats: float pointer[64];

Solution 5

It doesn't seem to be supported yet.

You could use the memory read function (memory read / x), like

(lldb) memory read -ff -c10 `test`

to print a float ten times from that pointer. This should be the same functionality as gdb's @.

Share:
38,841

Related videos on Youtube

midinastasurazz
Author by

midinastasurazz

Updated on December 05, 2020

Comments

  • midinastasurazz
    midinastasurazz over 3 years

    I would like to view an array of elements pointed to by a pointer. In GDB this can be done by treating the pointed memory as an artificial array of a given length using the operator '@' as

    *pointer @ length
    

    where length is the number of elements I want to view.

    The above syntax does not work in LLDB supplied with Xcode 4.1.

    Is there any way how to accomplish the above in LLDB?

    • Paul R
      Paul R almost 12 years
      Nearly a year later and there still doesn't seem to be this kind of functionality in lldb (I'm using LLDB-112.2 with Xcode 4.3.3) - adding a bounty in the hope that someone can come up with a usable workaround (other than going back to gdb).
  • Kaelin Colclasure
    Kaelin Colclasure about 11 years
    Newer versions of lldb (or perhaps Python) require that the assignments to first and count be on separate lines. Apart from that this works great! Thanks for this!
  • Nathan
    Nathan almost 11 years
    I don't really understand anything there but it works and is very helpful! Where do you learn such great lldb tricks?
  • Raffi
    Raffi over 10 years
    tip: if you need to reload the script after some modification, type "script reload(parray)" (see libertypages.com/clarktech/?p=4303)
  • Martin R
    Martin R over 10 years
    @Raffi: Thank you for the tip. And every link to lldb/Python information if valuable, as the official documentations is still limited.
  • Raffi
    Raffi over 10 years
    I was struggling for an hour to adapt Martin R to my specific case, thanks for the GetValueForVariablePath tip !!
  • NoahR
    NoahR over 10 years
    Great attempt and very useful. For most pointer expressions I am interested in GetValueForVariablePath is returning No Value. I'm using lldb-300.2.47 in Xcode 5.0. For int array[8], parry array 8 returns No Value eight times while print array[0] works as expected.
  • pmdj
    pmdj over 10 years
    You can use backticks to evaluate a pointer expression, e.g.: (lldb) memory read -ff -c10 `test`
  • Dave Reed
    Dave Reed almost 10 years
    I believe the problem is that lldb.frame is set at module import so instead, you need the command to get the current frame: target = debugger.GetSelectedTarget() process = target.GetProcess() thread = process.GetSelectedThread() frame = thread.GetSelectedFrame() and then use frame.GetValueForVariablePath instead of lldb.frame.GetValueForVariablePath
  • NoahR
    NoahR almost 10 years
    The comment above by @DaveReed addressed part of the problem. Simple pointer usage started working. (pointer variable in the current frame, no type conversion or arithmetic). I want to do more sophisticated expressions, so I've changed out GetValueForVariablePath for EvaluateExpression because I was still seeing No value. Now a pointer expression like this works: parray ((double*)sourcePointer+1) 5. The return type for both functions is the same per the API documentation, so EvaluateExpression seems a better way to go.
  • NoahR
    NoahR almost 10 years
    Oops. commented on your comment before seeing your answer. With this, simple pointer usage works. (pointer variable in the current frame, no type conversion or arithmetic). I want to do more sophisticated expressions, so I've changed out GetValueForVariablePath for EvaluateExpression because I was still seeing No value. Now a pointer expression like this works: parray ((double*)sourcePointer+1) 5. The return type for both functions is the same per the API documentation, so EvaluateExpression seems a better way to go. Do you agree?
  • NoahR
    NoahR almost 10 years
    Well, one difference is that the output of EvaluateExpression is assigned to lldb variables, and the array index isn't printed. So, the output is lines like: (double) $68 = 0
  • NoahR
    NoahR almost 10 years
    @MartinR because in my experimentation, the value 'a' has to be a straight-up pointer that exists in the stack frame and does not work if its an expression of any kind. (e.g. pointer cast, offset applied, etc.)
  • wxs
    wxs over 9 years
    This should be the accepted answer! It's easy and works out of the box
  • Bill
    Bill over 9 years
    Not really - you can add the Python version live to the LLDB session without having to restart your program and reproduce the error.
  • Bill
    Bill over 9 years
    This is a nice answer - it deserves more upvotes. No need for custom scripting or anything, and it even works with structs.
  • wardw
    wardw about 9 years
    And to save some typing x/10f test
  • Andrew Hundt
    Andrew Hundt over 8 years
    For those using the Xcode GUI who have a pointer only showing the first data elment, do the following: right click on data pointer > View value as... > Custom Type... In expression field put *(double(*)[10])value_type. That will print out the 10 values pointed to. You can modify double and 10 to be the type/quantity you want.
  • weezma2004
    weezma2004 over 8 years
    Thanks @AndrewHundt for the GUI related help. That's exactly what I wanted.
  • Andrew Hundt
    Andrew Hundt over 8 years
    @weezma2004 I'd appreciate if you could upvote the comment then :-) @ Siyuan Ren perhaps the info could be incorporated into your answer?
  • weezma2004
    weezma2004 over 8 years
    @AndrewHundt Done. Didn't even know you could upvote comments until now. :)
  • Siyuan Ren
    Siyuan Ren over 8 years
    @AndrewHundt: I don't think it is a good idea to incorporate your comment directly in the answer, since OP didn't ask a question about the GUI. Commenting section is good enough for information that people may need, but does not answer the question per se.
  • Shmidt
    Shmidt over 8 years
    @dave-reed, how to install or attach this script to lldb? Should I save it somewhere and then add to .lldbinit?
  • fpg1503
    fpg1503 about 8 years
    When I try to print an array inside a struct I get AttributeError: 'NoneType' object has no attribute 'FindVariable'
  • uliwitness
    uliwitness about 7 years
    Wouldn't that make every float* printed from now on show up as an array of 64 elements?
  • davidA
    davidA about 7 years
    Yes, it does. You can delete the type summary when you don't need it any more. Still better than only seeing the first value.
  • MarcusJ
    MarcusJ over 6 years
    Is there a way to set this variable permenantly, so I don't have to retype this into the lldb command prompt each time I run my app?
  • Jim Ingham
    Jim Ingham over 6 years
    Not quite sure what you mean. If you have an lldb command you want to use verbatim many times you can use command alias to make a shortcut.