#include "test.h"
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef __APPLE__
#include <sys/types.h>
#endif
extern "C" int __tsan_get_alloc_stack(void *addr, void **trace, size_t size,
int *thread_id, uint64_t *os_id);
char *mem;
void alloc_func() { mem = (char *)malloc(10); }
void *AllocThread(void *context) {
uint64_t tid;
#ifdef __APPLE__
pthread_threadid_np(NULL, &tid);
#else
tid = gettid();
#endif
fprintf(stderr, "alloc stack thread os id = 0x%llx\n", tid);
alloc_func();
return NULL;
}
void *RaceThread(void *context) {
*mem = 'a';
barrier_wait(&barrier);
return NULL;
}
int main() {
pthread_t t;
barrier_init(&barrier, 2);
pthread_create(&t, NULL, AllocThread, NULL);
pthread_join(t, NULL);
void *trace[100];
size_t num_frames = 100;
int thread_id;
uint64_t thread_os_id;
num_frames =
__tsan_get_alloc_stack(mem, trace, num_frames, &thread_id, &thread_os_id);
fprintf(stderr, "alloc stack retval %s\n",
(num_frames > 0 && num_frames < 10) ? "ok" : "");
fprintf(stderr, "thread id = %d\n", thread_id);
fprintf(stderr, "thread os id = 0x%llx\n", thread_os_id);
fprintf(stderr, "%p\n", trace[0]);
fprintf(stderr, "%p\n", trace[1]);
fprintf(stderr, "%p\n", trace[2]);
pthread_create(&t, NULL, RaceThread, NULL);
barrier_wait(&barrier);
mem[0] = 'b';
pthread_join(t, NULL);
free(mem);
return 0;
}