From 10379f18d500fb76563b0909e612d7151901c793 Mon Sep 17 00:00:00 2001 From: Dillon Date: Wed, 20 Nov 2024 00:32:08 -0800 Subject: [PATCH] All hooks are required now, they are only initialized if they are used --- pthread_trace.cc | 96 ++++++++++++++---------------------------------- 1 file changed, 28 insertions(+), 68 deletions(-) diff --git a/pthread_trace.cc b/pthread_trace.cc index 74e57ba..21e5b44 100644 --- a/pthread_trace.cc +++ b/pthread_trace.cc @@ -83,7 +83,7 @@ int (*sem_timedwait)(sem_t*, const struct timespec*) = nullptr; int (*sem_trywait)(sem_t*) = nullptr; int (*sem_post)(sem_t*) = nullptr; -template +template NOINLINE void init(T& hook, T def, const char* name, const char* version = nullptr) { if (hook) return; @@ -96,12 +96,8 @@ NOINLINE void init(T& hook, T def, const char* name, const char* version = nullp } } if (!result) { - if (Required) { - fprintf(stderr, "Failed to find %s\n", name); - exit(1); - } else { - return; - } + fprintf(stderr, "Failed to find %s\n", name); + exit(1); } // This might run on more than one thread, this is a benign race. @@ -624,82 +620,68 @@ track track::dummy{std::false_type()}; extern "C" { unsigned int sleep(unsigned int secs) { - if (!hooks::sleep) hooks::init(hooks::sleep, __sleep, "sleep"); - auto& t = track::get_thread(); t.write_begin(slice_begin_sleep); - assert(hooks::sleep); + if (!hooks::sleep) hooks::init(hooks::sleep, __sleep, "sleep"); unsigned int result = hooks::sleep(secs); t.write_end(); // slice_begin_sleep return result; } int usleep(useconds_t usecs) { - if (!hooks::usleep) hooks::init(hooks::usleep, __usleep, "usleep"); - auto& t = track::get_thread(); t.write_begin(slice_begin_usleep); - assert(hooks::usleep); + if (!hooks::usleep) hooks::init(hooks::usleep, __usleep, "usleep"); int result = hooks::usleep(usecs); t.write_end(); // slice_begin_usleep return result; } int nanosleep(const struct timespec* duration, struct timespec* rem) { - if (!hooks::nanosleep) hooks::init(hooks::nanosleep, __nanosleep, "nanosleep"); - auto& t = track::get_thread(); t.write_begin(slice_begin_nanosleep); - assert(hooks::nanosleep); + if (!hooks::nanosleep) hooks::init(hooks::nanosleep, __nanosleep, "nanosleep"); int result = hooks::nanosleep(duration, rem); t.write_end(); // slice_begin_nanosleep return result; } int sched_yield() { - if (!hooks::sched_yield) hooks::init(hooks::sched_yield, __sched_yield, "sched_yield"); - auto& t = track::get_thread(); t.write_begin(slice_begin_yield); - assert(hooks::sched_yield); + if (!hooks::sched_yield) hooks::init(hooks::sched_yield, __sched_yield, "sched_yield"); int result = hooks::sched_yield(); t.write_end(); // slice_begin_yield return result; } int pthread_cond_broadcast(pthread_cond_t* cond) { - if (!hooks::pthread_cond_broadcast) - hooks::init(hooks::pthread_cond_broadcast, __pthread_cond_broadcast, "pthread_cond_broadcast", "GLIBC_2.3.2"); - auto& t = track::get_thread(); t.write_begin(slice_begin_cond_broadcast); - assert(hooks::pthread_cond_broadcast); + if (!hooks::pthread_cond_broadcast) + hooks::init(hooks::pthread_cond_broadcast, __pthread_cond_broadcast, "pthread_cond_broadcast", "GLIBC_2.3.2"); int result = hooks::pthread_cond_broadcast(cond); t.write_end(); // slice_begin_cond_broadcast return result; } int pthread_cond_signal(pthread_cond_t* cond) { - if (!hooks::pthread_cond_signal) - hooks::init(hooks::pthread_cond_signal, __pthread_cond_signal, "pthread_cond_signal", "GLIBC_2.3.2"); - auto& t = track::get_thread(); t.write_begin(slice_begin_cond_signal); - assert(hooks::pthread_cond_signal); + if (!hooks::pthread_cond_signal) + hooks::init(hooks::pthread_cond_signal, __pthread_cond_signal, "pthread_cond_signal", "GLIBC_2.3.2"); int result = hooks::pthread_cond_signal(cond); t.write_end(); // slice_begin_cond_signal return result; } int pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* abstime) { - if (!hooks::pthread_cond_timedwait) - hooks::init(hooks::pthread_cond_timedwait, __pthread_cond_timedwait, "pthread_cond_timedwait", "GLIBC_2.3.2"); - // When we wait on a cond var, the mutex gets unlocked, and then relocked before returning. auto& t = track::get_thread(); t.write_end_mutex_locked("mutex", mutex); t.write_begin(slice_begin_cond_timedwait); - assert(hooks::pthread_cond_timedwait); + if (!hooks::pthread_cond_timedwait) + hooks::init(hooks::pthread_cond_timedwait, __pthread_cond_timedwait, "pthread_cond_timedwait", "GLIBC_2.3.2"); int result = hooks::pthread_cond_timedwait(cond, mutex, abstime); t.write_end(); // slice_begin_cond_timedwait t.write_begin_mutex_locked("mutex", mutex); @@ -707,14 +689,12 @@ int pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, const s } int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex) { - if (!hooks::pthread_cond_wait) - hooks::init(hooks::pthread_cond_wait, __pthread_cond_wait, "pthread_cond_wait", "GLIBC_2.3.2"); - // When we wait on a cond var, the mutex gets unlocked, and then relocked before returning. auto& t = track::get_thread(); t.write_end_mutex_locked("mutex", mutex); t.write_begin(slice_begin_cond_wait); - assert(hooks::pthread_cond_wait); + if (!hooks::pthread_cond_wait) + hooks::init(hooks::pthread_cond_wait, __pthread_cond_wait, "pthread_cond_wait", "GLIBC_2.3.2"); int result = hooks::pthread_cond_wait(cond, mutex); t.write_end(); // slice_begin_cond_wait t.write_begin_mutex_locked("mutex", mutex); @@ -722,23 +702,19 @@ int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex) { } int pthread_join(pthread_t thread, void** value_ptr) { - if (!hooks::pthread_join) hooks::init(hooks::pthread_join, __pthread_join, "pthread_join"); - auto& t = track::get_thread(); t.write_begin(slice_begin_join); - assert(hooks::pthread_join); + if (!hooks::pthread_join) hooks::init(hooks::pthread_join, __pthread_join, "pthread_join"); int result = hooks::pthread_join(thread, value_ptr); t.write_end(); // slice_begin_join return result; } int pthread_mutex_lock(pthread_mutex_t* mutex) { - if (!hooks::pthread_mutex_lock) - hooks::init(hooks::pthread_mutex_lock, __pthread_mutex_lock, "pthread_mutex_lock"); - auto& t = track::get_thread(); t.write_begin(slice_begin_mutex_lock); - assert(hooks::pthread_mutex_lock); + if (!hooks::pthread_mutex_lock) + hooks::init(hooks::pthread_mutex_lock, __pthread_mutex_lock, "pthread_mutex_lock"); int result = hooks::pthread_mutex_lock(mutex); t.write_end(); // slice_begin_mutex_lock t.write_begin_mutex_locked("mutex", mutex); @@ -746,12 +722,10 @@ int pthread_mutex_lock(pthread_mutex_t* mutex) { } int pthread_mutex_trylock(pthread_mutex_t* mutex) { - if (!hooks::pthread_mutex_trylock) - hooks::init(hooks::pthread_mutex_trylock, __pthread_mutex_trylock, "pthread_mutex_trylock"); - auto& t = track::get_thread(); t.write_begin(slice_begin_mutex_trylock); - assert(hooks::pthread_mutex_trylock); + if (!hooks::pthread_mutex_trylock) + hooks::init(hooks::pthread_mutex_trylock, __pthread_mutex_trylock, "pthread_mutex_trylock"); int result = hooks::pthread_mutex_trylock(mutex); t.write_end(); // slice_begin_mutex_trylock if (result == 0) { @@ -761,46 +735,38 @@ int pthread_mutex_trylock(pthread_mutex_t* mutex) { } int pthread_mutex_unlock(pthread_mutex_t* mutex) { - if (!hooks::pthread_mutex_unlock) - hooks::init(hooks::pthread_mutex_unlock, __pthread_mutex_unlock, "pthread_mutex_unlock"); - auto& t = track::get_thread(); t.write_end_mutex_locked("mutex", mutex); t.write_begin(slice_begin_mutex_unlock); - assert(hooks::pthread_mutex_unlock); + if (!hooks::pthread_mutex_unlock) + hooks::init(hooks::pthread_mutex_unlock, __pthread_mutex_unlock, "pthread_mutex_unlock"); int result = hooks::pthread_mutex_unlock(mutex); t.write_end(); // slice_begin_mutex_unlock return result; } int pthread_once(pthread_once_t* once_control, void (*init_routine)(void)) { - if (!hooks::pthread_once) hooks::init(hooks::pthread_once, __pthread_once, "pthread_once"); - auto& t = track::get_thread(); t.write_begin(slice_begin_once); - assert(hooks::pthread_once); + if (!hooks::pthread_once) hooks::init(hooks::pthread_once, __pthread_once, "pthread_once"); int result = hooks::pthread_once(once_control, init_routine); t.write_end(); // slice_begin_once return result; } int pthread_barrier_wait(pthread_barrier_t* barrier) { - hooks::init(hooks::pthread_barrier_wait, __pthread_barrier_wait, "pthread_barrier_wait"); - auto& t = track::get_thread(); t.write_begin(slice_begin_barrier_wait); - assert(hooks::pthread_barrier_wait); + hooks::init(hooks::pthread_barrier_wait, __pthread_barrier_wait, "pthread_barrier_wait"); int result = hooks::pthread_barrier_wait(barrier); t.write_end(); return result; } int sem_wait(sem_t* sem) { - if (!hooks::sem_wait) hooks::init(hooks::sem_wait, __sem_wait, "sem_wait"); - auto& t = track::get_thread(); t.write_begin(slice_begin_sem_wait); - assert(hooks::sem_wait); + if (!hooks::sem_wait) hooks::init(hooks::sem_wait, __sem_wait, "sem_wait"); int result = hooks::sem_wait(sem); t.write_end(); t.write_begin_mutex_locked("semaphore", sem); @@ -808,11 +774,9 @@ int sem_wait(sem_t* sem) { } int sem_timedwait(sem_t* sem, const struct timespec* abstime) { - if (!hooks::sem_timedwait) hooks::init(hooks::sem_timedwait, __sem_timedwait, "sem_timedwait"); - auto& t = track::get_thread(); t.write_begin(slice_begin_sem_timedwait); - assert(hooks::sem_timedwait); + if (!hooks::sem_timedwait) hooks::init(hooks::sem_timedwait, __sem_timedwait, "sem_timedwait"); int result = hooks::sem_timedwait(sem, abstime); t.write_end(); if (result == 0) { @@ -822,11 +786,9 @@ int sem_timedwait(sem_t* sem, const struct timespec* abstime) { } int sem_trywait(sem_t* sem) { - if (!hooks::sem_trywait) hooks::init(hooks::sem_trywait, __sem_trywait, "sem_trywait"); - auto& t = track::get_thread(); t.write_begin(slice_begin_sem_trywait); - assert(hooks::sem_trywait); + if (!hooks::sem_trywait) hooks::init(hooks::sem_trywait, __sem_trywait, "sem_trywait"); int result = hooks::sem_trywait(sem); t.write_end(); if (result == 0) { @@ -836,12 +798,10 @@ int sem_trywait(sem_t* sem) { } int sem_post(sem_t* sem) { - if (!hooks::sem_post) hooks::init(hooks::sem_post, __sem_post, "sem_post"); - auto& t = track::get_thread(); t.write_end_mutex_locked("semaphore", sem); t.write_begin(slice_begin_sem_post); - assert(hooks::sem_post); + if (!hooks::sem_post) hooks::init(hooks::sem_post, __sem_post, "sem_post"); int result = hooks::sem_post(sem); t.write_end(); return result;