Is there any way to debug macros?

I got a legacy application with a bunch of global singletons defined as macroses in precompiled headers:

#define myGlobalState [MyGlobalState getInstance]
#define settingsManager [SettingsManager getInstance]

Is there any way I could access these singletons from inside a debugger repl?

Upvotes: 1

Views: 224

Answers (1)

bbum
bbum

Reputation: 162712

Is there any way I could access these singletons from inside a debugger repl?

So, first, they aren't singletons. They are expressions that get evaluated every time the macro is used. Unfortunate, that.

Secondly, prefixed with get? That's a sure sign that the codebase was created by someone not familiar with Objective-C and an indication that there'll likely be other significant problems with the design.

Finally, the easiest solution -- but it changes behavior -- is to create actual global variables called myGlobalState and settingsManager, set them in some initialization phase of the app or library, and get rid of the #define altogether. That would make them available in the debugger and would likely render a behavior a lot closer to what the original author meant.

Alternatively (and, arguably, a better solution), would be to rename the get methods to sharedInstance (as they should be), then find and replace all instances of myGlobalState with [MyGlobalState sharedInstance] and do away with the macro entirely.

For the former, you can use the global directly in the debugger. For the latter, you'd have to evaluate [MyGlobalState sharedInstance] in the debugger, but that isn't a big deal.

(And, yeah, you could just evaluate [MyGlobalState getGlobalState] in the debugger, leave all the mis-named stuff alone and call it a day.)

Upvotes: 2

Related Questions