1 // Check that when forking in basic logging mode, we get the different tids for child and parent 2 // RUN: %clangxx_xray -g -std=c++11 %s -o %t 3 // RUN: rm -f fork-basic-logging-test-* 4 // RUN: XRAY_OPTIONS="patch_premain=true xray_logfile_base=fork-basic-logging-test- \ 5 // RUN: xray_mode=xray-basic verbosity=1 xray_naive_log_func_duration_threshold_us=0" \ 6 // RUN: %run %t 2>&1 | FileCheck %s 7 // RUN: %llvm_xray convert --symbolize --output-format=yaml -instr_map=%t \ 8 // RUN: "`ls -S fork-basic-logging-test-* | head -1`" \ 9 // RUN: | FileCheck %s --check-prefix=TRACE 10 11 // REQUIRES: x86_64-target-arch 12 // REQUIRES: built-in-llvm-tree 13 14 // Not ported. 15 // UNSUPPORTED: netbsd 16 17 #include "xray/xray_log_interface.h" 18 #include <stdio.h> 19 #include <unistd.h> 20 #include <stdint.h> 21 #include <sys/syscall.h> 22 23 //modified from sanitizer 24 25 static uintptr_t syscall_gettid() { 26 uint64_t retval; 27 asm volatile("syscall" : "=a"(retval) : "a"(__NR_gettid) : "rcx", "r11", 28 "memory", "cc"); 29 return retval; 30 } 31 32 ///////////// 33 34 static uint64_t parent_tid; 35 36 [[clang::xray_always_instrument]] 37 uint64_t __attribute__((noinline)) log_syscall_gettid() 38 { 39 //don't optimize this function away 40 uint64_t tid = syscall_gettid(); 41 printf("Logging tid %lu\n", tid); 42 return tid; 43 } 44 45 [[clang::xray_always_instrument, clang::xray_log_args(1)]] 46 void __attribute__((noinline)) print_parent_tid(uint64_t tid) 47 { 48 printf("Parent with tid %lu", tid); 49 } 50 51 [[clang::xray_always_instrument, clang::xray_log_args(1)]] 52 void __attribute__((noinline)) print_child_tid(uint64_t tid) 53 { 54 printf("Child with tid %lu", tid); 55 } 56 57 [[clang::xray_always_instrument]] void __attribute__((noinline)) print_parent_or_child() 58 { 59 uint64_t tid = syscall_gettid(); 60 if(tid == parent_tid) 61 { 62 print_parent_tid(tid); 63 } 64 else 65 { 66 print_child_tid(tid); 67 } 68 } 69 70 int main() 71 { 72 parent_tid = log_syscall_gettid(); 73 if(fork()) 74 { 75 print_parent_or_child(); 76 // CHECK-DAG: Parent with tid 77 } 78 else 79 { 80 print_parent_or_child(); 81 // CHECK-DAG: Child with tid 82 } 83 return 0; 84 } 85 86 // Make sure we know which thread is the parent process 87 // TRACE-DAG: - { type: 0, func-id: [[LSGT:[0-9]+]], function: {{.*log_syscall_gettid.*}}, cpu: {{.*}}, thread: [[THREAD1:[0-9]+]], process: [[PROCESS1:[0-9]+]], kind: function-enter, tsc: {{[0-9]+}}, data: '' } 88 89 // TRACE-DAG: - { type: 0, func-id: [[PPOC:[0-9]+]], function: {{.*print_parent_or_child.*}}, cpu: {{.*}}, thread: [[THREAD1]], process: [[PROCESS1]], kind: function-enter, tsc: {{[0-9]+}}, data: '' } 90 // 91 // The parent will print its pid 92 // TRACE-DAG: - { type: 0, func-id: [[PPTARG:[0-9]+]], function: {{.*print_parent_tid.*}}, args: [ [[THREAD1]] ], cpu: {{.*}}, thread: [[THREAD1]], process: [[PROCESS1]], kind: function-enter-arg, tsc: {{[0-9]+}}, data: '' } 93 // TRACE-DAG: - { type: 0, func-id: [[PPTARG]], function: {{.*print_parent_tid.*}}, cpu: {{.*}}, thread: [[THREAD1]], process: [[PROCESS1]], kind: function-exit, tsc: {{[0-9]+}}, data: '' } 94 // 95 // TRACE-DAG - { type: 0, func-id: [[PPOC]], function: {{.*print_parent_or_child.*}}, cpu: {{.*}}, thread: [[THREAD1]], process: [[PROCESS1]], kind: function-{{exit|tail-exit}}, tsc: {{[0-9]+}}, data: '' } 96 97 // TRACE-DAG: - { type: 0, func-id: [[PPOC]], function: {{.*print_parent_or_child.*}}, cpu: {{.*}}, thread: [[THREAD2:[0-9]+]], process: [[PROCESS2:[0-9]+]], kind: function-enter, tsc: {{[0-9]+}}, data: '' } 98 // 99 // The child will print its pid 100 // TRACE-DAG: - { type: 0, func-id: [[PCTARG:[0-9]+]], function: {{.*print_child_tid.*}}, args: [ [[THREAD2]] ], cpu: {{.*}}, thread: [[THREAD2]], process: [[PROCESS2]], kind: function-enter-arg, tsc: {{[0-9]+}}, data: '' } 101 // TRACE-DAG: - { type: 0, func-id: [[PCTARG]], function: {{.*print_child_tid.*}}, cpu: {{.*}}, thread: [[THREAD2]], process: [[PROCESS2]], kind: function-exit, tsc: {{[0-9]+}}, data: '' } 102 // 103 // TRACE-DAG: - { type: 0, func-id: [[PPOC]], function: {{.*print_parent_or_child.*}}, cpu: {{.*}}, thread: [[THREAD2]], process: [[PROCESS2]], kind: function-{{exit|tail-exit}}, tsc: {{[0-9]+}}, data: '' } 104