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