summaryrefslogtreecommitdiff
path: root/lldb/test/API/functionalities/unwind/libunwind_ret_injection/main.cpp
blob: 00685e4d6b137aea46081c26199fc6f5382042d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Test program to verify libunwind ret injection feature for execution flow
// rebalancing.
//
// This test creates a multi-frame call stack and throws a C++ exception to
// trigger libunwind's two-phase exception handling. The test verifies that
// libunwind correctly injects the right amount of 'ret' instructions to
// rebalance the execution flow when returning to the landing pad, which is
// important for Apple Processor Trace analysis.

#include <cstdio>
#include <exception>
#include <stdexcept>

// Marker functions with noinline to ensure they appear in the stack.
static void __attribute__((noinline)) func_d() {
  printf("In func_d, about to throw exception\n");
  throw std::runtime_error("test exception");
}

static void __attribute__((noinline)) func_c() {
  printf("In func_c\n");
  func_d();
}

static void __attribute__((noinline)) func_b() {
  printf("In func_b\n");
  func_c();
}

static void __attribute__((noinline)) func_a() {
  printf("In func_a\n");
  func_b();
}

int main(int argc, char *argv[]) {
  try {
    printf("In main, about to call func_a\n");
    func_a();
    printf("ERROR: Should not reach here\n");
    return 1;
  } catch (const std::exception &e) {
    printf("Caught exception in main: %s\n", e.what());
    return 0;
  }
}