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 #define SEN_NAME(syscall_name) SEN_ ## syscall_name
85 #define SEN(syscall_name) SEN_NAME(syscall_name), SYS_FUNC_NAME(syscall_name)
87 const struct_sysent sysent0[] = {
88 #include "syscallent.h"
91 #if SUPPORTED_PERSONALITIES > 1
92 static const struct_sysent sysent1[] = {
93 # include "syscallent1.h"
97 #if SUPPORTED_PERSONALITIES > 2
98 static const struct_sysent sysent2[] = {
99 # include "syscallent2.h"
103 /* Now undef them since short defines cause wicked namespace pollution. */
119 * `ioctlent[012].h' files are automatically generated by the auxiliary
120 * program `ioctlsort', such that the list is sorted by the `code' field.
121 * This has the side-effect of resolving the _IO.. macros into
122 * plain integers, eliminating the need to include here everything
126 const char *const errnoent0[] = {
127 #include "errnoent.h"
129 const char *const signalent0[] = {
130 #include "signalent.h"
132 const struct_ioctlent ioctlent0[] = {
133 #include "ioctlent0.h"
136 #if SUPPORTED_PERSONALITIES > 1
137 static const char *const errnoent1[] = {
138 # include "errnoent1.h"
140 static const char *const signalent1[] = {
141 # include "signalent1.h"
143 static const struct_ioctlent ioctlent1[] = {
144 # include "ioctlent1.h"
148 #if SUPPORTED_PERSONALITIES > 2
149 static const char *const errnoent2[] = {
150 # include "errnoent2.h"
152 static const char *const signalent2[] = {
153 # include "signalent2.h"
155 static const struct_ioctlent ioctlent2[] = {
156 # include "ioctlent2.h"
161 nsyscalls0 = ARRAY_SIZE(sysent0)
162 #if SUPPORTED_PERSONALITIES > 1
163 , nsyscalls1 = ARRAY_SIZE(sysent1)
164 # if SUPPORTED_PERSONALITIES > 2
165 , nsyscalls2 = ARRAY_SIZE(sysent2)
171 nerrnos0 = ARRAY_SIZE(errnoent0)
172 #if SUPPORTED_PERSONALITIES > 1
173 , nerrnos1 = ARRAY_SIZE(errnoent1)
174 # if SUPPORTED_PERSONALITIES > 2
175 , nerrnos2 = ARRAY_SIZE(errnoent2)
181 nsignals0 = ARRAY_SIZE(signalent0)
182 #if SUPPORTED_PERSONALITIES > 1
183 , nsignals1 = ARRAY_SIZE(signalent1)
184 # if SUPPORTED_PERSONALITIES > 2
185 , nsignals2 = ARRAY_SIZE(signalent2)
191 nioctlents0 = ARRAY_SIZE(ioctlent0)
192 #if SUPPORTED_PERSONALITIES > 1
193 , nioctlents1 = ARRAY_SIZE(ioctlent1)
194 # if SUPPORTED_PERSONALITIES > 2
195 , nioctlents2 = ARRAY_SIZE(ioctlent2)
200 #if SUPPORTED_PERSONALITIES > 1
201 const struct_sysent *sysent = sysent0;
202 const char *const *errnoent = errnoent0;
203 const char *const *signalent = signalent0;
204 const struct_ioctlent *ioctlent = ioctlent0;
206 unsigned nsyscalls = nsyscalls0;
207 unsigned nerrnos = nerrnos0;
208 unsigned nsignals = nsignals0;
209 unsigned nioctlents = nioctlents0;
212 qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
214 static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
216 #if SUPPORTED_PERSONALITIES > 1
219 #if SUPPORTED_PERSONALITIES > 2
223 static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
225 #if SUPPORTED_PERSONALITIES > 1
228 #if SUPPORTED_PERSONALITIES > 2
234 MAX_NSYSCALLS1 = (nsyscalls0
235 #if SUPPORTED_PERSONALITIES > 1
236 > nsyscalls1 ? nsyscalls0 : nsyscalls1
239 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
240 #if SUPPORTED_PERSONALITIES > 2
241 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
244 MAX_NSYSCALLS = MAX_NSYSCALLS2,
245 /* We are ready for arches with up to 255 signals,
246 * even though the largest known signo is on MIPS and it is 128.
247 * The number of existing syscalls on all arches is
248 * larger that 255 anyway, so it is just a pedantic matter.
250 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
253 #if SUPPORTED_PERSONALITIES > 1
254 unsigned current_personality;
256 # ifndef current_wordsize
257 unsigned current_wordsize;
258 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
259 PERSONALITY0_WORDSIZE,
260 PERSONALITY1_WORDSIZE,
261 # if SUPPORTED_PERSONALITIES > 2
262 PERSONALITY2_WORDSIZE,
268 set_personality(int personality)
270 nsyscalls = nsyscall_vec[personality];
271 sysent = sysent_vec[personality];
273 switch (personality) {
275 errnoent = errnoent0;
277 ioctlent = ioctlent0;
278 nioctlents = nioctlents0;
279 signalent = signalent0;
280 nsignals = nsignals0;
284 errnoent = errnoent1;
286 ioctlent = ioctlent1;
287 nioctlents = nioctlents1;
288 signalent = signalent1;
289 nsignals = nsignals1;
292 # if SUPPORTED_PERSONALITIES > 2
294 errnoent = errnoent2;
296 ioctlent = ioctlent2;
297 nioctlents = nioctlents2;
298 signalent = signalent2;
299 nsignals = nsignals2;
304 current_personality = personality;
305 # ifndef current_wordsize
306 current_wordsize = personality_wordsize[personality];
311 update_personality(struct tcb *tcp, unsigned int personality)
313 if (personality == current_personality)
315 set_personality(personality);
317 if (personality == tcp->currpers)
319 tcp->currpers = personality;
321 # undef PERSONALITY_NAMES
322 # if defined POWERPC64
323 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
324 # elif defined X86_64
325 # define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
327 # define PERSONALITY_NAMES {"x32", "32 bit"}
328 # elif defined AARCH64
329 # define PERSONALITY_NAMES {"32-bit", "AArch64"}
331 # define PERSONALITY_NAMES {"64-bit", "32-bit"}
333 # ifdef PERSONALITY_NAMES
335 static const char *const names[] = PERSONALITY_NAMES;
336 error_msg("[ Process PID=%d runs in %s mode. ]",
337 tcp->pid, names[personality]);
343 static int qual_syscall(), qual_signal(), qual_desc();
345 static const struct qual_options {
346 unsigned int bitflag;
347 const char *option_name;
348 int (*qualify)(const char *, int, int);
349 const char *argument_name;
351 { QUAL_TRACE, "trace", qual_syscall, "system call" },
352 { QUAL_TRACE, "t", qual_syscall, "system call" },
353 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
354 { QUAL_ABBREV, "a", qual_syscall, "system call" },
355 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
356 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
357 { QUAL_RAW, "raw", qual_syscall, "system call" },
358 { QUAL_RAW, "x", qual_syscall, "system call" },
359 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
360 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
361 { QUAL_SIGNAL, "s", qual_signal, "signal" },
362 { QUAL_READ, "read", qual_desc, "descriptor" },
363 { QUAL_READ, "reads", qual_desc, "descriptor" },
364 { QUAL_READ, "r", qual_desc, "descriptor" },
365 { QUAL_WRITE, "write", qual_desc, "descriptor" },
366 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
367 { QUAL_WRITE, "w", qual_desc, "descriptor" },
368 { 0, NULL, NULL, NULL },
372 reallocate_qual(const unsigned int n)
376 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
377 qp = qual_vec[p] = xreallocarray(qual_vec[p], n,
379 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
385 qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
390 reallocate_qual(n + 1);
392 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
393 if (pers == p || pers < 0) {
395 qual_vec[p][n] &= ~bitflag;
397 qual_vec[p][n] |= bitflag;
403 qual_syscall(const char *s, const unsigned int bitflag, const int not)
409 if (*s >= '0' && *s <= '9') {
410 i = string_to_uint(s);
411 if (i >= MAX_NSYSCALLS)
413 qualify_one(i, bitflag, not, -1);
417 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
418 for (i = 0; i < nsyscall_vec[p]; i++) {
419 if (sysent_vec[p][i].sys_name
420 && strcmp(s, sysent_vec[p][i].sys_name) == 0
422 qualify_one(i, bitflag, not, p);
432 qual_signal(const char *s, const unsigned int bitflag, const int not)
436 if (*s >= '0' && *s <= '9') {
437 int signo = string_to_uint(s);
438 if (signo < 0 || signo > 255)
440 qualify_one(signo, bitflag, not, -1);
443 if (strncasecmp(s, "SIG", 3) == 0)
445 for (i = 0; i <= NSIG; i++) {
446 if (strcasecmp(s, signame(i) + 3) == 0) {
447 qualify_one(i, bitflag, not, -1);
455 qual_desc(const char *s, const unsigned int bitflag, const int not)
457 if (*s >= '0' && *s <= '9') {
458 int desc = string_to_uint(s);
459 if (desc < 0 || desc > 0x7fff) /* paranoia */
461 qualify_one(desc, bitflag, not, -1);
468 lookup_class(const char *s)
470 if (strcmp(s, "file") == 0)
472 if (strcmp(s, "ipc") == 0)
474 if (strcmp(s, "network") == 0)
475 return TRACE_NETWORK;
476 if (strcmp(s, "process") == 0)
477 return TRACE_PROCESS;
478 if (strcmp(s, "signal") == 0)
480 if (strcmp(s, "desc") == 0)
482 if (strcmp(s, "memory") == 0)
488 qualify(const char *s)
490 const struct qual_options *opt;
497 reallocate_qual(MIN_QUALS);
499 opt = &qual_options[0];
500 for (i = 0; (p = qual_options[i].option_name); i++) {
501 unsigned int len = strlen(p);
502 if (strncmp(s, p, len) == 0 && s[len] == '=') {
503 opt = &qual_options[i];
513 if (strcmp(s, "none") == 0) {
517 if (strcmp(s, "all") == 0) {
518 for (i = 0; i < num_quals; i++) {
519 qualify_one(i, opt->bitflag, not, -1);
523 for (i = 0; i < num_quals; i++) {
524 qualify_one(i, opt->bitflag, !not, -1);
527 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
529 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
531 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
532 for (i = 0; i < nsyscall_vec[pers]; i++)
533 if (sysent_vec[pers][i].sys_flags & n)
534 qualify_one(i, opt->bitflag, not, pers);
538 if (opt->qualify(p, opt->bitflag, not)) {
539 error_msg_and_die("invalid %s '%s'",
540 opt->argument_name, p);
547 #ifdef SYS_socket_subcall
549 decode_socket_subcall(struct tcb *tcp)
554 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
557 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
558 tcp->qual_flg = qual_flags[tcp->scno];
559 tcp->s_ent = &sysent[tcp->scno];
560 addr = tcp->u_arg[1];
561 n = tcp->s_ent->nargs;
562 if (sizeof(tcp->u_arg[0]) == current_wordsize) {
563 memset(tcp->u_arg, 0, n * sizeof(tcp->u_arg[0]));
564 (void) umoven(tcp, addr, n * sizeof(tcp->u_arg[0]), tcp->u_arg);
566 unsigned int args[n];
569 memset(args, 0, sizeof(args));
570 (void) umove(tcp, addr, &args);
571 for (i = 0; i < n; ++i)
572 tcp->u_arg[i] = args[i];
577 #ifdef SYS_ipc_subcall
579 decode_ipc_subcall(struct tcb *tcp)
583 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
586 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
587 tcp->qual_flg = qual_flags[tcp->scno];
588 tcp->s_ent = &sysent[tcp->scno];
589 n = tcp->s_ent->nargs;
590 for (i = 0; i < n; i++)
591 tcp->u_arg[i] = tcp->u_arg[i + 1];
597 decode_mips_subcall(struct tcb *tcp)
599 if (!SCNO_IS_VALID(tcp->u_arg[0]))
601 tcp->scno = tcp->u_arg[0];
602 tcp->qual_flg = qual_flags[tcp->scno];
603 tcp->s_ent = &sysent[tcp->scno];
604 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
605 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
607 * Fetching the last arg of 7-arg syscalls (fadvise64_64
608 * and sync_file_range) would require additional code,
609 * see linux/mips/get_syscall_args.c
615 return printargs(tcp);
620 printargs(struct tcb *tcp)
624 int n = tcp->s_ent->nargs;
625 for (i = 0; i < n; i++)
626 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
632 printargs_lu(struct tcb *tcp)
636 int n = tcp->s_ent->nargs;
637 for (i = 0; i < n; i++)
638 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
644 printargs_ld(struct tcb *tcp)
648 int n = tcp->s_ent->nargs;
649 for (i = 0; i < n; i++)
650 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
656 dumpio(struct tcb *tcp)
662 if ((unsigned long) tcp->u_arg[0] >= num_quals)
664 sen = tcp->s_ent->sen;
665 if (SEN_printargs == sen)
667 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
673 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
676 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
680 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
683 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
688 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
694 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
697 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
701 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
704 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
712 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
713 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
715 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
717 shuffle_scno(unsigned long scno)
719 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
722 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
723 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
725 if (scno == 0x000ffff0)
726 return ARM_FIRST_SHUFFLED_SYSCALL;
728 #define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
730 * Is it ARM specific syscall?
731 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
732 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
734 if (scno >= 0x000f0000 &&
735 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
736 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
738 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
739 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
745 # define shuffle_scno(scno) ((long)(scno))
749 undefined_scno_name(struct tcb *tcp)
751 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
753 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
757 static long get_regs_error;
765 static int get_syscall_args(struct tcb *);
766 static int get_syscall_result(struct tcb *);
769 trace_syscall_entering(struct tcb *tcp)
773 scno_good = res = get_scno(tcp);
777 res = get_syscall_args(tcp);
782 tprints("????" /* anti-trigraph gap */ "(");
783 else if (tcp->qual_flg & UNDEFINED_SCNO)
784 tprintf("%s(", undefined_scno_name(tcp));
786 tprintf("%s(", tcp->s_ent->sys_name);
788 * " <unavailable>" will be added later by the code which
789 * detects ptrace errors.
795 if (SEN_syscall == tcp->s_ent->sen)
796 decode_mips_subcall(tcp);
799 if ( SEN_execve == tcp->s_ent->sen
800 # if defined(SPARC) || defined(SPARC64)
801 || SEN_execv == tcp->s_ent->sen
804 hide_log_until_execve = 0;
807 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
808 switch (tcp->s_ent->sen) {
809 # ifdef SYS_socket_subcall
811 decode_socket_subcall(tcp);
814 # ifdef SYS_ipc_subcall
816 decode_ipc_subcall(tcp);
822 if (!(tcp->qual_flg & QUAL_TRACE)
823 || (tracing_paths && !pathtrace_match(tcp))
825 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
826 tcp->sys_func_rval = 0;
830 tcp->flags &= ~TCB_FILTERED;
832 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
838 if (stack_trace_enabled) {
839 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
840 unwind_capture_stacktrace(tcp);
845 if (tcp->qual_flg & UNDEFINED_SCNO)
846 tprintf("%s(", undefined_scno_name(tcp));
848 tprintf("%s(", tcp->s_ent->sys_name);
849 if ((tcp->qual_flg & QUAL_RAW) && SEN_exit != tcp->s_ent->sen)
850 res = printargs(tcp);
852 res = tcp->s_ent->sys_func(tcp);
856 tcp->flags |= TCB_INSYSCALL;
857 tcp->sys_func_rval = res;
858 /* Measure the entrance time as late as possible to avoid errors. */
860 gettimeofday(&tcp->etime, NULL);
865 trace_syscall_exiting(struct tcb *tcp)
872 /* Measure the exit time as early as possible to avoid errors. */
874 gettimeofday(&tv, NULL);
877 if (stack_trace_enabled) {
878 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
879 unwind_cache_invalidate(tcp);
883 #if SUPPORTED_PERSONALITIES > 1
884 update_personality(tcp, tcp->currpers);
886 res = (get_regs_error ? -1 : get_syscall_result(tcp));
887 if (filtered(tcp) || hide_log_until_execve)
891 count_syscall(tcp, &tv);
892 if (cflag == CFLAG_ONLY_STATS) {
897 /* If not in -ff mode, and printing_tcp != tcp,
898 * then the log currently does not end with output
899 * of _our syscall entry_, but with something else.
900 * We need to say which syscall's return is this.
902 * Forced reprinting via TCB_REPRINT is used only by
903 * "strace -ff -oLOG test/threaded_execve" corner case.
904 * It's the only case when -ff mode needs reprinting.
906 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
907 tcp->flags &= ~TCB_REPRINT;
909 if (tcp->qual_flg & UNDEFINED_SCNO)
910 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
912 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
916 tcp->s_prev_ent = NULL;
918 /* There was error in one of prior ptrace ops */
921 tprints("= ? <unavailable>\n");
923 tcp->flags &= ~TCB_INSYSCALL;
924 tcp->sys_func_rval = 0;
927 tcp->s_prev_ent = tcp->s_ent;
930 if (tcp->qual_flg & QUAL_RAW) {
931 /* sys_res = printargs(tcp); - but it's nop on sysexit */
933 /* FIXME: not_failing_only (IOW, option -z) is broken:
934 * failure of syscall is known only after syscall return.
935 * Thus we end up with something like this on, say, ENOENT:
936 * open("doesnt_exist", O_RDONLY <unfinished ...>
937 * {next syscall decode}
938 * whereas the intended result is that open(...) line
939 * is not shown at all.
941 if (not_failing_only && tcp->u_error)
942 goto ret; /* ignore failed syscalls */
943 if (tcp->sys_func_rval & RVAL_DECODED)
944 sys_res = tcp->sys_func_rval;
946 sys_res = tcp->s_ent->sys_func(tcp);
951 u_error = tcp->u_error;
952 if (tcp->qual_flg & QUAL_RAW) {
954 tprintf("= -1 (errno %ld)", u_error);
956 tprintf("= %#lx", tcp->u_rval);
958 else if (!(sys_res & RVAL_NONE) && u_error) {
960 /* Blocked signals do not interrupt any syscalls.
961 * In this case syscalls don't return ERESTARTfoo codes.
963 * Deadly signals set to SIG_DFL interrupt syscalls
964 * and kill the process regardless of which of the codes below
965 * is returned by the interrupted syscall.
966 * In some cases, kernel forces a kernel-generated deadly
967 * signal to be unblocked and set to SIG_DFL (and thus cause
968 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
969 * or SIGILL. (The alternative is to leave process spinning
970 * forever on the faulty instruction - not useful).
972 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
973 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
974 * but kernel will always restart them.
977 /* Most common type of signal-interrupted syscall exit code.
978 * The system call will be restarted with the same arguments
979 * if SA_RESTART is set; otherwise, it will fail with EINTR.
981 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
984 /* Rare. For example, fork() returns this if interrupted.
985 * SA_RESTART is ignored (assumed set): the restart is unconditional.
987 tprints("= ? ERESTARTNOINTR (To be restarted)");
990 /* pause(), rt_sigsuspend() etc use this code.
991 * SA_RESTART is ignored (assumed not set):
992 * syscall won't restart (will return EINTR instead)
993 * even after signal with SA_RESTART set. However,
994 * after SIG_IGN or SIG_DFL signal it will restart
995 * (thus the name "restart only if has no handler").
997 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
999 case ERESTART_RESTARTBLOCK:
1000 /* Syscalls like nanosleep(), poll() which can't be
1001 * restarted with their original arguments use this
1002 * code. Kernel will execute restart_syscall() instead,
1003 * which changes arguments before restarting syscall.
1004 * SA_RESTART is ignored (assumed not set) similarly
1005 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
1006 * since restart data is saved in "restart block"
1007 * in task struct, and if signal handler uses a syscall
1008 * which in turn saves another such restart block,
1009 * old data is lost and restart becomes impossible)
1011 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
1014 if ((unsigned long) u_error < nerrnos
1015 && errnoent[u_error])
1016 tprintf("= -1 %s (%s)", errnoent[u_error],
1019 tprintf("= -1 ERRNO_%lu (%s)", u_error,
1023 if ((sys_res & RVAL_STR) && tcp->auxstr)
1024 tprintf(" (%s)", tcp->auxstr);
1027 if (sys_res & RVAL_NONE)
1030 switch (sys_res & RVAL_MASK) {
1032 #if SUPPORTED_PERSONALITIES > 1
1033 if (current_wordsize < sizeof(long))
1035 (unsigned int) tcp->u_rval);
1038 tprintf("= %#lx", tcp->u_rval);
1041 tprintf("= %#lo", tcp->u_rval);
1044 tprintf("= %lu", tcp->u_rval);
1047 tprintf("= %ld", tcp->u_rval);
1052 printfd(tcp, tcp->u_rval);
1055 tprintf("= %ld", tcp->u_rval);
1057 #if defined(LINUX_MIPSN32) || defined(X32)
1060 tprintf("= %#llx", tcp->u_lrval);
1063 tprintf("= %#llo", tcp->u_lrval);
1066 case RVAL_LUDECIMAL:
1067 tprintf("= %llu", tcp->u_lrval);
1071 tprintf("= %lld", tcp->u_lrval);
1076 error_msg("invalid rval format");
1080 if ((sys_res & RVAL_STR) && tcp->auxstr)
1081 tprintf(" (%s)", tcp->auxstr);
1084 tv_sub(&tv, &tv, &tcp->etime);
1085 tprintf(" <%ld.%06ld>",
1086 (long) tv.tv_sec, (long) tv.tv_usec);
1092 #ifdef USE_LIBUNWIND
1093 if (stack_trace_enabled)
1094 unwind_print_stacktrace(tcp);
1098 tcp->flags &= ~TCB_INSYSCALL;
1099 tcp->sys_func_rval = 0;
1104 trace_syscall(struct tcb *tcp)
1106 return exiting(tcp) ?
1107 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
1111 * Cannot rely on __kernel_[u]long_t being defined,
1112 * it is quite a recent feature of <asm/posix_types.h>.
1114 #ifdef __kernel_long_t
1115 typedef __kernel_long_t kernel_long_t;
1116 typedef __kernel_ulong_t kernel_ulong_t;
1119 typedef long long kernel_long_t;
1120 typedef unsigned long long kernel_ulong_t;
1122 typedef long kernel_long_t;
1123 typedef unsigned long kernel_ulong_t;
1128 * Check the syscall return value register value for whether it is
1129 * a negated errno code indicating an error, or a success return value.
1132 is_negated_errno(kernel_ulong_t val)
1134 /* Linux kernel defines MAX_ERRNO to 4095. */
1135 kernel_ulong_t max = -(kernel_long_t) 4095;
1137 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1138 if (current_wordsize < sizeof(val)) {
1139 val = (uint32_t) val;
1140 max = (uint32_t) max;
1144 * current_wordsize is 4 even in personality 0 (native X32)
1145 * but truncation _must not_ be done in it.
1146 * can't check current_wordsize here!
1148 if (current_personality != 0) {
1149 val = (uint32_t) val;
1150 max = (uint32_t) max;
1157 #include "arch_regs.c"
1159 #ifdef HAVE_GETRVAL2
1160 # include "arch_getrval2.c"
1164 print_pc(struct tcb *tcp)
1169 #ifdef current_wordsize
1170 # define pc_wordsize current_wordsize
1172 # define pc_wordsize personality_wordsize[tcp->currpers]
1175 if (pc_wordsize == 4) {
1177 bad = "[????????] ";
1180 bad = "[????????????????] ";
1184 #define PRINTBADPC tprints(bad)
1186 if (get_regs_error) {
1191 #include "print_pc.c"
1194 #if defined X86_64 || defined POWERPC
1195 # include "getregs_old.c"
1198 #if defined ARCH_REGS_FOR_GETREGSET
1200 get_regset(pid_t pid)
1202 # ifdef ARCH_IOVEC_FOR_GETREGSET
1203 /* variable iovec */
1204 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1205 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1206 &ARCH_IOVEC_FOR_GETREGSET);
1208 /* constant iovec */
1209 static struct iovec io = {
1210 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1211 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1213 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1217 #endif /* ARCH_REGS_FOR_GETREGSET */
1222 #ifdef ARCH_REGS_FOR_GETREGSET
1224 /* Try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS. */
1225 static int getregset_support;
1227 if (getregset_support >= 0) {
1228 get_regs_error = get_regset(pid);
1229 if (getregset_support > 0)
1231 if (get_regs_error >= 0) {
1232 getregset_support = 1;
1235 if (errno == EPERM || errno == ESRCH)
1237 getregset_support = -1;
1240 # else /* !X86_64 */
1241 /* Assume that PTRACE_GETREGSET works. */
1242 get_regs_error = get_regset(pid);
1244 #elif defined ARCH_REGS_FOR_GETREGS
1245 # if defined SPARC || defined SPARC64
1246 /* SPARC systems have the meaning of data and addr reversed */
1247 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&ARCH_REGS_FOR_GETREGS, 0);
1248 # elif defined POWERPC
1249 static bool old_kernel = 0;
1252 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1253 if (get_regs_error && errno == EIO) {
1256 get_regs_error = getregs_old(pid);
1259 /* Assume that PTRACE_GETREGS works. */
1260 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1263 #else /* !ARCH_REGS_FOR_GETREGSET && !ARCH_REGS_FOR_GETREGS */
1264 # warning get_regs is not implemented for this architecture yet
1270 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1271 * 1: ok, continue in trace_syscall_entering().
1272 * other: error, trace_syscall_entering() should print error indicator
1273 * ("????" etc) and bail out.
1276 get_scno(struct tcb *tcp)
1283 #include "get_scno.c"
1286 if (SCNO_IS_VALID(tcp->scno)) {
1287 tcp->s_ent = &sysent[scno];
1288 tcp->qual_flg = qual_flags[scno];
1290 static const struct_sysent unknown = {
1293 .sys_func = printargs,
1294 .sys_name = "system call",
1296 tcp->s_ent = &unknown;
1297 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1299 error_msg("pid %d invalid syscall %ld", tcp->pid, scno);
1304 /* Return -1 on error or 1 on success (never 0!) */
1306 get_syscall_args(struct tcb *tcp)
1308 #include "get_syscall_args.c"
1313 get_error(struct tcb *tcp)
1315 const bool check_errno = !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS);
1318 #include "get_error.c"
1322 * 1: ok, continue in trace_syscall_exiting().
1323 * -1: error, trace_syscall_exiting() should print error indicator
1324 * ("????" etc) and bail out.
1327 get_syscall_result(struct tcb *tcp)
1329 #if defined ARCH_REGS_FOR_GETREGSET || defined ARCH_REGS_FOR_GETREGS
1330 /* already done by get_regs */
1332 # include "get_syscall_result.c"