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 #if defined(__linux__)
22 #include <sys/syscall.h>
23 #elif defined(__FreeBSD__)
24 #include <sys/thr.h>
25 #endif
26
27 //modified from sanitizer
28
syscall_gettid()29 static uintptr_t syscall_gettid() {
30 uint64_t retval;
31 #ifdef __linux__
32 asm volatile("syscall" : "=a"(retval) : "a"(__NR_gettid) : "rcx", "r11",
33 "memory", "cc");
34 #else
35 long t;
36 thr_self(&t);
37 retval = static_cast<uint64_t>(t);
38 #endif
39 return retval;
40 }
41
42 /////////////
43
44 static uint64_t parent_tid;
45
46 [[clang::xray_always_instrument]]
log_syscall_gettid()47 uint64_t __attribute__((noinline)) log_syscall_gettid()
48 {
49 //don't optimize this function away
50 uint64_t tid = syscall_gettid();
51 printf("Logging tid %lu\n", tid);
52 return tid;
53 }
54
55 [[clang::xray_always_instrument, clang::xray_log_args(1)]]
print_parent_tid(uint64_t tid)56 void __attribute__((noinline)) print_parent_tid(uint64_t tid)
57 {
58 printf("Parent with tid %lu", tid);
59 }
60
61 [[clang::xray_always_instrument, clang::xray_log_args(1)]]
print_child_tid(uint64_t tid)62 void __attribute__((noinline)) print_child_tid(uint64_t tid)
63 {
64 printf("Child with tid %lu", tid);
65 }
66
print_parent_or_child()67 [[clang::xray_always_instrument]] void __attribute__((noinline)) print_parent_or_child()
68 {
69 uint64_t tid = syscall_gettid();
70 if(tid == parent_tid)
71 {
72 print_parent_tid(tid);
73 }
74 else
75 {
76 print_child_tid(tid);
77 }
78 }
79
main()80 int main()
81 {
82 parent_tid = log_syscall_gettid();
83 if(fork())
84 {
85 print_parent_or_child();
86 // CHECK-DAG: Parent with tid
87 }
88 else
89 {
90 print_parent_or_child();
91 // CHECK-DAG: Child with tid
92 }
93 return 0;
94 }
95
96 // Make sure we know which thread is the parent process
97 // 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: '' }
98
99 // 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: '' }
100 //
101 // The parent will print its pid
102 // 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: '' }
103 // TRACE-DAG: - { type: 0, func-id: [[PPTARG]], function: {{.*print_parent_tid.*}}, cpu: {{.*}}, thread: [[THREAD1]], process: [[PROCESS1]], kind: function-exit, tsc: {{[0-9]+}}, data: '' }
104 //
105 // 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: '' }
106
107 // 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: '' }
108 //
109 // The child will print its pid
110 // 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: '' }
111 // TRACE-DAG: - { type: 0, func-id: [[PCTARG]], function: {{.*print_child_tid.*}}, cpu: {{.*}}, thread: [[THREAD2]], process: [[PROCESS2]], kind: function-exit, tsc: {{[0-9]+}}, data: '' }
112 //
113 // 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: '' }
114