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