]> granicus.if.org Git - strace/blob - desc.c
8931ca73c6526ea3aa8f1e0fe165d4c60f4bd268
[strace] / desc.c
1 /*
2  * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "defs.h"
32 #include <fcntl.h>
33 #include <sys/file.h>
34 #ifdef HAVE_SYS_EPOLL_H
35 # include <sys/epoll.h>
36 #endif
37
38 #include "xlat/fcntlcmds.h"
39 #include "xlat/fdflags.h"
40 #include "xlat/flockcmds.h"
41 #include "xlat/lockfcmds.h"
42 #include "xlat/notifyflags.h"
43
44 /*
45  * Assume that F_SETLK64, F_SETLKW64, and F_GETLK64 are either defined
46  * or not defined altogether.
47  */
48 #if defined(F_SETLK64) && F_SETLK64 + 0 != F_SETLK
49 # define USE_PRINTFLOCK64 1
50 #else
51 # define USE_PRINTFLOCK64 0
52 #endif
53
54 #if USE_PRINTFLOCK64
55
56 # ifndef HAVE_STRUCT_FLOCK64
57 struct flock64 {
58         short int l_type, l_whence;
59         int64_t l_start, l_len;
60         int l_pid;
61 };
62 # endif
63
64 static void
65 printflock64(struct tcb *tcp, long addr, int getlk)
66 {
67         struct flock64 fl;
68
69         if (umove_or_printaddr(tcp, addr, &fl))
70                 return;
71         tprints("{type=");
72         printxval(lockfcmds, fl.l_type, "F_???");
73         tprints(", whence=");
74         printxval(whence_codes, fl.l_whence, "SEEK_???");
75         tprintf(", start=%lld, len=%lld", (long long) fl.l_start, (long long) fl.l_len);
76         if (getlk)
77                 tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
78         else
79                 tprints("}");
80 }
81 #endif /* USE_PRINTFLOCK64 */
82
83 static void
84 printflock(struct tcb *tcp, long addr, int getlk)
85 {
86         struct flock fl;
87
88 #if SUPPORTED_PERSONALITIES > 1
89         if (
90 # if SIZEOF_OFF_T > SIZEOF_LONG
91             current_personality != DEFAULT_PERSONALITY &&
92 # endif
93             current_wordsize != sizeof(fl.l_start)) {
94                 if (current_wordsize == 4) {
95                         /* 32-bit x86 app on x86_64 and similar cases */
96                         struct {
97                                 short int l_type;
98                                 short int l_whence;
99                                 int32_t l_start; /* off_t */
100                                 int32_t l_len; /* off_t */
101                                 int32_t l_pid; /* pid_t */
102                         } fl32;
103                         if (umove_or_printaddr(tcp, addr, &fl32))
104                                 return;
105                         fl.l_type = fl32.l_type;
106                         fl.l_whence = fl32.l_whence;
107                         fl.l_start = fl32.l_start;
108                         fl.l_len = fl32.l_len;
109                         fl.l_pid = fl32.l_pid;
110                 } else {
111                         /* let people know we have a problem here */
112                         tprintf("<decode error: unsupported wordsize %d>",
113                                 current_wordsize);
114                         return;
115                 }
116         } else
117 #endif
118         if (umove_or_printaddr(tcp, addr, &fl))
119                 return;
120         tprints("{type=");
121         printxval(lockfcmds, fl.l_type, "F_???");
122         tprints(", whence=");
123         printxval(whence_codes, fl.l_whence, "SEEK_???");
124 #if SIZEOF_OFF_T > SIZEOF_LONG
125         tprintf(", start=%lld, len=%lld", fl.l_start, fl.l_len);
126 #else
127         tprintf(", start=%ld, len=%ld", fl.l_start, fl.l_len);
128 #endif
129         if (getlk)
130                 tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
131         else
132                 tprints("}");
133 }
134
135 SYS_FUNC(fcntl)
136 {
137         if (entering(tcp)) {
138                 printfd(tcp, tcp->u_arg[0]);
139                 tprints(", ");
140                 printxval(fcntlcmds, tcp->u_arg[1], "F_???");
141                 switch (tcp->u_arg[1]) {
142                 case F_SETFD:
143                         tprints(", ");
144                         printflags(fdflags, tcp->u_arg[2], "FD_???");
145                         break;
146                 case F_SETOWN: case F_DUPFD:
147 #ifdef F_DUPFD_CLOEXEC
148                 case F_DUPFD_CLOEXEC:
149 #endif
150                         tprintf(", %ld", tcp->u_arg[2]);
151                         break;
152                 case F_SETFL:
153                         tprints(", ");
154                         tprint_open_modes(tcp->u_arg[2]);
155                         break;
156                 case F_SETLK: case F_SETLKW:
157                         tprints(", ");
158                         printflock(tcp, tcp->u_arg[2], 0);
159                         break;
160 #if USE_PRINTFLOCK64
161                 case F_SETLK64: case F_SETLKW64:
162                         tprints(", ");
163                         printflock64(tcp, tcp->u_arg[2], 0);
164                         break;
165 #endif /* USE_PRINTFLOCK64 */
166 #ifdef F_NOTIFY
167                 case F_NOTIFY:
168                         tprints(", ");
169                         printflags(notifyflags, tcp->u_arg[2], "DN_???");
170                         break;
171 #endif
172 #ifdef F_SETLEASE
173                 case F_SETLEASE:
174                         tprints(", ");
175                         printxval(lockfcmds, tcp->u_arg[2], "F_???");
176                         break;
177 #endif
178                 }
179         } else {
180                 switch (tcp->u_arg[1]) {
181                 case F_DUPFD:
182 #ifdef F_DUPFD_CLOEXEC
183                 case F_DUPFD_CLOEXEC:
184 #endif
185                 case F_SETFD: case F_SETFL:
186                 case F_SETLK: case F_SETLKW:
187                 case F_SETOWN: case F_GETOWN:
188 #ifdef F_NOTIFY
189                 case F_NOTIFY:
190 #endif
191 #ifdef F_SETLEASE
192                 case F_SETLEASE:
193 #endif
194                         break;
195                 case F_GETFD:
196                         if (syserror(tcp) || tcp->u_rval == 0)
197                                 return 0;
198                         tcp->auxstr = sprintflags("flags ", fdflags, tcp->u_rval);
199                         return RVAL_HEX|RVAL_STR;
200                 case F_GETFL:
201                         if (syserror(tcp))
202                                 return 0;
203                         tcp->auxstr = sprint_open_modes(tcp->u_rval);
204                         return RVAL_HEX|RVAL_STR;
205                 case F_GETLK:
206                         tprints(", ");
207                         printflock(tcp, tcp->u_arg[2], 1);
208                         break;
209 #if USE_PRINTFLOCK64
210                 case F_GETLK64:
211                         tprints(", ");
212                         printflock64(tcp, tcp->u_arg[2], 1);
213                         break;
214 #endif
215 #ifdef F_GETLEASE
216                 case F_GETLEASE:
217                         if (syserror(tcp))
218                                 return 0;
219                         tcp->auxstr = xlookup(lockfcmds, tcp->u_rval);
220                         return RVAL_HEX|RVAL_STR;
221 #endif
222                 default:
223                         tprintf(", %#lx", tcp->u_arg[2]);
224                         break;
225                 }
226         }
227         return 0;
228 }
229
230 #ifdef LOCK_SH
231
232 SYS_FUNC(flock)
233 {
234         printfd(tcp, tcp->u_arg[0]);
235         tprints(", ");
236         printflags(flockcmds, tcp->u_arg[1], "LOCK_???");
237
238         return RVAL_DECODED;
239 }
240 #endif /* LOCK_SH */
241
242 SYS_FUNC(close)
243 {
244         printfd(tcp, tcp->u_arg[0]);
245
246         return RVAL_DECODED;
247 }
248
249 SYS_FUNC(dup)
250 {
251         printfd(tcp, tcp->u_arg[0]);
252
253         return RVAL_DECODED | RVAL_FD;
254 }
255
256 static int
257 do_dup2(struct tcb *tcp, int flags_arg)
258 {
259         printfd(tcp, tcp->u_arg[0]);
260         tprints(", ");
261         printfd(tcp, tcp->u_arg[1]);
262         if (flags_arg >= 0) {
263                 tprints(", ");
264                 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
265         }
266
267         return RVAL_DECODED | RVAL_FD;
268 }
269
270 SYS_FUNC(dup2)
271 {
272         return do_dup2(tcp, -1);
273 }
274
275 SYS_FUNC(dup3)
276 {
277         return do_dup2(tcp, 2);
278 }
279
280 #if defined(ALPHA)
281 SYS_FUNC(getdtablesize)
282 {
283         return 0;
284 }
285 #endif
286
287 static int
288 decode_select(struct tcb *tcp, long *args, enum bitness_t bitness)
289 {
290         int i, j;
291         int nfds, fdsize;
292         fd_set *fds = NULL;
293         const char *sep;
294         long arg;
295
296         /* Kernel truncates arg[0] to int, we do the same. */
297         nfds = (int) args[0];
298
299         /* Kernel rejects negative nfds, so we don't parse it either. */
300         if (nfds < 0)
301                 nfds = 0;
302
303         /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
304         if (nfds > 1024*1024)
305                 nfds = 1024*1024;
306
307         /*
308          * We had bugs a-la "while (j < args[0])" and "umoven(args[0])" below.
309          * Instead of args[0], use nfds for fd count, fdsize for array lengths.
310          */
311         fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
312
313         if (entering(tcp)) {
314                 tprintf("%d", (int) args[0]);
315
316                 if (verbose(tcp) && fdsize > 0)
317                         fds = malloc(fdsize);
318                 for (i = 0; i < 3; i++) {
319                         arg = args[i+1];
320                         tprints(", ");
321                         if (!fds) {
322                                 printaddr(arg);
323                                 continue;
324                         }
325                         if (umoven_or_printaddr(tcp, arg, fdsize, fds))
326                                 continue;
327                         tprints("[");
328                         for (j = 0, sep = "";; j++) {
329                                 j = next_set_bit(fds, j, nfds);
330                                 if (j < 0)
331                                         break;
332                                 tprints(sep);
333                                 printfd(tcp, j);
334                                 sep = " ";
335                         }
336                         tprints("]");
337                 }
338                 free(fds);
339                 tprints(", ");
340                 printtv_bitness(tcp, args[4], bitness, 0);
341         } else {
342                 static char outstr[1024];
343                 char *outptr;
344 #define end_outstr (outstr + sizeof(outstr))
345                 int ready_fds;
346
347                 if (syserror(tcp))
348                         return 0;
349
350                 ready_fds = tcp->u_rval;
351                 if (ready_fds == 0) {
352                         tcp->auxstr = "Timeout";
353                         return RVAL_STR;
354                 }
355
356                 fds = malloc(fdsize);
357
358                 outptr = outstr;
359                 sep = "";
360                 for (i = 0; i < 3 && ready_fds > 0; i++) {
361                         int first = 1;
362
363                         arg = args[i+1];
364                         if (!arg || !fds || umoven(tcp, arg, fdsize, fds) < 0)
365                                 continue;
366                         for (j = 0;; j++) {
367                                 j = next_set_bit(fds, j, nfds);
368                                 if (j < 0)
369                                         break;
370                                 /* +2 chars needed at the end: ']',NUL */
371                                 if (outptr < end_outstr - (sizeof(", except [") + sizeof(int)*3 + 2)) {
372                                         if (first) {
373                                                 outptr += sprintf(outptr, "%s%s [%u",
374                                                         sep,
375                                                         i == 0 ? "in" : i == 1 ? "out" : "except",
376                                                         j
377                                                 );
378                                                 first = 0;
379                                                 sep = ", ";
380                                         }
381                                         else {
382                                                 outptr += sprintf(outptr, " %u", j);
383                                         }
384                                 }
385                                 if (--ready_fds == 0)
386                                         break;
387                         }
388                         if (outptr != outstr)
389                                 *outptr++ = ']';
390                 }
391                 free(fds);
392                 /* This contains no useful information on SunOS.  */
393                 if (args[4]) {
394                         if (outptr < end_outstr - (10 + TIMEVAL_TEXT_BUFSIZE)) {
395                                 outptr += sprintf(outptr, "%sleft ", sep);
396                                 outptr = sprinttv(outptr, tcp, args[4], bitness, /*special:*/ 0);
397                         }
398                 }
399                 *outptr = '\0';
400                 tcp->auxstr = outstr;
401                 return RVAL_STR;
402 #undef end_outstr
403         }
404         return 0;
405 }
406
407 SYS_FUNC(oldselect)
408 {
409         long long_args[5];
410 #undef oldselect_args
411 #if SIZEOF_LONG == 4
412 # define oldselect_args long_args
413 #else
414         unsigned int oldselect_args[5];
415         unsigned int i;
416 #endif
417
418         if (umove(tcp, tcp->u_arg[0], &oldselect_args) < 0) {
419                 printaddr(tcp->u_arg[0]);
420                 return 0;
421         }
422 #ifndef oldselect_args
423         for (i = 0; i < 5; i++) {
424                 long_args[i] = oldselect_args[i];
425         }
426 #endif
427         return decode_select(tcp, long_args, BITNESS_CURRENT);
428 #undef oldselect_args
429 }
430
431 #ifdef ALPHA
432 SYS_FUNC(osf_select)
433 {
434         return decode_select(tcp, tcp->u_arg, BITNESS_32);
435 }
436 #endif
437
438 #include "xlat/epollctls.h"
439 #include "xlat/epollevents.h"
440 #include "xlat/epollflags.h"
441
442 SYS_FUNC(epoll_create1)
443 {
444         printflags(epollflags, tcp->u_arg[0], "EPOLL_???");
445
446         return RVAL_DECODED;
447 }
448
449 #ifdef HAVE_SYS_EPOLL_H
450 static void
451 print_epoll_event(struct epoll_event *ev)
452 {
453         tprints("{");
454         printflags(epollevents, ev->events, "EPOLL???");
455         /* We cannot know what format the program uses, so print u32 and u64
456            which will cover every value.  */
457         tprintf(", {u32=%" PRIu32 ", u64=%" PRIu64 "}}",
458                 ev->data.u32, ev->data.u64);
459 }
460 #endif
461
462 SYS_FUNC(epoll_ctl)
463 {
464         struct epoll_event ev;
465
466         printfd(tcp, tcp->u_arg[0]);
467         tprints(", ");
468         printxval(epollctls, tcp->u_arg[1], "EPOLL_CTL_???");
469         tprints(", ");
470         printfd(tcp, tcp->u_arg[2]);
471         tprints(", ");
472 #ifdef HAVE_SYS_EPOLL_H
473         if (EPOLL_CTL_DEL == tcp->u_arg[1])
474                 printaddr(tcp->u_arg[3]);
475         else if (!umove_or_printaddr(tcp, tcp->u_arg[3], &ev))
476                 print_epoll_event(&ev);
477 #else
478         printaddr(tcp->u_arg[3]);
479 #endif
480
481         return RVAL_DECODED;
482 }
483
484 static void
485 print_epoll_event_array(struct tcb *tcp, const long addr, const long len)
486 {
487 #ifdef HAVE_SYS_EPOLL_H
488         struct epoll_event ev, *start, *cur, *end;
489
490         if (!len) {
491                 tprints("[]");
492                 return;
493         }
494
495         if (umove_or_printaddr(tcp, addr, &ev))
496                 return;
497
498         tprints("[");
499         print_epoll_event(&ev);
500
501         start = (struct epoll_event *) addr;
502         end = start + len;
503         for (cur = start + 1; cur < end; ++cur) {
504                 tprints(", ");
505                 if (umove_or_printaddr(tcp, (long) cur, &ev))
506                         break;
507                 print_epoll_event(&ev);
508         }
509         tprints("]");
510 #else
511         printaddr(addr);
512 #endif
513 }
514
515 static void
516 epoll_wait_common(struct tcb *tcp)
517 {
518         if (entering(tcp)) {
519                 printfd(tcp, tcp->u_arg[0]);
520                 tprints(", ");
521         } else {
522                 print_epoll_event_array(tcp, tcp->u_arg[1], tcp->u_rval);
523                 tprintf(", %d, %d", (int) tcp->u_arg[2], (int) tcp->u_arg[3]);
524         }
525 }
526
527 SYS_FUNC(epoll_wait)
528 {
529         epoll_wait_common(tcp);
530         return 0;
531 }
532
533 SYS_FUNC(epoll_pwait)
534 {
535         epoll_wait_common(tcp);
536         if (exiting(tcp)) {
537                 tprints(", ");
538                 /* NB: kernel requires arg[5] == NSIG / 8 */
539                 print_sigset_addr_len(tcp, tcp->u_arg[4], tcp->u_arg[5]);
540                 tprintf(", %lu", tcp->u_arg[5]);
541         }
542         return 0;
543 }
544
545 SYS_FUNC(select)
546 {
547         return decode_select(tcp, tcp->u_arg, BITNESS_CURRENT);
548 }
549
550 SYS_FUNC(pselect6)
551 {
552         int rc = decode_select(tcp, tcp->u_arg, BITNESS_CURRENT);
553         if (entering(tcp)) {
554                 long r;
555                 struct {
556                         unsigned long ptr;
557                         unsigned long len;
558                 } data;
559
560                 tprints(", ");
561 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
562                 if (current_wordsize == 4) {
563                         struct {
564                                 uint32_t ptr;
565                                 uint32_t len;
566                         } data32;
567                         r = umove_or_printaddr(tcp, tcp->u_arg[5], &data32);
568                         data.ptr = data32.ptr;
569                         data.len = data32.len;
570                 } else
571 #endif
572                         r = umove_or_printaddr(tcp, tcp->u_arg[5], &data);
573                 if (r == 0) {
574                         tprints("{");
575                         /* NB: kernel requires data.len == NSIG / 8 */
576                         print_sigset_addr_len(tcp, data.ptr, data.len);
577                         tprintf(", %lu}", data.len);
578                 }
579         }
580         return rc;
581 }
582
583 static int
584 do_eventfd(struct tcb *tcp, int flags_arg)
585 {
586         tprintf("%lu", tcp->u_arg[0]);
587         if (flags_arg >= 0) {
588                 tprints(", ");
589                 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
590         }
591
592         return RVAL_DECODED;
593 }
594
595 SYS_FUNC(eventfd)
596 {
597         return do_eventfd(tcp, -1);
598 }
599
600 SYS_FUNC(eventfd2)
601 {
602         return do_eventfd(tcp, 1);
603 }