115 lines · cpp
1// Check that when forking in basic logging mode, we get the different tids for child and parent2// RUN: %clangxx_xray -g -std=c++11 %s -o %t3// RUN: rm -f fork-basic-logging-test-*4// RUN: env 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 %s7// RUN: ls -S fork-basic-logging-test-* | head -1 | tr -d '\n' > %t.log8// RUN: %llvm_xray convert --symbolize --output-format=yaml -instr_map=%t \9// RUN: "%{readfile:%t.log}" \10// RUN: | FileCheck %s --check-prefix=TRACE11 12// REQUIRES: x86_64-target-arch13// REQUIRES: built-in-llvm-tree14 15// Not ported.16// UNSUPPORTED: target={{.*netbsd.*}}17 18#include "xray/xray_log_interface.h"19#include <stdio.h>20#include <unistd.h>21#include <stdint.h>22#if defined(__linux__)23#include <sys/syscall.h>24#elif defined(__FreeBSD__)25#include <sys/thr.h>26#endif27 28//modified from sanitizer29 30static uintptr_t syscall_gettid() {31 uint64_t retval;32#ifdef __linux__33 asm volatile("syscall" : "=a"(retval) : "a"(__NR_gettid) : "rcx", "r11",34 "memory", "cc");35#else36 long t;37 thr_self(&t);38 retval = static_cast<uint64_t>(t);39#endif40 return retval;41}42 43/////////////44 45static uint64_t parent_tid;46 47[[clang::xray_always_instrument]]48uint64_t __attribute__((noinline)) log_syscall_gettid()49{50 //don't optimize this function away51 uint64_t tid = syscall_gettid();52 printf("Logging tid %lu\n", tid);53 return tid;54}55 56[[clang::xray_always_instrument, clang::xray_log_args(1)]]57void __attribute__((noinline)) print_parent_tid(uint64_t tid)58{59 printf("Parent with tid %lu", tid);60}61 62[[clang::xray_always_instrument, clang::xray_log_args(1)]]63void __attribute__((noinline)) print_child_tid(uint64_t tid)64{65 printf("Child with tid %lu", tid);66}67 68[[clang::xray_always_instrument]] void __attribute__((noinline)) print_parent_or_child()69{70 uint64_t tid = syscall_gettid();71 if(tid == parent_tid)72 {73 print_parent_tid(tid);74 }75 else76 {77 print_child_tid(tid);78 }79}80 81int main()82{83 parent_tid = log_syscall_gettid();84 if(fork())85 {86 print_parent_or_child();87 // CHECK-DAG: Parent with tid88 }89 else90 {91 print_parent_or_child();92 // CHECK-DAG: Child with tid93 }94 return 0;95}96 97// Make sure we know which thread is the parent process98// 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: '' }99 100// 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: '' }101//102// The parent will print its pid103// 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: '' }104// TRACE-DAG: - { type: 0, func-id: [[PPTARG]], function: {{.*print_parent_tid.*}}, cpu: {{.*}}, thread: [[THREAD1]], process: [[PROCESS1]], kind: function-exit, tsc: {{[0-9]+}}, data: '' }105//106// 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: '' }107 108// 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: '' }109//110// The child will print its pid111// 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: '' }112// TRACE-DAG: - { type: 0, func-id: [[PCTARG]], function: {{.*print_child_tid.*}}, cpu: {{.*}}, thread: [[THREAD2]], process: [[PROCESS2]], kind: function-exit, tsc: {{[0-9]+}}, data: '' }113//114// 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: '' }115