Dynamically accessing local variables in Objective-C runtime

前端 未结 1 912
一个人的身影
一个人的身影 2021-01-23 03:45

When attached to the debugger via Xcode, LLDB provides a useful view of local variables (the bottom left of the screenshot):

相关标签:
1条回答
  • 2021-01-23 04:24

    Xcode/LLDB can show you this information because they have access to debug information in the binary, called a symbol table, which help it understand what memory locations correspond to which names in your source code. This is all outside the Objective-C runtime, and there's no interface in the runtime to get at it.

    There's another reason why this won't work, though. When you're building code to run in the debugger, compiler optimizations are turned off, so all the variables you reference in your code are there.

    When you build for release, though, generally the compiler optimizations get in there and re-arrange all your carefully named local variables to make things run faster. They might not even ever get stored in memory, just in CPU registers. Or they might not exist at all, if the optimizer can prove to itself that it doesn't need them.

    My advice is to think again about the larger problem you're trying to solve...

    0 讨论(0)
提交回复
热议问题