Tests: Add callstack contexts tests
[lttng-tools.git] / tests / utils / testapp / gen-syscall-events-callstack / gen-syscall-events-callstack.c
1 /*
2 * Copyright (C) - 2017 Francis Deslauriers <francis.deslauriers@efficios.com>
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by the
6 * Free Software Foundation; version 2.1 of the License.
7 *
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11 * for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 #include <fcntl.h>
19 #include <signal.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/syscall.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #include "utils.h"
29
30 /**
31 * The process waits for the creation of a file passed as argument from an
32 * external processes to execute a syscall and exiting. This is useful for tests
33 * in combinaison with LTTng's PID tracker feature where we can trace the kernel
34 * events generated by our test process only.
35 */
36
37 volatile int val = 0;
38
39 long __attribute__ ((noinline))
40 my_gettid(void)
41 {
42 long ret;
43 #ifdef __x86_64
44 asm volatile
45 (
46 "syscall"
47 : "=a" (ret)
48 : "0"(__NR_gettid)
49 : "cc", "rcx", "r11", "memory"
50 );
51 #elif __i386
52 asm volatile
53 (
54 "int $0x80"
55 : "=a" (ret)
56 : "0"(__NR_gettid)
57 : "cc", "edi", "esi", "memory"
58 );
59 #else
60 #error "Userspace callstack test not supported for this architecture."
61 #endif
62 return ret;
63 }
64
65 int __attribute__ ((noinline))
66 fct_c(void)
67 {
68 return my_gettid();
69 }
70
71 int __attribute__ ((noinline))
72 fct_b(void)
73 {
74 val += fct_c();
75 return val;
76 }
77
78 int __attribute__ ((noinline))
79 fct_a(void)
80 {
81 val += fct_b();
82 return val;
83 }
84
85 int main(int argc, char **argv)
86 {
87 int ret = 0;
88 char *start_file;
89
90 if (argc != 2) {
91 fprintf(stderr, "Error: Missing argument\n");
92 fprintf(stderr, "USAGE: %s PATH_WAIT_FILE\n", argv[0]);
93 ret = -1;
94 goto error;
95 }
96
97 start_file = argv[1];
98
99 /*
100 * Wait for the start_file to be created by an external process
101 * (typically the test script) before executing the syscall
102 */
103 ret = wait_on_file(start_file);
104 if (ret != 0) {
105 goto error;
106 }
107
108 /* Start the callchain to the syscall */
109 ret = fct_a();
110
111 /* Return success */
112 if (ret >= 0) {
113 ret = 0;
114 }
115
116 error:
117 return ret;
118 }
This page took 0.033927 seconds and 5 git commands to generate.