summaryrefslogtreecommitdiff
path: root/tests/Gx64-test-dwarf-expressions.c
blob: 209f87131248da92c016117980048e8bd5161691 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#include <libunwind.h>

static int verbose;
static int nerrors;

#define panic(args...)							  \
	do { printf (args); ++nerrors; } while (0)

// Assembly routine which sets up the stack for the test then calls another one
// which clobbers the stack, and which in turn calls recover_register below
extern int64_t DW_CFA_expression_testcase(int64_t regnum, int64_t height);

// recover_register is called by the assembly routines. It returns the value of
// a register at a specified height from the inner-most frame. The return value
// is propagated back through the assembly routines to the testcase.
extern int64_t recover_register(int64_t regnum, int64_t height)
{
  // Initialize cursor to current frame
  int rc, i;
  unw_cursor_t cursor;
  unw_context_t context;
  unw_getcontext(&context);
  unw_init_local(&cursor, &context);
  // Unwind frames until required height from inner-most frame (i.e. this one)
  for (i = 0; i < height; ++i)
    {
      rc = unw_step(&cursor);
      if (rc < 0)
        panic("%s: unw_step failed on step %d with return code %d", __FUNCTION__, i, rc);
      else if (rc == 0)
        panic("%s: unw_step failed to reach the end of the stack", __FUNCTION__);
      unw_word_t pc;
      rc = unw_get_reg(&cursor, UNW_REG_IP, &pc);
      if (rc < 0 || pc == 0)
        panic("%s: unw_get_reg failed to locate the program counter", __FUNCTION__);
    }
  // We're now at the required height, extract register
  uint64_t value;
  if ((rc = unw_get_reg(&cursor, (unw_regnum_t) regnum, &value)) != 0)
    panic("%s: unw_get_reg failed to retrieve register %lu", __FUNCTION__, regnum);
  return value;
}

int
main (int argc, char **argv)
{
  if (argc > 1)
    verbose = 1;

  if (DW_CFA_expression_testcase(12, 1) != 0)
    panic("r12 should be clobbered at height 1 (DW_CFA_expression_inner)");
  if (DW_CFA_expression_testcase(12, 2) != 111222333)
    panic("r12 should be restored at height 2 (DW_CFA_expression_testcase)");

  if (nerrors > 0)
    {
      fprintf (stderr, "FAILURE: detected %d errors\n", nerrors);
      exit (-1);
    }

  if (verbose)
    printf ("SUCCESS.\n");
  return 0;
}