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 int *i = (int *)cnt; 10 ++*i; 11 if (*i > NUM_FRAMES_UPPER_BOUND) { 12 abort(); 13 } 14 return _URC_NO_REASON; 15 } 16 test_backtrace()17void test_backtrace() { 18 int n = 0; 19 _Unwind_Backtrace(&callback, &n); 20 if (n < EXPECTED_NUM_FRAMES) { 21 abort(); 22 } 23 } 24 test(int i)25int test(int i) { 26 if (i == 0) { 27 test_backtrace(); 28 return 0; 29 } else { 30 return i + test(i - 1); 31 } 32 } 33 main()34int main() { 35 int total = test(50); 36 assert(total == 1275); 37 } 38