xref: /llvm-project/compiler-rt/test/tsan/debugging.cpp (revision e3230295faad27c9f6f2de0d9471c25111a7dd67)
1bcaeed49SFangrui Song // RUN: %clangxx_tsan -O1 %s -o %t
2bcaeed49SFangrui Song // RUN: %deflake %run %t 2>&1 | FileCheck %s
3bcaeed49SFangrui Song 
4bcaeed49SFangrui Song #include <pthread.h>
5bcaeed49SFangrui Song #include <stdint.h>
6bcaeed49SFangrui Song #include <stdio.h>
7bcaeed49SFangrui Song #include <stdlib.h>
8bcaeed49SFangrui Song #include <string.h>
9bcaeed49SFangrui Song 
10bcaeed49SFangrui Song #include "test.h"
11bcaeed49SFangrui Song 
12*e3230295SHans Wennborg extern "C" {
13*e3230295SHans Wennborg void __tsan_on_report(void *report);
14*e3230295SHans Wennborg void *__tsan_get_current_report();
15*e3230295SHans Wennborg int __tsan_get_report_data(void *report, const char **description, int *count,
16*e3230295SHans Wennborg                            int *stack_count, int *mop_count, int *loc_count,
17*e3230295SHans Wennborg                            int *mutex_count, int *thread_count,
18*e3230295SHans Wennborg                            int *unique_tid_count, void **sleep_trace,
19*e3230295SHans Wennborg                            unsigned long trace_size);
20*e3230295SHans Wennborg int __tsan_get_report_mop(void *report, unsigned long idx, int *tid,
21*e3230295SHans Wennborg                           void **addr, int *size, int *write, int *atomic,
22*e3230295SHans Wennborg                           void **trace, unsigned long trace_size);
23*e3230295SHans Wennborg int __tsan_get_report_thread(void *report, unsigned long idx, int *tid,
24*e3230295SHans Wennborg                              uint64_t *os_id, int *running,
25*e3230295SHans Wennborg                              const char **name, int *parent_tid, void **trace,
26*e3230295SHans Wennborg                              unsigned long trace_size);
27*e3230295SHans Wennborg }
28*e3230295SHans Wennborg 
29bcaeed49SFangrui Song long my_global;
30bcaeed49SFangrui Song 
Thread(void * a)31bcaeed49SFangrui Song void *Thread(void *a) {
32bcaeed49SFangrui Song   barrier_wait(&barrier);
33bcaeed49SFangrui Song   my_global = 42;
34bcaeed49SFangrui Song   return NULL;
35bcaeed49SFangrui Song }
36bcaeed49SFangrui Song 
main()37bcaeed49SFangrui Song int main() {
38bcaeed49SFangrui Song   barrier_init(&barrier, 2);
39bcaeed49SFangrui Song   fprintf(stderr, "&my_global = %p\n", &my_global);
40bcaeed49SFangrui Song   // CHECK: &my_global = [[GLOBAL:0x[0-9a-f]+]]
41bcaeed49SFangrui Song   pthread_t t;
42bcaeed49SFangrui Song   pthread_create(&t, 0, Thread, 0);
43bcaeed49SFangrui Song   my_global = 41;
44bcaeed49SFangrui Song   barrier_wait(&barrier);
45bcaeed49SFangrui Song   pthread_join(t, 0);
46bcaeed49SFangrui Song   fprintf(stderr, "Done.\n");
47bcaeed49SFangrui Song }
48bcaeed49SFangrui Song 
493879d3edSRoy Sundahl // Required for dyld macOS 12.0+
503879d3edSRoy Sundahl #if (__APPLE__)
513879d3edSRoy Sundahl __attribute__((weak))
523879d3edSRoy Sundahl #endif
533879d3edSRoy Sundahl __attribute__((disable_sanitizer_instrumentation))
543879d3edSRoy Sundahl extern "C" void
__tsan_on_report(void * report)5524af1ba6SDmitry Vyukov __tsan_on_report(void *report) {
56bcaeed49SFangrui Song   fprintf(stderr, "__tsan_on_report(%p)\n", report);
57bcaeed49SFangrui Song   fprintf(stderr, "__tsan_get_current_report() = %p\n",
58bcaeed49SFangrui Song           __tsan_get_current_report());
59bcaeed49SFangrui Song   // CHECK: __tsan_on_report([[REPORT:0x[0-9a-f]+]])
60bcaeed49SFangrui Song   // CHECK: __tsan_get_current_report() = [[REPORT]]
61bcaeed49SFangrui Song 
62bcaeed49SFangrui Song   const char *description;
63bcaeed49SFangrui Song   int count;
64bcaeed49SFangrui Song   int stack_count, mop_count, loc_count, mutex_count, thread_count,
65bcaeed49SFangrui Song       unique_tid_count;
66bcaeed49SFangrui Song   void *sleep_trace[16] = {0};
67bcaeed49SFangrui Song   __tsan_get_report_data(report, &description, &count, &stack_count, &mop_count,
68bcaeed49SFangrui Song                          &loc_count, &mutex_count, &thread_count,
69bcaeed49SFangrui Song                          &unique_tid_count, sleep_trace, 16);
70bcaeed49SFangrui Song   fprintf(stderr, "report type = '%s', count = %d\n", description, count);
71bcaeed49SFangrui Song   // CHECK: report type = 'data-race', count = 0
72bcaeed49SFangrui Song 
73bcaeed49SFangrui Song   fprintf(stderr, "mop_count = %d\n", mop_count);
74bcaeed49SFangrui Song   // CHECK: mop_count = 2
75bcaeed49SFangrui Song 
76bcaeed49SFangrui Song   int tid;
77bcaeed49SFangrui Song   void *addr;
78bcaeed49SFangrui Song   int size, write, atomic;
79bcaeed49SFangrui Song   void *trace[16] = {0};
80bcaeed49SFangrui Song 
81bcaeed49SFangrui Song   __tsan_get_report_mop(report, 0, &tid, &addr, &size, &write, &atomic, trace,
82bcaeed49SFangrui Song                         16);
83bcaeed49SFangrui Song   fprintf(stderr, "tid = %d, addr = %p, size = %d, write = %d, atomic = %d\n",
84bcaeed49SFangrui Song           tid, addr, size, write, atomic);
85bcaeed49SFangrui Song   // CHECK: tid = 1, addr = [[GLOBAL]], size = 8, write = 1, atomic = 0
86bcaeed49SFangrui Song   fprintf(stderr, "trace[0] = %p, trace[1] = %p\n", trace[0], trace[1]);
87bcaeed49SFangrui Song   // CHECK: trace[0] = 0x{{[0-9a-f]+}}, trace[1] = {{0x0|\(nil\)|\(null\)}}
88bcaeed49SFangrui Song 
89bcaeed49SFangrui Song   __tsan_get_report_mop(report, 1, &tid, &addr, &size, &write, &atomic, trace,
90bcaeed49SFangrui Song                         16);
91bcaeed49SFangrui Song   fprintf(stderr, "tid = %d, addr = %p, size = %d, write = %d, atomic = %d\n",
92bcaeed49SFangrui Song           tid, addr, size, write, atomic);
93bcaeed49SFangrui Song   // CHECK: tid = 0, addr = [[GLOBAL]], size = 8, write = 1, atomic = 0
94bcaeed49SFangrui Song   fprintf(stderr, "trace[0] = %p, trace[1] = %p\n", trace[0], trace[1]);
95bcaeed49SFangrui Song   // CHECK: trace[0] = 0x{{[0-9a-f]+}}, trace[1] = {{0x0|\(nil\)|\(null\)}}
96bcaeed49SFangrui Song 
97bcaeed49SFangrui Song   fprintf(stderr, "thread_count = %d\n", thread_count);
98bcaeed49SFangrui Song   // CHECK: thread_count = 2
99bcaeed49SFangrui Song 
100bcaeed49SFangrui Song   uint64_t os_id;
101bcaeed49SFangrui Song   int running;
102bcaeed49SFangrui Song   const char *name;
103bcaeed49SFangrui Song   int parent_tid;
104bcaeed49SFangrui Song 
105bcaeed49SFangrui Song   __tsan_get_report_thread(report, 0, &tid, &os_id, &running, &name, &parent_tid, trace, 16);
106bcaeed49SFangrui Song   fprintf(stderr, "tid = %d\n", tid);
107bcaeed49SFangrui Song   // CHECK: tid = 1
108bcaeed49SFangrui Song 
109bcaeed49SFangrui Song   __tsan_get_report_thread(report, 1, &tid, &os_id, &running, &name, &parent_tid, trace, 16);
110bcaeed49SFangrui Song   fprintf(stderr, "tid = %d\n", tid);
111bcaeed49SFangrui Song   // CHECK: tid = 0
112bcaeed49SFangrui Song }
113bcaeed49SFangrui Song 
114bcaeed49SFangrui Song // CHECK: Done.
115bcaeed49SFangrui Song // CHECK: ThreadSanitizer: reported 1 warnings
116