source: libcfa/src/interpose_thread.cfa @ 70cd431

ADTast-experimental
Last change on this file since 70cd431 was 7f81ef4, checked in by Thierry Delisle <tdelisle@…>, 18 months ago

added pthread once among the list of routines in the cfa_libpthread.

  • Property mode set to 100644
File size: 4.8 KB
Line 
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
19#include <signal.h>
20#include <pthread.h>
21extern "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
34typedef void (* generic_fptr_t)(void);
35
36generic_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        const char * error;
42
43        static void * library;
44        if ( ! library ) {
45                #if defined( RTLD_NEXT )
46                        library = RTLD_NEXT;
47                #else
48                        // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
49                        library = dlopen( "libpthread.so", RTLD_LAZY );
50                        error = dlerror();
51                        if ( error ) {
52                                abort( "interpose_symbol : failed to open libpthread, %s\n", error );
53                        }
54                #endif
55        } // if
56
57        return do_interpose_symbol(library, symbol, version);
58}
59
60#define INTERPOSE( x, ver ) __cabi_libpthread.x = (typeof(__cabi_libpthread.x))interpose_symbol( do_interpose_symbol, #x, ver )
61
62//=============================================================================================
63// Interposition Startup logic
64//=============================================================================================
65
66static struct {
67        int (*pthread_create)(pthread_t *_thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
68        int (*pthread_join)(pthread_t _thread, void **retval);
69        pthread_t (*pthread_self)(void);
70        int (*pthread_attr_init)(pthread_attr_t *attr);
71        int (*pthread_attr_destroy)(pthread_attr_t *attr);
72        int (*pthread_attr_setstack)( pthread_attr_t *attr, void *stackaddr, size_t stacksize );
73        int (*pthread_attr_getstacksize)( const pthread_attr_t *attr, size_t *stacksize );
74        int (*pthread_sigmask)(int how, const sigset_t *set, sigset_t *oldset);
75        int (*pthread_sigqueue)(pthread_t _thread, int sig, const union sigval value);
76        int (*pthread_once)(pthread_once_t *once_control, void (*init_routine)(void));
77} __cabi_libpthread;
78
79extern "C" {
80        void __cfathreadabi_interpose_startup( generic_fptr_t (*do_interpose_symbol)( void * library, const char symbol[], const char version[] ) ) libcfa_public {
81                const char *version = 0p;
82
83#pragma GCC diagnostic push
84#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
85                INTERPOSE( pthread_create , version );
86                INTERPOSE( pthread_join , version );
87                INTERPOSE( pthread_self , version );
88                INTERPOSE( pthread_attr_init , version );
89                INTERPOSE( pthread_attr_destroy , version );
90                INTERPOSE( pthread_attr_setstack , version );
91                INTERPOSE( pthread_attr_getstacksize , version );
92                INTERPOSE( pthread_sigmask , version );
93                INTERPOSE( pthread_sigqueue , version );
94                INTERPOSE( pthread_once , version );
95#pragma GCC diagnostic pop
96        }
97
98        int __cfaabi_pthread_create(pthread_t *_thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg){
99                return __cabi_libpthread.pthread_create(_thread, attr, start_routine, arg);
100        }
101
102        int __cfaabi_pthread_join(pthread_t _thread, void **retval){
103                return __cabi_libpthread.pthread_join(_thread, retval);
104        }
105
106        pthread_t __cfaabi_pthread_self(void){
107                return __cabi_libpthread.pthread_self();
108        }
109
110        int __cfaabi_pthread_attr_init(pthread_attr_t *attr){
111                return __cabi_libpthread.pthread_attr_init(attr);
112        }
113
114        int __cfaabi_pthread_attr_destroy(pthread_attr_t *attr){
115                return __cabi_libpthread.pthread_attr_destroy(attr);
116        }
117
118        int __cfaabi_pthread_attr_setstack( pthread_attr_t *attr, void *stackaddr, size_t stacksize ){
119                return __cabi_libpthread.pthread_attr_setstack(attr, stackaddr, stacksize);
120        }
121
122        int read_pthread_attr_getstacksize( const pthread_attr_t *attr, size_t *stacksize ){
123                return __cabi_libpthread.pthread_attr_getstacksize(attr, stacksize);
124        }
125
126        int __cfaabi_pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset){
127                return __cabi_libpthread.pthread_sigmask(how, set, oldset);
128        }
129
130        int __cfaabi_pthread_sigqueue(pthread_t _thread, int sig, const union sigval value) {
131                return __cabi_libpthread.pthread_sigqueue(_thread, sig, value);
132        }
133
134        int __cfaabi_pthread_once(pthread_once_t *once_control, void (*init_routine)(void)) {
135                return __cabi_libpthread.pthread_once(once_control, init_routine);
136        }
137}
Note: See TracBrowser for help on using the repository browser.