-
Notifications
You must be signed in to change notification settings - Fork 0
/
libfuncs.cpp
69 lines (57 loc) · 2.39 KB
/
libfuncs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Copyright (C)
* 2017 - Jinpeng Zhou, Jinpeng.Zhou@utsa.edu
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
/**
* @file libfuncs.cpp
* @brief intercept functions
* @author Tongping Liu, <http://www.cs.utsa.edu/~tongpingliu/>
* @author Jinpeng Zhou, Jinpeng.Zhou@utsa.edu
*/
#include <dlfcn.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include "libfuncs.hh"
int (*WRAP(pthread_create))(pthread_t*, const pthread_attr_t*, void *(*)(void*), void*);
int (*WRAP(pthread_join))(pthread_t, void**);
int (*WRAP(pthread_mutex_init))(pthread_mutex_t*, const pthread_mutexattr_t*);
int (*WRAP(pthread_mutex_destroy))(pthread_mutex_t*);
int (*WRAP(pthread_mutex_lock))(pthread_mutex_t*);
int (*WRAP(pthread_mutex_unlock))(pthread_mutex_t*);
int (*WRAP(pthread_mutex_trylock))(pthread_mutex_t*);
int (*WRAP(pthread_cond_timedwait))(pthread_cond_t*, pthread_mutex_t*,const struct timespec*);
int (*WRAP(pthread_cond_wait))(pthread_cond_t*, pthread_mutex_t*);
#ifndef assert
#define assert(x)
#endif
#define SET_WRAPPED(x, handle) WRAP(x) = (decltype(WRAP(x)))dlsym(handle, #x); assert(#x)
void init_real_functions() {
void *pthread_handle = dlopen("libpthread.so.0", RTLD_NOW | RTLD_GLOBAL | RTLD_NOLOAD);
if (pthread_handle == NULL) {
fprintf(stderr, "Unable to load libpthread.so.0\n");
_exit(2);
}
SET_WRAPPED(pthread_create, pthread_handle);
SET_WRAPPED(pthread_join, pthread_handle);
SET_WRAPPED(pthread_mutex_init, pthread_handle);
SET_WRAPPED(pthread_mutex_destroy, pthread_handle);
SET_WRAPPED(pthread_mutex_lock, pthread_handle);
SET_WRAPPED(pthread_mutex_unlock, pthread_handle);
SET_WRAPPED(pthread_mutex_trylock, pthread_handle);
SET_WRAPPED(pthread_cond_timedwait, pthread_handle);
SET_WRAPPED(pthread_cond_wait, pthread_handle);
}