| 1 | /* |
| 2 | * Copyright (C) 2017 Francis Deslauriers <francis.deslauriers@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 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 | |
| 27 | #if defined(__clang__) |
| 28 | #define nooptimization __attribute__((noinline)) __attribute__((optnone)) |
| 29 | #else |
| 30 | #define nooptimization __attribute__((noinline)) __attribute__((optimize(0))) |
| 31 | #endif |
| 32 | |
| 33 | volatile int val = 0; |
| 34 | |
| 35 | long nooptimization my_gettid(void); |
| 36 | long nooptimization |
| 37 | my_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 | |
| 62 | int nooptimization fct_c(void); |
| 63 | int nooptimization |
| 64 | fct_c(void) |
| 65 | { |
| 66 | return my_gettid(); |
| 67 | } |
| 68 | |
| 69 | int nooptimization fct_b(void); |
| 70 | int nooptimization |
| 71 | fct_b(void) |
| 72 | { |
| 73 | val += fct_c(); |
| 74 | return val; |
| 75 | } |
| 76 | |
| 77 | int nooptimization fct_a(void); |
| 78 | int nooptimization |
| 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 | } |