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