]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gprofng/libcollector/iotrace.c
2.41 Release sources
[thirdparty/binutils-gdb.git] / gprofng / libcollector / iotrace.c
1 /* Copyright (C) 2021-2023 Free Software Foundation, Inc.
2 Contributed by Oracle.
3
4 This file is part of GNU Binutils.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 /*
22 * IO events
23 */
24 #include "config.h"
25 #include <dlfcn.h>
26 #include <errno.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29
30 // create() and others are defined in fcntl.h.
31 // Our 'create' should not have the __nonnull attribute
32 #undef __nonnull
33 #define __nonnull(x)
34 #include <fcntl.h>
35
36 #include "gp-defs.h"
37 #include "collector.h"
38 #include "gp-experiment.h"
39 #include "tsd.h"
40
41
42 /* define the packet that will be written out */
43 typedef struct IOTrace_packet
44 { /* IO tracing packet */
45 Common_packet comm;
46 IOTrace_type iotype; /* IO type */
47 int32_t fd; /* file descriptor */
48 Size_type nbyte; /* number of bytes */
49 hrtime_t requested; /* time of IO requested */
50 int32_t ofd; /* original file descriptor */
51 FileSystem_type fstype; /* file system type */
52 char fname; /* file name */
53 } IOTrace_packet;
54
55 typedef long long offset_t;
56
57 static int open_experiment (const char *);
58 static int start_data_collection (void);
59 static int stop_data_collection (void);
60 static int close_experiment (void);
61 static int detach_experiment (void);
62 static int init_io_intf ();
63
64 static ModuleInterface module_interface ={
65 SP_IOTRACE_FILE, /* description */
66 NULL, /* initInterface */
67 open_experiment, /* openExperiment */
68 start_data_collection, /* startDataCollection */
69 stop_data_collection, /* stopDataCollection */
70 close_experiment, /* closeExperiment */
71 detach_experiment /* detachExperiment (fork child) */
72 };
73
74 static CollectorInterface *collector_interface = NULL;
75 static struct Heap *io_heap = NULL;
76 static int io_mode = 0;
77 static CollectorModule io_hndl = COLLECTOR_MODULE_ERR;
78 static unsigned io_key = COLLECTOR_TSD_INVALID_KEY;
79
80 #define CHCK_REENTRANCE(x) (!io_mode || ((x) = collector_interface->getKey( io_key )) == NULL || (*(x) != 0))
81 #define RECHCK_REENTRANCE(x) (!io_mode || ((x) = collector_interface->getKey( io_key )) == NULL || (*(x) == 0))
82 #define PUSH_REENTRANCE(x) ((*(x))++)
83 #define POP_REENTRANCE(x) ((*(x))--)
84 #define gethrtime collector_interface->getHiResTime
85
86
87 /* interposition function handles */
88 static int (*__real_open)(const char *path, int oflag, ...) = NULL;
89 static int (*__real_fcntl)(int fildes, int cmd, ...) = NULL;
90 static int (*__real_openat)(int fildes, const char *path, int oflag, ...) = NULL;
91 static int (*__real_close)(int fildes) = NULL;
92 static FILE *(*__real_fopen)(const char *filename, const char *mode) = NULL;
93 static int (*__real_fclose)(FILE *stream) = NULL;
94 static int (*__real_dup)(int fildes) = NULL;
95 static int (*__real_dup2)(int fildes, int fildes2) = NULL;
96 static int (*__real_pipe)(int fildes[2]) = NULL;
97 static int (*__real_socket)(int domain, int type, int protocol) = NULL;
98 static int (*__real_mkstemp)(char *template) = NULL;
99 static int (*__real_mkstemps)(char *template, int slen) = NULL;
100 static int (*__real_creat)(const char *path, mode_t mode) = NULL;
101 static FILE *(*__real_fdopen)(int fildes, const char *mode) = NULL;
102 static ssize_t (*__real_read)(int fildes, void *buf, size_t nbyte) = NULL;
103 static ssize_t (*__real_write)(int fildes, const void *buf, size_t nbyte) = NULL;
104 static ssize_t (*__real_readv)(int fildes, const struct iovec *iov, int iovcnt) = NULL;
105 static ssize_t (*__real_writev)(int fildes, const struct iovec *iov, int iovcnt) = NULL;
106 static size_t (*__real_fread)(void *ptr, size_t size, size_t nitems, FILE *stream) = NULL;
107 static size_t (*__real_fwrite)(const void *ptr, size_t size, size_t nitems, FILE *stream) = NULL;
108 static ssize_t (*__real_pread)(int fildes, void *buf, size_t nbyte, off_t offset) = NULL;
109 static ssize_t (*__real_pwrite)(int fildes, const void *buf, size_t nbyte, off_t offset) = NULL;
110 static ssize_t (*__real_pwrite64)(int fildes, const void *buf, size_t nbyte, off64_t offset) = NULL;
111 static char *(*__real_fgets)(char *s, int n, FILE *stream) = NULL;
112 static int (*__real_fputs)(const char *s, FILE *stream) = NULL;
113 static int (*__real_fputc)(int c, FILE *stream) = NULL;
114 static int (*__real_fprintf)(FILE *stream, const char *format, ...) = NULL;
115 static int (*__real_vfprintf)(FILE *stream, const char *format, va_list ap) = NULL;
116 static off_t (*__real_lseek)(int fildes, off_t offset, int whence) = NULL;
117 static offset_t (*__real_llseek)(int fildes, offset_t offset, int whence) = NULL;
118 static int (*__real_chmod)(const char *path, mode_t mode) = NULL;
119 static int (*__real_access)(const char *path, int amode) = NULL;
120 static int (*__real_rename)(const char *old, const char *new) = NULL;
121 static int (*__real_mkdir)(const char *path, mode_t mode) = NULL;
122 static int (*__real_getdents)(int fildes, struct dirent *buf, size_t nbyte) = NULL;
123 static int (*__real_unlink)(const char *path) = NULL;
124 static int (*__real_fseek)(FILE *stream, long offset, int whence) = NULL;
125 static void (*__real_rewind)(FILE *stream) = NULL;
126 static long (*__real_ftell)(FILE *stream) = NULL;
127 static int (*__real_fgetpos)(FILE *stream, fpos_t *pos) = NULL;
128 static int (*__real_fsetpos)(FILE *stream, const fpos_t *pos) = NULL;
129 static int (*__real_fsync)(int fildes) = NULL;
130 static struct dirent *(*__real_readdir)(DIR *dirp) = NULL;
131 static int (*__real_flock)(int fd, int operation) = NULL;
132 static int (*__real_lockf)(int fildes, int function, off_t size) = NULL;
133 static int (*__real_fflush)(FILE *stream) = NULL;
134 static int (*__real_open64)(const char *path, int oflag, ...) = NULL;
135 static int (*__real_open64_2_2)(const char *path, int oflag, ...) = NULL;
136 static int (*__real_creat64)(const char *path, mode_t mode) = NULL;
137 static int (*__real_fgetpos64)(FILE *stream, fpos64_t *pos) = NULL;
138 static int (*__real_fsetpos64)(FILE *stream, const fpos64_t *pos) = NULL;
139 static FILE *(*__real_fopen_2_17)(const char *filename, const char *mode) = NULL;
140 static FILE *(*__real_fopen_2_2_5)(const char *filename, const char *mode) = NULL;
141 static FILE *(*__real_fopen_2_1)(const char *filename, const char *mode) = NULL;
142 static FILE *(*__real_fopen_2_0)(const char *filename, const char *mode) = NULL;
143 static int (*__real_fclose_2_17)(FILE *stream) = NULL;
144 static int (*__real_fclose_2_2_5)(FILE *stream) = NULL;
145 static int (*__real_fclose_2_1)(FILE *stream) = NULL;
146 static int (*__real_fclose_2_0)(FILE *stream) = NULL;
147 static FILE *(*__real_fdopen_2_17)(int fildes, const char *mode) = NULL;
148 static FILE *(*__real_fdopen_2_2_5)(int fildes, const char *mode) = NULL;
149 static FILE *(*__real_fdopen_2_1)(int fildes, const char *mode) = NULL;
150 static FILE *(*__real_fdopen_2_0)(int fildes, const char *mode) = NULL;
151 static int (*__real_fgetpos_2_17)(FILE *stream, fpos_t *pos) = NULL;
152 static int (*__real_fgetpos_2_2_5)(FILE *stream, fpos_t *pos) = NULL;
153 static int (*__real_fgetpos_2_2)(FILE *stream, fpos_t *pos) = NULL;
154 static int (*__real_fgetpos_2_0)(FILE *stream, fpos_t *pos) = NULL;
155 static int (*__real_fsetpos_2_17)(FILE *stream, const fpos_t *pos) = NULL;
156 static int (*__real_fsetpos_2_2_5)(FILE *stream, const fpos_t *pos) = NULL;
157 static int (*__real_fsetpos_2_2)(FILE *stream, const fpos_t *pos) = NULL;
158 static int (*__real_fsetpos_2_0)(FILE *stream, const fpos_t *pos) = NULL;
159 static ssize_t (*__real_pread_2_2)(int fildes, void *buf, size_t nbyte, off_t offset) = NULL;
160 static ssize_t (*__real_pwrite_2_2)(int fildes, const void *buf, size_t nbyte, off_t offset) = NULL;
161 static int (*__real_fgetpos64_2_17)(FILE *stream, fpos64_t *pos) = NULL;
162 static int (*__real_fgetpos64_2_2_5)(FILE *stream, fpos64_t *pos) = NULL;
163 static int (*__real_fgetpos64_2_2)(FILE *stream, fpos64_t *pos) = NULL;
164 static int (*__real_fgetpos64_2_1)(FILE *stream, fpos64_t *pos) = NULL;
165 static int (*__real_fsetpos64_2_17)(FILE *stream, const fpos64_t *pos) = NULL;
166 static int (*__real_fsetpos64_2_2_5)(FILE *stream, const fpos64_t *pos) = NULL;
167 static int (*__real_fsetpos64_2_2)(FILE *stream, const fpos64_t *pos) = NULL;
168 static int (*__real_fsetpos64_2_1)(FILE *stream, const fpos64_t *pos) = NULL;
169 static ssize_t (*__real_pwrite64_2_2)(int fildes, const void *buf, size_t nbyte, off64_t offset) = NULL;
170
171 static int
172 collector_align_pktsize (int sz)
173 {
174 int pktSize = sz;
175 if (sz <= 0)
176 return sz;
177 if ((sz % 8) != 0)
178 {
179 pktSize = (sz / 8) + 1;
180 pktSize *= 8;
181 }
182 return pktSize;
183 }
184
185 static void
186 collector_memset (void *s, int c, size_t n)
187 {
188 unsigned char *s1 = s;
189 while (n--)
190 *s1++ = (unsigned char) c;
191 }
192
193 static size_t
194 collector_strlen (const char *s)
195 {
196 if (s == NULL)
197 return 0;
198 int len = -1;
199 while (s[++len] != '\0')
200 ;
201 return len;
202 }
203
204 static size_t
205 collector_strncpy (char *dst, const char *src, size_t dstsize)
206 {
207 size_t i;
208 for (i = 0; i < dstsize; i++)
209 {
210 dst[i] = src[i];
211 if (src[i] == '\0')
212 break;
213 }
214 return i;
215 }
216
217 static char *
218 collector_strchr (const char *s, int c)
219 {
220 do
221 {
222 if (*s == (char) c)
223 return ((char *) s);
224 }
225 while (*s++);
226 return (NULL);
227 }
228
229 static FileSystem_type
230 collector_fstype (const char *path)
231 {
232 return UNKNOWNFS_TYPE;
233 }
234
235 void
236 __collector_module_init (CollectorInterface *_collector_interface)
237 {
238 if (_collector_interface == NULL)
239 return;
240 collector_interface = _collector_interface;
241 Tprintf (0, "iotrace: __collector_module_init\n");
242 io_hndl = collector_interface->registerModule (&module_interface);
243 /* Initialize next module */
244 ModuleInitFunc next_init = (ModuleInitFunc) dlsym (RTLD_NEXT, "__collector_module_init");
245 if (next_init != NULL)
246 next_init (_collector_interface);
247 return;
248 }
249
250 static int
251 open_experiment (const char *exp)
252 {
253 if (collector_interface == NULL)
254 {
255 Tprintf (0, "iotrace: collector_interface is null.\n");
256 return COL_ERROR_IOINIT;
257 }
258 if (io_hndl == COLLECTOR_MODULE_ERR)
259 {
260 Tprintf (0, "iotrace: handle create failed.\n");
261 collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">data handle not created</event>\n",
262 SP_JCMD_CERROR, COL_ERROR_IOINIT);
263 return COL_ERROR_IOINIT;
264 }
265 TprintfT (0, "iotrace: open_experiment %s\n", exp);
266 if (NULL_PTR (fopen))
267 init_io_intf ();
268 if (io_heap == NULL)
269 {
270 io_heap = collector_interface->newHeap ();
271 if (io_heap == NULL)
272 {
273 Tprintf (0, "iotrace: new heap failed.\n");
274 collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">new iotrace heap not created</event>\n",
275 SP_JCMD_CERROR, COL_ERROR_IOINIT);
276 return COL_ERROR_IOINIT;
277 }
278 }
279
280 const char *params = collector_interface->getParams ();
281 while (params)
282 {
283 if ((params[0] == 'i') && (params[1] == ':'))
284 {
285 params += 2;
286 break;
287 }
288 params = collector_strchr (params, ';');
289 if (params)
290 params++;
291 }
292 if (params == NULL) /* IO data collection not specified */
293 return COL_ERROR_IOINIT;
294
295 io_key = collector_interface->createKey (sizeof ( int), NULL, NULL);
296 if (io_key == (unsigned) - 1)
297 {
298 Tprintf (0, "iotrace: TSD key create failed.\n");
299 collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">TSD key not created</event>\n",
300 SP_JCMD_CERROR, COL_ERROR_IOINIT);
301 return COL_ERROR_IOINIT;
302 }
303
304 collector_interface->writeLog ("<profile name=\"%s\">\n", SP_JCMD_IOTRACE);
305 collector_interface->writeLog (" <profdata fname=\"%s\"/>\n",
306 module_interface.description);
307 /* Record IOTrace_packet description */
308 IOTrace_packet *pp = NULL;
309 collector_interface->writeLog (" <profpckt kind=\"%d\" uname=\"IO tracing data\">\n", IOTRACE_PCKT);
310 collector_interface->writeLog (" <field name=\"LWPID\" uname=\"Lightweight process id\" offset=\"%d\" type=\"%s\"/>\n",
311 &pp->comm.lwp_id, sizeof (pp->comm.lwp_id) == 4 ? "INT32" : "INT64");
312 collector_interface->writeLog (" <field name=\"THRID\" uname=\"Thread number\" offset=\"%d\" type=\"%s\"/>\n",
313 &pp->comm.thr_id, sizeof (pp->comm.thr_id) == 4 ? "INT32" : "INT64");
314 collector_interface->writeLog (" <field name=\"CPUID\" uname=\"CPU id\" offset=\"%d\" type=\"%s\"/>\n",
315 &pp->comm.cpu_id, sizeof (pp->comm.cpu_id) == 4 ? "INT32" : "INT64");
316 collector_interface->writeLog (" <field name=\"TSTAMP\" uname=\"High resolution timestamp\" offset=\"%d\" type=\"%s\"/>\n",
317 &pp->comm.tstamp, sizeof (pp->comm.tstamp) == 4 ? "INT32" : "INT64");
318 collector_interface->writeLog (" <field name=\"FRINFO\" offset=\"%d\" type=\"%s\"/>\n",
319 &pp->comm.frinfo, sizeof (pp->comm.frinfo) == 4 ? "INT32" : "INT64");
320 collector_interface->writeLog (" <field name=\"IOTYPE\" uname=\"IO trace function type\" offset=\"%d\" type=\"%s\"/>\n",
321 &pp->iotype, sizeof (pp->iotype) == 4 ? "INT32" : "INT64");
322 collector_interface->writeLog (" <field name=\"IOFD\" uname=\"File descriptor\" offset=\"%d\" type=\"%s\"/>\n",
323 &pp->fd, sizeof (pp->fd) == 4 ? "INT32" : "INT64");
324 collector_interface->writeLog (" <field name=\"IONBYTE\" uname=\"Number of bytes\" offset=\"%d\" type=\"%s\"/>\n",
325 &pp->nbyte, sizeof (pp->nbyte) == 4 ? "INT32" : "INT64");
326 collector_interface->writeLog (" <field name=\"IORQST\" uname=\"Time of IO requested\" offset=\"%d\" type=\"%s\"/>\n",
327 &pp->requested, sizeof (pp->requested) == 4 ? "INT32" : "INT64");
328 collector_interface->writeLog (" <field name=\"IOOFD\" uname=\"Original file descriptor\" offset=\"%d\" type=\"%s\"/>\n",
329 &pp->ofd, sizeof (pp->ofd) == 4 ? "INT32" : "INT64");
330 collector_interface->writeLog (" <field name=\"IOFSTYPE\" uname=\"File system type\" offset=\"%d\" type=\"%s\"/>\n",
331 &pp->fstype, sizeof (pp->fstype) == 4 ? "INT32" : "INT64");
332 collector_interface->writeLog (" <field name=\"IOFNAME\" uname=\"File name\" offset=\"%d\" type=\"%s\"/>\n",
333 &pp->fname, "STRING");
334 collector_interface->writeLog (" </profpckt>\n");
335 collector_interface->writeLog ("</profile>\n");
336 return COL_ERROR_NONE;
337 }
338
339 static int
340 start_data_collection (void)
341 {
342 io_mode = 1;
343 Tprintf (0, "iotrace: start_data_collection\n");
344 return 0;
345 }
346
347 static int
348 stop_data_collection (void)
349 {
350 io_mode = 0;
351 Tprintf (0, "iotrace: stop_data_collection\n");
352 return 0;
353 }
354
355 static int
356 close_experiment (void)
357 {
358 io_mode = 0;
359 io_key = COLLECTOR_TSD_INVALID_KEY;
360 if (io_heap != NULL)
361 {
362 collector_interface->deleteHeap (io_heap);
363 io_heap = NULL;
364 }
365 Tprintf (0, "iotrace: close_experiment\n");
366 return 0;
367 }
368
369 static int
370 detach_experiment (void)
371 {
372 /* fork child. Clean up state but don't write to experiment */
373 io_mode = 0;
374 io_key = COLLECTOR_TSD_INVALID_KEY;
375 if (io_heap != NULL)
376 {
377 collector_interface->deleteHeap (io_heap);
378 io_heap = NULL;
379 }
380 Tprintf (0, "iotrace: detach_experiment\n");
381 return 0;
382 }
383
384 static int
385 init_fopen (void *dlflag)
386 {
387 __real_fopen_2_17 = dlvsym (dlflag, "fopen", "GLIBC_2.17");
388 __real_fopen_2_2_5 = dlvsym (dlflag, "fopen", "GLIBC_2.2.5");
389 __real_fopen_2_1 = dlvsym (dlflag, "fopen", "GLIBC_2.1");
390 __real_fopen_2_0 = dlvsym (dlflag, "fopen", "GLIBC_2.0");
391 if (__real_fopen_2_17)
392 __real_fopen = __real_fopen_2_17;
393 else if (__real_fopen_2_2_5)
394 __real_fopen = __real_fopen_2_2_5;
395 else if (__real_fopen_2_1)
396 __real_fopen = __real_fopen_2_1;
397 else if (__real_fopen_2_0)
398 __real_fopen = __real_fopen_2_0;
399 else
400 __real_fopen = dlsym (dlflag, "fopen");
401 return __real_fopen ? 1 : 0;
402 }
403
404 static int
405 init_io_intf ()
406 {
407 void *dlflag;
408 int rc = 0;
409 /* if we detect recursion/reentrance, SEGV so we can get a stack */
410 static int init_io_intf_started;
411 static int init_io_intf_finished;
412 init_io_intf_started++;
413 if (!init_io_intf_finished && init_io_intf_started >= 3)
414 {
415 /* pull the plug if recursion occurs... */
416 abort ();
417 }
418
419 /* lookup fprint to print fatal error message */
420 void *ptr = dlsym (RTLD_NEXT, "fprintf");
421 if (ptr)
422 __real_fprintf = (int (*)(FILE*, const char*, ...)) ptr;
423 else
424 abort ();
425
426 dlflag = RTLD_NEXT;
427 if (init_fopen (dlflag) == 0)
428 {
429 if (init_fopen (RTLD_DEFAULT))
430 dlflag = RTLD_DEFAULT;
431 else
432 {
433 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT fopen\n");
434 rc = COL_ERROR_IOINIT;
435 }
436 }
437
438 __real_fgetpos64_2_17 = dlvsym (dlflag, "fgetpos64", "GLIBC_2.17");
439 __real_fgetpos64_2_2_5 = dlvsym (dlflag, "fgetpos64", "GLIBC_2.2.5");
440 __real_fgetpos64_2_2 = dlvsym (dlflag, "fgetpos64", "GLIBC_2.2");
441 __real_fgetpos64_2_1 = dlvsym (dlflag, "fgetpos64", "GLIBC_2.1");
442 if (__real_fgetpos64_2_17)
443 __real_fgetpos64 = __real_fgetpos64_2_17;
444 else if (__real_fgetpos64_2_2_5)
445 __real_fgetpos64 = __real_fgetpos64_2_2_5;
446 else if (__real_fgetpos64_2_2)
447 __real_fgetpos64 = __real_fgetpos64_2_2;
448 else if (__real_fgetpos64_2_1)
449 __real_fgetpos64 = __real_fgetpos64_2_1;
450 else
451 __real_fgetpos64 = dlsym (dlflag, "fgetpos64");
452
453 __real_fsetpos64_2_17 = dlvsym (dlflag, "fsetpos64", "GLIBC_2.17");
454 __real_fsetpos64_2_2_5 = dlvsym (dlflag, "fsetpos64", "GLIBC_2.2.5");
455 __real_fsetpos64_2_2 = dlvsym (dlflag, "fsetpos64", "GLIBC_2.2");
456 __real_fsetpos64_2_1 = dlvsym (dlflag, "fsetpos64", "GLIBC_2.1");
457 if (__real_fsetpos64_2_17)
458 __real_fsetpos64 = __real_fsetpos64_2_17;
459 else if (__real_fsetpos64_2_2_5)
460 __real_fsetpos64 = __real_fsetpos64_2_2_5;
461 else if (__real_fsetpos64_2_2)
462 __real_fsetpos64 = __real_fsetpos64_2_2;
463 else if (__real_fsetpos64_2_1)
464 __real_fsetpos64 = __real_fsetpos64_2_1;
465 else
466 __real_fsetpos64 = dlsym (dlflag, "fsetpos64");
467
468 __real_pread_2_2 = dlvsym (dlflag, "pread", "GLIBC_2.2");
469 if (__real_pread_2_2)
470 __real_pread = __real_pread_2_2;
471 else
472 __real_pread = dlsym (dlflag, "pread");
473 if (__real_pread == NULL)
474 {
475 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT pread\n");
476 rc = COL_ERROR_IOINIT;
477 }
478
479 __real_pwrite_2_2 = dlvsym (dlflag, "pwrite", "GLIBC_2.2");
480 if (__real_pwrite_2_2)
481 __real_pwrite = __real_pwrite_2_2;
482 else
483 __real_pwrite = dlsym (dlflag, "pwrite");
484 if (__real_pwrite == NULL)
485 {
486 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT pwrite\n");
487 rc = COL_ERROR_IOINIT;
488 }
489
490
491 __real_pwrite64_2_2 = dlvsym (dlflag, "pwrite64", "GLIBC_2.2");
492 if (__real_pwrite64_2_2)
493 __real_pwrite64 = __real_pwrite64_2_2;
494 else
495 __real_pwrite64 = dlsym (dlflag, "pwrite64");
496
497 __real_fclose_2_17 = dlvsym (dlflag, "fclose", "GLIBC_2.17");
498 __real_fclose_2_2_5 = dlvsym (dlflag, "fclose", "GLIBC_2.2.5");
499 __real_fclose_2_1 = dlvsym (dlflag, "fclose", "GLIBC_2.1");
500 __real_fclose_2_0 = dlvsym (dlflag, "fclose", "GLIBC_2.0");
501 if (__real_fclose_2_17)
502 __real_fclose = __real_fclose_2_17;
503 else if (__real_fclose_2_2_5)
504 __real_fclose = __real_fclose_2_2_5;
505 else if (__real_fclose_2_1)
506 __real_fclose = __real_fclose_2_1;
507 else if (__real_fclose_2_0)
508 __real_fclose = __real_fclose_2_0;
509 else
510 __real_fclose = dlsym (dlflag, "fclose");
511 if (__real_fclose == NULL)
512 {
513 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT fclose\n");
514 rc = COL_ERROR_IOINIT;
515 }
516
517 __real_fdopen_2_17 = dlvsym (dlflag, "fdopen", "GLIBC_2.17");
518 __real_fdopen_2_2_5 = dlvsym (dlflag, "fdopen", "GLIBC_2.2.5");
519 __real_fdopen_2_1 = dlvsym (dlflag, "fdopen", "GLIBC_2.1");
520 __real_fdopen_2_0 = dlvsym (dlflag, "fdopen", "GLIBC_2.0");
521 if (__real_fdopen_2_17)
522 __real_fdopen = __real_fdopen_2_17;
523 else if (__real_fdopen_2_2_5)
524 __real_fdopen = __real_fdopen_2_2_5;
525 else if (__real_fdopen_2_1)
526 __real_fdopen = __real_fdopen_2_1;
527 else if (__real_fdopen_2_0)
528 __real_fdopen = __real_fdopen_2_0;
529 else
530 __real_fdopen = dlsym (dlflag, "fdopen");
531 if (__real_fdopen == NULL)
532 {
533 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT fdopen\n");
534 rc = COL_ERROR_IOINIT;
535 }
536
537 __real_fgetpos_2_17 = dlvsym (dlflag, "fgetpos", "GLIBC_2.17");
538 __real_fgetpos_2_2_5 = dlvsym (dlflag, "fgetpos", "GLIBC_2.2.5");
539 __real_fgetpos_2_2 = dlvsym (dlflag, "fgetpos", "GLIBC_2.2");
540 __real_fgetpos_2_0 = dlvsym (dlflag, "fgetpos", "GLIBC_2.0");
541 if (__real_fgetpos_2_17)
542 __real_fgetpos = __real_fgetpos_2_17;
543 else if (__real_fgetpos_2_2_5)
544 __real_fgetpos = __real_fgetpos_2_2_5;
545 else if (__real_fgetpos_2_2)
546 __real_fgetpos = __real_fgetpos_2_2;
547 else if (__real_fgetpos_2_0)
548 __real_fgetpos = __real_fgetpos_2_0;
549 else
550 __real_fgetpos = dlsym (dlflag, "fgetpos");
551 if (__real_fgetpos == NULL)
552 {
553 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT fgetpos\n");
554 rc = COL_ERROR_IOINIT;
555 }
556
557 __real_fsetpos_2_17 = dlvsym (dlflag, "fsetpos", "GLIBC_2.17");
558 __real_fsetpos_2_2_5 = dlvsym (dlflag, "fsetpos", "GLIBC_2.2.5");
559 __real_fsetpos_2_2 = dlvsym (dlflag, "fsetpos", "GLIBC_2.2");
560 __real_fsetpos_2_0 = dlvsym (dlflag, "fsetpos", "GLIBC_2.0");
561 if (__real_fsetpos_2_17)
562 __real_fsetpos = __real_fsetpos_2_17;
563 else if (__real_fsetpos_2_2_5)
564 __real_fsetpos = __real_fsetpos_2_2_5;
565 else if (__real_fsetpos_2_2)
566 __real_fsetpos = __real_fsetpos_2_2;
567 else if (__real_fsetpos_2_0)
568 __real_fsetpos = __real_fsetpos_2_0;
569 else
570 __real_fsetpos = dlsym (dlflag, "fsetpos");
571 if (__real_fsetpos == NULL)
572 {
573 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT fsetpos\n");
574 rc = COL_ERROR_IOINIT;
575 }
576
577 __real_open = (int (*)(const char*, int, ...))dlsym (dlflag, "open");
578 if (__real_open == NULL)
579 {
580 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT open\n");
581 rc = COL_ERROR_IOINIT;
582 }
583
584 __real_open64_2_2 = dlvsym (dlflag, "open64", "GLIBC_2.2");
585 if (__real_open64_2_2)
586 __real_open64 = __real_open64_2_2;
587 else
588 __real_open64 = dlsym (dlflag, "open64");
589
590 __real_fcntl = (int (*)(int, int, ...))dlsym (dlflag, "fcntl");
591 if (__real_fcntl == NULL)
592 {
593 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT fcntl\n");
594 rc = COL_ERROR_IOINIT;
595 }
596
597 __real_openat = (int (*)(int, const char*, int, ...))dlsym (dlflag, "openat");
598 if (__real_openat == NULL)
599 {
600 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT openat\n");
601 rc = COL_ERROR_IOINIT;
602 }
603
604 __real_close = (int (*)(int))dlsym (dlflag, "close");
605 if (__real_close == NULL)
606 {
607 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT close\n");
608 rc = COL_ERROR_IOINIT;
609 }
610
611 __real_dup = (int (*)(int))dlsym (dlflag, "dup");
612 if (__real_dup == NULL)
613 {
614 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT dup\n");
615 rc = COL_ERROR_IOINIT;
616 }
617
618 __real_dup2 = (int (*)(int, int))dlsym (dlflag, "dup2");
619 if (__real_dup2 == NULL)
620 {
621 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT dup2\n");
622 rc = COL_ERROR_IOINIT;
623 }
624
625 __real_pipe = (int (*)(int[]))dlsym (dlflag, "pipe");
626 if (__real_pipe == NULL)
627 {
628 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT pipe\n");
629 rc = COL_ERROR_IOINIT;
630 }
631
632 __real_socket = (int (*)(int, int, int))dlsym (dlflag, "socket");
633 if (__real_socket == NULL)
634 {
635 __real_socket = (int (*)(int, int, int))dlsym (RTLD_NEXT, "socket");
636 if (__real_socket == NULL)
637 {
638 #if 0
639 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERXXX_IOINIT socket\n");
640 rc = COL_ERROR_IOINIT;
641 #endif
642 }
643 }
644
645 __real_mkstemp = (int (*)(char*))dlsym (dlflag, "mkstemp");
646 if (__real_mkstemp == NULL)
647 {
648 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT mkstemp\n");
649 rc = COL_ERROR_IOINIT;
650 }
651
652 __real_mkstemps = (int (*)(char*, int))dlsym (dlflag, "mkstemps");
653 if (__real_mkstemps == NULL)
654 {
655 #if 0
656 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERXXX_IOINIT mkstemps\n");
657 rc = COL_ERROR_IOINIT;
658 #endif
659 }
660
661 __real_creat = (int (*)(const char*, mode_t))dlsym (dlflag, "creat");
662 if (__real_creat == NULL)
663 {
664 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT creat\n");
665 rc = COL_ERROR_IOINIT;
666 }
667
668 __real_creat64 = (int (*)(const char*, mode_t))dlsym (dlflag, "creat64");
669 if (__real_creat64 == NULL)
670 {
671 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT creat64\n");
672 rc = COL_ERROR_IOINIT;
673 }
674
675 __real_read = (ssize_t (*)(int, void*, size_t))dlsym (dlflag, "read");
676 if (__real_read == NULL)
677 {
678 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT read\n");
679 rc = COL_ERROR_IOINIT;
680 }
681
682 __real_write = (ssize_t (*)(int, const void*, size_t))dlsym (dlflag, "write");
683 if (__real_write == NULL)
684 {
685 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT write\n");
686 rc = COL_ERROR_IOINIT;
687 }
688
689 __real_readv = (ssize_t (*)(int, const struct iovec*, int))dlsym (dlflag, "readv");
690 if (__real_readv == NULL)
691 {
692 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT readv\n");
693 rc = COL_ERROR_IOINIT;
694 }
695
696 __real_writev = (ssize_t (*)(int, const struct iovec*, int))dlsym (dlflag, "writev");
697 if (__real_writev == NULL)
698 {
699 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT writev\n");
700 rc = COL_ERROR_IOINIT;
701 }
702
703 __real_fread = (size_t (*)(void*, size_t, size_t, FILE*))dlsym (dlflag, "fread");
704 if (__real_fread == NULL)
705 {
706 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT fread\n");
707 rc = COL_ERROR_IOINIT;
708 }
709
710 __real_fwrite = (size_t (*)(const void*, size_t, size_t, FILE*))dlsym (dlflag, "fwrite");
711 if (__real_fwrite == NULL)
712 {
713 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT fwrite\n");
714 rc = COL_ERROR_IOINIT;
715 }
716
717 __real_pread = (ssize_t (*)(int, void*, size_t, off_t))dlsym (dlflag, "pread");
718 if (__real_pread == NULL)
719 {
720 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT pread\n");
721 rc = COL_ERROR_IOINIT;
722 }
723
724 __real_pwrite = (ssize_t (*)(int, const void*, size_t, off_t))dlsym (dlflag, "pwrite");
725 if (__real_pwrite == NULL)
726 {
727 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT pwrite\n");
728 rc = COL_ERROR_IOINIT;
729 }
730
731 __real_pwrite64 = (ssize_t (*)(int, const void*, size_t, off64_t))dlsym (dlflag, "pwrite64");
732 if (__real_pwrite64 == NULL)
733 {
734 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT pwrite64\n");
735 rc = COL_ERROR_IOINIT;
736 }
737
738 __real_fgets = (char* (*)(char*, int, FILE*))dlsym (dlflag, "fgets");
739 if (__real_fgets == NULL)
740 {
741 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT fgets\n");
742 rc = COL_ERROR_IOINIT;
743 }
744
745 __real_fputs = (int (*)(const char*, FILE*))dlsym (dlflag, "fputs");
746 if (__real_fputs == NULL)
747 {
748 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT fputs\n");
749 rc = COL_ERROR_IOINIT;
750 }
751
752 __real_fputc = (int (*)(int, FILE*))dlsym (dlflag, "fputc");
753 if (__real_fputc == NULL)
754 {
755 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT fputc\n");
756 rc = COL_ERROR_IOINIT;
757 }
758
759 __real_vfprintf = (int (*)(FILE*, const char*, va_list))dlsym (dlflag, "vfprintf");
760 if (__real_vfprintf == NULL)
761 {
762 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT vfprintf\n");
763 rc = COL_ERROR_IOINIT;
764 }
765
766
767 __real_lseek = (off_t (*)(int, off_t, int))dlsym (dlflag, "lseek");
768 if (__real_lseek == NULL)
769 {
770 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT lseek\n");
771 rc = COL_ERROR_IOINIT;
772 }
773
774 __real_llseek = (offset_t (*)(int, offset_t, int))dlsym (dlflag, "llseek");
775 if (__real_llseek == NULL)
776 {
777 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT llseek\n");
778 rc = COL_ERROR_IOINIT;
779 }
780
781 __real_chmod = (int (*)(const char*, mode_t))dlsym (dlflag, "chmod");
782 if (__real_chmod == NULL)
783 {
784 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT chmod\n");
785 rc = COL_ERROR_IOINIT;
786 }
787
788 __real_access = (int (*)(const char*, int))dlsym (dlflag, "access");
789 if (__real_access == NULL)
790 {
791 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT access\n");
792 rc = COL_ERROR_IOINIT;
793 }
794
795 __real_rename = (int (*)(const char*, const char*))dlsym (dlflag, "rename");
796 if (__real_rename == NULL)
797 {
798 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT rename\n");
799 rc = COL_ERROR_IOINIT;
800 }
801
802 __real_mkdir = (int (*)(const char*, mode_t))dlsym (dlflag, "mkdir");
803 if (__real_mkdir == NULL)
804 {
805 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT mkdir\n");
806 rc = COL_ERROR_IOINIT;
807 }
808
809 __real_getdents = (int (*)(int, struct dirent*, size_t))dlsym (dlflag, "getdents");
810 if (__real_getdents == NULL)
811 {
812 #if 0
813 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERXXX_IOINIT getdents\n");
814 rc = COL_ERROR_IOINIT;
815 #endif
816 }
817
818 __real_unlink = (int (*)(const char*))dlsym (dlflag, "unlink");
819 if (__real_unlink == NULL)
820 {
821 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT unlink\n");
822 rc = COL_ERROR_IOINIT;
823 }
824
825 __real_fseek = (int (*)(FILE*, long, int))dlsym (dlflag, "fseek");
826 if (__real_fseek == NULL)
827 {
828 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT fseek\n");
829 rc = COL_ERROR_IOINIT;
830 }
831
832 __real_rewind = (void (*)(FILE*))dlsym (dlflag, "rewind");
833 if (__real_rewind == NULL)
834 {
835 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT rewind\n");
836 rc = COL_ERROR_IOINIT;
837 }
838
839 __real_ftell = (long (*)(FILE*))dlsym (dlflag, "ftell");
840 if (__real_ftell == NULL)
841 {
842 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT ftell\n");
843 rc = COL_ERROR_IOINIT;
844 }
845
846 __real_fsync = (int (*)(int))dlsym (dlflag, "fsync");
847 if (__real_fsync == NULL)
848 {
849 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT fsync\n");
850 rc = COL_ERROR_IOINIT;
851 }
852
853 __real_readdir = (struct dirent * (*)(DIR*))dlsym (dlflag, "readdir");
854 if (__real_readdir == NULL)
855 {
856 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT readdir\n");
857 rc = COL_ERROR_IOINIT;
858 }
859
860 __real_flock = (int (*)(int, int))dlsym (dlflag, "flock");
861 if (__real_flock == NULL)
862 {
863 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT flock\n");
864 rc = COL_ERROR_IOINIT;
865 }
866
867 __real_lockf = (int (*)(int, int, off_t))dlsym (dlflag, "lockf");
868 if (__real_lockf == NULL)
869 {
870 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT lockf\n");
871 rc = COL_ERROR_IOINIT;
872 }
873
874 __real_fflush = (int (*)(FILE*))dlsym (dlflag, "fflush");
875 if (__real_fflush == NULL)
876 {
877 CALL_REAL (fprintf)(stderr, "iotrace_init COL_ERROR_IOINIT fflush\n");
878 rc = COL_ERROR_IOINIT;
879 }
880
881 init_io_intf_finished++;
882 return rc;
883 }
884
885 static void
886 write_io_packet (int fd, ssize_t ret, hrtime_t reqt, int iotype)
887 {
888 IOTrace_packet iopkt;
889 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
890 iopkt.comm.tsize = sizeof (IOTrace_packet);
891 iopkt.comm.tstamp = gethrtime ();
892 iopkt.requested = reqt;
893 iopkt.iotype = iotype;
894 iopkt.fd = fd;
895 iopkt.nbyte = ret;
896 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl,
897 iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
898 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
899 }
900
901 /*------------------------------------------------------------- open */
902 int
903 open (const char *path, int oflag, ...)
904 {
905 int *guard;
906 int fd;
907 void *packet;
908 IOTrace_packet *iopkt;
909 mode_t mode;
910 va_list ap;
911 size_t sz;
912 unsigned pktSize;
913
914 va_start (ap, oflag);
915 mode = va_arg (ap, mode_t);
916 va_end (ap);
917
918 if (NULL_PTR (open))
919 init_io_intf ();
920
921 if (CHCK_REENTRANCE (guard) || path == NULL)
922 return CALL_REAL (open)(path, oflag, mode);
923 PUSH_REENTRANCE (guard);
924 hrtime_t reqt = gethrtime ();
925 fd = CALL_REAL (open)(path, oflag, mode);
926 if (RECHCK_REENTRANCE (guard))
927 {
928 POP_REENTRANCE (guard);
929 return fd;
930 }
931 hrtime_t grnt = gethrtime ();
932 sz = collector_strlen (path);
933 pktSize = sizeof (IOTrace_packet) + sz;
934 pktSize = collector_align_pktsize (pktSize);
935 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
936 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
937 if (packet != NULL)
938 {
939 iopkt = (IOTrace_packet *) packet;
940 collector_memset (iopkt, 0, pktSize);
941 iopkt->comm.tsize = pktSize;
942 iopkt->comm.tstamp = grnt;
943 iopkt->requested = reqt;
944 if (fd != -1)
945 iopkt->iotype = OPEN_TRACE;
946 else
947 iopkt->iotype = OPEN_TRACE_ERROR;
948 iopkt->fd = fd;
949 iopkt->fstype = collector_fstype (path);
950 collector_strncpy (&(iopkt->fname), path, sz);
951 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
952 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
953 collector_interface->freeCSize (io_heap, packet, pktSize);
954 }
955 else
956 {
957 Tprintf (0, "iotrace: ERROR: open cannot allocate memory\n");
958 return -1;
959 }
960 POP_REENTRANCE (guard);
961 return fd;
962 }
963
964 /*------------------------------------------------------------- open64 */
965 static int
966 gprofng_open64 (int(real_open64) (const char *, int, ...),
967 const char *path, int oflag, mode_t mode)
968 {
969 int *guard;
970 int fd;
971 void *packet;
972 IOTrace_packet *iopkt;
973 size_t sz;
974 unsigned pktSize;
975 if (CHCK_REENTRANCE (guard) || path == NULL)
976 return real_open64 (path, oflag, mode);
977 PUSH_REENTRANCE (guard);
978 hrtime_t reqt = gethrtime ();
979 fd = real_open64 (path, oflag, mode);
980 if (RECHCK_REENTRANCE (guard) || path == NULL)
981 {
982 POP_REENTRANCE (guard);
983 return fd;
984 }
985 hrtime_t grnt = gethrtime ();
986 sz = collector_strlen (path);
987 pktSize = sizeof (IOTrace_packet) + sz;
988 pktSize = collector_align_pktsize (pktSize);
989 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
990 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
991 if (packet != NULL)
992 {
993 iopkt = (IOTrace_packet *) packet;
994 collector_memset (iopkt, 0, pktSize);
995 iopkt->comm.tsize = pktSize;
996 iopkt->comm.tstamp = grnt;
997 iopkt->requested = reqt;
998 if (fd != -1)
999 iopkt->iotype = OPEN_TRACE;
1000 else
1001 iopkt->iotype = OPEN_TRACE_ERROR;
1002 iopkt->fd = fd;
1003 iopkt->fstype = collector_fstype (path);
1004 collector_strncpy (&(iopkt->fname), path, sz);
1005 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl,
1006 iopkt->comm.tstamp, FRINFO_FROM_STACK_ARG, &iopkt);
1007 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
1008 collector_interface->freeCSize (io_heap, packet, pktSize);
1009 }
1010 else
1011 {
1012 Tprintf (0, "iotrace: ERROR: open64 cannot allocate memory\n");
1013 return -1;
1014 }
1015 POP_REENTRANCE (guard);
1016 return fd;
1017 }
1018
1019 #define DCL_OPEN64(dcl_f) \
1020 int dcl_f (const char *path, int oflag, ...) \
1021 { \
1022 if (__real_open64 == NULL) \
1023 init_io_intf (); \
1024 mode_t mode; \
1025 va_list ap; \
1026 va_start (ap, oflag); \
1027 mode = va_arg (ap, mode_t); \
1028 va_end (ap); \
1029 return gprofng_open64 (__real_open64, path, oflag, mode); \
1030 }
1031
1032 DCL_FUNC_VER (DCL_OPEN64, open64_2_2, open64@GLIBC_2.2)
1033 DCL_OPEN64 (open64)
1034
1035
1036 #define F_ERROR_ARG 0
1037 #define F_INT_ARG 1
1038 #define F_LONG_ARG 2
1039 #define F_VOID_ARG 3
1040
1041 /*
1042 * The following macro is not defined in the
1043 * older versions of Linux.
1044 * #define F_DUPFD_CLOEXEC 1030
1045 *
1046 * Instead use the command that is defined below
1047 * until we start compiling mpmt on the newer
1048 * versions of Linux.
1049 */
1050 #define TMP_F_DUPFD_CLOEXEC 1030
1051
1052 /*------------------------------------------------------------- fcntl */
1053 int
1054 fcntl (int fildes, int cmd, ...)
1055 {
1056 int *guard;
1057 int fd = 0;
1058 IOTrace_packet iopkt;
1059 long long_arg = 0;
1060 int int_arg = 0;
1061 int which_arg = F_ERROR_ARG;
1062 va_list ap;
1063 switch (cmd)
1064 {
1065 case F_DUPFD:
1066 case TMP_F_DUPFD_CLOEXEC:
1067 case F_SETFD:
1068 case F_SETFL:
1069 case F_SETOWN:
1070 case F_SETSIG:
1071 case F_SETLEASE:
1072 case F_NOTIFY:
1073 case F_SETLK:
1074 case F_SETLKW:
1075 case F_GETLK:
1076 va_start (ap, cmd);
1077 long_arg = va_arg (ap, long);
1078 va_end (ap);
1079 which_arg = F_LONG_ARG;
1080 break;
1081 case F_GETFD:
1082 case F_GETFL:
1083 case F_GETOWN:
1084 case F_GETLEASE:
1085 case F_GETSIG:
1086 which_arg = F_VOID_ARG;
1087 break;
1088 }
1089 if (NULL_PTR (fcntl))
1090 init_io_intf ();
1091 if (CHCK_REENTRANCE (guard))
1092 {
1093 switch (which_arg)
1094 {
1095 case F_INT_ARG:
1096 return CALL_REAL (fcntl)(fildes, cmd, int_arg);
1097 case F_LONG_ARG:
1098 return CALL_REAL (fcntl)(fildes, cmd, long_arg);
1099 case F_VOID_ARG:
1100 return CALL_REAL (fcntl)(fildes, cmd);
1101 case F_ERROR_ARG:
1102 Tprintf (0, "iotrace: ERROR: Unsupported fcntl command\n");
1103 return -1;
1104 }
1105 return -1;
1106 }
1107 if (cmd != F_DUPFD && cmd != TMP_F_DUPFD_CLOEXEC)
1108 {
1109 switch (which_arg)
1110 {
1111 case F_INT_ARG:
1112 return CALL_REAL (fcntl)(fildes, cmd, int_arg);
1113 case F_LONG_ARG:
1114 return CALL_REAL (fcntl)(fildes, cmd, long_arg);
1115 case F_VOID_ARG:
1116 return CALL_REAL (fcntl)(fildes, cmd);
1117 case F_ERROR_ARG:
1118 Tprintf (0, "iotrace: ERROR: Unsupported fcntl command\n");
1119 return -1;
1120 }
1121 return -1;
1122 }
1123 PUSH_REENTRANCE (guard);
1124 hrtime_t reqt = gethrtime ();
1125 switch (cmd)
1126 {
1127 case F_DUPFD:
1128 case TMP_F_DUPFD_CLOEXEC:
1129 fd = CALL_REAL (fcntl)(fildes, cmd, long_arg);
1130 break;
1131 }
1132 if (RECHCK_REENTRANCE (guard))
1133 {
1134 POP_REENTRANCE (guard);
1135 return fd;
1136 }
1137 hrtime_t grnt = gethrtime ();
1138 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1139 iopkt.comm.tsize = sizeof (IOTrace_packet);
1140 iopkt.comm.tstamp = grnt;
1141 iopkt.requested = reqt;
1142 if (fd != -1)
1143 iopkt.iotype = OPEN_TRACE;
1144 else
1145 iopkt.iotype = OPEN_TRACE_ERROR;
1146 iopkt.fd = fd;
1147 iopkt.ofd = fildes;
1148 iopkt.fstype = UNKNOWNFS_TYPE;
1149 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1150 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1151 POP_REENTRANCE (guard);
1152 return fd;
1153 }
1154
1155 /*------------------------------------------------------------- openat */
1156 int
1157 openat (int fildes, const char *path, int oflag, ...)
1158 {
1159 int *guard;
1160 int fd;
1161 void *packet;
1162 IOTrace_packet *iopkt;
1163 mode_t mode;
1164 va_list ap;
1165 size_t sz;
1166 unsigned pktSize;
1167
1168 va_start (ap, oflag);
1169 mode = va_arg (ap, mode_t);
1170 va_end (ap);
1171 if (NULL_PTR (openat))
1172 init_io_intf ();
1173 if (CHCK_REENTRANCE (guard) || path == NULL)
1174 return CALL_REAL (openat)(fildes, path, oflag, mode);
1175 PUSH_REENTRANCE (guard);
1176 hrtime_t reqt = gethrtime ();
1177 fd = CALL_REAL (openat)(fildes, path, oflag, mode);
1178 if (RECHCK_REENTRANCE (guard))
1179 {
1180 POP_REENTRANCE (guard);
1181 return fd;
1182 }
1183 hrtime_t grnt = gethrtime ();
1184 sz = collector_strlen (path);
1185 pktSize = sizeof (IOTrace_packet) + sz;
1186 pktSize = collector_align_pktsize (pktSize);
1187 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
1188 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
1189 if (packet != NULL)
1190 {
1191 iopkt = (IOTrace_packet *) packet;
1192 collector_memset (iopkt, 0, pktSize);
1193 iopkt->comm.tsize = pktSize;
1194 iopkt->comm.tstamp = grnt;
1195 iopkt->requested = reqt;
1196 if (fd != -1)
1197 iopkt->iotype = OPEN_TRACE;
1198 else
1199 iopkt->iotype = OPEN_TRACE_ERROR;
1200 iopkt->fd = fd;
1201 iopkt->fstype = collector_fstype (path);
1202 collector_strncpy (&(iopkt->fname), path, sz);
1203 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1204 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
1205 collector_interface->freeCSize (io_heap, packet, pktSize);
1206 }
1207 else
1208 {
1209 Tprintf (0, "iotrace: ERROR: openat cannot allocate memory\n");
1210 return -1;
1211 }
1212 POP_REENTRANCE (guard);
1213 return fd;
1214 }
1215
1216 /*------------------------------------------------------------- creat */
1217 int
1218 creat (const char *path, mode_t mode)
1219 {
1220 int *guard;
1221 int fd;
1222 void *packet;
1223 IOTrace_packet *iopkt;
1224 size_t sz;
1225 unsigned pktSize;
1226 if (NULL_PTR (creat))
1227 init_io_intf ();
1228 if (CHCK_REENTRANCE (guard) || path == NULL)
1229 return CALL_REAL (creat)(path, mode);
1230 PUSH_REENTRANCE (guard);
1231 hrtime_t reqt = gethrtime ();
1232 fd = CALL_REAL (creat)(path, mode);
1233 if (RECHCK_REENTRANCE (guard))
1234 {
1235 POP_REENTRANCE (guard);
1236 return fd;
1237 }
1238 hrtime_t grnt = gethrtime ();
1239 sz = collector_strlen (path);
1240 pktSize = sizeof (IOTrace_packet) + sz;
1241 pktSize = collector_align_pktsize (pktSize);
1242 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
1243 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
1244 if (packet != NULL)
1245 {
1246 iopkt = (IOTrace_packet *) packet;
1247 collector_memset (iopkt, 0, pktSize);
1248 iopkt->comm.tsize = pktSize;
1249 iopkt->comm.tstamp = grnt;
1250 iopkt->requested = reqt;
1251 if (fd != -1)
1252 iopkt->iotype = OPEN_TRACE;
1253 else
1254 iopkt->iotype = OPEN_TRACE_ERROR;
1255 iopkt->fd = fd;
1256 iopkt->fstype = collector_fstype (path);
1257 collector_strncpy (&(iopkt->fname), path, sz);
1258 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1259 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
1260 collector_interface->freeCSize (io_heap, packet, pktSize);
1261 }
1262 else
1263 {
1264 Tprintf (0, "iotrace: ERROR: creat cannot allocate memory\n");
1265 return -1;
1266 }
1267 POP_REENTRANCE (guard);
1268 return fd;
1269 }
1270
1271 /*------------------------------------------------------------- creat64 */
1272 #if WSIZE(32) && !defined(__USE_LARGEFILE64)
1273 int
1274 creat64 (const char *path, mode_t mode)
1275 {
1276 int *guard;
1277 int fd;
1278 void *packet;
1279 IOTrace_packet *iopkt;
1280 size_t sz;
1281 unsigned pktSize;
1282
1283 if (NULL_PTR (creat64))
1284 init_io_intf ();
1285 if (CHCK_REENTRANCE (guard) || path == NULL)
1286 return CALL_REAL (creat64)(path, mode);
1287 PUSH_REENTRANCE (guard);
1288 hrtime_t reqt = gethrtime ();
1289 fd = CALL_REAL (creat64)(path, mode);
1290 if (RECHCK_REENTRANCE (guard))
1291 {
1292 POP_REENTRANCE (guard);
1293 return fd;
1294 }
1295 hrtime_t grnt = gethrtime ();
1296 sz = collector_strlen (path);
1297 pktSize = sizeof (IOTrace_packet) + sz;
1298 pktSize = collector_align_pktsize (pktSize);
1299 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
1300 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
1301 if (packet != NULL)
1302 {
1303 iopkt = (IOTrace_packet *) packet;
1304 collector_memset (iopkt, 0, pktSize);
1305 iopkt->comm.tsize = pktSize;
1306 iopkt->comm.tstamp = grnt;
1307 iopkt->requested = reqt;
1308 if (fd != -1)
1309 iopkt->iotype = OPEN_TRACE;
1310 else
1311 iopkt->iotype = OPEN_TRACE_ERROR;
1312 iopkt->fd = fd;
1313 iopkt->fstype = collector_fstype (path);
1314 collector_strncpy (&(iopkt->fname), path, sz);
1315 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1316 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
1317 collector_interface->freeCSize (io_heap, packet, pktSize);
1318 }
1319 else
1320 {
1321 Tprintf (0, "iotrace: ERROR: creat64 cannot allocate memory\n");
1322 return -1;
1323 }
1324 POP_REENTRANCE (guard);
1325 return fd;
1326 }
1327 #endif
1328
1329 /*------------------------------------------------------------- mkstemp */
1330 int
1331 mkstemp (char *template)
1332 {
1333 int *guard;
1334 int fd;
1335 void *packet;
1336 IOTrace_packet *iopkt;
1337 size_t sz;
1338 unsigned pktSize;
1339 if (NULL_PTR (mkstemp))
1340 init_io_intf ();
1341 if (CHCK_REENTRANCE (guard) || template == NULL)
1342 return CALL_REAL (mkstemp)(template);
1343 PUSH_REENTRANCE (guard);
1344 hrtime_t reqt = gethrtime ();
1345 fd = CALL_REAL (mkstemp)(template);
1346 if (RECHCK_REENTRANCE (guard))
1347 {
1348 POP_REENTRANCE (guard);
1349 return fd;
1350 }
1351 hrtime_t grnt = gethrtime ();
1352 sz = collector_strlen (template);
1353 pktSize = sizeof (IOTrace_packet) + sz;
1354 pktSize = collector_align_pktsize (pktSize);
1355 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
1356 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
1357 if (packet != NULL)
1358 {
1359 iopkt = (IOTrace_packet *) packet;
1360 collector_memset (iopkt, 0, pktSize);
1361 iopkt->comm.tsize = pktSize;
1362 iopkt->comm.tstamp = grnt;
1363 iopkt->requested = reqt;
1364 if (fd != -1)
1365 iopkt->iotype = OPEN_TRACE;
1366 else
1367 iopkt->iotype = OPEN_TRACE_ERROR;
1368 iopkt->fd = fd;
1369 iopkt->fstype = collector_fstype (template);
1370 collector_strncpy (&(iopkt->fname), template, sz);
1371 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1372 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
1373 collector_interface->freeCSize (io_heap, packet, pktSize);
1374 }
1375 else
1376 {
1377 Tprintf (0, "iotrace: ERROR: mkstemp cannot allocate memory\n");
1378 return -1;
1379 }
1380 POP_REENTRANCE (guard);
1381 return fd;
1382 }
1383
1384 /*------------------------------------------------------------- mkstemps */
1385 int
1386 mkstemps (char *template, int slen)
1387 {
1388 int *guard;
1389 int fd;
1390 void *packet;
1391 IOTrace_packet *iopkt;
1392 size_t sz;
1393 unsigned pktSize;
1394 if (NULL_PTR (mkstemps))
1395 init_io_intf ();
1396 if (CHCK_REENTRANCE (guard) || template == NULL)
1397 return CALL_REAL (mkstemps)(template, slen);
1398 PUSH_REENTRANCE (guard);
1399 hrtime_t reqt = gethrtime ();
1400 fd = CALL_REAL (mkstemps)(template, slen);
1401 if (RECHCK_REENTRANCE (guard))
1402 {
1403 POP_REENTRANCE (guard);
1404 return fd;
1405 }
1406 hrtime_t grnt = gethrtime ();
1407 sz = collector_strlen (template);
1408 pktSize = sizeof (IOTrace_packet) + sz;
1409 pktSize = collector_align_pktsize (pktSize);
1410 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
1411 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
1412 if (packet != NULL)
1413 {
1414 iopkt = (IOTrace_packet *) packet;
1415 collector_memset (iopkt, 0, pktSize);
1416 iopkt->comm.tsize = pktSize;
1417 iopkt->comm.tstamp = grnt;
1418 iopkt->requested = reqt;
1419 if (fd != -1)
1420 iopkt->iotype = OPEN_TRACE;
1421 else
1422 iopkt->iotype = OPEN_TRACE_ERROR;
1423 iopkt->fd = fd;
1424 iopkt->fstype = collector_fstype (template);
1425 collector_strncpy (&(iopkt->fname), template, sz);
1426 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1427 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
1428 collector_interface->freeCSize (io_heap, packet, pktSize);
1429 }
1430 else
1431 {
1432 Tprintf (0, "iotrace: ERROR: mkstemps cannot allocate memory\n");
1433 return -1;
1434 }
1435 POP_REENTRANCE (guard);
1436 return fd;
1437 }
1438
1439 /*------------------------------------------------------------- close */
1440 int
1441 close (int fildes)
1442 {
1443 int *guard;
1444 int stat;
1445 IOTrace_packet iopkt;
1446 if (NULL_PTR (close))
1447 init_io_intf ();
1448 if (CHCK_REENTRANCE (guard))
1449 return CALL_REAL (close)(fildes);
1450 PUSH_REENTRANCE (guard);
1451 hrtime_t reqt = gethrtime ();
1452 stat = CALL_REAL (close)(fildes);
1453 if (RECHCK_REENTRANCE (guard))
1454 {
1455 POP_REENTRANCE (guard);
1456 return stat;
1457 }
1458 hrtime_t grnt = gethrtime ();
1459 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1460 iopkt.comm.tsize = sizeof (IOTrace_packet);
1461 iopkt.comm.tstamp = grnt;
1462 iopkt.requested = reqt;
1463 if (stat == 0)
1464 iopkt.iotype = CLOSE_TRACE;
1465 else
1466 iopkt.iotype = CLOSE_TRACE_ERROR;
1467 iopkt.fd = fildes;
1468 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1469 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1470 POP_REENTRANCE (guard);
1471 return stat;
1472 }
1473
1474 /*------------------------------------------------------------- fopen */
1475 static FILE*
1476 gprofng_fopen (FILE*(real_fopen) (), const char *filename, const char *mode)
1477 {
1478 int *guard;
1479 FILE *fp = NULL;
1480 void *packet;
1481 IOTrace_packet *iopkt;
1482 size_t sz;
1483 unsigned pktSize;
1484 if (CHCK_REENTRANCE (guard) || filename == NULL)
1485 return real_fopen (filename, mode);
1486 PUSH_REENTRANCE (guard);
1487 hrtime_t reqt = gethrtime ();
1488
1489 fp = real_fopen (filename, mode);
1490 if (RECHCK_REENTRANCE (guard))
1491 {
1492 POP_REENTRANCE (guard);
1493 return fp;
1494 }
1495 hrtime_t grnt = gethrtime ();
1496 sz = collector_strlen (filename);
1497 pktSize = sizeof (IOTrace_packet) + sz;
1498 pktSize = collector_align_pktsize (pktSize);
1499 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
1500 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
1501 if (packet != NULL)
1502 {
1503 iopkt = (IOTrace_packet *) packet;
1504 collector_memset (iopkt, 0, pktSize);
1505 iopkt->comm.tsize = pktSize;
1506 iopkt->comm.tstamp = grnt;
1507 iopkt->requested = reqt;
1508 if (fp != NULL)
1509 {
1510 iopkt->iotype = OPEN_TRACE;
1511 iopkt->fd = fileno (fp);
1512 }
1513 else
1514 {
1515 iopkt->iotype = OPEN_TRACE_ERROR;
1516 iopkt->fd = -1;
1517 }
1518 iopkt->fstype = collector_fstype (filename);
1519 collector_strncpy (&(iopkt->fname), filename, sz);
1520 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl,
1521 iopkt->comm.tstamp, FRINFO_FROM_STACK_ARG, &iopkt);
1522 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
1523 collector_interface->freeCSize (io_heap, packet, pktSize);
1524 }
1525 else
1526 {
1527 Tprintf (0, "iotrace: ERROR: fopen cannot allocate memory\n");
1528 return NULL;
1529 }
1530 POP_REENTRANCE (guard);
1531 return fp;
1532 }
1533
1534 #define DCL_FOPEN(dcl_f) \
1535 FILE *dcl_f (const char *filename, const char *mode) \
1536 { \
1537 if (__real_fopen == NULL) \
1538 init_io_intf (); \
1539 return gprofng_fopen (__real_fopen, filename, mode); \
1540 }
1541
1542 DCL_FUNC_VER (DCL_FOPEN, fopen_2_17, fopen@GLIBC_2.17)
1543 DCL_FUNC_VER (DCL_FOPEN, fopen_2_2_5, fopen@GLIBC_2.2.5)
1544 DCL_FUNC_VER (DCL_FOPEN, fopen_2_1, fopen@GLIBC_2.1)
1545 DCL_FUNC_VER (DCL_FOPEN, fopen_2_0, fopen@GLIBC_2.0)
1546 DCL_FOPEN (fopen)
1547
1548 /*------------------------------------------------------------- fclose */
1549 static int
1550 gprofng_fclose (int(real_fclose) (), FILE *stream)
1551 {
1552 int *guard;
1553 int stat;
1554 IOTrace_packet iopkt;
1555 if (CHCK_REENTRANCE (guard) || stream == NULL)
1556 return real_fclose (stream);
1557 PUSH_REENTRANCE (guard);
1558 hrtime_t reqt = gethrtime ();
1559 stat = real_fclose (stream);
1560 if (RECHCK_REENTRANCE (guard))
1561 {
1562 POP_REENTRANCE (guard);
1563 return stat;
1564 }
1565 hrtime_t grnt = gethrtime ();
1566 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1567 iopkt.comm.tsize = sizeof (IOTrace_packet);
1568 iopkt.comm.tstamp = grnt;
1569 iopkt.requested = reqt;
1570 if (stat == 0)
1571 iopkt.iotype = CLOSE_TRACE;
1572 else
1573 iopkt.iotype = CLOSE_TRACE_ERROR;
1574 iopkt.fd = fileno (stream);
1575 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl,
1576 iopkt.comm.tstamp, FRINFO_FROM_STACK_ARG, &iopkt);
1577 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1578 POP_REENTRANCE (guard);
1579 return stat;
1580 }
1581
1582 #define DCL_FCLOSE(dcl_f) \
1583 int dcl_f (FILE *stream) \
1584 { \
1585 if (__real_fclose == NULL) \
1586 init_io_intf (); \
1587 return gprofng_fclose (__real_fclose, stream); \
1588 }
1589
1590 DCL_FUNC_VER (DCL_FCLOSE, fclose_2_17, fclose@GLIBC_2.17)
1591 DCL_FUNC_VER (DCL_FCLOSE, fclose_2_2_5, fclose@GLIBC_2.2.5)
1592 DCL_FUNC_VER (DCL_FCLOSE, fclose_2_1, fclose@GLIBC_2.1)
1593 DCL_FUNC_VER (DCL_FCLOSE, fclose_2_0, fclose@GLIBC_2.0)
1594 DCL_FCLOSE (fclose)
1595
1596 /*------------------------------------------------------------- fflush */
1597 int
1598 fflush (FILE *stream)
1599 {
1600 int *guard;
1601 int stat;
1602 IOTrace_packet iopkt;
1603 if (NULL_PTR (fflush))
1604 init_io_intf ();
1605 if (CHCK_REENTRANCE (guard))
1606 return CALL_REAL (fflush)(stream);
1607 PUSH_REENTRANCE (guard);
1608 hrtime_t reqt = gethrtime ();
1609 stat = CALL_REAL (fflush)(stream);
1610 if (RECHCK_REENTRANCE (guard))
1611 {
1612 POP_REENTRANCE (guard);
1613 return stat;
1614 }
1615 hrtime_t grnt = gethrtime ();
1616 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1617 iopkt.comm.tsize = sizeof (IOTrace_packet);
1618 iopkt.comm.tstamp = grnt;
1619 iopkt.requested = reqt;
1620 if (stat == 0)
1621 iopkt.iotype = OTHERIO_TRACE;
1622 else
1623 iopkt.iotype = OTHERIO_TRACE_ERROR;
1624 if (stream != NULL)
1625 iopkt.fd = fileno (stream);
1626 else
1627 iopkt.fd = -1;
1628 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1629 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1630 POP_REENTRANCE (guard);
1631 return stat;
1632 }
1633
1634 /*------------------------------------------------------------- fdopen */
1635 static FILE*
1636 gprofng_fdopen (FILE*(real_fdopen) (), int fildes, const char *mode)
1637 {
1638 int *guard;
1639 FILE *fp = NULL;
1640 IOTrace_packet iopkt;
1641 if (NULL_PTR (fdopen))
1642 init_io_intf ();
1643 if (CHCK_REENTRANCE (guard))
1644 return real_fdopen (fildes, mode);
1645 PUSH_REENTRANCE (guard);
1646 hrtime_t reqt = gethrtime ();
1647 fp = real_fdopen (fildes, mode);
1648 if (RECHCK_REENTRANCE (guard))
1649 {
1650 POP_REENTRANCE (guard);
1651 return fp;
1652 }
1653 hrtime_t grnt = gethrtime ();
1654 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1655 iopkt.comm.tsize = sizeof (IOTrace_packet);
1656 iopkt.comm.tstamp = grnt;
1657 iopkt.requested = reqt;
1658 if (fp != NULL)
1659 iopkt.iotype = OPEN_TRACE;
1660 else
1661 iopkt.iotype = OPEN_TRACE_ERROR;
1662 iopkt.fd = fildes;
1663 iopkt.fstype = UNKNOWNFS_TYPE;
1664 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl,
1665 iopkt.comm.tstamp, FRINFO_FROM_STACK_ARG, &iopkt);
1666 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1667 POP_REENTRANCE (guard);
1668 return fp;
1669 }
1670
1671 #define DCL_FDOPEN(dcl_f) \
1672 FILE *dcl_f (int fildes, const char *mode) \
1673 { \
1674 if (__real_fdopen == NULL) \
1675 init_io_intf (); \
1676 return gprofng_fdopen (__real_fdopen, fildes, mode); \
1677 }
1678
1679 DCL_FUNC_VER (DCL_FDOPEN, fdopen_2_17, fdopen@GLIBC_2.17)
1680 DCL_FUNC_VER (DCL_FDOPEN, fdopen_2_2_5, fdopen@GLIBC_2.2.5)
1681 DCL_FUNC_VER (DCL_FDOPEN, fdopen_2_1, fdopen@GLIBC_2.1)
1682 DCL_FUNC_VER (DCL_FDOPEN, fdopen_2_0, fdopen@GLIBC_2.0)
1683 DCL_FDOPEN (fdopen)
1684
1685 /*------------------------------------------------------------- dup */
1686 int
1687 dup (int fildes)
1688 {
1689 int *guard;
1690 int fd;
1691 IOTrace_packet iopkt;
1692 if (NULL_PTR (dup))
1693 init_io_intf ();
1694 if (CHCK_REENTRANCE (guard))
1695 return CALL_REAL (dup)(fildes);
1696 PUSH_REENTRANCE (guard);
1697 hrtime_t reqt = gethrtime ();
1698 fd = CALL_REAL (dup)(fildes);
1699 if (RECHCK_REENTRANCE (guard))
1700 {
1701 POP_REENTRANCE (guard);
1702 return fd;
1703 }
1704 hrtime_t grnt = gethrtime ();
1705 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1706 iopkt.comm.tsize = sizeof (IOTrace_packet);
1707 iopkt.comm.tstamp = grnt;
1708 iopkt.requested = reqt;
1709 if (fd != -1)
1710 iopkt.iotype = OPEN_TRACE;
1711 else
1712 iopkt.iotype = OPEN_TRACE_ERROR;
1713
1714 iopkt.fd = fd;
1715 iopkt.ofd = fildes;
1716 iopkt.fstype = UNKNOWNFS_TYPE;
1717 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1718 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1719 POP_REENTRANCE (guard);
1720 return fd;
1721 }
1722
1723 /*------------------------------------------------------------- dup2 */
1724 int
1725 dup2 (int fildes, int fildes2)
1726 {
1727 int *guard;
1728 int fd;
1729 IOTrace_packet iopkt;
1730 if (NULL_PTR (dup2))
1731 init_io_intf ();
1732 if (CHCK_REENTRANCE (guard))
1733 return CALL_REAL (dup2)(fildes, fildes2);
1734 PUSH_REENTRANCE (guard);
1735 hrtime_t reqt = gethrtime ();
1736 fd = CALL_REAL (dup2)(fildes, fildes2);
1737 if (RECHCK_REENTRANCE (guard))
1738 {
1739 POP_REENTRANCE (guard);
1740 return fd;
1741 }
1742 hrtime_t grnt = gethrtime ();
1743 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1744 iopkt.comm.tsize = sizeof (IOTrace_packet);
1745 iopkt.comm.tstamp = grnt;
1746 iopkt.requested = reqt;
1747 if (fd != -1)
1748 iopkt.iotype = OPEN_TRACE;
1749 else
1750 iopkt.iotype = OPEN_TRACE_ERROR;
1751 iopkt.fd = fd;
1752 iopkt.ofd = fildes;
1753 iopkt.fstype = UNKNOWNFS_TYPE;
1754 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1755 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1756 POP_REENTRANCE (guard);
1757 return fd;
1758 }
1759
1760 /*------------------------------------------------------------- pipe */
1761 int
1762 pipe (int fildes[2])
1763 {
1764 int *guard;
1765 int ret;
1766 IOTrace_packet iopkt;
1767 if (NULL_PTR (pipe))
1768 init_io_intf ();
1769 if (CHCK_REENTRANCE (guard))
1770 return CALL_REAL (pipe)(fildes);
1771 PUSH_REENTRANCE (guard);
1772 hrtime_t reqt = gethrtime ();
1773 ret = CALL_REAL (pipe)(fildes);
1774 if (RECHCK_REENTRANCE (guard))
1775 {
1776 POP_REENTRANCE (guard);
1777 return ret;
1778 }
1779 hrtime_t grnt = gethrtime ();
1780 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1781 iopkt.comm.tsize = sizeof (IOTrace_packet);
1782 iopkt.comm.tstamp = grnt;
1783 iopkt.requested = reqt;
1784 if (ret != -1)
1785 iopkt.iotype = OPEN_TRACE;
1786 else
1787 iopkt.iotype = OPEN_TRACE_ERROR;
1788 iopkt.fd = fildes[0];
1789 iopkt.fstype = UNKNOWNFS_TYPE;
1790 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1791 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1792 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1793 iopkt.comm.tsize = sizeof (IOTrace_packet);
1794 iopkt.comm.tstamp = grnt;
1795 iopkt.requested = reqt;
1796 if (ret != -1)
1797 iopkt.iotype = OPEN_TRACE;
1798 else
1799 iopkt.iotype = OPEN_TRACE_ERROR;
1800 iopkt.fd = fildes[1];
1801 iopkt.fstype = UNKNOWNFS_TYPE;
1802 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1803 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1804 POP_REENTRANCE (guard);
1805 return ret;
1806 }
1807
1808 /*------------------------------------------------------------- socket */
1809 int
1810 socket (int domain, int type, int protocol)
1811 {
1812 int *guard;
1813 int fd;
1814 IOTrace_packet iopkt;
1815 if (NULL_PTR (socket))
1816 init_io_intf ();
1817 if (CHCK_REENTRANCE (guard))
1818 return CALL_REAL (socket)(domain, type, protocol);
1819 PUSH_REENTRANCE (guard);
1820 hrtime_t reqt = gethrtime ();
1821 fd = CALL_REAL (socket)(domain, type, protocol);
1822 if (RECHCK_REENTRANCE (guard))
1823 {
1824 POP_REENTRANCE (guard);
1825 return fd;
1826 }
1827 hrtime_t grnt = gethrtime ();
1828 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1829 iopkt.comm.tsize = sizeof (IOTrace_packet);
1830 iopkt.comm.tstamp = grnt;
1831 iopkt.requested = reqt;
1832 if (fd != -1)
1833 iopkt.iotype = OPEN_TRACE;
1834 else
1835 iopkt.iotype = OPEN_TRACE_ERROR;
1836 iopkt.fd = fd;
1837 iopkt.fstype = UNKNOWNFS_TYPE;
1838 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1839 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1840 POP_REENTRANCE (guard);
1841 return fd;
1842 }
1843
1844 /*------------------------------------------------------------- read */
1845 ssize_t
1846 read (int fildes, void *buf, size_t nbyte)
1847 {
1848 int *guard;
1849 ssize_t ret;
1850 IOTrace_packet iopkt;
1851 if (NULL_PTR (read))
1852 init_io_intf ();
1853 if (CHCK_REENTRANCE (guard))
1854 return CALL_REAL (read)(fildes, buf, nbyte);
1855 PUSH_REENTRANCE (guard);
1856 hrtime_t reqt = gethrtime ();
1857 ret = CALL_REAL (read)(fildes, buf, nbyte);
1858 if (RECHCK_REENTRANCE (guard))
1859 {
1860 POP_REENTRANCE (guard);
1861 return ret;
1862 }
1863 hrtime_t grnt = gethrtime ();
1864 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
1865 iopkt.comm.tsize = sizeof ( IOTrace_packet);
1866 iopkt.comm.tstamp = grnt;
1867 iopkt.requested = reqt;
1868 if (ret >= 0)
1869 iopkt.iotype = READ_TRACE;
1870 else
1871 iopkt.iotype = READ_TRACE_ERROR;
1872 iopkt.fd = fildes;
1873 iopkt.nbyte = ret;
1874 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1875 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1876 POP_REENTRANCE (guard);
1877 return ret;
1878 }
1879
1880 /*------------------------------------------------------------- write */
1881 ssize_t
1882 write (int fildes, const void *buf, size_t nbyte)
1883 {
1884 int *guard;
1885 ssize_t ret;
1886 IOTrace_packet iopkt;
1887 if (NULL_PTR (write))
1888 init_io_intf ();
1889 if (CHCK_REENTRANCE (guard))
1890 return CALL_REAL (write)(fildes, buf, nbyte);
1891 PUSH_REENTRANCE (guard);
1892 hrtime_t reqt = gethrtime ();
1893 ret = CALL_REAL (write)(fildes, buf, nbyte);
1894 if (RECHCK_REENTRANCE (guard))
1895 {
1896 POP_REENTRANCE (guard);
1897 return ret;
1898 }
1899 hrtime_t grnt = gethrtime ();
1900 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
1901 iopkt.comm.tsize = sizeof ( IOTrace_packet);
1902 iopkt.comm.tstamp = grnt;
1903 iopkt.requested = reqt;
1904 if (ret >= 0)
1905 iopkt.iotype = WRITE_TRACE;
1906 else
1907 iopkt.iotype = WRITE_TRACE_ERROR;
1908 iopkt.fd = fildes;
1909 iopkt.nbyte = ret;
1910 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1911 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1912 POP_REENTRANCE (guard);
1913 return ret;
1914 }
1915
1916 /*------------------------------------------------------------- readv */
1917 ssize_t
1918 readv (int fildes, const struct iovec *iov, int iovcnt)
1919 {
1920 int *guard;
1921 ssize_t ret;
1922 IOTrace_packet iopkt;
1923 if (NULL_PTR (readv))
1924 init_io_intf ();
1925 if (CHCK_REENTRANCE (guard))
1926 return CALL_REAL (readv)(fildes, iov, iovcnt);
1927 PUSH_REENTRANCE (guard);
1928 hrtime_t reqt = gethrtime ();
1929 ret = CALL_REAL (readv)(fildes, iov, iovcnt);
1930 if (RECHCK_REENTRANCE (guard))
1931 {
1932 POP_REENTRANCE (guard);
1933 return ret;
1934 }
1935 hrtime_t grnt = gethrtime ();
1936 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
1937 iopkt.comm.tsize = sizeof ( IOTrace_packet);
1938 iopkt.comm.tstamp = grnt;
1939 iopkt.requested = reqt;
1940 if (ret >= 0)
1941 iopkt.iotype = READ_TRACE;
1942 else
1943 iopkt.iotype = READ_TRACE_ERROR;
1944 iopkt.fd = fildes;
1945 iopkt.nbyte = ret;
1946 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1947 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1948 POP_REENTRANCE (guard);
1949 return ret;
1950 }
1951
1952 /*------------------------------------------------------------- writev */
1953 ssize_t
1954 writev (int fildes, const struct iovec *iov, int iovcnt)
1955 {
1956 int *guard;
1957 ssize_t ret;
1958 IOTrace_packet iopkt;
1959 if (NULL_PTR (writev))
1960 init_io_intf ();
1961 if (CHCK_REENTRANCE (guard))
1962 return CALL_REAL (writev)(fildes, iov, iovcnt);
1963 PUSH_REENTRANCE (guard);
1964 hrtime_t reqt = gethrtime ();
1965 ret = CALL_REAL (writev)(fildes, iov, iovcnt);
1966 if (RECHCK_REENTRANCE (guard))
1967 {
1968 POP_REENTRANCE (guard);
1969 return ret;
1970 }
1971 hrtime_t grnt = gethrtime ();
1972 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
1973 iopkt.comm.tsize = sizeof ( IOTrace_packet);
1974 iopkt.comm.tstamp = grnt;
1975 iopkt.requested = reqt;
1976 if (ret >= 0)
1977 iopkt.iotype = WRITE_TRACE;
1978 else
1979 iopkt.iotype = WRITE_TRACE_ERROR;
1980 iopkt.fd = fildes;
1981 iopkt.nbyte = ret;
1982 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1983 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1984 POP_REENTRANCE (guard);
1985 return ret;
1986 }
1987
1988 /*------------------------------------------------------------- fread */
1989 size_t
1990 fread (void *ptr, size_t size, size_t nitems, FILE *stream)
1991 {
1992 int *guard;
1993 size_t ret;
1994 IOTrace_packet iopkt;
1995 if (NULL_PTR (fread))
1996 init_io_intf ();
1997 if (CHCK_REENTRANCE (guard) || stream == NULL)
1998 return CALL_REAL (fread)(ptr, size, nitems, stream);
1999 PUSH_REENTRANCE (guard);
2000 hrtime_t reqt = gethrtime ();
2001 ret = CALL_REAL (fread)(ptr, size, nitems, stream);
2002 if (RECHCK_REENTRANCE (guard))
2003 {
2004 POP_REENTRANCE (guard);
2005 return ret;
2006 }
2007 hrtime_t grnt = gethrtime ();
2008 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
2009 iopkt.comm.tsize = sizeof ( IOTrace_packet);
2010 iopkt.comm.tstamp = grnt;
2011 iopkt.requested = reqt;
2012 if (ferror (stream) == 0)
2013 {
2014 iopkt.iotype = READ_TRACE;
2015 iopkt.nbyte = ret * size;
2016 }
2017 else
2018 {
2019 iopkt.iotype = READ_TRACE_ERROR;
2020 iopkt.nbyte = 0;
2021 }
2022 iopkt.fd = fileno (stream);
2023 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2024 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2025 POP_REENTRANCE (guard);
2026 return ret;
2027 }
2028
2029 /*------------------------------------------------------------- fwrite */
2030 size_t
2031 fwrite (const void *ptr, size_t size, size_t nitems, FILE *stream)
2032 {
2033 int *guard;
2034 size_t ret;
2035 IOTrace_packet iopkt;
2036 if (NULL_PTR (fwrite))
2037 init_io_intf ();
2038 if (CHCK_REENTRANCE (guard) || stream == NULL)
2039 return CALL_REAL (fwrite)(ptr, size, nitems, stream);
2040 PUSH_REENTRANCE (guard);
2041 hrtime_t reqt = gethrtime ();
2042 ret = CALL_REAL (fwrite)(ptr, size, nitems, stream);
2043 if (RECHCK_REENTRANCE (guard))
2044 {
2045 POP_REENTRANCE (guard);
2046 return ret;
2047 }
2048 hrtime_t grnt = gethrtime ();
2049 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
2050 iopkt.comm.tsize = sizeof ( IOTrace_packet);
2051 iopkt.comm.tstamp = grnt;
2052 iopkt.requested = reqt;
2053 if (ferror (stream) == 0)
2054 {
2055 iopkt.iotype = WRITE_TRACE;
2056 iopkt.nbyte = ret * size;
2057 }
2058 else
2059 {
2060 iopkt.iotype = WRITE_TRACE_ERROR;
2061 iopkt.nbyte = 0;
2062 }
2063 iopkt.fd = fileno (stream);
2064 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2065 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2066 POP_REENTRANCE (guard);
2067 return ret;
2068 }
2069
2070 /*------------------------------------------------------------- pread */
2071 static ssize_t
2072 gprofng_pread (ssize_t(real_pread) (int, void *, size_t, off_t),
2073 int fildes, void *buf, size_t nbyte, off_t offset)
2074 {
2075 int *guard;
2076 ssize_t ret;
2077 IOTrace_packet iopkt;
2078 if (CHCK_REENTRANCE (guard))
2079 return real_pread (fildes, buf, nbyte, offset);
2080 PUSH_REENTRANCE (guard);
2081 hrtime_t reqt = gethrtime ();
2082 ret = real_pread (fildes, buf, nbyte, offset);
2083 if (RECHCK_REENTRANCE (guard))
2084 {
2085 POP_REENTRANCE (guard);
2086 return ret;
2087 }
2088 hrtime_t grnt = gethrtime ();
2089 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
2090 iopkt.comm.tsize = sizeof ( IOTrace_packet);
2091 iopkt.comm.tstamp = grnt;
2092 iopkt.requested = reqt;
2093 if (ret >= 0)
2094 iopkt.iotype = READ_TRACE;
2095 else
2096 iopkt.iotype = READ_TRACE_ERROR;
2097 iopkt.fd = fildes;
2098 iopkt.nbyte = ret;
2099 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl,
2100 iopkt.comm.tstamp, FRINFO_FROM_STACK_ARG, &iopkt);
2101 collector_interface->writeDataRecord (io_hndl, (Common_packet*) &iopkt);
2102 POP_REENTRANCE (guard);
2103 return ret;
2104 }
2105
2106 #define DCL_PREAD(dcl_f) \
2107 ssize_t dcl_f (int fildes, void *buf, size_t nbyte, off_t offset) \
2108 { \
2109 if (__real_pread == NULL) \
2110 init_io_intf (); \
2111 return gprofng_pread (__real_pread, fildes, buf, nbyte, offset); \
2112 }
2113
2114 DCL_FUNC_VER (DCL_PREAD, pread_2_2, pread@GLIBC_2.2)
2115 DCL_PREAD (pread)
2116
2117 /*------------------------------------------------------------- pwrite */
2118
2119 #if !defined(__MUSL_LIBC)
2120 // map interposed symbol versions
2121
2122 SYMVER_ATTRIBUTE (__collector_pwrite_2_2, pwrite@GLIBC_2.2)
2123 int
2124 __collector_pwrite_2_2 (int fildes, const void *buf, size_t nbyte, off_t offset)
2125 {
2126 int *guard;
2127 if (NULL_PTR (pwrite_2_2))
2128 init_io_intf ();
2129 if (CHCK_REENTRANCE (guard))
2130 return CALL_REAL (pwrite_2_2)(fildes, buf, nbyte, offset);
2131 PUSH_REENTRANCE (guard);
2132 hrtime_t reqt = gethrtime ();
2133 ssize_t ret = CALL_REAL (pwrite_2_2)(fildes, buf, nbyte, offset);
2134 if (RECHCK_REENTRANCE (guard))
2135 {
2136 POP_REENTRANCE (guard);
2137 return ret;
2138 }
2139 write_io_packet (fildes, ret, reqt, ret >= 0 ? WRITE_TRACE : WRITE_TRACE_ERROR);
2140 POP_REENTRANCE (guard);
2141 return ret;
2142 }
2143
2144 #endif
2145 ssize_t
2146 pwrite (int fildes, const void *buf, size_t nbyte, off_t offset)
2147 {
2148 int *guard;
2149 if (NULL_PTR (pwrite))
2150 init_io_intf ();
2151 if (CHCK_REENTRANCE (guard))
2152 return CALL_REAL (pwrite)(fildes, buf, nbyte, offset);
2153 PUSH_REENTRANCE (guard);
2154 hrtime_t reqt = gethrtime ();
2155 ssize_t ret = CALL_REAL (pwrite)(fildes, buf, nbyte, offset);
2156 if (RECHCK_REENTRANCE (guard))
2157 {
2158 POP_REENTRANCE (guard);
2159 return ret;
2160 }
2161 write_io_packet (fildes, ret, reqt, ret >= 0 ? WRITE_TRACE : WRITE_TRACE_ERROR);
2162 POP_REENTRANCE (guard);
2163 return ret;
2164 }
2165
2166 /*------------------------------------------------------------- pwrite64 */
2167 #if WSIZE(32) && !defined(__USE_FILE_OFFSET64)
2168 #if !defined(__MUSL_LIBC)
2169 // map interposed symbol versions
2170
2171 SYMVER_ATTRIBUTE (__collector_pwrite64_2_2, pwrite64@GLIBC_2.2)
2172 ssize_t
2173 __collector_pwrite64_2_2 (int fildes, const void *buf, size_t nbyte, off64_t offset)
2174 {
2175 int *guard;
2176 if (NULL_PTR (pwrite64_2_2))
2177 init_io_intf ();
2178 if (CHCK_REENTRANCE (guard))
2179 return CALL_REAL (pwrite64_2_2)(fildes, buf, nbyte, offset);
2180 PUSH_REENTRANCE (guard);
2181 hrtime_t reqt = gethrtime ();
2182 ssize_t ret = CALL_REAL (pwrite64_2_2)(fildes, buf, nbyte, offset);
2183 if (RECHCK_REENTRANCE (guard))
2184 {
2185 POP_REENTRANCE (guard);
2186 return ret;
2187 }
2188 write_io_packet (fildes, ret, reqt, ret >= 0 ? WRITE_TRACE : WRITE_TRACE_ERROR);
2189 POP_REENTRANCE (guard);
2190 return ret;
2191 }
2192 #endif
2193
2194 ssize_t
2195 pwrite64 (int fildes, const void *buf, size_t nbyte, off64_t offset)
2196 {
2197 int *guard;
2198 if (NULL_PTR (pwrite64))
2199 init_io_intf ();
2200 if (CHCK_REENTRANCE (guard))
2201 return CALL_REAL (pwrite64)(fildes, buf, nbyte, offset);
2202 PUSH_REENTRANCE (guard);
2203 hrtime_t reqt = gethrtime ();
2204 ssize_t ret = CALL_REAL (pwrite64)(fildes, buf, nbyte, offset);
2205 if (RECHCK_REENTRANCE (guard))
2206 {
2207 POP_REENTRANCE (guard);
2208 return ret;
2209 }
2210 write_io_packet (fildes, ret, reqt, ret >= 0 ? WRITE_TRACE : WRITE_TRACE_ERROR);
2211 POP_REENTRANCE (guard);
2212 return ret;
2213 }
2214 #endif
2215
2216 /*------------------------------------------------------------- fgets */
2217 char*
2218 fgets (char *s, int n, FILE *stream)
2219 {
2220 int *guard;
2221 char *ptr;
2222 IOTrace_packet iopkt;
2223 if (NULL_PTR (fgets))
2224 init_io_intf ();
2225 if (CHCK_REENTRANCE (guard) || stream == NULL)
2226 return CALL_REAL (fgets)(s, n, stream);
2227 PUSH_REENTRANCE (guard);
2228 hrtime_t reqt = gethrtime ();
2229 ptr = CALL_REAL (fgets)(s, n, stream);
2230 if (RECHCK_REENTRANCE (guard))
2231 {
2232 POP_REENTRANCE (guard);
2233 return ptr;
2234 }
2235 int error = errno;
2236 hrtime_t grnt = gethrtime ();
2237 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
2238 iopkt.comm.tsize = sizeof ( IOTrace_packet);
2239 iopkt.comm.tstamp = grnt;
2240 iopkt.requested = reqt;
2241 if (ptr != NULL)
2242 {
2243 iopkt.iotype = READ_TRACE;
2244 iopkt.nbyte = collector_strlen (ptr);
2245 }
2246 else if (ptr == NULL && error != EAGAIN && error != EBADF && error != EINTR &&
2247 error != EIO && error != EOVERFLOW && error != ENOMEM && error != ENXIO)
2248 {
2249 iopkt.iotype = READ_TRACE;
2250 iopkt.nbyte = 0;
2251 }
2252 else
2253 iopkt.iotype = READ_TRACE_ERROR;
2254 iopkt.fd = fileno (stream);
2255 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2256 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2257 POP_REENTRANCE (guard);
2258 return ptr;
2259 }
2260
2261 /*------------------------------------------------------------- fputs */
2262 int
2263 fputs (const char *s, FILE *stream)
2264 {
2265 int *guard;
2266 int ret;
2267 IOTrace_packet iopkt;
2268 if (NULL_PTR (fputs))
2269 init_io_intf ();
2270 if (CHCK_REENTRANCE (guard) || stream == NULL)
2271 return CALL_REAL (fputs)(s, stream);
2272 PUSH_REENTRANCE (guard);
2273 hrtime_t reqt = gethrtime ();
2274 ret = CALL_REAL (fputs)(s, stream);
2275 if (RECHCK_REENTRANCE (guard))
2276 {
2277 POP_REENTRANCE (guard);
2278 return ret;
2279 }
2280 hrtime_t grnt = gethrtime ();
2281 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
2282 iopkt.comm.tsize = sizeof ( IOTrace_packet);
2283 iopkt.comm.tstamp = grnt;
2284 iopkt.requested = reqt;
2285 if (ret != EOF)
2286 {
2287 iopkt.iotype = WRITE_TRACE;
2288 iopkt.nbyte = ret;
2289 }
2290 else
2291 {
2292 iopkt.iotype = WRITE_TRACE_ERROR;
2293 iopkt.nbyte = 0;
2294 }
2295 iopkt.fd = fileno (stream);
2296 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2297 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2298 POP_REENTRANCE (guard);
2299 return ret;
2300 }
2301
2302 /*------------------------------------------------------------- fputc */
2303 int
2304 fputc (int c, FILE *stream)
2305 {
2306 int *guard;
2307 int ret;
2308 IOTrace_packet iopkt;
2309 if (NULL_PTR (fputc))
2310 init_io_intf ();
2311 if (CHCK_REENTRANCE (guard) || stream == NULL)
2312 return CALL_REAL (fputc)(c, stream);
2313 PUSH_REENTRANCE (guard);
2314 hrtime_t reqt = gethrtime ();
2315 ret = CALL_REAL (fputc)(c, stream);
2316 if (RECHCK_REENTRANCE (guard))
2317 {
2318 POP_REENTRANCE (guard);
2319 return ret;
2320 }
2321 hrtime_t grnt = gethrtime ();
2322 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
2323 iopkt.comm.tsize = sizeof ( IOTrace_packet);
2324 iopkt.comm.tstamp = grnt;
2325 iopkt.requested = reqt;
2326 if (ret != EOF)
2327 {
2328 iopkt.iotype = WRITE_TRACE;
2329 iopkt.nbyte = ret;
2330 }
2331 else
2332 {
2333 iopkt.iotype = WRITE_TRACE_ERROR;
2334 iopkt.nbyte = 0;
2335 }
2336 iopkt.fd = fileno (stream);
2337 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2338 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2339 POP_REENTRANCE (guard);
2340 return ret;
2341 }
2342
2343 /*------------------------------------------------------------- fprintf */
2344 int
2345 fprintf (FILE *stream, const char *format, ...)
2346 {
2347 int *guard;
2348 int ret;
2349 IOTrace_packet iopkt;
2350 va_list ap;
2351 va_start (ap, format);
2352 if (NULL_PTR (fprintf))
2353 init_io_intf ();
2354 if (NULL_PTR (vfprintf))
2355 init_io_intf ();
2356 if (CHCK_REENTRANCE (guard) || stream == NULL)
2357 {
2358 ret = CALL_REAL (vfprintf)(stream, format, ap);
2359 va_end (ap);
2360 return ret;
2361 }
2362 PUSH_REENTRANCE (guard);
2363 hrtime_t reqt = gethrtime ();
2364 ret = CALL_REAL (vfprintf)(stream, format, ap);
2365 va_end (ap);
2366 if (RECHCK_REENTRANCE (guard))
2367 {
2368 POP_REENTRANCE (guard);
2369 return ret;
2370 }
2371 hrtime_t grnt = gethrtime ();
2372 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
2373 iopkt.comm.tsize = sizeof ( IOTrace_packet);
2374 iopkt.comm.tstamp = grnt;
2375 iopkt.requested = reqt;
2376 if (ret >= 0)
2377 iopkt.iotype = WRITE_TRACE;
2378 else
2379 iopkt.iotype = WRITE_TRACE_ERROR;
2380 iopkt.fd = fileno (stream);
2381 iopkt.nbyte = ret;
2382 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2383 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2384 POP_REENTRANCE (guard);
2385 return ret;
2386 }
2387
2388 /*------------------------------------------------------------- vfprintf */
2389 int
2390 vfprintf (FILE *stream, const char *format, va_list ap)
2391 {
2392 int *guard;
2393 int ret;
2394 IOTrace_packet iopkt;
2395 if (NULL_PTR (vfprintf))
2396 init_io_intf ();
2397 if (CHCK_REENTRANCE (guard) || stream == NULL)
2398 return CALL_REAL (vfprintf)(stream, format, ap);
2399 PUSH_REENTRANCE (guard);
2400 hrtime_t reqt = gethrtime ();
2401 ret = CALL_REAL (vfprintf)(stream, format, ap);
2402 if (RECHCK_REENTRANCE (guard))
2403 {
2404 POP_REENTRANCE (guard);
2405 return ret;
2406 }
2407 hrtime_t grnt = gethrtime ();
2408 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
2409 iopkt.comm.tsize = sizeof ( IOTrace_packet);
2410 iopkt.comm.tstamp = grnt;
2411 iopkt.requested = reqt;
2412 if (ret >= 0)
2413 iopkt.iotype = WRITE_TRACE;
2414 else
2415 iopkt.iotype = WRITE_TRACE_ERROR;
2416 iopkt.fd = fileno (stream);
2417 iopkt.nbyte = ret;
2418 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2419 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2420 POP_REENTRANCE (guard);
2421 return ret;
2422 }
2423
2424 /*------------------------------------------------------------- lseek */
2425 off_t
2426 lseek (int fildes, off_t offset, int whence)
2427 {
2428 int *guard;
2429 off_t ret;
2430 IOTrace_packet iopkt;
2431 if (NULL_PTR (lseek))
2432 init_io_intf ();
2433 if (CHCK_REENTRANCE (guard))
2434 return CALL_REAL (lseek)(fildes, offset, whence);
2435 PUSH_REENTRANCE (guard);
2436 hrtime_t reqt = gethrtime ();
2437 ret = CALL_REAL (lseek)(fildes, offset, whence);
2438 if (RECHCK_REENTRANCE (guard))
2439 {
2440 POP_REENTRANCE (guard);
2441 return ret;
2442 }
2443 hrtime_t grnt = gethrtime ();
2444 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
2445 iopkt.comm.tsize = sizeof (IOTrace_packet);
2446 iopkt.comm.tstamp = grnt;
2447 iopkt.requested = reqt;
2448 if (ret != -1)
2449 iopkt.iotype = OTHERIO_TRACE;
2450 else
2451 iopkt.iotype = OTHERIO_TRACE_ERROR;
2452 iopkt.fd = fildes;
2453 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2454 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2455 POP_REENTRANCE (guard);
2456 return ret;
2457 }
2458
2459 /*------------------------------------------------------------- llseek */
2460 offset_t
2461 llseek (int fildes, offset_t offset, int whence)
2462 {
2463 int *guard;
2464 offset_t ret;
2465 IOTrace_packet iopkt;
2466 if (NULL_PTR (llseek))
2467 init_io_intf ();
2468 if (CHCK_REENTRANCE (guard))
2469 return CALL_REAL (llseek)(fildes, offset, whence);
2470 PUSH_REENTRANCE (guard);
2471 hrtime_t reqt = gethrtime ();
2472 ret = CALL_REAL (llseek)(fildes, offset, whence);
2473 if (RECHCK_REENTRANCE (guard))
2474 {
2475 POP_REENTRANCE (guard);
2476 return ret;
2477 }
2478 hrtime_t grnt = gethrtime ();
2479 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
2480 iopkt.comm.tsize = sizeof (IOTrace_packet);
2481 iopkt.comm.tstamp = grnt;
2482 iopkt.requested = reqt;
2483 if (ret != -1)
2484 iopkt.iotype = OTHERIO_TRACE;
2485 else
2486 iopkt.iotype = OTHERIO_TRACE_ERROR;
2487 iopkt.fd = fildes;
2488 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2489 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2490 POP_REENTRANCE (guard);
2491 return ret;
2492 }
2493
2494 /*------------------------------------------------------------- chmod */
2495 int
2496 chmod (const char *path, mode_t mode)
2497 {
2498 int *guard;
2499 int ret;
2500 void *packet;
2501 IOTrace_packet *iopkt;
2502 size_t sz;
2503 unsigned pktSize;
2504 if (NULL_PTR (chmod))
2505 init_io_intf ();
2506 if (CHCK_REENTRANCE (guard) || path == NULL)
2507 return CALL_REAL (chmod)(path, mode);
2508 PUSH_REENTRANCE (guard);
2509 hrtime_t reqt = gethrtime ();
2510 ret = CALL_REAL (chmod)(path, mode);
2511 if (RECHCK_REENTRANCE (guard))
2512 {
2513 POP_REENTRANCE (guard);
2514 return ret;
2515 }
2516 hrtime_t grnt = gethrtime ();
2517 sz = collector_strlen (path);
2518 pktSize = sizeof (IOTrace_packet) + sz;
2519 pktSize = collector_align_pktsize (pktSize);
2520 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
2521 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
2522 if (packet != NULL)
2523 {
2524 iopkt = (IOTrace_packet *) packet;
2525 collector_memset (iopkt, 0, pktSize);
2526 iopkt->comm.tsize = pktSize;
2527 iopkt->comm.tstamp = grnt;
2528 iopkt->requested = reqt;
2529 if (ret != -1)
2530 iopkt->iotype = OTHERIO_TRACE;
2531 else
2532 iopkt->iotype = OTHERIO_TRACE_ERROR;
2533 collector_strncpy (&(iopkt->fname), path, sz);
2534 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2535 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
2536 collector_interface->freeCSize (io_heap, packet, pktSize);
2537 }
2538 else
2539 {
2540 Tprintf (0, "iotrace: ERROR: chmod cannot allocate memory\n");
2541 return 0;
2542 }
2543 POP_REENTRANCE (guard);
2544 return ret;
2545 }
2546
2547 /*------------------------------------------------------------- access */
2548 int
2549 access (const char *path, int amode)
2550 {
2551 int *guard;
2552 int ret;
2553 void *packet;
2554 IOTrace_packet *iopkt;
2555 size_t sz;
2556 unsigned pktSize;
2557 if (NULL_PTR (access))
2558 init_io_intf ();
2559 if (CHCK_REENTRANCE (guard) || path == NULL)
2560 return CALL_REAL (access)(path, amode);
2561 PUSH_REENTRANCE (guard);
2562 hrtime_t reqt = gethrtime ();
2563 ret = CALL_REAL (access)(path, amode);
2564 if (RECHCK_REENTRANCE (guard))
2565 {
2566 POP_REENTRANCE (guard);
2567 return ret;
2568 }
2569 hrtime_t grnt = gethrtime ();
2570 sz = collector_strlen (path);
2571 pktSize = sizeof (IOTrace_packet) + sz;
2572 pktSize = collector_align_pktsize (pktSize);
2573 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
2574 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
2575 if (packet != NULL)
2576 {
2577 iopkt = (IOTrace_packet *) packet;
2578 collector_memset (iopkt, 0, pktSize);
2579 iopkt->comm.tsize = pktSize;
2580 iopkt->comm.tstamp = grnt;
2581 iopkt->requested = reqt;
2582 if (ret != -1)
2583 iopkt->iotype = OTHERIO_TRACE;
2584 else
2585 iopkt->iotype = OTHERIO_TRACE_ERROR;
2586 collector_strncpy (&(iopkt->fname), path, sz);
2587 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2588 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
2589 collector_interface->freeCSize (io_heap, packet, pktSize);
2590 }
2591 else
2592 {
2593 Tprintf (0, "iotrace: ERROR: access cannot allocate memory\n");
2594 return 0;
2595 }
2596 POP_REENTRANCE (guard);
2597 return ret;
2598 }
2599
2600 /*------------------------------------------------------------- rename */
2601 int
2602 rename (const char *old, const char *new)
2603 {
2604 int *guard;
2605 int ret;
2606 void *packet;
2607 IOTrace_packet *iopkt;
2608 size_t sz;
2609 unsigned pktSize;
2610 if (NULL_PTR (rename))
2611 init_io_intf ();
2612 if (CHCK_REENTRANCE (guard) || new == NULL)
2613 return CALL_REAL (rename)(old, new);
2614 PUSH_REENTRANCE (guard);
2615 hrtime_t reqt = gethrtime ();
2616 ret = CALL_REAL (rename)(old, new);
2617 if (RECHCK_REENTRANCE (guard))
2618 {
2619 POP_REENTRANCE (guard);
2620 return ret;
2621 }
2622 hrtime_t grnt = gethrtime ();
2623 sz = collector_strlen (new);
2624 pktSize = sizeof (IOTrace_packet) + sz;
2625 pktSize = collector_align_pktsize (pktSize);
2626 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
2627 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
2628 if (packet != NULL)
2629 {
2630 iopkt = (IOTrace_packet *) packet;
2631 collector_memset (iopkt, 0, pktSize);
2632 iopkt->comm.tsize = pktSize;
2633 iopkt->comm.tstamp = grnt;
2634 iopkt->requested = reqt;
2635 if (ret != -1)
2636 iopkt->iotype = OTHERIO_TRACE;
2637 else
2638 iopkt->iotype = OTHERIO_TRACE_ERROR;
2639 collector_strncpy (&(iopkt->fname), new, sz);
2640 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2641 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
2642 collector_interface->freeCSize (io_heap, packet, pktSize);
2643 }
2644 else
2645 {
2646 Tprintf (0, "iotrace: ERROR: rename cannot allocate memory\n");
2647 return 0;
2648 }
2649 POP_REENTRANCE (guard);
2650 return ret;
2651 }
2652
2653 /*------------------------------------------------------------- mkdir */
2654 int
2655 mkdir (const char *path, mode_t mode)
2656 {
2657 int *guard;
2658 int ret;
2659 void *packet;
2660 IOTrace_packet *iopkt;
2661 size_t sz;
2662 unsigned pktSize;
2663 if (NULL_PTR (mkdir))
2664 init_io_intf ();
2665 if (CHCK_REENTRANCE (guard) || path == NULL)
2666 return CALL_REAL (mkdir)(path, mode);
2667 PUSH_REENTRANCE (guard);
2668 hrtime_t reqt = gethrtime ();
2669 ret = CALL_REAL (mkdir)(path, mode);
2670 if (RECHCK_REENTRANCE (guard))
2671 {
2672 POP_REENTRANCE (guard);
2673 return ret;
2674 }
2675 hrtime_t grnt = gethrtime ();
2676 sz = collector_strlen (path);
2677 pktSize = sizeof (IOTrace_packet) + sz;
2678 pktSize = collector_align_pktsize (pktSize);
2679 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
2680 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
2681 if (packet != NULL)
2682 {
2683 iopkt = (IOTrace_packet *) packet;
2684 collector_memset (iopkt, 0, pktSize);
2685 iopkt->comm.tsize = pktSize;
2686 iopkt->comm.tstamp = grnt;
2687 iopkt->requested = reqt;
2688 if (ret != -1)
2689 iopkt->iotype = OTHERIO_TRACE;
2690 else
2691 iopkt->iotype = OTHERIO_TRACE_ERROR;
2692 collector_strncpy (&(iopkt->fname), path, sz);
2693 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2694 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
2695 collector_interface->freeCSize (io_heap, packet, pktSize);
2696 }
2697 else
2698 {
2699 Tprintf (0, "iotrace: ERROR: mkdir cannot allocate memory\n");
2700 return 0;
2701 }
2702 POP_REENTRANCE (guard);
2703 return ret;
2704 }
2705
2706 /*------------------------------------------------------------- getdents */
2707 int
2708 getdents (int fildes, struct dirent *buf, size_t nbyte)
2709 {
2710 int *guard;
2711 int ret;
2712 IOTrace_packet iopkt;
2713 if (NULL_PTR (getdents))
2714 init_io_intf ();
2715 if (CHCK_REENTRANCE (guard))
2716 return CALL_REAL (getdents)(fildes, buf, nbyte);
2717 PUSH_REENTRANCE (guard);
2718 hrtime_t reqt = gethrtime ();
2719 ret = CALL_REAL (getdents)(fildes, buf, nbyte);
2720 if (RECHCK_REENTRANCE (guard))
2721 {
2722 POP_REENTRANCE (guard);
2723 return ret;
2724 }
2725 hrtime_t grnt = gethrtime ();
2726 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
2727 iopkt.comm.tsize = sizeof (IOTrace_packet);
2728 iopkt.comm.tstamp = grnt;
2729 iopkt.requested = reqt;
2730 if (ret != -1)
2731 iopkt.iotype = OTHERIO_TRACE;
2732 else
2733 iopkt.iotype = OTHERIO_TRACE_ERROR;
2734 iopkt.fd = fildes;
2735 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2736 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2737 POP_REENTRANCE (guard);
2738 return ret;
2739 }
2740
2741 /*------------------------------------------------------------- unlink */
2742 int
2743 unlink (const char *path)
2744 {
2745 int *guard;
2746 int ret;
2747 void *packet;
2748 IOTrace_packet *iopkt;
2749 size_t sz;
2750 unsigned pktSize;
2751 if (NULL_PTR (unlink))
2752 init_io_intf ();
2753 if (CHCK_REENTRANCE (guard) || path == NULL)
2754 return CALL_REAL (unlink)(path);
2755 PUSH_REENTRANCE (guard);
2756 hrtime_t reqt = gethrtime ();
2757 ret = CALL_REAL (unlink)(path);
2758 if (RECHCK_REENTRANCE (guard))
2759 {
2760 POP_REENTRANCE (guard);
2761 return ret;
2762 }
2763 hrtime_t grnt = gethrtime ();
2764 sz = collector_strlen (path);
2765 pktSize = sizeof (IOTrace_packet) + sz;
2766 pktSize = collector_align_pktsize (pktSize);
2767 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
2768 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
2769 if (packet != NULL)
2770 {
2771 iopkt = (IOTrace_packet *) packet;
2772 collector_memset (iopkt, 0, pktSize);
2773 iopkt->comm.tsize = pktSize;
2774 iopkt->comm.tstamp = grnt;
2775 iopkt->requested = reqt;
2776 if (ret != -1)
2777 iopkt->iotype = OTHERIO_TRACE;
2778 else
2779 iopkt->iotype = OTHERIO_TRACE_ERROR;
2780 collector_strncpy (&(iopkt->fname), path, sz);
2781 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2782 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
2783 collector_interface->freeCSize (io_heap, packet, pktSize);
2784 }
2785 else
2786 {
2787 Tprintf (0, "iotrace: ERROR: unlink cannot allocate memory\n");
2788 return 0;
2789 }
2790 POP_REENTRANCE (guard);
2791 return ret;
2792 }
2793
2794 /*------------------------------------------------------------- fseek */
2795 int
2796 fseek (FILE *stream, long offset, int whence)
2797 {
2798 int *guard;
2799 int ret;
2800 IOTrace_packet iopkt;
2801 if (NULL_PTR (fseek))
2802 init_io_intf ();
2803 if (CHCK_REENTRANCE (guard) || stream == NULL)
2804 return CALL_REAL (fseek)(stream, offset, whence);
2805 PUSH_REENTRANCE (guard);
2806 hrtime_t reqt = gethrtime ();
2807 ret = CALL_REAL (fseek)(stream, offset, whence);
2808 if (RECHCK_REENTRANCE (guard))
2809 {
2810 POP_REENTRANCE (guard);
2811 return ret;
2812 }
2813 hrtime_t grnt = gethrtime ();
2814 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
2815 iopkt.comm.tsize = sizeof (IOTrace_packet);
2816 iopkt.comm.tstamp = grnt;
2817 iopkt.requested = reqt;
2818 if (ret != -1)
2819 iopkt.iotype = OTHERIO_TRACE;
2820 else
2821 iopkt.iotype = OTHERIO_TRACE_ERROR;
2822 iopkt.fd = fileno (stream);
2823 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2824 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2825 POP_REENTRANCE (guard);
2826 return ret;
2827 }
2828
2829 /*------------------------------------------------------------- rewind */
2830 void
2831 rewind (FILE *stream)
2832 {
2833 int *guard;
2834 IOTrace_packet iopkt;
2835 if (NULL_PTR (rewind))
2836 init_io_intf ();
2837 if (CHCK_REENTRANCE (guard) || stream == NULL)
2838 {
2839 CALL_REAL (rewind)(stream);
2840 return;
2841 }
2842 PUSH_REENTRANCE (guard);
2843 hrtime_t reqt = gethrtime ();
2844 CALL_REAL (rewind)(stream);
2845 if (RECHCK_REENTRANCE (guard))
2846 {
2847 POP_REENTRANCE (guard);
2848 return;
2849 }
2850 hrtime_t grnt = gethrtime ();
2851 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
2852 iopkt.comm.tsize = sizeof (IOTrace_packet);
2853 iopkt.comm.tstamp = grnt;
2854 iopkt.requested = reqt;
2855 iopkt.iotype = OTHERIO_TRACE;
2856 iopkt.fd = fileno (stream);
2857 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2858 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2859 POP_REENTRANCE (guard);
2860 }
2861
2862 /*------------------------------------------------------------- ftell */
2863 long
2864 ftell (FILE *stream)
2865 {
2866 int *guard;
2867 long ret;
2868 IOTrace_packet iopkt;
2869 if (NULL_PTR (ftell))
2870 init_io_intf ();
2871 if (CHCK_REENTRANCE (guard) || stream == NULL)
2872 return CALL_REAL (ftell)(stream);
2873 PUSH_REENTRANCE (guard);
2874 hrtime_t reqt = gethrtime ();
2875 ret = CALL_REAL (ftell)(stream);
2876 if (RECHCK_REENTRANCE (guard))
2877 {
2878 POP_REENTRANCE (guard);
2879 return ret;
2880 }
2881 hrtime_t grnt = gethrtime ();
2882 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
2883 iopkt.comm.tsize = sizeof (IOTrace_packet);
2884 iopkt.comm.tstamp = grnt;
2885 iopkt.requested = reqt;
2886 if (ret != -1)
2887 iopkt.iotype = OTHERIO_TRACE;
2888 else
2889 iopkt.iotype = OTHERIO_TRACE_ERROR;
2890 iopkt.fd = fileno (stream);
2891 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2892 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2893 POP_REENTRANCE (guard);
2894 return ret;
2895 }
2896
2897 /*------------------------------------------------------------- fgetpos */
2898 static int
2899 gprofng_fgetpos (int(real_fgetpos) (FILE *stream, fpos_t *pos),
2900 FILE *stream, fpos_t *pos)
2901 {
2902 int *guard;
2903 int ret;
2904 IOTrace_packet iopkt;
2905 if (CHCK_REENTRANCE (guard) || stream == NULL)
2906 return real_fgetpos (stream, pos);
2907 PUSH_REENTRANCE (guard);
2908 hrtime_t reqt = gethrtime ();
2909 ret = real_fgetpos (stream, pos);
2910 if (RECHCK_REENTRANCE (guard))
2911 {
2912 POP_REENTRANCE (guard);
2913 return ret;
2914 }
2915 hrtime_t grnt = gethrtime ();
2916 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
2917 iopkt.comm.tsize = sizeof (IOTrace_packet);
2918 iopkt.comm.tstamp = grnt;
2919 iopkt.requested = reqt;
2920 if (ret == 0)
2921 iopkt.iotype = OTHERIO_TRACE;
2922 else
2923 iopkt.iotype = OTHERIO_TRACE_ERROR;
2924 iopkt.fd = fileno (stream);
2925 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl,
2926 iopkt.comm.tstamp, FRINFO_FROM_STACK_ARG, &iopkt);
2927 collector_interface->writeDataRecord (io_hndl, (Common_packet*) &iopkt);
2928 POP_REENTRANCE (guard);
2929 return ret;
2930 }
2931
2932 #define DCL_FGETPOS(dcl_f) \
2933 int dcl_f (FILE *stream, fpos_t *pos) \
2934 { \
2935 if (__real_fgetpos == NULL) \
2936 init_io_intf (); \
2937 return gprofng_fgetpos (__real_fgetpos, stream, pos); \
2938 }
2939
2940 DCL_FUNC_VER (DCL_FGETPOS, fgetpos_2_17, fgetpos@GLIBC_2.17)
2941 DCL_FUNC_VER (DCL_FGETPOS, fgetpos_2_2_5, fgetpos@GLIBC_2.2.5)
2942 DCL_FUNC_VER (DCL_FGETPOS, fgetpos_2_2, fgetpos@GLIBC_2.2)
2943 DCL_FUNC_VER (DCL_FGETPOS, fgetpos_2_0, fgetpos@GLIBC_2.0)
2944 DCL_FGETPOS (fgetpos)
2945
2946 /*------------------------------------------------------------- fgetpos64 */
2947 static int
2948 gprofng_fgetpos64 (int(real_fgetpos64) (), FILE *stream, fpos64_t *pos)
2949 {
2950 int *guard;
2951 int ret;
2952 IOTrace_packet iopkt;
2953 if (CHCK_REENTRANCE (guard) || stream == NULL)
2954 return real_fgetpos64 (stream, pos);
2955 PUSH_REENTRANCE (guard);
2956 hrtime_t reqt = gethrtime ();
2957 ret = real_fgetpos64 (stream, pos);
2958 if (RECHCK_REENTRANCE (guard))
2959 {
2960 POP_REENTRANCE (guard);
2961 return ret;
2962 }
2963 hrtime_t grnt = gethrtime ();
2964 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
2965 iopkt.comm.tsize = sizeof (IOTrace_packet);
2966 iopkt.comm.tstamp = grnt;
2967 iopkt.requested = reqt;
2968 if (ret == 0)
2969 iopkt.iotype = OTHERIO_TRACE;
2970 else
2971 iopkt.iotype = OTHERIO_TRACE_ERROR;
2972 iopkt.fd = fileno (stream);
2973 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl,
2974 iopkt.comm.tstamp, FRINFO_FROM_STACK_ARG, &iopkt);
2975 collector_interface->writeDataRecord (io_hndl, (Common_packet*) &iopkt);
2976 POP_REENTRANCE (guard);
2977 return ret;
2978 }
2979
2980 #define DCL_FGETPOS64(dcl_f) \
2981 int dcl_f (FILE *stream, fpos64_t *pos) \
2982 { \
2983 if (__real_fgetpos64 == NULL) \
2984 init_io_intf (); \
2985 return gprofng_fgetpos64 (__real_fgetpos64, stream, pos); \
2986 }
2987
2988 DCL_FUNC_VER (DCL_FGETPOS64, fgetpos64_2_17, fgetpos64@GLIBC_2.17)
2989 DCL_FUNC_VER (DCL_FGETPOS64, fgetpos64_2_2_5, fgetpos64@GLIBC_2.2.5)
2990 DCL_FUNC_VER (DCL_FGETPOS64, fgetpos64_2_2, fgetpos64@GLIBC_2.2)
2991 DCL_FUNC_VER (DCL_FGETPOS64, fgetpos64_2_1, fgetpos64@GLIBC_2.1)
2992 DCL_FGETPOS64 (fgetpos64)
2993
2994 /*------------------------------------------------------------- fsetpos */
2995 static int
2996 gprofng_fsetpos (int(real_fsetpos) (FILE *, const fpos_t *),
2997 FILE *stream, const fpos_t *pos)
2998 {
2999 int *guard;
3000 int ret;
3001 IOTrace_packet iopkt;
3002 if (CHCK_REENTRANCE (guard) || stream == NULL)
3003 return real_fsetpos (stream, pos);
3004 PUSH_REENTRANCE (guard);
3005 hrtime_t reqt = gethrtime ();
3006 ret = real_fsetpos (stream, pos);
3007 if (RECHCK_REENTRANCE (guard))
3008 {
3009 POP_REENTRANCE (guard);
3010 return ret;
3011 }
3012 hrtime_t grnt = gethrtime ();
3013 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
3014 iopkt.comm.tsize = sizeof (IOTrace_packet);
3015 iopkt.comm.tstamp = grnt;
3016 iopkt.requested = reqt;
3017 if (ret == 0)
3018 iopkt.iotype = OTHERIO_TRACE;
3019 else
3020 iopkt.iotype = OTHERIO_TRACE_ERROR;
3021 iopkt.fd = fileno (stream);
3022 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl,
3023 iopkt.comm.tstamp, FRINFO_FROM_STACK_ARG, &iopkt);
3024 collector_interface->writeDataRecord (io_hndl, (Common_packet*) &iopkt);
3025 POP_REENTRANCE (guard);
3026 return ret;
3027 }
3028
3029 #define DCL_FSETPOS(dcl_f) \
3030 int dcl_f (FILE *stream, const fpos_t *pos) \
3031 { \
3032 if (__real_fsetpos == NULL) \
3033 init_io_intf (); \
3034 return gprofng_fsetpos (__real_fsetpos, stream, pos); \
3035 }
3036
3037 DCL_FUNC_VER (DCL_FSETPOS, fsetpos_2_17, fsetpos@GLIBC_2.17)
3038 DCL_FUNC_VER (DCL_FSETPOS, fsetpos_2_2_5, fsetpos@GLIBC_2.2.5)
3039 DCL_FUNC_VER (DCL_FSETPOS, fsetpos_2_2, fsetpos@GLIBC_2.2)
3040 DCL_FUNC_VER (DCL_FSETPOS, fsetpos_2_0, fsetpos@GLIBC_2.0)
3041 DCL_FSETPOS (fsetpos)
3042
3043 /*------------------------------------------------------------- fsetpos64 */
3044 static int
3045 gprofng_fsetpos64 (int(real_fsetpos64) (FILE *, const fpos64_t *),
3046 FILE *stream, const fpos64_t *pos)
3047 {
3048 int *guard;
3049 int ret;
3050 IOTrace_packet iopkt;
3051 if (CHCK_REENTRANCE (guard) || stream == NULL)
3052 return real_fsetpos64 (stream, pos);
3053 PUSH_REENTRANCE (guard);
3054 hrtime_t reqt = gethrtime ();
3055 ret = real_fsetpos64 (stream, pos);
3056 if (RECHCK_REENTRANCE (guard))
3057 {
3058 POP_REENTRANCE (guard);
3059 return ret;
3060 }
3061 hrtime_t grnt = gethrtime ();
3062 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
3063 iopkt.comm.tsize = sizeof (IOTrace_packet);
3064 iopkt.comm.tstamp = grnt;
3065 iopkt.requested = reqt;
3066 if (ret == 0)
3067 iopkt.iotype = OTHERIO_TRACE;
3068 else
3069 iopkt.iotype = OTHERIO_TRACE_ERROR;
3070 iopkt.fd = fileno (stream);
3071 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl,
3072 iopkt.comm.tstamp, FRINFO_FROM_STACK_ARG, &iopkt);
3073 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
3074 POP_REENTRANCE (guard);
3075 return ret;
3076 }
3077
3078 #define DCL_FSETPOS64(dcl_f) \
3079 int dcl_f (FILE *stream, const fpos64_t *pos) \
3080 { \
3081 if (__real_fsetpos64 == NULL) \
3082 init_io_intf (); \
3083 return gprofng_fsetpos64 (__real_fsetpos64, stream, pos); \
3084 }
3085
3086 DCL_FUNC_VER (DCL_FSETPOS64, fsetpos64_2_17, fsetpos64@GLIBC_2.17)
3087 DCL_FUNC_VER (DCL_FSETPOS64, fsetpos64_2_2_5, fsetpos64@GLIBC_2.2.5)
3088 DCL_FUNC_VER (DCL_FSETPOS64, fsetpos64_2_2, fsetpos64@GLIBC_2.2)
3089 DCL_FUNC_VER (DCL_FSETPOS64, fsetpos64_2_1, fsetpos64@GLIBC_2.1)
3090 DCL_FSETPOS64 (fsetpos64)
3091
3092 /*------------------------------------------------------------- fsync */
3093 int
3094 fsync (int fildes)
3095 {
3096 int *guard;
3097 int ret;
3098 IOTrace_packet iopkt;
3099 if (NULL_PTR (fsync))
3100 init_io_intf ();
3101 if (CHCK_REENTRANCE (guard))
3102 return CALL_REAL (fsync)(fildes);
3103 PUSH_REENTRANCE (guard);
3104 hrtime_t reqt = gethrtime ();
3105 ret = CALL_REAL (fsync)(fildes);
3106 if (RECHCK_REENTRANCE (guard))
3107 {
3108 POP_REENTRANCE (guard);
3109 return ret;
3110 }
3111 hrtime_t grnt = gethrtime ();
3112 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
3113 iopkt.comm.tsize = sizeof (IOTrace_packet);
3114 iopkt.comm.tstamp = grnt;
3115 iopkt.requested = reqt;
3116 if (ret == 0)
3117 iopkt.iotype = OTHERIO_TRACE;
3118 else
3119 iopkt.iotype = OTHERIO_TRACE_ERROR;
3120 iopkt.fd = fildes;
3121 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
3122 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
3123 POP_REENTRANCE (guard);
3124 return ret;
3125 }
3126
3127 /*------------------------------------------------------------- readdir */
3128 struct dirent*
3129 readdir (DIR *dirp)
3130 {
3131 int *guard;
3132 struct dirent *ptr;
3133 IOTrace_packet iopkt;
3134 if (NULL_PTR (readdir))
3135 init_io_intf ();
3136 if (CHCK_REENTRANCE (guard))
3137 return CALL_REAL (readdir)(dirp);
3138 PUSH_REENTRANCE (guard);
3139 hrtime_t reqt = gethrtime ();
3140 ptr = CALL_REAL (readdir)(dirp);
3141 if (RECHCK_REENTRANCE (guard))
3142 {
3143 POP_REENTRANCE (guard);
3144 return ptr;
3145 }
3146 hrtime_t grnt = gethrtime ();
3147 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
3148 iopkt.comm.tsize = sizeof ( IOTrace_packet);
3149 iopkt.comm.tstamp = grnt;
3150 iopkt.requested = reqt;
3151 if (ptr != NULL)
3152 iopkt.iotype = OTHERIO_TRACE;
3153 else
3154 iopkt.iotype = OTHERIO_TRACE_ERROR;
3155 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
3156 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
3157 POP_REENTRANCE (guard);
3158 return ptr;
3159 }
3160
3161 /*------------------------------------------------------------- flock */
3162 int
3163 flock (int fd, int operation)
3164 {
3165 int *guard;
3166 int ret;
3167 IOTrace_packet iopkt;
3168 if (NULL_PTR (flock))
3169 init_io_intf ();
3170 if (CHCK_REENTRANCE (guard))
3171 return CALL_REAL (flock)(fd, operation);
3172 PUSH_REENTRANCE (guard);
3173 hrtime_t reqt = gethrtime ();
3174 ret = CALL_REAL (flock)(fd, operation);
3175 if (RECHCK_REENTRANCE (guard))
3176 {
3177 POP_REENTRANCE (guard);
3178 return ret;
3179 }
3180 hrtime_t grnt = gethrtime ();
3181 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
3182 iopkt.comm.tsize = sizeof (IOTrace_packet);
3183 iopkt.comm.tstamp = grnt;
3184 iopkt.requested = reqt;
3185 if (ret == 0)
3186 iopkt.iotype = OTHERIO_TRACE;
3187 else
3188 iopkt.iotype = OTHERIO_TRACE_ERROR;
3189 iopkt.fd = fd;
3190 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
3191 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
3192 POP_REENTRANCE (guard);
3193 return ret;
3194 }
3195
3196 /*------------------------------------------------------------- lockf */
3197 int
3198 lockf (int fildes, int function, off_t size)
3199 {
3200 int *guard;
3201 int ret;
3202 IOTrace_packet iopkt;
3203 if (NULL_PTR (lockf))
3204 init_io_intf ();
3205 if (CHCK_REENTRANCE (guard))
3206 return CALL_REAL (lockf)(fildes, function, size);
3207 PUSH_REENTRANCE (guard);
3208 hrtime_t reqt = gethrtime ();
3209 ret = CALL_REAL (lockf)(fildes, function, size);
3210 if (RECHCK_REENTRANCE (guard))
3211 {
3212 POP_REENTRANCE (guard);
3213 return ret;
3214 }
3215 hrtime_t grnt = gethrtime ();
3216 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
3217 iopkt.comm.tsize = sizeof (IOTrace_packet);
3218 iopkt.comm.tstamp = grnt;
3219 iopkt.requested = reqt;
3220 if (ret == 0)
3221 iopkt.iotype = OTHERIO_TRACE;
3222 else
3223 iopkt.iotype = OTHERIO_TRACE_ERROR;
3224 iopkt.fd = fildes;
3225 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
3226 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
3227 POP_REENTRANCE (guard);
3228 return ret;
3229 }