]> granicus.if.org Git - strace/blob - syscall.c
xlat: provide fallback definitions for XDP_FLAGS_* constants
[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-2019 The strace developers.
10  * All rights reserved.
11  *
12  * SPDX-License-Identifier: LGPL-2.1-or-later
13  */
14
15 #include "defs.h"
16 #include "get_personality.h"
17 #include "mmap_notify.h"
18 #include "native_defs.h"
19 #include "ptrace.h"
20 #include "ptrace_syscall_info.h"
21 #include "nsig.h"
22 #include "number_set.h"
23 #include "delay.h"
24 #include "retval.h"
25 #include <limits.h>
26
27 /* for struct iovec */
28 #include <sys/uio.h>
29
30 /* for __X32_SYSCALL_BIT */
31 #include "scno.h"
32
33 #include "regs.h"
34
35 #if defined(SPARC64)
36 # undef PTRACE_GETREGS
37 # define PTRACE_GETREGS PTRACE_GETREGS64
38 # undef PTRACE_SETREGS
39 # define PTRACE_SETREGS PTRACE_SETREGS64
40 #endif
41
42 #include "syscall.h"
43 #include "xstring.h"
44 #include "syscallent_base_nr.h"
45
46 /* Define these shorthand notations to simplify the syscallent files. */
47 #include "sysent_shorthand_defs.h"
48
49 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
50
51 const struct_sysent sysent0[] = {
52 #include "syscallent.h"
53 };
54
55 #if SUPPORTED_PERSONALITIES > 1
56 # include PERSONALITY1_INCLUDE_FUNCS
57 static const struct_sysent sysent1[] = {
58 # include "syscallent1.h"
59 };
60 #endif
61
62 #if SUPPORTED_PERSONALITIES > 2
63 # include PERSONALITY2_INCLUDE_FUNCS
64 static const struct_sysent sysent2[] = {
65 # include "syscallent2.h"
66 };
67 #endif
68
69 /* Now undef them since short defines cause wicked namespace pollution. */
70 #include "sysent_shorthand_undefs.h"
71
72 const char *const errnoent[] = {
73 #include "errnoent.h"
74 };
75 const char *const signalent[] = {
76 #include "signalent.h"
77
78 };
79 /*
80  * `ioctlent[012].h' files are automatically generated by the auxiliary
81  * program `ioctlsort', such that the list is sorted by the `code' field.
82  * This has the side-effect of resolving the _IO.. macros into
83  * plain integers, eliminating the need to include here everything
84  * in "/usr/include".
85  */
86
87 const struct_ioctlent ioctlent0[] = {
88 #include "ioctlent0.h"
89 };
90
91 #if SUPPORTED_PERSONALITIES > 1
92 static const struct_ioctlent ioctlent1[] = {
93 # include "ioctlent1.h"
94 };
95 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
96 static const struct_printers printers0 = {
97 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
98 };
99 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
100 static const struct_printers printers1 = {
101 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
102 };
103 #endif
104
105 #if SUPPORTED_PERSONALITIES > 2
106 static const struct_ioctlent ioctlent2[] = {
107 # include "ioctlent2.h"
108 };
109 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
110 static const struct_printers printers2 = {
111 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
112 };
113 #endif
114
115 enum {
116         nsyscalls0 = ARRAY_SIZE(sysent0)
117 #if SUPPORTED_PERSONALITIES > 1
118         , nsyscalls1 = ARRAY_SIZE(sysent1)
119 # if SUPPORTED_PERSONALITIES > 2
120         , nsyscalls2 = ARRAY_SIZE(sysent2)
121 # endif
122 #endif
123 };
124
125 enum {
126         nioctlents0 = ARRAY_SIZE(ioctlent0)
127 #if SUPPORTED_PERSONALITIES > 1
128         , nioctlents1 = ARRAY_SIZE(ioctlent1)
129 # if SUPPORTED_PERSONALITIES > 2
130         , nioctlents2 = ARRAY_SIZE(ioctlent2)
131 # endif
132 #endif
133 };
134
135 #if SUPPORTED_PERSONALITIES > 1
136 const struct_sysent *sysent = sysent0;
137 const struct_ioctlent *ioctlent = ioctlent0;
138 const struct_printers *printers = &printers0;
139 #endif
140
141 const unsigned int nerrnos = ARRAY_SIZE(errnoent);
142 const unsigned int nsignals = ARRAY_SIZE(signalent);
143 unsigned nsyscalls = nsyscalls0;
144 unsigned nioctlents = nioctlents0;
145
146 const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
147         nsyscalls0,
148 #if SUPPORTED_PERSONALITIES > 1
149         nsyscalls1,
150 #endif
151 #if SUPPORTED_PERSONALITIES > 2
152         nsyscalls2,
153 #endif
154 };
155 const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
156         sysent0,
157 #if SUPPORTED_PERSONALITIES > 1
158         sysent1,
159 #endif
160 #if SUPPORTED_PERSONALITIES > 2
161         sysent2,
162 #endif
163 };
164
165 const char *const personality_names[] =
166 #if defined X86_64
167         {"64 bit", "32 bit", "x32"}
168 #elif defined X32
169         {"x32", "32 bit"}
170 #elif SUPPORTED_PERSONALITIES == 2
171         {"64 bit", "32 bit"}
172 #else
173         {STRINGIFY_VAL(__WORDSIZE) " bit"}
174 #endif
175         ;
176
177 const char *const personality_designators[] =
178 #if defined X86_64
179         { "64", "32", "x32" }
180 #elif defined X32
181         { "x32", "32" }
182 #elif SUPPORTED_PERSONALITIES == 2
183         { "64", "32" }
184 #else
185         { STRINGIFY_VAL(__WORDSIZE) }
186 #endif
187         ;
188
189 #if SUPPORTED_PERSONALITIES > 1
190
191 unsigned current_personality;
192
193 # ifndef current_wordsize
194 unsigned current_wordsize = PERSONALITY0_WORDSIZE;
195 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
196         PERSONALITY0_WORDSIZE,
197         PERSONALITY1_WORDSIZE,
198 #  if SUPPORTED_PERSONALITIES > 2
199         PERSONALITY2_WORDSIZE,
200 #  endif
201 };
202 # endif
203
204 # ifndef current_klongsize
205 unsigned current_klongsize = PERSONALITY0_KLONGSIZE;
206 static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
207         PERSONALITY0_KLONGSIZE,
208         PERSONALITY1_KLONGSIZE,
209 #  if SUPPORTED_PERSONALITIES > 2
210         PERSONALITY2_KLONGSIZE,
211 #  endif
212 };
213 # endif
214
215 void
216 set_personality(unsigned int personality)
217 {
218         if (personality == current_personality)
219                 return;
220
221         if (personality >= SUPPORTED_PERSONALITIES)
222                 error_msg_and_die("Requested switch to unsupported personality "
223                                   "%u", personality);
224
225         nsyscalls = nsyscall_vec[personality];
226         sysent = sysent_vec[personality];
227
228         switch (personality) {
229         case 0:
230                 ioctlent = ioctlent0;
231                 nioctlents = nioctlents0;
232                 printers = &printers0;
233                 break;
234
235         case 1:
236                 ioctlent = ioctlent1;
237                 nioctlents = nioctlents1;
238                 printers = &printers1;
239                 break;
240
241 # if SUPPORTED_PERSONALITIES > 2
242         case 2:
243                 ioctlent = ioctlent2;
244                 nioctlents = nioctlents2;
245                 printers = &printers2;
246                 break;
247 # endif
248         }
249
250         current_personality = personality;
251 # ifndef current_wordsize
252         current_wordsize = personality_wordsize[personality];
253 # endif
254 # ifndef current_klongsize
255         current_klongsize = personality_klongsize[personality];
256 # endif
257 }
258
259 static void
260 update_personality(struct tcb *tcp, unsigned int personality)
261 {
262         static bool need_mpers_warning[] =
263                 { false, !HAVE_PERSONALITY_1_MPERS, !HAVE_PERSONALITY_2_MPERS };
264
265         set_personality(personality);
266
267         if (personality == tcp->currpers)
268                 return;
269         tcp->currpers = personality;
270
271         if (!qflag) {
272                 error_msg("[ Process PID=%d runs in %s mode. ]",
273                           tcp->pid, personality_names[personality]);
274         }
275
276         if (need_mpers_warning[personality]) {
277                 error_msg("WARNING: Proper structure decoding for this "
278                           "personality is not supported, please consider "
279                           "building strace with mpers support enabled.");
280                 need_mpers_warning[personality] = false;
281         }
282 }
283 #endif
284
285 #ifdef SYS_socket_subcall
286 static void
287 decode_socket_subcall(struct tcb *tcp)
288 {
289         const int call = tcp->u_arg[0];
290
291         if (call < 1 || call >= SYS_socket_nsubcalls)
292                 return;
293
294         const kernel_ulong_t scno = SYS_socket_subcall + call;
295         const unsigned int nargs = sysent[scno].nargs;
296         uint64_t buf[nargs];
297
298         if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
299                 return;
300
301         tcp->scno = scno;
302         tcp->qual_flg = qual_flags(scno);
303         tcp->s_ent = &sysent[scno];
304
305         unsigned int i;
306         for (i = 0; i < nargs; ++i)
307                 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
308                                 ? ((uint32_t *) (void *) buf)[i] : buf[i];
309 }
310 #endif /* SYS_socket_subcall */
311
312 #ifdef SYS_ipc_subcall
313 static void
314 decode_ipc_subcall(struct tcb *tcp)
315 {
316         unsigned int call = tcp->u_arg[0];
317         const unsigned int version = call >> 16;
318
319         if (version) {
320 # if defined S390 || defined S390X
321                 return;
322 # else
323 #  ifdef SPARC64
324                 if (current_wordsize == 8)
325                         return;
326 #  endif
327                 set_tcb_priv_ulong(tcp, version);
328                 call &= 0xffff;
329 # endif
330         }
331
332         switch (call) {
333                 case  1: case  2: case  3: case  4:
334                 case 11: case 12: case 13: case 14:
335                 case 21: case 22: case 23: case 24:
336                         break;
337                 default:
338                         return;
339         }
340
341         tcp->scno = SYS_ipc_subcall + call;
342         tcp->qual_flg = qual_flags(tcp->scno);
343         tcp->s_ent = &sysent[tcp->scno];
344
345         const unsigned int n = n_args(tcp);
346         unsigned int i;
347         for (i = 0; i < n; i++)
348                 tcp->u_arg[i] = tcp->u_arg[i + 1];
349 }
350 #endif /* SYS_ipc_subcall */
351
352 #ifdef SYS_syscall_subcall
353 /* The implementation is architecture specific.  */
354 static void decode_syscall_subcall(struct tcb *);
355 #endif /* SYS_syscall_subcall */
356
357 static void
358 dumpio(struct tcb *tcp)
359 {
360         int fd = tcp->u_arg[0];
361         if (fd < 0)
362                 return;
363
364         if (is_number_in_set(fd, write_set)) {
365                 switch (tcp_sysent(tcp)->sen) {
366                 case SEN_write:
367                 case SEN_pwrite:
368                 case SEN_send:
369                 case SEN_sendto:
370                 case SEN_mq_timedsend_time32:
371                 case SEN_mq_timedsend_time64:
372                         dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
373                         break;
374                 case SEN_writev:
375                 case SEN_pwritev:
376                 case SEN_pwritev2:
377                 case SEN_vmsplice:
378                         dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
379                         break;
380                 case SEN_sendmsg:
381                         dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
382                         break;
383                 case SEN_sendmmsg:
384                         dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
385                         break;
386                 }
387         }
388
389         if (syserror(tcp))
390                 return;
391
392         if (is_number_in_set(fd, read_set)) {
393                 switch (tcp_sysent(tcp)->sen) {
394                 case SEN_read:
395                 case SEN_pread:
396                 case SEN_recv:
397                 case SEN_recvfrom:
398                 case SEN_mq_timedreceive_time32:
399                 case SEN_mq_timedreceive_time64:
400                         dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
401                         return;
402                 case SEN_readv:
403                 case SEN_preadv:
404                 case SEN_preadv2:
405                         dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
406                                      tcp->u_rval);
407                         return;
408                 case SEN_recvmsg:
409                         dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
410                         return;
411                 case SEN_recvmmsg:
412                 case SEN_recvmmsg_time32:
413                 case SEN_recvmmsg_time64:
414                         dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
415                         return;
416                 }
417         }
418 }
419
420 static const char *
421 err_name(uint64_t err)
422 {
423         return err < nerrnos ? errnoent[err] : NULL;
424 }
425
426 void
427 print_err(int64_t err, bool negated)
428 {
429         const char *str = err_name(negated ? -err : err);
430
431         if (!str || xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
432                 tprintf(negated ? "%" PRId64 : "%" PRIu64, err);
433         if (!str || xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
434                 return;
435         (xlat_verbose(xlat_verbosity) == XLAT_STYLE_ABBREV
436                 ? tprintf : tprintf_comment)("%s%s",
437                                              negated ? "-" : "", str);
438 }
439
440 static void
441 print_err_ret(kernel_ulong_t ret, unsigned long u_error)
442 {
443         const char *u_error_str = err_name(u_error);
444
445         if (u_error_str)
446                 tprintf("= %" PRI_kld " %s (%s)",
447                         ret, u_error_str, strerror(u_error));
448         else
449                 tprintf("= %" PRI_kld " (errno %lu)", ret, u_error);
450 }
451
452 static long get_regs(struct tcb *);
453 static int get_syscall_args(struct tcb *);
454 static int get_syscall_result(struct tcb *);
455 static void get_error(struct tcb *, bool);
456 static void set_error(struct tcb *, unsigned long);
457 static void set_success(struct tcb *, kernel_long_t);
458 static int arch_get_scno(struct tcb *);
459 static int arch_check_scno(struct tcb *);
460 static int arch_set_scno(struct tcb *, kernel_ulong_t);
461 static int arch_get_syscall_args(struct tcb *);
462 static void arch_get_error(struct tcb *, bool);
463 static int arch_set_error(struct tcb *);
464 static int arch_set_success(struct tcb *);
465 #if MAX_ARGS > 6
466 static void arch_get_syscall_args_extra(struct tcb *, unsigned int);
467 #endif
468
469 struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
470
471 static struct inject_opts *
472 tcb_inject_opts(struct tcb *tcp)
473 {
474         return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
475                ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
476 }
477
478
479 static long
480 tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
481 {
482         if (!tcp->inject_vec[current_personality]) {
483                 tcp->inject_vec[current_personality] =
484                         xcalloc(nsyscalls, sizeof(**inject_vec));
485                 memcpy(tcp->inject_vec[current_personality],
486                        inject_vec[current_personality],
487                        nsyscalls * sizeof(**inject_vec));
488         }
489
490         struct inject_opts *opts = tcb_inject_opts(tcp);
491
492         if (!opts || opts->first == 0)
493                 return 0;
494
495         --opts->first;
496
497         if (opts->first != 0)
498                 return 0;
499
500         opts->first = opts->step;
501
502         if (!recovering(tcp)) {
503                 if (opts->data.flags & INJECT_F_SIGNAL)
504                         *signo = opts->data.signo;
505                 if (opts->data.flags & (INJECT_F_ERROR | INJECT_F_RETVAL)) {
506                         kernel_long_t scno =
507                                 (opts->data.flags & INJECT_F_SYSCALL)
508                                 ? (kernel_long_t) shuffle_scno(opts->data.scno)
509                                 : -1;
510
511                         if (!arch_set_scno(tcp, scno)) {
512                                 tcp->flags |= TCB_TAMPERED;
513                                 if (scno != -1)
514                                         tcp->flags |= TCB_TAMPERED_NO_FAIL;
515 #if ARCH_NEEDS_SET_ERROR_FOR_SCNO_TAMPERING
516                                 /*
517                                  * So far it's just a workaround for hppa,
518                                  * but let's pretend it could be used elsewhere.
519                                  */
520                                 else {
521                                         kernel_long_t rval =
522                                                 (opts->data.flags & INJECT_F_RETVAL) ?
523                                                 ENOSYS : retval_get(opts->data.rval_idx);
524
525                                         tcp->u_error = 0; /* force reset */
526                                         set_error(tcp, rval);
527                                 }
528 #endif
529                         }
530                 }
531                 if (opts->data.flags & INJECT_F_DELAY_ENTER)
532                         delay_tcb(tcp, opts->data.delay_idx, true);
533                 if (opts->data.flags & INJECT_F_DELAY_EXIT)
534                         tcp->flags |= TCB_INJECT_DELAY_EXIT;
535         }
536
537         return 0;
538 }
539
540 static long
541 tamper_with_syscall_exiting(struct tcb *tcp)
542 {
543         struct inject_opts *opts = tcb_inject_opts(tcp);
544         if (!opts)
545                 return 0;
546
547         if (inject_delay_exit(tcp))
548                 delay_tcb(tcp, opts->data.delay_idx, false);
549
550         if (!syscall_tampered(tcp))
551                 return 0;
552
553         if (!syserror(tcp) ^ !!syscall_tampered_nofail(tcp)) {
554                 error_msg("Failed to tamper with process %d: unexpectedly got"
555                           " %serror (return value %#" PRI_klx ", error %lu)",
556                           tcp->pid, syscall_tampered_nofail(tcp) ? "" : "no ",
557                           tcp->u_rval, tcp->u_error);
558
559                 return 1;
560         }
561
562         if (opts->data.flags & INJECT_F_RETVAL)
563                 set_success(tcp, retval_get(opts->data.rval_idx));
564         else
565                 set_error(tcp, retval_get(opts->data.rval_idx));
566
567         return 0;
568 }
569
570 /*
571  * Returns:
572  * 0: "ignore this ptrace stop", bail out silently.
573  * 1: ok, decoded; call
574  *    syscall_entering_finish(tcp, syscall_entering_trace(tcp, ...)).
575  * other: error; call syscall_entering_finish(tcp, res), where res is the value
576  *    returned.
577  */
578 int
579 syscall_entering_decode(struct tcb *tcp)
580 {
581         int res = get_scno(tcp);
582         if (res == 0)
583                 return res;
584         if (res != 1 || (res = get_syscall_args(tcp)) != 1) {
585                 printleader(tcp);
586                 tprintf("%s(", tcp_sysent(tcp)->sys_name);
587                 /*
588                  * " <unavailable>" will be added later by the code which
589                  * detects ptrace errors.
590                  */
591                 return res;
592         }
593
594 #ifdef SYS_syscall_subcall
595         if (tcp_sysent(tcp)->sen == SEN_syscall)
596                 decode_syscall_subcall(tcp);
597 #endif
598 #if defined SYS_ipc_subcall     \
599  || defined SYS_socket_subcall
600         switch (tcp_sysent(tcp)->sen) {
601 # ifdef SYS_ipc_subcall
602                 case SEN_ipc:
603                         decode_ipc_subcall(tcp);
604                         break;
605 # endif
606 # ifdef SYS_socket_subcall
607                 case SEN_socketcall:
608                         decode_socket_subcall(tcp);
609                         break;
610 # endif
611         }
612 #endif
613
614         return 1;
615 }
616
617 int
618 syscall_entering_trace(struct tcb *tcp, unsigned int *sig)
619 {
620         if (hide_log(tcp)) {
621                 /*
622                  * Restrain from fault injection
623                  * while the trace executes strace code.
624                  */
625                 tcp->qual_flg &= ~QUAL_INJECT;
626
627                 switch (tcp_sysent(tcp)->sen) {
628                         case SEN_execve:
629                         case SEN_execveat:
630                         case SEN_execv:
631                                 /*
632                                  * First exec* syscall makes the log visible.
633                                  */
634                                 tcp->flags &= ~TCB_HIDE_LOG;
635                                 /*
636                                  * Check whether this exec* syscall succeeds.
637                                  */
638                                 tcp->flags |= TCB_CHECK_EXEC_SYSCALL;
639                                 break;
640                 }
641         }
642
643         if (hide_log(tcp) || !traced(tcp) || (tracing_paths && !pathtrace_match(tcp))) {
644                 tcp->flags |= TCB_FILTERED;
645                 return 0;
646         }
647
648         tcp->flags &= ~TCB_FILTERED;
649
650         if (inject(tcp))
651                 tamper_with_syscall_entering(tcp, sig);
652
653         if (cflag == CFLAG_ONLY_STATS) {
654                 return 0;
655         }
656
657 #ifdef ENABLE_STACKTRACE
658         if (stack_trace_enabled) {
659                 if (tcp_sysent(tcp)->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
660                         unwind_tcb_capture(tcp);
661         }
662 #endif
663
664         if (!is_complete_set(status_set, NUMBER_OF_STATUSES))
665                 strace_open_memstream(tcp);
666
667         printleader(tcp);
668         tprintf("%s(", tcp_sysent(tcp)->sys_name);
669         int res = raw(tcp) ? printargs(tcp) : tcp_sysent(tcp)->sys_func(tcp);
670         fflush(tcp->outf);
671         return res;
672 }
673
674 void
675 syscall_entering_finish(struct tcb *tcp, int res)
676 {
677         tcp->flags |= TCB_INSYSCALL;
678         tcp->sys_func_rval = res;
679
680         /* Measure the entrance time as late as possible to avoid errors. */
681         if ((Tflag || cflag) && !filtered(tcp))
682                 clock_gettime(CLOCK_MONOTONIC, &tcp->etime);
683
684         /* Start tracking system time */
685         if (cflag) {
686                 if (debug_flag) {
687                         struct timespec dt;
688
689                         ts_sub(&dt, &tcp->stime, &tcp->ltime);
690
691                         if (ts_nz(&dt))
692                                 debug_func_msg("pid %d: %.9f seconds of system "
693                                                "time spent since the last "
694                                                "syscall exit",
695                                                tcp->pid, ts_float(&dt));
696                 }
697
698                 tcp->ltime = tcp->stime;
699         }
700 }
701
702 /* Returns:
703  * 0: "bail out".
704  * 1: ok.
705  * -1: error in one of ptrace ops.
706  *
707  * If not 0, call syscall_exiting_trace(tcp, res), where res is the return
708  *    value. Anyway, call syscall_exiting_finish(tcp) then.
709  */
710 int
711 syscall_exiting_decode(struct tcb *tcp, struct timespec *pts)
712 {
713         /* Measure the exit time as early as possible to avoid errors. */
714         if ((Tflag || cflag) && !filtered(tcp))
715                 clock_gettime(CLOCK_MONOTONIC, pts);
716
717         if (tcp_sysent(tcp)->sys_flags & MEMORY_MAPPING_CHANGE)
718                 mmap_notify_report(tcp);
719
720         if (filtered(tcp))
721                 return 0;
722
723         if (check_exec_syscall(tcp)) {
724                 /* The check failed, hide the log.  */
725                 tcp->flags |= TCB_HIDE_LOG;
726         }
727
728 #if SUPPORTED_PERSONALITIES > 1
729         update_personality(tcp, tcp->currpers);
730 #endif
731
732         return get_syscall_result(tcp);
733 }
734
735 void
736 print_syscall_resume(struct tcb *tcp)
737 {
738         /* If not in -ff mode, and printing_tcp != tcp,
739          * then the log currently does not end with output
740          * of _our syscall entry_, but with something else.
741          * We need to say which syscall's return is this.
742          *
743          * Forced reprinting via TCB_REPRINT is used only by
744          * "strace -ff -oLOG test/threaded_execve" corner case.
745          * It's the only case when -ff mode needs reprinting.
746          */
747         if ((followfork < 2 && printing_tcp != tcp && !tcp->staged_output_data)
748             || (tcp->flags & TCB_REPRINT)) {
749                 tcp->flags &= ~TCB_REPRINT;
750                 printleader(tcp);
751                 tprintf("<... %s resumed>", tcp_sysent(tcp)->sys_name);
752         }
753 }
754
755 int
756 syscall_exiting_trace(struct tcb *tcp, struct timespec *ts, int res)
757 {
758         if (syscall_tampered(tcp) || inject_delay_exit(tcp))
759                 tamper_with_syscall_exiting(tcp);
760
761         if (cflag) {
762                 count_syscall(tcp, ts);
763                 if (cflag == CFLAG_ONLY_STATS) {
764                         return 0;
765                 }
766         }
767
768         print_syscall_resume(tcp);
769         printing_tcp = tcp;
770
771         tcp->s_prev_ent = NULL;
772         if (res != 1) {
773                 /* There was error in one of prior ptrace ops */
774                 tprints(") ");
775                 tabto();
776                 tprints("= ? <unavailable>\n");
777                 if (!is_complete_set(status_set, NUMBER_OF_STATUSES)) {
778                         bool publish = is_number_in_set(STATUS_UNAVAILABLE,
779                                                         status_set);
780                         strace_close_memstream(tcp, publish);
781                 }
782                 line_ended();
783                 return res;
784         }
785         tcp->s_prev_ent = tcp->s_ent;
786
787         int sys_res = 0;
788         if (raw(tcp)) {
789                 /* sys_res = printargs(tcp); - but it's nop on sysexit */
790         } else {
791                 if (tcp->sys_func_rval & RVAL_DECODED)
792                         sys_res = tcp->sys_func_rval;
793                 else
794                         sys_res = tcp_sysent(tcp)->sys_func(tcp);
795         }
796
797         if (!is_complete_set(status_set, NUMBER_OF_STATUSES)) {
798                 bool publish = syserror(tcp)
799                                && is_number_in_set(STATUS_FAILED, status_set);
800                 publish |= !syserror(tcp)
801                            && is_number_in_set(STATUS_SUCCESSFUL, status_set);
802                 strace_close_memstream(tcp, publish);
803                 if (!publish) {
804                         line_ended();
805                         return 0;
806                 }
807         }
808
809         tprints(") ");
810         tabto();
811
812         if (raw(tcp)) {
813                 if (tcp->u_error)
814                         print_err_ret(tcp->u_rval, tcp->u_error);
815                 else
816                         tprintf("= %#" PRI_klx, tcp->u_rval);
817
818                 if (syscall_tampered(tcp))
819                         tprints(" (INJECTED)");
820         } else if (!(sys_res & RVAL_NONE) && tcp->u_error) {
821                 switch (tcp->u_error) {
822                 /* Blocked signals do not interrupt any syscalls.
823                  * In this case syscalls don't return ERESTARTfoo codes.
824                  *
825                  * Deadly signals set to SIG_DFL interrupt syscalls
826                  * and kill the process regardless of which of the codes below
827                  * is returned by the interrupted syscall.
828                  * In some cases, kernel forces a kernel-generated deadly
829                  * signal to be unblocked and set to SIG_DFL (and thus cause
830                  * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
831                  * or SIGILL. (The alternative is to leave process spinning
832                  * forever on the faulty instruction - not useful).
833                  *
834                  * SIG_IGNed signals and non-deadly signals set to SIG_DFL
835                  * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
836                  * but kernel will always restart them.
837                  */
838                 case ERESTARTSYS:
839                         /* Most common type of signal-interrupted syscall exit code.
840                          * The system call will be restarted with the same arguments
841                          * if SA_RESTART is set; otherwise, it will fail with EINTR.
842                          */
843                         tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
844                         break;
845                 case ERESTARTNOINTR:
846                         /* Rare. For example, fork() returns this if interrupted.
847                          * SA_RESTART is ignored (assumed set): the restart is unconditional.
848                          */
849                         tprints("= ? ERESTARTNOINTR (To be restarted)");
850                         break;
851                 case ERESTARTNOHAND:
852                         /* pause(), rt_sigsuspend() etc use this code.
853                          * SA_RESTART is ignored (assumed not set):
854                          * syscall won't restart (will return EINTR instead)
855                          * even after signal with SA_RESTART set. However,
856                          * after SIG_IGN or SIG_DFL signal it will restart
857                          * (thus the name "restart only if has no handler").
858                          */
859                         tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
860                         break;
861                 case ERESTART_RESTARTBLOCK:
862                         /* Syscalls like nanosleep(), poll() which can't be
863                          * restarted with their original arguments use this
864                          * code. Kernel will execute restart_syscall() instead,
865                          * which changes arguments before restarting syscall.
866                          * SA_RESTART is ignored (assumed not set) similarly
867                          * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
868                          * since restart data is saved in "restart block"
869                          * in task struct, and if signal handler uses a syscall
870                          * which in turn saves another such restart block,
871                          * old data is lost and restart becomes impossible)
872                          */
873                         tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
874                         break;
875                 default:
876                         print_err_ret(tcp->u_rval, tcp->u_error);
877                         break;
878                 }
879                 if (syscall_tampered(tcp))
880                         tprints(" (INJECTED)");
881                 if ((sys_res & RVAL_STR) && tcp->auxstr)
882                         tprintf(" (%s)", tcp->auxstr);
883         } else {
884                 if (sys_res & RVAL_NONE)
885                         tprints("= ?");
886                 else {
887                         switch (sys_res & RVAL_MASK) {
888                         case RVAL_HEX:
889 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
890                                 if (current_klongsize < sizeof(tcp->u_rval)) {
891                                         tprintf("= %#x",
892                                                 (unsigned int) tcp->u_rval);
893                                 } else
894 #endif
895                                 {
896                                         tprintf("= %#" PRI_klx, tcp->u_rval);
897                                 }
898                                 break;
899                         case RVAL_OCTAL:
900                                 tprints("= ");
901                                 print_numeric_long_umask(tcp->u_rval);
902                                 break;
903                         case RVAL_UDECIMAL:
904 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
905                                 if (current_klongsize < sizeof(tcp->u_rval)) {
906                                         tprintf("= %u",
907                                                 (unsigned int) tcp->u_rval);
908                                 } else
909 #endif
910                                 {
911                                         tprintf("= %" PRI_klu, tcp->u_rval);
912                                 }
913                                 break;
914                         case RVAL_FD:
915                                 if (show_fd_path) {
916                                         tprints("= ");
917                                         printfd(tcp, tcp->u_rval);
918                                 } else
919                                         tprintf("= %" PRI_kld, tcp->u_rval);
920                                 break;
921                         default:
922                                 error_msg("invalid rval format");
923                                 break;
924                         }
925                 }
926                 if ((sys_res & RVAL_STR) && tcp->auxstr)
927                         tprintf(" (%s)", tcp->auxstr);
928                 if (syscall_tampered(tcp))
929                         tprints(" (INJECTED)");
930         }
931         if (Tflag) {
932                 ts_sub(ts, ts, &tcp->etime);
933                 tprintf(" <%ld.%06ld>",
934                         (long) ts->tv_sec, (long) ts->tv_nsec / 1000);
935         }
936         tprints("\n");
937         dumpio(tcp);
938         line_ended();
939
940 #ifdef ENABLE_STACKTRACE
941         if (stack_trace_enabled)
942                 unwind_tcb_print(tcp);
943 #endif
944         return 0;
945 }
946
947 void
948 syscall_exiting_finish(struct tcb *tcp)
949 {
950         tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED | TCB_INJECT_DELAY_EXIT);
951         tcp->sys_func_rval = 0;
952         free_tcb_priv_data(tcp);
953
954         if (cflag)
955                 tcp->ltime = tcp->stime;
956 }
957
958 bool
959 is_erestart(struct tcb *tcp)
960 {
961         switch (tcp->u_error) {
962                 case ERESTARTSYS:
963                 case ERESTARTNOINTR:
964                 case ERESTARTNOHAND:
965                 case ERESTART_RESTARTBLOCK:
966                         return true;
967                 default:
968                         return false;
969         }
970 }
971
972 static unsigned long saved_u_error;
973
974 void
975 temporarily_clear_syserror(struct tcb *tcp)
976 {
977         saved_u_error = tcp->u_error;
978         tcp->u_error = 0;
979 }
980
981 void
982 restore_cleared_syserror(struct tcb *tcp)
983 {
984         tcp->u_error = saved_u_error;
985 }
986
987 static struct ptrace_syscall_info ptrace_sci;
988
989 static bool
990 ptrace_syscall_info_is_valid(void)
991 {
992         return ptrace_get_syscall_info_supported &&
993                ptrace_sci.op <= PTRACE_SYSCALL_INFO_SECCOMP;
994 }
995
996 #define XLAT_MACROS_ONLY
997 #include "xlat/nt_descriptor_types.h"
998 #undef XLAT_MACROS_ONLY
999
1000 #define ARCH_MIGHT_USE_SET_REGS 1
1001
1002 #include "arch_regs.c"
1003
1004 #if HAVE_ARCH_GETRVAL2
1005 # include "arch_getrval2.c"
1006 #endif
1007
1008 #include "getregs_old.h"
1009 #ifdef HAVE_GETREGS_OLD
1010 /* Either getregs_old() or set_regs() */
1011 # undef ARCH_MIGHT_USE_SET_REGS
1012 # define ARCH_MIGHT_USE_SET_REGS 0
1013 #endif
1014
1015 #undef ptrace_getregset_or_getregs
1016 #undef ptrace_setregset_or_setregs
1017 #ifdef ARCH_REGS_FOR_GETREGSET
1018
1019 # define ptrace_getregset_or_getregs ptrace_getregset
1020 static long
1021 ptrace_getregset(pid_t pid)
1022 {
1023 # ifdef ARCH_IOVEC_FOR_GETREGSET
1024         /* variable iovec */
1025         ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1026         return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1027                       &ARCH_IOVEC_FOR_GETREGSET);
1028 # else
1029         /* constant iovec */
1030         static struct iovec io = {
1031                 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1032                 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1033         };
1034         return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1035
1036 # endif
1037 }
1038
1039 # if ARCH_MIGHT_USE_SET_REGS
1040 #  define ptrace_setregset_or_setregs ptrace_setregset
1041 static int
1042 ptrace_setregset(pid_t pid)
1043 {
1044 #  ifdef ARCH_IOVEC_FOR_GETREGSET
1045         /* variable iovec */
1046         return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1047                       &ARCH_IOVEC_FOR_GETREGSET);
1048 #  else
1049         /* constant iovec */
1050         static struct iovec io = {
1051                 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1052                 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1053         };
1054         return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1055 #  endif
1056 }
1057 # endif /* ARCH_MIGHT_USE_SET_REGS */
1058
1059 #elif defined ARCH_REGS_FOR_GETREGS
1060
1061 # define ptrace_getregset_or_getregs ptrace_getregs
1062 static long
1063 ptrace_getregs(pid_t pid)
1064 {
1065 # if defined SPARC || defined SPARC64
1066         /* SPARC systems have the meaning of data and addr reversed */
1067         return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1068 # else
1069         return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1070 # endif
1071 }
1072
1073 # if ARCH_MIGHT_USE_SET_REGS
1074 #  define ptrace_setregset_or_setregs ptrace_setregs
1075 static int
1076 ptrace_setregs(pid_t pid)
1077 {
1078 #  if defined SPARC || defined SPARC64
1079         /* SPARC systems have the meaning of data and addr reversed */
1080         return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1081 #  else
1082         return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1083 #  endif
1084 }
1085 # endif /* ARCH_MIGHT_USE_SET_REGS */
1086
1087 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1088
1089 static long get_regs_error = -1;
1090
1091 void
1092 clear_regs(struct tcb *tcp)
1093 {
1094         ptrace_sci.op = 0xff;
1095         get_regs_error = -1;
1096 }
1097
1098 static long
1099 get_regs(struct tcb *const tcp)
1100 {
1101 #ifdef ptrace_getregset_or_getregs
1102
1103         if (get_regs_error != -1)
1104                 return get_regs_error;
1105
1106 # ifdef HAVE_GETREGS_OLD
1107         /*
1108          * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1109          * fallback to getregs_old.
1110          */
1111         static int use_getregs_old;
1112         if (use_getregs_old < 0) {
1113                 return get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1114         } else if (use_getregs_old == 0) {
1115                 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1116                 if (get_regs_error >= 0) {
1117                         use_getregs_old = -1;
1118                         return get_regs_error;
1119                 }
1120                 if (errno == EPERM || errno == ESRCH)
1121                         return get_regs_error;
1122                 use_getregs_old = 1;
1123         }
1124         return get_regs_error = getregs_old(tcp);
1125 # else /* !HAVE_GETREGS_OLD */
1126         /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1127         get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1128
1129 #  if defined ARCH_PERSONALITY_0_IOV_SIZE
1130         if (get_regs_error)
1131                 return get_regs_error;
1132
1133         switch (ARCH_IOVEC_FOR_GETREGSET.iov_len) {
1134         case ARCH_PERSONALITY_0_IOV_SIZE:
1135                 update_personality(tcp, 0);
1136                 break;
1137         case ARCH_PERSONALITY_1_IOV_SIZE:
1138                 update_personality(tcp, 1);
1139                 break;
1140         default: {
1141                 static bool printed = false;
1142
1143                 if (!printed) {
1144                         error_msg("Unsupported regset size returned by "
1145                                   "PTRACE_GETREGSET: %zu",
1146                                   ARCH_IOVEC_FOR_GETREGSET.iov_len);
1147
1148                         printed = true;
1149                 }
1150
1151                 update_personality(tcp, 0);
1152         }
1153         }
1154 #  endif /* ARCH_PERSONALITY_0_IOV_SIZE */
1155
1156         return get_regs_error;
1157
1158 # endif /* !HAVE_GETREGS_OLD */
1159
1160 #else /* !ptrace_getregset_or_getregs */
1161
1162 # warning get_regs is not implemented for this architecture yet
1163         return 0;
1164
1165 #endif /* !ptrace_getregset_or_getregs */
1166 }
1167
1168 #ifdef ptrace_setregset_or_setregs
1169 static int
1170 set_regs(pid_t pid)
1171 {
1172         return ptrace_setregset_or_setregs(pid);
1173 }
1174 #endif /* ptrace_setregset_or_setregs */
1175
1176 struct sysent_buf {
1177         struct tcb *tcp;
1178         struct_sysent ent;
1179         char buf[sizeof("syscall_0x") + sizeof(kernel_ulong_t) * 2];
1180 };
1181
1182 static void
1183 free_sysent_buf(void *ptr)
1184 {
1185         struct sysent_buf *s = ptr;
1186         s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1187         free(ptr);
1188 }
1189
1190 static bool
1191 strace_get_syscall_info(struct tcb *tcp)
1192 {
1193         /*
1194          * ptrace_get_syscall_info_supported should have been checked
1195          * by the caller.
1196          */
1197         if (ptrace_sci.op == 0xff) {
1198                 const size_t size = sizeof(ptrace_sci);
1199                 if (ptrace(PTRACE_GET_SYSCALL_INFO, tcp->pid,
1200                            (void *) size, &ptrace_sci) < 0) {
1201                         get_regs_error = -2;
1202                         return false;
1203                 }
1204 #if SUPPORTED_PERSONALITIES > 1
1205                 int newpers = get_personality_from_syscall_info(&ptrace_sci);
1206                 if (newpers >= 0)
1207                         update_personality(tcp, newpers);
1208 #endif
1209         }
1210
1211         if (entering(tcp)) {
1212                 if (ptrace_sci.op == PTRACE_SYSCALL_INFO_EXIT) {
1213                         error_msg("pid %d: entering"
1214                                   ", ptrace_syscall_info.op == %u",
1215                                   tcp->pid, ptrace_sci.op);
1216                         /* TODO: handle this.  */
1217                 }
1218         } else {
1219                 if (ptrace_sci.op == PTRACE_SYSCALL_INFO_ENTRY) {
1220                         error_msg("pid %d: exiting"
1221                                   ", ptrace_syscall_info.op == %u",
1222                                   tcp->pid, ptrace_sci.op);
1223                         /* TODO: handle this.  */
1224                 }
1225         }
1226
1227         return true;
1228 }
1229
1230 bool
1231 get_instruction_pointer(struct tcb *tcp, kernel_ulong_t *ip)
1232 {
1233         if (get_regs_error < -1)
1234                 return false;
1235
1236         if (ptrace_get_syscall_info_supported) {
1237                 if (!strace_get_syscall_info(tcp))
1238                         return false;
1239                 *ip = (kernel_ulong_t) ptrace_sci.instruction_pointer;
1240                 return true;
1241         }
1242
1243 #if defined ARCH_PC_REG
1244         if (get_regs(tcp) < 0)
1245                 return false;
1246         *ip = (kernel_ulong_t) ARCH_PC_REG;
1247         return true;
1248 #elif defined ARCH_PC_PEEK_ADDR
1249         if (upeek(tcp, ARCH_PC_PEEK_ADDR, ip) < 0)
1250                 return false;
1251         return true;
1252 #else
1253 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1254 #endif
1255 }
1256
1257 bool
1258 get_stack_pointer(struct tcb *tcp, kernel_ulong_t *sp)
1259 {
1260         if (get_regs_error < -1)
1261                 return false;
1262
1263         if (ptrace_get_syscall_info_supported) {
1264                 if (!strace_get_syscall_info(tcp))
1265                         return false;
1266                 *sp = (kernel_ulong_t) ptrace_sci.stack_pointer;
1267                 return true;
1268         }
1269
1270 #if defined ARCH_SP_REG
1271         if (get_regs(tcp) < 0)
1272                 return false;
1273         *sp = (kernel_ulong_t) ARCH_SP_REG;
1274         return true;
1275 #elif defined ARCH_SP_PEEK_ADDR
1276         if (upeek(tcp, ARCH_SP_PEEK_ADDR, sp) < 0)
1277                 return false;
1278         return true;
1279 #else
1280         return false;
1281 #endif
1282 }
1283
1284 static int
1285 get_syscall_regs(struct tcb *tcp)
1286 {
1287         if (get_regs_error != -1)
1288                 return get_regs_error;
1289
1290         if (ptrace_get_syscall_info_supported)
1291                 return strace_get_syscall_info(tcp) ? 0 : get_regs_error;
1292
1293         return get_regs(tcp);
1294 }
1295
1296 const struct_sysent stub_sysent = {
1297         .nargs = MAX_ARGS,
1298         .sys_flags = MEMORY_MAPPING_CHANGE,
1299         .sen = SEN_printargs,
1300         .sys_func = printargs,
1301         .sys_name = "????",
1302 };
1303
1304 /*
1305  * Returns:
1306  * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
1307  *    out silently" code.
1308  * 1: ok, continue in syscall_entering_decode().
1309  * other: error, syscall_entering_decode() should print error indicator
1310  *    ("????" etc) and return an appropriate code.
1311  */
1312 int
1313 get_scno(struct tcb *tcp)
1314 {
1315         tcp->scno = -1;
1316         tcp->s_ent = NULL;
1317         tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1318
1319         if (get_syscall_regs(tcp) < 0)
1320                 return -1;
1321
1322         if (ptrace_syscall_info_is_valid()) {
1323                 /*
1324                  * So far it's just a workaround for x32,
1325                  * but let's pretend it could be used elsewhere.
1326                  */
1327                 int rc = arch_check_scno(tcp);
1328                 if (rc != 1)
1329                         return rc;
1330                 tcp->scno = ptrace_sci.entry.nr;
1331         } else {
1332                 int rc = arch_get_scno(tcp);
1333                 if (rc != 1)
1334                         return rc;
1335         }
1336
1337         tcp->scno = shuffle_scno(tcp->scno);
1338
1339         if (scno_is_valid(tcp->scno)) {
1340                 tcp->s_ent = &sysent[tcp->scno];
1341                 tcp->qual_flg = qual_flags(tcp->scno);
1342         } else {
1343                 struct sysent_buf *s = xzalloc(sizeof(*s));
1344
1345                 s->tcp = tcp;
1346                 s->ent = stub_sysent;
1347                 s->ent.sys_name = s->buf;
1348                 xsprintf(s->buf, "syscall_%#" PRI_klx, shuffle_scno(tcp->scno));
1349
1350                 tcp->s_ent = &s->ent;
1351
1352                 set_tcb_priv_data(tcp, s, free_sysent_buf);
1353
1354                 debug_msg("pid %d invalid syscall %#" PRI_klx,
1355                           tcp->pid, shuffle_scno(tcp->scno));
1356         }
1357
1358         /*
1359          * We refrain from argument decoding during recovering
1360          * as tracee memory mappings has changed and the registers
1361          * are very likely pointing to garbage already.
1362          */
1363         if (recovering(tcp))
1364                 tcp->qual_flg |= QUAL_RAW;
1365
1366         return 1;
1367 }
1368
1369 static int
1370 get_syscall_args(struct tcb *tcp)
1371 {
1372         if (ptrace_syscall_info_is_valid()) {
1373                 const unsigned int n =
1374                         MIN(ARRAY_SIZE(tcp->u_arg),
1375                             ARRAY_SIZE(ptrace_sci.entry.args));
1376                 for (unsigned int i = 0; i < n; ++i)
1377                         tcp->u_arg[i] = ptrace_sci.entry.args[i];
1378 #if SUPPORTED_PERSONALITIES > 1
1379                 if (tcp_sysent(tcp)->sys_flags & COMPAT_SYSCALL_TYPES) {
1380                         for (unsigned int i = 0; i < n; ++i)
1381                                 tcp->u_arg[i] = (uint32_t) tcp->u_arg[i];
1382                 }
1383 #endif
1384                 /*
1385                  * So far it's just a workaround for mips o32,
1386                  * but let's pretend it could be used elsewhere.
1387                  */
1388 #if MAX_ARGS > 6
1389                 arch_get_syscall_args_extra(tcp, n);
1390 #endif
1391                 return 1;
1392         }
1393         return arch_get_syscall_args(tcp);
1394 }
1395
1396 #ifdef ptrace_getregset_or_getregs
1397 # define get_syscall_result_regs get_syscall_regs
1398 #else
1399 static int get_syscall_result_regs(struct tcb *);
1400 #endif
1401
1402 /* Returns:
1403  * 1: ok, continue in syscall_exiting_trace().
1404  * -1: error, syscall_exiting_trace() should print error indicator
1405  *    ("????" etc) and bail out.
1406  */
1407 static int
1408 get_syscall_result(struct tcb *tcp)
1409 {
1410         if (get_syscall_result_regs(tcp) < 0)
1411                 return -1;
1412         get_error(tcp,
1413                   (!(tcp_sysent(tcp)->sys_flags & SYSCALL_NEVER_FAILS)
1414                         || syscall_tampered(tcp))
1415                   && !syscall_tampered_nofail(tcp));
1416
1417         return 1;
1418 }
1419
1420 static void
1421 get_error(struct tcb *tcp, const bool check_errno)
1422 {
1423         if (ptrace_syscall_info_is_valid()) {
1424                 if (ptrace_sci.exit.is_error) {
1425                         tcp->u_rval = -1;
1426                         tcp->u_error = -ptrace_sci.exit.rval;
1427                 } else {
1428                         tcp->u_error = 0;
1429                         tcp->u_rval = ptrace_sci.exit.rval;
1430                 }
1431         } else {
1432                 tcp->u_error = 0;
1433                 arch_get_error(tcp, check_errno);
1434         }
1435 }
1436
1437 static void
1438 set_error(struct tcb *tcp, unsigned long new_error)
1439 {
1440         const unsigned long old_error = tcp->u_error;
1441
1442         if (new_error == old_error || new_error > MAX_ERRNO_VALUE)
1443                 return;
1444
1445 #ifdef ptrace_setregset_or_setregs
1446         /* if we are going to invoke set_regs, call get_regs first */
1447         if (get_regs(tcp) < 0)
1448                 return;
1449 #endif
1450
1451         tcp->u_error = new_error;
1452         if (arch_set_error(tcp)) {
1453                 tcp->u_error = old_error;
1454                 /* arch_set_error does not update u_rval */
1455         } else {
1456                 if (ptrace_syscall_info_is_valid())
1457                         tcp->u_rval = -1;
1458                 else
1459                         get_error(tcp, !(tcp_sysent(tcp)->sys_flags &
1460                                          SYSCALL_NEVER_FAILS));
1461         }
1462 }
1463
1464 static void
1465 set_success(struct tcb *tcp, kernel_long_t new_rval)
1466 {
1467         const kernel_long_t old_rval = tcp->u_rval;
1468
1469 #ifdef ptrace_setregset_or_setregs
1470         /* if we are going to invoke set_regs, call get_regs first */
1471         if (get_regs(tcp) < 0)
1472                 return;
1473 #endif
1474
1475         tcp->u_rval = new_rval;
1476         if (arch_set_success(tcp)) {
1477                 tcp->u_rval = old_rval;
1478                 /* arch_set_success does not update u_error */
1479         } else {
1480                 if (ptrace_syscall_info_is_valid())
1481                         tcp->u_error = 0;
1482                 else
1483                         get_error(tcp, !(tcp_sysent(tcp)->sys_flags &
1484                                          SYSCALL_NEVER_FAILS));
1485         }
1486 }
1487
1488 #include "get_scno.c"
1489 #include "check_scno.c"
1490 #include "set_scno.c"
1491 #include "get_syscall_args.c"
1492 #ifndef ptrace_getregset_or_getregs
1493 # include "get_syscall_result.c"
1494 #endif
1495 #include "get_error.c"
1496 #include "set_error.c"
1497 #ifdef HAVE_GETREGS_OLD
1498 # include "getregs_old.c"
1499 #endif
1500 #include "shuffle_scno.c"
1501
1502 const char *
1503 syscall_name(kernel_ulong_t scno)
1504 {
1505         return scno_is_valid(scno) ? sysent[scno].sys_name : NULL;
1506 }