Syntax for passing a user-defined variable as a command option


With LLDB version 8 I'm trying to dump the code bytes for a function "fh", using the memory read command, using variables to calculate the length:

(lldb) expr unsigned $addr = (unsigned) fh
(lldb) expr unsigned $next_addr = (unsigned) fi
(lldb) expr unsigned $len = $next_addr - $addr
(lldb) p/x $addr
(unsigned int) $addr = 0x00000286
(lldb) p/x $next_addr
(unsigned int) $next_addr = 0x000002e4
(lldb) p/x $len
(unsigned int) $len = 0x0000005e

Passing $addr as the address is correctly interpreted with a literal integer as the length:

(lldb) memory read --size 1 --format x --count 0x5e $addr
0x00000286: 0xc8 0x47 0202 0x48 0xc9 0x42 ...

but passing $len as an argument to the count option fails:

(lldb) memory read --size 1 --format x --count $len $addr
error: invalid uint64_t string value: '$len'

This also happens with the breakpoint syntax, so it may be a general limitation with option parsing:

(lldb) breakpoint set -l $len
error: invalid line number: $len.

I also tried passing it through a command alias in the hope that substitution of the variable value would then happen earlier, but had similar results:

(lldb) command alias foop memory read  --size 1 --format x --count %1 %2
(lldb) foop $len $addr
error: invalid uint64_t string value: '$len'

Is there some other syntax for evaluating a command with variables? I would prefer to avoid relying on the Python support as the toolchain I'm using doesn't reliably provide it.

asked on Stack Overflow Oct 11, 2019 by Tom Goodfellow

1 Answer


You can tell whether an option argument will be evaluated or read in directly by looking at the help for the option. For instance, the argument to memory read is given as an <address-expression> and:

(lldb) help address-expression
  <address-expression> -- An expression that resolves to an address.

but count is just of type "count" - which is just an unsigned int.

But... Another bit of lldb syntax is that if any argument or option value is surrounded by `` it is first evaluated as an expression and if the result is a scalar, that value is used for the option.

So you want to say:

memory read --size 1 --format x --count `$len` $addr
answered on Stack Overflow Oct 11, 2019 by Jim Ingham

User contributions licensed under CC BY-SA 3.0