regen config
[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 #if !defined(__USE_LARGEFILE64)
1034 DCL_OPEN64 (open64)
1035 #endif
1036
1037 #define F_ERROR_ARG 0
1038 #define F_INT_ARG 1
1039 #define F_LONG_ARG 2
1040 #define F_VOID_ARG 3
1041
1042 /*
1043 * The following macro is not defined in the
1044 * older versions of Linux.
1045 * #define F_DUPFD_CLOEXEC 1030
1046 *
1047 * Instead use the command that is defined below
1048 * until we start compiling mpmt on the newer
1049 * versions of Linux.
1050 */
1051 #define TMP_F_DUPFD_CLOEXEC 1030
1052
1053 /*------------------------------------------------------------- fcntl */
1054 int
1055 fcntl (int fildes, int cmd, ...)
1056 {
1057 int *guard;
1058 int fd = 0;
1059 IOTrace_packet iopkt;
1060 long long_arg = 0;
1061 int int_arg = 0;
1062 int which_arg = F_ERROR_ARG;
1063 va_list ap;
1064 switch (cmd)
1065 {
1066 case F_DUPFD:
1067 case TMP_F_DUPFD_CLOEXEC:
1068 case F_SETFD:
1069 case F_SETFL:
1070 case F_SETOWN:
1071 case F_SETSIG:
1072 case F_SETLEASE:
1073 case F_NOTIFY:
1074 case F_SETLK:
1075 case F_SETLKW:
1076 case F_GETLK:
1077 va_start (ap, cmd);
1078 long_arg = va_arg (ap, long);
1079 va_end (ap);
1080 which_arg = F_LONG_ARG;
1081 break;
1082 case F_GETFD:
1083 case F_GETFL:
1084 case F_GETOWN:
1085 case F_GETLEASE:
1086 case F_GETSIG:
1087 which_arg = F_VOID_ARG;
1088 break;
1089 }
1090 if (NULL_PTR (fcntl))
1091 init_io_intf ();
1092 if (CHCK_REENTRANCE (guard))
1093 {
1094 switch (which_arg)
1095 {
1096 case F_INT_ARG:
1097 return CALL_REAL (fcntl)(fildes, cmd, int_arg);
1098 case F_LONG_ARG:
1099 return CALL_REAL (fcntl)(fildes, cmd, long_arg);
1100 case F_VOID_ARG:
1101 return CALL_REAL (fcntl)(fildes, cmd);
1102 case F_ERROR_ARG:
1103 Tprintf (0, "iotrace: ERROR: Unsupported fcntl command\n");
1104 return -1;
1105 }
1106 return -1;
1107 }
1108 if (cmd != F_DUPFD && cmd != TMP_F_DUPFD_CLOEXEC)
1109 {
1110 switch (which_arg)
1111 {
1112 case F_INT_ARG:
1113 return CALL_REAL (fcntl)(fildes, cmd, int_arg);
1114 case F_LONG_ARG:
1115 return CALL_REAL (fcntl)(fildes, cmd, long_arg);
1116 case F_VOID_ARG:
1117 return CALL_REAL (fcntl)(fildes, cmd);
1118 case F_ERROR_ARG:
1119 Tprintf (0, "iotrace: ERROR: Unsupported fcntl command\n");
1120 return -1;
1121 }
1122 return -1;
1123 }
1124 PUSH_REENTRANCE (guard);
1125 hrtime_t reqt = gethrtime ();
1126 switch (cmd)
1127 {
1128 case F_DUPFD:
1129 case TMP_F_DUPFD_CLOEXEC:
1130 fd = CALL_REAL (fcntl)(fildes, cmd, long_arg);
1131 break;
1132 }
1133 if (RECHCK_REENTRANCE (guard))
1134 {
1135 POP_REENTRANCE (guard);
1136 return fd;
1137 }
1138 hrtime_t grnt = gethrtime ();
1139 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1140 iopkt.comm.tsize = sizeof (IOTrace_packet);
1141 iopkt.comm.tstamp = grnt;
1142 iopkt.requested = reqt;
1143 if (fd != -1)
1144 iopkt.iotype = OPEN_TRACE;
1145 else
1146 iopkt.iotype = OPEN_TRACE_ERROR;
1147 iopkt.fd = fd;
1148 iopkt.ofd = fildes;
1149 iopkt.fstype = UNKNOWNFS_TYPE;
1150 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1151 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1152 POP_REENTRANCE (guard);
1153 return fd;
1154 }
1155
1156 /*------------------------------------------------------------- openat */
1157 int
1158 openat (int fildes, const char *path, int oflag, ...)
1159 {
1160 int *guard;
1161 int fd;
1162 void *packet;
1163 IOTrace_packet *iopkt;
1164 mode_t mode;
1165 va_list ap;
1166 size_t sz;
1167 unsigned pktSize;
1168
1169 va_start (ap, oflag);
1170 mode = va_arg (ap, mode_t);
1171 va_end (ap);
1172 if (NULL_PTR (openat))
1173 init_io_intf ();
1174 if (CHCK_REENTRANCE (guard) || path == NULL)
1175 return CALL_REAL (openat)(fildes, path, oflag, mode);
1176 PUSH_REENTRANCE (guard);
1177 hrtime_t reqt = gethrtime ();
1178 fd = CALL_REAL (openat)(fildes, path, oflag, mode);
1179 if (RECHCK_REENTRANCE (guard))
1180 {
1181 POP_REENTRANCE (guard);
1182 return fd;
1183 }
1184 hrtime_t grnt = gethrtime ();
1185 sz = collector_strlen (path);
1186 pktSize = sizeof (IOTrace_packet) + sz;
1187 pktSize = collector_align_pktsize (pktSize);
1188 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
1189 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
1190 if (packet != NULL)
1191 {
1192 iopkt = (IOTrace_packet *) packet;
1193 collector_memset (iopkt, 0, pktSize);
1194 iopkt->comm.tsize = pktSize;
1195 iopkt->comm.tstamp = grnt;
1196 iopkt->requested = reqt;
1197 if (fd != -1)
1198 iopkt->iotype = OPEN_TRACE;
1199 else
1200 iopkt->iotype = OPEN_TRACE_ERROR;
1201 iopkt->fd = fd;
1202 iopkt->fstype = collector_fstype (path);
1203 collector_strncpy (&(iopkt->fname), path, sz);
1204 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1205 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
1206 collector_interface->freeCSize (io_heap, packet, pktSize);
1207 }
1208 else
1209 {
1210 Tprintf (0, "iotrace: ERROR: openat cannot allocate memory\n");
1211 return -1;
1212 }
1213 POP_REENTRANCE (guard);
1214 return fd;
1215 }
1216
1217 /*------------------------------------------------------------- creat */
1218 int
1219 creat (const char *path, mode_t mode)
1220 {
1221 int *guard;
1222 int fd;
1223 void *packet;
1224 IOTrace_packet *iopkt;
1225 size_t sz;
1226 unsigned pktSize;
1227 if (NULL_PTR (creat))
1228 init_io_intf ();
1229 if (CHCK_REENTRANCE (guard) || path == NULL)
1230 return CALL_REAL (creat)(path, mode);
1231 PUSH_REENTRANCE (guard);
1232 hrtime_t reqt = gethrtime ();
1233 fd = CALL_REAL (creat)(path, mode);
1234 if (RECHCK_REENTRANCE (guard))
1235 {
1236 POP_REENTRANCE (guard);
1237 return fd;
1238 }
1239 hrtime_t grnt = gethrtime ();
1240 sz = collector_strlen (path);
1241 pktSize = sizeof (IOTrace_packet) + sz;
1242 pktSize = collector_align_pktsize (pktSize);
1243 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
1244 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
1245 if (packet != NULL)
1246 {
1247 iopkt = (IOTrace_packet *) packet;
1248 collector_memset (iopkt, 0, pktSize);
1249 iopkt->comm.tsize = pktSize;
1250 iopkt->comm.tstamp = grnt;
1251 iopkt->requested = reqt;
1252 if (fd != -1)
1253 iopkt->iotype = OPEN_TRACE;
1254 else
1255 iopkt->iotype = OPEN_TRACE_ERROR;
1256 iopkt->fd = fd;
1257 iopkt->fstype = collector_fstype (path);
1258 collector_strncpy (&(iopkt->fname), path, sz);
1259 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1260 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
1261 collector_interface->freeCSize (io_heap, packet, pktSize);
1262 }
1263 else
1264 {
1265 Tprintf (0, "iotrace: ERROR: creat cannot allocate memory\n");
1266 return -1;
1267 }
1268 POP_REENTRANCE (guard);
1269 return fd;
1270 }
1271
1272 /*------------------------------------------------------------- creat64 */
1273 #if WSIZE(32) && !defined(__USE_LARGEFILE64)
1274 int
1275 creat64 (const char *path, mode_t mode)
1276 {
1277 int *guard;
1278 int fd;
1279 void *packet;
1280 IOTrace_packet *iopkt;
1281 size_t sz;
1282 unsigned pktSize;
1283
1284 if (NULL_PTR (creat64))
1285 init_io_intf ();
1286 if (CHCK_REENTRANCE (guard) || path == NULL)
1287 return CALL_REAL (creat64)(path, mode);
1288 PUSH_REENTRANCE (guard);
1289 hrtime_t reqt = gethrtime ();
1290 fd = CALL_REAL (creat64)(path, mode);
1291 if (RECHCK_REENTRANCE (guard))
1292 {
1293 POP_REENTRANCE (guard);
1294 return fd;
1295 }
1296 hrtime_t grnt = gethrtime ();
1297 sz = collector_strlen (path);
1298 pktSize = sizeof (IOTrace_packet) + sz;
1299 pktSize = collector_align_pktsize (pktSize);
1300 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
1301 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
1302 if (packet != NULL)
1303 {
1304 iopkt = (IOTrace_packet *) packet;
1305 collector_memset (iopkt, 0, pktSize);
1306 iopkt->comm.tsize = pktSize;
1307 iopkt->comm.tstamp = grnt;
1308 iopkt->requested = reqt;
1309 if (fd != -1)
1310 iopkt->iotype = OPEN_TRACE;
1311 else
1312 iopkt->iotype = OPEN_TRACE_ERROR;
1313 iopkt->fd = fd;
1314 iopkt->fstype = collector_fstype (path);
1315 collector_strncpy (&(iopkt->fname), path, sz);
1316 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1317 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
1318 collector_interface->freeCSize (io_heap, packet, pktSize);
1319 }
1320 else
1321 {
1322 Tprintf (0, "iotrace: ERROR: creat64 cannot allocate memory\n");
1323 return -1;
1324 }
1325 POP_REENTRANCE (guard);
1326 return fd;
1327 }
1328 #endif
1329
1330 /*------------------------------------------------------------- mkstemp */
1331 int
1332 mkstemp (char *template)
1333 {
1334 int *guard;
1335 int fd;
1336 void *packet;
1337 IOTrace_packet *iopkt;
1338 size_t sz;
1339 unsigned pktSize;
1340 if (NULL_PTR (mkstemp))
1341 init_io_intf ();
1342 if (CHCK_REENTRANCE (guard) || template == NULL)
1343 return CALL_REAL (mkstemp)(template);
1344 PUSH_REENTRANCE (guard);
1345 hrtime_t reqt = gethrtime ();
1346 fd = CALL_REAL (mkstemp)(template);
1347 if (RECHCK_REENTRANCE (guard))
1348 {
1349 POP_REENTRANCE (guard);
1350 return fd;
1351 }
1352 hrtime_t grnt = gethrtime ();
1353 sz = collector_strlen (template);
1354 pktSize = sizeof (IOTrace_packet) + sz;
1355 pktSize = collector_align_pktsize (pktSize);
1356 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
1357 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
1358 if (packet != NULL)
1359 {
1360 iopkt = (IOTrace_packet *) packet;
1361 collector_memset (iopkt, 0, pktSize);
1362 iopkt->comm.tsize = pktSize;
1363 iopkt->comm.tstamp = grnt;
1364 iopkt->requested = reqt;
1365 if (fd != -1)
1366 iopkt->iotype = OPEN_TRACE;
1367 else
1368 iopkt->iotype = OPEN_TRACE_ERROR;
1369 iopkt->fd = fd;
1370 iopkt->fstype = collector_fstype (template);
1371 collector_strncpy (&(iopkt->fname), template, sz);
1372 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1373 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
1374 collector_interface->freeCSize (io_heap, packet, pktSize);
1375 }
1376 else
1377 {
1378 Tprintf (0, "iotrace: ERROR: mkstemp cannot allocate memory\n");
1379 return -1;
1380 }
1381 POP_REENTRANCE (guard);
1382 return fd;
1383 }
1384
1385 /*------------------------------------------------------------- mkstemps */
1386 int
1387 mkstemps (char *template, int slen)
1388 {
1389 int *guard;
1390 int fd;
1391 void *packet;
1392 IOTrace_packet *iopkt;
1393 size_t sz;
1394 unsigned pktSize;
1395 if (NULL_PTR (mkstemps))
1396 init_io_intf ();
1397 if (CHCK_REENTRANCE (guard) || template == NULL)
1398 return CALL_REAL (mkstemps)(template, slen);
1399 PUSH_REENTRANCE (guard);
1400 hrtime_t reqt = gethrtime ();
1401 fd = CALL_REAL (mkstemps)(template, slen);
1402 if (RECHCK_REENTRANCE (guard))
1403 {
1404 POP_REENTRANCE (guard);
1405 return fd;
1406 }
1407 hrtime_t grnt = gethrtime ();
1408 sz = collector_strlen (template);
1409 pktSize = sizeof (IOTrace_packet) + sz;
1410 pktSize = collector_align_pktsize (pktSize);
1411 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
1412 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
1413 if (packet != NULL)
1414 {
1415 iopkt = (IOTrace_packet *) packet;
1416 collector_memset (iopkt, 0, pktSize);
1417 iopkt->comm.tsize = pktSize;
1418 iopkt->comm.tstamp = grnt;
1419 iopkt->requested = reqt;
1420 if (fd != -1)
1421 iopkt->iotype = OPEN_TRACE;
1422 else
1423 iopkt->iotype = OPEN_TRACE_ERROR;
1424 iopkt->fd = fd;
1425 iopkt->fstype = collector_fstype (template);
1426 collector_strncpy (&(iopkt->fname), template, sz);
1427 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1428 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
1429 collector_interface->freeCSize (io_heap, packet, pktSize);
1430 }
1431 else
1432 {
1433 Tprintf (0, "iotrace: ERROR: mkstemps cannot allocate memory\n");
1434 return -1;
1435 }
1436 POP_REENTRANCE (guard);
1437 return fd;
1438 }
1439
1440 /*------------------------------------------------------------- close */
1441 int
1442 close (int fildes)
1443 {
1444 int *guard;
1445 int stat;
1446 IOTrace_packet iopkt;
1447 if (NULL_PTR (close))
1448 init_io_intf ();
1449 if (CHCK_REENTRANCE (guard))
1450 return CALL_REAL (close)(fildes);
1451 PUSH_REENTRANCE (guard);
1452 hrtime_t reqt = gethrtime ();
1453 stat = CALL_REAL (close)(fildes);
1454 if (RECHCK_REENTRANCE (guard))
1455 {
1456 POP_REENTRANCE (guard);
1457 return stat;
1458 }
1459 hrtime_t grnt = gethrtime ();
1460 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1461 iopkt.comm.tsize = sizeof (IOTrace_packet);
1462 iopkt.comm.tstamp = grnt;
1463 iopkt.requested = reqt;
1464 if (stat == 0)
1465 iopkt.iotype = CLOSE_TRACE;
1466 else
1467 iopkt.iotype = CLOSE_TRACE_ERROR;
1468 iopkt.fd = fildes;
1469 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1470 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1471 POP_REENTRANCE (guard);
1472 return stat;
1473 }
1474
1475 /*------------------------------------------------------------- fopen */
1476 static FILE*
1477 gprofng_fopen (FILE*(real_fopen) (), const char *filename, const char *mode)
1478 {
1479 int *guard;
1480 FILE *fp = NULL;
1481 void *packet;
1482 IOTrace_packet *iopkt;
1483 size_t sz;
1484 unsigned pktSize;
1485 if (CHCK_REENTRANCE (guard) || filename == NULL)
1486 return real_fopen (filename, mode);
1487 PUSH_REENTRANCE (guard);
1488 hrtime_t reqt = gethrtime ();
1489
1490 fp = real_fopen (filename, mode);
1491 if (RECHCK_REENTRANCE (guard))
1492 {
1493 POP_REENTRANCE (guard);
1494 return fp;
1495 }
1496 hrtime_t grnt = gethrtime ();
1497 sz = collector_strlen (filename);
1498 pktSize = sizeof (IOTrace_packet) + sz;
1499 pktSize = collector_align_pktsize (pktSize);
1500 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
1501 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
1502 if (packet != NULL)
1503 {
1504 iopkt = (IOTrace_packet *) packet;
1505 collector_memset (iopkt, 0, pktSize);
1506 iopkt->comm.tsize = pktSize;
1507 iopkt->comm.tstamp = grnt;
1508 iopkt->requested = reqt;
1509 if (fp != NULL)
1510 {
1511 iopkt->iotype = OPEN_TRACE;
1512 iopkt->fd = fileno (fp);
1513 }
1514 else
1515 {
1516 iopkt->iotype = OPEN_TRACE_ERROR;
1517 iopkt->fd = -1;
1518 }
1519 iopkt->fstype = collector_fstype (filename);
1520 collector_strncpy (&(iopkt->fname), filename, sz);
1521 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl,
1522 iopkt->comm.tstamp, FRINFO_FROM_STACK_ARG, &iopkt);
1523 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
1524 collector_interface->freeCSize (io_heap, packet, pktSize);
1525 }
1526 else
1527 {
1528 Tprintf (0, "iotrace: ERROR: fopen cannot allocate memory\n");
1529 return NULL;
1530 }
1531 POP_REENTRANCE (guard);
1532 return fp;
1533 }
1534
1535 #define DCL_FOPEN(dcl_f) \
1536 FILE *dcl_f (const char *filename, const char *mode) \
1537 { \
1538 if (__real_fopen == NULL) \
1539 init_io_intf (); \
1540 return gprofng_fopen (__real_fopen, filename, mode); \
1541 }
1542
1543 DCL_FUNC_VER (DCL_FOPEN, fopen_2_17, fopen@GLIBC_2.17)
1544 DCL_FUNC_VER (DCL_FOPEN, fopen_2_2_5, fopen@GLIBC_2.2.5)
1545 DCL_FUNC_VER (DCL_FOPEN, fopen_2_1, fopen@GLIBC_2.1)
1546 DCL_FUNC_VER (DCL_FOPEN, fopen_2_0, fopen@GLIBC_2.0)
1547 DCL_FOPEN (fopen)
1548
1549 /*------------------------------------------------------------- fclose */
1550 static int
1551 gprofng_fclose (int(real_fclose) (), FILE *stream)
1552 {
1553 int *guard;
1554 int stat;
1555 IOTrace_packet iopkt;
1556 if (CHCK_REENTRANCE (guard) || stream == NULL)
1557 return real_fclose (stream);
1558 PUSH_REENTRANCE (guard);
1559 hrtime_t reqt = gethrtime ();
1560 stat = real_fclose (stream);
1561 if (RECHCK_REENTRANCE (guard))
1562 {
1563 POP_REENTRANCE (guard);
1564 return stat;
1565 }
1566 hrtime_t grnt = gethrtime ();
1567 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1568 iopkt.comm.tsize = sizeof (IOTrace_packet);
1569 iopkt.comm.tstamp = grnt;
1570 iopkt.requested = reqt;
1571 if (stat == 0)
1572 iopkt.iotype = CLOSE_TRACE;
1573 else
1574 iopkt.iotype = CLOSE_TRACE_ERROR;
1575 iopkt.fd = fileno (stream);
1576 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl,
1577 iopkt.comm.tstamp, FRINFO_FROM_STACK_ARG, &iopkt);
1578 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1579 POP_REENTRANCE (guard);
1580 return stat;
1581 }
1582
1583 #define DCL_FCLOSE(dcl_f) \
1584 int dcl_f (FILE *stream) \
1585 { \
1586 if (__real_fclose == NULL) \
1587 init_io_intf (); \
1588 return gprofng_fclose (__real_fclose, stream); \
1589 }
1590
1591 DCL_FUNC_VER (DCL_FCLOSE, fclose_2_17, fclose@GLIBC_2.17)
1592 DCL_FUNC_VER (DCL_FCLOSE, fclose_2_2_5, fclose@GLIBC_2.2.5)
1593 DCL_FUNC_VER (DCL_FCLOSE, fclose_2_1, fclose@GLIBC_2.1)
1594 DCL_FUNC_VER (DCL_FCLOSE, fclose_2_0, fclose@GLIBC_2.0)
1595 DCL_FCLOSE (fclose)
1596
1597 /*------------------------------------------------------------- fflush */
1598 int
1599 fflush (FILE *stream)
1600 {
1601 int *guard;
1602 int stat;
1603 IOTrace_packet iopkt;
1604 if (NULL_PTR (fflush))
1605 init_io_intf ();
1606 if (CHCK_REENTRANCE (guard))
1607 return CALL_REAL (fflush)(stream);
1608 PUSH_REENTRANCE (guard);
1609 hrtime_t reqt = gethrtime ();
1610 stat = CALL_REAL (fflush)(stream);
1611 if (RECHCK_REENTRANCE (guard))
1612 {
1613 POP_REENTRANCE (guard);
1614 return stat;
1615 }
1616 hrtime_t grnt = gethrtime ();
1617 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1618 iopkt.comm.tsize = sizeof (IOTrace_packet);
1619 iopkt.comm.tstamp = grnt;
1620 iopkt.requested = reqt;
1621 if (stat == 0)
1622 iopkt.iotype = OTHERIO_TRACE;
1623 else
1624 iopkt.iotype = OTHERIO_TRACE_ERROR;
1625 if (stream != NULL)
1626 iopkt.fd = fileno (stream);
1627 else
1628 iopkt.fd = -1;
1629 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1630 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1631 POP_REENTRANCE (guard);
1632 return stat;
1633 }
1634
1635 /*------------------------------------------------------------- fdopen */
1636 static FILE*
1637 gprofng_fdopen (FILE*(real_fdopen) (), int fildes, const char *mode)
1638 {
1639 int *guard;
1640 FILE *fp = NULL;
1641 IOTrace_packet iopkt;
1642 if (NULL_PTR (fdopen))
1643 init_io_intf ();
1644 if (CHCK_REENTRANCE (guard))
1645 return real_fdopen (fildes, mode);
1646 PUSH_REENTRANCE (guard);
1647 hrtime_t reqt = gethrtime ();
1648 fp = real_fdopen (fildes, mode);
1649 if (RECHCK_REENTRANCE (guard))
1650 {
1651 POP_REENTRANCE (guard);
1652 return fp;
1653 }
1654 hrtime_t grnt = gethrtime ();
1655 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1656 iopkt.comm.tsize = sizeof (IOTrace_packet);
1657 iopkt.comm.tstamp = grnt;
1658 iopkt.requested = reqt;
1659 if (fp != NULL)
1660 iopkt.iotype = OPEN_TRACE;
1661 else
1662 iopkt.iotype = OPEN_TRACE_ERROR;
1663 iopkt.fd = fildes;
1664 iopkt.fstype = UNKNOWNFS_TYPE;
1665 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl,
1666 iopkt.comm.tstamp, FRINFO_FROM_STACK_ARG, &iopkt);
1667 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1668 POP_REENTRANCE (guard);
1669 return fp;
1670 }
1671
1672 #define DCL_FDOPEN(dcl_f) \
1673 FILE *dcl_f (int fildes, const char *mode) \
1674 { \
1675 if (__real_fdopen == NULL) \
1676 init_io_intf (); \
1677 return gprofng_fdopen (__real_fdopen, fildes, mode); \
1678 }
1679
1680 DCL_FUNC_VER (DCL_FDOPEN, fdopen_2_17, fdopen@GLIBC_2.17)
1681 DCL_FUNC_VER (DCL_FDOPEN, fdopen_2_2_5, fdopen@GLIBC_2.2.5)
1682 DCL_FUNC_VER (DCL_FDOPEN, fdopen_2_1, fdopen@GLIBC_2.1)
1683 DCL_FUNC_VER (DCL_FDOPEN, fdopen_2_0, fdopen@GLIBC_2.0)
1684 DCL_FDOPEN (fdopen)
1685
1686 /*------------------------------------------------------------- dup */
1687 int
1688 dup (int fildes)
1689 {
1690 int *guard;
1691 int fd;
1692 IOTrace_packet iopkt;
1693 if (NULL_PTR (dup))
1694 init_io_intf ();
1695 if (CHCK_REENTRANCE (guard))
1696 return CALL_REAL (dup)(fildes);
1697 PUSH_REENTRANCE (guard);
1698 hrtime_t reqt = gethrtime ();
1699 fd = CALL_REAL (dup)(fildes);
1700 if (RECHCK_REENTRANCE (guard))
1701 {
1702 POP_REENTRANCE (guard);
1703 return fd;
1704 }
1705 hrtime_t grnt = gethrtime ();
1706 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1707 iopkt.comm.tsize = sizeof (IOTrace_packet);
1708 iopkt.comm.tstamp = grnt;
1709 iopkt.requested = reqt;
1710 if (fd != -1)
1711 iopkt.iotype = OPEN_TRACE;
1712 else
1713 iopkt.iotype = OPEN_TRACE_ERROR;
1714
1715 iopkt.fd = fd;
1716 iopkt.ofd = fildes;
1717 iopkt.fstype = UNKNOWNFS_TYPE;
1718 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1719 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1720 POP_REENTRANCE (guard);
1721 return fd;
1722 }
1723
1724 /*------------------------------------------------------------- dup2 */
1725 int
1726 dup2 (int fildes, int fildes2)
1727 {
1728 int *guard;
1729 int fd;
1730 IOTrace_packet iopkt;
1731 if (NULL_PTR (dup2))
1732 init_io_intf ();
1733 if (CHCK_REENTRANCE (guard))
1734 return CALL_REAL (dup2)(fildes, fildes2);
1735 PUSH_REENTRANCE (guard);
1736 hrtime_t reqt = gethrtime ();
1737 fd = CALL_REAL (dup2)(fildes, fildes2);
1738 if (RECHCK_REENTRANCE (guard))
1739 {
1740 POP_REENTRANCE (guard);
1741 return fd;
1742 }
1743 hrtime_t grnt = gethrtime ();
1744 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1745 iopkt.comm.tsize = sizeof (IOTrace_packet);
1746 iopkt.comm.tstamp = grnt;
1747 iopkt.requested = reqt;
1748 if (fd != -1)
1749 iopkt.iotype = OPEN_TRACE;
1750 else
1751 iopkt.iotype = OPEN_TRACE_ERROR;
1752 iopkt.fd = fd;
1753 iopkt.ofd = fildes;
1754 iopkt.fstype = UNKNOWNFS_TYPE;
1755 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1756 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1757 POP_REENTRANCE (guard);
1758 return fd;
1759 }
1760
1761 /*------------------------------------------------------------- pipe */
1762 int
1763 pipe (int fildes[2])
1764 {
1765 int *guard;
1766 int ret;
1767 IOTrace_packet iopkt;
1768 if (NULL_PTR (pipe))
1769 init_io_intf ();
1770 if (CHCK_REENTRANCE (guard))
1771 return CALL_REAL (pipe)(fildes);
1772 PUSH_REENTRANCE (guard);
1773 hrtime_t reqt = gethrtime ();
1774 ret = CALL_REAL (pipe)(fildes);
1775 if (RECHCK_REENTRANCE (guard))
1776 {
1777 POP_REENTRANCE (guard);
1778 return ret;
1779 }
1780 hrtime_t grnt = gethrtime ();
1781 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1782 iopkt.comm.tsize = sizeof (IOTrace_packet);
1783 iopkt.comm.tstamp = grnt;
1784 iopkt.requested = reqt;
1785 if (ret != -1)
1786 iopkt.iotype = OPEN_TRACE;
1787 else
1788 iopkt.iotype = OPEN_TRACE_ERROR;
1789 iopkt.fd = fildes[0];
1790 iopkt.fstype = UNKNOWNFS_TYPE;
1791 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1792 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1793 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1794 iopkt.comm.tsize = sizeof (IOTrace_packet);
1795 iopkt.comm.tstamp = grnt;
1796 iopkt.requested = reqt;
1797 if (ret != -1)
1798 iopkt.iotype = OPEN_TRACE;
1799 else
1800 iopkt.iotype = OPEN_TRACE_ERROR;
1801 iopkt.fd = fildes[1];
1802 iopkt.fstype = UNKNOWNFS_TYPE;
1803 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1804 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1805 POP_REENTRANCE (guard);
1806 return ret;
1807 }
1808
1809 /*------------------------------------------------------------- socket */
1810 int
1811 socket (int domain, int type, int protocol)
1812 {
1813 int *guard;
1814 int fd;
1815 IOTrace_packet iopkt;
1816 if (NULL_PTR (socket))
1817 init_io_intf ();
1818 if (CHCK_REENTRANCE (guard))
1819 return CALL_REAL (socket)(domain, type, protocol);
1820 PUSH_REENTRANCE (guard);
1821 hrtime_t reqt = gethrtime ();
1822 fd = CALL_REAL (socket)(domain, type, protocol);
1823 if (RECHCK_REENTRANCE (guard))
1824 {
1825 POP_REENTRANCE (guard);
1826 return fd;
1827 }
1828 hrtime_t grnt = gethrtime ();
1829 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
1830 iopkt.comm.tsize = sizeof (IOTrace_packet);
1831 iopkt.comm.tstamp = grnt;
1832 iopkt.requested = reqt;
1833 if (fd != -1)
1834 iopkt.iotype = OPEN_TRACE;
1835 else
1836 iopkt.iotype = OPEN_TRACE_ERROR;
1837 iopkt.fd = fd;
1838 iopkt.fstype = UNKNOWNFS_TYPE;
1839 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1840 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1841 POP_REENTRANCE (guard);
1842 return fd;
1843 }
1844
1845 /*------------------------------------------------------------- read */
1846 ssize_t
1847 read (int fildes, void *buf, size_t nbyte)
1848 {
1849 int *guard;
1850 ssize_t ret;
1851 IOTrace_packet iopkt;
1852 if (NULL_PTR (read))
1853 init_io_intf ();
1854 if (CHCK_REENTRANCE (guard))
1855 return CALL_REAL (read)(fildes, buf, nbyte);
1856 PUSH_REENTRANCE (guard);
1857 hrtime_t reqt = gethrtime ();
1858 ret = CALL_REAL (read)(fildes, buf, nbyte);
1859 if (RECHCK_REENTRANCE (guard))
1860 {
1861 POP_REENTRANCE (guard);
1862 return ret;
1863 }
1864 hrtime_t grnt = gethrtime ();
1865 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
1866 iopkt.comm.tsize = sizeof ( IOTrace_packet);
1867 iopkt.comm.tstamp = grnt;
1868 iopkt.requested = reqt;
1869 if (ret >= 0)
1870 iopkt.iotype = READ_TRACE;
1871 else
1872 iopkt.iotype = READ_TRACE_ERROR;
1873 iopkt.fd = fildes;
1874 iopkt.nbyte = ret;
1875 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1876 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1877 POP_REENTRANCE (guard);
1878 return ret;
1879 }
1880
1881 /*------------------------------------------------------------- write */
1882 ssize_t
1883 write (int fildes, const void *buf, size_t nbyte)
1884 {
1885 int *guard;
1886 ssize_t ret;
1887 IOTrace_packet iopkt;
1888 if (NULL_PTR (write))
1889 init_io_intf ();
1890 if (CHCK_REENTRANCE (guard))
1891 return CALL_REAL (write)(fildes, buf, nbyte);
1892 PUSH_REENTRANCE (guard);
1893 hrtime_t reqt = gethrtime ();
1894 ret = CALL_REAL (write)(fildes, buf, nbyte);
1895 if (RECHCK_REENTRANCE (guard))
1896 {
1897 POP_REENTRANCE (guard);
1898 return ret;
1899 }
1900 hrtime_t grnt = gethrtime ();
1901 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
1902 iopkt.comm.tsize = sizeof ( IOTrace_packet);
1903 iopkt.comm.tstamp = grnt;
1904 iopkt.requested = reqt;
1905 if (ret >= 0)
1906 iopkt.iotype = WRITE_TRACE;
1907 else
1908 iopkt.iotype = WRITE_TRACE_ERROR;
1909 iopkt.fd = fildes;
1910 iopkt.nbyte = ret;
1911 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1912 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1913 POP_REENTRANCE (guard);
1914 return ret;
1915 }
1916
1917 /*------------------------------------------------------------- readv */
1918 ssize_t
1919 readv (int fildes, const struct iovec *iov, int iovcnt)
1920 {
1921 int *guard;
1922 ssize_t ret;
1923 IOTrace_packet iopkt;
1924 if (NULL_PTR (readv))
1925 init_io_intf ();
1926 if (CHCK_REENTRANCE (guard))
1927 return CALL_REAL (readv)(fildes, iov, iovcnt);
1928 PUSH_REENTRANCE (guard);
1929 hrtime_t reqt = gethrtime ();
1930 ret = CALL_REAL (readv)(fildes, iov, iovcnt);
1931 if (RECHCK_REENTRANCE (guard))
1932 {
1933 POP_REENTRANCE (guard);
1934 return ret;
1935 }
1936 hrtime_t grnt = gethrtime ();
1937 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
1938 iopkt.comm.tsize = sizeof ( IOTrace_packet);
1939 iopkt.comm.tstamp = grnt;
1940 iopkt.requested = reqt;
1941 if (ret >= 0)
1942 iopkt.iotype = READ_TRACE;
1943 else
1944 iopkt.iotype = READ_TRACE_ERROR;
1945 iopkt.fd = fildes;
1946 iopkt.nbyte = ret;
1947 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1948 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1949 POP_REENTRANCE (guard);
1950 return ret;
1951 }
1952
1953 /*------------------------------------------------------------- writev */
1954 ssize_t
1955 writev (int fildes, const struct iovec *iov, int iovcnt)
1956 {
1957 int *guard;
1958 ssize_t ret;
1959 IOTrace_packet iopkt;
1960 if (NULL_PTR (writev))
1961 init_io_intf ();
1962 if (CHCK_REENTRANCE (guard))
1963 return CALL_REAL (writev)(fildes, iov, iovcnt);
1964 PUSH_REENTRANCE (guard);
1965 hrtime_t reqt = gethrtime ();
1966 ret = CALL_REAL (writev)(fildes, iov, iovcnt);
1967 if (RECHCK_REENTRANCE (guard))
1968 {
1969 POP_REENTRANCE (guard);
1970 return ret;
1971 }
1972 hrtime_t grnt = gethrtime ();
1973 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
1974 iopkt.comm.tsize = sizeof ( IOTrace_packet);
1975 iopkt.comm.tstamp = grnt;
1976 iopkt.requested = reqt;
1977 if (ret >= 0)
1978 iopkt.iotype = WRITE_TRACE;
1979 else
1980 iopkt.iotype = WRITE_TRACE_ERROR;
1981 iopkt.fd = fildes;
1982 iopkt.nbyte = ret;
1983 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
1984 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
1985 POP_REENTRANCE (guard);
1986 return ret;
1987 }
1988
1989 /*------------------------------------------------------------- fread */
1990 size_t
1991 fread (void *ptr, size_t size, size_t nitems, FILE *stream)
1992 {
1993 int *guard;
1994 size_t ret;
1995 IOTrace_packet iopkt;
1996 if (NULL_PTR (fread))
1997 init_io_intf ();
1998 if (CHCK_REENTRANCE (guard) || stream == NULL)
1999 return CALL_REAL (fread)(ptr, size, nitems, stream);
2000 PUSH_REENTRANCE (guard);
2001 hrtime_t reqt = gethrtime ();
2002 ret = CALL_REAL (fread)(ptr, size, nitems, stream);
2003 if (RECHCK_REENTRANCE (guard))
2004 {
2005 POP_REENTRANCE (guard);
2006 return ret;
2007 }
2008 hrtime_t grnt = gethrtime ();
2009 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
2010 iopkt.comm.tsize = sizeof ( IOTrace_packet);
2011 iopkt.comm.tstamp = grnt;
2012 iopkt.requested = reqt;
2013 if (ferror (stream) == 0)
2014 {
2015 iopkt.iotype = READ_TRACE;
2016 iopkt.nbyte = ret * size;
2017 }
2018 else
2019 {
2020 iopkt.iotype = READ_TRACE_ERROR;
2021 iopkt.nbyte = 0;
2022 }
2023 iopkt.fd = fileno (stream);
2024 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2025 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2026 POP_REENTRANCE (guard);
2027 return ret;
2028 }
2029
2030 /*------------------------------------------------------------- fwrite */
2031 size_t
2032 fwrite (const void *ptr, size_t size, size_t nitems, FILE *stream)
2033 {
2034 int *guard;
2035 size_t ret;
2036 IOTrace_packet iopkt;
2037 if (NULL_PTR (fwrite))
2038 init_io_intf ();
2039 if (CHCK_REENTRANCE (guard) || stream == NULL)
2040 return CALL_REAL (fwrite)(ptr, size, nitems, stream);
2041 PUSH_REENTRANCE (guard);
2042 hrtime_t reqt = gethrtime ();
2043 ret = CALL_REAL (fwrite)(ptr, size, nitems, stream);
2044 if (RECHCK_REENTRANCE (guard))
2045 {
2046 POP_REENTRANCE (guard);
2047 return ret;
2048 }
2049 hrtime_t grnt = gethrtime ();
2050 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
2051 iopkt.comm.tsize = sizeof ( IOTrace_packet);
2052 iopkt.comm.tstamp = grnt;
2053 iopkt.requested = reqt;
2054 if (ferror (stream) == 0)
2055 {
2056 iopkt.iotype = WRITE_TRACE;
2057 iopkt.nbyte = ret * size;
2058 }
2059 else
2060 {
2061 iopkt.iotype = WRITE_TRACE_ERROR;
2062 iopkt.nbyte = 0;
2063 }
2064 iopkt.fd = fileno (stream);
2065 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2066 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2067 POP_REENTRANCE (guard);
2068 return ret;
2069 }
2070
2071 /*------------------------------------------------------------- pread */
2072 static ssize_t
2073 gprofng_pread (ssize_t(real_pread) (int, void *, size_t, off_t),
2074 int fildes, void *buf, size_t nbyte, off_t offset)
2075 {
2076 int *guard;
2077 ssize_t ret;
2078 IOTrace_packet iopkt;
2079 if (CHCK_REENTRANCE (guard))
2080 return real_pread (fildes, buf, nbyte, offset);
2081 PUSH_REENTRANCE (guard);
2082 hrtime_t reqt = gethrtime ();
2083 ret = real_pread (fildes, buf, nbyte, offset);
2084 if (RECHCK_REENTRANCE (guard))
2085 {
2086 POP_REENTRANCE (guard);
2087 return ret;
2088 }
2089 hrtime_t grnt = gethrtime ();
2090 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
2091 iopkt.comm.tsize = sizeof ( IOTrace_packet);
2092 iopkt.comm.tstamp = grnt;
2093 iopkt.requested = reqt;
2094 if (ret >= 0)
2095 iopkt.iotype = READ_TRACE;
2096 else
2097 iopkt.iotype = READ_TRACE_ERROR;
2098 iopkt.fd = fildes;
2099 iopkt.nbyte = ret;
2100 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl,
2101 iopkt.comm.tstamp, FRINFO_FROM_STACK_ARG, &iopkt);
2102 collector_interface->writeDataRecord (io_hndl, (Common_packet*) &iopkt);
2103 POP_REENTRANCE (guard);
2104 return ret;
2105 }
2106
2107 #define DCL_PREAD(dcl_f) \
2108 ssize_t dcl_f (int fildes, void *buf, size_t nbyte, off_t offset) \
2109 { \
2110 if (__real_pread == NULL) \
2111 init_io_intf (); \
2112 return gprofng_pread (__real_pread, fildes, buf, nbyte, offset); \
2113 }
2114
2115 DCL_FUNC_VER (DCL_PREAD, pread_2_2, pread@GLIBC_2.2)
2116 DCL_PREAD (pread)
2117
2118 /*------------------------------------------------------------- pwrite */
2119
2120 #if !defined(__MUSL_LIBC)
2121 // map interposed symbol versions
2122
2123 SYMVER_ATTRIBUTE (__collector_pwrite_2_2, pwrite@GLIBC_2.2)
2124 int
2125 __collector_pwrite_2_2 (int fildes, const void *buf, size_t nbyte, off_t offset)
2126 {
2127 int *guard;
2128 if (NULL_PTR (pwrite_2_2))
2129 init_io_intf ();
2130 if (CHCK_REENTRANCE (guard))
2131 return CALL_REAL (pwrite_2_2)(fildes, buf, nbyte, offset);
2132 PUSH_REENTRANCE (guard);
2133 hrtime_t reqt = gethrtime ();
2134 ssize_t ret = CALL_REAL (pwrite_2_2)(fildes, buf, nbyte, offset);
2135 if (RECHCK_REENTRANCE (guard))
2136 {
2137 POP_REENTRANCE (guard);
2138 return ret;
2139 }
2140 write_io_packet (fildes, ret, reqt, ret >= 0 ? WRITE_TRACE : WRITE_TRACE_ERROR);
2141 POP_REENTRANCE (guard);
2142 return ret;
2143 }
2144
2145 #endif
2146 ssize_t
2147 pwrite (int fildes, const void *buf, size_t nbyte, off_t offset)
2148 {
2149 int *guard;
2150 if (NULL_PTR (pwrite))
2151 init_io_intf ();
2152 if (CHCK_REENTRANCE (guard))
2153 return CALL_REAL (pwrite)(fildes, buf, nbyte, offset);
2154 PUSH_REENTRANCE (guard);
2155 hrtime_t reqt = gethrtime ();
2156 ssize_t ret = CALL_REAL (pwrite)(fildes, buf, nbyte, offset);
2157 if (RECHCK_REENTRANCE (guard))
2158 {
2159 POP_REENTRANCE (guard);
2160 return ret;
2161 }
2162 write_io_packet (fildes, ret, reqt, ret >= 0 ? WRITE_TRACE : WRITE_TRACE_ERROR);
2163 POP_REENTRANCE (guard);
2164 return ret;
2165 }
2166
2167 /*------------------------------------------------------------- pwrite64 */
2168 #if WSIZE(32) && !defined(__USE_FILE_OFFSET64)
2169 #if !defined(__MUSL_LIBC)
2170 // map interposed symbol versions
2171
2172 SYMVER_ATTRIBUTE (__collector_pwrite64_2_2, pwrite64@GLIBC_2.2)
2173 ssize_t
2174 __collector_pwrite64_2_2 (int fildes, const void *buf, size_t nbyte, off64_t offset)
2175 {
2176 int *guard;
2177 if (NULL_PTR (pwrite64_2_2))
2178 init_io_intf ();
2179 if (CHCK_REENTRANCE (guard))
2180 return CALL_REAL (pwrite64_2_2)(fildes, buf, nbyte, offset);
2181 PUSH_REENTRANCE (guard);
2182 hrtime_t reqt = gethrtime ();
2183 ssize_t ret = CALL_REAL (pwrite64_2_2)(fildes, buf, nbyte, offset);
2184 if (RECHCK_REENTRANCE (guard))
2185 {
2186 POP_REENTRANCE (guard);
2187 return ret;
2188 }
2189 write_io_packet (fildes, ret, reqt, ret >= 0 ? WRITE_TRACE : WRITE_TRACE_ERROR);
2190 POP_REENTRANCE (guard);
2191 return ret;
2192 }
2193 #endif
2194
2195 ssize_t
2196 pwrite64 (int fildes, const void *buf, size_t nbyte, off64_t offset)
2197 {
2198 int *guard;
2199 if (NULL_PTR (pwrite64))
2200 init_io_intf ();
2201 if (CHCK_REENTRANCE (guard))
2202 return CALL_REAL (pwrite64)(fildes, buf, nbyte, offset);
2203 PUSH_REENTRANCE (guard);
2204 hrtime_t reqt = gethrtime ();
2205 ssize_t ret = CALL_REAL (pwrite64)(fildes, buf, nbyte, offset);
2206 if (RECHCK_REENTRANCE (guard))
2207 {
2208 POP_REENTRANCE (guard);
2209 return ret;
2210 }
2211 write_io_packet (fildes, ret, reqt, ret >= 0 ? WRITE_TRACE : WRITE_TRACE_ERROR);
2212 POP_REENTRANCE (guard);
2213 return ret;
2214 }
2215 #endif
2216
2217 /*------------------------------------------------------------- fgets */
2218 char*
2219 fgets (char *s, int n, FILE *stream)
2220 {
2221 int *guard;
2222 char *ptr;
2223 IOTrace_packet iopkt;
2224 if (NULL_PTR (fgets))
2225 init_io_intf ();
2226 if (CHCK_REENTRANCE (guard) || stream == NULL)
2227 return CALL_REAL (fgets)(s, n, stream);
2228 PUSH_REENTRANCE (guard);
2229 hrtime_t reqt = gethrtime ();
2230 ptr = CALL_REAL (fgets)(s, n, stream);
2231 if (RECHCK_REENTRANCE (guard))
2232 {
2233 POP_REENTRANCE (guard);
2234 return ptr;
2235 }
2236 int error = errno;
2237 hrtime_t grnt = gethrtime ();
2238 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
2239 iopkt.comm.tsize = sizeof ( IOTrace_packet);
2240 iopkt.comm.tstamp = grnt;
2241 iopkt.requested = reqt;
2242 if (ptr != NULL)
2243 {
2244 iopkt.iotype = READ_TRACE;
2245 iopkt.nbyte = collector_strlen (ptr);
2246 }
2247 else if (ptr == NULL && error != EAGAIN && error != EBADF && error != EINTR &&
2248 error != EIO && error != EOVERFLOW && error != ENOMEM && error != ENXIO)
2249 {
2250 iopkt.iotype = READ_TRACE;
2251 iopkt.nbyte = 0;
2252 }
2253 else
2254 iopkt.iotype = READ_TRACE_ERROR;
2255 iopkt.fd = fileno (stream);
2256 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2257 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2258 POP_REENTRANCE (guard);
2259 return ptr;
2260 }
2261
2262 /*------------------------------------------------------------- fputs */
2263 int
2264 fputs (const char *s, FILE *stream)
2265 {
2266 int *guard;
2267 int ret;
2268 IOTrace_packet iopkt;
2269 if (NULL_PTR (fputs))
2270 init_io_intf ();
2271 if (CHCK_REENTRANCE (guard) || stream == NULL)
2272 return CALL_REAL (fputs)(s, stream);
2273 PUSH_REENTRANCE (guard);
2274 hrtime_t reqt = gethrtime ();
2275 ret = CALL_REAL (fputs)(s, stream);
2276 if (RECHCK_REENTRANCE (guard))
2277 {
2278 POP_REENTRANCE (guard);
2279 return ret;
2280 }
2281 hrtime_t grnt = gethrtime ();
2282 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
2283 iopkt.comm.tsize = sizeof ( IOTrace_packet);
2284 iopkt.comm.tstamp = grnt;
2285 iopkt.requested = reqt;
2286 if (ret != EOF)
2287 {
2288 iopkt.iotype = WRITE_TRACE;
2289 iopkt.nbyte = ret;
2290 }
2291 else
2292 {
2293 iopkt.iotype = WRITE_TRACE_ERROR;
2294 iopkt.nbyte = 0;
2295 }
2296 iopkt.fd = fileno (stream);
2297 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2298 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2299 POP_REENTRANCE (guard);
2300 return ret;
2301 }
2302
2303 /*------------------------------------------------------------- fputc */
2304 int
2305 fputc (int c, FILE *stream)
2306 {
2307 int *guard;
2308 int ret;
2309 IOTrace_packet iopkt;
2310 if (NULL_PTR (fputc))
2311 init_io_intf ();
2312 if (CHCK_REENTRANCE (guard) || stream == NULL)
2313 return CALL_REAL (fputc)(c, stream);
2314 PUSH_REENTRANCE (guard);
2315 hrtime_t reqt = gethrtime ();
2316 ret = CALL_REAL (fputc)(c, stream);
2317 if (RECHCK_REENTRANCE (guard))
2318 {
2319 POP_REENTRANCE (guard);
2320 return ret;
2321 }
2322 hrtime_t grnt = gethrtime ();
2323 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
2324 iopkt.comm.tsize = sizeof ( IOTrace_packet);
2325 iopkt.comm.tstamp = grnt;
2326 iopkt.requested = reqt;
2327 if (ret != EOF)
2328 {
2329 iopkt.iotype = WRITE_TRACE;
2330 iopkt.nbyte = ret;
2331 }
2332 else
2333 {
2334 iopkt.iotype = WRITE_TRACE_ERROR;
2335 iopkt.nbyte = 0;
2336 }
2337 iopkt.fd = fileno (stream);
2338 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2339 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2340 POP_REENTRANCE (guard);
2341 return ret;
2342 }
2343
2344 /*------------------------------------------------------------- fprintf */
2345 int
2346 fprintf (FILE *stream, const char *format, ...)
2347 {
2348 int *guard;
2349 int ret;
2350 IOTrace_packet iopkt;
2351 va_list ap;
2352 va_start (ap, format);
2353 if (NULL_PTR (fprintf))
2354 init_io_intf ();
2355 if (NULL_PTR (vfprintf))
2356 init_io_intf ();
2357 if (CHCK_REENTRANCE (guard) || stream == NULL)
2358 {
2359 ret = CALL_REAL (vfprintf)(stream, format, ap);
2360 va_end (ap);
2361 return ret;
2362 }
2363 PUSH_REENTRANCE (guard);
2364 hrtime_t reqt = gethrtime ();
2365 ret = CALL_REAL (vfprintf)(stream, format, ap);
2366 va_end (ap);
2367 if (RECHCK_REENTRANCE (guard))
2368 {
2369 POP_REENTRANCE (guard);
2370 return ret;
2371 }
2372 hrtime_t grnt = gethrtime ();
2373 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
2374 iopkt.comm.tsize = sizeof ( IOTrace_packet);
2375 iopkt.comm.tstamp = grnt;
2376 iopkt.requested = reqt;
2377 if (ret >= 0)
2378 iopkt.iotype = WRITE_TRACE;
2379 else
2380 iopkt.iotype = WRITE_TRACE_ERROR;
2381 iopkt.fd = fileno (stream);
2382 iopkt.nbyte = ret;
2383 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2384 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2385 POP_REENTRANCE (guard);
2386 return ret;
2387 }
2388
2389 /*------------------------------------------------------------- vfprintf */
2390 int
2391 vfprintf (FILE *stream, const char *format, va_list ap)
2392 {
2393 int *guard;
2394 int ret;
2395 IOTrace_packet iopkt;
2396 if (NULL_PTR (vfprintf))
2397 init_io_intf ();
2398 if (CHCK_REENTRANCE (guard) || stream == NULL)
2399 return CALL_REAL (vfprintf)(stream, format, ap);
2400 PUSH_REENTRANCE (guard);
2401 hrtime_t reqt = gethrtime ();
2402 ret = CALL_REAL (vfprintf)(stream, format, ap);
2403 if (RECHCK_REENTRANCE (guard))
2404 {
2405 POP_REENTRANCE (guard);
2406 return ret;
2407 }
2408 hrtime_t grnt = gethrtime ();
2409 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
2410 iopkt.comm.tsize = sizeof ( IOTrace_packet);
2411 iopkt.comm.tstamp = grnt;
2412 iopkt.requested = reqt;
2413 if (ret >= 0)
2414 iopkt.iotype = WRITE_TRACE;
2415 else
2416 iopkt.iotype = WRITE_TRACE_ERROR;
2417 iopkt.fd = fileno (stream);
2418 iopkt.nbyte = ret;
2419 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2420 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2421 POP_REENTRANCE (guard);
2422 return ret;
2423 }
2424
2425 /*------------------------------------------------------------- lseek */
2426 off_t
2427 lseek (int fildes, off_t offset, int whence)
2428 {
2429 int *guard;
2430 off_t ret;
2431 IOTrace_packet iopkt;
2432 if (NULL_PTR (lseek))
2433 init_io_intf ();
2434 if (CHCK_REENTRANCE (guard))
2435 return CALL_REAL (lseek)(fildes, offset, whence);
2436 PUSH_REENTRANCE (guard);
2437 hrtime_t reqt = gethrtime ();
2438 ret = CALL_REAL (lseek)(fildes, offset, whence);
2439 if (RECHCK_REENTRANCE (guard))
2440 {
2441 POP_REENTRANCE (guard);
2442 return ret;
2443 }
2444 hrtime_t grnt = gethrtime ();
2445 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
2446 iopkt.comm.tsize = sizeof (IOTrace_packet);
2447 iopkt.comm.tstamp = grnt;
2448 iopkt.requested = reqt;
2449 if (ret != -1)
2450 iopkt.iotype = OTHERIO_TRACE;
2451 else
2452 iopkt.iotype = OTHERIO_TRACE_ERROR;
2453 iopkt.fd = fildes;
2454 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2455 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2456 POP_REENTRANCE (guard);
2457 return ret;
2458 }
2459
2460 /*------------------------------------------------------------- llseek */
2461 offset_t
2462 llseek (int fildes, offset_t offset, int whence)
2463 {
2464 int *guard;
2465 offset_t ret;
2466 IOTrace_packet iopkt;
2467 if (NULL_PTR (llseek))
2468 init_io_intf ();
2469 if (CHCK_REENTRANCE (guard))
2470 return CALL_REAL (llseek)(fildes, offset, whence);
2471 PUSH_REENTRANCE (guard);
2472 hrtime_t reqt = gethrtime ();
2473 ret = CALL_REAL (llseek)(fildes, offset, whence);
2474 if (RECHCK_REENTRANCE (guard))
2475 {
2476 POP_REENTRANCE (guard);
2477 return ret;
2478 }
2479 hrtime_t grnt = gethrtime ();
2480 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
2481 iopkt.comm.tsize = sizeof (IOTrace_packet);
2482 iopkt.comm.tstamp = grnt;
2483 iopkt.requested = reqt;
2484 if (ret != -1)
2485 iopkt.iotype = OTHERIO_TRACE;
2486 else
2487 iopkt.iotype = OTHERIO_TRACE_ERROR;
2488 iopkt.fd = fildes;
2489 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2490 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2491 POP_REENTRANCE (guard);
2492 return ret;
2493 }
2494
2495 /*------------------------------------------------------------- chmod */
2496 int
2497 chmod (const char *path, mode_t mode)
2498 {
2499 int *guard;
2500 int ret;
2501 void *packet;
2502 IOTrace_packet *iopkt;
2503 size_t sz;
2504 unsigned pktSize;
2505 if (NULL_PTR (chmod))
2506 init_io_intf ();
2507 if (CHCK_REENTRANCE (guard) || path == NULL)
2508 return CALL_REAL (chmod)(path, mode);
2509 PUSH_REENTRANCE (guard);
2510 hrtime_t reqt = gethrtime ();
2511 ret = CALL_REAL (chmod)(path, mode);
2512 if (RECHCK_REENTRANCE (guard))
2513 {
2514 POP_REENTRANCE (guard);
2515 return ret;
2516 }
2517 hrtime_t grnt = gethrtime ();
2518 sz = collector_strlen (path);
2519 pktSize = sizeof (IOTrace_packet) + sz;
2520 pktSize = collector_align_pktsize (pktSize);
2521 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
2522 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
2523 if (packet != NULL)
2524 {
2525 iopkt = (IOTrace_packet *) packet;
2526 collector_memset (iopkt, 0, pktSize);
2527 iopkt->comm.tsize = pktSize;
2528 iopkt->comm.tstamp = grnt;
2529 iopkt->requested = reqt;
2530 if (ret != -1)
2531 iopkt->iotype = OTHERIO_TRACE;
2532 else
2533 iopkt->iotype = OTHERIO_TRACE_ERROR;
2534 collector_strncpy (&(iopkt->fname), path, sz);
2535 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2536 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
2537 collector_interface->freeCSize (io_heap, packet, pktSize);
2538 }
2539 else
2540 {
2541 Tprintf (0, "iotrace: ERROR: chmod cannot allocate memory\n");
2542 return 0;
2543 }
2544 POP_REENTRANCE (guard);
2545 return ret;
2546 }
2547
2548 /*------------------------------------------------------------- access */
2549 int
2550 access (const char *path, int amode)
2551 {
2552 int *guard;
2553 int ret;
2554 void *packet;
2555 IOTrace_packet *iopkt;
2556 size_t sz;
2557 unsigned pktSize;
2558 if (NULL_PTR (access))
2559 init_io_intf ();
2560 if (CHCK_REENTRANCE (guard) || path == NULL)
2561 return CALL_REAL (access)(path, amode);
2562 PUSH_REENTRANCE (guard);
2563 hrtime_t reqt = gethrtime ();
2564 ret = CALL_REAL (access)(path, amode);
2565 if (RECHCK_REENTRANCE (guard))
2566 {
2567 POP_REENTRANCE (guard);
2568 return ret;
2569 }
2570 hrtime_t grnt = gethrtime ();
2571 sz = collector_strlen (path);
2572 pktSize = sizeof (IOTrace_packet) + sz;
2573 pktSize = collector_align_pktsize (pktSize);
2574 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
2575 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
2576 if (packet != NULL)
2577 {
2578 iopkt = (IOTrace_packet *) packet;
2579 collector_memset (iopkt, 0, pktSize);
2580 iopkt->comm.tsize = pktSize;
2581 iopkt->comm.tstamp = grnt;
2582 iopkt->requested = reqt;
2583 if (ret != -1)
2584 iopkt->iotype = OTHERIO_TRACE;
2585 else
2586 iopkt->iotype = OTHERIO_TRACE_ERROR;
2587 collector_strncpy (&(iopkt->fname), path, sz);
2588 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2589 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
2590 collector_interface->freeCSize (io_heap, packet, pktSize);
2591 }
2592 else
2593 {
2594 Tprintf (0, "iotrace: ERROR: access cannot allocate memory\n");
2595 return 0;
2596 }
2597 POP_REENTRANCE (guard);
2598 return ret;
2599 }
2600
2601 /*------------------------------------------------------------- rename */
2602 int
2603 rename (const char *old, const char *new)
2604 {
2605 int *guard;
2606 int ret;
2607 void *packet;
2608 IOTrace_packet *iopkt;
2609 size_t sz;
2610 unsigned pktSize;
2611 if (NULL_PTR (rename))
2612 init_io_intf ();
2613 if (CHCK_REENTRANCE (guard) || new == NULL)
2614 return CALL_REAL (rename)(old, new);
2615 PUSH_REENTRANCE (guard);
2616 hrtime_t reqt = gethrtime ();
2617 ret = CALL_REAL (rename)(old, new);
2618 if (RECHCK_REENTRANCE (guard))
2619 {
2620 POP_REENTRANCE (guard);
2621 return ret;
2622 }
2623 hrtime_t grnt = gethrtime ();
2624 sz = collector_strlen (new);
2625 pktSize = sizeof (IOTrace_packet) + sz;
2626 pktSize = collector_align_pktsize (pktSize);
2627 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
2628 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
2629 if (packet != NULL)
2630 {
2631 iopkt = (IOTrace_packet *) packet;
2632 collector_memset (iopkt, 0, pktSize);
2633 iopkt->comm.tsize = pktSize;
2634 iopkt->comm.tstamp = grnt;
2635 iopkt->requested = reqt;
2636 if (ret != -1)
2637 iopkt->iotype = OTHERIO_TRACE;
2638 else
2639 iopkt->iotype = OTHERIO_TRACE_ERROR;
2640 collector_strncpy (&(iopkt->fname), new, sz);
2641 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2642 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
2643 collector_interface->freeCSize (io_heap, packet, pktSize);
2644 }
2645 else
2646 {
2647 Tprintf (0, "iotrace: ERROR: rename cannot allocate memory\n");
2648 return 0;
2649 }
2650 POP_REENTRANCE (guard);
2651 return ret;
2652 }
2653
2654 /*------------------------------------------------------------- mkdir */
2655 int
2656 mkdir (const char *path, mode_t mode)
2657 {
2658 int *guard;
2659 int ret;
2660 void *packet;
2661 IOTrace_packet *iopkt;
2662 size_t sz;
2663 unsigned pktSize;
2664 if (NULL_PTR (mkdir))
2665 init_io_intf ();
2666 if (CHCK_REENTRANCE (guard) || path == NULL)
2667 return CALL_REAL (mkdir)(path, mode);
2668 PUSH_REENTRANCE (guard);
2669 hrtime_t reqt = gethrtime ();
2670 ret = CALL_REAL (mkdir)(path, mode);
2671 if (RECHCK_REENTRANCE (guard))
2672 {
2673 POP_REENTRANCE (guard);
2674 return ret;
2675 }
2676 hrtime_t grnt = gethrtime ();
2677 sz = collector_strlen (path);
2678 pktSize = sizeof (IOTrace_packet) + sz;
2679 pktSize = collector_align_pktsize (pktSize);
2680 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
2681 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
2682 if (packet != NULL)
2683 {
2684 iopkt = (IOTrace_packet *) packet;
2685 collector_memset (iopkt, 0, pktSize);
2686 iopkt->comm.tsize = pktSize;
2687 iopkt->comm.tstamp = grnt;
2688 iopkt->requested = reqt;
2689 if (ret != -1)
2690 iopkt->iotype = OTHERIO_TRACE;
2691 else
2692 iopkt->iotype = OTHERIO_TRACE_ERROR;
2693 collector_strncpy (&(iopkt->fname), path, sz);
2694 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2695 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
2696 collector_interface->freeCSize (io_heap, packet, pktSize);
2697 }
2698 else
2699 {
2700 Tprintf (0, "iotrace: ERROR: mkdir cannot allocate memory\n");
2701 return 0;
2702 }
2703 POP_REENTRANCE (guard);
2704 return ret;
2705 }
2706
2707 /*------------------------------------------------------------- getdents */
2708 int
2709 getdents (int fildes, struct dirent *buf, size_t nbyte)
2710 {
2711 int *guard;
2712 int ret;
2713 IOTrace_packet iopkt;
2714 if (NULL_PTR (getdents))
2715 init_io_intf ();
2716 if (CHCK_REENTRANCE (guard))
2717 return CALL_REAL (getdents)(fildes, buf, nbyte);
2718 PUSH_REENTRANCE (guard);
2719 hrtime_t reqt = gethrtime ();
2720 ret = CALL_REAL (getdents)(fildes, buf, nbyte);
2721 if (RECHCK_REENTRANCE (guard))
2722 {
2723 POP_REENTRANCE (guard);
2724 return ret;
2725 }
2726 hrtime_t grnt = gethrtime ();
2727 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
2728 iopkt.comm.tsize = sizeof (IOTrace_packet);
2729 iopkt.comm.tstamp = grnt;
2730 iopkt.requested = reqt;
2731 if (ret != -1)
2732 iopkt.iotype = OTHERIO_TRACE;
2733 else
2734 iopkt.iotype = OTHERIO_TRACE_ERROR;
2735 iopkt.fd = fildes;
2736 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2737 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2738 POP_REENTRANCE (guard);
2739 return ret;
2740 }
2741
2742 /*------------------------------------------------------------- unlink */
2743 int
2744 unlink (const char *path)
2745 {
2746 int *guard;
2747 int ret;
2748 void *packet;
2749 IOTrace_packet *iopkt;
2750 size_t sz;
2751 unsigned pktSize;
2752 if (NULL_PTR (unlink))
2753 init_io_intf ();
2754 if (CHCK_REENTRANCE (guard) || path == NULL)
2755 return CALL_REAL (unlink)(path);
2756 PUSH_REENTRANCE (guard);
2757 hrtime_t reqt = gethrtime ();
2758 ret = CALL_REAL (unlink)(path);
2759 if (RECHCK_REENTRANCE (guard))
2760 {
2761 POP_REENTRANCE (guard);
2762 return ret;
2763 }
2764 hrtime_t grnt = gethrtime ();
2765 sz = collector_strlen (path);
2766 pktSize = sizeof (IOTrace_packet) + sz;
2767 pktSize = collector_align_pktsize (pktSize);
2768 Tprintf (DBG_LT1, "iotrace allocating %u from io_heap\n", pktSize);
2769 packet = collector_interface->allocCSize (io_heap, pktSize, 1);
2770 if (packet != NULL)
2771 {
2772 iopkt = (IOTrace_packet *) packet;
2773 collector_memset (iopkt, 0, pktSize);
2774 iopkt->comm.tsize = pktSize;
2775 iopkt->comm.tstamp = grnt;
2776 iopkt->requested = reqt;
2777 if (ret != -1)
2778 iopkt->iotype = OTHERIO_TRACE;
2779 else
2780 iopkt->iotype = OTHERIO_TRACE_ERROR;
2781 collector_strncpy (&(iopkt->fname), path, sz);
2782 iopkt->comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt->comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2783 collector_interface->writeDataRecord (io_hndl, (Common_packet*) iopkt);
2784 collector_interface->freeCSize (io_heap, packet, pktSize);
2785 }
2786 else
2787 {
2788 Tprintf (0, "iotrace: ERROR: unlink cannot allocate memory\n");
2789 return 0;
2790 }
2791 POP_REENTRANCE (guard);
2792 return ret;
2793 }
2794
2795 /*------------------------------------------------------------- fseek */
2796 int
2797 fseek (FILE *stream, long offset, int whence)
2798 {
2799 int *guard;
2800 int ret;
2801 IOTrace_packet iopkt;
2802 if (NULL_PTR (fseek))
2803 init_io_intf ();
2804 if (CHCK_REENTRANCE (guard) || stream == NULL)
2805 return CALL_REAL (fseek)(stream, offset, whence);
2806 PUSH_REENTRANCE (guard);
2807 hrtime_t reqt = gethrtime ();
2808 ret = CALL_REAL (fseek)(stream, offset, whence);
2809 if (RECHCK_REENTRANCE (guard))
2810 {
2811 POP_REENTRANCE (guard);
2812 return ret;
2813 }
2814 hrtime_t grnt = gethrtime ();
2815 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
2816 iopkt.comm.tsize = sizeof (IOTrace_packet);
2817 iopkt.comm.tstamp = grnt;
2818 iopkt.requested = reqt;
2819 if (ret != -1)
2820 iopkt.iotype = OTHERIO_TRACE;
2821 else
2822 iopkt.iotype = OTHERIO_TRACE_ERROR;
2823 iopkt.fd = fileno (stream);
2824 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2825 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2826 POP_REENTRANCE (guard);
2827 return ret;
2828 }
2829
2830 /*------------------------------------------------------------- rewind */
2831 void
2832 rewind (FILE *stream)
2833 {
2834 int *guard;
2835 IOTrace_packet iopkt;
2836 if (NULL_PTR (rewind))
2837 init_io_intf ();
2838 if (CHCK_REENTRANCE (guard) || stream == NULL)
2839 {
2840 CALL_REAL (rewind)(stream);
2841 return;
2842 }
2843 PUSH_REENTRANCE (guard);
2844 hrtime_t reqt = gethrtime ();
2845 CALL_REAL (rewind)(stream);
2846 if (RECHCK_REENTRANCE (guard))
2847 {
2848 POP_REENTRANCE (guard);
2849 return;
2850 }
2851 hrtime_t grnt = gethrtime ();
2852 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
2853 iopkt.comm.tsize = sizeof (IOTrace_packet);
2854 iopkt.comm.tstamp = grnt;
2855 iopkt.requested = reqt;
2856 iopkt.iotype = OTHERIO_TRACE;
2857 iopkt.fd = fileno (stream);
2858 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2859 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2860 POP_REENTRANCE (guard);
2861 }
2862
2863 /*------------------------------------------------------------- ftell */
2864 long
2865 ftell (FILE *stream)
2866 {
2867 int *guard;
2868 long ret;
2869 IOTrace_packet iopkt;
2870 if (NULL_PTR (ftell))
2871 init_io_intf ();
2872 if (CHCK_REENTRANCE (guard) || stream == NULL)
2873 return CALL_REAL (ftell)(stream);
2874 PUSH_REENTRANCE (guard);
2875 hrtime_t reqt = gethrtime ();
2876 ret = CALL_REAL (ftell)(stream);
2877 if (RECHCK_REENTRANCE (guard))
2878 {
2879 POP_REENTRANCE (guard);
2880 return ret;
2881 }
2882 hrtime_t grnt = gethrtime ();
2883 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
2884 iopkt.comm.tsize = sizeof (IOTrace_packet);
2885 iopkt.comm.tstamp = grnt;
2886 iopkt.requested = reqt;
2887 if (ret != -1)
2888 iopkt.iotype = OTHERIO_TRACE;
2889 else
2890 iopkt.iotype = OTHERIO_TRACE_ERROR;
2891 iopkt.fd = fileno (stream);
2892 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
2893 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
2894 POP_REENTRANCE (guard);
2895 return ret;
2896 }
2897
2898 /*------------------------------------------------------------- fgetpos */
2899 static int
2900 gprofng_fgetpos (int(real_fgetpos) (FILE *stream, fpos_t *pos),
2901 FILE *stream, fpos_t *pos)
2902 {
2903 int *guard;
2904 int ret;
2905 IOTrace_packet iopkt;
2906 if (CHCK_REENTRANCE (guard) || stream == NULL)
2907 return real_fgetpos (stream, pos);
2908 PUSH_REENTRANCE (guard);
2909 hrtime_t reqt = gethrtime ();
2910 ret = real_fgetpos (stream, pos);
2911 if (RECHCK_REENTRANCE (guard))
2912 {
2913 POP_REENTRANCE (guard);
2914 return ret;
2915 }
2916 hrtime_t grnt = gethrtime ();
2917 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
2918 iopkt.comm.tsize = sizeof (IOTrace_packet);
2919 iopkt.comm.tstamp = grnt;
2920 iopkt.requested = reqt;
2921 if (ret == 0)
2922 iopkt.iotype = OTHERIO_TRACE;
2923 else
2924 iopkt.iotype = OTHERIO_TRACE_ERROR;
2925 iopkt.fd = fileno (stream);
2926 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl,
2927 iopkt.comm.tstamp, FRINFO_FROM_STACK_ARG, &iopkt);
2928 collector_interface->writeDataRecord (io_hndl, (Common_packet*) &iopkt);
2929 POP_REENTRANCE (guard);
2930 return ret;
2931 }
2932
2933 #define DCL_FGETPOS(dcl_f) \
2934 int dcl_f (FILE *stream, fpos_t *pos) \
2935 { \
2936 if (__real_fgetpos == NULL) \
2937 init_io_intf (); \
2938 return gprofng_fgetpos (__real_fgetpos, stream, pos); \
2939 }
2940
2941 DCL_FUNC_VER (DCL_FGETPOS, fgetpos_2_17, fgetpos@GLIBC_2.17)
2942 DCL_FUNC_VER (DCL_FGETPOS, fgetpos_2_2_5, fgetpos@GLIBC_2.2.5)
2943 DCL_FUNC_VER (DCL_FGETPOS, fgetpos_2_2, fgetpos@GLIBC_2.2)
2944 DCL_FUNC_VER (DCL_FGETPOS, fgetpos_2_0, fgetpos@GLIBC_2.0)
2945 DCL_FGETPOS (fgetpos)
2946
2947 /*------------------------------------------------------------- fgetpos64 */
2948 static int
2949 gprofng_fgetpos64 (int(real_fgetpos64) (), FILE *stream, fpos64_t *pos)
2950 {
2951 int *guard;
2952 int ret;
2953 IOTrace_packet iopkt;
2954 if (CHCK_REENTRANCE (guard) || stream == NULL)
2955 return real_fgetpos64 (stream, pos);
2956 PUSH_REENTRANCE (guard);
2957 hrtime_t reqt = gethrtime ();
2958 ret = real_fgetpos64 (stream, pos);
2959 if (RECHCK_REENTRANCE (guard))
2960 {
2961 POP_REENTRANCE (guard);
2962 return ret;
2963 }
2964 hrtime_t grnt = gethrtime ();
2965 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
2966 iopkt.comm.tsize = sizeof (IOTrace_packet);
2967 iopkt.comm.tstamp = grnt;
2968 iopkt.requested = reqt;
2969 if (ret == 0)
2970 iopkt.iotype = OTHERIO_TRACE;
2971 else
2972 iopkt.iotype = OTHERIO_TRACE_ERROR;
2973 iopkt.fd = fileno (stream);
2974 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl,
2975 iopkt.comm.tstamp, FRINFO_FROM_STACK_ARG, &iopkt);
2976 collector_interface->writeDataRecord (io_hndl, (Common_packet*) &iopkt);
2977 POP_REENTRANCE (guard);
2978 return ret;
2979 }
2980
2981 #define DCL_FGETPOS64(dcl_f) \
2982 int dcl_f (FILE *stream, fpos64_t *pos) \
2983 { \
2984 if (__real_fgetpos64 == NULL) \
2985 init_io_intf (); \
2986 return gprofng_fgetpos64 (__real_fgetpos64, stream, pos); \
2987 }
2988
2989 DCL_FUNC_VER (DCL_FGETPOS64, fgetpos64_2_17, fgetpos64@GLIBC_2.17)
2990 DCL_FUNC_VER (DCL_FGETPOS64, fgetpos64_2_2_5, fgetpos64@GLIBC_2.2.5)
2991 DCL_FUNC_VER (DCL_FGETPOS64, fgetpos64_2_2, fgetpos64@GLIBC_2.2)
2992 DCL_FUNC_VER (DCL_FGETPOS64, fgetpos64_2_1, fgetpos64@GLIBC_2.1)
2993 #if !defined(__USE_LARGEFILE64)
2994 DCL_FGETPOS64 (fgetpos64)
2995 #endif
2996 /*------------------------------------------------------------- fsetpos */
2997 static int
2998 gprofng_fsetpos (int(real_fsetpos) (FILE *, const fpos_t *),
2999 FILE *stream, const fpos_t *pos)
3000 {
3001 int *guard;
3002 int ret;
3003 IOTrace_packet iopkt;
3004 if (CHCK_REENTRANCE (guard) || stream == NULL)
3005 return real_fsetpos (stream, pos);
3006 PUSH_REENTRANCE (guard);
3007 hrtime_t reqt = gethrtime ();
3008 ret = real_fsetpos (stream, pos);
3009 if (RECHCK_REENTRANCE (guard))
3010 {
3011 POP_REENTRANCE (guard);
3012 return ret;
3013 }
3014 hrtime_t grnt = gethrtime ();
3015 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
3016 iopkt.comm.tsize = sizeof (IOTrace_packet);
3017 iopkt.comm.tstamp = grnt;
3018 iopkt.requested = reqt;
3019 if (ret == 0)
3020 iopkt.iotype = OTHERIO_TRACE;
3021 else
3022 iopkt.iotype = OTHERIO_TRACE_ERROR;
3023 iopkt.fd = fileno (stream);
3024 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl,
3025 iopkt.comm.tstamp, FRINFO_FROM_STACK_ARG, &iopkt);
3026 collector_interface->writeDataRecord (io_hndl, (Common_packet*) &iopkt);
3027 POP_REENTRANCE (guard);
3028 return ret;
3029 }
3030
3031 #define DCL_FSETPOS(dcl_f) \
3032 int dcl_f (FILE *stream, const fpos_t *pos) \
3033 { \
3034 if (__real_fsetpos == NULL) \
3035 init_io_intf (); \
3036 return gprofng_fsetpos (__real_fsetpos, stream, pos); \
3037 }
3038
3039 DCL_FUNC_VER (DCL_FSETPOS, fsetpos_2_17, fsetpos@GLIBC_2.17)
3040 DCL_FUNC_VER (DCL_FSETPOS, fsetpos_2_2_5, fsetpos@GLIBC_2.2.5)
3041 DCL_FUNC_VER (DCL_FSETPOS, fsetpos_2_2, fsetpos@GLIBC_2.2)
3042 DCL_FUNC_VER (DCL_FSETPOS, fsetpos_2_0, fsetpos@GLIBC_2.0)
3043 DCL_FSETPOS (fsetpos)
3044
3045 /*------------------------------------------------------------- fsetpos64 */
3046 static int
3047 gprofng_fsetpos64 (int(real_fsetpos64) (FILE *, const fpos64_t *),
3048 FILE *stream, const fpos64_t *pos)
3049 {
3050 int *guard;
3051 int ret;
3052 IOTrace_packet iopkt;
3053 if (CHCK_REENTRANCE (guard) || stream == NULL)
3054 return real_fsetpos64 (stream, pos);
3055 PUSH_REENTRANCE (guard);
3056 hrtime_t reqt = gethrtime ();
3057 ret = real_fsetpos64 (stream, pos);
3058 if (RECHCK_REENTRANCE (guard))
3059 {
3060 POP_REENTRANCE (guard);
3061 return ret;
3062 }
3063 hrtime_t grnt = gethrtime ();
3064 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
3065 iopkt.comm.tsize = sizeof (IOTrace_packet);
3066 iopkt.comm.tstamp = grnt;
3067 iopkt.requested = reqt;
3068 if (ret == 0)
3069 iopkt.iotype = OTHERIO_TRACE;
3070 else
3071 iopkt.iotype = OTHERIO_TRACE_ERROR;
3072 iopkt.fd = fileno (stream);
3073 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl,
3074 iopkt.comm.tstamp, FRINFO_FROM_STACK_ARG, &iopkt);
3075 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
3076 POP_REENTRANCE (guard);
3077 return ret;
3078 }
3079
3080 #define DCL_FSETPOS64(dcl_f) \
3081 int dcl_f (FILE *stream, const fpos64_t *pos) \
3082 { \
3083 if (__real_fsetpos64 == NULL) \
3084 init_io_intf (); \
3085 return gprofng_fsetpos64 (__real_fsetpos64, stream, pos); \
3086 }
3087
3088 DCL_FUNC_VER (DCL_FSETPOS64, fsetpos64_2_17, fsetpos64@GLIBC_2.17)
3089 DCL_FUNC_VER (DCL_FSETPOS64, fsetpos64_2_2_5, fsetpos64@GLIBC_2.2.5)
3090 DCL_FUNC_VER (DCL_FSETPOS64, fsetpos64_2_2, fsetpos64@GLIBC_2.2)
3091 DCL_FUNC_VER (DCL_FSETPOS64, fsetpos64_2_1, fsetpos64@GLIBC_2.1)
3092 #if !defined(__USE_LARGEFILE64)
3093 DCL_FSETPOS64 (fsetpos64)
3094 #endif
3095 /*------------------------------------------------------------- fsync */
3096 int
3097 fsync (int fildes)
3098 {
3099 int *guard;
3100 int ret;
3101 IOTrace_packet iopkt;
3102 if (NULL_PTR (fsync))
3103 init_io_intf ();
3104 if (CHCK_REENTRANCE (guard))
3105 return CALL_REAL (fsync)(fildes);
3106 PUSH_REENTRANCE (guard);
3107 hrtime_t reqt = gethrtime ();
3108 ret = CALL_REAL (fsync)(fildes);
3109 if (RECHCK_REENTRANCE (guard))
3110 {
3111 POP_REENTRANCE (guard);
3112 return ret;
3113 }
3114 hrtime_t grnt = gethrtime ();
3115 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
3116 iopkt.comm.tsize = sizeof (IOTrace_packet);
3117 iopkt.comm.tstamp = grnt;
3118 iopkt.requested = reqt;
3119 if (ret == 0)
3120 iopkt.iotype = OTHERIO_TRACE;
3121 else
3122 iopkt.iotype = OTHERIO_TRACE_ERROR;
3123 iopkt.fd = fildes;
3124 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
3125 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
3126 POP_REENTRANCE (guard);
3127 return ret;
3128 }
3129
3130 /*------------------------------------------------------------- readdir */
3131 struct dirent*
3132 readdir (DIR *dirp)
3133 {
3134 int *guard;
3135 struct dirent *ptr;
3136 IOTrace_packet iopkt;
3137 if (NULL_PTR (readdir))
3138 init_io_intf ();
3139 if (CHCK_REENTRANCE (guard))
3140 return CALL_REAL (readdir)(dirp);
3141 PUSH_REENTRANCE (guard);
3142 hrtime_t reqt = gethrtime ();
3143 ptr = CALL_REAL (readdir)(dirp);
3144 if (RECHCK_REENTRANCE (guard))
3145 {
3146 POP_REENTRANCE (guard);
3147 return ptr;
3148 }
3149 hrtime_t grnt = gethrtime ();
3150 collector_memset (&iopkt, 0, sizeof ( IOTrace_packet));
3151 iopkt.comm.tsize = sizeof ( IOTrace_packet);
3152 iopkt.comm.tstamp = grnt;
3153 iopkt.requested = reqt;
3154 if (ptr != NULL)
3155 iopkt.iotype = OTHERIO_TRACE;
3156 else
3157 iopkt.iotype = OTHERIO_TRACE_ERROR;
3158 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
3159 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
3160 POP_REENTRANCE (guard);
3161 return ptr;
3162 }
3163
3164 /*------------------------------------------------------------- flock */
3165 int
3166 flock (int fd, int operation)
3167 {
3168 int *guard;
3169 int ret;
3170 IOTrace_packet iopkt;
3171 if (NULL_PTR (flock))
3172 init_io_intf ();
3173 if (CHCK_REENTRANCE (guard))
3174 return CALL_REAL (flock)(fd, operation);
3175 PUSH_REENTRANCE (guard);
3176 hrtime_t reqt = gethrtime ();
3177 ret = CALL_REAL (flock)(fd, operation);
3178 if (RECHCK_REENTRANCE (guard))
3179 {
3180 POP_REENTRANCE (guard);
3181 return ret;
3182 }
3183 hrtime_t grnt = gethrtime ();
3184 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
3185 iopkt.comm.tsize = sizeof (IOTrace_packet);
3186 iopkt.comm.tstamp = grnt;
3187 iopkt.requested = reqt;
3188 if (ret == 0)
3189 iopkt.iotype = OTHERIO_TRACE;
3190 else
3191 iopkt.iotype = OTHERIO_TRACE_ERROR;
3192 iopkt.fd = fd;
3193 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
3194 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
3195 POP_REENTRANCE (guard);
3196 return ret;
3197 }
3198
3199 /*------------------------------------------------------------- lockf */
3200 int
3201 lockf (int fildes, int function, off_t size)
3202 {
3203 int *guard;
3204 int ret;
3205 IOTrace_packet iopkt;
3206 if (NULL_PTR (lockf))
3207 init_io_intf ();
3208 if (CHCK_REENTRANCE (guard))
3209 return CALL_REAL (lockf)(fildes, function, size);
3210 PUSH_REENTRANCE (guard);
3211 hrtime_t reqt = gethrtime ();
3212 ret = CALL_REAL (lockf)(fildes, function, size);
3213 if (RECHCK_REENTRANCE (guard))
3214 {
3215 POP_REENTRANCE (guard);
3216 return ret;
3217 }
3218 hrtime_t grnt = gethrtime ();
3219 collector_memset (&iopkt, 0, sizeof (IOTrace_packet));
3220 iopkt.comm.tsize = sizeof (IOTrace_packet);
3221 iopkt.comm.tstamp = grnt;
3222 iopkt.requested = reqt;
3223 if (ret == 0)
3224 iopkt.iotype = OTHERIO_TRACE;
3225 else
3226 iopkt.iotype = OTHERIO_TRACE_ERROR;
3227 iopkt.fd = fildes;
3228 iopkt.comm.frinfo = collector_interface->getFrameInfo (io_hndl, iopkt.comm.tstamp, FRINFO_FROM_STACK, &iopkt);
3229 collector_interface->writeDataRecord (io_hndl, (Common_packet*) & iopkt);
3230 POP_REENTRANCE (guard);
3231 return ret;
3232 }