libunwind_02.pass.cpp 655 B

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