docs: Add supported versions and fix-backport policy
[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
28f23191
JG
8#include "utils.h"
9
591ee332
FD
10#include <fcntl.h>
11#include <signal.h>
12#include <stdbool.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <sys/syscall.h>
17#include <sys/types.h>
18#include <unistd.h>
19
591ee332
FD
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);
28f23191 36long nooptimization my_gettid(void)
591ee332 37{
28f23191 38 long ret;
591ee332 39#ifdef __x86_64
28f23191 40 asm volatile("syscall" : "=a"(ret) : "0"(__NR_gettid) : "cc", "rcx", "r11", "memory");
f3af9f34 41#elif defined(__i386)
28f23191 42 asm volatile("int $0x80" : "=a"(ret) : "0"(__NR_gettid) : "cc", "edi", "esi", "memory");
591ee332
FD
43#else
44#error "Userspace callstack test not supported for this architecture."
45#endif
28f23191 46 return ret;
591ee332
FD
47}
48
701a99d1 49int nooptimization fct_c(void);
28f23191 50int nooptimization fct_c(void)
591ee332
FD
51{
52 return my_gettid();
53}
54
701a99d1 55int nooptimization fct_b(void);
28f23191 56int nooptimization fct_b(void)
591ee332
FD
57{
58 val += fct_c();
59 return val;
60}
61
701a99d1 62int nooptimization fct_a(void);
28f23191 63int nooptimization fct_a(void)
591ee332
FD
64{
65 val += fct_b();
66 return val;
67}
68
69int main(int argc, char **argv)
70{
71 int ret = 0;
72 char *start_file;
73
74 if (argc != 2) {
75 fprintf(stderr, "Error: Missing argument\n");
76 fprintf(stderr, "USAGE: %s PATH_WAIT_FILE\n", argv[0]);
77 ret = -1;
78 goto error;
79 }
80
81 start_file = argv[1];
82
83 /*
84 * Wait for the start_file to be created by an external process
85 * (typically the test script) before executing the syscall
86 */
87 ret = wait_on_file(start_file);
88 if (ret != 0) {
89 goto error;
90 }
91
92 /* Start the callchain to the syscall */
93 ret = fct_a();
94
95 /* Return success */
96 if (ret >= 0) {
97 ret = 0;
98 }
99
100error:
101 return ret;
102}
This page took 0.051937 seconds and 4 git commands to generate.