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