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