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>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/param.h>
37 /* for struct iovec */
44 # undef PTRACE_GETREGS
45 # define PTRACE_GETREGS PTRACE_GETREGS64
46 # undef PTRACE_SETREGS
47 # define PTRACE_SETREGS PTRACE_SETREGS64
51 # include <asm/psrcompat.h>
61 # define NT_PRSTATUS 1
65 # warning: NSIG is not defined, using 32
71 /* Define these shorthand notations to simplify the syscallent files. */
75 #define TN TRACE_NETWORK
76 #define TP TRACE_PROCESS
77 #define TS TRACE_SIGNAL
78 #define TM TRACE_MEMORY
79 #define NF SYSCALL_NEVER_FAILS
81 #define SI STACKTRACE_INVALIDATE_CACHE
82 #define SE STACKTRACE_CAPTURE_ON_ENTER
84 const struct_sysent sysent0[] = {
85 #include "syscallent.h"
88 #if SUPPORTED_PERSONALITIES > 1
89 static const struct_sysent sysent1[] = {
90 # include "syscallent1.h"
94 #if SUPPORTED_PERSONALITIES > 2
95 static const struct_sysent sysent2[] = {
96 # include "syscallent2.h"
100 /* Now undef them since short defines cause wicked namespace pollution. */
114 * `ioctlent[012].h' files are automatically generated by the auxiliary
115 * program `ioctlsort', such that the list is sorted by the `code' field.
116 * This has the side-effect of resolving the _IO.. macros into
117 * plain integers, eliminating the need to include here everything
121 const char *const errnoent0[] = {
122 #include "errnoent.h"
124 const char *const signalent0[] = {
125 #include "signalent.h"
127 const struct_ioctlent ioctlent0[] = {
128 #include "ioctlent0.h"
131 #if SUPPORTED_PERSONALITIES > 1
132 static const char *const errnoent1[] = {
133 # include "errnoent1.h"
135 static const char *const signalent1[] = {
136 # include "signalent1.h"
138 static const struct_ioctlent ioctlent1[] = {
139 # include "ioctlent1.h"
143 #if SUPPORTED_PERSONALITIES > 2
144 static const char *const errnoent2[] = {
145 # include "errnoent2.h"
147 static const char *const signalent2[] = {
148 # include "signalent2.h"
150 static const struct_ioctlent ioctlent2[] = {
151 # include "ioctlent2.h"
156 nsyscalls0 = ARRAY_SIZE(sysent0)
157 #if SUPPORTED_PERSONALITIES > 1
158 , nsyscalls1 = ARRAY_SIZE(sysent1)
159 # if SUPPORTED_PERSONALITIES > 2
160 , nsyscalls2 = ARRAY_SIZE(sysent2)
166 nerrnos0 = ARRAY_SIZE(errnoent0)
167 #if SUPPORTED_PERSONALITIES > 1
168 , nerrnos1 = ARRAY_SIZE(errnoent1)
169 # if SUPPORTED_PERSONALITIES > 2
170 , nerrnos2 = ARRAY_SIZE(errnoent2)
176 nsignals0 = ARRAY_SIZE(signalent0)
177 #if SUPPORTED_PERSONALITIES > 1
178 , nsignals1 = ARRAY_SIZE(signalent1)
179 # if SUPPORTED_PERSONALITIES > 2
180 , nsignals2 = ARRAY_SIZE(signalent2)
186 nioctlents0 = ARRAY_SIZE(ioctlent0)
187 #if SUPPORTED_PERSONALITIES > 1
188 , nioctlents1 = ARRAY_SIZE(ioctlent1)
189 # if SUPPORTED_PERSONALITIES > 2
190 , nioctlents2 = ARRAY_SIZE(ioctlent2)
195 #if SUPPORTED_PERSONALITIES > 1
196 const struct_sysent *sysent = sysent0;
197 const char *const *errnoent = errnoent0;
198 const char *const *signalent = signalent0;
199 const struct_ioctlent *ioctlent = ioctlent0;
201 unsigned nsyscalls = nsyscalls0;
202 unsigned nerrnos = nerrnos0;
203 unsigned nsignals = nsignals0;
204 unsigned nioctlents = nioctlents0;
207 qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
209 static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
211 #if SUPPORTED_PERSONALITIES > 1
214 #if SUPPORTED_PERSONALITIES > 2
218 static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
220 #if SUPPORTED_PERSONALITIES > 1
223 #if SUPPORTED_PERSONALITIES > 2
229 MAX_NSYSCALLS1 = (nsyscalls0
230 #if SUPPORTED_PERSONALITIES > 1
231 > nsyscalls1 ? nsyscalls0 : nsyscalls1
234 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
235 #if SUPPORTED_PERSONALITIES > 2
236 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
239 MAX_NSYSCALLS = MAX_NSYSCALLS2,
240 /* We are ready for arches with up to 255 signals,
241 * even though the largest known signo is on MIPS and it is 128.
242 * The number of existing syscalls on all arches is
243 * larger that 255 anyway, so it is just a pedantic matter.
245 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
248 #if SUPPORTED_PERSONALITIES > 1
249 unsigned current_personality;
251 # ifndef current_wordsize
252 unsigned current_wordsize;
253 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
254 PERSONALITY0_WORDSIZE,
255 PERSONALITY1_WORDSIZE,
256 # if SUPPORTED_PERSONALITIES > 2
257 PERSONALITY2_WORDSIZE,
263 set_personality(int personality)
265 nsyscalls = nsyscall_vec[personality];
266 sysent = sysent_vec[personality];
268 switch (personality) {
270 errnoent = errnoent0;
272 ioctlent = ioctlent0;
273 nioctlents = nioctlents0;
274 signalent = signalent0;
275 nsignals = nsignals0;
279 errnoent = errnoent1;
281 ioctlent = ioctlent1;
282 nioctlents = nioctlents1;
283 signalent = signalent1;
284 nsignals = nsignals1;
287 # if SUPPORTED_PERSONALITIES > 2
289 errnoent = errnoent2;
291 ioctlent = ioctlent2;
292 nioctlents = nioctlents2;
293 signalent = signalent2;
294 nsignals = nsignals2;
299 current_personality = personality;
300 # ifndef current_wordsize
301 current_wordsize = personality_wordsize[personality];
306 update_personality(struct tcb *tcp, unsigned int personality)
308 if (personality == current_personality)
310 set_personality(personality);
312 if (personality == tcp->currpers)
314 tcp->currpers = personality;
316 # undef PERSONALITY_NAMES
317 # if defined POWERPC64
318 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
319 # elif defined X86_64
320 # define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
322 # define PERSONALITY_NAMES {"x32", "32 bit"}
323 # elif defined AARCH64
324 # define PERSONALITY_NAMES {"32-bit", "AArch64"}
326 # define PERSONALITY_NAMES {"64-bit", "32-bit"}
328 # ifdef PERSONALITY_NAMES
330 static const char *const names[] = PERSONALITY_NAMES;
331 error_msg("[ Process PID=%d runs in %s mode. ]",
332 tcp->pid, names[personality]);
338 static int qual_syscall(), qual_signal(), qual_desc();
340 static const struct qual_options {
341 unsigned int bitflag;
342 const char *option_name;
343 int (*qualify)(const char *, int, int);
344 const char *argument_name;
346 { QUAL_TRACE, "trace", qual_syscall, "system call" },
347 { QUAL_TRACE, "t", qual_syscall, "system call" },
348 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
349 { QUAL_ABBREV, "a", qual_syscall, "system call" },
350 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
351 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
352 { QUAL_RAW, "raw", qual_syscall, "system call" },
353 { QUAL_RAW, "x", qual_syscall, "system call" },
354 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
355 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
356 { QUAL_SIGNAL, "s", qual_signal, "signal" },
357 { QUAL_READ, "read", qual_desc, "descriptor" },
358 { QUAL_READ, "reads", qual_desc, "descriptor" },
359 { QUAL_READ, "r", qual_desc, "descriptor" },
360 { QUAL_WRITE, "write", qual_desc, "descriptor" },
361 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
362 { QUAL_WRITE, "w", qual_desc, "descriptor" },
363 { 0, NULL, NULL, NULL },
367 reallocate_qual(const unsigned int n)
371 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
372 qp = qual_vec[p] = xreallocarray(qual_vec[p], n,
374 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
380 qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
385 reallocate_qual(n + 1);
387 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
388 if (pers == p || pers < 0) {
390 qual_vec[p][n] &= ~bitflag;
392 qual_vec[p][n] |= bitflag;
398 qual_syscall(const char *s, const unsigned int bitflag, const int not)
404 if (*s >= '0' && *s <= '9') {
405 i = string_to_uint(s);
406 if (i >= MAX_NSYSCALLS)
408 qualify_one(i, bitflag, not, -1);
412 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
413 for (i = 0; i < nsyscall_vec[p]; i++) {
414 if (sysent_vec[p][i].sys_name
415 && strcmp(s, sysent_vec[p][i].sys_name) == 0
417 qualify_one(i, bitflag, not, p);
427 qual_signal(const char *s, const unsigned int bitflag, const int not)
431 if (*s >= '0' && *s <= '9') {
432 int signo = string_to_uint(s);
433 if (signo < 0 || signo > 255)
435 qualify_one(signo, bitflag, not, -1);
438 if (strncasecmp(s, "SIG", 3) == 0)
440 for (i = 0; i <= NSIG; i++) {
441 if (strcasecmp(s, signame(i) + 3) == 0) {
442 qualify_one(i, bitflag, not, -1);
450 qual_desc(const char *s, const unsigned int bitflag, const int not)
452 if (*s >= '0' && *s <= '9') {
453 int desc = string_to_uint(s);
454 if (desc < 0 || desc > 0x7fff) /* paranoia */
456 qualify_one(desc, bitflag, not, -1);
463 lookup_class(const char *s)
465 if (strcmp(s, "file") == 0)
467 if (strcmp(s, "ipc") == 0)
469 if (strcmp(s, "network") == 0)
470 return TRACE_NETWORK;
471 if (strcmp(s, "process") == 0)
472 return TRACE_PROCESS;
473 if (strcmp(s, "signal") == 0)
475 if (strcmp(s, "desc") == 0)
477 if (strcmp(s, "memory") == 0)
483 qualify(const char *s)
485 const struct qual_options *opt;
492 reallocate_qual(MIN_QUALS);
494 opt = &qual_options[0];
495 for (i = 0; (p = qual_options[i].option_name); i++) {
496 unsigned int len = strlen(p);
497 if (strncmp(s, p, len) == 0 && s[len] == '=') {
498 opt = &qual_options[i];
508 if (strcmp(s, "none") == 0) {
512 if (strcmp(s, "all") == 0) {
513 for (i = 0; i < num_quals; i++) {
514 qualify_one(i, opt->bitflag, not, -1);
518 for (i = 0; i < num_quals; i++) {
519 qualify_one(i, opt->bitflag, !not, -1);
522 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
524 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
526 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
527 for (i = 0; i < nsyscall_vec[pers]; i++)
528 if (sysent_vec[pers][i].sys_flags & n)
529 qualify_one(i, opt->bitflag, not, pers);
533 if (opt->qualify(p, opt->bitflag, not)) {
534 error_msg_and_die("invalid %s '%s'",
535 opt->argument_name, p);
542 #ifdef SYS_socket_subcall
544 decode_socket_subcall(struct tcb *tcp)
549 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
552 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
553 tcp->qual_flg = qual_flags[tcp->scno];
554 tcp->s_ent = &sysent[tcp->scno];
555 addr = tcp->u_arg[1];
556 n = tcp->s_ent->nargs;
557 if (sizeof(tcp->u_arg[0]) == current_wordsize) {
558 memset(tcp->u_arg, 0, n * sizeof(tcp->u_arg[0]));
559 (void) umoven(tcp, addr, n * sizeof(tcp->u_arg[0]), tcp->u_arg);
561 unsigned int args[n];
564 memset(args, 0, sizeof(args));
565 (void) umove(tcp, addr, &args);
566 for (i = 0; i < n; ++i)
567 tcp->u_arg[i] = args[i];
572 #ifdef SYS_ipc_subcall
574 decode_ipc_subcall(struct tcb *tcp)
578 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
581 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
582 tcp->qual_flg = qual_flags[tcp->scno];
583 tcp->s_ent = &sysent[tcp->scno];
584 n = tcp->s_ent->nargs;
585 for (i = 0; i < n; i++)
586 tcp->u_arg[i] = tcp->u_arg[i + 1];
592 decode_mips_subcall(struct tcb *tcp)
594 if (!SCNO_IS_VALID(tcp->u_arg[0]))
596 tcp->scno = tcp->u_arg[0];
597 tcp->qual_flg = qual_flags[tcp->scno];
598 tcp->s_ent = &sysent[tcp->scno];
599 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
600 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
602 * Fetching the last arg of 7-arg syscalls (fadvise64_64
603 * and sync_file_range) would require additional code,
604 * see linux/mips/get_syscall_args.c
610 return printargs(tcp);
615 printargs(struct tcb *tcp)
619 int n = tcp->s_ent->nargs;
620 for (i = 0; i < n; i++)
621 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
627 printargs_lu(struct tcb *tcp)
631 int n = tcp->s_ent->nargs;
632 for (i = 0; i < n; i++)
633 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
639 printargs_ld(struct tcb *tcp)
643 int n = tcp->s_ent->nargs;
644 for (i = 0; i < n; i++)
645 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
651 dumpio(struct tcb *tcp)
657 if ((unsigned long) tcp->u_arg[0] >= num_quals)
659 func = tcp->s_ent->sys_func;
660 if (func == printargs)
662 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
663 if (func == sys_read ||
666 func == sys_recvfrom) {
667 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
669 } else if (func == sys_readv) {
670 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
673 } else if (func == sys_recvmsg) {
674 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
676 } else if (func == sys_recvmmsg) {
677 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
682 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
683 if (func == sys_write ||
684 func == sys_pwrite ||
687 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
688 else if (func == sys_writev)
689 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
691 else if (func == sys_sendmsg)
692 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
693 else if (func == sys_sendmmsg)
694 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
700 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
701 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
703 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
705 shuffle_scno(unsigned long scno)
707 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
710 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
711 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
713 if (scno == 0x000ffff0)
714 return ARM_FIRST_SHUFFLED_SYSCALL;
716 #define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
718 * Is it ARM specific syscall?
719 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
720 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
722 if (scno >= 0x000f0000 &&
723 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
724 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
726 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
727 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
733 # define shuffle_scno(scno) ((long)(scno))
737 undefined_scno_name(struct tcb *tcp)
739 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
741 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
745 static long get_regs_error;
753 static int get_syscall_args(struct tcb *);
754 static int get_syscall_result(struct tcb *);
757 trace_syscall_entering(struct tcb *tcp)
761 scno_good = res = get_scno(tcp);
765 res = get_syscall_args(tcp);
770 tprints("????" /* anti-trigraph gap */ "(");
771 else if (tcp->qual_flg & UNDEFINED_SCNO)
772 tprintf("%s(", undefined_scno_name(tcp));
774 tprintf("%s(", tcp->s_ent->sys_name);
776 * " <unavailable>" will be added later by the code which
777 * detects ptrace errors.
783 if (sys_syscall == tcp->s_ent->sys_func)
784 decode_mips_subcall(tcp);
787 if ( sys_execve == tcp->s_ent->sys_func
788 # if defined(SPARC) || defined(SPARC64)
789 || sys_execv == tcp->s_ent->sys_func
792 hide_log_until_execve = 0;
795 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
797 # ifdef SYS_socket_subcall
798 if (tcp->s_ent->sys_func == sys_socketcall) {
799 decode_socket_subcall(tcp);
803 # ifdef SYS_ipc_subcall
804 if (tcp->s_ent->sys_func == sys_ipc) {
805 decode_ipc_subcall(tcp);
813 if (!(tcp->qual_flg & QUAL_TRACE)
814 || (tracing_paths && !pathtrace_match(tcp))
816 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
820 tcp->flags &= ~TCB_FILTERED;
822 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
828 if (stack_trace_enabled) {
829 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
830 unwind_capture_stacktrace(tcp);
835 if (tcp->qual_flg & UNDEFINED_SCNO)
836 tprintf("%s(", undefined_scno_name(tcp));
838 tprintf("%s(", tcp->s_ent->sys_name);
839 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
840 res = printargs(tcp);
842 res = tcp->s_ent->sys_func(tcp);
846 tcp->flags |= TCB_INSYSCALL;
847 /* Measure the entrance time as late as possible to avoid errors. */
849 gettimeofday(&tcp->etime, NULL);
854 trace_syscall_exiting(struct tcb *tcp)
861 /* Measure the exit time as early as possible to avoid errors. */
863 gettimeofday(&tv, NULL);
866 if (stack_trace_enabled) {
867 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
868 unwind_cache_invalidate(tcp);
872 #if SUPPORTED_PERSONALITIES > 1
873 update_personality(tcp, tcp->currpers);
875 res = (get_regs_error ? -1 : get_syscall_result(tcp));
877 if (filtered(tcp) || hide_log_until_execve)
882 count_syscall(tcp, &tv);
883 if (cflag == CFLAG_ONLY_STATS) {
888 /* If not in -ff mode, and printing_tcp != tcp,
889 * then the log currently does not end with output
890 * of _our syscall entry_, but with something else.
891 * We need to say which syscall's return is this.
893 * Forced reprinting via TCB_REPRINT is used only by
894 * "strace -ff -oLOG test/threaded_execve" corner case.
895 * It's the only case when -ff mode needs reprinting.
897 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
898 tcp->flags &= ~TCB_REPRINT;
900 if (tcp->qual_flg & UNDEFINED_SCNO)
901 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
903 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
907 tcp->s_prev_ent = NULL;
909 /* There was error in one of prior ptrace ops */
912 tprints("= ? <unavailable>\n");
914 tcp->flags &= ~TCB_INSYSCALL;
917 tcp->s_prev_ent = tcp->s_ent;
920 if (tcp->qual_flg & QUAL_RAW) {
921 /* sys_res = printargs(tcp); - but it's nop on sysexit */
923 /* FIXME: not_failing_only (IOW, option -z) is broken:
924 * failure of syscall is known only after syscall return.
925 * Thus we end up with something like this on, say, ENOENT:
926 * open("doesnt_exist", O_RDONLY <unfinished ...>
927 * {next syscall decode}
928 * whereas the intended result is that open(...) line
929 * is not shown at all.
931 if (not_failing_only && tcp->u_error)
932 goto ret; /* ignore failed syscalls */
933 sys_res = tcp->s_ent->sys_func(tcp);
938 u_error = tcp->u_error;
939 if (tcp->qual_flg & QUAL_RAW) {
941 tprintf("= -1 (errno %ld)", u_error);
943 tprintf("= %#lx", tcp->u_rval);
945 else if (!(sys_res & RVAL_NONE) && u_error) {
947 /* Blocked signals do not interrupt any syscalls.
948 * In this case syscalls don't return ERESTARTfoo codes.
950 * Deadly signals set to SIG_DFL interrupt syscalls
951 * and kill the process regardless of which of the codes below
952 * is returned by the interrupted syscall.
953 * In some cases, kernel forces a kernel-generated deadly
954 * signal to be unblocked and set to SIG_DFL (and thus cause
955 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
956 * or SIGILL. (The alternative is to leave process spinning
957 * forever on the faulty instruction - not useful).
959 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
960 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
961 * but kernel will always restart them.
964 /* Most common type of signal-interrupted syscall exit code.
965 * The system call will be restarted with the same arguments
966 * if SA_RESTART is set; otherwise, it will fail with EINTR.
968 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
971 /* Rare. For example, fork() returns this if interrupted.
972 * SA_RESTART is ignored (assumed set): the restart is unconditional.
974 tprints("= ? ERESTARTNOINTR (To be restarted)");
977 /* pause(), rt_sigsuspend() etc use this code.
978 * SA_RESTART is ignored (assumed not set):
979 * syscall won't restart (will return EINTR instead)
980 * even after signal with SA_RESTART set. However,
981 * after SIG_IGN or SIG_DFL signal it will restart
982 * (thus the name "restart only if has no handler").
984 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
986 case ERESTART_RESTARTBLOCK:
987 /* Syscalls like nanosleep(), poll() which can't be
988 * restarted with their original arguments use this
989 * code. Kernel will execute restart_syscall() instead,
990 * which changes arguments before restarting syscall.
991 * SA_RESTART is ignored (assumed not set) similarly
992 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
993 * since restart data is saved in "restart block"
994 * in task struct, and if signal handler uses a syscall
995 * which in turn saves another such restart block,
996 * old data is lost and restart becomes impossible)
998 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
1001 if ((unsigned long) u_error < nerrnos
1002 && errnoent[u_error])
1003 tprintf("= -1 %s (%s)", errnoent[u_error],
1006 tprintf("= -1 ERRNO_%lu (%s)", u_error,
1010 if ((sys_res & RVAL_STR) && tcp->auxstr)
1011 tprintf(" (%s)", tcp->auxstr);
1014 if (sys_res & RVAL_NONE)
1017 switch (sys_res & RVAL_MASK) {
1019 #if SUPPORTED_PERSONALITIES > 1
1020 if (current_wordsize < sizeof(long))
1022 (unsigned int) tcp->u_rval);
1025 tprintf("= %#lx", tcp->u_rval);
1028 tprintf("= %#lo", tcp->u_rval);
1031 tprintf("= %lu", tcp->u_rval);
1034 tprintf("= %ld", tcp->u_rval);
1039 printfd(tcp, tcp->u_rval);
1042 tprintf("= %ld", tcp->u_rval);
1044 #if defined(LINUX_MIPSN32) || defined(X32)
1047 tprintf("= %#llx", tcp->u_lrval);
1050 tprintf("= %#llo", tcp->u_lrval);
1053 case RVAL_LUDECIMAL:
1054 tprintf("= %llu", tcp->u_lrval);
1058 tprintf("= %lld", tcp->u_lrval);
1063 error_msg("invalid rval format");
1067 if ((sys_res & RVAL_STR) && tcp->auxstr)
1068 tprintf(" (%s)", tcp->auxstr);
1071 tv_sub(&tv, &tv, &tcp->etime);
1072 tprintf(" <%ld.%06ld>",
1073 (long) tv.tv_sec, (long) tv.tv_usec);
1079 #ifdef USE_LIBUNWIND
1080 if (stack_trace_enabled)
1081 unwind_print_stacktrace(tcp);
1085 tcp->flags &= ~TCB_INSYSCALL;
1090 trace_syscall(struct tcb *tcp)
1092 return exiting(tcp) ?
1093 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
1097 * Cannot rely on __kernel_[u]long_t being defined,
1098 * it is quite a recent feature of <asm/posix_types.h>.
1100 #ifdef __kernel_long_t
1101 typedef __kernel_long_t kernel_long_t;
1102 typedef __kernel_ulong_t kernel_ulong_t;
1105 typedef long long kernel_long_t;
1106 typedef unsigned long long kernel_ulong_t;
1108 typedef long kernel_long_t;
1109 typedef unsigned long kernel_ulong_t;
1114 * Check the syscall return value register value for whether it is
1115 * a negated errno code indicating an error, or a success return value.
1118 is_negated_errno(kernel_ulong_t val)
1120 /* Linux kernel defines MAX_ERRNO to 4095. */
1121 kernel_ulong_t max = -(kernel_long_t) 4095;
1123 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1124 if (current_wordsize < sizeof(val)) {
1125 val = (uint32_t) val;
1126 max = (uint32_t) max;
1130 * current_wordsize is 4 even in personality 0 (native X32)
1131 * but truncation _must not_ be done in it.
1132 * can't check current_wordsize here!
1134 if (current_personality != 0) {
1135 val = (uint32_t) val;
1136 max = (uint32_t) max;
1143 #include "arch_regs.c"
1145 #ifdef HAVE_GETRVAL2
1146 # include "arch_getrval2.c"
1150 print_pc(struct tcb *tcp)
1155 #ifdef current_wordsize
1156 # define pc_wordsize current_wordsize
1158 # define pc_wordsize personality_wordsize[tcp->currpers]
1161 if (pc_wordsize == 4) {
1163 bad = "[????????] ";
1166 bad = "[????????????????] ";
1170 #define PRINTBADPC tprints(bad)
1172 if (get_regs_error) {
1177 #include "print_pc.c"
1180 #if defined X86_64 || defined POWERPC
1181 # include "getregs_old.c"
1184 #if defined ARCH_REGS_FOR_GETREGSET
1186 get_regset(pid_t pid)
1188 # ifdef ARCH_IOVEC_FOR_GETREGSET
1189 /* variable iovec */
1190 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1191 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1192 &ARCH_IOVEC_FOR_GETREGSET);
1194 /* constant iovec */
1195 static struct iovec io = {
1196 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1197 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1199 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1203 #endif /* ARCH_REGS_FOR_GETREGSET */
1208 #ifdef ARCH_REGS_FOR_GETREGSET
1210 /* Try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS. */
1211 static int getregset_support;
1213 if (getregset_support >= 0) {
1214 get_regs_error = get_regset(pid);
1215 if (getregset_support > 0)
1217 if (get_regs_error >= 0) {
1218 getregset_support = 1;
1221 if (errno == EPERM || errno == ESRCH)
1223 getregset_support = -1;
1226 # else /* !X86_64 */
1227 /* Assume that PTRACE_GETREGSET works. */
1228 get_regs_error = get_regset(pid);
1230 #elif defined ARCH_REGS_FOR_GETREGS
1231 # if defined SPARC || defined SPARC64
1232 /* SPARC systems have the meaning of data and addr reversed */
1233 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&ARCH_REGS_FOR_GETREGS, 0);
1234 # elif defined POWERPC
1235 static bool old_kernel = 0;
1238 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1239 if (get_regs_error && errno == EIO) {
1242 get_regs_error = getregs_old(pid);
1245 /* Assume that PTRACE_GETREGS works. */
1246 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1249 #else /* !ARCH_REGS_FOR_GETREGSET && !ARCH_REGS_FOR_GETREGS */
1250 # warning get_regs is not implemented for this architecture yet
1256 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1257 * 1: ok, continue in trace_syscall_entering().
1258 * other: error, trace_syscall_entering() should print error indicator
1259 * ("????" etc) and bail out.
1262 get_scno(struct tcb *tcp)
1269 #include "get_scno.c"
1272 if (SCNO_IS_VALID(tcp->scno)) {
1273 tcp->s_ent = &sysent[scno];
1274 tcp->qual_flg = qual_flags[scno];
1276 static const struct_sysent unknown = {
1279 .sys_func = printargs,
1280 .sys_name = "system call",
1282 tcp->s_ent = &unknown;
1283 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1285 error_msg("pid %d invalid syscall %ld", tcp->pid, scno);
1290 /* Return -1 on error or 1 on success (never 0!) */
1292 get_syscall_args(struct tcb *tcp)
1294 #include "get_syscall_args.c"
1299 get_error(struct tcb *tcp)
1301 const bool check_errno = !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS);
1304 #include "get_error.c"
1308 * 1: ok, continue in trace_syscall_exiting().
1309 * -1: error, trace_syscall_exiting() should print error indicator
1310 * ("????" etc) and bail out.
1313 get_syscall_result(struct tcb *tcp)
1315 #if defined ARCH_REGS_FOR_GETREGSET || defined ARCH_REGS_FOR_GETREGS
1316 /* already done by get_regs */
1318 # include "get_syscall_result.c"