Nate Birkholz
Nate Birkholz

Reputation: 2809

iOS Objective-C XCode: Logging Breakpoints and "magic" variables

I am trying to use breakpoints for logging instead of NSLog, but I am having trouble accessing the "magic" compiler variables like __FUNCTION__, __FILE__, etc.

Neither Debugger Command nor Log Message breakpoints seem to evaluate the variables in the same way as NSLog.

NSLog(@"%s", __FILE__) results in correct debug output of /Users/nbirkholz/Documents/project_name/folder_name/file_name.m

When I set a Debugger Command breakpoint of po __FILE__ I receive debug output of "Parse" { 'P' 'a' 'r' 's' 'e' <nil>}

When I use p __FILE__ I receive debug output of (const char [6]) $1 = "Parse"

Similar results ensue from p/po __func__/__PRETTY_FUNCTION__/__FUNCTION__/__LINE__

po [NSString stringWithFormat:@"file is: %s", __FILE__] results in error: too many arguments to method call, expected 1, have 2

po (void)NSLog(@"the file is: %s", __FILE__) returns <timestamp> <module> the file is: Parse

expr/expression (void)NSLog(@"the file is: %s", __FILE__) gives the same results.

Similarly, adding a Log Message breakpoint either fails to evaluate the expression at all or produces similar results, I can't seem to find an expression syntax that works encased in @ @

For example a Log Message of the file is @__FILE__@ begets the file is "Parse"

Is there a way to get this to work without adding an NSLog() to the code directly and get it to properly evaluate the variable?

Upvotes: 0

Views: 199

Answers (2)

CuriousRabbit
CuriousRabbit

Reputation: 2211

These "Magic variables" are actually macros whose value is derived during compilation at the point they are referenced. They will not have the same value at debug time as they have at compile time. In fact, at debug time, you are most likely getting the variable's value as compiled into the debugger. I.E., not the file name of that code from MySpecialProgram, but the file name of the code executing in the debugger, likely the command parser.

Either put the logging in the code, as in:

NSLog(@"this is %s line %d function %s", __FILE__, __LINE__, __FUNCTION__);

or assign the vars to something you can access:

char[] foo = __FILE__;

Upvotes: 2

Rengers
Rengers

Reputation: 15218

I'm not sure here but afaik these are preprocessor macros, I think you can't get these to work in runtime.

Upvotes: 1

Related Questions