1#include <stdio.h>
2#include <libunwind.h>
3#include <dlfcn.h>
4
5extern void unw_iterate_dwarf_unwind_cache(void (*func)(unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh));
6
7
8
9typedef void (*FooProc)();
10static FooProc foo;
11
12static bool fooInCache = false;
13static void callback(unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)
14{
15	//fprintf(stderr, "ip_start = 0x%llX, ip_end = 0x%llX\n", ip_start, ip_end);
16	if ( (unw_word_t)(long)foo == ip_start )
17		fooInCache = true;
18}
19
20
21int main()
22{
23	void* handle = dlopen("libfoo.dylib", RTLD_FIRST);
24	if ( handle == NULL ) {
25		printf("FAIL: dlopen could not load libfoo.dylib\n");
26		return 1;
27	}
28
29	foo = (FooProc)dlsym(handle, "foo");
30	if ( foo == NULL ) {
31		printf("FAIL: dlsym could not find foo\n");
32		return 1;
33	}
34
35	//fprintf(stderr, "foo=%p\n", foo);
36	(*foo)();
37
38	// verify foo is in cache
39	fooInCache = false;
40	unw_iterate_dwarf_unwind_cache(&callback);
41	if ( !fooInCache ) {
42		printf("FAIL: foo is not in cache\n");
43		return 1;
44	}
45
46	dlclose(handle);
47	//fprintf(stderr, "dlclose\n");
48
49	// verify foo is no longer in cache
50	fooInCache = false;
51	unw_iterate_dwarf_unwind_cache(&callback);
52	if ( fooInCache ){
53		printf("FAIL: foo is still in cache\n");
54		return 1;
55	}
56
57	return 0;
58}
59