1 #include <assert.h> 2 #include <stdlib.h> 3 #include <unwind.h> 4 5 #define EXPECTED_NUM_FRAMES 50 6 #define NUM_FRAMES_UPPER_BOUND 100 7 callback(_Unwind_Context * context,void * cnt)8_Unwind_Reason_Code callback(_Unwind_Context *context, void *cnt) { 9 (void)context; 10 int *i = (int *)cnt; 11 ++*i; 12 if (*i > NUM_FRAMES_UPPER_BOUND) { 13 abort(); 14 } 15 return _URC_NO_REASON; 16 } 17 test_backtrace()18void test_backtrace() { 19 int n = 0; 20 _Unwind_Backtrace(&callback, &n); 21 if (n < EXPECTED_NUM_FRAMES) { 22 abort(); 23 } 24 } 25 test(int i)26int test(int i) { 27 if (i == 0) { 28 test_backtrace(); 29 return 0; 30 } else { 31 return i + test(i - 1); 32 } 33 } 34 main(int,char **)35int main(int, char**) { 36 int total = test(50); 37 assert(total == 1275); 38 return 0; 39 } 40