1 #include <libunwind.h> 2 #include <stdlib.h> 3 4 void backtrace(int lower_bound) { 5 unw_context_t context; 6 unw_getcontext(&context); 7 8 unw_cursor_t cursor; 9 unw_init_local(&cursor, &context); 10 11 int n = 0; 12 do { 13 ++n; 14 if (n > 100) { 15 abort(); 16 } 17 } while (unw_step(&cursor) > 0); 18 19 if (n < lower_bound) { 20 abort(); 21 } 22 } 23 24 void test1(int i) { 25 backtrace(i); 26 } 27 28 void test2(int i, int j) { 29 backtrace(i); 30 test1(j); 31 } 32 33 void test3(int i, int j, int k) { 34 backtrace(i); 35 test2(j, k); 36 } 37 38 void test_no_info() { 39 unw_context_t context; 40 unw_getcontext(&context); 41 42 unw_cursor_t cursor; 43 unw_init_local(&cursor, &context); 44 45 unw_proc_info_t info; 46 int ret = unw_get_proc_info(&cursor, &info); 47 if (ret != UNW_ESUCCESS) 48 abort(); 49 50 // Set the IP to an address clearly outside any function. 51 unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)0); 52 53 ret = unw_get_proc_info(&cursor, &info); 54 if (ret != UNW_ENOINFO) 55 abort(); 56 } 57 58 int main() { 59 test1(1); 60 test2(1, 2); 61 test3(1, 2, 3); 62 test_no_info(); 63 } 64