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