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