views:

302

answers:

5

I want to write a debug function or method that will help print useful information. When it is called, I need:

  • the memory address of the calling object (if called by an object)
  • the method signature of the caller (or the name of the method), or the name of the function
  • the class name that owns that method or function

Is it possible to get this information without passing a whole bunch of parameters?

I want to make something like:

debug();

which then goes into every method and function, and helps printing out useful informations about what's going on.

+3  A: 
  1. Set a symbolic breakpoint on the methods you are interested in debugging.
  2. If you need to move back up in the stack (to see where the method call came from) you can either use the Xcode Debugger, or if you want to automated it, use backtrace n to move back up in the stack n number of frames.
kubi
+3  A: 

My first instinct would be to suggest using gdb and breakpoints, since most of that information is available in a stack trace. However, if you really want to see it printed, there are ways to approximate what you're talking about

The preprocessor recognizes the __PRETTY_FUNCTION__ macro for printing the function/method name, and it works well for Objective-C methods. If you print the method name and the value of self at each method of interest, you've pretty much generated a poor man's stack trace.

Try including a #define like this in a header that every file that wants it includes:

#define METHOD() printf("%s\t0x%x\n", __PRETTY_FUNCTION__, (unsigned int)self)

Then just include this line whenever you want to print that information:

METHOD();

The output will look something like this:

-[MyClass initWithFoo:bar:] 0x12345678

As I mentioned, this type of approach is likely to produce huge amounts of output, and gdb is probably a more pragmatic option.

Quinn Taylor
+3  A: 

I use Karl Kraft's DebugLog

Marco Mustapic
+2  A: 

Sorry I don't have a full answer for you, just some relevant info.

NSThread defines a method that gets you an unsymbolicated backtrace, callStackReturnAddresses. In 10.6, it also will give you strings satisfying your second and third requests callStackSymbols.

Getting address of the calling object is interesting, but not totally simple. It's going to involve walking up the stack and picking out where the receiver object is usually stored if there is a usual place it's stored on ARM. For this you (or someone) would need to understand the calling conventions for ARM, which are probably documented in an ARM ABI (application binary interface) somewhere. I don't know ARM. This would be doable on i386, and not really on ppc.

Ken
Of course, ARM is only relevant if he's targeting the iPhone and not the Mac.
Peter Hosey
Huh. I could swear the question mentioned the iPhone before.
Ken
You're right. It originally bore the iphone, uikit, and cocoa-touch tags, before Quinn Taylor replaced them with debugging, program-flow, and methods: http://stackoverflow.com/revisions/1373991/list Sorry for the confusion.
Peter Hosey
+1  A: 

You can use the backtrace(3) API to find out what method or function called you. Getting the calling object, if there was one, is much, much harder, though.

Peter Hosey
i've tried that, but backtrace(3) seems not to be available?
HelloMoon
Did you `#include <execinfo.h>`?
Wevah