| 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 | #if defined(__clang__) |
| 38 | #define nooptimization __attribute__((noinline)) __attribute__((optnone)) |
| 39 | #else |
| 40 | #define nooptimization __attribute__((noinline)) __attribute__((optimize(0))) |
| 41 | #endif |
| 42 | |
| 43 | volatile int val = 0; |
| 44 | |
| 45 | long nooptimization |
| 46 | my_gettid(void) |
| 47 | { |
| 48 | long ret; |
| 49 | #ifdef __x86_64 |
| 50 | asm volatile |
| 51 | ( |
| 52 | "syscall" |
| 53 | : "=a" (ret) |
| 54 | : "0"(__NR_gettid) |
| 55 | : "cc", "rcx", "r11", "memory" |
| 56 | ); |
| 57 | #elif __i386 |
| 58 | asm volatile |
| 59 | ( |
| 60 | "int $0x80" |
| 61 | : "=a" (ret) |
| 62 | : "0"(__NR_gettid) |
| 63 | : "cc", "edi", "esi", "memory" |
| 64 | ); |
| 65 | #else |
| 66 | #error "Userspace callstack test not supported for this architecture." |
| 67 | #endif |
| 68 | return ret; |
| 69 | } |
| 70 | |
| 71 | int nooptimization |
| 72 | fct_c(void) |
| 73 | { |
| 74 | return my_gettid(); |
| 75 | } |
| 76 | |
| 77 | int nooptimization |
| 78 | fct_b(void) |
| 79 | { |
| 80 | val += fct_c(); |
| 81 | return val; |
| 82 | } |
| 83 | |
| 84 | int nooptimization |
| 85 | fct_a(void) |
| 86 | { |
| 87 | val += fct_b(); |
| 88 | return val; |
| 89 | } |
| 90 | |
| 91 | int main(int argc, char **argv) |
| 92 | { |
| 93 | int ret = 0; |
| 94 | char *start_file; |
| 95 | |
| 96 | if (argc != 2) { |
| 97 | fprintf(stderr, "Error: Missing argument\n"); |
| 98 | fprintf(stderr, "USAGE: %s PATH_WAIT_FILE\n", argv[0]); |
| 99 | ret = -1; |
| 100 | goto error; |
| 101 | } |
| 102 | |
| 103 | start_file = argv[1]; |
| 104 | |
| 105 | /* |
| 106 | * Wait for the start_file to be created by an external process |
| 107 | * (typically the test script) before executing the syscall |
| 108 | */ |
| 109 | ret = wait_on_file(start_file); |
| 110 | if (ret != 0) { |
| 111 | goto error; |
| 112 | } |
| 113 | |
| 114 | /* Start the callchain to the syscall */ |
| 115 | ret = fct_a(); |
| 116 | |
| 117 | /* Return success */ |
| 118 | if (ret >= 0) { |
| 119 | ret = 0; |
| 120 | } |
| 121 | |
| 122 | error: |
| 123 | return ret; |
| 124 | } |