View array in LLDB: equivalent of GDB's '@

2019-01-10 01:19发布

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?

8条回答
戒情不戒烟
2楼-- · 2019-01-10 01:23

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.

查看更多
SAY GOODBYE
3楼-- · 2019-01-10 01:26

I tried to add a comment but that wasn't great for posting a full answer so I made my own answer. This solves the problem with getting "No Value". You need to get the current frame as I believe lldb.frame is set at module import time so it doesn't have the current frame when you stop at a breakpoint if you load the module from .lldbinit. The other version would work if you import or reloaded the script when you stopped at the breakpoint. The version below should always work.

import lldb
import shlex

@lldb.command('parray', 'command script add -f parray.parray parray')
def parray(debugger, command, result, dict):

    target = debugger.GetSelectedTarget()
    process = target.GetProcess()
    thread = process.GetSelectedThread()
    frame = thread.GetSelectedFrame()

    args = shlex.split(command)
    if len(args) == 2:
        count = int(args[1])
        indices = range(count)
    elif len(args) == 3:
        first = int(args[1])
        count = int(args[2])
        indices = range(first, first + count)
    else:
        print 'Usage: parray ARRAY [FIRST] COUNT'
        return

    for i in indices:
        print frame.GetValueForVariablePath(args[0] + "[" + str(i) + "]")
查看更多
Explosion°爆炸
4楼-- · 2019-01-10 01:31

Actually there is a simple way to do it, 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.

查看更多
一夜七次
5楼-- · 2019-01-10 01:35

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 @.

查看更多
可以哭但决不认输i
6楼-- · 2019-01-10 01:41

Starting with Martin R answer I improved it as follow:

  1. If the pointer is not a simple variable, e.g.:

    struct {
      int* at;
      size_t size;
    } a;
    

    Then "parray a.at 5" fails.

    I fixed this by replacing "FindVariable" with "GetValueForVariablePath".

  2. Now what if the elements in your array are aggregates, e.g.:

    struct {
      struct { float x; float y; }* at;
      size_t size;
    } a;
    

    Then "parray a.at 5" prints: a.at->x, a.at->y, a.at[2], a.at[3], a.at[4] because GetChildAtIndex() returns members of aggregates.

    I fixed this by resolving "a.at" + "[" + str(i) + "]" inside the loop instead of resolving "a.at" and then retrieving its children.

  3. Added an optional "first" argument (Usage: parray [FIRST] COUNT), which is useful when you have a huge number of elements.

  4. Made it do the "command script add -f parray.parray parray" at init

Here is my modified version:

import lldb
import shlex

def parray(debugger, command, result, dict):
  args = shlex.split(command)
  if len(args) == 2:
    count = int(args[1])
    indices = range(count)
  elif len(args) == 3:
    first = int(args[1]), count = int(args[2])
    indices = range(first, first + count)
  else:
    print 'Usage: parray ARRAY [FIRST] COUNT'
    return
  for i in indices:
    print lldb.frame.GetValueForVariablePath(args[0] + "[" + str(i) + "]")

def __lldb_init_module(debugger, internal_dict):
  debugger.HandleCommand('command script add -f parray.parray parray')
查看更多
Rolldiameter
7楼-- · 2019-01-10 01:44

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];

查看更多
登录 后发表回答