Skip to content

Instantly share code, notes, and snippets.

@lambdageek
Created February 24, 2020 20:03
Show Gist options
  • Save lambdageek/f98b0a09eb185af941468aca4d31dd58 to your computer and use it in GitHub Desktop.
Save lambdageek/f98b0a09eb185af941468aca4d31dd58 to your computer and use it in GitHub Desktop.
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#define MONO_NEVER_INLINE __attribute__ ((__noinline__))
#define MONO_API __attribute__((visibility("default")))
MONO_API void call2 (int* x);
static int count;
MONO_API
void call1 (int* x) {
if (++count == 3) {
void* foo[10];
int n = backtrace (&foo[0], 10);
if (n > 0) {
char **syms = backtrace_symbols (&foo[0], n);
for (int i = 0; i < n; ++i) {
printf ("sym[%d] = %s\n", i, syms[i]);
}
free (syms);
} else {
printf ("no symbols");
}
return;
} else
call2 ((int*)&x);
}
clang -o foo -O3 -Wall -Wextra -fvisibility=hidden -g foo.c bar.c
#include <stdio.h>
#define MONO_NEVER_INLINE __attribute__ ((noinline))
#define MONO_API __attribute__((visibility("default")))
MONO_API void call1 (int* x);
static
MONO_NEVER_INLINE
void
static_fn (int *x)
{
call1 ((int*)&x);
}
static int count;
// MONO_NEVER_INLINE
MONO_API
void call2 (int* x)
{
if (++count > 3)
return;
if (++count == 0) {
printf ("a\n");
call2 (x);
printf ("b\n");
} else {
static_fn (x);
static_fn (x);
}
}
int main (void)
{
int x = 0;
call1 (&x);
return 0;
}
static_fn with noinline attribute
./foo
sym[0] = 0 foo 0x000000010db14e65 call1 + 69
sym[1] = 1 foo 0x000000010db14df5 static_fn + 21
sym[2] = 2 foo 0x000000010db14da7 call2 + 55
sym[3] = 3 foo 0x000000010db14ece call1 + 174
sym[4] = 4 foo 0x000000010db14df5 static_fn + 21
sym[5] = 5 foo 0x000000010db14da7 call2 + 55
sym[6] = 6 foo 0x000000010db14ece call1 + 174
sym[7] = 7 foo 0x000000010db14e18 main + 24
sym[8] = 8 libdyld.dylib 0x00007fff689317fd start + 1
sym[9] = 9 ??? 0x0000000000000001 0x0 + 1
static_fn without noinline attribute
./foo
sym[0] = 0 foo 0x000000010eec9e65 call1 + 69
sym[1] = 1 foo 0x000000010eec9dc5 call2 + 53
sym[2] = 2 foo 0x000000010eec9ece call1 + 174
sym[3] = 3 foo 0x000000010eec9dc5 call2 + 53
sym[4] = 4 foo 0x000000010eec9ece call1 + 174
sym[5] = 5 foo 0x000000010eec9e18 main + 24
sym[6] = 6 libdyld.dylib 0x00007fff689317fd start + 1
sym[7] = 7 ??? 0x0000000000000001 0x0 + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment