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