[95dab9e] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2022 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // interpose_thread.c -- |
---|
| 8 | // |
---|
| 9 | // Author : Thierry Delisle |
---|
| 10 | // Created On : Wed Sep 21 11:55:16 2022 |
---|
| 11 | // Last Modified By : |
---|
| 12 | // Last Modified On : |
---|
| 13 | // Update Count : |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | #include <stdarg.h> // va_start, va_end |
---|
| 17 | #include <stdio.h> |
---|
| 18 | #include <string.h> // strlen |
---|
[9cd5bd2] | 19 | #include <signal.h> |
---|
| 20 | #include <pthread.h> |
---|
[95dab9e] | 21 | extern "C" { |
---|
| 22 | #include <dlfcn.h> // dlopen, dlsym |
---|
| 23 | #include <execinfo.h> // backtrace, messages |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | #include "bits/debug.hfa" |
---|
| 27 | #include "bits/defs.hfa" |
---|
| 28 | #include <assert.h> |
---|
| 29 | |
---|
| 30 | //============================================================================================= |
---|
| 31 | // Interposing helpers |
---|
| 32 | //============================================================================================= |
---|
| 33 | |
---|
| 34 | typedef void (* generic_fptr_t)(void); |
---|
| 35 | |
---|
| 36 | generic_fptr_t interpose_symbol( |
---|
| 37 | generic_fptr_t (*do_interpose_symbol)( void * library, const char symbol[], const char version[] ), |
---|
| 38 | const char symbol[], |
---|
| 39 | const char version[] |
---|
| 40 | ) libcfa_public { |
---|
| 41 | static void * library; |
---|
| 42 | if ( ! library ) { |
---|
[c910709] | 43 | library = dlopen( "libpthread.so", RTLD_LAZY ); |
---|
| 44 | const char * error = dlerror(); |
---|
| 45 | if ( error ) { |
---|
| 46 | abort( "interpose_symbol : failed to open libpthread, %s\n", error ); |
---|
| 47 | } |
---|
[95dab9e] | 48 | } // if |
---|
| 49 | |
---|
| 50 | return do_interpose_symbol(library, symbol, version); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | #define INTERPOSE( x, ver ) __cabi_libpthread.x = (typeof(__cabi_libpthread.x))interpose_symbol( do_interpose_symbol, #x, ver ) |
---|
| 54 | |
---|
| 55 | //============================================================================================= |
---|
| 56 | // Interposition Startup logic |
---|
| 57 | //============================================================================================= |
---|
| 58 | |
---|
| 59 | static struct { |
---|
| 60 | int (*pthread_create)(pthread_t *_thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); |
---|
| 61 | int (*pthread_join)(pthread_t _thread, void **retval); |
---|
| 62 | pthread_t (*pthread_self)(void); |
---|
| 63 | int (*pthread_attr_init)(pthread_attr_t *attr); |
---|
| 64 | int (*pthread_attr_destroy)(pthread_attr_t *attr); |
---|
| 65 | int (*pthread_attr_setstack)( pthread_attr_t *attr, void *stackaddr, size_t stacksize ); |
---|
| 66 | int (*pthread_attr_getstacksize)( const pthread_attr_t *attr, size_t *stacksize ); |
---|
| 67 | int (*pthread_sigmask)(int how, const sigset_t *set, sigset_t *oldset); |
---|
| 68 | int (*pthread_sigqueue)(pthread_t _thread, int sig, const union sigval value); |
---|
[7f81ef4] | 69 | int (*pthread_once)(pthread_once_t *once_control, void (*init_routine)(void)); |
---|
[95dab9e] | 70 | } __cabi_libpthread; |
---|
| 71 | |
---|
| 72 | extern "C" { |
---|
| 73 | void __cfathreadabi_interpose_startup( generic_fptr_t (*do_interpose_symbol)( void * library, const char symbol[], const char version[] ) ) libcfa_public { |
---|
| 74 | const char *version = 0p; |
---|
| 75 | |
---|
| 76 | #pragma GCC diagnostic push |
---|
| 77 | #pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" |
---|
| 78 | INTERPOSE( pthread_create , version ); |
---|
| 79 | INTERPOSE( pthread_join , version ); |
---|
| 80 | INTERPOSE( pthread_self , version ); |
---|
| 81 | INTERPOSE( pthread_attr_init , version ); |
---|
| 82 | INTERPOSE( pthread_attr_destroy , version ); |
---|
| 83 | INTERPOSE( pthread_attr_setstack , version ); |
---|
| 84 | INTERPOSE( pthread_attr_getstacksize , version ); |
---|
| 85 | INTERPOSE( pthread_sigmask , version ); |
---|
| 86 | INTERPOSE( pthread_sigqueue , version ); |
---|
[7f81ef4] | 87 | INTERPOSE( pthread_once , version ); |
---|
[95dab9e] | 88 | #pragma GCC diagnostic pop |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | int __cfaabi_pthread_create(pthread_t *_thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg){ |
---|
| 92 | return __cabi_libpthread.pthread_create(_thread, attr, start_routine, arg); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | int __cfaabi_pthread_join(pthread_t _thread, void **retval){ |
---|
| 96 | return __cabi_libpthread.pthread_join(_thread, retval); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | pthread_t __cfaabi_pthread_self(void){ |
---|
| 100 | return __cabi_libpthread.pthread_self(); |
---|
| 101 | } |
---|
[7f81ef4] | 102 | |
---|
[95dab9e] | 103 | int __cfaabi_pthread_attr_init(pthread_attr_t *attr){ |
---|
| 104 | return __cabi_libpthread.pthread_attr_init(attr); |
---|
| 105 | } |
---|
[7f81ef4] | 106 | |
---|
[95dab9e] | 107 | int __cfaabi_pthread_attr_destroy(pthread_attr_t *attr){ |
---|
| 108 | return __cabi_libpthread.pthread_attr_destroy(attr); |
---|
| 109 | } |
---|
[7f81ef4] | 110 | |
---|
[95dab9e] | 111 | int __cfaabi_pthread_attr_setstack( pthread_attr_t *attr, void *stackaddr, size_t stacksize ){ |
---|
| 112 | return __cabi_libpthread.pthread_attr_setstack(attr, stackaddr, stacksize); |
---|
| 113 | } |
---|
[7f81ef4] | 114 | |
---|
[95dab9e] | 115 | int read_pthread_attr_getstacksize( const pthread_attr_t *attr, size_t *stacksize ){ |
---|
| 116 | return __cabi_libpthread.pthread_attr_getstacksize(attr, stacksize); |
---|
| 117 | } |
---|
[7f81ef4] | 118 | |
---|
[95dab9e] | 119 | int __cfaabi_pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset){ |
---|
| 120 | return __cabi_libpthread.pthread_sigmask(how, set, oldset); |
---|
| 121 | } |
---|
[7f81ef4] | 122 | |
---|
| 123 | int __cfaabi_pthread_sigqueue(pthread_t _thread, int sig, const union sigval value) { |
---|
[95dab9e] | 124 | return __cabi_libpthread.pthread_sigqueue(_thread, sig, value); |
---|
| 125 | } |
---|
[7f81ef4] | 126 | |
---|
| 127 | int __cfaabi_pthread_once(pthread_once_t *once_control, void (*init_routine)(void)) { |
---|
| 128 | return __cabi_libpthread.pthread_once(once_control, init_routine); |
---|
| 129 | } |
---|
[fbdfcd8] | 130 | } |
---|