I am using LLDB and wondering how to print the contents of a specific memory address, for example 0xb0987654.
5 Answers
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.
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?
-
Best shortcut ever: sometimes they're combinations of 2-3 special keys (⌘^⇧, ⌘⌥⇧, ^⌥⇧ or whatever). Apple is out of shortcuts and they went all in.– DogNov 21, 2019 at 8:35
-
1@drewster it does, but because of Xcode's new UX you have to decide where to open the memory browser (so just press enter after entering the shortcut)– EricApr 6, 2020 at 11:07
-
1
"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.
-
1I'm using lldb on Apple macOS 11.3 version
lldb-1200.0.44.2
and I had to alter the command slightly:me read -o /tmp/mem.txt -c512 0xb0987654
Sep 29, 2021 at 15:29
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++.
print *(int*)0xb0987654
?gdb
, perhaps by compilng it from source).