libunwind_01.pass.cpp 1.0 KB

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