How to print the contents of a memory address using LLDB?
I am using LLDB and wondering how to print the contents of a specific memory address, for example 0xb0987654.
Solution 1:
To complement Michael's answer.
I tend to use:
memory read -s1 -fu -c10000 0xb0987654 --force
That will print in the debugger.
- -s for bytes grouping so use 1 for uint8 for example and 4 for int
- -f for format. I inherently forget the right symbol. Just put the statement with -f and it will snap back at you and give you the list of all the options
- -c is for count of bytes
- if you are printing more than 1024 bytes, append with --force
Hope this helps.
Solution 2:
Xcode has a very nice Memory Browser window, which will very nicely display the contents of memory addresses. It also lets you control byte grouping and number of bytes displayed, and move back or forward a memory page:
You can access it by pressing ⌘^⌥⇧M
. After entering it, press enter to open the memory browser in the main editor.
or
Debug --> Debug Workflow --> View Memory
Notice the field on its bottom left corner where you can paste the memory address you want to inspect!
Documentation here: https://developer.apple.com/library/ios/recipes/xcode_help-debugger/articles/viewing_memory.html
Related answer here: How do I open the memory browser in Xcode 4?
Solution 3:
"me
" is the command you're looking for.
For example, this lldb command:
me -r -o /tmp/mem.txt -c512 0xb0987654
will copy 512 bytes from your memory address into a file at /tmp/mem.txt.
Solution 4:
for example, print memory of length 16x4 bytes.
x/16 0xb0987654
Solution 5:
Here's a simple trick for displaying typed arrays of fixed-length in lldb. If your program contains a long* variable that points to 9 elements, you can declare a struct type that contains a fixed array of 9 long values and cast the pointer to that type:
long *values = new long[9]{...};
(lldb) expr typedef struct { long values[9]; } l9; *(l9 *)values
(l9) $1 = {
values = {
[0] = 0
[1] = 1
[2] = 4
[3] = 9
[4] = 16
[5] = 25
[6] = 36
[7] = 49
[8] = 64
}
}
I use the typedef when I'm coding in C, it's not needed in C++.