Objective-C print out call stack in your own method

Max
1 min readOct 25, 2016

--

Sometimes it’s useful to know which other method calls your method and it’s easy to do so in Objective C.

#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__);
}

You can change the size of the array to print deeper levels.

From http://stackoverflow.com/questions/4046833/print-the-name-of-the-calling-function-to-the-debug-log

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Max
Max

Written by Max

iOS Developer & Bluetooth LE Veteran

No responses yet

Write a response