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