summaryrefslogtreecommitdiff
path: root/testcase/userstack.c
blob: 578b2c0e4e8b67de41408e99e966e515d8cbd781 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdio.h>
// #include <libunwind.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>


// void customFunction1(int n);
// void *customFunction1(void *n);
// void customFunction2(int n);
// void customFunction3(int n);

// Call this function to get a backtrace.
// void backtrace() {
//   unw_cursor_t cursor;
//   unw_context_t context;

//   // Initialize cursor to current frame for local unwinding.
//   unw_getcontext(&context);
//   unw_init_local(&cursor, &context);

//   // Unwind frames one by one, going up the frame stack.
//   while (unw_step(&cursor) > 0) {
//     unw_word_t offset, pc;
//     unw_get_reg(&cursor, UNW_REG_IP, &pc);
//     if (pc == 0) {
//       break;
//     }
//     printf("0x%lx:", pc);

//     char sym[256];
//     if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) {
//       printf(" (%s+0x%lx)\n", sym, offset);
//     } else {
//       printf(" -- error: unable to obtain symbol name for this frame\n");
//     }
//   }
// }

pid_t gettid() {
    return syscall(SYS_gettid);
}

static void additionalFunction(int n) {
  printf("Additional function called with n = %d\n", n);
}

static void customFunction3(int n) {
  printf("Calling customFunction3\n");
  additionalFunction(n);
  if (n <= 0) {
    printf("End of recursion\n");
    printf("pid: %d\n", getpid());
    while (1) {
      sleep(1);
    } // never return, keep stack
      // return NULL;
  }
  customFunction2(n - 1);
}

void customFunction2(int n) {
  printf("Calling customFunction2\n");
  additionalFunction(n);
  if (n <= 0) {
    printf("End of recursion\n");
    printf("pid: %d\n", getpid());
    while (1) {
      sleep(1);
    } // never return, keep stack
      // return NULL;
  }
  customFunction3(n - 1);
}

void *customFunction1(void *n) {
  int num = *((int *)n);
  if (num <= 0) {
    printf("End of recursion\n");
    printf("pid: %d\n", getpid());
    while (1) {
      sleep(1);
    } // never return, keep stack
      // return NULL;
  } else {
    printf("Calling customFunction2\n");
    customFunction2(num - 1);
  }
  return NULL;
}

void *customFunction122(void *n) {
  printf("tid: %d\n", gettid());
  while (1)
  {
    /* code */
  }
}

int main() {
  int num1 = 10;
  int num2 = 11;
  int num3 = 12;

  pthread_t thread_id1, thread_id2, thread_id3;

  pthread_create(&thread_id2, NULL, customFunction1, &num2);
  pthread_create(&thread_id3, NULL, customFunction1, &num3);
  pthread_create(&thread_id1, NULL, customFunction122, &num1);

  pthread_join(thread_id1, NULL);
  pthread_join(thread_id2, NULL);
  pthread_join(thread_id3, NULL);
  return 0;
}