-
Notifications
You must be signed in to change notification settings - Fork 1
/
override.c
148 lines (132 loc) · 3.92 KB
/
override.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#define _GNU_SOURCE
#include <assert.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
typedef int (openfqn)(const char*, int, ...);
typedef ssize_t (readfqn)(int, void*, size_t);
typedef ssize_t (writefqn)(int, const void*, size_t);
typedef int (closefqn)(int);
typedef int (dup2fqn)(int, int);
typedef FILE* (fopenfqn)(const char*, const char*);
typedef int (fclosefqn)(FILE*);
typedef int (f95_st_openfqn)(void*, void*, void*, void*, void*);
typedef void* (write_blockfqn)(void*, int);
typedef int (f95_arwritefqn)(void*, void*, int, int, int);
typedef void* MPI_Comm;
typedef void* MPI_Info;
typedef void* MPI_File;
typedef int (mpi_file_openfqn)(MPI_Comm, char*, int, MPI_Info, MPI_File*);
static openfqn* openf = NULL;
static readfqn* readf = NULL;
static writefqn* writef = NULL;
static closefqn* closef = NULL;
static dup2fqn* dup2f = NULL;
static fopenfqn* fopenf = NULL;
static fclosefqn* fclosef = NULL;
static mpi_file_openfqn* mpi_file_openf = NULL;
static f95_st_openfqn* f95_st_openf = NULL;
static write_blockfqn* write_blockf = NULL;
static f95_arwritefqn* f95_array_writef = NULL;
__attribute__((constructor)) static void
fp_init()
{
fprintf(stderr, "[%d] initializing function pointers.\n", (int)getpid());
openf = dlsym(RTLD_NEXT, "open");
readf = dlsym(RTLD_NEXT, "read");
writef = dlsym(RTLD_NEXT, "write");
closef = dlsym(RTLD_NEXT, "close");
dup2f = dlsym(RTLD_NEXT, "dup2");
fopenf = dlsym(RTLD_NEXT, "fopen");
fclosef = dlsym(RTLD_NEXT, "fclose");
mpi_file_openf = dlsym(RTLD_NEXT, "MPI_File_open");
f95_st_openf = dlsym(RTLD_NEXT, "_gfortran_st_open");
write_blockf = dlsym(RTLD_NEXT, "write_block");
f95_array_writef = dlsym(RTLD_NEXT, "_gfortran_transfer_array_write");
assert(openf != NULL);
assert(readf != NULL);
assert(writef != NULL);
assert(closef != NULL);
assert(dup2f != NULL);
assert(fopenf != NULL);
assert(fclosef != NULL);
}
int
open(const char* fn, int flags, ...)
{
va_list aq;
va_start(aq, flags);
fprintf(stderr, "[%d] opening %s ...", (int)getpid(), fn);
int des = openf(fn, flags, aq);
fprintf(stderr, " -> %d\n", des);
va_end(aq);
return des;
}
ssize_t
read(int d, void* buf, size_t sz)
{
fprintf(stderr, "[%d] reading %zu bytes from %d...\n", (int)getpid(), sz, d);
assert(readf != NULL);
return readf(d, buf, sz);
}
ssize_t
write(int d, const void *buf, size_t sz)
{
fprintf(stderr, "[%d] writing %zu bytes to %d\n", (int)getpid(), sz, d);
return writef(d, buf, sz);
}
int
close(int d)
{
fprintf(stderr, "[%d] closing %d\n", (int)getpid(), d);
return closef(d);
}
int
dup2(int old, int new)
{
fprintf(stderr, "[%d] dup2-ing %d to %d\n", (int)getpid(), old, new);
return dup2f(old, new);
}
FILE*
fopen(const char* name, const char* mode)
{
fprintf(stderr, "[%d] opening %s\n", (int)getpid(), name);
return fopenf(name, mode);
}
int
fclose(FILE* fp)
{
fprintf(stderr, "[%d] closing %p\n", (int)getpid(), fp);
return fclosef(fp);
}
int MPI_File_open(MPI_Comm comm, char *filename, int amode,
MPI_Info info, MPI_File *fh)
{
fprintf(stderr, "[%d] mpi_file_open '%s'\n", (int)getpid(), filename);
return mpi_file_openf(comm, filename, amode, info, fh);
}
int
_gfortran_st_open(void* ptr1, void* ptr2, void* ptr3, void* ptr4, void* ptr5)
{
fprintf(stderr, "[%d] gfortran_st_open(%p, %p, %p, %p, %p)\n", (int)getpid(),
ptr1, ptr2, ptr3, ptr4, ptr5);
return f95_st_openf(ptr1, ptr2, ptr3, ptr4, ptr5);
}
int
_gfortran_transfer_array_write(void* ptr1, void* ptr2, int n1, int n2,
int n3)
{
fprintf(stderr, "[%d] _gfortran_transfer_array_write(%p, %p, %d, %d, "
"%d)\n", (int)getpid(), ptr1, ptr2, n1, n2, n3);
return f95_array_writef(ptr1, ptr2, n1, n2, n3);
}
void*
write_block(void* p, int x)
{
fprintf(stderr, "[%d] write_block(%p, %d)\n", (int)getpid(), p, x);
return write_blockf(p, x);
}