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