开发者

Print the name of the calling function to the debug log

Objective-C's runtime seems to be rather robust, so I was wondering if there's a way to log the name of the function that called the current function (for debugging purposes).

My situation is that a bunch of things assign to a property, and rather than set a breakpoint and examine the call stack each time, I'd like to just NSLog the name of the func开发者_开发百科tion that is setting the property, along with the new value.

So is it possible to get access to the call stack at runtime?


Try this:

#include <execinfo.h>

void *addr[2];
int nframes = backtrace(addr, sizeof(addr)/sizeof(*addr));
if (nframes > 1) {
    char **syms = backtrace_symbols(addr, nframes);
    NSLog(@"%s: caller: %s", __func__, syms[1]);
    free(syms);
} else {
    NSLog(@"%s: *** Failed to generate backtrace.", __func__);
}


Great Question. Combining Jeremy's Answer above and what we always use for our debugging, you'll get a nice string like this:

NSArray *syms = [NSThread  callStackSymbols]; 
if ([syms count] > 1) { 
    NSLog(@"<%@ %p> %@ - caller: %@ ", [self class], self, NSStringFromSelector(_cmd),[syms objectAtIndex:1]);
} else {
     NSLog(@"<%@ %p> %@", [self class], self, NSStringFromSelector(_cmd)); 
}


There is no facility for getting the sender. Or, at least, nothing centric to Objective-C.

There are a couple of alternatives, though.

First, you could use GDB commands. Go to the GDB console and do something like:

comm 1
bt
po argName
cont

Alternatively, you could use dtrace. This is hard. Or you could use Instruments which makes dtrace somewhat easier.

Or you could use the backtrace() function. See the backtrace man page (x-man-page://backtrace).


There is a C macro called __PRETTY_FUNCTION__ that will return a C-String with the name of the current function. If you would like to convert that to an NSString for easily printing to NSLog, you can create a macro with

#define NSSTRING_PRETTY_FUNCTION [NSString stringWithCString:__PRETTY_FUNCTION__ encoding:NSASCIIStringEncoding]

I use it all the time in my projects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜