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 "native_defs.h"
36 #include <sys/param.h>
39 /* for struct iovec */
46 # undef PTRACE_GETREGS
47 # define PTRACE_GETREGS PTRACE_GETREGS64
48 # undef PTRACE_SETREGS
49 # define PTRACE_SETREGS PTRACE_SETREGS64
53 # include <asm/psrcompat.h>
63 # define NT_PRSTATUS 1
67 # warning: NSIG is not defined, using 32
73 /* Define these shorthand notations to simplify the syscallent files. */
77 #define TN TRACE_NETWORK
78 #define TP TRACE_PROCESS
79 #define TS TRACE_SIGNAL
80 #define TM TRACE_MEMORY
81 #define NF SYSCALL_NEVER_FAILS
83 #define SI STACKTRACE_INVALIDATE_CACHE
84 #define SE STACKTRACE_CAPTURE_ON_ENTER
86 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
88 const struct_sysent sysent0[] = {
89 #include "syscallent.h"
92 #if SUPPORTED_PERSONALITIES > 1
93 # include PERSONALITY1_INCLUDE_FUNCS
94 static const struct_sysent sysent1[] = {
95 # include "syscallent1.h"
99 #if SUPPORTED_PERSONALITIES > 2
100 # include PERSONALITY2_INCLUDE_FUNCS
101 static const struct_sysent sysent2[] = {
102 # include "syscallent2.h"
106 /* Now undef them since short defines cause wicked namespace pollution. */
121 * `ioctlent[012].h' files are automatically generated by the auxiliary
122 * program `ioctlsort', such that the list is sorted by the `code' field.
123 * This has the side-effect of resolving the _IO.. macros into
124 * plain integers, eliminating the need to include here everything
128 const char *const errnoent0[] = {
129 #include "errnoent.h"
131 const char *const signalent0[] = {
132 #include "signalent.h"
134 const struct_ioctlent ioctlent0[] = {
135 #include "ioctlent0.h"
138 #if SUPPORTED_PERSONALITIES > 1
139 static const char *const errnoent1[] = {
140 # include "errnoent1.h"
142 static const char *const signalent1[] = {
143 # include "signalent1.h"
145 static const struct_ioctlent ioctlent1[] = {
146 # include "ioctlent1.h"
148 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
149 static const struct_printers printers0 = {
150 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
152 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
153 static const struct_printers printers1 = {
154 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
158 #if SUPPORTED_PERSONALITIES > 2
159 static const char *const errnoent2[] = {
160 # include "errnoent2.h"
162 static const char *const signalent2[] = {
163 # include "signalent2.h"
165 static const struct_ioctlent ioctlent2[] = {
166 # include "ioctlent2.h"
168 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
169 static const struct_printers printers2 = {
170 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
175 nsyscalls0 = ARRAY_SIZE(sysent0)
176 #if SUPPORTED_PERSONALITIES > 1
177 , nsyscalls1 = ARRAY_SIZE(sysent1)
178 # if SUPPORTED_PERSONALITIES > 2
179 , nsyscalls2 = ARRAY_SIZE(sysent2)
185 nerrnos0 = ARRAY_SIZE(errnoent0)
186 #if SUPPORTED_PERSONALITIES > 1
187 , nerrnos1 = ARRAY_SIZE(errnoent1)
188 # if SUPPORTED_PERSONALITIES > 2
189 , nerrnos2 = ARRAY_SIZE(errnoent2)
195 nsignals0 = ARRAY_SIZE(signalent0)
196 #if SUPPORTED_PERSONALITIES > 1
197 , nsignals1 = ARRAY_SIZE(signalent1)
198 # if SUPPORTED_PERSONALITIES > 2
199 , nsignals2 = ARRAY_SIZE(signalent2)
205 nioctlents0 = ARRAY_SIZE(ioctlent0)
206 #if SUPPORTED_PERSONALITIES > 1
207 , nioctlents1 = ARRAY_SIZE(ioctlent1)
208 # if SUPPORTED_PERSONALITIES > 2
209 , nioctlents2 = ARRAY_SIZE(ioctlent2)
214 #if SUPPORTED_PERSONALITIES > 1
215 const struct_sysent *sysent = sysent0;
216 const char *const *errnoent = errnoent0;
217 const char *const *signalent = signalent0;
218 const struct_ioctlent *ioctlent = ioctlent0;
219 const struct_printers *printers = &printers0;
222 unsigned nsyscalls = nsyscalls0;
223 unsigned nerrnos = nerrnos0;
224 unsigned nsignals = nsignals0;
225 unsigned nioctlents = nioctlents0;
228 qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
230 static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
232 #if SUPPORTED_PERSONALITIES > 1
235 #if SUPPORTED_PERSONALITIES > 2
239 static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
241 #if SUPPORTED_PERSONALITIES > 1
244 #if SUPPORTED_PERSONALITIES > 2
250 MAX_NSYSCALLS1 = (nsyscalls0
251 #if SUPPORTED_PERSONALITIES > 1
252 > nsyscalls1 ? nsyscalls0 : nsyscalls1
255 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
256 #if SUPPORTED_PERSONALITIES > 2
257 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
260 MAX_NSYSCALLS = MAX_NSYSCALLS2,
261 /* We are ready for arches with up to 255 signals,
262 * even though the largest known signo is on MIPS and it is 128.
263 * The number of existing syscalls on all arches is
264 * larger that 255 anyway, so it is just a pedantic matter.
266 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
269 #if SUPPORTED_PERSONALITIES > 1
270 unsigned current_personality;
272 # ifndef current_wordsize
273 unsigned current_wordsize;
274 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
275 PERSONALITY0_WORDSIZE,
276 PERSONALITY1_WORDSIZE,
277 # if SUPPORTED_PERSONALITIES > 2
278 PERSONALITY2_WORDSIZE,
284 set_personality(int personality)
286 nsyscalls = nsyscall_vec[personality];
287 sysent = sysent_vec[personality];
289 switch (personality) {
291 errnoent = errnoent0;
293 ioctlent = ioctlent0;
294 nioctlents = nioctlents0;
295 signalent = signalent0;
296 nsignals = nsignals0;
297 printers = &printers0;
301 errnoent = errnoent1;
303 ioctlent = ioctlent1;
304 nioctlents = nioctlents1;
305 signalent = signalent1;
306 nsignals = nsignals1;
307 printers = &printers1;
310 # if SUPPORTED_PERSONALITIES > 2
312 errnoent = errnoent2;
314 ioctlent = ioctlent2;
315 nioctlents = nioctlents2;
316 signalent = signalent2;
317 nsignals = nsignals2;
318 printers = &printers2;
323 current_personality = personality;
324 # ifndef current_wordsize
325 current_wordsize = personality_wordsize[personality];
330 update_personality(struct tcb *tcp, unsigned int personality)
332 if (personality == current_personality)
334 set_personality(personality);
336 if (personality == tcp->currpers)
338 tcp->currpers = personality;
340 # undef PERSONALITY_NAMES
341 # if defined POWERPC64
342 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
343 # elif defined X86_64
344 # define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
346 # define PERSONALITY_NAMES {"x32", "32 bit"}
347 # elif defined AARCH64
348 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
350 # define PERSONALITY_NAMES {"64-bit", "32-bit"}
352 # ifdef PERSONALITY_NAMES
354 static const char *const names[] = PERSONALITY_NAMES;
355 error_msg("[ Process PID=%d runs in %s mode. ]",
356 tcp->pid, names[personality]);
362 static int qual_desc(const char *, unsigned int, int);
363 static int qual_fault(const char *, unsigned int, int);
364 static int qual_signal(const char *, unsigned int, int);
365 static int qual_syscall(const char *, unsigned int, int);
367 static const struct qual_options {
368 unsigned int bitflag;
369 const char *option_name;
370 int (*qualify)(const char *, unsigned int, int);
371 const char *argument_name;
373 { QUAL_TRACE, "trace", qual_syscall, "system call" },
374 { QUAL_TRACE, "t", qual_syscall, "system call" },
375 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
376 { QUAL_ABBREV, "a", qual_syscall, "system call" },
377 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
378 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
379 { QUAL_RAW, "raw", qual_syscall, "system call" },
380 { QUAL_RAW, "x", qual_syscall, "system call" },
381 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
382 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
383 { QUAL_SIGNAL, "s", qual_signal, "signal" },
384 { QUAL_READ, "read", qual_desc, "descriptor" },
385 { QUAL_READ, "reads", qual_desc, "descriptor" },
386 { QUAL_READ, "r", qual_desc, "descriptor" },
387 { QUAL_WRITE, "write", qual_desc, "descriptor" },
388 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
389 { QUAL_WRITE, "w", qual_desc, "descriptor" },
390 { QUAL_FAULT, "fault", qual_fault, "fault argument"},
391 { 0, NULL, NULL, NULL },
395 reallocate_vec(void **vec, unsigned int old_nmemb,
396 size_t size, unsigned int new_nmemb)
400 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
401 vec[p] = xreallocarray(vec[p], new_nmemb, size);
402 memset(vec[p] + size * old_nmemb, 0,
403 (new_nmemb - old_nmemb) * size);
408 reallocate_qual(const unsigned int n)
410 reallocate_vec((void **) qual_vec, num_quals, sizeof(qualbits_t), n);
420 static unsigned int num_faults;
421 static struct fault_opts *fault_vec[SUPPORTED_PERSONALITIES];
424 reallocate_fault(const unsigned int n)
426 reallocate_vec((void **) fault_vec, num_faults,
427 sizeof(struct fault_opts), n);
432 qualify_one(const unsigned int n, unsigned int bitflag, const int not,
433 const int pers, const struct fault_opts *fopts)
437 if (num_quals <= n) {
438 reallocate_qual(n + 1);
439 reallocate_fault(n + 1);
442 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
443 if (pers == p || pers < 0) {
445 qual_vec[p][n] &= ~bitflag;
447 qual_vec[p][n] |= bitflag;
449 memcpy(&fault_vec[p][n], fopts,
457 qualify_scno(const char *const s, const unsigned int bitflag,
458 const int not, const struct fault_opts *const fopts)
460 int n = string_to_uint_upto(s, MAX_NSYSCALLS - 1);
465 /* set bitflag for all syscall numbers except n */
467 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
470 for (i = 0; i < nsyscall_vec[p]; ++i) {
471 if (i != (unsigned int) n
472 && sysent_vec[p][i].sys_name) {
473 qualify_one(i, bitflag, 0, p, fopts);
478 qualify_one(n, bitflag, not, -1, fopts);
485 lookup_class(const char *s)
487 if (strcmp(s, "all") == 0)
489 if (strcmp(s, "file") == 0)
491 if (strcmp(s, "ipc") == 0)
493 if (strcmp(s, "network") == 0)
494 return TRACE_NETWORK;
495 if (strcmp(s, "process") == 0)
496 return TRACE_PROCESS;
497 if (strcmp(s, "signal") == 0)
499 if (strcmp(s, "desc") == 0)
501 if (strcmp(s, "memory") == 0)
507 qualify_syscall_class(const char *const s, const unsigned int bitflag,
508 const int not, const struct fault_opts *const fopts)
511 const int n = lookup_class(s);
516 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
519 for (i = 0; i < nsyscall_vec[p]; ++i) {
520 if (!sysent_vec[p][i].sys_name)
522 const bool match = (sysent_vec[p][i].sys_flags & n) == n;
523 if (match ^ (not && fopts)) {
524 qualify_one(i, bitflag, not && !fopts, p, fopts);
533 qualify_syscall_name(const char *const s, const unsigned int bitflag,
534 const int not, const struct fault_opts *const fopts)
539 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
542 for (i = 0; i < nsyscall_vec[p]; ++i) {
543 if (!sysent_vec[p][i].sys_name)
545 const bool match = !strcmp(s, sysent_vec[p][i].sys_name);
546 found = found || match;
547 if (match ^ (not && fopts)) {
548 qualify_one(i, bitflag, not && !fopts, p, fopts);
557 qual_syscall_ex(const char *const s, const unsigned int bitflag,
558 const int not, const struct fault_opts *const fopts)
560 if (qualify_scno(s, bitflag, not, fopts)
561 || qualify_syscall_class(s, bitflag, not, fopts)
562 || qualify_syscall_name(s, bitflag, not, fopts)) {
570 qual_syscall(const char *const s, const unsigned int bitflag, const int not)
572 return qual_syscall_ex(s, bitflag, not, NULL);
576 * Returns NULL if STR does not start with PREFIX,
577 * or a pointer to the first char in STR after PREFIX.
580 strip_prefix(const char *prefix, const char *str)
582 size_t len = strlen(prefix);
584 return (len > strlen(str) || memcmp(prefix, str, len))
589 find_errno_by_name(const char *name)
593 for (i = 1; i < nerrnos; ++i) {
594 if (errnoent[i] && (strcmp(name, errnoent[i]) == 0))
602 parse_fault_token(const char *const token, struct fault_opts *const fopts)
607 if ((val = strip_prefix("when=", token))) {
615 intval = string_to_uint_ex(val, &end, 0xffff, "+");
619 fopts->first = intval;
625 intval = string_to_uint_upto(val, 0xffff);
628 fopts->step = intval;
637 } else if ((val = strip_prefix("error=", token))) {
638 intval = string_to_uint_upto(val, 4095);
640 intval = find_errno_by_name(val);
652 parse_fault_expression(const char *const s, char **buf,
653 struct fault_opts *const fopts)
655 char *saveptr = NULL;
660 for (token = strtok_r(*buf, ":", &saveptr); token;
661 token = strtok_r(NULL, ":", &saveptr)) {
664 else if (!parse_fault_token(token, fopts))
677 qual_fault(const char *const s, const unsigned int bitflag, const int not)
679 struct fault_opts opts = {
686 char *name = parse_fault_expression(s, &buf, &opts);
687 char *saveptr = NULL;
694 for (token = strtok_r(name, ",", &saveptr); token;
695 token = strtok_r(NULL, ",", &saveptr)) {
696 rc = qual_syscall_ex(token, bitflag, not, &opts);
706 qual_signal(const char *s, const unsigned int bitflag, const int not)
710 if (*s >= '0' && *s <= '9') {
711 i = string_to_uint_upto(s, 255);
714 qualify_one(i, bitflag, not, -1, NULL);
717 if (strncasecmp(s, "SIG", 3) == 0)
719 for (i = 0; i <= NSIG; ++i) {
720 const char *name = signame(i);
721 if (strncasecmp(name, "SIG", 3) != 0)
725 if (strcasecmp(name, s) != 0)
727 qualify_one(i, bitflag, not, -1, NULL);
734 qual_desc(const char *s, const unsigned int bitflag, const int not)
736 int desc = string_to_uint_upto(s, 0x7fff);
739 qualify_one(desc, bitflag, not, -1, NULL);
744 qualify(const char *s)
746 const struct qual_options *opt;
752 if (num_quals == 0) {
753 reallocate_qual(MIN_QUALS);
754 reallocate_fault(MIN_QUALS);
757 opt = &qual_options[0];
758 for (i = 0; (p = qual_options[i].option_name); i++) {
759 unsigned int len = strlen(p);
760 if (strncmp(s, p, len) == 0 && s[len] == '=') {
761 opt = &qual_options[i];
771 if (strcmp(s, "none") == 0) {
775 if (opt->bitflag == QUAL_FAULT) {
776 if (opt->qualify(s, opt->bitflag, not)) {
777 error_msg_and_die("invalid %s '%s'",
778 opt->argument_name, s);
782 if (strcmp(s, "all") == 0) {
783 for (i = 0; i < num_quals; ++i) {
784 qualify_one(i, opt->bitflag, not, -1, NULL);
788 for (i = 0; i < num_quals; ++i) {
789 qualify_one(i, opt->bitflag, !not, -1, NULL);
792 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
793 if (opt->qualify(p, opt->bitflag, not)) {
794 error_msg_and_die("invalid %s '%s'",
795 opt->argument_name, p);
802 #ifdef SYS_socket_subcall
804 decode_socket_subcall(struct tcb *tcp)
806 const int call = tcp->u_arg[0];
808 if (call < 1 || call >= SYS_socket_nsubcalls)
811 const unsigned long scno = SYS_socket_subcall + call;
812 const unsigned int nargs = sysent[scno].nargs;
815 if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
819 tcp->qual_flg = qual_flags[scno];
820 tcp->s_ent = &sysent[scno];
823 for (i = 0; i < nargs; ++i)
824 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
825 ? ((uint32_t *) (void *) buf)[i] : buf[i];
829 #ifdef SYS_ipc_subcall
831 decode_ipc_subcall(struct tcb *tcp)
833 unsigned int call = tcp->u_arg[0];
834 const unsigned int version = call >> 16;
837 # if defined S390 || defined S390X
841 if (current_wordsize == 8)
844 set_tcb_priv_ulong(tcp, version);
850 case 1: case 2: case 3: case 4:
851 case 11: case 12: case 13: case 14:
852 case 21: case 22: case 23: case 24:
858 tcp->scno = SYS_ipc_subcall + call;
859 tcp->qual_flg = qual_flags[tcp->scno];
860 tcp->s_ent = &sysent[tcp->scno];
862 const unsigned int n = tcp->s_ent->nargs;
864 for (i = 0; i < n; i++)
865 tcp->u_arg[i] = tcp->u_arg[i + 1];
871 decode_mips_subcall(struct tcb *tcp)
873 if (!SCNO_IS_VALID(tcp->u_arg[0]))
875 tcp->scno = tcp->u_arg[0];
876 tcp->qual_flg = qual_flags[tcp->scno];
877 tcp->s_ent = &sysent[tcp->scno];
878 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
879 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
881 * Fetching the last arg of 7-arg syscalls (fadvise64_64
882 * and sync_file_range) requires additional code,
883 * see linux/mips/get_syscall_args.c
885 if (tcp->s_ent->nargs == MAX_ARGS) {
887 mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
888 sizeof(tcp->u_arg[0]),
889 &tcp->u_arg[MAX_ARGS - 1]) < 0)
890 tcp->u_arg[MAX_ARGS - 1] = 0;
893 #endif /* LINUX_MIPSO32 */
896 dumpio(struct tcb *tcp)
902 if ((unsigned long) tcp->u_arg[0] >= num_quals)
904 sen = tcp->s_ent->sen;
905 if (SEN_printargs == sen)
907 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
913 case SEN_mq_timedreceive:
914 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
919 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
923 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
926 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
930 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
936 case SEN_mq_timedsend:
937 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
943 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
946 dumpiov_in_msghdr(tcp, tcp->u_arg[1],
947 (unsigned long) -1L);
950 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
957 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
958 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
960 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
962 shuffle_scno(unsigned long scno)
964 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
967 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
968 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
970 if (scno == 0x000ffff0)
971 return ARM_FIRST_SHUFFLED_SYSCALL;
973 #define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
975 * Is it ARM specific syscall?
976 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
977 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
979 if (scno >= 0x000f0000 &&
980 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
981 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
983 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
984 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
990 # define shuffle_scno(scno) ((long)(scno))
994 syscall_name(long scno)
996 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
998 if (SCNO_IS_VALID(scno))
999 return sysent[scno].sys_name;
1001 sprintf(buf, "syscall_%lu", scno);
1007 err_name(unsigned long err)
1009 if ((err < nerrnos) && errnoent[err])
1010 return errnoent[err];
1015 static long get_regs_error;
1020 get_regs_error = -1;
1023 static int get_syscall_args(struct tcb *);
1024 static int get_syscall_result(struct tcb *);
1025 static int arch_get_scno(struct tcb *tcp);
1026 static int arch_set_scno(struct tcb *, long);
1027 static void get_error(struct tcb *, const bool);
1028 static int arch_set_error(struct tcb *);
1030 static struct fault_opts *
1031 tcb_fault_opts(struct tcb *tcp)
1033 return (SCNO_IN_RANGE(tcp->scno) && tcp->fault_vec[current_personality])
1034 ? &tcp->fault_vec[current_personality][tcp->scno] : NULL;
1039 inject_syscall_fault_entering(struct tcb *tcp)
1041 if (!tcp->fault_vec[current_personality]) {
1042 tcp->fault_vec[current_personality] =
1043 xreallocarray(NULL, num_faults,
1044 sizeof(struct fault_opts));
1045 memcpy(tcp->fault_vec[current_personality],
1046 fault_vec[current_personality],
1047 num_faults * sizeof(struct fault_opts));
1050 struct fault_opts *opts = tcb_fault_opts(tcp);
1052 if (opts->first == 0)
1057 if (opts->first != 0)
1060 opts->first = opts->step;
1062 if (!arch_set_scno(tcp, -1))
1063 tcp->flags |= TCB_FAULT_INJ;
1069 update_syscall_fault_exiting(struct tcb *tcp)
1071 struct fault_opts *opts = tcb_fault_opts(tcp);
1073 if (opts && opts->err && tcp->u_error != opts->err) {
1074 unsigned long u_error = tcp->u_error;
1075 tcp->u_error = opts->err;
1076 if (arch_set_error(tcp))
1077 tcp->u_error = u_error;
1084 trace_syscall_entering(struct tcb *tcp)
1088 scno_good = res = get_scno(tcp);
1092 res = get_syscall_args(tcp);
1096 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
1098 * " <unavailable>" will be added later by the code which
1099 * detects ptrace errors.
1104 #ifdef LINUX_MIPSO32
1105 if (SEN_syscall == tcp->s_ent->sen)
1106 decode_mips_subcall(tcp);
1109 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
1110 switch (tcp->s_ent->sen) {
1111 # ifdef SYS_socket_subcall
1112 case SEN_socketcall:
1113 decode_socket_subcall(tcp);
1116 # ifdef SYS_ipc_subcall
1118 decode_ipc_subcall(tcp);
1124 /* Restrain from fault injection while the trace executes strace code. */
1125 if (hide_log(tcp)) {
1126 tcp->qual_flg &= ~QUAL_FAULT;
1129 switch (tcp->s_ent->sen) {
1132 #if defined SPARC || defined SPARC64
1135 tcp->flags &= ~TCB_HIDE_LOG;
1139 if (!(tcp->qual_flg & QUAL_TRACE)
1140 || (tracing_paths && !pathtrace_match(tcp))
1142 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1143 tcp->sys_func_rval = 0;
1147 tcp->flags &= ~TCB_FILTERED;
1149 if (hide_log(tcp)) {
1154 if (tcp->qual_flg & QUAL_FAULT)
1155 inject_syscall_fault_entering(tcp);
1157 if (cflag == CFLAG_ONLY_STATS) {
1162 #ifdef USE_LIBUNWIND
1163 if (stack_trace_enabled) {
1164 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
1165 unwind_capture_stacktrace(tcp);
1170 tprintf("%s(", tcp->s_ent->sys_name);
1171 if (tcp->qual_flg & QUAL_RAW)
1172 res = printargs(tcp);
1174 res = tcp->s_ent->sys_func(tcp);
1178 tcp->flags |= TCB_INSYSCALL;
1179 tcp->sys_func_rval = res;
1180 /* Measure the entrance time as late as possible to avoid errors. */
1182 gettimeofday(&tcp->etime, NULL);
1187 syscall_fault_injected(struct tcb *tcp)
1189 return tcp->flags & TCB_FAULT_INJ;
1193 trace_syscall_exiting(struct tcb *tcp)
1198 unsigned long u_error;
1199 const char *u_error_str;
1201 /* Measure the exit time as early as possible to avoid errors. */
1203 gettimeofday(&tv, NULL);
1205 #ifdef USE_LIBUNWIND
1206 if (stack_trace_enabled) {
1207 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
1208 unwind_cache_invalidate(tcp);
1212 #if SUPPORTED_PERSONALITIES > 1
1213 update_personality(tcp, tcp->currpers);
1215 res = (get_regs_error ? -1 : get_syscall_result(tcp));
1216 if (filtered(tcp) || hide_log(tcp))
1219 if (syserror(tcp) && syscall_fault_injected(tcp))
1220 update_syscall_fault_exiting(tcp);
1223 count_syscall(tcp, &tv);
1224 if (cflag == CFLAG_ONLY_STATS) {
1229 /* If not in -ff mode, and printing_tcp != tcp,
1230 * then the log currently does not end with output
1231 * of _our syscall entry_, but with something else.
1232 * We need to say which syscall's return is this.
1234 * Forced reprinting via TCB_REPRINT is used only by
1235 * "strace -ff -oLOG test/threaded_execve" corner case.
1236 * It's the only case when -ff mode needs reprinting.
1238 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
1239 tcp->flags &= ~TCB_REPRINT;
1241 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
1245 tcp->s_prev_ent = NULL;
1247 /* There was error in one of prior ptrace ops */
1250 tprints("= ? <unavailable>\n");
1252 tcp->flags &= ~(TCB_INSYSCALL | TCB_FAULT_INJ);
1253 tcp->sys_func_rval = 0;
1254 free_tcb_priv_data(tcp);
1257 tcp->s_prev_ent = tcp->s_ent;
1260 if (tcp->qual_flg & QUAL_RAW) {
1261 /* sys_res = printargs(tcp); - but it's nop on sysexit */
1263 /* FIXME: not_failing_only (IOW, option -z) is broken:
1264 * failure of syscall is known only after syscall return.
1265 * Thus we end up with something like this on, say, ENOENT:
1266 * open("doesnt_exist", O_RDONLY <unfinished ...>
1267 * {next syscall decode}
1268 * whereas the intended result is that open(...) line
1269 * is not shown at all.
1271 if (not_failing_only && tcp->u_error)
1272 goto ret; /* ignore failed syscalls */
1273 if (tcp->sys_func_rval & RVAL_DECODED)
1274 sys_res = tcp->sys_func_rval;
1276 sys_res = tcp->s_ent->sys_func(tcp);
1281 u_error = tcp->u_error;
1283 if (tcp->qual_flg & QUAL_RAW) {
1285 tprintf("= -1 (errno %lu)", u_error);
1286 if (syscall_fault_injected(tcp))
1287 tprints(" (INJECTED)");
1289 tprintf("= %#lx", tcp->u_rval);
1292 else if (!(sys_res & RVAL_NONE) && u_error) {
1294 /* Blocked signals do not interrupt any syscalls.
1295 * In this case syscalls don't return ERESTARTfoo codes.
1297 * Deadly signals set to SIG_DFL interrupt syscalls
1298 * and kill the process regardless of which of the codes below
1299 * is returned by the interrupted syscall.
1300 * In some cases, kernel forces a kernel-generated deadly
1301 * signal to be unblocked and set to SIG_DFL (and thus cause
1302 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
1303 * or SIGILL. (The alternative is to leave process spinning
1304 * forever on the faulty instruction - not useful).
1306 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
1307 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
1308 * but kernel will always restart them.
1311 /* Most common type of signal-interrupted syscall exit code.
1312 * The system call will be restarted with the same arguments
1313 * if SA_RESTART is set; otherwise, it will fail with EINTR.
1315 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
1317 case ERESTARTNOINTR:
1318 /* Rare. For example, fork() returns this if interrupted.
1319 * SA_RESTART is ignored (assumed set): the restart is unconditional.
1321 tprints("= ? ERESTARTNOINTR (To be restarted)");
1323 case ERESTARTNOHAND:
1324 /* pause(), rt_sigsuspend() etc use this code.
1325 * SA_RESTART is ignored (assumed not set):
1326 * syscall won't restart (will return EINTR instead)
1327 * even after signal with SA_RESTART set. However,
1328 * after SIG_IGN or SIG_DFL signal it will restart
1329 * (thus the name "restart only if has no handler").
1331 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
1333 case ERESTART_RESTARTBLOCK:
1334 /* Syscalls like nanosleep(), poll() which can't be
1335 * restarted with their original arguments use this
1336 * code. Kernel will execute restart_syscall() instead,
1337 * which changes arguments before restarting syscall.
1338 * SA_RESTART is ignored (assumed not set) similarly
1339 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
1340 * since restart data is saved in "restart block"
1341 * in task struct, and if signal handler uses a syscall
1342 * which in turn saves another such restart block,
1343 * old data is lost and restart becomes impossible)
1345 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
1348 u_error_str = err_name(u_error);
1350 tprintf("= -1 %s (%s)",
1351 u_error_str, strerror(u_error));
1353 tprintf("= -1 %lu (%s)",
1354 u_error, strerror(u_error));
1357 if (syscall_fault_injected(tcp))
1358 tprintf(" (INJECTED)");
1359 if ((sys_res & RVAL_STR) && tcp->auxstr)
1360 tprintf(" (%s)", tcp->auxstr);
1363 if (sys_res & RVAL_NONE)
1366 switch (sys_res & RVAL_MASK) {
1368 #if SUPPORTED_PERSONALITIES > 1
1369 if (current_wordsize < sizeof(long))
1371 (unsigned int) tcp->u_rval);
1374 tprintf("= %#lx", tcp->u_rval);
1378 print_numeric_long_umask(tcp->u_rval);
1381 #if SUPPORTED_PERSONALITIES > 1
1382 if (current_wordsize < sizeof(long))
1384 (unsigned int) tcp->u_rval);
1387 tprintf("= %lu", tcp->u_rval);
1390 tprintf("= %ld", tcp->u_rval);
1395 printfd(tcp, tcp->u_rval);
1398 tprintf("= %ld", tcp->u_rval);
1400 #if HAVE_STRUCT_TCB_EXT_ARG
1403 tprintf("= %#llx", tcp->u_lrval);
1406 tprintf("= %#llo", tcp->u_lrval);
1409 case RVAL_LUDECIMAL:
1410 tprintf("= %llu", tcp->u_lrval);
1414 tprintf("= %lld", tcp->u_lrval);
1417 #endif /* HAVE_STRUCT_TCB_EXT_ARG */
1419 error_msg("invalid rval format");
1423 if ((sys_res & RVAL_STR) && tcp->auxstr)
1424 tprintf(" (%s)", tcp->auxstr);
1427 tv_sub(&tv, &tv, &tcp->etime);
1428 tprintf(" <%ld.%06ld>",
1429 (long) tv.tv_sec, (long) tv.tv_usec);
1435 #ifdef USE_LIBUNWIND
1436 if (stack_trace_enabled)
1437 unwind_print_stacktrace(tcp);
1441 tcp->flags &= ~(TCB_INSYSCALL | TCB_FAULT_INJ);
1442 tcp->sys_func_rval = 0;
1443 free_tcb_priv_data(tcp);
1448 trace_syscall(struct tcb *tcp)
1450 return exiting(tcp) ?
1451 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
1455 is_erestart(struct tcb *tcp)
1457 switch (tcp->u_error) {
1459 case ERESTARTNOINTR:
1460 case ERESTARTNOHAND:
1461 case ERESTART_RESTARTBLOCK:
1468 static unsigned long saved_u_error;
1471 temporarily_clear_syserror(struct tcb *tcp)
1473 saved_u_error = tcp->u_error;
1478 restore_cleared_syserror(struct tcb *tcp)
1480 tcp->u_error = saved_u_error;
1483 #include "kernel_types.h"
1486 * Check the syscall return value register value for whether it is
1487 * a negated errno code indicating an error, or a success return value.
1490 is_negated_errno(kernel_ulong_t val)
1492 /* Linux kernel defines MAX_ERRNO to 4095. */
1493 kernel_ulong_t max = -(kernel_long_t) 4095;
1495 #if defined X86_64 || defined X32
1497 * current_wordsize is 4 for x32 personality
1498 * but truncation _must not_ be done in it, so
1499 * check current_personality instead.
1501 if (current_personality == 1) {
1502 val = (uint32_t) val;
1503 max = (uint32_t) max;
1505 #elif SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1506 if (current_wordsize < sizeof(val)) {
1507 val = (uint32_t) val;
1508 max = (uint32_t) max;
1515 #include "arch_regs.c"
1517 #ifdef HAVE_GETRVAL2
1518 # include "arch_getrval2.c"
1522 print_pc(struct tcb *tcp)
1524 #if defined ARCH_PC_REG
1525 # define ARCH_GET_PC 0
1526 #elif defined ARCH_PC_PEEK_ADDR
1528 # define ARCH_PC_REG pc
1529 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1531 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1533 if (get_regs_error || ARCH_GET_PC)
1534 tprints(current_wordsize == 4 ? "[????????] "
1535 : "[????????????????] ");
1537 tprintf(current_wordsize == 4 ? "[%08lx] " : "[%016lx] ",
1538 (unsigned long) ARCH_PC_REG);
1541 #include "getregs_old.h"
1543 #undef ptrace_getregset_or_getregs
1544 #undef ptrace_setregset_or_setregs
1545 #ifdef ARCH_REGS_FOR_GETREGSET
1547 # define ptrace_getregset_or_getregs ptrace_getregset
1549 ptrace_getregset(pid_t pid)
1551 # ifdef ARCH_IOVEC_FOR_GETREGSET
1552 /* variable iovec */
1553 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1554 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1555 &ARCH_IOVEC_FOR_GETREGSET);
1557 /* constant iovec */
1558 static struct iovec io = {
1559 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1560 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1562 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1567 # ifndef HAVE_GETREGS_OLD
1568 # define ptrace_setregset_or_setregs ptrace_setregset
1570 ptrace_setregset(pid_t pid)
1572 # ifdef ARCH_IOVEC_FOR_GETREGSET
1573 /* variable iovec */
1574 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1575 &ARCH_IOVEC_FOR_GETREGSET);
1577 /* constant iovec */
1578 static struct iovec io = {
1579 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1580 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1582 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1585 # endif /* !HAVE_GETREGS_OLD */
1587 #elif defined ARCH_REGS_FOR_GETREGS
1589 # define ptrace_getregset_or_getregs ptrace_getregs
1591 ptrace_getregs(pid_t pid)
1593 # if defined SPARC || defined SPARC64
1594 /* SPARC systems have the meaning of data and addr reversed */
1595 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1597 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1601 # ifndef HAVE_GETREGS_OLD
1602 # define ptrace_setregset_or_setregs ptrace_setregs
1604 ptrace_setregs(pid_t pid)
1606 # if defined SPARC || defined SPARC64
1607 /* SPARC systems have the meaning of data and addr reversed */
1608 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1610 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1613 # endif /* !HAVE_GETREGS_OLD */
1615 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1620 #undef USE_GET_SYSCALL_RESULT_REGS
1621 #ifdef ptrace_getregset_or_getregs
1623 # ifdef HAVE_GETREGS_OLD
1625 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1626 * fallback to getregs_old.
1628 static int use_getregs_old;
1629 if (use_getregs_old < 0) {
1630 get_regs_error = ptrace_getregset_or_getregs(pid);
1632 } else if (use_getregs_old == 0) {
1633 get_regs_error = ptrace_getregset_or_getregs(pid);
1634 if (get_regs_error >= 0) {
1635 use_getregs_old = -1;
1638 if (errno == EPERM || errno == ESRCH)
1640 use_getregs_old = 1;
1642 get_regs_error = getregs_old(pid);
1643 # else /* !HAVE_GETREGS_OLD */
1644 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1645 get_regs_error = ptrace_getregset_or_getregs(pid);
1646 # endif /* !HAVE_GETREGS_OLD */
1648 #else /* !ptrace_getregset_or_getregs */
1650 # define USE_GET_SYSCALL_RESULT_REGS 1
1651 # warning get_regs is not implemented for this architecture yet
1654 #endif /* !ptrace_getregset_or_getregs */
1657 #ifdef ptrace_setregset_or_setregs
1661 return ptrace_setregset_or_setregs(pid);
1663 #endif /* ptrace_setregset_or_setregs */
1668 char buf[sizeof("syscall_%lu") + sizeof(long) * 3];
1672 free_sysent_buf(void *ptr)
1674 struct sysent_buf *s = ptr;
1675 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1681 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1682 * 1: ok, continue in trace_syscall_entering().
1683 * other: error, trace_syscall_entering() should print error indicator
1684 * ("????" etc) and bail out.
1687 get_scno(struct tcb *tcp)
1692 int rc = arch_get_scno(tcp);
1696 if (SCNO_IS_VALID(tcp->scno)) {
1697 tcp->s_ent = &sysent[tcp->scno];
1698 tcp->qual_flg = qual_flags[tcp->scno];
1700 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1703 s->ent.nargs = MAX_ARGS;
1704 s->ent.sen = SEN_printargs;
1705 s->ent.sys_func = printargs;
1706 s->ent.sys_name = s->buf;
1707 sprintf(s->buf, "syscall_%lu", shuffle_scno(tcp->scno));
1709 tcp->s_ent = &s->ent;
1710 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1712 set_tcb_priv_data(tcp, s, free_sysent_buf);
1715 error_msg("pid %d invalid syscall %ld", tcp->pid, tcp->scno);
1720 #ifdef USE_GET_SYSCALL_RESULT_REGS
1721 static int get_syscall_result_regs(struct tcb *);
1725 * 1: ok, continue in trace_syscall_exiting().
1726 * -1: error, trace_syscall_exiting() should print error indicator
1727 * ("????" etc) and bail out.
1730 get_syscall_result(struct tcb *tcp)
1732 #ifdef USE_GET_SYSCALL_RESULT_REGS
1733 if (get_syscall_result_regs(tcp))
1737 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1742 #include "get_scno.c"
1743 #include "set_scno.c"
1744 #include "get_syscall_args.c"
1745 #ifdef USE_GET_SYSCALL_RESULT_REGS
1746 # include "get_syscall_result.c"
1748 #include "get_error.c"
1749 #include "set_error.c"
1750 #ifdef HAVE_GETREGS_OLD
1751 # include "getregs_old.c"