tests: Move to kernel style SPDX license identifiers
[lttng-tools.git] / tests / utils / testapp / gen-syscall-events-callstack / gen-syscall-events-callstack.c
CommitLineData
591ee332 1/*
9d16b343 2 * Copyright (C) 2017 Francis Deslauriers <francis.deslauriers@efficios.com>
591ee332 3 *
9d16b343 4 * SPDX-License-Identifier: LGPL-2.1-only
591ee332 5 *
591ee332
FD
6 */
7
8#include <fcntl.h>
9#include <signal.h>
10#include <stdbool.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sys/syscall.h>
15#include <sys/types.h>
16#include <unistd.h>
17
18#include "utils.h"
19
20/**
21 * The process waits for the creation of a file passed as argument from an
22 * external processes to execute a syscall and exiting. This is useful for tests
23 * in combinaison with LTTng's PID tracker feature where we can trace the kernel
24 * events generated by our test process only.
25 */
26
51c2fb6c
FD
27#if defined(__clang__)
28#define nooptimization __attribute__((noinline)) __attribute__((optnone))
29#else
30#define nooptimization __attribute__((noinline)) __attribute__((optimize(0)))
31#endif
32
591ee332
FD
33volatile int val = 0;
34
701a99d1 35long nooptimization my_gettid(void);
51c2fb6c 36long nooptimization
591ee332
FD
37my_gettid(void)
38{
39 long ret;
40#ifdef __x86_64
41 asm volatile
42 (
43 "syscall"
44 : "=a" (ret)
45 : "0"(__NR_gettid)
46 : "cc", "rcx", "r11", "memory"
47 );
48#elif __i386
49 asm volatile
50 (
51 "int $0x80"
52 : "=a" (ret)
53 : "0"(__NR_gettid)
54 : "cc", "edi", "esi", "memory"
55 );
56#else
57#error "Userspace callstack test not supported for this architecture."
58#endif
59 return ret;
60}
61
701a99d1 62int nooptimization fct_c(void);
51c2fb6c 63int nooptimization
591ee332
FD
64fct_c(void)
65{
66 return my_gettid();
67}
68
701a99d1 69int nooptimization fct_b(void);
51c2fb6c 70int nooptimization
591ee332
FD
71fct_b(void)
72{
73 val += fct_c();
74 return val;
75}
76
701a99d1 77int nooptimization fct_a(void);
51c2fb6c 78int nooptimization
591ee332
FD
79fct_a(void)
80{
81 val += fct_b();
82 return val;
83}
84
85int 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
116error:
117 return ret;
118}
This page took 0.036422 seconds and 5 git commands to generate.