• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
21  *
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.
32  */
33 
34 #include "defs.h"
35 #include "native_defs.h"
36 #include <sys/param.h>
37 
38 /* for struct iovec */
39 #include <sys/uio.h>
40 
41 #include "regs.h"
42 #include "ptrace.h"
43 
44 #if defined(SPARC64)
45 # undef PTRACE_GETREGS
46 # define PTRACE_GETREGS PTRACE_GETREGS64
47 # undef PTRACE_SETREGS
48 # define PTRACE_SETREGS PTRACE_SETREGS64
49 #endif
50 
51 #if defined SPARC64
52 # include <asm/psrcompat.h>
53 #elif defined SPARC
54 # include <asm/psr.h>
55 #endif
56 
57 #ifdef IA64
58 # include <asm/rse.h>
59 #endif
60 
61 #ifndef NT_PRSTATUS
62 # define NT_PRSTATUS 1
63 #endif
64 
65 #ifndef NSIG
66 # warning: NSIG is not defined, using 32
67 # define NSIG 32
68 #endif
69 
70 #include "syscall.h"
71 
72 /* Define these shorthand notations to simplify the syscallent files. */
73 #define TD TRACE_DESC
74 #define TF TRACE_FILE
75 #define TI TRACE_IPC
76 #define TN TRACE_NETWORK
77 #define TP TRACE_PROCESS
78 #define TS TRACE_SIGNAL
79 #define TM TRACE_MEMORY
80 #define NF SYSCALL_NEVER_FAILS
81 #define MA MAX_ARGS
82 #define SI STACKTRACE_INVALIDATE_CACHE
83 #define SE STACKTRACE_CAPTURE_ON_ENTER
84 
85 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
86 
87 const struct_sysent sysent0[] = {
88 #include "syscallent.h"
89 };
90 
91 #if SUPPORTED_PERSONALITIES > 1
92 # include PERSONALITY1_INCLUDE_FUNCS
93 static const struct_sysent sysent1[] = {
94 # include "syscallent1.h"
95 };
96 #endif
97 
98 #if SUPPORTED_PERSONALITIES > 2
99 # include PERSONALITY2_INCLUDE_FUNCS
100 static const struct_sysent sysent2[] = {
101 # include "syscallent2.h"
102 };
103 #endif
104 
105 /* Now undef them since short defines cause wicked namespace pollution. */
106 #undef SEN
107 #undef TD
108 #undef TF
109 #undef TI
110 #undef TN
111 #undef TP
112 #undef TS
113 #undef TM
114 #undef NF
115 #undef MA
116 #undef SI
117 #undef SE
118 
119 /*
120  * `ioctlent[012].h' files are automatically generated by the auxiliary
121  * program `ioctlsort', such that the list is sorted by the `code' field.
122  * This has the side-effect of resolving the _IO.. macros into
123  * plain integers, eliminating the need to include here everything
124  * in "/usr/include".
125  */
126 
127 const char *const errnoent0[] = {
128 #include "errnoent.h"
129 };
130 const char *const signalent0[] = {
131 #include "signalent.h"
132 };
133 const struct_ioctlent ioctlent0[] = {
134 #include "ioctlent0.h"
135 };
136 
137 #if SUPPORTED_PERSONALITIES > 1
138 static const char *const errnoent1[] = {
139 # include "errnoent1.h"
140 };
141 static const char *const signalent1[] = {
142 # include "signalent1.h"
143 };
144 static const struct_ioctlent ioctlent1[] = {
145 # include "ioctlent1.h"
146 };
147 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
148 static const struct_printers printers0 = {
149 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
150 };
151 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
152 static const struct_printers printers1 = {
153 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
154 };
155 #endif
156 
157 #if SUPPORTED_PERSONALITIES > 2
158 static const char *const errnoent2[] = {
159 # include "errnoent2.h"
160 };
161 static const char *const signalent2[] = {
162 # include "signalent2.h"
163 };
164 static const struct_ioctlent ioctlent2[] = {
165 # include "ioctlent2.h"
166 };
167 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
168 static const struct_printers printers2 = {
169 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
170 };
171 #endif
172 
173 enum {
174 	nsyscalls0 = ARRAY_SIZE(sysent0)
175 #if SUPPORTED_PERSONALITIES > 1
176 	, nsyscalls1 = ARRAY_SIZE(sysent1)
177 # if SUPPORTED_PERSONALITIES > 2
178 	, nsyscalls2 = ARRAY_SIZE(sysent2)
179 # endif
180 #endif
181 };
182 
183 enum {
184 	nerrnos0 = ARRAY_SIZE(errnoent0)
185 #if SUPPORTED_PERSONALITIES > 1
186 	, nerrnos1 = ARRAY_SIZE(errnoent1)
187 # if SUPPORTED_PERSONALITIES > 2
188 	, nerrnos2 = ARRAY_SIZE(errnoent2)
189 # endif
190 #endif
191 };
192 
193 enum {
194 	nsignals0 = ARRAY_SIZE(signalent0)
195 #if SUPPORTED_PERSONALITIES > 1
196 	, nsignals1 = ARRAY_SIZE(signalent1)
197 # if SUPPORTED_PERSONALITIES > 2
198 	, nsignals2 = ARRAY_SIZE(signalent2)
199 # endif
200 #endif
201 };
202 
203 enum {
204 	nioctlents0 = ARRAY_SIZE(ioctlent0)
205 #if SUPPORTED_PERSONALITIES > 1
206 	, nioctlents1 = ARRAY_SIZE(ioctlent1)
207 # if SUPPORTED_PERSONALITIES > 2
208 	, nioctlents2 = ARRAY_SIZE(ioctlent2)
209 # endif
210 #endif
211 };
212 
213 #if SUPPORTED_PERSONALITIES > 1
214 const struct_sysent *sysent = sysent0;
215 const char *const *errnoent = errnoent0;
216 const char *const *signalent = signalent0;
217 const struct_ioctlent *ioctlent = ioctlent0;
218 const struct_printers *printers = &printers0;
219 #endif
220 
221 unsigned nsyscalls = nsyscalls0;
222 unsigned nerrnos = nerrnos0;
223 unsigned nsignals = nsignals0;
224 unsigned nioctlents = nioctlents0;
225 
226 unsigned num_quals;
227 qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
228 
229 static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
230 	nsyscalls0,
231 #if SUPPORTED_PERSONALITIES > 1
232 	nsyscalls1,
233 #endif
234 #if SUPPORTED_PERSONALITIES > 2
235 	nsyscalls2,
236 #endif
237 };
238 static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
239 	sysent0,
240 #if SUPPORTED_PERSONALITIES > 1
241 	sysent1,
242 #endif
243 #if SUPPORTED_PERSONALITIES > 2
244 	sysent2,
245 #endif
246 };
247 
248 enum {
249 	MAX_NSYSCALLS1 = (nsyscalls0
250 #if SUPPORTED_PERSONALITIES > 1
251 			> nsyscalls1 ? nsyscalls0 : nsyscalls1
252 #endif
253 			),
254 	MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
255 #if SUPPORTED_PERSONALITIES > 2
256 			> nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
257 #endif
258 			),
259 	MAX_NSYSCALLS = MAX_NSYSCALLS2,
260 	/* We are ready for arches with up to 255 signals,
261 	 * even though the largest known signo is on MIPS and it is 128.
262 	 * The number of existing syscalls on all arches is
263 	 * larger that 255 anyway, so it is just a pedantic matter.
264 	 */
265 	MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
266 };
267 
268 #if SUPPORTED_PERSONALITIES > 1
269 unsigned current_personality;
270 
271 # ifndef current_wordsize
272 unsigned current_wordsize;
273 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
274 	PERSONALITY0_WORDSIZE,
275 	PERSONALITY1_WORDSIZE,
276 # if SUPPORTED_PERSONALITIES > 2
277 	PERSONALITY2_WORDSIZE,
278 # endif
279 };
280 # endif
281 
282 void
set_personality(int personality)283 set_personality(int personality)
284 {
285 	nsyscalls = nsyscall_vec[personality];
286 	sysent = sysent_vec[personality];
287 
288 	switch (personality) {
289 	case 0:
290 		errnoent = errnoent0;
291 		nerrnos = nerrnos0;
292 		ioctlent = ioctlent0;
293 		nioctlents = nioctlents0;
294 		signalent = signalent0;
295 		nsignals = nsignals0;
296 		printers = &printers0;
297 		break;
298 
299 	case 1:
300 		errnoent = errnoent1;
301 		nerrnos = nerrnos1;
302 		ioctlent = ioctlent1;
303 		nioctlents = nioctlents1;
304 		signalent = signalent1;
305 		nsignals = nsignals1;
306 		printers = &printers1;
307 		break;
308 
309 # if SUPPORTED_PERSONALITIES > 2
310 	case 2:
311 		errnoent = errnoent2;
312 		nerrnos = nerrnos2;
313 		ioctlent = ioctlent2;
314 		nioctlents = nioctlents2;
315 		signalent = signalent2;
316 		nsignals = nsignals2;
317 		printers = &printers2;
318 		break;
319 # endif
320 	}
321 
322 	current_personality = personality;
323 # ifndef current_wordsize
324 	current_wordsize = personality_wordsize[personality];
325 # endif
326 }
327 
328 static void
update_personality(struct tcb * tcp,unsigned int personality)329 update_personality(struct tcb *tcp, unsigned int personality)
330 {
331 	if (personality == current_personality)
332 		return;
333 	set_personality(personality);
334 
335 	if (personality == tcp->currpers)
336 		return;
337 	tcp->currpers = personality;
338 
339 # undef PERSONALITY_NAMES
340 # if defined POWERPC64
341 #  define PERSONALITY_NAMES {"64 bit", "32 bit"}
342 # elif defined X86_64
343 #  define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
344 # elif defined X32
345 #  define PERSONALITY_NAMES {"x32", "32 bit"}
346 # elif defined AARCH64
347 #  define PERSONALITY_NAMES {"64 bit", "32 bit"}
348 # elif defined TILE
349 #  define PERSONALITY_NAMES {"64-bit", "32-bit"}
350 # endif
351 # ifdef PERSONALITY_NAMES
352 	if (!qflag) {
353 		static const char *const names[] = PERSONALITY_NAMES;
354 		error_msg("[ Process PID=%d runs in %s mode. ]",
355 			  tcp->pid, names[personality]);
356 	}
357 # endif
358 }
359 #endif
360 
361 static int qual_syscall(), qual_signal(), qual_desc();
362 
363 static const struct qual_options {
364 	unsigned int bitflag;
365 	const char *option_name;
366 	int (*qualify)(const char *, int, int);
367 	const char *argument_name;
368 } qual_options[] = {
369 	{ QUAL_TRACE,	"trace",	qual_syscall,	"system call"	},
370 	{ QUAL_TRACE,	"t",		qual_syscall,	"system call"	},
371 	{ QUAL_ABBREV,	"abbrev",	qual_syscall,	"system call"	},
372 	{ QUAL_ABBREV,	"a",		qual_syscall,	"system call"	},
373 	{ QUAL_VERBOSE,	"verbose",	qual_syscall,	"system call"	},
374 	{ QUAL_VERBOSE,	"v",		qual_syscall,	"system call"	},
375 	{ QUAL_RAW,	"raw",		qual_syscall,	"system call"	},
376 	{ QUAL_RAW,	"x",		qual_syscall,	"system call"	},
377 	{ QUAL_SIGNAL,	"signal",	qual_signal,	"signal"	},
378 	{ QUAL_SIGNAL,	"signals",	qual_signal,	"signal"	},
379 	{ QUAL_SIGNAL,	"s",		qual_signal,	"signal"	},
380 	{ QUAL_READ,	"read",		qual_desc,	"descriptor"	},
381 	{ QUAL_READ,	"reads",	qual_desc,	"descriptor"	},
382 	{ QUAL_READ,	"r",		qual_desc,	"descriptor"	},
383 	{ QUAL_WRITE,	"write",	qual_desc,	"descriptor"	},
384 	{ QUAL_WRITE,	"writes",	qual_desc,	"descriptor"	},
385 	{ QUAL_WRITE,	"w",		qual_desc,	"descriptor"	},
386 	{ 0,		NULL,		NULL,		NULL		},
387 };
388 
389 static void
reallocate_qual(const unsigned int n)390 reallocate_qual(const unsigned int n)
391 {
392 	unsigned p;
393 	qualbits_t *qp;
394 	for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
395 		qp = qual_vec[p] = xreallocarray(qual_vec[p], n,
396 						 sizeof(qualbits_t));
397 		memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
398 	}
399 	num_quals = n;
400 }
401 
402 static void
qualify_one(const unsigned int n,unsigned int bitflag,const int not,const int pers)403 qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
404 {
405 	int p;
406 
407 	if (num_quals <= n)
408 		reallocate_qual(n + 1);
409 
410 	for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
411 		if (pers == p || pers < 0) {
412 			if (not)
413 				qual_vec[p][n] &= ~bitflag;
414 			else
415 				qual_vec[p][n] |= bitflag;
416 		}
417 	}
418 }
419 
420 static int
qual_syscall(const char * s,const unsigned int bitflag,const int not)421 qual_syscall(const char *s, const unsigned int bitflag, const int not)
422 {
423 	int p;
424 	unsigned int i;
425 	int rc = -1;
426 
427 	if (*s >= '0' && *s <= '9') {
428 		i = string_to_uint(s);
429 		if (i >= MAX_NSYSCALLS)
430 			return -1;
431 		qualify_one(i, bitflag, not, -1);
432 		return 0;
433 	}
434 
435 	for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
436 		for (i = 0; i < nsyscall_vec[p]; i++) {
437 			if (sysent_vec[p][i].sys_name
438 			 && strcmp(s, sysent_vec[p][i].sys_name) == 0
439 			) {
440 				qualify_one(i, bitflag, not, p);
441 				rc = 0;
442 			}
443 		}
444 	}
445 
446 	return rc;
447 }
448 
449 static int
qual_signal(const char * s,const unsigned int bitflag,const int not)450 qual_signal(const char *s, const unsigned int bitflag, const int not)
451 {
452 	unsigned int i;
453 
454 	if (*s >= '0' && *s <= '9') {
455 		int signo = string_to_uint(s);
456 		if (signo < 0 || signo > 255)
457 			return -1;
458 		qualify_one(signo, bitflag, not, -1);
459 		return 0;
460 	}
461 	if (strncasecmp(s, "SIG", 3) == 0)
462 		s += 3;
463 	for (i = 0; i <= NSIG; i++) {
464 		if (strcasecmp(s, signame(i) + 3) == 0) {
465 			qualify_one(i, bitflag, not, -1);
466 			return 0;
467 		}
468 	}
469 	return -1;
470 }
471 
472 static int
qual_desc(const char * s,const unsigned int bitflag,const int not)473 qual_desc(const char *s, const unsigned int bitflag, const int not)
474 {
475 	if (*s >= '0' && *s <= '9') {
476 		int desc = string_to_uint(s);
477 		if (desc < 0 || desc > 0x7fff) /* paranoia */
478 			return -1;
479 		qualify_one(desc, bitflag, not, -1);
480 		return 0;
481 	}
482 	return -1;
483 }
484 
485 static int
lookup_class(const char * s)486 lookup_class(const char *s)
487 {
488 	if (strcmp(s, "file") == 0)
489 		return TRACE_FILE;
490 	if (strcmp(s, "ipc") == 0)
491 		return TRACE_IPC;
492 	if (strcmp(s, "network") == 0)
493 		return TRACE_NETWORK;
494 	if (strcmp(s, "process") == 0)
495 		return TRACE_PROCESS;
496 	if (strcmp(s, "signal") == 0)
497 		return TRACE_SIGNAL;
498 	if (strcmp(s, "desc") == 0)
499 		return TRACE_DESC;
500 	if (strcmp(s, "memory") == 0)
501 		return TRACE_MEMORY;
502 	return -1;
503 }
504 
505 void
qualify(const char * s)506 qualify(const char *s)
507 {
508 	const struct qual_options *opt;
509 	char *copy;
510 	const char *p;
511 	int not;
512 	unsigned int i;
513 
514 	if (num_quals == 0)
515 		reallocate_qual(MIN_QUALS);
516 
517 	opt = &qual_options[0];
518 	for (i = 0; (p = qual_options[i].option_name); i++) {
519 		unsigned int len = strlen(p);
520 		if (strncmp(s, p, len) == 0 && s[len] == '=') {
521 			opt = &qual_options[i];
522 			s += len + 1;
523 			break;
524 		}
525 	}
526 	not = 0;
527 	if (*s == '!') {
528 		not = 1;
529 		s++;
530 	}
531 	if (strcmp(s, "none") == 0) {
532 		not = 1 - not;
533 		s = "all";
534 	}
535 	if (strcmp(s, "all") == 0) {
536 		for (i = 0; i < num_quals; i++) {
537 			qualify_one(i, opt->bitflag, not, -1);
538 		}
539 		return;
540 	}
541 	for (i = 0; i < num_quals; i++) {
542 		qualify_one(i, opt->bitflag, !not, -1);
543 	}
544 	copy = xstrdup(s);
545 	for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
546 		int n;
547 		if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
548 			unsigned pers;
549 			for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
550 				for (i = 0; i < nsyscall_vec[pers]; i++)
551 					if (sysent_vec[pers][i].sys_flags & n)
552 						qualify_one(i, opt->bitflag, not, pers);
553 			}
554 			continue;
555 		}
556 		if (opt->qualify(p, opt->bitflag, not)) {
557 			error_msg_and_die("invalid %s '%s'",
558 				opt->argument_name, p);
559 		}
560 	}
561 	free(copy);
562 	return;
563 }
564 
565 #ifdef SYS_socket_subcall
566 static void
decode_socket_subcall(struct tcb * tcp)567 decode_socket_subcall(struct tcb *tcp)
568 {
569 	unsigned long addr;
570 	unsigned int n;
571 
572 	if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
573 		return;
574 
575 	tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
576 	tcp->qual_flg = qual_flags[tcp->scno];
577 	tcp->s_ent = &sysent[tcp->scno];
578 	addr = tcp->u_arg[1];
579 	n = tcp->s_ent->nargs;
580 	if (sizeof(tcp->u_arg[0]) == current_wordsize) {
581 		memset(tcp->u_arg, 0, n * sizeof(tcp->u_arg[0]));
582 		(void) umoven(tcp, addr, n * sizeof(tcp->u_arg[0]), tcp->u_arg);
583 	} else {
584 		unsigned int args[n];
585 		unsigned int i;
586 
587 		memset(args, 0, sizeof(args));
588 		(void) umove(tcp, addr, &args);
589 		for (i = 0; i < n; ++i)
590 			tcp->u_arg[i] = args[i];
591 	}
592 }
593 #endif
594 
595 #ifdef SYS_ipc_subcall
596 static void
decode_ipc_subcall(struct tcb * tcp)597 decode_ipc_subcall(struct tcb *tcp)
598 {
599 	unsigned int i, n;
600 
601 	if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
602 		return;
603 
604 	tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
605 	tcp->qual_flg = qual_flags[tcp->scno];
606 	tcp->s_ent = &sysent[tcp->scno];
607 	n = tcp->s_ent->nargs;
608 	for (i = 0; i < n; i++)
609 		tcp->u_arg[i] = tcp->u_arg[i + 1];
610 }
611 #endif
612 
613 #ifdef LINUX_MIPSO32
614 static void
decode_mips_subcall(struct tcb * tcp)615 decode_mips_subcall(struct tcb *tcp)
616 {
617 	if (!SCNO_IS_VALID(tcp->u_arg[0]))
618 		return;
619 	tcp->scno = tcp->u_arg[0];
620 	tcp->qual_flg = qual_flags[tcp->scno];
621 	tcp->s_ent = &sysent[tcp->scno];
622 	memmove(&tcp->u_arg[0], &tcp->u_arg[1],
623 		sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
624 	/*
625 	 * Fetching the last arg of 7-arg syscalls (fadvise64_64
626 	 * and sync_file_range) would require additional code,
627 	 * see linux/mips/get_syscall_args.c
628 	 */
629 }
630 
SYS_FUNC(syscall)631 SYS_FUNC(syscall)
632 {
633 	return printargs(tcp);
634 }
635 #endif
636 
637 int
printargs(struct tcb * tcp)638 printargs(struct tcb *tcp)
639 {
640 	if (entering(tcp)) {
641 		int i;
642 		int n = tcp->s_ent->nargs;
643 		for (i = 0; i < n; i++)
644 			tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
645 	}
646 	return 0;
647 }
648 
649 int
printargs_lu(struct tcb * tcp)650 printargs_lu(struct tcb *tcp)
651 {
652 	if (entering(tcp)) {
653 		int i;
654 		int n = tcp->s_ent->nargs;
655 		for (i = 0; i < n; i++)
656 			tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
657 	}
658 	return 0;
659 }
660 
661 int
printargs_ld(struct tcb * tcp)662 printargs_ld(struct tcb *tcp)
663 {
664 	if (entering(tcp)) {
665 		int i;
666 		int n = tcp->s_ent->nargs;
667 		for (i = 0; i < n; i++)
668 			tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
669 	}
670 	return 0;
671 }
672 
673 static void
dumpio(struct tcb * tcp)674 dumpio(struct tcb *tcp)
675 {
676 	int sen;
677 
678 	if (syserror(tcp))
679 		return;
680 	if ((unsigned long) tcp->u_arg[0] >= num_quals)
681 		return;
682 	sen = tcp->s_ent->sen;
683 	if (SEN_printargs == sen)
684 		return;
685 	if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
686 		switch (sen) {
687 		case SEN_read:
688 		case SEN_pread:
689 		case SEN_recv:
690 		case SEN_recvfrom:
691 			dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
692 			return;
693 		case SEN_readv:
694 			dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
695 			return;
696 		case SEN_recvmsg:
697 			dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
698 			return;
699 		case SEN_recvmmsg:
700 			dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
701 			return;
702 		}
703 	}
704 	if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
705 		switch (sen) {
706 		case SEN_write:
707 		case SEN_pwrite:
708 		case SEN_send:
709 		case SEN_sendto:
710 			dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
711 			break;
712 		case SEN_writev:
713 			dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
714 			break;
715 		case SEN_sendmsg:
716 			dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
717 			break;
718 		case SEN_sendmmsg:
719 			dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
720 			break;
721 		}
722 	}
723 }
724 
725 /*
726  * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
727  * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
728  */
729 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
730 static long
shuffle_scno(unsigned long scno)731 shuffle_scno(unsigned long scno)
732 {
733 	if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
734 		return scno;
735 
736 	/* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
737 	if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
738 		return 0x000ffff0;
739 	if (scno == 0x000ffff0)
740 		return ARM_FIRST_SHUFFLED_SYSCALL;
741 
742 #define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
743 	/*
744 	 * Is it ARM specific syscall?
745 	 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
746 	 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
747 	 */
748 	if (scno >= 0x000f0000 &&
749 	    scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
750 		return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
751 	}
752 	if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
753 		return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
754 	}
755 
756 	return scno;
757 }
758 #else
759 # define shuffle_scno(scno) ((long)(scno))
760 #endif
761 
762 const char *
syscall_name(long scno)763 syscall_name(long scno)
764 {
765 	static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
766 
767 	if (SCNO_IS_VALID(scno))
768 		return sysent[scno].sys_name;
769 	else {
770 		sprintf(buf, "syscall_%lu", shuffle_scno(scno));
771 		return buf;
772 	}
773 }
774 
775 static long get_regs_error;
776 
777 void
clear_regs(void)778 clear_regs(void)
779 {
780 	get_regs_error = -1;
781 }
782 
783 static int get_syscall_args(struct tcb *);
784 static int get_syscall_result(struct tcb *);
785 static int arch_get_scno(struct tcb *tcp);
786 static void get_error(struct tcb *, const bool);
787 #if defined X86_64 || defined POWERPC
788 static int getregs_old(pid_t);
789 #endif
790 
791 static int
trace_syscall_entering(struct tcb * tcp)792 trace_syscall_entering(struct tcb *tcp)
793 {
794 	int res, scno_good;
795 
796 	scno_good = res = get_scno(tcp);
797 	if (res == 0)
798 		return res;
799 	if (res == 1)
800 		res = get_syscall_args(tcp);
801 
802 	if (res != 1) {
803 		printleader(tcp);
804 		if (scno_good != 1)
805 			tprints("????" /* anti-trigraph gap */ "(");
806 		else if (tcp->qual_flg & UNDEFINED_SCNO)
807 			tprintf("%s(", syscall_name(tcp->scno));
808 		else
809 			tprintf("%s(", tcp->s_ent->sys_name);
810 		/*
811 		 * " <unavailable>" will be added later by the code which
812 		 * detects ptrace errors.
813 		 */
814 		goto ret;
815 	}
816 
817 #ifdef LINUX_MIPSO32
818 	if (SEN_syscall == tcp->s_ent->sen)
819 		decode_mips_subcall(tcp);
820 #endif
821 
822 	if (   SEN_execve == tcp->s_ent->sen
823 # if defined(SPARC) || defined(SPARC64)
824 	    || SEN_execv == tcp->s_ent->sen
825 # endif
826 	   ) {
827 		hide_log_until_execve = 0;
828 	}
829 
830 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
831 	switch (tcp->s_ent->sen) {
832 # ifdef SYS_socket_subcall
833 		case SEN_socketcall:
834 			decode_socket_subcall(tcp);
835 			break;
836 # endif
837 # ifdef SYS_ipc_subcall
838 		case SEN_ipc:
839 			decode_ipc_subcall(tcp);
840 			break;
841 # endif
842 	}
843 #endif
844 
845 	if (!(tcp->qual_flg & QUAL_TRACE)
846 	 || (tracing_paths && !pathtrace_match(tcp))
847 	) {
848 		tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
849 		tcp->sys_func_rval = 0;
850 		return 0;
851 	}
852 
853 	tcp->flags &= ~TCB_FILTERED;
854 
855 	if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
856 		res = 0;
857 		goto ret;
858 	}
859 
860 #ifdef USE_LIBUNWIND
861 	if (stack_trace_enabled) {
862 		if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
863 			unwind_capture_stacktrace(tcp);
864 	}
865 #endif
866 
867 	printleader(tcp);
868 	if (tcp->qual_flg & UNDEFINED_SCNO)
869 		tprintf("%s(", syscall_name(tcp->scno));
870 	else
871 		tprintf("%s(", tcp->s_ent->sys_name);
872 	if ((tcp->qual_flg & QUAL_RAW) && SEN_exit != tcp->s_ent->sen)
873 		res = printargs(tcp);
874 	else
875 		res = tcp->s_ent->sys_func(tcp);
876 
877 	fflush(tcp->outf);
878  ret:
879 	tcp->flags |= TCB_INSYSCALL;
880 	tcp->sys_func_rval = res;
881 	/* Measure the entrance time as late as possible to avoid errors. */
882 	if (Tflag || cflag)
883 		gettimeofday(&tcp->etime, NULL);
884 	return res;
885 }
886 
887 static int
trace_syscall_exiting(struct tcb * tcp)888 trace_syscall_exiting(struct tcb *tcp)
889 {
890 	int sys_res;
891 	struct timeval tv;
892 	int res;
893 	long u_error;
894 
895 	/* Measure the exit time as early as possible to avoid errors. */
896 	if (Tflag || cflag)
897 		gettimeofday(&tv, NULL);
898 
899 #ifdef USE_LIBUNWIND
900 	if (stack_trace_enabled) {
901 		if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
902 			unwind_cache_invalidate(tcp);
903 	}
904 #endif
905 
906 #if SUPPORTED_PERSONALITIES > 1
907 	update_personality(tcp, tcp->currpers);
908 #endif
909 	res = (get_regs_error ? -1 : get_syscall_result(tcp));
910 	if (filtered(tcp) || hide_log_until_execve)
911 		goto ret;
912 
913 	if (cflag) {
914 		count_syscall(tcp, &tv);
915 		if (cflag == CFLAG_ONLY_STATS) {
916 			goto ret;
917 		}
918 	}
919 
920 	/* If not in -ff mode, and printing_tcp != tcp,
921 	 * then the log currently does not end with output
922 	 * of _our syscall entry_, but with something else.
923 	 * We need to say which syscall's return is this.
924 	 *
925 	 * Forced reprinting via TCB_REPRINT is used only by
926 	 * "strace -ff -oLOG test/threaded_execve" corner case.
927 	 * It's the only case when -ff mode needs reprinting.
928 	 */
929 	if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
930 		tcp->flags &= ~TCB_REPRINT;
931 		printleader(tcp);
932 		if (tcp->qual_flg & UNDEFINED_SCNO)
933 			tprintf("<... %s resumed> ", syscall_name(tcp->scno));
934 		else
935 			tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
936 	}
937 	printing_tcp = tcp;
938 
939 	tcp->s_prev_ent = NULL;
940 	if (res != 1) {
941 		/* There was error in one of prior ptrace ops */
942 		tprints(") ");
943 		tabto();
944 		tprints("= ? <unavailable>\n");
945 		line_ended();
946 		tcp->flags &= ~TCB_INSYSCALL;
947 		tcp->sys_func_rval = 0;
948 		return res;
949 	}
950 	tcp->s_prev_ent = tcp->s_ent;
951 
952 	sys_res = 0;
953 	if (tcp->qual_flg & QUAL_RAW) {
954 		/* sys_res = printargs(tcp); - but it's nop on sysexit */
955 	} else {
956 	/* FIXME: not_failing_only (IOW, option -z) is broken:
957 	 * failure of syscall is known only after syscall return.
958 	 * Thus we end up with something like this on, say, ENOENT:
959 	 *     open("doesnt_exist", O_RDONLY <unfinished ...>
960 	 *     {next syscall decode}
961 	 * whereas the intended result is that open(...) line
962 	 * is not shown at all.
963 	 */
964 		if (not_failing_only && tcp->u_error)
965 			goto ret;	/* ignore failed syscalls */
966 		if (tcp->sys_func_rval & RVAL_DECODED)
967 			sys_res = tcp->sys_func_rval;
968 		else
969 			sys_res = tcp->s_ent->sys_func(tcp);
970 	}
971 
972 	tprints(") ");
973 	tabto();
974 	u_error = tcp->u_error;
975 	if (tcp->qual_flg & QUAL_RAW) {
976 		if (u_error)
977 			tprintf("= -1 (errno %ld)", u_error);
978 		else
979 			tprintf("= %#lx", tcp->u_rval);
980 	}
981 	else if (!(sys_res & RVAL_NONE) && u_error) {
982 		switch (u_error) {
983 		/* Blocked signals do not interrupt any syscalls.
984 		 * In this case syscalls don't return ERESTARTfoo codes.
985 		 *
986 		 * Deadly signals set to SIG_DFL interrupt syscalls
987 		 * and kill the process regardless of which of the codes below
988 		 * is returned by the interrupted syscall.
989 		 * In some cases, kernel forces a kernel-generated deadly
990 		 * signal to be unblocked and set to SIG_DFL (and thus cause
991 		 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
992 		 * or SIGILL. (The alternative is to leave process spinning
993 		 * forever on the faulty instruction - not useful).
994 		 *
995 		 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
996 		 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
997 		 * but kernel will always restart them.
998 		 */
999 		case ERESTARTSYS:
1000 			/* Most common type of signal-interrupted syscall exit code.
1001 			 * The system call will be restarted with the same arguments
1002 			 * if SA_RESTART is set; otherwise, it will fail with EINTR.
1003 			 */
1004 			tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
1005 			break;
1006 		case ERESTARTNOINTR:
1007 			/* Rare. For example, fork() returns this if interrupted.
1008 			 * SA_RESTART is ignored (assumed set): the restart is unconditional.
1009 			 */
1010 			tprints("= ? ERESTARTNOINTR (To be restarted)");
1011 			break;
1012 		case ERESTARTNOHAND:
1013 			/* pause(), rt_sigsuspend() etc use this code.
1014 			 * SA_RESTART is ignored (assumed not set):
1015 			 * syscall won't restart (will return EINTR instead)
1016 			 * even after signal with SA_RESTART set. However,
1017 			 * after SIG_IGN or SIG_DFL signal it will restart
1018 			 * (thus the name "restart only if has no handler").
1019 			 */
1020 			tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
1021 			break;
1022 		case ERESTART_RESTARTBLOCK:
1023 			/* Syscalls like nanosleep(), poll() which can't be
1024 			 * restarted with their original arguments use this
1025 			 * code. Kernel will execute restart_syscall() instead,
1026 			 * which changes arguments before restarting syscall.
1027 			 * SA_RESTART is ignored (assumed not set) similarly
1028 			 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
1029 			 * since restart data is saved in "restart block"
1030 			 * in task struct, and if signal handler uses a syscall
1031 			 * which in turn saves another such restart block,
1032 			 * old data is lost and restart becomes impossible)
1033 			 */
1034 			tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
1035 			break;
1036 		default:
1037 			if ((unsigned long) u_error < nerrnos
1038 			    && errnoent[u_error])
1039 				tprintf("= -1 %s (%s)", errnoent[u_error],
1040 					strerror(u_error));
1041 			else
1042 				tprintf("= -1 ERRNO_%lu (%s)", u_error,
1043 					strerror(u_error));
1044 			break;
1045 		}
1046 		if ((sys_res & RVAL_STR) && tcp->auxstr)
1047 			tprintf(" (%s)", tcp->auxstr);
1048 	}
1049 	else {
1050 		if (sys_res & RVAL_NONE)
1051 			tprints("= ?");
1052 		else {
1053 			switch (sys_res & RVAL_MASK) {
1054 			case RVAL_HEX:
1055 #if SUPPORTED_PERSONALITIES > 1
1056 				if (current_wordsize < sizeof(long))
1057 					tprintf("= %#x",
1058 						(unsigned int) tcp->u_rval);
1059 				else
1060 #endif
1061 					tprintf("= %#lx", tcp->u_rval);
1062 				break;
1063 			case RVAL_OCTAL:
1064 				tprintf("= %#lo", tcp->u_rval);
1065 				break;
1066 			case RVAL_UDECIMAL:
1067 				tprintf("= %lu", tcp->u_rval);
1068 				break;
1069 			case RVAL_DECIMAL:
1070 				tprintf("= %ld", tcp->u_rval);
1071 				break;
1072 			case RVAL_FD:
1073 				if (show_fd_path) {
1074 					tprints("= ");
1075 					printfd(tcp, tcp->u_rval);
1076 				}
1077 				else
1078 					tprintf("= %ld", tcp->u_rval);
1079 				break;
1080 #if defined(LINUX_MIPSN32) || defined(X32)
1081 			/*
1082 			case RVAL_LHEX:
1083 				tprintf("= %#llx", tcp->u_lrval);
1084 				break;
1085 			case RVAL_LOCTAL:
1086 				tprintf("= %#llo", tcp->u_lrval);
1087 				break;
1088 			*/
1089 			case RVAL_LUDECIMAL:
1090 				tprintf("= %llu", tcp->u_lrval);
1091 				break;
1092 			/*
1093 			case RVAL_LDECIMAL:
1094 				tprintf("= %lld", tcp->u_lrval);
1095 				break;
1096 			*/
1097 #endif
1098 			default:
1099 				error_msg("invalid rval format");
1100 				break;
1101 			}
1102 		}
1103 		if ((sys_res & RVAL_STR) && tcp->auxstr)
1104 			tprintf(" (%s)", tcp->auxstr);
1105 	}
1106 	if (Tflag) {
1107 		tv_sub(&tv, &tv, &tcp->etime);
1108 		tprintf(" <%ld.%06ld>",
1109 			(long) tv.tv_sec, (long) tv.tv_usec);
1110 	}
1111 	tprints("\n");
1112 	dumpio(tcp);
1113 	line_ended();
1114 
1115 #ifdef USE_LIBUNWIND
1116 	if (stack_trace_enabled)
1117 		unwind_print_stacktrace(tcp);
1118 #endif
1119 
1120  ret:
1121 	tcp->flags &= ~TCB_INSYSCALL;
1122 	tcp->sys_func_rval = 0;
1123 	return 0;
1124 }
1125 
1126 int
trace_syscall(struct tcb * tcp)1127 trace_syscall(struct tcb *tcp)
1128 {
1129 	return exiting(tcp) ?
1130 		trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
1131 }
1132 
1133 bool
is_erestart(struct tcb * tcp)1134 is_erestart(struct tcb *tcp)
1135 {
1136 	switch (tcp->u_error) {
1137 		case ERESTARTSYS:
1138 		case ERESTARTNOINTR:
1139 		case ERESTARTNOHAND:
1140 		case ERESTART_RESTARTBLOCK:
1141 			return true;
1142 		default:
1143 			return false;
1144 	}
1145 }
1146 
1147 static int saved_u_error;
1148 
1149 void
temporarily_clear_syserror(struct tcb * tcp)1150 temporarily_clear_syserror(struct tcb *tcp)
1151 {
1152 	saved_u_error = tcp->u_error;
1153 	tcp->u_error = 0;
1154 }
1155 
1156 void
restore_cleared_syserror(struct tcb * tcp)1157 restore_cleared_syserror(struct tcb *tcp)
1158 {
1159 	tcp->u_error = saved_u_error;
1160 }
1161 
1162 #include "kernel_types.h"
1163 
1164 /*
1165  * Check the syscall return value register value for whether it is
1166  * a negated errno code indicating an error, or a success return value.
1167  */
1168 static inline bool
is_negated_errno(kernel_ulong_t val)1169 is_negated_errno(kernel_ulong_t val)
1170 {
1171 	/* Linux kernel defines MAX_ERRNO to 4095. */
1172 	kernel_ulong_t max = -(kernel_long_t) 4095;
1173 
1174 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1175 	if (current_wordsize < sizeof(val)) {
1176 		val = (uint32_t) val;
1177 		max = (uint32_t) max;
1178 	}
1179 #elif defined X32
1180 	/*
1181 	 * current_wordsize is 4 even in personality 0 (native X32)
1182 	 * but truncation _must not_ be done in it.
1183 	 * can't check current_wordsize here!
1184 	 */
1185 	if (current_personality != 0) {
1186 		val = (uint32_t) val;
1187 		max = (uint32_t) max;
1188 	}
1189 #endif
1190 
1191 	return val >= max;
1192 }
1193 
1194 #include "arch_regs.c"
1195 
1196 #ifdef HAVE_GETRVAL2
1197 # include "arch_getrval2.c"
1198 #endif
1199 
1200 void
print_pc(struct tcb * tcp)1201 print_pc(struct tcb *tcp)
1202 {
1203 #if defined ARCH_PC_REG
1204 # define ARCH_GET_PC 0
1205 #elif defined ARCH_PC_PEEK_ADDR
1206 	long pc;
1207 # define ARCH_PC_REG pc
1208 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1209 #else
1210 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1211 #endif
1212 	if (get_regs_error || ARCH_GET_PC)
1213 		tprints(current_wordsize == 4 ? "[????????] "
1214 					      : "[????????????????] ");
1215 	else
1216 		tprintf(current_wordsize == 4 ? "[%08lx] " : "[%016lx] ",
1217 			(unsigned long) ARCH_PC_REG);
1218 }
1219 
1220 #if defined ARCH_REGS_FOR_GETREGSET
1221 static long
get_regset(pid_t pid)1222 get_regset(pid_t pid)
1223 {
1224 # ifdef ARCH_IOVEC_FOR_GETREGSET
1225 	/* variable iovec */
1226 	ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1227 	return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1228 		      &ARCH_IOVEC_FOR_GETREGSET);
1229 # else
1230 	/* constant iovec */
1231 	static struct iovec io = {
1232 		.iov_base = &ARCH_REGS_FOR_GETREGSET,
1233 		.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1234 	};
1235 	return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1236 
1237 # endif
1238 }
1239 #endif /* ARCH_REGS_FOR_GETREGSET */
1240 
1241 void
get_regs(pid_t pid)1242 get_regs(pid_t pid)
1243 {
1244 #undef USE_GET_SYSCALL_RESULT_REGS
1245 #ifdef ARCH_REGS_FOR_GETREGSET
1246 # ifdef X86_64
1247 	/* Try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS. */
1248 	static int getregset_support;
1249 
1250 	if (getregset_support >= 0) {
1251 		get_regs_error = get_regset(pid);
1252 		if (getregset_support > 0)
1253 			return;
1254 		if (get_regs_error >= 0) {
1255 			getregset_support = 1;
1256 			return;
1257 		}
1258 		if (errno == EPERM || errno == ESRCH)
1259 			return;
1260 		getregset_support = -1;
1261 	}
1262 	get_regs_error = getregs_old(pid);
1263 # else /* !X86_64 */
1264 	/* Assume that PTRACE_GETREGSET works. */
1265 	get_regs_error = get_regset(pid);
1266 # endif
1267 #elif defined ARCH_REGS_FOR_GETREGS
1268 # if defined SPARC || defined SPARC64
1269 	/* SPARC systems have the meaning of data and addr reversed */
1270 	get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&ARCH_REGS_FOR_GETREGS, 0);
1271 # elif defined POWERPC
1272 	static bool old_kernel = 0;
1273 	if (old_kernel)
1274 		goto old;
1275 	get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1276 	if (get_regs_error && errno == EIO) {
1277 		old_kernel = 1;
1278  old:
1279 		get_regs_error = getregs_old(pid);
1280 	}
1281 # else
1282 	/* Assume that PTRACE_GETREGS works. */
1283 	get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1284 # endif
1285 
1286 #else /* !ARCH_REGS_FOR_GETREGSET && !ARCH_REGS_FOR_GETREGS */
1287 # define USE_GET_SYSCALL_RESULT_REGS 1
1288 # warning get_regs is not implemented for this architecture yet
1289 	get_regs_error = 0;
1290 #endif
1291 }
1292 
1293 /*
1294  * Returns:
1295  * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1296  * 1: ok, continue in trace_syscall_entering().
1297  * other: error, trace_syscall_entering() should print error indicator
1298  *    ("????" etc) and bail out.
1299  */
1300 int
get_scno(struct tcb * tcp)1301 get_scno(struct tcb *tcp)
1302 {
1303 	if (get_regs_error)
1304 		return -1;
1305 
1306 	int rc = arch_get_scno(tcp);
1307 	if (rc != 1)
1308 		return rc;
1309 
1310 	if (SCNO_IS_VALID(tcp->scno)) {
1311 		tcp->s_ent = &sysent[tcp->scno];
1312 		tcp->qual_flg = qual_flags[tcp->scno];
1313 	} else {
1314 		static const struct_sysent unknown = {
1315 			.nargs = MAX_ARGS,
1316 			.sys_flags = 0,
1317 			.sys_func = printargs,
1318 			.sys_name = "system call",
1319 		};
1320 		tcp->s_ent = &unknown;
1321 		tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1322 		if (debug_flag)
1323 			error_msg("pid %d invalid syscall %ld", tcp->pid, tcp->scno);
1324 	}
1325 	return 1;
1326 }
1327 
1328 #ifdef USE_GET_SYSCALL_RESULT_REGS
1329 static int get_syscall_result_regs(struct tcb *);
1330 #endif
1331 
1332 /* Returns:
1333  * 1: ok, continue in trace_syscall_exiting().
1334  * -1: error, trace_syscall_exiting() should print error indicator
1335  *    ("????" etc) and bail out.
1336  */
1337 static int
get_syscall_result(struct tcb * tcp)1338 get_syscall_result(struct tcb *tcp)
1339 {
1340 #ifdef USE_GET_SYSCALL_RESULT_REGS
1341 	if (get_syscall_result_regs(tcp))
1342 		return -1;
1343 #endif
1344 	tcp->u_error = 0;
1345 	get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1346 
1347 	return 1;
1348 }
1349 
1350 #include "get_scno.c"
1351 #include "get_syscall_args.c"
1352 #ifdef USE_GET_SYSCALL_RESULT_REGS
1353 # include "get_syscall_result.c"
1354 #endif
1355 #include "get_error.c"
1356 #if defined X86_64 || defined POWERPC
1357 # include "getregs_old.c"
1358 #endif
1359