/
preload1.c
515 lines (315 loc) · 12.4 KB
/
preload1.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#define _GNU_SOURCE
#include <time.h> // For clock ticks
#include <stdio.h> // For file I/O functions
#include <stdlib.h>
#include <unistd.h> // For file I/O functions
#include <sys/types.h>
#include <dlfcn.h> // For dlsym()
#include <fcntl.h>
#include <stdarg.h> // For variable argumenrs
#include <string.h>
#include <limits.h>
// Global Variable to hold clock ticks at program start
static clock_t program_start;
static FILE *logFile;
// Defining a new constructor and destructor
static void wrap_init(void) __attribute__((constructor));
static void wrap_deinit(void) __attribute__((destructor));
// Pointers to original stdio.h functions
static FILE* (*_fopen) (const char *, const char *) = NULL;
static int (*_fclose)(FILE *stream) = NULL;
static int (*_fgetc) (FILE *__stream) = NULL;
static int (*_fputc) (int character, FILE *stream)= NULL;
static size_t(*_fread) ( void * ptr, size_t size, size_t count, FILE * stream ) = NULL;
static size_t(*_fwrite)( const void * ptr, size_t size, size_t count, FILE * stream ) = NULL;
static int (*_fputs) ( const char * str, FILE * stream ) = NULL;
static char* (*_fgets) ( char * str, int num, FILE * stream ) = NULL;
static int (*_fgetpos) ( FILE * stream, fpos_t * pos ) = NULL;
static int (*_fsetpos) ( FILE * stream, const fpos_t * pos ) = NULL;
static int (*_fseek) ( FILE * stream, long int offset, int origin ) = NULL;
static long int (*_ftell) ( FILE * stream ) = NULL;
static int (*_feof) ( FILE * stream ) = NULL;
static int (*_close)(int fd) = NULL;
/*
int fprintf(FILE *stream, const char *format, ...);
int vfprintf(FILE *stream, const char *format, va_list ap);
*/
// Pointers to original fcntl.h and unistd.h functions
static int (*_open)(const char *, int, mode_t) = NULL;
static ssize_t (*_read)(int fd, void *buf, size_t count) = NULL;
static ssize_t (*_write)(int fd, const void *buf, size_t count) = NULL;
static ssize_t (*_pread)(int fd, void *buf, size_t count, off_t offset) = NULL;
static ssize_t (*_pwrite)(int fd, const void *buf, size_t count, off_t offset) = NULL;
//static int (*_open64)(const char *, int, mode_t) = NULL;
// Constructor
static void wrap_init(void)
{
// Set original stdio functions
_fopen = dlsym(RTLD_NEXT,"fopen");
_fclose = dlsym(RTLD_NEXT,"fclose");
//_fprintf = dlsym(RTLD_NEXT,"fprintf");
//_vfprintf = dlsym(RTLD_NEXT,"vfprintf");
//_fscanf = dlsym(RTLD_NEXT,"fscanf");
//_sscanf = dlsym(RTstreamLD_NEXT,"sscanf");
_fgetc = dlsym(RTLD_NEXT,"fgetc");
_fputc = dlsym(RTLD_NEXT,"fputc");
_feof = dlsym(RTLD_NEXT,"feof");
_fread = dlsym(RTLD_NEXT,"fread");
_fwrite = dlsym(RTLD_NEXT,"fwrite");
_fgets = dlsym(RTLD_NEXT,"fgets");
_fputs = dlsym(RTLD_NEXT,"fputs");
_fseek = dlsym(RTLD_NEXT,"fseek");
//_freopen = dlsym(RTLD_NEXT,"freopen");
_fgetpos = dlsym(RTLD_NEXT,"fgetpos");
_fsetpos = dlsym(RTLD_NEXT,"fsetpos");
_ftell = dlsym(RTLD_NEXT,"ftell");
// Set original fcntl and unistd functions
_open = dlsym(RTLD_NEXT,"open");
//_open64 = dlsym(RTLD_NEXT,"open64");
_close = dlsym(RTLD_NEXT,"close");
_read = dlsym(RTLD_NEXT,"read");
_write = dlsym(RTLD_NEXT,"write");
_pread = dlsym(RTLD_NEXT,"pread");
_pwrite = dlsym(RTLD_NEXT,"pwrite");
//_creat = dlsym(RTLD_NEXT,"creat");
printf("START\n");
logFile = _fopen("logFile.txt","w");
program_start = clock(); // Set global program clock
}
// Destructor
static void wrap_deinit(void)
{
_fclose(logFile);
printf("End\n");
}
/* WRAPPER FOR FOPEN */
FILE* fopen(const char *pathname,const char *mode)
{
//printf("INTERCEPTED Fopen\n");
clock_t start = clock();
FILE * ret = _fopen(pathname,mode);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf fopen %s %s \n",called_time,exec_time,pathname,mode);
return ret;
}
/* WRAPPER FOR FCLOSE */
int fclose(FILE *stream)
{
//printf("INTERCEPTED Fclose\n");
clock_t start = clock();
int ret = _fclose(stream);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf fclose %p = %d\n",called_time,exec_time,stream,ret);
return ret;
}
/* WRAPPER FOR FGETC */
int fgetc(FILE *__stream)
{
//printf("INTERCEPTED Fgetc\n");
clock_t start = clock();
int ret = _fgetc(__stream);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf fgetc %p = %d\n",called_time,exec_time,__stream,ret);
return ret;
}
/* WRAPPER FOR FPUTC*/
int fputc(int character, FILE *stream)
{
//printf("INTERCEPTED Fputc\n");
clock_t start = clock();
int ret = _fputc(character,stream);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf fputc %d %p = %d\n",called_time,exec_time,character,stream,ret);
return ret;
}
/* WRAPPER FOR FREAD */
size_t fread( void * ptr, size_t size, size_t count, FILE * stream )
{
//printf("INTERCEPTED fread\n");
clock_t start = clock();
size_t ret = _fread(ptr,size,count,stream);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf fread %zu %zu %p = %zu\n",called_time,exec_time,size,count,stream,ret);
return ret;
}
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream )
{
//printf("INTERCEPTED fwriten");
clock_t start = clock();
size_t ret = _fwrite(ptr,size,count,stream);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf fwrite %zu %zu %p = %zu\n",called_time,exec_time,size,count,stream,ret);
return ret;
}
int fsetpos ( FILE * stream, const fpos_t * pos )
{
//printf("INTERCEPTED fsetpos\n");
clock_t start = clock();
int ret = _fsetpos(stream,pos);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf fsetpos %p = %d\n",called_time,exec_time,stream,ret);
return ret;
}
int fgetpos ( FILE * stream, fpos_t * pos )
{
//printf("INTERCEPTED fgetpos\n");
clock_t start = clock();
int ret = _fgetpos(stream,pos);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf fgetpos %p = %d\n",called_time,exec_time,stream,ret);
return ret;
}
char * fgets ( char * str, int num, FILE * stream )
{
//printf("INTERCEPTED fgets\n");
clock_t start = clock();
char* ret = _fgets(str,num,stream);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf fgets %s %d %p = %s\n",called_time,exec_time,str,num,stream,ret);
return ret;
}
int fputs ( const char * str, FILE * stream )
{
//printf("INTERCEPTED fputs\n");
clock_t start = clock();
int ret = _fputs(str,stream);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf fputs %s %p = %d\n",called_time,exec_time,str,stream,ret);
return ret;
}
int fseek ( FILE * stream, long int offset, int origin )
{
//printf("INTERCEPTED fseek\n");
clock_t start = clock();
int ret = _fseek(stream,offset,origin);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf fseek %p %ld %d = %d\n",called_time,exec_time,stream,offset,origin,ret);
return ret;
}
long int ftell ( FILE * stream )
{
//printf("INTERCEPTED ftell\n");
clock_t start = clock();
long int ret = _ftell(stream);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf ftell %p = %ld\n",called_time,exec_time,stream,ret);
return ret;
}
int feof ( FILE * stream )
{
//printf("INTERCEPTED feof\n");
clock_t start = clock();
int ret = _feof(stream);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf feof %p = %d\n",called_time,exec_time,stream,ret);
return ret;
}
/* WRAPPER FOR OPEN */
int open(const char *pathname, int flags, ...)
{
//printf("INTERCEPTED Open\n");
mode_t mode;
va_list vl;
va_start(vl, flags);
mode = va_arg(vl, mode_t);
va_end(vl);
clock_t start = clock();
int ret = _open(pathname, flags, mode);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf open %s %d = %d\n",called_time,exec_time,pathname,flags,ret);
return ret;
}
ssize_t read(int fd, void *buf, size_t count)
{
//printf("INTERCEPTED read\n");
clock_t start = clock();
ssize_t ret = _read(fd,buf,count);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf read %d %p %zu = %zu\n",called_time,exec_time,fd,buf,count,ret);
return ret;
}
ssize_t write(int fd, const void *buf, size_t count)
{
//printf("INTERCEPTED write\n");
clock_t start = clock();
ssize_t ret = _write(fd,buf,count);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf write %d %p %zu = %zu\n",called_time,exec_time,fd,buf,count,ret);
return ret;
}
ssize_t pread(int fd, void *buf, size_t count, off_t offset)
{
//printf("INTERCEPTED pread\n");
clock_t start = clock();
ssize_t ret = _pread(fd,buf,count,offset);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf pread %d %p %zu %ld = %zu\n",called_time,exec_time,fd,buf,count,offset,ret);
return ret;
}
ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset)
{
//printf("INTERCEPTED pwrite\n");
clock_t start = clock();
ssize_t ret = _pwrite(fd,buf,count,offset);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf pwrite %d %p %zu %ld = %zu\n",called_time,exec_time,fd,buf,count,offset,ret);
return ret;
}
int close(int fd)
{
clock_t start = clock();
int ret = _close(fd);
clock_t end = clock();
double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);
double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);
fprintf(logFile,"%lf %lf close %d = %d\n",called_time,exec_time,fd,ret);
return ret;
}
/*
int open64(const char *pathname, int flags, ...)
{
printf("INTERCEPTED Open64\n");
mode_t mode;
va_list vl;
va_start(vl, flags);
mode = va_arg(vl, mode_t);
va_end(vl);
return _open64(pathname, flags, mode);
}
*/