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