]> granicus.if.org Git - strace/blob - syscall.c
Replace "(unsigned long) -1L" with -1UL
[strace] / syscall.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  * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7  *                     Linux for s390 port by D.J. Barrow
8  *                    <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include "defs.h"
35 #include "native_defs.h"
36 #include <sys/param.h>
37 #include <signal.h>
38
39 /* for struct iovec */
40 #include <sys/uio.h>
41
42 #include "regs.h"
43 #include "ptrace.h"
44
45 #if defined(SPARC64)
46 # undef PTRACE_GETREGS
47 # define PTRACE_GETREGS PTRACE_GETREGS64
48 # undef PTRACE_SETREGS
49 # define PTRACE_SETREGS PTRACE_SETREGS64
50 #endif
51
52 #if defined SPARC64
53 # include <asm/psrcompat.h>
54 #elif defined SPARC
55 # include <asm/psr.h>
56 #endif
57
58 #ifdef IA64
59 # include <asm/rse.h>
60 #endif
61
62 #ifndef NT_PRSTATUS
63 # define NT_PRSTATUS 1
64 #endif
65
66 #ifndef NSIG
67 # warning: NSIG is not defined, using 32
68 # define NSIG 32
69 #endif
70
71 #include "syscall.h"
72
73 /* Define these shorthand notations to simplify the syscallent files. */
74 #define TD TRACE_DESC
75 #define TF TRACE_FILE
76 #define TI TRACE_IPC
77 #define TN TRACE_NETWORK
78 #define TP TRACE_PROCESS
79 #define TS TRACE_SIGNAL
80 #define TM TRACE_MEMORY
81 #define NF SYSCALL_NEVER_FAILS
82 #define MA MAX_ARGS
83 #define SI STACKTRACE_INVALIDATE_CACHE
84 #define SE STACKTRACE_CAPTURE_ON_ENTER
85
86 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
87
88 const struct_sysent sysent0[] = {
89 #include "syscallent.h"
90 };
91
92 #if SUPPORTED_PERSONALITIES > 1
93 # include PERSONALITY1_INCLUDE_FUNCS
94 static const struct_sysent sysent1[] = {
95 # include "syscallent1.h"
96 };
97 #endif
98
99 #if SUPPORTED_PERSONALITIES > 2
100 # include PERSONALITY2_INCLUDE_FUNCS
101 static const struct_sysent sysent2[] = {
102 # include "syscallent2.h"
103 };
104 #endif
105
106 /* Now undef them since short defines cause wicked namespace pollution. */
107 #undef SEN
108 #undef TD
109 #undef TF
110 #undef TI
111 #undef TN
112 #undef TP
113 #undef TS
114 #undef TM
115 #undef NF
116 #undef MA
117 #undef SI
118 #undef SE
119
120 /*
121  * `ioctlent[012].h' files are automatically generated by the auxiliary
122  * program `ioctlsort', such that the list is sorted by the `code' field.
123  * This has the side-effect of resolving the _IO.. macros into
124  * plain integers, eliminating the need to include here everything
125  * in "/usr/include".
126  */
127
128 const char *const errnoent0[] = {
129 #include "errnoent.h"
130 };
131 const char *const signalent0[] = {
132 #include "signalent.h"
133 };
134 const struct_ioctlent ioctlent0[] = {
135 #include "ioctlent0.h"
136 };
137
138 #if SUPPORTED_PERSONALITIES > 1
139 static const char *const errnoent1[] = {
140 # include "errnoent1.h"
141 };
142 static const char *const signalent1[] = {
143 # include "signalent1.h"
144 };
145 static const struct_ioctlent ioctlent1[] = {
146 # include "ioctlent1.h"
147 };
148 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
149 static const struct_printers printers0 = {
150 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
151 };
152 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
153 static const struct_printers printers1 = {
154 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
155 };
156 #endif
157
158 #if SUPPORTED_PERSONALITIES > 2
159 static const char *const errnoent2[] = {
160 # include "errnoent2.h"
161 };
162 static const char *const signalent2[] = {
163 # include "signalent2.h"
164 };
165 static const struct_ioctlent ioctlent2[] = {
166 # include "ioctlent2.h"
167 };
168 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
169 static const struct_printers printers2 = {
170 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
171 };
172 #endif
173
174 enum {
175         nsyscalls0 = ARRAY_SIZE(sysent0)
176 #if SUPPORTED_PERSONALITIES > 1
177         , nsyscalls1 = ARRAY_SIZE(sysent1)
178 # if SUPPORTED_PERSONALITIES > 2
179         , nsyscalls2 = ARRAY_SIZE(sysent2)
180 # endif
181 #endif
182 };
183
184 enum {
185         nerrnos0 = ARRAY_SIZE(errnoent0)
186 #if SUPPORTED_PERSONALITIES > 1
187         , nerrnos1 = ARRAY_SIZE(errnoent1)
188 # if SUPPORTED_PERSONALITIES > 2
189         , nerrnos2 = ARRAY_SIZE(errnoent2)
190 # endif
191 #endif
192 };
193
194 enum {
195         nsignals0 = ARRAY_SIZE(signalent0)
196 #if SUPPORTED_PERSONALITIES > 1
197         , nsignals1 = ARRAY_SIZE(signalent1)
198 # if SUPPORTED_PERSONALITIES > 2
199         , nsignals2 = ARRAY_SIZE(signalent2)
200 # endif
201 #endif
202 };
203
204 enum {
205         nioctlents0 = ARRAY_SIZE(ioctlent0)
206 #if SUPPORTED_PERSONALITIES > 1
207         , nioctlents1 = ARRAY_SIZE(ioctlent1)
208 # if SUPPORTED_PERSONALITIES > 2
209         , nioctlents2 = ARRAY_SIZE(ioctlent2)
210 # endif
211 #endif
212 };
213
214 #if SUPPORTED_PERSONALITIES > 1
215 const struct_sysent *sysent = sysent0;
216 const char *const *errnoent = errnoent0;
217 const char *const *signalent = signalent0;
218 const struct_ioctlent *ioctlent = ioctlent0;
219 const struct_printers *printers = &printers0;
220 #endif
221
222 unsigned nsyscalls = nsyscalls0;
223 unsigned nerrnos = nerrnos0;
224 unsigned nsignals = nsignals0;
225 unsigned nioctlents = nioctlents0;
226
227 const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
228         nsyscalls0,
229 #if SUPPORTED_PERSONALITIES > 1
230         nsyscalls1,
231 #endif
232 #if SUPPORTED_PERSONALITIES > 2
233         nsyscalls2,
234 #endif
235 };
236 const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
237         sysent0,
238 #if SUPPORTED_PERSONALITIES > 1
239         sysent1,
240 #endif
241 #if SUPPORTED_PERSONALITIES > 2
242         sysent2,
243 #endif
244 };
245
246 #if SUPPORTED_PERSONALITIES > 1
247 unsigned current_personality;
248
249 # ifndef current_wordsize
250 unsigned current_wordsize;
251 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
252         PERSONALITY0_WORDSIZE,
253         PERSONALITY1_WORDSIZE,
254 # if SUPPORTED_PERSONALITIES > 2
255         PERSONALITY2_WORDSIZE,
256 # endif
257 };
258 # endif
259
260 void
261 set_personality(int personality)
262 {
263         nsyscalls = nsyscall_vec[personality];
264         sysent = sysent_vec[personality];
265
266         switch (personality) {
267         case 0:
268                 errnoent = errnoent0;
269                 nerrnos = nerrnos0;
270                 ioctlent = ioctlent0;
271                 nioctlents = nioctlents0;
272                 signalent = signalent0;
273                 nsignals = nsignals0;
274                 printers = &printers0;
275                 break;
276
277         case 1:
278                 errnoent = errnoent1;
279                 nerrnos = nerrnos1;
280                 ioctlent = ioctlent1;
281                 nioctlents = nioctlents1;
282                 signalent = signalent1;
283                 nsignals = nsignals1;
284                 printers = &printers1;
285                 break;
286
287 # if SUPPORTED_PERSONALITIES > 2
288         case 2:
289                 errnoent = errnoent2;
290                 nerrnos = nerrnos2;
291                 ioctlent = ioctlent2;
292                 nioctlents = nioctlents2;
293                 signalent = signalent2;
294                 nsignals = nsignals2;
295                 printers = &printers2;
296                 break;
297 # endif
298         }
299
300         current_personality = personality;
301 # ifndef current_wordsize
302         current_wordsize = personality_wordsize[personality];
303 # endif
304 }
305
306 static void
307 update_personality(struct tcb *tcp, unsigned int personality)
308 {
309         if (personality == current_personality)
310                 return;
311         set_personality(personality);
312
313         if (personality == tcp->currpers)
314                 return;
315         tcp->currpers = personality;
316
317 # undef PERSONALITY_NAMES
318 # if defined POWERPC64
319 #  define PERSONALITY_NAMES {"64 bit", "32 bit"}
320 # elif defined X86_64
321 #  define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
322 # elif defined X32
323 #  define PERSONALITY_NAMES {"x32", "32 bit"}
324 # elif defined AARCH64
325 #  define PERSONALITY_NAMES {"64 bit", "32 bit"}
326 # elif defined TILE
327 #  define PERSONALITY_NAMES {"64-bit", "32-bit"}
328 # endif
329 # ifdef PERSONALITY_NAMES
330         if (!qflag) {
331                 static const char *const names[] = PERSONALITY_NAMES;
332                 error_msg("[ Process PID=%d runs in %s mode. ]",
333                           tcp->pid, names[personality]);
334         }
335 # endif
336 }
337 #endif
338
339 #ifdef SYS_socket_subcall
340 static void
341 decode_socket_subcall(struct tcb *tcp)
342 {
343         const int call = tcp->u_arg[0];
344
345         if (call < 1 || call >= SYS_socket_nsubcalls)
346                 return;
347
348         const unsigned long scno = SYS_socket_subcall + call;
349         const unsigned int nargs = sysent[scno].nargs;
350         uint64_t buf[nargs];
351
352         if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
353                 return;
354
355         tcp->scno = scno;
356         tcp->qual_flg = qual_flags(scno);
357         tcp->s_ent = &sysent[scno];
358
359         unsigned int i;
360         for (i = 0; i < nargs; ++i)
361                 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
362                                 ? ((uint32_t *) (void *) buf)[i] : buf[i];
363 }
364 #endif
365
366 #ifdef SYS_ipc_subcall
367 static void
368 decode_ipc_subcall(struct tcb *tcp)
369 {
370         unsigned int call = tcp->u_arg[0];
371         const unsigned int version = call >> 16;
372
373         if (version) {
374 # if defined S390 || defined S390X
375                 return;
376 # else
377 #  ifdef SPARC64
378                 if (current_wordsize == 8)
379                         return;
380 #  endif
381                 set_tcb_priv_ulong(tcp, version);
382                 call &= 0xffff;
383 # endif
384         }
385
386         switch (call) {
387                 case  1: case  2: case  3: case  4:
388                 case 11: case 12: case 13: case 14:
389                 case 21: case 22: case 23: case 24:
390                         break;
391                 default:
392                         return;
393         }
394
395         tcp->scno = SYS_ipc_subcall + call;
396         tcp->qual_flg = qual_flags(tcp->scno);
397         tcp->s_ent = &sysent[tcp->scno];
398
399         const unsigned int n = tcp->s_ent->nargs;
400         unsigned int i;
401         for (i = 0; i < n; i++)
402                 tcp->u_arg[i] = tcp->u_arg[i + 1];
403 }
404 #endif
405
406 #ifdef LINUX_MIPSO32
407 static void
408 decode_mips_subcall(struct tcb *tcp)
409 {
410         if (!SCNO_IS_VALID(tcp->u_arg[0]))
411                 return;
412         tcp->scno = tcp->u_arg[0];
413         tcp->qual_flg = qual_flags(tcp->scno);
414         tcp->s_ent = &sysent[tcp->scno];
415         memmove(&tcp->u_arg[0], &tcp->u_arg[1],
416                 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
417         /*
418          * Fetching the last arg of 7-arg syscalls (fadvise64_64
419          * and sync_file_range) requires additional code,
420          * see linux/mips/get_syscall_args.c
421          */
422         if (tcp->s_ent->nargs == MAX_ARGS) {
423                 if (umoven(tcp,
424                            mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
425                            sizeof(tcp->u_arg[0]),
426                            &tcp->u_arg[MAX_ARGS - 1]) < 0)
427                 tcp->u_arg[MAX_ARGS - 1] = 0;
428         }
429 }
430 #endif /* LINUX_MIPSO32 */
431
432 static void
433 dumpio(struct tcb *tcp)
434 {
435         if (syserror(tcp))
436                 return;
437
438         int fd = tcp->u_arg[0];
439         if (fd < 0)
440                 return;
441
442         if (is_number_in_set(fd, &read_set)) {
443                 switch (tcp->s_ent->sen) {
444                 case SEN_read:
445                 case SEN_pread:
446                 case SEN_recv:
447                 case SEN_recvfrom:
448                 case SEN_mq_timedreceive:
449                         dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
450                         return;
451                 case SEN_readv:
452                 case SEN_preadv:
453                 case SEN_preadv2:
454                         dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
455                                      tcp->u_rval);
456                         return;
457                 case SEN_recvmsg:
458                         dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
459                         return;
460                 case SEN_recvmmsg:
461                         dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
462                         return;
463                 }
464         }
465         if (is_number_in_set(fd, &write_set)) {
466                 switch (tcp->s_ent->sen) {
467                 case SEN_write:
468                 case SEN_pwrite:
469                 case SEN_send:
470                 case SEN_sendto:
471                 case SEN_mq_timedsend:
472                         dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
473                         break;
474                 case SEN_writev:
475                 case SEN_pwritev:
476                 case SEN_pwritev2:
477                 case SEN_vmsplice:
478                         dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
479                         break;
480                 case SEN_sendmsg:
481                         dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1UL);
482                         break;
483                 case SEN_sendmmsg:
484                         dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
485                         break;
486                 }
487         }
488 }
489
490 /*
491  * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
492  * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
493  */
494 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
495 static long
496 shuffle_scno(unsigned long scno)
497 {
498         if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
499                 return scno;
500
501         /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
502         if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
503                 return 0x000ffff0;
504         if (scno == 0x000ffff0)
505                 return ARM_FIRST_SHUFFLED_SYSCALL;
506
507 #define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
508         /*
509          * Is it ARM specific syscall?
510          * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
511          * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
512          */
513         if (scno >= 0x000f0000 &&
514             scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
515                 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
516         }
517         if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
518                 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
519         }
520
521         return scno;
522 }
523 #else
524 # define shuffle_scno(scno) ((long)(scno))
525 #endif
526
527 const char *
528 err_name(unsigned long err)
529 {
530         if ((err < nerrnos) && errnoent[err])
531                 return errnoent[err];
532
533         return NULL;
534 }
535
536 static long get_regs_error;
537
538 void
539 clear_regs(void)
540 {
541         get_regs_error = -1;
542 }
543
544 static int get_syscall_args(struct tcb *);
545 static int get_syscall_result(struct tcb *);
546 static int arch_get_scno(struct tcb *tcp);
547 static int arch_set_scno(struct tcb *, long);
548 static void get_error(struct tcb *, const bool);
549 static int arch_set_error(struct tcb *);
550
551 struct fault_opts *fault_vec[SUPPORTED_PERSONALITIES];
552
553 static struct fault_opts *
554 tcb_fault_opts(struct tcb *tcp)
555 {
556         return (SCNO_IN_RANGE(tcp->scno) && tcp->fault_vec[current_personality])
557                ? &tcp->fault_vec[current_personality][tcp->scno] : NULL;
558 }
559
560
561 static long
562 inject_syscall_fault_entering(struct tcb *tcp)
563 {
564         if (!tcp->fault_vec[current_personality]) {
565                 tcp->fault_vec[current_personality] =
566                         xcalloc(nsyscalls, sizeof(**fault_vec));
567                 memcpy(tcp->fault_vec[current_personality],
568                        fault_vec[current_personality],
569                        nsyscalls * sizeof(**fault_vec));
570         }
571
572         struct fault_opts *opts = tcb_fault_opts(tcp);
573
574         if (!opts || opts->first == 0)
575                 return 0;
576
577         --opts->first;
578
579         if (opts->first != 0)
580                 return 0;
581
582         opts->first = opts->step;
583
584         if (!arch_set_scno(tcp, -1))
585                 tcp->flags |= TCB_FAULT_INJ;
586
587         return 0;
588 }
589
590 static long
591 update_syscall_fault_exiting(struct tcb *tcp)
592 {
593         struct fault_opts *opts = tcb_fault_opts(tcp);
594
595         if (opts && opts->err && tcp->u_error != opts->err) {
596                 unsigned long u_error = tcp->u_error;
597                 tcp->u_error = opts->err;
598                 if (arch_set_error(tcp))
599                         tcp->u_error = u_error;
600         }
601
602         return 0;
603 }
604
605 static int
606 trace_syscall_entering(struct tcb *tcp)
607 {
608         int res, scno_good;
609
610         scno_good = res = get_scno(tcp);
611         if (res == 0)
612                 return res;
613         if (res == 1)
614                 res = get_syscall_args(tcp);
615
616         if (res != 1) {
617                 printleader(tcp);
618                 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
619                 /*
620                  * " <unavailable>" will be added later by the code which
621                  * detects ptrace errors.
622                  */
623                 goto ret;
624         }
625
626 #ifdef LINUX_MIPSO32
627         if (SEN_syscall == tcp->s_ent->sen)
628                 decode_mips_subcall(tcp);
629 #endif
630
631 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
632         switch (tcp->s_ent->sen) {
633 # ifdef SYS_socket_subcall
634                 case SEN_socketcall:
635                         decode_socket_subcall(tcp);
636                         break;
637 # endif
638 # ifdef SYS_ipc_subcall
639                 case SEN_ipc:
640                         decode_ipc_subcall(tcp);
641                         break;
642 # endif
643         }
644 #endif
645
646         /* Restrain from fault injection while the trace executes strace code. */
647         if (hide_log(tcp)) {
648                 tcp->qual_flg &= ~QUAL_FAULT;
649         }
650
651         switch (tcp->s_ent->sen) {
652                 case SEN_execve:
653                 case SEN_execveat:
654 #if defined SPARC || defined SPARC64
655                 case SEN_execv:
656 #endif
657                         tcp->flags &= ~TCB_HIDE_LOG;
658                         break;
659         }
660
661         if (!(tcp->qual_flg & QUAL_TRACE)
662          || (tracing_paths && !pathtrace_match(tcp))
663         ) {
664                 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
665                 tcp->sys_func_rval = 0;
666                 return 0;
667         }
668
669         tcp->flags &= ~TCB_FILTERED;
670
671         if (hide_log(tcp)) {
672                 res = 0;
673                 goto ret;
674         }
675
676         if (tcp->qual_flg & QUAL_FAULT)
677                 inject_syscall_fault_entering(tcp);
678
679         if (cflag == CFLAG_ONLY_STATS) {
680                 res = 0;
681                 goto ret;
682         }
683
684 #ifdef USE_LIBUNWIND
685         if (stack_trace_enabled) {
686                 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
687                         unwind_capture_stacktrace(tcp);
688         }
689 #endif
690
691         printleader(tcp);
692         tprintf("%s(", tcp->s_ent->sys_name);
693         if (tcp->qual_flg & QUAL_RAW)
694                 res = printargs(tcp);
695         else
696                 res = tcp->s_ent->sys_func(tcp);
697
698         fflush(tcp->outf);
699  ret:
700         tcp->flags |= TCB_INSYSCALL;
701         tcp->sys_func_rval = res;
702         /* Measure the entrance time as late as possible to avoid errors. */
703         if (Tflag || cflag)
704                 gettimeofday(&tcp->etime, NULL);
705         return res;
706 }
707
708 static bool
709 syscall_fault_injected(struct tcb *tcp)
710 {
711         return tcp->flags & TCB_FAULT_INJ;
712 }
713
714 static int
715 trace_syscall_exiting(struct tcb *tcp)
716 {
717         int sys_res;
718         struct timeval tv;
719         int res;
720         unsigned long u_error;
721         const char *u_error_str;
722
723         /* Measure the exit time as early as possible to avoid errors. */
724         if (Tflag || cflag)
725                 gettimeofday(&tv, NULL);
726
727 #ifdef USE_LIBUNWIND
728         if (stack_trace_enabled) {
729                 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
730                         unwind_cache_invalidate(tcp);
731         }
732 #endif
733
734 #if SUPPORTED_PERSONALITIES > 1
735         update_personality(tcp, tcp->currpers);
736 #endif
737         res = (get_regs_error ? -1 : get_syscall_result(tcp));
738         if (filtered(tcp) || hide_log(tcp))
739                 goto ret;
740
741         if (syserror(tcp) && syscall_fault_injected(tcp))
742                 update_syscall_fault_exiting(tcp);
743
744         if (cflag) {
745                 count_syscall(tcp, &tv);
746                 if (cflag == CFLAG_ONLY_STATS) {
747                         goto ret;
748                 }
749         }
750
751         /* If not in -ff mode, and printing_tcp != tcp,
752          * then the log currently does not end with output
753          * of _our syscall entry_, but with something else.
754          * We need to say which syscall's return is this.
755          *
756          * Forced reprinting via TCB_REPRINT is used only by
757          * "strace -ff -oLOG test/threaded_execve" corner case.
758          * It's the only case when -ff mode needs reprinting.
759          */
760         if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
761                 tcp->flags &= ~TCB_REPRINT;
762                 printleader(tcp);
763                 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
764         }
765         printing_tcp = tcp;
766
767         tcp->s_prev_ent = NULL;
768         if (res != 1) {
769                 /* There was error in one of prior ptrace ops */
770                 tprints(") ");
771                 tabto();
772                 tprints("= ? <unavailable>\n");
773                 line_ended();
774                 tcp->flags &= ~(TCB_INSYSCALL | TCB_FAULT_INJ);
775                 tcp->sys_func_rval = 0;
776                 free_tcb_priv_data(tcp);
777                 return res;
778         }
779         tcp->s_prev_ent = tcp->s_ent;
780
781         sys_res = 0;
782         if (tcp->qual_flg & QUAL_RAW) {
783                 /* sys_res = printargs(tcp); - but it's nop on sysexit */
784         } else {
785         /* FIXME: not_failing_only (IOW, option -z) is broken:
786          * failure of syscall is known only after syscall return.
787          * Thus we end up with something like this on, say, ENOENT:
788          *     open("doesnt_exist", O_RDONLY <unfinished ...>
789          *     {next syscall decode}
790          * whereas the intended result is that open(...) line
791          * is not shown at all.
792          */
793                 if (not_failing_only && tcp->u_error)
794                         goto ret;       /* ignore failed syscalls */
795                 if (tcp->sys_func_rval & RVAL_DECODED)
796                         sys_res = tcp->sys_func_rval;
797                 else
798                         sys_res = tcp->s_ent->sys_func(tcp);
799         }
800
801         tprints(") ");
802         tabto();
803         u_error = tcp->u_error;
804
805         if (tcp->qual_flg & QUAL_RAW) {
806                 if (u_error) {
807                         tprintf("= -1 (errno %lu)", u_error);
808                         if (syscall_fault_injected(tcp))
809                                 tprints(" (INJECTED)");
810                 } else {
811                         tprintf("= %#lx", tcp->u_rval);
812                 }
813         }
814         else if (!(sys_res & RVAL_NONE) && u_error) {
815                 switch (u_error) {
816                 /* Blocked signals do not interrupt any syscalls.
817                  * In this case syscalls don't return ERESTARTfoo codes.
818                  *
819                  * Deadly signals set to SIG_DFL interrupt syscalls
820                  * and kill the process regardless of which of the codes below
821                  * is returned by the interrupted syscall.
822                  * In some cases, kernel forces a kernel-generated deadly
823                  * signal to be unblocked and set to SIG_DFL (and thus cause
824                  * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
825                  * or SIGILL. (The alternative is to leave process spinning
826                  * forever on the faulty instruction - not useful).
827                  *
828                  * SIG_IGNed signals and non-deadly signals set to SIG_DFL
829                  * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
830                  * but kernel will always restart them.
831                  */
832                 case ERESTARTSYS:
833                         /* Most common type of signal-interrupted syscall exit code.
834                          * The system call will be restarted with the same arguments
835                          * if SA_RESTART is set; otherwise, it will fail with EINTR.
836                          */
837                         tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
838                         break;
839                 case ERESTARTNOINTR:
840                         /* Rare. For example, fork() returns this if interrupted.
841                          * SA_RESTART is ignored (assumed set): the restart is unconditional.
842                          */
843                         tprints("= ? ERESTARTNOINTR (To be restarted)");
844                         break;
845                 case ERESTARTNOHAND:
846                         /* pause(), rt_sigsuspend() etc use this code.
847                          * SA_RESTART is ignored (assumed not set):
848                          * syscall won't restart (will return EINTR instead)
849                          * even after signal with SA_RESTART set. However,
850                          * after SIG_IGN or SIG_DFL signal it will restart
851                          * (thus the name "restart only if has no handler").
852                          */
853                         tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
854                         break;
855                 case ERESTART_RESTARTBLOCK:
856                         /* Syscalls like nanosleep(), poll() which can't be
857                          * restarted with their original arguments use this
858                          * code. Kernel will execute restart_syscall() instead,
859                          * which changes arguments before restarting syscall.
860                          * SA_RESTART is ignored (assumed not set) similarly
861                          * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
862                          * since restart data is saved in "restart block"
863                          * in task struct, and if signal handler uses a syscall
864                          * which in turn saves another such restart block,
865                          * old data is lost and restart becomes impossible)
866                          */
867                         tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
868                         break;
869                 default:
870                         u_error_str = err_name(u_error);
871                         if (u_error_str)
872                                 tprintf("= -1 %s (%s)",
873                                         u_error_str, strerror(u_error));
874                         else
875                                 tprintf("= -1 %lu (%s)",
876                                         u_error, strerror(u_error));
877                         break;
878                 }
879                 if (syscall_fault_injected(tcp))
880                         tprintf(" (INJECTED)");
881                 if ((sys_res & RVAL_STR) && tcp->auxstr)
882                         tprintf(" (%s)", tcp->auxstr);
883         }
884         else {
885                 if (sys_res & RVAL_NONE)
886                         tprints("= ?");
887                 else {
888                         switch (sys_res & RVAL_MASK) {
889                         case RVAL_HEX:
890 #if SUPPORTED_PERSONALITIES > 1
891                                 if (current_wordsize < sizeof(long))
892                                         tprintf("= %#x",
893                                                 (unsigned int) tcp->u_rval);
894                                 else
895 #endif
896                                         tprintf("= %#lx", tcp->u_rval);
897                                 break;
898                         case RVAL_OCTAL:
899                                 tprints("= ");
900                                 print_numeric_long_umask(tcp->u_rval);
901                                 break;
902                         case RVAL_UDECIMAL:
903 #if SUPPORTED_PERSONALITIES > 1
904                                 if (current_wordsize < sizeof(long))
905                                         tprintf("= %u",
906                                                 (unsigned int) tcp->u_rval);
907                                 else
908 #endif
909                                         tprintf("= %lu", tcp->u_rval);
910                                 break;
911                         case RVAL_DECIMAL:
912                                 tprintf("= %ld", tcp->u_rval);
913                                 break;
914                         case RVAL_FD:
915                                 if (show_fd_path) {
916                                         tprints("= ");
917                                         printfd(tcp, tcp->u_rval);
918                                 }
919                                 else
920                                         tprintf("= %ld", tcp->u_rval);
921                                 break;
922 #if HAVE_STRUCT_TCB_EXT_ARG
923                         /*
924                         case RVAL_LHEX:
925                                 tprintf("= %#llx", tcp->u_lrval);
926                                 break;
927                         case RVAL_LOCTAL:
928                                 tprintf("= %#llo", tcp->u_lrval);
929                                 break;
930                         */
931                         case RVAL_LUDECIMAL:
932                                 tprintf("= %llu", tcp->u_lrval);
933                                 break;
934                         /*
935                         case RVAL_LDECIMAL:
936                                 tprintf("= %lld", tcp->u_lrval);
937                                 break;
938                         */
939 #endif /* HAVE_STRUCT_TCB_EXT_ARG */
940                         default:
941                                 error_msg("invalid rval format");
942                                 break;
943                         }
944                 }
945                 if ((sys_res & RVAL_STR) && tcp->auxstr)
946                         tprintf(" (%s)", tcp->auxstr);
947         }
948         if (Tflag) {
949                 tv_sub(&tv, &tv, &tcp->etime);
950                 tprintf(" <%ld.%06ld>",
951                         (long) tv.tv_sec, (long) tv.tv_usec);
952         }
953         tprints("\n");
954         dumpio(tcp);
955         line_ended();
956
957 #ifdef USE_LIBUNWIND
958         if (stack_trace_enabled)
959                 unwind_print_stacktrace(tcp);
960 #endif
961
962  ret:
963         tcp->flags &= ~(TCB_INSYSCALL | TCB_FAULT_INJ);
964         tcp->sys_func_rval = 0;
965         free_tcb_priv_data(tcp);
966         return 0;
967 }
968
969 int
970 trace_syscall(struct tcb *tcp)
971 {
972         return exiting(tcp) ?
973                 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
974 }
975
976 bool
977 is_erestart(struct tcb *tcp)
978 {
979         switch (tcp->u_error) {
980                 case ERESTARTSYS:
981                 case ERESTARTNOINTR:
982                 case ERESTARTNOHAND:
983                 case ERESTART_RESTARTBLOCK:
984                         return true;
985                 default:
986                         return false;
987         }
988 }
989
990 static unsigned long saved_u_error;
991
992 void
993 temporarily_clear_syserror(struct tcb *tcp)
994 {
995         saved_u_error = tcp->u_error;
996         tcp->u_error = 0;
997 }
998
999 void
1000 restore_cleared_syserror(struct tcb *tcp)
1001 {
1002         tcp->u_error = saved_u_error;
1003 }
1004
1005 /*
1006  * Check the syscall return value register value for whether it is
1007  * a negated errno code indicating an error, or a success return value.
1008  */
1009 static inline bool
1010 is_negated_errno(kernel_ulong_t val)
1011 {
1012         /* Linux kernel defines MAX_ERRNO to 4095. */
1013         kernel_ulong_t max = -(kernel_long_t) 4095;
1014
1015 #if defined X86_64 || defined X32
1016         /*
1017          * current_wordsize is 4 for x32 personality
1018          * but truncation _must not_ be done in it, so
1019          * check current_personality instead.
1020          */
1021         if (current_personality == 1) {
1022                 val = (uint32_t) val;
1023                 max = (uint32_t) max;
1024         }
1025 #elif SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1026         if (current_wordsize < sizeof(val)) {
1027                 val = (uint32_t) val;
1028                 max = (uint32_t) max;
1029         }
1030 #endif
1031
1032         return val >= max;
1033 }
1034
1035 #include "arch_regs.c"
1036
1037 #ifdef HAVE_GETRVAL2
1038 # include "arch_getrval2.c"
1039 #endif
1040
1041 void
1042 print_pc(struct tcb *tcp)
1043 {
1044 #if defined ARCH_PC_REG
1045 # define ARCH_GET_PC 0
1046 #elif defined ARCH_PC_PEEK_ADDR
1047         long pc;
1048 # define ARCH_PC_REG pc
1049 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1050 #else
1051 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1052 #endif
1053         if (get_regs_error || ARCH_GET_PC)
1054                 tprints(current_wordsize == 4 ? "[????????] "
1055                                               : "[????????????????] ");
1056         else
1057                 tprintf(current_wordsize == 4 ? "[%08lx] " : "[%016lx] ",
1058                         (unsigned long) ARCH_PC_REG);
1059 }
1060
1061 #include "getregs_old.h"
1062
1063 #undef ptrace_getregset_or_getregs
1064 #undef ptrace_setregset_or_setregs
1065 #ifdef ARCH_REGS_FOR_GETREGSET
1066
1067 # define ptrace_getregset_or_getregs ptrace_getregset
1068 static long
1069 ptrace_getregset(pid_t pid)
1070 {
1071 # ifdef ARCH_IOVEC_FOR_GETREGSET
1072         /* variable iovec */
1073         ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1074         return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1075                       &ARCH_IOVEC_FOR_GETREGSET);
1076 # else
1077         /* constant iovec */
1078         static struct iovec io = {
1079                 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1080                 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1081         };
1082         return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1083
1084 # endif
1085 }
1086
1087 # ifndef HAVE_GETREGS_OLD
1088 #  define ptrace_setregset_or_setregs ptrace_setregset
1089 static int
1090 ptrace_setregset(pid_t pid)
1091 {
1092 #  ifdef ARCH_IOVEC_FOR_GETREGSET
1093         /* variable iovec */
1094         return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1095                       &ARCH_IOVEC_FOR_GETREGSET);
1096 #  else
1097         /* constant iovec */
1098         static struct iovec io = {
1099                 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1100                 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1101         };
1102         return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1103 #  endif
1104 }
1105 # endif /* !HAVE_GETREGS_OLD */
1106
1107 #elif defined ARCH_REGS_FOR_GETREGS
1108
1109 # define ptrace_getregset_or_getregs ptrace_getregs
1110 static long
1111 ptrace_getregs(pid_t pid)
1112 {
1113 # if defined SPARC || defined SPARC64
1114         /* SPARC systems have the meaning of data and addr reversed */
1115         return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1116 # else
1117         return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1118 # endif
1119 }
1120
1121 # ifndef HAVE_GETREGS_OLD
1122 #  define ptrace_setregset_or_setregs ptrace_setregs
1123 static int
1124 ptrace_setregs(pid_t pid)
1125 {
1126 #  if defined SPARC || defined SPARC64
1127         /* SPARC systems have the meaning of data and addr reversed */
1128         return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1129 #  else
1130         return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1131 #  endif
1132 }
1133 # endif /* !HAVE_GETREGS_OLD */
1134
1135 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1136
1137 void
1138 get_regs(pid_t pid)
1139 {
1140 #undef USE_GET_SYSCALL_RESULT_REGS
1141 #ifdef ptrace_getregset_or_getregs
1142
1143 # ifdef HAVE_GETREGS_OLD
1144         /*
1145          * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1146          * fallback to getregs_old.
1147          */
1148         static int use_getregs_old;
1149         if (use_getregs_old < 0) {
1150                 get_regs_error = ptrace_getregset_or_getregs(pid);
1151                 return;
1152         } else if (use_getregs_old == 0) {
1153                 get_regs_error = ptrace_getregset_or_getregs(pid);
1154                 if (get_regs_error >= 0) {
1155                         use_getregs_old = -1;
1156                         return;
1157                 }
1158                 if (errno == EPERM || errno == ESRCH)
1159                         return;
1160                 use_getregs_old = 1;
1161         }
1162         get_regs_error = getregs_old(pid);
1163 # else /* !HAVE_GETREGS_OLD */
1164         /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1165         get_regs_error = ptrace_getregset_or_getregs(pid);
1166 # endif /* !HAVE_GETREGS_OLD */
1167
1168 #else /* !ptrace_getregset_or_getregs */
1169
1170 # define USE_GET_SYSCALL_RESULT_REGS 1
1171 # warning get_regs is not implemented for this architecture yet
1172         get_regs_error = 0;
1173
1174 #endif /* !ptrace_getregset_or_getregs */
1175 }
1176
1177 #ifdef ptrace_setregset_or_setregs
1178 static int
1179 set_regs(pid_t pid)
1180 {
1181         return ptrace_setregset_or_setregs(pid);
1182 }
1183 #endif /* ptrace_setregset_or_setregs */
1184
1185 struct sysent_buf {
1186         struct tcb *tcp;
1187         struct_sysent ent;
1188         char buf[sizeof("syscall_%lu") + sizeof(long) * 3];
1189 };
1190
1191 static void
1192 free_sysent_buf(void *ptr)
1193 {
1194         struct sysent_buf *s = ptr;
1195         s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1196         free(ptr);
1197 }
1198
1199 /*
1200  * Returns:
1201  * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1202  * 1: ok, continue in trace_syscall_entering().
1203  * other: error, trace_syscall_entering() should print error indicator
1204  *    ("????" etc) and bail out.
1205  */
1206 int
1207 get_scno(struct tcb *tcp)
1208 {
1209         if (get_regs_error)
1210                 return -1;
1211
1212         int rc = arch_get_scno(tcp);
1213         if (rc != 1)
1214                 return rc;
1215
1216         if (SCNO_IS_VALID(tcp->scno)) {
1217                 tcp->s_ent = &sysent[tcp->scno];
1218                 tcp->qual_flg = qual_flags(tcp->scno);
1219         } else {
1220                 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1221
1222                 s->tcp = tcp;
1223                 s->ent.nargs = MAX_ARGS;
1224                 s->ent.sen = SEN_printargs;
1225                 s->ent.sys_func = printargs;
1226                 s->ent.sys_name = s->buf;
1227                 sprintf(s->buf, "syscall_%lu", shuffle_scno(tcp->scno));
1228
1229                 tcp->s_ent = &s->ent;
1230                 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1231
1232                 set_tcb_priv_data(tcp, s, free_sysent_buf);
1233
1234                 if (debug_flag)
1235                         error_msg("pid %d invalid syscall %ld", tcp->pid, tcp->scno);
1236         }
1237         return 1;
1238 }
1239
1240 #ifdef USE_GET_SYSCALL_RESULT_REGS
1241 static int get_syscall_result_regs(struct tcb *);
1242 #endif
1243
1244 /* Returns:
1245  * 1: ok, continue in trace_syscall_exiting().
1246  * -1: error, trace_syscall_exiting() should print error indicator
1247  *    ("????" etc) and bail out.
1248  */
1249 static int
1250 get_syscall_result(struct tcb *tcp)
1251 {
1252 #ifdef USE_GET_SYSCALL_RESULT_REGS
1253         if (get_syscall_result_regs(tcp))
1254                 return -1;
1255 #endif
1256         tcp->u_error = 0;
1257         get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1258
1259         return 1;
1260 }
1261
1262 #include "get_scno.c"
1263 #include "set_scno.c"
1264 #include "get_syscall_args.c"
1265 #ifdef USE_GET_SYSCALL_RESULT_REGS
1266 # include "get_syscall_result.c"
1267 #endif
1268 #include "get_error.c"
1269 #include "set_error.c"
1270 #ifdef HAVE_GETREGS_OLD
1271 # include "getregs_old.c"
1272 #endif
1273
1274 const char *
1275 syscall_name(long scno)
1276 {
1277 #if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
1278         if (current_personality == X32_PERSONALITY_NUMBER)
1279                 scno &= ~__X32_SYSCALL_BIT;
1280 #endif
1281         return SCNO_IS_VALID(scno) ? sysent[scno].sys_name: NULL;
1282 }