]> granicus.if.org Git - strace/blob - syscall.c
xlat: update NT_* 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-2017 The strace developers.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include "defs.h"
36 #include "native_defs.h"
37 #include "nsig.h"
38 #include "number_set.h"
39 #include <sys/param.h>
40
41 /* for struct iovec */
42 #include <sys/uio.h>
43
44 /* for __X32_SYSCALL_BIT */
45 #include <asm/unistd.h>
46
47 #include "regs.h"
48 #include "ptrace.h"
49
50 #if defined(SPARC64)
51 # undef PTRACE_GETREGS
52 # define PTRACE_GETREGS PTRACE_GETREGS64
53 # undef PTRACE_SETREGS
54 # define PTRACE_SETREGS PTRACE_SETREGS64
55 #endif
56
57 #ifndef NT_PRSTATUS
58 # define NT_PRSTATUS 1
59 #endif
60
61 #include "syscall.h"
62 #include "xstring.h"
63
64 /* Define these shorthand notations to simplify the syscallent files. */
65 #include "sysent_shorthand_defs.h"
66
67 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
68
69 const struct_sysent sysent0[] = {
70 #include "syscallent.h"
71 };
72
73 #if SUPPORTED_PERSONALITIES > 1
74 # include PERSONALITY1_INCLUDE_FUNCS
75 static const struct_sysent sysent1[] = {
76 # include "syscallent1.h"
77 };
78 #endif
79
80 #if SUPPORTED_PERSONALITIES > 2
81 # include PERSONALITY2_INCLUDE_FUNCS
82 static const struct_sysent sysent2[] = {
83 # include "syscallent2.h"
84 };
85 #endif
86
87 /* Now undef them since short defines cause wicked namespace pollution. */
88 #include "sysent_shorthand_undefs.h"
89
90 /*
91  * `ioctlent[012].h' files are automatically generated by the auxiliary
92  * program `ioctlsort', such that the list is sorted by the `code' field.
93  * This has the side-effect of resolving the _IO.. macros into
94  * plain integers, eliminating the need to include here everything
95  * in "/usr/include".
96  */
97
98 const char *const errnoent0[] = {
99 #include "errnoent.h"
100 };
101 const char *const signalent0[] = {
102 #include "signalent.h"
103 };
104 const struct_ioctlent ioctlent0[] = {
105 #include "ioctlent0.h"
106 };
107
108 #if SUPPORTED_PERSONALITIES > 1
109 static const char *const errnoent1[] = {
110 # include "errnoent1.h"
111 };
112 static const char *const signalent1[] = {
113 # include "signalent1.h"
114 };
115 static const struct_ioctlent ioctlent1[] = {
116 # include "ioctlent1.h"
117 };
118 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
119 static const struct_printers printers0 = {
120 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
121 };
122 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
123 static const struct_printers printers1 = {
124 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
125 };
126 #endif
127
128 #if SUPPORTED_PERSONALITIES > 2
129 static const char *const errnoent2[] = {
130 # include "errnoent2.h"
131 };
132 static const char *const signalent2[] = {
133 # include "signalent2.h"
134 };
135 static const struct_ioctlent ioctlent2[] = {
136 # include "ioctlent2.h"
137 };
138 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
139 static const struct_printers printers2 = {
140 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
141 };
142 #endif
143
144 enum {
145         nsyscalls0 = ARRAY_SIZE(sysent0)
146 #if SUPPORTED_PERSONALITIES > 1
147         , nsyscalls1 = ARRAY_SIZE(sysent1)
148 # if SUPPORTED_PERSONALITIES > 2
149         , nsyscalls2 = ARRAY_SIZE(sysent2)
150 # endif
151 #endif
152 };
153
154 enum {
155         nerrnos0 = ARRAY_SIZE(errnoent0)
156 #if SUPPORTED_PERSONALITIES > 1
157         , nerrnos1 = ARRAY_SIZE(errnoent1)
158 # if SUPPORTED_PERSONALITIES > 2
159         , nerrnos2 = ARRAY_SIZE(errnoent2)
160 # endif
161 #endif
162 };
163
164 enum {
165         nsignals0 = ARRAY_SIZE(signalent0)
166 #if SUPPORTED_PERSONALITIES > 1
167         , nsignals1 = ARRAY_SIZE(signalent1)
168 # if SUPPORTED_PERSONALITIES > 2
169         , nsignals2 = ARRAY_SIZE(signalent2)
170 # endif
171 #endif
172 };
173
174 enum {
175         nioctlents0 = ARRAY_SIZE(ioctlent0)
176 #if SUPPORTED_PERSONALITIES > 1
177         , nioctlents1 = ARRAY_SIZE(ioctlent1)
178 # if SUPPORTED_PERSONALITIES > 2
179         , nioctlents2 = ARRAY_SIZE(ioctlent2)
180 # endif
181 #endif
182 };
183
184 #if SUPPORTED_PERSONALITIES > 1
185 const struct_sysent *sysent = sysent0;
186 const char *const *errnoent = errnoent0;
187 const char *const *signalent = signalent0;
188 const struct_ioctlent *ioctlent = ioctlent0;
189 const struct_printers *printers = &printers0;
190 #endif
191
192 unsigned nsyscalls = nsyscalls0;
193 unsigned nerrnos = nerrnos0;
194 unsigned nsignals = nsignals0;
195 unsigned nioctlents = nioctlents0;
196
197 const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
198         nsyscalls0,
199 #if SUPPORTED_PERSONALITIES > 1
200         nsyscalls1,
201 #endif
202 #if SUPPORTED_PERSONALITIES > 2
203         nsyscalls2,
204 #endif
205 };
206 const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
207         sysent0,
208 #if SUPPORTED_PERSONALITIES > 1
209         sysent1,
210 #endif
211 #if SUPPORTED_PERSONALITIES > 2
212         sysent2,
213 #endif
214 };
215
216 const char *const personality_names[] =
217 # if defined X86_64
218         {"64 bit", "32 bit", "x32"}
219 # elif defined X32
220         {"x32", "32 bit"}
221 # elif SUPPORTED_PERSONALITIES == 2
222         {"64 bit", "32 bit"}
223 # else
224         {STRINGIFY_VAL(__WORDSIZE) " bit"}
225 # endif
226         ;
227
228 #if SUPPORTED_PERSONALITIES > 1
229
230 unsigned current_personality;
231
232 # ifndef current_wordsize
233 unsigned current_wordsize;
234 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
235         PERSONALITY0_WORDSIZE,
236         PERSONALITY1_WORDSIZE,
237 # if SUPPORTED_PERSONALITIES > 2
238         PERSONALITY2_WORDSIZE,
239 # endif
240 };
241 # endif
242
243 # ifndef current_klongsize
244 unsigned current_klongsize;
245 static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
246         PERSONALITY0_KLONGSIZE,
247         PERSONALITY1_KLONGSIZE,
248 #  if SUPPORTED_PERSONALITIES > 2
249         PERSONALITY2_KLONGSIZE,
250 #  endif
251 };
252 # endif
253
254 void
255 set_personality(int personality)
256 {
257         nsyscalls = nsyscall_vec[personality];
258         sysent = sysent_vec[personality];
259
260         switch (personality) {
261         case 0:
262                 errnoent = errnoent0;
263                 nerrnos = nerrnos0;
264                 ioctlent = ioctlent0;
265                 nioctlents = nioctlents0;
266                 signalent = signalent0;
267                 nsignals = nsignals0;
268                 printers = &printers0;
269                 break;
270
271         case 1:
272                 errnoent = errnoent1;
273                 nerrnos = nerrnos1;
274                 ioctlent = ioctlent1;
275                 nioctlents = nioctlents1;
276                 signalent = signalent1;
277                 nsignals = nsignals1;
278                 printers = &printers1;
279                 break;
280
281 # if SUPPORTED_PERSONALITIES > 2
282         case 2:
283                 errnoent = errnoent2;
284                 nerrnos = nerrnos2;
285                 ioctlent = ioctlent2;
286                 nioctlents = nioctlents2;
287                 signalent = signalent2;
288                 nsignals = nsignals2;
289                 printers = &printers2;
290                 break;
291 # endif
292         }
293
294         current_personality = personality;
295 # ifndef current_wordsize
296         current_wordsize = personality_wordsize[personality];
297 # endif
298 # ifndef current_klongsize
299         current_klongsize = personality_klongsize[personality];
300 # endif
301 }
302
303 static void
304 update_personality(struct tcb *tcp, unsigned int personality)
305 {
306         static bool need_mpers_warning[] =
307                 { false, !HAVE_PERSONALITY_1_MPERS, !HAVE_PERSONALITY_2_MPERS };
308
309         if (personality == current_personality)
310                 return;
311         set_personality(personality);
312
313         if (personality == tcp->currpers)
314                 return;
315         tcp->currpers = personality;
316
317         if (!qflag) {
318                 error_msg("[ Process PID=%d runs in %s mode. ]",
319                           tcp->pid, personality_names[personality]);
320         }
321
322         if (need_mpers_warning[personality]) {
323                 error_msg("WARNING: Proper structure decoding for this "
324                           "personality is not supported, please consider "
325                           "building strace with mpers support enabled.");
326                 need_mpers_warning[personality] = false;
327         }
328 }
329 #endif
330
331 #ifdef SYS_socket_subcall
332 static void
333 decode_socket_subcall(struct tcb *tcp)
334 {
335         const int call = tcp->u_arg[0];
336
337         if (call < 1 || call >= SYS_socket_nsubcalls)
338                 return;
339
340         const kernel_ulong_t scno = SYS_socket_subcall + call;
341         const unsigned int nargs = sysent[scno].nargs;
342         uint64_t buf[nargs];
343
344         if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
345                 return;
346
347         tcp->scno = scno;
348         tcp->qual_flg = qual_flags(scno);
349         tcp->s_ent = &sysent[scno];
350
351         unsigned int i;
352         for (i = 0; i < nargs; ++i)
353                 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
354                                 ? ((uint32_t *) (void *) buf)[i] : buf[i];
355 }
356 #endif /* SYS_socket_subcall */
357
358 #ifdef SYS_ipc_subcall
359 static void
360 decode_ipc_subcall(struct tcb *tcp)
361 {
362         unsigned int call = tcp->u_arg[0];
363         const unsigned int version = call >> 16;
364
365         if (version) {
366 # if defined S390 || defined S390X
367                 return;
368 # else
369 #  ifdef SPARC64
370                 if (current_wordsize == 8)
371                         return;
372 #  endif
373                 set_tcb_priv_ulong(tcp, version);
374                 call &= 0xffff;
375 # endif
376         }
377
378         switch (call) {
379                 case  1: case  2: case  3: case  4:
380                 case 11: case 12: case 13: case 14:
381                 case 21: case 22: case 23: case 24:
382                         break;
383                 default:
384                         return;
385         }
386
387         tcp->scno = SYS_ipc_subcall + call;
388         tcp->qual_flg = qual_flags(tcp->scno);
389         tcp->s_ent = &sysent[tcp->scno];
390
391         const unsigned int n = tcp->s_ent->nargs;
392         unsigned int i;
393         for (i = 0; i < n; i++)
394                 tcp->u_arg[i] = tcp->u_arg[i + 1];
395 }
396 #endif /* SYS_ipc_subcall */
397
398 #ifdef SYS_syscall_subcall
399 static void
400 decode_syscall_subcall(struct tcb *tcp)
401 {
402         if (!scno_is_valid(tcp->u_arg[0]))
403                 return;
404         tcp->scno = tcp->u_arg[0];
405         tcp->qual_flg = qual_flags(tcp->scno);
406         tcp->s_ent = &sysent[tcp->scno];
407         memmove(&tcp->u_arg[0], &tcp->u_arg[1],
408                 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
409 # ifdef LINUX_MIPSO32
410         /*
411          * Fetching the last arg of 7-arg syscalls (fadvise64_64
412          * and sync_file_range) requires additional code,
413          * see linux/mips/get_syscall_args.c
414          */
415         if (tcp->s_ent->nargs == MAX_ARGS) {
416                 if (umoven(tcp,
417                            mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
418                            sizeof(tcp->u_arg[0]),
419                            &tcp->u_arg[MAX_ARGS - 1]) < 0)
420                 tcp->u_arg[MAX_ARGS - 1] = 0;
421         }
422 # endif /* LINUX_MIPSO32 */
423 }
424 #endif /* SYS_syscall_subcall */
425
426 static void
427 dumpio(struct tcb *tcp)
428 {
429         if (syserror(tcp))
430                 return;
431
432         int fd = tcp->u_arg[0];
433         if (fd < 0)
434                 return;
435
436         if (is_number_in_set(fd, read_set)) {
437                 switch (tcp->s_ent->sen) {
438                 case SEN_read:
439                 case SEN_pread:
440                 case SEN_recv:
441                 case SEN_recvfrom:
442                 case SEN_mq_timedreceive:
443                         dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
444                         return;
445                 case SEN_readv:
446                 case SEN_preadv:
447                 case SEN_preadv2:
448                         dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
449                                      tcp->u_rval);
450                         return;
451                 case SEN_recvmsg:
452                         dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
453                         return;
454                 case SEN_recvmmsg:
455                         dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
456                         return;
457                 }
458         }
459         if (is_number_in_set(fd, write_set)) {
460                 switch (tcp->s_ent->sen) {
461                 case SEN_write:
462                 case SEN_pwrite:
463                 case SEN_send:
464                 case SEN_sendto:
465                 case SEN_mq_timedsend:
466                         dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
467                         break;
468                 case SEN_writev:
469                 case SEN_pwritev:
470                 case SEN_pwritev2:
471                 case SEN_vmsplice:
472                         dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
473                         break;
474                 case SEN_sendmsg:
475                         dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
476                         break;
477                 case SEN_sendmmsg:
478                         dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
479                         break;
480                 }
481         }
482 }
483
484 /*
485  * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
486  * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
487  */
488 static kernel_ulong_t
489 shuffle_scno(kernel_ulong_t scno)
490 {
491 #ifdef ARM_FIRST_SHUFFLED_SYSCALL       /* So far only 32-bit ARM needs this */
492         if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
493                 return scno;
494
495         /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
496         if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
497                 return 0x000ffff0;
498         if (scno == 0x000ffff0)
499                 return ARM_FIRST_SHUFFLED_SYSCALL;
500
501 # define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
502         /*
503          * Is it ARM specific syscall?
504          * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
505          * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
506          */
507         if (scno >= 0x000f0000 &&
508             scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
509                 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
510         }
511         if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
512                 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
513         }
514 #endif /* ARM_FIRST_SHUFFLED_SYSCALL */
515
516         return scno;
517 }
518
519 const char *
520 err_name(unsigned long err)
521 {
522         if ((err < nerrnos) && errnoent[err])
523                 return errnoent[err];
524
525         return NULL;
526 }
527
528 static long get_regs(struct tcb *);
529 static int get_syscall_args(struct tcb *);
530 static int get_syscall_result(struct tcb *);
531 static int arch_get_scno(struct tcb *tcp);
532 static int arch_set_scno(struct tcb *, kernel_ulong_t);
533 static void get_error(struct tcb *, const bool);
534 static int arch_set_error(struct tcb *);
535 static int arch_set_success(struct tcb *);
536
537 struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
538
539 static struct inject_opts *
540 tcb_inject_opts(struct tcb *tcp)
541 {
542         return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
543                ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
544 }
545
546
547 static long
548 tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
549 {
550         if (!tcp->inject_vec[current_personality]) {
551                 tcp->inject_vec[current_personality] =
552                         xcalloc(nsyscalls, sizeof(**inject_vec));
553                 memcpy(tcp->inject_vec[current_personality],
554                        inject_vec[current_personality],
555                        nsyscalls * sizeof(**inject_vec));
556         }
557
558         struct inject_opts *opts = tcb_inject_opts(tcp);
559
560         if (!opts || opts->first == 0)
561                 return 0;
562
563         --opts->first;
564
565         if (opts->first != 0)
566                 return 0;
567
568         opts->first = opts->step;
569
570         if (opts->data.flags & INJECT_F_SIGNAL)
571                 *signo = opts->data.signo;
572         if (opts->data.flags & INJECT_F_RETVAL && !arch_set_scno(tcp, -1))
573                 tcp->flags |= TCB_TAMPERED;
574
575         return 0;
576 }
577
578 static long
579 tamper_with_syscall_exiting(struct tcb *tcp)
580 {
581         struct inject_opts *opts = tcb_inject_opts(tcp);
582
583         if (!opts)
584                 return 0;
585
586         if (opts->data.rval >= 0) {
587                 kernel_long_t u_rval = tcp->u_rval;
588
589                 tcp->u_rval = opts->data.rval;
590                 if (arch_set_success(tcp)) {
591                         tcp->u_rval = u_rval;
592                 } else {
593                         tcp->u_error = 0;
594                 }
595         } else {
596                 unsigned long new_error = -opts->data.rval;
597
598                 if (new_error != tcp->u_error && new_error <= MAX_ERRNO_VALUE) {
599                         unsigned long u_error = tcp->u_error;
600
601                         tcp->u_error = new_error;
602                         if (arch_set_error(tcp)) {
603                                 tcp->u_error = u_error;
604                         }
605                 }
606         }
607
608         return 0;
609 }
610
611 /*
612  * Returns:
613  * 0: "ignore this ptrace stop", bail out silently.
614  * 1: ok, decoded; call
615  *    syscall_entering_finish(tcp, syscall_entering_trace(tcp, ...)).
616  * other: error; call syscall_entering_finish(tcp, res), where res is the value
617  *    returned.
618  */
619 int
620 syscall_entering_decode(struct tcb *tcp)
621 {
622         int res = get_scno(tcp);
623         if (res == 0)
624                 return res;
625         int scno_good = res;
626         if (res != 1 || (res = get_syscall_args(tcp)) != 1) {
627                 printleader(tcp);
628                 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
629                 /*
630                  * " <unavailable>" will be added later by the code which
631                  * detects ptrace errors.
632                  */
633                 return res;
634         }
635
636 #if defined SYS_ipc_subcall     \
637  || defined SYS_socket_subcall  \
638  || defined SYS_syscall_subcall
639         for (;;) {
640                 switch (tcp->s_ent->sen) {
641 # ifdef SYS_ipc_subcall
642                 case SEN_ipc:
643                         decode_ipc_subcall(tcp);
644                         break;
645 # endif
646 # ifdef SYS_socket_subcall
647                 case SEN_socketcall:
648                         decode_socket_subcall(tcp);
649                         break;
650 # endif
651 # ifdef SYS_syscall_subcall
652                 case SEN_syscall:
653                         decode_syscall_subcall(tcp);
654                         if (tcp->s_ent->sen != SEN_syscall)
655                                 continue;
656                         break;
657 # endif
658                 }
659                 break;
660         }
661 #endif
662
663         return 1;
664 }
665
666 int
667 syscall_entering_trace(struct tcb *tcp, unsigned int *sig)
668 {
669         /* Restrain from fault injection while the trace executes strace code. */
670         if (hide_log(tcp)) {
671                 tcp->qual_flg &= ~QUAL_INJECT;
672         }
673
674         switch (tcp->s_ent->sen) {
675                 case SEN_execve:
676                 case SEN_execveat:
677 #if defined SPARC || defined SPARC64
678                 case SEN_execv:
679 #endif
680                         tcp->flags &= ~TCB_HIDE_LOG;
681                         break;
682         }
683
684         if (!traced(tcp) || (tracing_paths && !pathtrace_match(tcp))) {
685                 tcp->flags |= TCB_FILTERED;
686                 return 0;
687         }
688
689         tcp->flags &= ~TCB_FILTERED;
690
691         if (hide_log(tcp)) {
692                 return 0;
693         }
694
695         if (inject(tcp))
696                 tamper_with_syscall_entering(tcp, sig);
697
698         if (cflag == CFLAG_ONLY_STATS) {
699                 return 0;
700         }
701
702 #ifdef USE_LIBUNWIND
703         if (stack_trace_enabled) {
704                 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
705                         unwind_capture_stacktrace(tcp);
706         }
707 #endif
708
709         printleader(tcp);
710         tprintf("%s(", tcp->s_ent->sys_name);
711         int res = raw(tcp) ? printargs(tcp) : tcp->s_ent->sys_func(tcp);
712         fflush(tcp->outf);
713         return res;
714 }
715
716 void
717 syscall_entering_finish(struct tcb *tcp, int res)
718 {
719         tcp->flags |= TCB_INSYSCALL;
720         tcp->sys_func_rval = res;
721         /* Measure the entrance time as late as possible to avoid errors. */
722         if ((Tflag || cflag) && !filtered(tcp))
723                 gettimeofday(&tcp->etime, NULL);
724 }
725
726 /* Returns:
727  * 0: "bail out".
728  * 1: ok.
729  * -1: error in one of ptrace ops.
730  *
731  * If not 0, call syscall_exiting_trace(tcp, res), where res is the return
732  *    value. Anyway, call syscall_exiting_finish(tcp) then.
733  */
734 int
735 syscall_exiting_decode(struct tcb *tcp, struct timeval *ptv)
736 {
737         /* Measure the exit time as early as possible to avoid errors. */
738         if ((Tflag || cflag) && !(filtered(tcp) || hide_log(tcp)))
739                 gettimeofday(ptv, NULL);
740
741 #ifdef USE_LIBUNWIND
742         if (stack_trace_enabled) {
743                 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
744                         unwind_cache_invalidate(tcp);
745         }
746 #endif
747
748         if (filtered(tcp) || hide_log(tcp))
749                 return 0;
750
751 #if SUPPORTED_PERSONALITIES > 1
752         update_personality(tcp, tcp->currpers);
753 #endif
754
755         return get_syscall_result(tcp);
756 }
757
758 int
759 syscall_exiting_trace(struct tcb *tcp, struct timeval tv, int res)
760 {
761         if (syserror(tcp) && syscall_tampered(tcp))
762                 tamper_with_syscall_exiting(tcp);
763
764         if (cflag) {
765                 count_syscall(tcp, &tv);
766                 if (cflag == CFLAG_ONLY_STATS) {
767                         return 0;
768                 }
769         }
770
771         /* If not in -ff mode, and printing_tcp != tcp,
772          * then the log currently does not end with output
773          * of _our syscall entry_, but with something else.
774          * We need to say which syscall's return is this.
775          *
776          * Forced reprinting via TCB_REPRINT is used only by
777          * "strace -ff -oLOG test/threaded_execve" corner case.
778          * It's the only case when -ff mode needs reprinting.
779          */
780         if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
781                 tcp->flags &= ~TCB_REPRINT;
782                 printleader(tcp);
783                 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
784         }
785         printing_tcp = tcp;
786
787         tcp->s_prev_ent = NULL;
788         if (res != 1) {
789                 /* There was error in one of prior ptrace ops */
790                 tprints(") ");
791                 tabto();
792                 tprints("= ? <unavailable>\n");
793                 line_ended();
794                 return res;
795         }
796         tcp->s_prev_ent = tcp->s_ent;
797
798         int sys_res = 0;
799         if (raw(tcp)) {
800                 /* sys_res = printargs(tcp); - but it's nop on sysexit */
801         } else {
802         /* FIXME: not_failing_only (IOW, option -z) is broken:
803          * failure of syscall is known only after syscall return.
804          * Thus we end up with something like this on, say, ENOENT:
805          *     open("does_not_exist", O_RDONLY <unfinished ...>
806          *     {next syscall decode}
807          * whereas the intended result is that open(...) line
808          * is not shown at all.
809          */
810                 if (not_failing_only && tcp->u_error)
811                         return 0;       /* ignore failed syscalls */
812                 if (tcp->sys_func_rval & RVAL_DECODED)
813                         sys_res = tcp->sys_func_rval;
814                 else
815                         sys_res = tcp->s_ent->sys_func(tcp);
816         }
817
818         tprints(") ");
819         tabto();
820         unsigned long u_error = tcp->u_error;
821         kernel_long_t u_rval;
822
823         if (raw(tcp)) {
824                 if (u_error) {
825                         tprintf("= -1 (errno %lu)", u_error);
826                 } else {
827                         tprintf("= %#" PRI_klx, tcp->u_rval);
828                 }
829                 if (syscall_tampered(tcp))
830                         tprints(" (INJECTED)");
831         } else if (!(sys_res & RVAL_NONE) && u_error) {
832                 const char *u_error_str;
833
834                 switch (u_error) {
835                 /* Blocked signals do not interrupt any syscalls.
836                  * In this case syscalls don't return ERESTARTfoo codes.
837                  *
838                  * Deadly signals set to SIG_DFL interrupt syscalls
839                  * and kill the process regardless of which of the codes below
840                  * is returned by the interrupted syscall.
841                  * In some cases, kernel forces a kernel-generated deadly
842                  * signal to be unblocked and set to SIG_DFL (and thus cause
843                  * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
844                  * or SIGILL. (The alternative is to leave process spinning
845                  * forever on the faulty instruction - not useful).
846                  *
847                  * SIG_IGNed signals and non-deadly signals set to SIG_DFL
848                  * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
849                  * but kernel will always restart them.
850                  */
851                 case ERESTARTSYS:
852                         /* Most common type of signal-interrupted syscall exit code.
853                          * The system call will be restarted with the same arguments
854                          * if SA_RESTART is set; otherwise, it will fail with EINTR.
855                          */
856                         tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
857                         break;
858                 case ERESTARTNOINTR:
859                         /* Rare. For example, fork() returns this if interrupted.
860                          * SA_RESTART is ignored (assumed set): the restart is unconditional.
861                          */
862                         tprints("= ? ERESTARTNOINTR (To be restarted)");
863                         break;
864                 case ERESTARTNOHAND:
865                         /* pause(), rt_sigsuspend() etc use this code.
866                          * SA_RESTART is ignored (assumed not set):
867                          * syscall won't restart (will return EINTR instead)
868                          * even after signal with SA_RESTART set. However,
869                          * after SIG_IGN or SIG_DFL signal it will restart
870                          * (thus the name "restart only if has no handler").
871                          */
872                         tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
873                         break;
874                 case ERESTART_RESTARTBLOCK:
875                         /* Syscalls like nanosleep(), poll() which can't be
876                          * restarted with their original arguments use this
877                          * code. Kernel will execute restart_syscall() instead,
878                          * which changes arguments before restarting syscall.
879                          * SA_RESTART is ignored (assumed not set) similarly
880                          * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
881                          * since restart data is saved in "restart block"
882                          * in task struct, and if signal handler uses a syscall
883                          * which in turn saves another such restart block,
884                          * old data is lost and restart becomes impossible)
885                          */
886                         tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
887                         break;
888                 default:
889                         u_rval = sys_res & RVAL_PRINT_ERR_VAL ?
890                                  tcp->u_rval : -1;
891                         u_error_str = err_name(u_error);
892                         if (u_error_str)
893                                 tprintf("= %" PRI_kld " %s (%s)",
894                                         u_rval, u_error_str, strerror(u_error));
895                         else
896                                 tprintf("= %" PRI_kld " %lu (%s)",
897                                         u_rval, u_error, strerror(u_error));
898                         break;
899                 }
900                 if (syscall_tampered(tcp))
901                         tprints(" (INJECTED)");
902                 if ((sys_res & RVAL_STR) && tcp->auxstr)
903                         tprintf(" (%s)", tcp->auxstr);
904         } else {
905                 if (sys_res & RVAL_NONE)
906                         tprints("= ?");
907                 else {
908                         switch (sys_res & RVAL_MASK) {
909                         case RVAL_HEX:
910 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
911                                 if (current_wordsize < sizeof(tcp->u_rval)) {
912                                         tprintf("= %#x",
913                                                 (unsigned int) tcp->u_rval);
914                                 } else
915 #endif
916                                 {
917                                         tprintf("= %#" PRI_klx, tcp->u_rval);
918                                 }
919                                 break;
920                         case RVAL_OCTAL:
921                                 tprints("= ");
922                                 print_numeric_long_umask(tcp->u_rval);
923                                 break;
924                         case RVAL_UDECIMAL:
925 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
926                                 if (current_wordsize < sizeof(tcp->u_rval)) {
927                                         tprintf("= %u",
928                                                 (unsigned int) tcp->u_rval);
929                                 } else
930 #endif
931                                 {
932                                         tprintf("= %" PRI_klu, tcp->u_rval);
933                                 }
934                                 break;
935                         case RVAL_DECIMAL:
936                                 tprintf("= %" PRI_kld, tcp->u_rval);
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                 tv_sub(&tv, &tv, &tcp->etime);
957                 tprintf(" <%ld.%06ld>",
958                         (long) tv.tv_sec, (long) tv.tv_usec);
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);
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 #ifdef HAVE_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         return get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1153 # endif /* !HAVE_GETREGS_OLD */
1154
1155 #else /* !ptrace_getregset_or_getregs */
1156
1157 # warning get_regs is not implemented for this architecture yet
1158         return 0;
1159
1160 #endif /* !ptrace_getregset_or_getregs */
1161 }
1162
1163 #ifdef ptrace_setregset_or_setregs
1164 static int
1165 set_regs(pid_t pid)
1166 {
1167         return ptrace_setregset_or_setregs(pid);
1168 }
1169 #endif /* ptrace_setregset_or_setregs */
1170
1171 struct sysent_buf {
1172         struct tcb *tcp;
1173         struct_sysent ent;
1174         char buf[sizeof("syscall_%lu") + sizeof(kernel_ulong_t) * 3];
1175 };
1176
1177 static void
1178 free_sysent_buf(void *ptr)
1179 {
1180         struct sysent_buf *s = ptr;
1181         s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1182         free(ptr);
1183 }
1184
1185 /*
1186  * Returns:
1187  * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
1188  *    out silently" code.
1189  * 1: ok, continue in syscall_entering_decode().
1190  * other: error, syscall_entering_decode() should print error indicator
1191  *    ("????" etc) and return an appropriate code.
1192  */
1193 int
1194 get_scno(struct tcb *tcp)
1195 {
1196         if (get_regs(tcp) < 0)
1197                 return -1;
1198
1199         int rc = arch_get_scno(tcp);
1200         if (rc != 1)
1201                 return rc;
1202
1203         if (scno_is_valid(tcp->scno)) {
1204                 tcp->s_ent = &sysent[tcp->scno];
1205                 tcp->qual_flg = qual_flags(tcp->scno);
1206         } else {
1207                 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1208
1209                 s->tcp = tcp;
1210                 s->ent.nargs = MAX_ARGS;
1211                 s->ent.sen = SEN_printargs;
1212                 s->ent.sys_func = printargs;
1213                 s->ent.sys_name = s->buf;
1214                 xsprintf(s->buf, "syscall_%" PRI_klu, shuffle_scno(tcp->scno));
1215
1216                 tcp->s_ent = &s->ent;
1217                 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1218
1219                 set_tcb_priv_data(tcp, s, free_sysent_buf);
1220
1221                 debug_msg("pid %d invalid syscall %" PRI_kld,
1222                           tcp->pid, tcp->scno);
1223         }
1224         return 1;
1225 }
1226
1227 #ifdef ptrace_getregset_or_getregs
1228 # define get_syscall_result_regs get_regs
1229 #else
1230 static int get_syscall_result_regs(struct tcb *);
1231 #endif
1232
1233 /* Returns:
1234  * 1: ok, continue in syscall_exiting_trace().
1235  * -1: error, syscall_exiting_trace() should print error indicator
1236  *    ("????" etc) and bail out.
1237  */
1238 static int
1239 get_syscall_result(struct tcb *tcp)
1240 {
1241         if (get_syscall_result_regs(tcp) < 0)
1242                 return -1;
1243         tcp->u_error = 0;
1244         get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1245
1246         return 1;
1247 }
1248
1249 #include "get_scno.c"
1250 #include "set_scno.c"
1251 #include "get_syscall_args.c"
1252 #ifndef ptrace_getregset_or_getregs
1253 # include "get_syscall_result.c"
1254 #endif
1255 #include "get_error.c"
1256 #include "set_error.c"
1257 #ifdef HAVE_GETREGS_OLD
1258 # include "getregs_old.c"
1259 #endif
1260
1261 const char *
1262 syscall_name(kernel_ulong_t scno)
1263 {
1264 #if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
1265         if (current_personality == X32_PERSONALITY_NUMBER)
1266                 scno &= ~__X32_SYSCALL_BIT;
1267 #endif
1268         return scno_is_valid(scno) ? sysent[scno].sys_name : NULL;
1269 }