How to find out who is the calling method or function?

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

  • memory address of the calling object (if called by the object)
  • caller method signature (or method name) or function name
  • name of the class to which this method or function belongs

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

I want to do something like:

debug();

which then goes into each method and function and helps print out useful information about what is happening.

+3
source share
5 answers

gdb , . , , , ,

__PRETTY_FUNCTION__ / Objective-C. self , .

#define, , , :

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

, :

METHOD();

:

-[MyClass initWithFoo:bar:] 0x12345678

, , , , gdb, , .

+3
+2

Karl Kraft DebugLog

+2

, , - .

NSThread , unsymbolicated backtrace, callStackReturnAddresses. 10.6 , callStackSymbols.

, . , - , , ARM. ( -) ARM, , , - ARM ABI ( ). ARM. i386, ppc.

+1

You can use the backtrace (3) API to find out which method or function you called. Getting the caller, if any, is much, much more complicated.

0
source

Source: https://habr.com/ru/post/1716796/


All Articles