Given an arbitrary memory address how do I find (in runtime) the nature of memory block it belongs to?
For stack addresses I guess there's some "stack start" and "stack end" of the current thread. For other threads' stacks - I guess I'd have to enumerate all threads to get those ranges. I also found that I can use malloc_size and sometimes it gives me correct result (the size if non zero at least), although it doesn't give me the beginning of the block memory address belongs to. For anything else I have no clue at the moment.
Ideal method I am looking for:
struct MemoryBlock { let type: MemoryBlockType // stack, heap, unmapped, etc let start: UnsafeRawPointer let size: Int let attributes // e.g. red / write }
func findMemoryBlock(_ address: UnsafeRawPointer) -> MemoryBlock
PS. the language doesn't matter (e.g. can be C) so long as this method works in a swift/obj-c app.
For debugging purposes only my go-to tool for this is mach_vm_read
.
Note that this is a relatively slow call, so it’d be best to tweak your dumpByte
abstraction to return a block of bytes.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"