syscall.c revision c956ef085f5685b47bd7a66d0bff5abcb0a50484
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 <sys/user.h> 36#include <sys/param.h> 37 38#ifdef HAVE_SYS_REG_H 39# include <sys/reg.h> 40# ifndef PTRACE_PEEKUSR 41# define PTRACE_PEEKUSR PTRACE_PEEKUSER 42# endif 43#elif defined(HAVE_LINUX_PTRACE_H) 44# undef PTRACE_SYSCALL 45# ifdef HAVE_STRUCT_IA64_FPREG 46# define ia64_fpreg XXX_ia64_fpreg 47# endif 48# ifdef HAVE_STRUCT_PT_ALL_USER_REGS 49# define pt_all_user_regs XXX_pt_all_user_regs 50# endif 51# include <linux/ptrace.h> 52# undef ia64_fpreg 53# undef pt_all_user_regs 54#endif 55 56#if defined(SPARC64) 57# undef PTRACE_GETREGS 58# define PTRACE_GETREGS PTRACE_GETREGS64 59# undef PTRACE_SETREGS 60# define PTRACE_SETREGS PTRACE_SETREGS64 61#endif 62 63#if defined(IA64) 64# include <asm/ptrace_offsets.h> 65# include <asm/rse.h> 66#endif 67 68#if defined(X86_64) || defined(X32) 69# include <linux/ptrace.h> 70# include <sys/uio.h> 71# include <elf.h> 72#endif 73 74#if defined(AARCH64) 75# include <asm/ptrace.h> 76# include <sys/uio.h> 77# include <elf.h> 78#endif 79 80#if defined(OR1K) 81# include <sys/uio.h> 82# include <elf.h> 83#endif 84 85#ifndef ERESTARTSYS 86# define ERESTARTSYS 512 87#endif 88#ifndef ERESTARTNOINTR 89# define ERESTARTNOINTR 513 90#endif 91#ifndef ERESTARTNOHAND 92# define ERESTARTNOHAND 514 /* restart if no handler */ 93#endif 94#ifndef ERESTART_RESTARTBLOCK 95# define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */ 96#endif 97 98#ifndef NSIG 99# warning: NSIG is not defined, using 32 100# define NSIG 32 101#endif 102#ifdef ARM 103/* Ugh. Is this really correct? ARM has no RT signals?! */ 104# undef NSIG 105# define NSIG 32 106#endif 107 108#include "syscall.h" 109 110/* Define these shorthand notations to simplify the syscallent files. */ 111#define TD TRACE_DESC 112#define TF TRACE_FILE 113#define TI TRACE_IPC 114#define TN TRACE_NETWORK 115#define TP TRACE_PROCESS 116#define TS TRACE_SIGNAL 117#define TM TRACE_MEMORY 118#define NF SYSCALL_NEVER_FAILS 119#define MA MAX_ARGS 120 121static const struct sysent sysent0[] = { 122#include "syscallent.h" 123}; 124 125#if SUPPORTED_PERSONALITIES >= 2 126static const struct sysent sysent1[] = { 127# include "syscallent1.h" 128}; 129#endif 130 131#if SUPPORTED_PERSONALITIES >= 3 132static const struct sysent sysent2[] = { 133# include "syscallent2.h" 134}; 135#endif 136 137/* Now undef them since short defines cause wicked namespace pollution. */ 138#undef TD 139#undef TF 140#undef TI 141#undef TN 142#undef TP 143#undef TS 144#undef TM 145#undef NF 146#undef MA 147 148/* 149 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary 150 * program `ioctlsort', such that the list is sorted by the `code' field. 151 * This has the side-effect of resolving the _IO.. macros into 152 * plain integers, eliminating the need to include here everything 153 * in "/usr/include". 154 */ 155 156static const char *const errnoent0[] = { 157#include "errnoent.h" 158}; 159static const char *const signalent0[] = { 160#include "signalent.h" 161}; 162static const struct ioctlent ioctlent0[] = { 163#include "ioctlent.h" 164}; 165enum { nsyscalls0 = ARRAY_SIZE(sysent0) }; 166enum { nerrnos0 = ARRAY_SIZE(errnoent0) }; 167enum { nsignals0 = ARRAY_SIZE(signalent0) }; 168enum { nioctlents0 = ARRAY_SIZE(ioctlent0) }; 169int qual_flags0[MAX_QUALS]; 170 171#if SUPPORTED_PERSONALITIES >= 2 172static const char *const errnoent1[] = { 173# include "errnoent1.h" 174}; 175static const char *const signalent1[] = { 176# include "signalent1.h" 177}; 178static const struct ioctlent ioctlent1[] = { 179# include "ioctlent1.h" 180}; 181enum { nsyscalls1 = ARRAY_SIZE(sysent1) }; 182enum { nerrnos1 = ARRAY_SIZE(errnoent1) }; 183enum { nsignals1 = ARRAY_SIZE(signalent1) }; 184enum { nioctlents1 = ARRAY_SIZE(ioctlent1) }; 185int qual_flags1[MAX_QUALS]; 186#endif 187 188#if SUPPORTED_PERSONALITIES >= 3 189static const char *const errnoent2[] = { 190# include "errnoent2.h" 191}; 192static const char *const signalent2[] = { 193# include "signalent2.h" 194}; 195static const struct ioctlent ioctlent2[] = { 196# include "ioctlent2.h" 197}; 198enum { nsyscalls2 = ARRAY_SIZE(sysent2) }; 199enum { nerrnos2 = ARRAY_SIZE(errnoent2) }; 200enum { nsignals2 = ARRAY_SIZE(signalent2) }; 201enum { nioctlents2 = ARRAY_SIZE(ioctlent2) }; 202int qual_flags2[MAX_QUALS]; 203#endif 204 205const struct sysent *sysent = sysent0; 206const char *const *errnoent = errnoent0; 207const char *const *signalent = signalent0; 208const struct ioctlent *ioctlent = ioctlent0; 209unsigned nsyscalls = nsyscalls0; 210unsigned nerrnos = nerrnos0; 211unsigned nsignals = nsignals0; 212unsigned nioctlents = nioctlents0; 213int *qual_flags = qual_flags0; 214 215#if SUPPORTED_PERSONALITIES > 1 216unsigned current_personality; 217 218# ifndef current_wordsize 219unsigned current_wordsize; 220static const int personality_wordsize[SUPPORTED_PERSONALITIES] = { 221 PERSONALITY0_WORDSIZE, 222 PERSONALITY1_WORDSIZE, 223# if SUPPORTED_PERSONALITIES > 2 224 PERSONALITY2_WORDSIZE, 225# endif 226}; 227# endif 228 229void 230set_personality(int personality) 231{ 232 switch (personality) { 233 case 0: 234 errnoent = errnoent0; 235 nerrnos = nerrnos0; 236 sysent = sysent0; 237 nsyscalls = nsyscalls0; 238 ioctlent = ioctlent0; 239 nioctlents = nioctlents0; 240 signalent = signalent0; 241 nsignals = nsignals0; 242 qual_flags = qual_flags0; 243 break; 244 245 case 1: 246 errnoent = errnoent1; 247 nerrnos = nerrnos1; 248 sysent = sysent1; 249 nsyscalls = nsyscalls1; 250 ioctlent = ioctlent1; 251 nioctlents = nioctlents1; 252 signalent = signalent1; 253 nsignals = nsignals1; 254 qual_flags = qual_flags1; 255 break; 256 257# if SUPPORTED_PERSONALITIES >= 3 258 case 2: 259 errnoent = errnoent2; 260 nerrnos = nerrnos2; 261 sysent = sysent2; 262 nsyscalls = nsyscalls2; 263 ioctlent = ioctlent2; 264 nioctlents = nioctlents2; 265 signalent = signalent2; 266 nsignals = nsignals2; 267 qual_flags = qual_flags2; 268 break; 269# endif 270 } 271 272 current_personality = personality; 273# ifndef current_wordsize 274 current_wordsize = personality_wordsize[personality]; 275# endif 276} 277 278static void 279update_personality(struct tcb *tcp, int personality) 280{ 281 if (personality == current_personality) 282 return; 283 set_personality(personality); 284 285 if (personality == tcp->currpers) 286 return; 287 tcp->currpers = personality; 288 289# if defined(POWERPC64) 290 if (!qflag) { 291 static const char *const names[] = {"64 bit", "32 bit"}; 292 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n", 293 tcp->pid, names[personality]); 294 } 295# elif defined(X86_64) 296 if (!qflag) { 297 static const char *const names[] = {"64 bit", "32 bit", "x32"}; 298 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n", 299 tcp->pid, names[personality]); 300 } 301# elif defined(X32) 302 if (!qflag) { 303 static const char *const names[] = {"x32", "32 bit"}; 304 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n", 305 tcp->pid, names[personality]); 306 } 307# elif defined(AARCH64) 308 if (!qflag) { 309 static const char *const names[] = {"32-bit", "AArch64"}; 310 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n", 311 tcp->pid, names[personality]); 312 } 313# elif defined(TILE) 314 if (!qflag) { 315 static const char *const names[] = {"64-bit", "32-bit"}; 316 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n", 317 tcp->pid, names[personality]); 318 } 319# endif 320} 321#endif 322 323static int qual_syscall(), qual_signal(), qual_fault(), qual_desc(); 324 325static const struct qual_options { 326 int bitflag; 327 const char *option_name; 328 int (*qualify)(const char *, int, int); 329 const char *argument_name; 330} qual_options[] = { 331 { QUAL_TRACE, "trace", qual_syscall, "system call" }, 332 { QUAL_TRACE, "t", qual_syscall, "system call" }, 333 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" }, 334 { QUAL_ABBREV, "a", qual_syscall, "system call" }, 335 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" }, 336 { QUAL_VERBOSE, "v", qual_syscall, "system call" }, 337 { QUAL_RAW, "raw", qual_syscall, "system call" }, 338 { QUAL_RAW, "x", qual_syscall, "system call" }, 339 { QUAL_SIGNAL, "signal", qual_signal, "signal" }, 340 { QUAL_SIGNAL, "signals", qual_signal, "signal" }, 341 { QUAL_SIGNAL, "s", qual_signal, "signal" }, 342 { QUAL_FAULT, "fault", qual_fault, "fault" }, 343 { QUAL_FAULT, "faults", qual_fault, "fault" }, 344 { QUAL_FAULT, "m", qual_fault, "fault" }, 345 { QUAL_READ, "read", qual_desc, "descriptor" }, 346 { QUAL_READ, "reads", qual_desc, "descriptor" }, 347 { QUAL_READ, "r", qual_desc, "descriptor" }, 348 { QUAL_WRITE, "write", qual_desc, "descriptor" }, 349 { QUAL_WRITE, "writes", qual_desc, "descriptor" }, 350 { QUAL_WRITE, "w", qual_desc, "descriptor" }, 351 { 0, NULL, NULL, NULL }, 352}; 353 354static void 355qualify_one(int n, int bitflag, int not, int pers) 356{ 357 if (pers == 0 || pers < 0) { 358 if (not) 359 qual_flags0[n] &= ~bitflag; 360 else 361 qual_flags0[n] |= bitflag; 362 } 363 364#if SUPPORTED_PERSONALITIES >= 2 365 if (pers == 1 || pers < 0) { 366 if (not) 367 qual_flags1[n] &= ~bitflag; 368 else 369 qual_flags1[n] |= bitflag; 370 } 371#endif 372 373#if SUPPORTED_PERSONALITIES >= 3 374 if (pers == 2 || pers < 0) { 375 if (not) 376 qual_flags2[n] &= ~bitflag; 377 else 378 qual_flags2[n] |= bitflag; 379 } 380#endif 381} 382 383static int 384qual_syscall(const char *s, int bitflag, int not) 385{ 386 int i; 387 int rc = -1; 388 389 if (*s >= '0' && *s <= '9') { 390 int i = string_to_uint(s); 391 if (i < 0 || i >= MAX_QUALS) 392 return -1; 393 qualify_one(i, bitflag, not, -1); 394 return 0; 395 } 396 for (i = 0; i < nsyscalls0; i++) 397 if (sysent0[i].sys_name && 398 strcmp(s, sysent0[i].sys_name) == 0) { 399 qualify_one(i, bitflag, not, 0); 400 rc = 0; 401 } 402 403#if SUPPORTED_PERSONALITIES >= 2 404 for (i = 0; i < nsyscalls1; i++) 405 if (sysent1[i].sys_name && 406 strcmp(s, sysent1[i].sys_name) == 0) { 407 qualify_one(i, bitflag, not, 1); 408 rc = 0; 409 } 410#endif 411 412#if SUPPORTED_PERSONALITIES >= 3 413 for (i = 0; i < nsyscalls2; i++) 414 if (sysent2[i].sys_name && 415 strcmp(s, sysent2[i].sys_name) == 0) { 416 qualify_one(i, bitflag, not, 2); 417 rc = 0; 418 } 419#endif 420 421 return rc; 422} 423 424static int 425qual_signal(const char *s, int bitflag, int not) 426{ 427 int i; 428 429 if (*s >= '0' && *s <= '9') { 430 int signo = string_to_uint(s); 431 if (signo < 0 || signo >= MAX_QUALS) 432 return -1; 433 qualify_one(signo, bitflag, not, -1); 434 return 0; 435 } 436 if (strncasecmp(s, "SIG", 3) == 0) 437 s += 3; 438 for (i = 0; i <= NSIG; i++) { 439 if (strcasecmp(s, signame(i) + 3) == 0) { 440 qualify_one(i, bitflag, not, -1); 441 return 0; 442 } 443 } 444 return -1; 445} 446 447static int 448qual_fault(const char *s, int bitflag, int not) 449{ 450 return -1; 451} 452 453static int 454qual_desc(const char *s, int bitflag, int not) 455{ 456 if (*s >= '0' && *s <= '9') { 457 int desc = string_to_uint(s); 458 if (desc < 0 || desc >= MAX_QUALS) 459 return -1; 460 qualify_one(desc, bitflag, not, -1); 461 return 0; 462 } 463 return -1; 464} 465 466static int 467lookup_class(const char *s) 468{ 469 if (strcmp(s, "file") == 0) 470 return TRACE_FILE; 471 if (strcmp(s, "ipc") == 0) 472 return TRACE_IPC; 473 if (strcmp(s, "network") == 0) 474 return TRACE_NETWORK; 475 if (strcmp(s, "process") == 0) 476 return TRACE_PROCESS; 477 if (strcmp(s, "signal") == 0) 478 return TRACE_SIGNAL; 479 if (strcmp(s, "desc") == 0) 480 return TRACE_DESC; 481 if (strcmp(s, "memory") == 0) 482 return TRACE_MEMORY; 483 return -1; 484} 485 486void 487qualify(const char *s) 488{ 489 const struct qual_options *opt; 490 int not; 491 char *copy; 492 const char *p; 493 int i, n; 494 495 opt = &qual_options[0]; 496 for (i = 0; (p = qual_options[i].option_name); i++) { 497 n = strlen(p); 498 if (strncmp(s, p, n) == 0 && s[n] == '=') { 499 opt = &qual_options[i]; 500 s += n + 1; 501 break; 502 } 503 } 504 not = 0; 505 if (*s == '!') { 506 not = 1; 507 s++; 508 } 509 if (strcmp(s, "none") == 0) { 510 not = 1 - not; 511 s = "all"; 512 } 513 if (strcmp(s, "all") == 0) { 514 for (i = 0; i < MAX_QUALS; i++) { 515 qualify_one(i, opt->bitflag, not, -1); 516 } 517 return; 518 } 519 for (i = 0; i < MAX_QUALS; i++) { 520 qualify_one(i, opt->bitflag, !not, -1); 521 } 522 copy = strdup(s); 523 if (!copy) 524 die_out_of_memory(); 525 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) { 526 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) { 527 for (i = 0; i < nsyscalls0; i++) 528 if (sysent0[i].sys_flags & n) 529 qualify_one(i, opt->bitflag, not, 0); 530 531#if SUPPORTED_PERSONALITIES >= 2 532 for (i = 0; i < nsyscalls1; i++) 533 if (sysent1[i].sys_flags & n) 534 qualify_one(i, opt->bitflag, not, 1); 535#endif 536 537#if SUPPORTED_PERSONALITIES >= 3 538 for (i = 0; i < nsyscalls2; i++) 539 if (sysent2[i].sys_flags & n) 540 qualify_one(i, opt->bitflag, not, 2); 541#endif 542 543 continue; 544 } 545 if (opt->qualify(p, opt->bitflag, not)) { 546 error_msg_and_die("invalid %s '%s'", 547 opt->argument_name, p); 548 } 549 } 550 free(copy); 551 return; 552} 553 554#ifdef SYS_socket_subcall 555static void 556decode_socket_subcall(struct tcb *tcp) 557{ 558 unsigned long addr; 559 unsigned int i, size; 560 561 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls) 562 return; 563 564 tcp->scno = SYS_socket_subcall + tcp->u_arg[0]; 565 addr = tcp->u_arg[1]; 566 tcp->u_nargs = sysent[tcp->scno].nargs; 567 size = current_wordsize; 568 for (i = 0; i < tcp->u_nargs; ++i) { 569 if (size == sizeof(int)) { 570 unsigned int arg; 571 if (umove(tcp, addr, &arg) < 0) 572 arg = 0; 573 tcp->u_arg[i] = arg; 574 } 575 else { 576 unsigned long arg; 577 if (umove(tcp, addr, &arg) < 0) 578 arg = 0; 579 tcp->u_arg[i] = arg; 580 } 581 addr += size; 582 } 583} 584#endif 585 586#ifdef SYS_ipc_subcall 587static void 588decode_ipc_subcall(struct tcb *tcp) 589{ 590 unsigned int i; 591 592 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls) 593 return; 594 595 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0]; 596 tcp->u_nargs = sysent[tcp->scno].nargs; 597 for (i = 0; i < tcp->u_nargs; i++) 598 tcp->u_arg[i] = tcp->u_arg[i + 1]; 599} 600#endif 601 602int 603printargs(struct tcb *tcp) 604{ 605 if (entering(tcp)) { 606 int i; 607 608 for (i = 0; i < tcp->u_nargs; i++) 609 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]); 610 } 611 return 0; 612} 613 614int 615printargs_lu(struct tcb *tcp) 616{ 617 if (entering(tcp)) { 618 int i; 619 620 for (i = 0; i < tcp->u_nargs; i++) 621 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]); 622 } 623 return 0; 624} 625 626int 627printargs_ld(struct tcb *tcp) 628{ 629 if (entering(tcp)) { 630 int i; 631 632 for (i = 0; i < tcp->u_nargs; i++) 633 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]); 634 } 635 return 0; 636} 637 638#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH) 639long 640getrval2(struct tcb *tcp) 641{ 642 long val; 643 644# if defined(SPARC) || defined(SPARC64) 645 val = sparc_regs.u_regs[U_REG_O1]; 646# elif defined(SH) 647 if (upeek(tcp, 4*(REG_REG0+1), &val) < 0) 648 return -1; 649# elif defined(IA64) 650 if (upeek(tcp, PT_R9, &val) < 0) 651 return -1; 652# endif 653 654 return val; 655} 656#endif 657 658int 659is_restart_error(struct tcb *tcp) 660{ 661 switch (tcp->u_error) { 662 case ERESTARTSYS: 663 case ERESTARTNOINTR: 664 case ERESTARTNOHAND: 665 case ERESTART_RESTARTBLOCK: 666 return 1; 667 default: 668 break; 669 } 670 return 0; 671} 672 673#if defined(I386) 674struct user_regs_struct i386_regs; 675#elif defined(X86_64) || defined(X32) 676/* 677 * On i386, pt_regs and user_regs_struct are the same, 678 * but on 64 bit x86, user_regs_struct has six more fields: 679 * fs_base, gs_base, ds, es, fs, gs. 680 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow. 681 */ 682struct i386_user_regs_struct { 683 uint32_t ebx; 684 uint32_t ecx; 685 uint32_t edx; 686 uint32_t esi; 687 uint32_t edi; 688 uint32_t ebp; 689 uint32_t eax; 690 uint32_t xds; 691 uint32_t xes; 692 uint32_t xfs; 693 uint32_t xgs; 694 uint32_t orig_eax; 695 uint32_t eip; 696 uint32_t xcs; 697 uint32_t eflags; 698 uint32_t esp; 699 uint32_t xss; 700}; 701static union { 702 struct user_regs_struct x86_64_r; 703 struct i386_user_regs_struct i386_r; 704} x86_regs_union; 705# define x86_64_regs x86_regs_union.x86_64_r 706# define i386_regs x86_regs_union.i386_r 707static struct iovec x86_io = { 708 .iov_base = &x86_regs_union 709}; 710#elif defined(IA64) 711long ia32 = 0; /* not static */ 712static long ia64_r8, ia64_r10; 713#elif defined(POWERPC) 714static long ppc_result; 715#elif defined(M68K) 716static long m68k_d0; 717#elif defined(BFIN) 718static long bfin_r0; 719#elif defined(ARM) 720struct pt_regs arm_regs; /* not static */ 721#elif defined(AARCH64) 722static union { 723 struct user_pt_regs aarch64_r; 724 struct arm_pt_regs arm_r; 725} arm_regs_union; 726# define aarch64_regs arm_regs_union.aarch64_r 727# define arm_regs arm_regs_union.arm_r 728static struct iovec aarch64_io = { 729 .iov_base = &arm_regs_union 730}; 731#elif defined(ALPHA) 732static long alpha_r0; 733static long alpha_a3; 734#elif defined(AVR32) 735static struct pt_regs avr32_regs; 736#elif defined(SPARC) || defined(SPARC64) 737struct pt_regs sparc_regs; /* not static */ 738#elif defined(LINUX_MIPSN32) 739static long long mips_a3; 740static long long mips_r2; 741#elif defined(MIPS) 742static long mips_a3; 743static long mips_r2; 744#elif defined(S390) || defined(S390X) 745static long gpr2; 746static long syscall_mode; 747#elif defined(HPPA) 748static long hppa_r28; 749#elif defined(SH) 750static long sh_r0; 751#elif defined(SH64) 752static long sh64_r9; 753#elif defined(CRISV10) || defined(CRISV32) 754static long cris_r10; 755#elif defined(TILE) 756struct pt_regs tile_regs; 757#elif defined(MICROBLAZE) 758static long microblaze_r3; 759#elif defined(OR1K) 760static struct user_regs_struct or1k_regs; 761static struct iovec or1k_io = { 762 .iov_base = &or1k_regs 763}; 764#endif 765 766void 767printcall(struct tcb *tcp) 768{ 769#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \ 770 sizeof(long) == 8 ? "[????????????????] " : \ 771 NULL /* crash */) 772 if (get_regs_error) { 773 PRINTBADPC; 774 return; 775 } 776#if defined(I386) 777 tprintf("[%08lx] ", i386_regs.eip); 778#elif defined(S390) || defined(S390X) 779 long psw; 780 if (upeek(tcp, PT_PSWADDR, &psw) < 0) { 781 PRINTBADPC; 782 return; 783 } 784# ifdef S390 785 tprintf("[%08lx] ", psw); 786# elif S390X 787 tprintf("[%016lx] ", psw); 788# endif 789#elif defined(X86_64) || defined(X32) 790 if (x86_io.iov_len == sizeof(i386_regs)) { 791 tprintf("[%08x] ", (unsigned) i386_regs.eip); 792 } else { 793# if defined(X86_64) 794 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip); 795# elif defined(X32) 796 /* Note: this truncates 64-bit rip to 32 bits */ 797 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip); 798# endif 799 } 800#elif defined(IA64) 801 long ip; 802 if (upeek(tcp, PT_B0, &ip) < 0) { 803 PRINTBADPC; 804 return; 805 } 806 tprintf("[%08lx] ", ip); 807#elif defined(POWERPC) 808 long pc; 809 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) { 810 PRINTBADPC; 811 return; 812 } 813# ifdef POWERPC64 814 tprintf("[%016lx] ", pc); 815# else 816 tprintf("[%08lx] ", pc); 817# endif 818#elif defined(M68K) 819 long pc; 820 if (upeek(tcp, 4*PT_PC, &pc) < 0) { 821 tprints("[????????] "); 822 return; 823 } 824 tprintf("[%08lx] ", pc); 825#elif defined(ALPHA) 826 long pc; 827 if (upeek(tcp, REG_PC, &pc) < 0) { 828 tprints("[????????????????] "); 829 return; 830 } 831 tprintf("[%08lx] ", pc); 832#elif defined(SPARC) 833 tprintf("[%08lx] ", sparc_regs.pc); 834#elif defined(SPARC64) 835 tprintf("[%08lx] ", sparc_regs.tpc); 836#elif defined(HPPA) 837 long pc; 838 if (upeek(tcp, PT_IAOQ0, &pc) < 0) { 839 tprints("[????????] "); 840 return; 841 } 842 tprintf("[%08lx] ", pc); 843#elif defined(MIPS) 844 long pc; 845 if (upeek(tcp, REG_EPC, &pc) < 0) { 846 tprints("[????????] "); 847 return; 848 } 849 tprintf("[%08lx] ", pc); 850#elif defined(SH) 851 long pc; 852 if (upeek(tcp, 4*REG_PC, &pc) < 0) { 853 tprints("[????????] "); 854 return; 855 } 856 tprintf("[%08lx] ", pc); 857#elif defined(SH64) 858 long pc; 859 if (upeek(tcp, REG_PC, &pc) < 0) { 860 tprints("[????????????????] "); 861 return; 862 } 863 tprintf("[%08lx] ", pc); 864#elif defined(ARM) 865 tprintf("[%08lx] ", arm_regs.ARM_pc); 866#elif defined(AARCH64) 867 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */ 868#elif defined(AVR32) 869 tprintf("[%08lx] ", avr32_regs.pc); 870#elif defined(BFIN) 871 long pc; 872 if (upeek(tcp, PT_PC, &pc) < 0) { 873 PRINTBADPC; 874 return; 875 } 876 tprintf("[%08lx] ", pc); 877#elif defined(CRISV10) 878 long pc; 879 if (upeek(tcp, 4*PT_IRP, &pc) < 0) { 880 PRINTBADPC; 881 return; 882 } 883 tprintf("[%08lx] ", pc); 884#elif defined(CRISV32) 885 long pc; 886 if (upeek(tcp, 4*PT_ERP, &pc) < 0) { 887 PRINTBADPC; 888 return; 889 } 890 tprintf("[%08lx] ", pc); 891#elif defined(TILE) 892# ifdef _LP64 893 tprintf("[%016lx] ", (unsigned long) tile_regs.pc); 894# else 895 tprintf("[%08lx] ", (unsigned long) tile_regs.pc); 896# endif 897#elif defined(OR1K) 898 tprintf("[%08lx] ", or1k_regs.pc); 899#endif /* architecture */ 900} 901 902#ifndef get_regs 903long get_regs_error; 904void 905get_regs(pid_t pid) 906{ 907# if defined(AVR32) 908 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs); 909# elif defined(I386) 910 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &i386_regs); 911# elif defined(X86_64) || defined(X32) 912 /* 913 * PTRACE_GETREGSET was introduced in 2.6.33. 914 * Let's be paranoid and require a bit later kernel. 915 */ 916 if (os_release >= KERNEL_VERSION(2,6,35)) { 917 /*x86_io.iov_base = &x86_regs_union; - already is */ 918 x86_io.iov_len = sizeof(x86_regs_union); 919 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (long) &x86_io); 920 } else { 921 /* Use old method, with heuristical detection of 32-bitness */ 922 x86_io.iov_len = sizeof(x86_64_regs); 923 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &x86_64_regs); 924 if (!get_regs_error && x86_64_regs.cs == 0x23) { 925 x86_io.iov_len = sizeof(i386_regs); 926 /* 927 * The order is important: i386_regs and x86_64_regs 928 * are overlaid in memory! 929 */ 930 i386_regs.ebx = x86_64_regs.rbx; 931 i386_regs.ecx = x86_64_regs.rcx; 932 i386_regs.edx = x86_64_regs.rdx; 933 i386_regs.esi = x86_64_regs.rsi; 934 i386_regs.edi = x86_64_regs.rdi; 935 i386_regs.ebp = x86_64_regs.rbp; 936 i386_regs.eax = x86_64_regs.rax; 937 /*i386_regs.xds = x86_64_regs.ds; unused by strace */ 938 /*i386_regs.xes = x86_64_regs.es; ditto... */ 939 /*i386_regs.xfs = x86_64_regs.fs;*/ 940 /*i386_regs.xgs = x86_64_regs.gs;*/ 941 i386_regs.orig_eax = x86_64_regs.orig_rax; 942 i386_regs.eip = x86_64_regs.rip; 943 /*i386_regs.xcs = x86_64_regs.cs;*/ 944 /*i386_regs.eflags = x86_64_regs.eflags;*/ 945 i386_regs.esp = x86_64_regs.rsp; 946 /*i386_regs.xss = x86_64_regs.ss;*/ 947 } 948 } 949# elif defined(ARM) 950 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (void *)&arm_regs); 951# elif defined(AARCH64) 952 /*aarch64_io.iov_base = &arm_regs_union; - already is */ 953 aarch64_io.iov_len = sizeof(arm_regs_union); 954 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (void *)&aarch64_io); 955# if 0 956 /* Paranoia checks */ 957 if (get_regs_error) 958 return; 959 switch (aarch64_io.iov_len) { 960 case sizeof(aarch64_regs): 961 /* We are in 64-bit mode */ 962 break; 963 case sizeof(arm_regs): 964 /* We are in 32-bit mode */ 965 break; 966 default: 967 get_regs_error = -1; 968 break; 969 } 970# endif 971# elif defined(SPARC) || defined(SPARC64) 972 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0); 973# elif defined(TILE) 974 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &tile_regs); 975# elif defined(OR1K) 976 or1k_io.iov_len = sizeof(or1k_regs); 977 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &or1k_io); 978# endif 979} 980#endif 981 982/* Returns: 983 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently. 984 * 1: ok, continue in trace_syscall_entering(). 985 * other: error, trace_syscall_entering() should print error indicator 986 * ("????" etc) and bail out. 987 */ 988static int 989get_scno(struct tcb *tcp) 990{ 991 long scno = 0; 992 993#if defined(S390) || defined(S390X) 994 if (upeek(tcp, PT_GPR2, &syscall_mode) < 0) 995 return -1; 996 997 if (syscall_mode != -ENOSYS) { 998 /* 999 * Since kernel version 2.5.44 the scno gets passed in gpr2. 1000 */ 1001 scno = syscall_mode; 1002 } else { 1003 /* 1004 * Old style of "passing" the scno via the SVC instruction. 1005 */ 1006 long psw; 1007 long opcode, offset_reg, tmp; 1008 void *svc_addr; 1009 static const int gpr_offset[16] = { 1010 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3, 1011 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7, 1012 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11, 1013 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15 1014 }; 1015 1016 if (upeek(tcp, PT_PSWADDR, &psw) < 0) 1017 return -1; 1018 errno = 0; 1019 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0); 1020 if (errno) { 1021 perror_msg("%s", "peektext(psw-oneword)"); 1022 return -1; 1023 } 1024 1025 /* 1026 * We have to check if the SVC got executed directly or via an 1027 * EXECUTE instruction. In case of EXECUTE it is necessary to do 1028 * instruction decoding to derive the system call number. 1029 * Unfortunately the opcode sizes of EXECUTE and SVC are differently, 1030 * so that this doesn't work if a SVC opcode is part of an EXECUTE 1031 * opcode. Since there is no way to find out the opcode size this 1032 * is the best we can do... 1033 */ 1034 if ((opcode & 0xff00) == 0x0a00) { 1035 /* SVC opcode */ 1036 scno = opcode & 0xff; 1037 } 1038 else { 1039 /* SVC got executed by EXECUTE instruction */ 1040 1041 /* 1042 * Do instruction decoding of EXECUTE. If you really want to 1043 * understand this, read the Principles of Operations. 1044 */ 1045 svc_addr = (void *) (opcode & 0xfff); 1046 1047 tmp = 0; 1048 offset_reg = (opcode & 0x000f0000) >> 16; 1049 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0)) 1050 return -1; 1051 svc_addr += tmp; 1052 1053 tmp = 0; 1054 offset_reg = (opcode & 0x0000f000) >> 12; 1055 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0)) 1056 return -1; 1057 svc_addr += tmp; 1058 1059 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0); 1060 if (errno) 1061 return -1; 1062# if defined(S390X) 1063 scno >>= 48; 1064# else 1065 scno >>= 16; 1066# endif 1067 tmp = 0; 1068 offset_reg = (opcode & 0x00f00000) >> 20; 1069 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0)) 1070 return -1; 1071 1072 scno = (scno | tmp) & 0xff; 1073 } 1074 } 1075#elif defined(POWERPC) 1076 if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0) 1077 return -1; 1078# ifdef POWERPC64 1079 /* TODO: speed up strace by not doing this at every syscall. 1080 * We only need to do it after execve. 1081 */ 1082 int currpers; 1083 long val; 1084 1085 /* Check for 64/32 bit mode. */ 1086 if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0) 1087 return -1; 1088 /* SF is bit 0 of MSR */ 1089 if (val < 0) 1090 currpers = 0; 1091 else 1092 currpers = 1; 1093 update_personality(tcp, currpers); 1094# endif 1095#elif defined(AVR32) 1096 scno = avr32_regs.r8; 1097#elif defined(BFIN) 1098 if (upeek(tcp, PT_ORIG_P0, &scno)) 1099 return -1; 1100#elif defined(I386) 1101 scno = i386_regs.orig_eax; 1102#elif defined(X86_64) || defined(X32) 1103# ifndef __X32_SYSCALL_BIT 1104# define __X32_SYSCALL_BIT 0x40000000 1105# endif 1106 int currpers; 1107# if 1 1108 /* GETREGSET of NT_PRSTATUS tells us regset size, 1109 * which unambiguously detects i386. 1110 * 1111 * Linux kernel distinguishes x86-64 and x32 processes 1112 * solely by looking at __X32_SYSCALL_BIT: 1113 * arch/x86/include/asm/compat.h::is_x32_task(): 1114 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT) 1115 * return true; 1116 */ 1117 if (x86_io.iov_len == sizeof(i386_regs)) { 1118 scno = i386_regs.orig_eax; 1119 currpers = 1; 1120 } else { 1121 scno = x86_64_regs.orig_rax; 1122 currpers = 0; 1123 if (scno & __X32_SYSCALL_BIT) { 1124 scno -= __X32_SYSCALL_BIT; 1125 currpers = 2; 1126 } 1127 } 1128# elif 0 1129 /* cs = 0x33 for long mode (native 64 bit and x32) 1130 * cs = 0x23 for compatibility mode (32 bit) 1131 * ds = 0x2b for x32 mode (x86-64 in 32 bit) 1132 */ 1133 scno = x86_64_regs.orig_rax; 1134 switch (x86_64_regs.cs) { 1135 case 0x23: currpers = 1; break; 1136 case 0x33: 1137 if (x86_64_regs.ds == 0x2b) { 1138 currpers = 2; 1139 scno &= ~__X32_SYSCALL_BIT; 1140 } else 1141 currpers = 0; 1142 break; 1143 default: 1144 fprintf(stderr, "Unknown value CS=0x%08X while " 1145 "detecting personality of process " 1146 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid); 1147 currpers = current_personality; 1148 break; 1149 } 1150# elif 0 1151 /* This version analyzes the opcode of a syscall instruction. 1152 * (int 0x80 on i386 vs. syscall on x86-64) 1153 * It works, but is too complicated, and strictly speaking, unreliable. 1154 */ 1155 unsigned long call, rip = x86_64_regs.rip; 1156 /* sizeof(syscall) == sizeof(int 0x80) == 2 */ 1157 rip -= 2; 1158 errno = 0; 1159 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0); 1160 if (errno) 1161 fprintf(stderr, "ptrace_peektext failed: %s\n", 1162 strerror(errno)); 1163 switch (call & 0xffff) { 1164 /* x86-64: syscall = 0x0f 0x05 */ 1165 case 0x050f: currpers = 0; break; 1166 /* i386: int 0x80 = 0xcd 0x80 */ 1167 case 0x80cd: currpers = 1; break; 1168 default: 1169 currpers = current_personality; 1170 fprintf(stderr, 1171 "Unknown syscall opcode (0x%04X) while " 1172 "detecting personality of process " 1173 "PID=%d\n", (int)call, tcp->pid); 1174 break; 1175 } 1176# endif 1177 1178# ifdef X32 1179 /* If we are built for a x32 system, then personality 0 is x32 1180 * (not x86_64), and stracing of x86_64 apps is not supported. 1181 * Stracing of i386 apps is still supported. 1182 */ 1183 if (currpers == 0) { 1184 fprintf(stderr, "syscall_%lu(...) in unsupported " 1185 "64-bit mode of process PID=%d\n", 1186 scno, tcp->pid); 1187 return 0; 1188 } 1189 currpers &= ~2; /* map 2,1 to 0,1 */ 1190# endif 1191 update_personality(tcp, currpers); 1192#elif defined(IA64) 1193# define IA64_PSR_IS ((long)1 << 34) 1194 long psr; 1195 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0) 1196 ia32 = (psr & IA64_PSR_IS) != 0; 1197 if (ia32) { 1198 if (upeek(tcp, PT_R1, &scno) < 0) 1199 return -1; 1200 } else { 1201 if (upeek(tcp, PT_R15, &scno) < 0) 1202 return -1; 1203 } 1204#elif defined(AARCH64) 1205 switch (aarch64_io.iov_len) { 1206 case sizeof(aarch64_regs): 1207 /* We are in 64-bit mode */ 1208 scno = aarch64_regs.regs[8]; 1209 update_personality(tcp, 1); 1210 break; 1211 case sizeof(arm_regs): 1212 /* We are in 32-bit mode */ 1213 scno = arm_regs.ARM_r7; 1214 update_personality(tcp, 0); 1215 break; 1216 } 1217#elif defined(ARM) 1218 /* 1219 * We only need to grab the syscall number on syscall entry. 1220 */ 1221 if (arm_regs.ARM_ip == 0) { 1222 /* 1223 * Note: we only deal with 32-bit CPUs here 1224 */ 1225 if (arm_regs.ARM_cpsr & 0x20) { 1226 /* 1227 * Get the Thumb-mode system call number 1228 */ 1229 scno = arm_regs.ARM_r7; 1230 } else { 1231 /* 1232 * Get the ARM-mode system call number 1233 */ 1234 errno = 0; 1235 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL); 1236 if (errno) 1237 return -1; 1238 1239 /* Handle the EABI syscall convention. We do not 1240 bother converting structures between the two 1241 ABIs, but basic functionality should work even 1242 if strace and the traced program have different 1243 ABIs. */ 1244 if (scno == 0xef000000) { 1245 scno = arm_regs.ARM_r7; 1246 } else { 1247 if ((scno & 0x0ff00000) != 0x0f900000) { 1248 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n", 1249 scno); 1250 return -1; 1251 } 1252 1253 /* 1254 * Fixup the syscall number 1255 */ 1256 scno &= 0x000fffff; 1257 } 1258 } 1259 if (scno & 0x0f0000) { 1260 /* 1261 * Handle ARM specific syscall 1262 */ 1263 update_personality(tcp, 1); 1264 scno &= 0x0000ffff; 1265 } else 1266 update_personality(tcp, 0); 1267 1268 } else { 1269 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid); 1270 tcp->flags |= TCB_INSYSCALL; 1271 } 1272#elif defined(M68K) 1273 if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0) 1274 return -1; 1275#elif defined(LINUX_MIPSN32) 1276 unsigned long long regs[38]; 1277 1278 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0) 1279 return -1; 1280 mips_a3 = regs[REG_A3]; 1281 mips_r2 = regs[REG_V0]; 1282 1283 scno = mips_r2; 1284 if (!SCNO_IS_VALID(scno)) { 1285 if (mips_a3 == 0 || mips_a3 == -1) { 1286 if (debug_flag) 1287 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno); 1288 return 0; 1289 } 1290 } 1291#elif defined(MIPS) 1292 if (upeek(tcp, REG_A3, &mips_a3) < 0) 1293 return -1; 1294 if (upeek(tcp, REG_V0, &scno) < 0) 1295 return -1; 1296 1297 if (!SCNO_IS_VALID(scno)) { 1298 if (mips_a3 == 0 || mips_a3 == -1) { 1299 if (debug_flag) 1300 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno); 1301 return 0; 1302 } 1303 } 1304#elif defined(ALPHA) 1305 if (upeek(tcp, REG_A3, &alpha_a3) < 0) 1306 return -1; 1307 if (upeek(tcp, REG_R0, &scno) < 0) 1308 return -1; 1309 1310 /* 1311 * Do some sanity checks to figure out if it's 1312 * really a syscall entry 1313 */ 1314 if (!SCNO_IS_VALID(scno)) { 1315 if (alpha_a3 == 0 || alpha_a3 == -1) { 1316 if (debug_flag) 1317 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno); 1318 return 0; 1319 } 1320 } 1321#elif defined(SPARC) || defined(SPARC64) 1322 /* Disassemble the syscall trap. */ 1323 /* Retrieve the syscall trap instruction. */ 1324 unsigned long trap; 1325 errno = 0; 1326# if defined(SPARC64) 1327 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0); 1328 trap >>= 32; 1329# else 1330 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0); 1331# endif 1332 if (errno) 1333 return -1; 1334 1335 /* Disassemble the trap to see what personality to use. */ 1336 switch (trap) { 1337 case 0x91d02010: 1338 /* Linux/SPARC syscall trap. */ 1339 update_personality(tcp, 0); 1340 break; 1341 case 0x91d0206d: 1342 /* Linux/SPARC64 syscall trap. */ 1343 update_personality(tcp, 2); 1344 break; 1345 case 0x91d02000: 1346 /* SunOS syscall trap. (pers 1) */ 1347 fprintf(stderr, "syscall: SunOS no support\n"); 1348 return -1; 1349 case 0x91d02008: 1350 /* Solaris 2.x syscall trap. (per 2) */ 1351 update_personality(tcp, 1); 1352 break; 1353 case 0x91d02009: 1354 /* NetBSD/FreeBSD syscall trap. */ 1355 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n"); 1356 return -1; 1357 case 0x91d02027: 1358 /* Solaris 2.x gettimeofday */ 1359 update_personality(tcp, 1); 1360 break; 1361 default: 1362# if defined(SPARC64) 1363 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc); 1364# else 1365 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc); 1366# endif 1367 return -1; 1368 } 1369 1370 /* Extract the system call number from the registers. */ 1371 if (trap == 0x91d02027) 1372 scno = 156; 1373 else 1374 scno = sparc_regs.u_regs[U_REG_G1]; 1375 if (scno == 0) { 1376 scno = sparc_regs.u_regs[U_REG_O0]; 1377 memmove(&sparc_regs.u_regs[U_REG_O0], &sparc_regs.u_regs[U_REG_O1], 7*sizeof(sparc_regs.u_regs[0])); 1378 } 1379#elif defined(HPPA) 1380 if (upeek(tcp, PT_GR20, &scno) < 0) 1381 return -1; 1382#elif defined(SH) 1383 /* 1384 * In the new syscall ABI, the system call number is in R3. 1385 */ 1386 if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0) 1387 return -1; 1388 1389 if (scno < 0) { 1390 /* Odd as it may seem, a glibc bug has been known to cause 1391 glibc to issue bogus negative syscall numbers. So for 1392 our purposes, make strace print what it *should* have been */ 1393 long correct_scno = (scno & 0xff); 1394 if (debug_flag) 1395 fprintf(stderr, 1396 "Detected glibc bug: bogus system call" 1397 " number = %ld, correcting to %ld\n", 1398 scno, 1399 correct_scno); 1400 scno = correct_scno; 1401 } 1402#elif defined(SH64) 1403 if (upeek(tcp, REG_SYSCALL, &scno) < 0) 1404 return -1; 1405 scno &= 0xFFFF; 1406#elif defined(CRISV10) || defined(CRISV32) 1407 if (upeek(tcp, 4*PT_R9, &scno) < 0) 1408 return -1; 1409#elif defined(TILE) 1410 int currpers; 1411 scno = tile_regs.regs[10]; 1412# ifdef __tilepro__ 1413 currpers = 1; 1414# else 1415# ifndef PT_FLAGS_COMPAT 1416# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */ 1417# endif 1418 if (tile_regs.flags & PT_FLAGS_COMPAT) 1419 currpers = 1; 1420 else 1421 currpers = 0; 1422# endif 1423 update_personality(tcp, currpers); 1424#elif defined(MICROBLAZE) 1425 if (upeek(tcp, 0, &scno) < 0) 1426 return -1; 1427#elif defined(OR1K) 1428 scno = or1k_regs.gpr[11]; 1429#endif 1430 1431 tcp->scno = scno; 1432 return 1; 1433} 1434 1435/* Called at each syscall entry. 1436 * Returns: 1437 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently. 1438 * 1: ok, continue in trace_syscall_entering(). 1439 * other: error, trace_syscall_entering() should print error indicator 1440 * ("????" etc) and bail out. 1441 */ 1442static int 1443syscall_fixup_on_sysenter(struct tcb *tcp) 1444{ 1445 /* A common case of "not a syscall entry" is post-execve SIGTRAP */ 1446#if defined(I386) 1447 if (i386_regs.eax != -ENOSYS) { 1448 if (debug_flag) 1449 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax); 1450 return 0; 1451 } 1452#elif defined(X86_64) || defined(X32) 1453 { 1454 long rax; 1455 if (x86_io.iov_len == sizeof(i386_regs)) { 1456 /* Sign extend from 32 bits */ 1457 rax = (int32_t)i386_regs.eax; 1458 } else { 1459 /* Note: in X32 build, this truncates 64 to 32 bits */ 1460 rax = x86_64_regs.rax; 1461 } 1462 if (rax != -ENOSYS) { 1463 if (debug_flag) 1464 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax); 1465 return 0; 1466 } 1467 } 1468#elif defined(S390) || defined(S390X) 1469 /* TODO: we already fetched PT_GPR2 in get_scno 1470 * and stored it in syscall_mode, reuse it here 1471 * instead of re-fetching? 1472 */ 1473 if (upeek(tcp, PT_GPR2, &gpr2) < 0) 1474 return -1; 1475 if (syscall_mode != -ENOSYS) 1476 syscall_mode = tcp->scno; 1477 if (gpr2 != syscall_mode) { 1478 if (debug_flag) 1479 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2); 1480 return 0; 1481 } 1482#elif defined(M68K) 1483 /* TODO? Eliminate upeek's in arches below like we did in x86 */ 1484 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0) 1485 return -1; 1486 if (m68k_d0 != -ENOSYS) { 1487 if (debug_flag) 1488 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0); 1489 return 0; 1490 } 1491#elif defined(IA64) 1492 if (upeek(tcp, PT_R10, &ia64_r10) < 0) 1493 return -1; 1494 if (upeek(tcp, PT_R8, &ia64_r8) < 0) 1495 return -1; 1496 if (ia32 && ia64_r8 != -ENOSYS) { 1497 if (debug_flag) 1498 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8); 1499 return 0; 1500 } 1501#elif defined(CRISV10) || defined(CRISV32) 1502 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0) 1503 return -1; 1504 if (cris_r10 != -ENOSYS) { 1505 if (debug_flag) 1506 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10); 1507 return 0; 1508 } 1509#elif defined(MICROBLAZE) 1510 if (upeek(tcp, 3 * 4, µblaze_r3) < 0) 1511 return -1; 1512 if (microblaze_r3 != -ENOSYS) { 1513 if (debug_flag) 1514 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3); 1515 return 0; 1516 } 1517#endif 1518 return 1; 1519} 1520 1521static void 1522internal_fork(struct tcb *tcp) 1523{ 1524#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32 1525# define ARG_FLAGS 1 1526#else 1527# define ARG_FLAGS 0 1528#endif 1529#ifndef CLONE_UNTRACED 1530# define CLONE_UNTRACED 0x00800000 1531#endif 1532 if ((ptrace_setoptions 1533 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK)) 1534 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK)) 1535 return; 1536 1537 if (!followfork) 1538 return; 1539 1540 if (entering(tcp)) { 1541 /* 1542 * We won't see the new child if clone is called with 1543 * CLONE_UNTRACED, so we keep the same logic with that option 1544 * and don't trace it. 1545 */ 1546 if ((sysent[tcp->scno].sys_func == sys_clone) && 1547 (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)) 1548 return; 1549 setbpt(tcp); 1550 } else { 1551 if (tcp->flags & TCB_BPTSET) 1552 clearbpt(tcp); 1553 } 1554} 1555 1556#if defined(TCB_WAITEXECVE) 1557static void 1558internal_exec(struct tcb *tcp) 1559{ 1560 /* Maybe we have post-execve SIGTRAP suppressed? */ 1561 if (ptrace_setoptions & PTRACE_O_TRACEEXEC) 1562 return; /* yes, no need to do anything */ 1563 1564 if (exiting(tcp) && syserror(tcp)) 1565 /* Error in execve, no post-execve SIGTRAP expected */ 1566 tcp->flags &= ~TCB_WAITEXECVE; 1567 else 1568 tcp->flags |= TCB_WAITEXECVE; 1569} 1570#endif 1571 1572static void 1573syscall_fixup_for_fork_exec(struct tcb *tcp) 1574{ 1575 /* 1576 * We must always trace a few critical system calls in order to 1577 * correctly support following forks in the presence of tracing 1578 * qualifiers. 1579 */ 1580 int (*func)(); 1581 1582 if (!SCNO_IS_VALID(tcp->scno)) 1583 return; 1584 1585 func = sysent[tcp->scno].sys_func; 1586 1587 if ( sys_fork == func 1588 || sys_vfork == func 1589 || sys_clone == func 1590 ) { 1591 internal_fork(tcp); 1592 return; 1593 } 1594 1595#if defined(TCB_WAITEXECVE) 1596 if ( sys_execve == func 1597# if defined(SPARC) || defined(SPARC64) 1598 || sys_execv == func 1599# endif 1600 ) { 1601 internal_exec(tcp); 1602 return; 1603 } 1604#endif 1605} 1606 1607/* Return -1 on error or 1 on success (never 0!) */ 1608static int 1609get_syscall_args(struct tcb *tcp) 1610{ 1611 int i, nargs; 1612 1613 if (SCNO_IS_VALID(tcp->scno)) 1614 nargs = tcp->u_nargs = sysent[tcp->scno].nargs; 1615 else 1616 nargs = tcp->u_nargs = MAX_ARGS; 1617 1618#if defined(S390) || defined(S390X) 1619 for (i = 0; i < nargs; ++i) 1620 if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0) 1621 return -1; 1622#elif defined(ALPHA) 1623 for (i = 0; i < nargs; ++i) 1624 if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0) 1625 return -1; 1626#elif defined(IA64) 1627 if (!ia32) { 1628 unsigned long *out0, cfm, sof, sol; 1629 long rbs_end; 1630 /* be backwards compatible with kernel < 2.4.4... */ 1631# ifndef PT_RBS_END 1632# define PT_RBS_END PT_AR_BSP 1633# endif 1634 1635 if (upeek(tcp, PT_RBS_END, &rbs_end) < 0) 1636 return -1; 1637 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0) 1638 return -1; 1639 1640 sof = (cfm >> 0) & 0x7f; 1641 sol = (cfm >> 7) & 0x7f; 1642 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol); 1643 1644 for (i = 0; i < nargs; ++i) { 1645 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i), 1646 sizeof(long), (char *) &tcp->u_arg[i]) < 0) 1647 return -1; 1648 } 1649 } else { 1650 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */, 1651 PT_R9 /* ECX = out1 */, 1652 PT_R10 /* EDX = out2 */, 1653 PT_R14 /* ESI = out3 */, 1654 PT_R15 /* EDI = out4 */, 1655 PT_R13 /* EBP = out5 */}; 1656 1657 for (i = 0; i < nargs; ++i) { 1658 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0) 1659 return -1; 1660 /* truncate away IVE sign-extension */ 1661 tcp->u_arg[i] &= 0xffffffff; 1662 } 1663 } 1664#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64) 1665 /* N32 and N64 both use up to six registers. */ 1666 unsigned long long regs[38]; 1667 1668 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0) 1669 return -1; 1670 1671 for (i = 0; i < nargs; ++i) { 1672 tcp->u_arg[i] = regs[REG_A0 + i]; 1673# if defined(LINUX_MIPSN32) 1674 tcp->ext_arg[i] = regs[REG_A0 + i]; 1675# endif 1676 } 1677#elif defined(MIPS) 1678 if (nargs > 4) { 1679 long sp; 1680 1681 if (upeek(tcp, REG_SP, &sp) < 0) 1682 return -1; 1683 for (i = 0; i < 4; ++i) 1684 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0) 1685 return -1; 1686 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]), 1687 (char *)(tcp->u_arg + 4)); 1688 } else { 1689 for (i = 0; i < nargs; ++i) 1690 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0) 1691 return -1; 1692 } 1693#elif defined(POWERPC) 1694# ifndef PT_ORIG_R3 1695# define PT_ORIG_R3 34 1696# endif 1697 for (i = 0; i < nargs; ++i) { 1698 if (upeek(tcp, (i==0) ? 1699 (sizeof(unsigned long) * PT_ORIG_R3) : 1700 ((i+PT_R3) * sizeof(unsigned long)), 1701 &tcp->u_arg[i]) < 0) 1702 return -1; 1703 } 1704#elif defined(SPARC) || defined(SPARC64) 1705 for (i = 0; i < nargs; ++i) 1706 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i]; 1707#elif defined(HPPA) 1708 for (i = 0; i < nargs; ++i) 1709 if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0) 1710 return -1; 1711#elif defined(ARM) || defined(AARCH64) 1712# if defined(AARCH64) 1713 if (tcp->currpers == 1) 1714 for (i = 0; i < nargs; ++i) 1715 tcp->u_arg[i] = aarch64_regs.regs[i]; 1716 else 1717# endif 1718 for (i = 0; i < nargs; ++i) 1719 tcp->u_arg[i] = arm_regs.uregs[i]; 1720#elif defined(AVR32) 1721 (void)i; 1722 (void)nargs; 1723 tcp->u_arg[0] = avr32_regs.r12; 1724 tcp->u_arg[1] = avr32_regs.r11; 1725 tcp->u_arg[2] = avr32_regs.r10; 1726 tcp->u_arg[3] = avr32_regs.r9; 1727 tcp->u_arg[4] = avr32_regs.r5; 1728 tcp->u_arg[5] = avr32_regs.r3; 1729#elif defined(BFIN) 1730 static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 }; 1731 1732 for (i = 0; i < nargs; ++i) 1733 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0) 1734 return -1; 1735#elif defined(SH) 1736 static const int syscall_regs[MAX_ARGS] = { 1737 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6), 1738 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1) 1739 }; 1740 1741 for (i = 0; i < nargs; ++i) 1742 if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0) 1743 return -1; 1744#elif defined(SH64) 1745 int i; 1746 /* Registers used by SH5 Linux system calls for parameters */ 1747 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 }; 1748 1749 for (i = 0; i < nargs; ++i) 1750 if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0) 1751 return -1; 1752#elif defined(I386) 1753 (void)i; 1754 (void)nargs; 1755 tcp->u_arg[0] = i386_regs.ebx; 1756 tcp->u_arg[1] = i386_regs.ecx; 1757 tcp->u_arg[2] = i386_regs.edx; 1758 tcp->u_arg[3] = i386_regs.esi; 1759 tcp->u_arg[4] = i386_regs.edi; 1760 tcp->u_arg[5] = i386_regs.ebp; 1761#elif defined(X86_64) || defined(X32) 1762 (void)i; 1763 (void)nargs; 1764 if (x86_io.iov_len != sizeof(i386_regs)) { 1765 /* x86-64 or x32 ABI */ 1766 tcp->u_arg[0] = x86_64_regs.rdi; 1767 tcp->u_arg[1] = x86_64_regs.rsi; 1768 tcp->u_arg[2] = x86_64_regs.rdx; 1769 tcp->u_arg[3] = x86_64_regs.r10; 1770 tcp->u_arg[4] = x86_64_regs.r8; 1771 tcp->u_arg[5] = x86_64_regs.r9; 1772# ifdef X32 1773 tcp->ext_arg[0] = x86_64_regs.rdi; 1774 tcp->ext_arg[1] = x86_64_regs.rsi; 1775 tcp->ext_arg[2] = x86_64_regs.rdx; 1776 tcp->ext_arg[3] = x86_64_regs.r10; 1777 tcp->ext_arg[4] = x86_64_regs.r8; 1778 tcp->ext_arg[5] = x86_64_regs.r9; 1779# endif 1780 } else { 1781 /* i386 ABI */ 1782 /* Zero-extend from 32 bits */ 1783 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers 1784 * if you need to use *sign-extended* parameter. 1785 */ 1786 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx; 1787 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx; 1788 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx; 1789 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi; 1790 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi; 1791 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp; 1792 } 1793#elif defined(MICROBLAZE) 1794 for (i = 0; i < nargs; ++i) 1795 if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0) 1796 return -1; 1797#elif defined(CRISV10) || defined(CRISV32) 1798 static const int crisregs[MAX_ARGS] = { 1799 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12, 1800 4*PT_R13 , 4*PT_MOF, 4*PT_SRP 1801 }; 1802 1803 for (i = 0; i < nargs; ++i) 1804 if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0) 1805 return -1; 1806#elif defined(TILE) 1807 for (i = 0; i < nargs; ++i) 1808 tcp->u_arg[i] = tile_regs.regs[i]; 1809#elif defined(M68K) 1810 for (i = 0; i < nargs; ++i) 1811 if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0) 1812 return -1; 1813#elif defined(OR1K) 1814 (void)nargs; 1815 for (i = 0; i < 6; ++i) 1816 tcp->u_arg[i] = or1k_regs.gpr[3 + i]; 1817#else /* Other architecture (32bits specific) */ 1818 for (i = 0; i < nargs; ++i) 1819 if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0) 1820 return -1; 1821#endif 1822 return 1; 1823} 1824 1825static int 1826trace_syscall_entering(struct tcb *tcp) 1827{ 1828 int res, scno_good; 1829 1830#if defined TCB_WAITEXECVE 1831 if (tcp->flags & TCB_WAITEXECVE) { 1832 /* This is the post-execve SIGTRAP. */ 1833 tcp->flags &= ~TCB_WAITEXECVE; 1834 return 0; 1835 } 1836#endif 1837 1838 scno_good = res = (get_regs_error ? -1 : get_scno(tcp)); 1839 if (res == 0) 1840 return res; 1841 if (res == 1) { 1842 res = syscall_fixup_on_sysenter(tcp); 1843 if (res == 0) 1844 return res; 1845 if (res == 1) 1846 res = get_syscall_args(tcp); 1847 } 1848 1849 if (res != 1) { 1850 printleader(tcp); 1851 if (scno_good != 1) 1852 tprints("????" /* anti-trigraph gap */ "("); 1853 else if (!SCNO_IS_VALID(tcp->scno)) 1854 tprintf("syscall_%lu(", tcp->scno); 1855 else 1856 tprintf("%s(", sysent[tcp->scno].sys_name); 1857 /* 1858 * " <unavailable>" will be added later by the code which 1859 * detects ptrace errors. 1860 */ 1861 goto ret; 1862 } 1863 1864#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall) 1865 while (SCNO_IS_VALID(tcp->scno)) { 1866# ifdef SYS_socket_subcall 1867 if (sysent[tcp->scno].sys_func == sys_socketcall) { 1868 decode_socket_subcall(tcp); 1869 break; 1870 } 1871# endif 1872# ifdef SYS_ipc_subcall 1873 if (sysent[tcp->scno].sys_func == sys_ipc) { 1874 decode_ipc_subcall(tcp); 1875 break; 1876 } 1877# endif 1878 break; 1879 } 1880#endif /* SYS_socket_subcall || SYS_ipc_subcall */ 1881 1882 if (need_fork_exec_workarounds) 1883 syscall_fixup_for_fork_exec(tcp); 1884 1885 if ((SCNO_IS_VALID(tcp->scno) && 1886 !(qual_flags[tcp->scno] & QUAL_TRACE)) || 1887 (tracing_paths && !pathtrace_match(tcp))) { 1888 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED; 1889 return 0; 1890 } 1891 1892 tcp->flags &= ~TCB_FILTERED; 1893 1894 if (cflag == CFLAG_ONLY_STATS) { 1895 res = 0; 1896 goto ret; 1897 } 1898 1899 printleader(tcp); 1900 if (!SCNO_IS_VALID(tcp->scno)) 1901 tprintf("syscall_%lu(", tcp->scno); 1902 else 1903 tprintf("%s(", sysent[tcp->scno].sys_name); 1904 if (!SCNO_IS_VALID(tcp->scno) || 1905 ((qual_flags[tcp->scno] & QUAL_RAW) && 1906 sysent[tcp->scno].sys_func != sys_exit)) 1907 res = printargs(tcp); 1908 else 1909 res = (*sysent[tcp->scno].sys_func)(tcp); 1910 1911 fflush(tcp->outf); 1912 ret: 1913 tcp->flags |= TCB_INSYSCALL; 1914 /* Measure the entrance time as late as possible to avoid errors. */ 1915 if (Tflag || cflag) 1916 gettimeofday(&tcp->etime, NULL); 1917 return res; 1918} 1919 1920/* Returns: 1921 * 1: ok, continue in trace_syscall_exiting(). 1922 * -1: error, trace_syscall_exiting() should print error indicator 1923 * ("????" etc) and bail out. 1924 */ 1925static int 1926get_syscall_result(struct tcb *tcp) 1927{ 1928#if defined(S390) || defined(S390X) 1929 if (upeek(tcp, PT_GPR2, &gpr2) < 0) 1930 return -1; 1931#elif defined(POWERPC) 1932# define SO_MASK 0x10000000 1933 { 1934 long flags; 1935 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0) 1936 return -1; 1937 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &ppc_result) < 0) 1938 return -1; 1939 if (flags & SO_MASK) 1940 ppc_result = -ppc_result; 1941 } 1942#elif defined(AVR32) 1943 /* already done by get_regs */ 1944#elif defined(BFIN) 1945 if (upeek(tcp, PT_R0, &bfin_r0) < 0) 1946 return -1; 1947#elif defined(I386) 1948 /* already done by get_regs */ 1949#elif defined(X86_64) || defined(X32) 1950 /* already done by get_regs */ 1951#elif defined(IA64) 1952# define IA64_PSR_IS ((long)1 << 34) 1953 long psr; 1954 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0) 1955 ia32 = (psr & IA64_PSR_IS) != 0; 1956 if (upeek(tcp, PT_R8, &ia64_r8) < 0) 1957 return -1; 1958 if (upeek(tcp, PT_R10, &ia64_r10) < 0) 1959 return -1; 1960#elif defined(ARM) 1961 /* already done by get_regs */ 1962#elif defined(AARCH64) 1963 /* register reading already done by get_regs */ 1964 1965 /* Used to do this, but we did it on syscall entry already: */ 1966 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs, 1967 * else it's personality 0. 1968 */ 1969 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/ 1970#elif defined(M68K) 1971 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0) 1972 return -1; 1973#elif defined(LINUX_MIPSN32) 1974 unsigned long long regs[38]; 1975 1976 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0) 1977 return -1; 1978 mips_a3 = regs[REG_A3]; 1979 mips_r2 = regs[REG_V0]; 1980#elif defined(MIPS) 1981 if (upeek(tcp, REG_A3, &mips_a3) < 0) 1982 return -1; 1983 if (upeek(tcp, REG_V0, &mips_r2) < 0) 1984 return -1; 1985#elif defined(ALPHA) 1986 if (upeek(tcp, REG_A3, &alpha_a3) < 0) 1987 return -1; 1988 if (upeek(tcp, REG_R0, &alpha_r0) < 0) 1989 return -1; 1990#elif defined(SPARC) || defined(SPARC64) 1991 /* already done by get_regs */ 1992#elif defined(HPPA) 1993 if (upeek(tcp, PT_GR28, &hppa_r28) < 0) 1994 return -1; 1995#elif defined(SH) 1996 /* new syscall ABI returns result in R0 */ 1997 if (upeek(tcp, 4*REG_REG0, (long *)&sh_r0) < 0) 1998 return -1; 1999#elif defined(SH64) 2000 /* ABI defines result returned in r9 */ 2001 if (upeek(tcp, REG_GENERAL(9), (long *)&sh64_r9) < 0) 2002 return -1; 2003#elif defined(CRISV10) || defined(CRISV32) 2004 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0) 2005 return -1; 2006#elif defined(TILE) 2007 /* already done by get_regs */ 2008#elif defined(MICROBLAZE) 2009 if (upeek(tcp, 3 * 4, µblaze_r3) < 0) 2010 return -1; 2011#elif defined(OR1K) 2012 /* already done by get_regs */ 2013#endif 2014 return 1; 2015} 2016 2017/* Called at each syscall exit */ 2018static void 2019syscall_fixup_on_sysexit(struct tcb *tcp) 2020{ 2021#if defined(S390) || defined(S390X) 2022 if (syscall_mode != -ENOSYS) 2023 syscall_mode = tcp->scno; 2024 if ((tcp->flags & TCB_WAITEXECVE) 2025 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) { 2026 /* 2027 * Return from execve. 2028 * Fake a return value of zero. We leave the TCB_WAITEXECVE 2029 * flag set for the post-execve SIGTRAP to see and reset. 2030 */ 2031 gpr2 = 0; 2032 } 2033#endif 2034} 2035 2036/* 2037 * Check the syscall return value register value for whether it is 2038 * a negated errno code indicating an error, or a success return value. 2039 */ 2040static inline int 2041is_negated_errno(unsigned long int val) 2042{ 2043 unsigned long int max = -(long int) nerrnos; 2044#if SUPPORTED_PERSONALITIES > 1 2045 if (current_wordsize < sizeof(val)) { 2046 val = (unsigned int) val; 2047 max = (unsigned int) max; 2048 } 2049#endif 2050 return val > max; 2051} 2052 2053#if defined(X32) 2054static inline int 2055is_negated_errno_x32(unsigned long long val) 2056{ 2057 unsigned long long max = -(long long) nerrnos; 2058 /* 2059 * current_wordsize is 4 even in personality 0 (native X32) 2060 * but truncation _must not_ be done in it. 2061 * can't check current_wordsize here! 2062 */ 2063 if (current_personality != 0) { 2064 val = (uint32_t) val; 2065 max = (uint32_t) max; 2066 } 2067 return val > max; 2068} 2069#endif 2070 2071/* Returns: 2072 * 1: ok, continue in trace_syscall_exiting(). 2073 * -1: error, trace_syscall_exiting() should print error indicator 2074 * ("????" etc) and bail out. 2075 */ 2076static void 2077get_error(struct tcb *tcp) 2078{ 2079 int u_error = 0; 2080 int check_errno = 1; 2081 if (SCNO_IN_RANGE(tcp->scno) 2082 && (sysent[tcp->scno].sys_flags & SYSCALL_NEVER_FAILS) 2083 ) { 2084 check_errno = 0; 2085 } 2086#if defined(S390) || defined(S390X) 2087 if (check_errno && is_negated_errno(gpr2)) { 2088 tcp->u_rval = -1; 2089 u_error = -gpr2; 2090 } 2091 else { 2092 tcp->u_rval = gpr2; 2093 } 2094#elif defined(I386) 2095 if (check_errno && is_negated_errno(i386_regs.eax)) { 2096 tcp->u_rval = -1; 2097 u_error = -i386_regs.eax; 2098 } 2099 else { 2100 tcp->u_rval = i386_regs.eax; 2101 } 2102#elif defined(X86_64) 2103 long rax; 2104 if (x86_io.iov_len == sizeof(i386_regs)) { 2105 /* Sign extend from 32 bits */ 2106 rax = (int32_t)i386_regs.eax; 2107 } else { 2108 rax = x86_64_regs.rax; 2109 } 2110 if (check_errno && is_negated_errno(rax)) { 2111 tcp->u_rval = -1; 2112 u_error = -rax; 2113 } 2114 else { 2115 tcp->u_rval = rax; 2116 } 2117#elif defined(X32) 2118 /* In X32, return value is 64-bit (llseek uses one). 2119 * Using merely "long rax" would not work. 2120 */ 2121 long long rax; 2122 if (x86_io.iov_len == sizeof(i386_regs)) { 2123 /* Sign extend from 32 bits */ 2124 rax = (int32_t)i386_regs.eax; 2125 } else { 2126 rax = x86_64_regs.rax; 2127 } 2128 /* Careful: is_negated_errno() works only on longs */ 2129 if (check_errno && is_negated_errno_x32(rax)) { 2130 tcp->u_rval = -1; 2131 u_error = -rax; 2132 } 2133 else { 2134 tcp->u_rval = rax; /* truncating */ 2135 tcp->u_lrval = rax; 2136 } 2137#elif defined(IA64) 2138 if (ia32) { 2139 int err; 2140 2141 err = (int)ia64_r8; 2142 if (check_errno && is_negated_errno(err)) { 2143 tcp->u_rval = -1; 2144 u_error = -err; 2145 } 2146 else { 2147 tcp->u_rval = err; 2148 } 2149 } else { 2150 if (check_errno && ia64_r10) { 2151 tcp->u_rval = -1; 2152 u_error = ia64_r8; 2153 } else { 2154 tcp->u_rval = ia64_r8; 2155 } 2156 } 2157#elif defined(MIPS) 2158 if (check_errno && mips_a3) { 2159 tcp->u_rval = -1; 2160 u_error = mips_r2; 2161 } else { 2162 tcp->u_rval = mips_r2; 2163# if defined(LINUX_MIPSN32) 2164 tcp->u_lrval = mips_r2; 2165# endif 2166 } 2167#elif defined(POWERPC) 2168 if (check_errno && is_negated_errno(ppc_result)) { 2169 tcp->u_rval = -1; 2170 u_error = -ppc_result; 2171 } 2172 else { 2173 tcp->u_rval = ppc_result; 2174 } 2175#elif defined(M68K) 2176 if (check_errno && is_negated_errno(m68k_d0)) { 2177 tcp->u_rval = -1; 2178 u_error = -m68k_d0; 2179 } 2180 else { 2181 tcp->u_rval = m68k_d0; 2182 } 2183#elif defined(ARM) || defined(AARCH64) 2184# if defined(AARCH64) 2185 if (tcp->currpers == 1) { 2186 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) { 2187 tcp->u_rval = -1; 2188 u_error = -aarch64_regs.regs[0]; 2189 } 2190 else { 2191 tcp->u_rval = aarch64_regs.regs[0]; 2192 } 2193 } 2194 else 2195# endif 2196 { 2197 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) { 2198 tcp->u_rval = -1; 2199 u_error = -arm_regs.ARM_r0; 2200 } 2201 else { 2202 tcp->u_rval = arm_regs.ARM_r0; 2203 } 2204 } 2205#elif defined(AVR32) 2206 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) { 2207 tcp->u_rval = -1; 2208 u_error = -avr32_regs.r12; 2209 } 2210 else { 2211 tcp->u_rval = avr32_regs.r12; 2212 } 2213#elif defined(BFIN) 2214 if (check_errno && is_negated_errno(bfin_r0)) { 2215 tcp->u_rval = -1; 2216 u_error = -bfin_r0; 2217 } else { 2218 tcp->u_rval = bfin_r0; 2219 } 2220#elif defined(ALPHA) 2221 if (check_errno && alpha_a3) { 2222 tcp->u_rval = -1; 2223 u_error = alpha_r0; 2224 } 2225 else { 2226 tcp->u_rval = alpha_r0; 2227 } 2228#elif defined(SPARC) 2229 if (check_errno && sparc_regs.psr & PSR_C) { 2230 tcp->u_rval = -1; 2231 u_error = sparc_regs.u_regs[U_REG_O0]; 2232 } 2233 else { 2234 tcp->u_rval = sparc_regs.u_regs[U_REG_O0]; 2235 } 2236#elif defined(SPARC64) 2237 if (check_errno && sparc_regs.tstate & 0x1100000000UL) { 2238 tcp->u_rval = -1; 2239 u_error = sparc_regs.u_regs[U_REG_O0]; 2240 } 2241 else { 2242 tcp->u_rval = sparc_regs.u_regs[U_REG_O0]; 2243 } 2244#elif defined(HPPA) 2245 if (check_errno && is_negated_errno(hppa_r28)) { 2246 tcp->u_rval = -1; 2247 u_error = -hppa_r28; 2248 } 2249 else { 2250 tcp->u_rval = hppa_r28; 2251 } 2252#elif defined(SH) 2253 if (check_errno && is_negated_errno(sh_r0)) { 2254 tcp->u_rval = -1; 2255 u_error = -sh_r0; 2256 } 2257 else { 2258 tcp->u_rval = sh_r0; 2259 } 2260#elif defined(SH64) 2261 if (check_errno && is_negated_errno(sh64_r9)) { 2262 tcp->u_rval = -1; 2263 u_error = -sh64_r9; 2264 } 2265 else { 2266 tcp->u_rval = sh64_r9; 2267 } 2268#elif defined(CRISV10) || defined(CRISV32) 2269 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) { 2270 tcp->u_rval = -1; 2271 u_error = -cris_r10; 2272 } 2273 else { 2274 tcp->u_rval = cris_r10; 2275 } 2276#elif defined(TILE) 2277 /* 2278 * The standard tile calling convention returns the value (or negative 2279 * errno) in r0, and zero (or positive errno) in r1. 2280 * Until at least kernel 3.8, however, the r1 value is not reflected 2281 * in ptregs at this point, so we use r0 here. 2282 */ 2283 if (check_errno && is_negated_errno(tile_regs.regs[0])) { 2284 tcp->u_rval = -1; 2285 u_error = -tile_regs.regs[0]; 2286 } else { 2287 tcp->u_rval = tile_regs.regs[0]; 2288 } 2289#elif defined(MICROBLAZE) 2290 if (check_errno && is_negated_errno(microblaze_r3)) { 2291 tcp->u_rval = -1; 2292 u_error = -microblaze_r3; 2293 } 2294 else { 2295 tcp->u_rval = microblaze_r3; 2296 } 2297#elif defined(OR1K) 2298 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) { 2299 tcp->u_rval = -1; 2300 u_error = -or1k_regs.gpr[11]; 2301 } 2302 else { 2303 tcp->u_rval = or1k_regs.gpr[11]; 2304 } 2305#endif 2306 tcp->u_error = u_error; 2307} 2308 2309static void 2310dumpio(struct tcb *tcp) 2311{ 2312 if (syserror(tcp)) 2313 return; 2314 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS) 2315 return; 2316 if (!SCNO_IS_VALID(tcp->scno)) 2317 return; 2318 if (sysent[tcp->scno].sys_func == printargs) 2319 return; 2320 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) { 2321 if (sysent[tcp->scno].sys_func == sys_read || 2322 sysent[tcp->scno].sys_func == sys_pread || 2323 sysent[tcp->scno].sys_func == sys_recv || 2324 sysent[tcp->scno].sys_func == sys_recvfrom) 2325 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval); 2326 else if (sysent[tcp->scno].sys_func == sys_readv) 2327 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]); 2328 return; 2329 } 2330 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) { 2331 if (sysent[tcp->scno].sys_func == sys_write || 2332 sysent[tcp->scno].sys_func == sys_pwrite || 2333 sysent[tcp->scno].sys_func == sys_send || 2334 sysent[tcp->scno].sys_func == sys_sendto) 2335 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]); 2336 else if (sysent[tcp->scno].sys_func == sys_writev) 2337 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]); 2338 return; 2339 } 2340} 2341 2342static int 2343trace_syscall_exiting(struct tcb *tcp) 2344{ 2345 int sys_res; 2346 struct timeval tv; 2347 int res; 2348 long u_error; 2349 2350 /* Measure the exit time as early as possible to avoid errors. */ 2351 if (Tflag || cflag) 2352 gettimeofday(&tv, NULL); 2353 2354#if SUPPORTED_PERSONALITIES > 1 2355 update_personality(tcp, tcp->currpers); 2356#endif 2357 res = (get_regs_error ? -1 : get_syscall_result(tcp)); 2358 if (res == 1) { 2359 syscall_fixup_on_sysexit(tcp); /* never fails */ 2360 get_error(tcp); /* never fails */ 2361 if (need_fork_exec_workarounds) 2362 syscall_fixup_for_fork_exec(tcp); 2363 if (filtered(tcp)) 2364 goto ret; 2365 } 2366 2367 if (cflag) { 2368 struct timeval t = tv; 2369 count_syscall(tcp, &t); 2370 if (cflag == CFLAG_ONLY_STATS) { 2371 goto ret; 2372 } 2373 } 2374 2375 /* If not in -ff mode, and printing_tcp != tcp, 2376 * then the log currently does not end with output 2377 * of _our syscall entry_, but with something else. 2378 * We need to say which syscall's return is this. 2379 * 2380 * Forced reprinting via TCB_REPRINT is used only by 2381 * "strace -ff -oLOG test/threaded_execve" corner case. 2382 * It's the only case when -ff mode needs reprinting. 2383 */ 2384 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) { 2385 tcp->flags &= ~TCB_REPRINT; 2386 printleader(tcp); 2387 if (!SCNO_IS_VALID(tcp->scno)) 2388 tprintf("<... syscall_%lu resumed> ", tcp->scno); 2389 else 2390 tprintf("<... %s resumed> ", sysent[tcp->scno].sys_name); 2391 } 2392 printing_tcp = tcp; 2393 2394 if (res != 1) { 2395 /* There was error in one of prior ptrace ops */ 2396 tprints(") "); 2397 tabto(); 2398 tprints("= ? <unavailable>\n"); 2399 line_ended(); 2400 tcp->flags &= ~TCB_INSYSCALL; 2401 return res; 2402 } 2403 2404 sys_res = 0; 2405 if (!SCNO_IS_VALID(tcp->scno) 2406 || (qual_flags[tcp->scno] & QUAL_RAW)) { 2407 /* sys_res = printargs(tcp); - but it's nop on sysexit */ 2408 } else { 2409 /* FIXME: not_failing_only (IOW, option -z) is broken: 2410 * failure of syscall is known only after syscall return. 2411 * Thus we end up with something like this on, say, ENOENT: 2412 * open("doesnt_exist", O_RDONLY <unfinished ...> 2413 * {next syscall decode} 2414 * whereas the intended result is that open(...) line 2415 * is not shown at all. 2416 */ 2417 if (not_failing_only && tcp->u_error) 2418 goto ret; /* ignore failed syscalls */ 2419 sys_res = (*sysent[tcp->scno].sys_func)(tcp); 2420 } 2421 2422 tprints(") "); 2423 tabto(); 2424 u_error = tcp->u_error; 2425 if (!SCNO_IS_VALID(tcp->scno) || 2426 qual_flags[tcp->scno] & QUAL_RAW) { 2427 if (u_error) 2428 tprintf("= -1 (errno %ld)", u_error); 2429 else 2430 tprintf("= %#lx", tcp->u_rval); 2431 } 2432 else if (!(sys_res & RVAL_NONE) && u_error) { 2433 switch (u_error) { 2434 /* Blocked signals do not interrupt any syscalls. 2435 * In this case syscalls don't return ERESTARTfoo codes. 2436 * 2437 * Deadly signals set to SIG_DFL interrupt syscalls 2438 * and kill the process regardless of which of the codes below 2439 * is returned by the interrupted syscall. 2440 * In some cases, kernel forces a kernel-generated deadly 2441 * signal to be unblocked and set to SIG_DFL (and thus cause 2442 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV 2443 * or SIGILL. (The alternative is to leave process spinning 2444 * forever on the faulty instruction - not useful). 2445 * 2446 * SIG_IGNed signals and non-deadly signals set to SIG_DFL 2447 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls, 2448 * but kernel will always restart them. 2449 */ 2450 case ERESTARTSYS: 2451 /* Most common type of signal-interrupted syscall exit code. 2452 * The system call will be restarted with the same arguments 2453 * if SA_RESTART is set; otherwise, it will fail with EINTR. 2454 */ 2455 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)"); 2456 break; 2457 case ERESTARTNOINTR: 2458 /* Rare. For example, fork() returns this if interrupted. 2459 * SA_RESTART is ignored (assumed set): the restart is unconditional. 2460 */ 2461 tprints("= ? ERESTARTNOINTR (To be restarted)"); 2462 break; 2463 case ERESTARTNOHAND: 2464 /* pause(), rt_sigsuspend() etc use this code. 2465 * SA_RESTART is ignored (assumed not set): 2466 * syscall won't restart (will return EINTR instead) 2467 * even after signal with SA_RESTART set. 2468 * However, after SIG_IGN or SIG_DFL signal it will. 2469 */ 2470 tprints("= ? ERESTARTNOHAND (Interrupted by signal)"); 2471 break; 2472 case ERESTART_RESTARTBLOCK: 2473 /* Syscalls like nanosleep(), poll() which can't be 2474 * restarted with their original arguments use this 2475 * code. Kernel will execute restart_syscall() instead, 2476 * which changes arguments before restarting syscall. 2477 * SA_RESTART is ignored (assumed not set) similarly 2478 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART 2479 * since restart data is saved in "restart block" 2480 * in task struct, and if signal handler uses a syscall 2481 * which in turn saves another such restart block, 2482 * old data is lost and restart becomes impossible) 2483 */ 2484 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)"); 2485 break; 2486 default: 2487 if (u_error < 0) 2488 tprintf("= -1 E??? (errno %ld)", u_error); 2489 else if (u_error < nerrnos) 2490 tprintf("= -1 %s (%s)", errnoent[u_error], 2491 strerror(u_error)); 2492 else 2493 tprintf("= -1 ERRNO_%ld (%s)", u_error, 2494 strerror(u_error)); 2495 break; 2496 } 2497 if ((sys_res & RVAL_STR) && tcp->auxstr) 2498 tprintf(" (%s)", tcp->auxstr); 2499 } 2500 else { 2501 if (sys_res & RVAL_NONE) 2502 tprints("= ?"); 2503 else { 2504 switch (sys_res & RVAL_MASK) { 2505 case RVAL_HEX: 2506 tprintf("= %#lx", tcp->u_rval); 2507 break; 2508 case RVAL_OCTAL: 2509 tprintf("= %#lo", tcp->u_rval); 2510 break; 2511 case RVAL_UDECIMAL: 2512 tprintf("= %lu", tcp->u_rval); 2513 break; 2514 case RVAL_DECIMAL: 2515 tprintf("= %ld", tcp->u_rval); 2516 break; 2517#if defined(LINUX_MIPSN32) || defined(X32) 2518 /* 2519 case RVAL_LHEX: 2520 tprintf("= %#llx", tcp->u_lrval); 2521 break; 2522 case RVAL_LOCTAL: 2523 tprintf("= %#llo", tcp->u_lrval); 2524 break; 2525 */ 2526 case RVAL_LUDECIMAL: 2527 tprintf("= %llu", tcp->u_lrval); 2528 break; 2529 /* 2530 case RVAL_LDECIMAL: 2531 tprintf("= %lld", tcp->u_lrval); 2532 break; 2533 */ 2534#endif 2535 default: 2536 fprintf(stderr, 2537 "invalid rval format\n"); 2538 break; 2539 } 2540 } 2541 if ((sys_res & RVAL_STR) && tcp->auxstr) 2542 tprintf(" (%s)", tcp->auxstr); 2543 } 2544 if (Tflag) { 2545 tv_sub(&tv, &tv, &tcp->etime); 2546 tprintf(" <%ld.%06ld>", 2547 (long) tv.tv_sec, (long) tv.tv_usec); 2548 } 2549 tprints("\n"); 2550 dumpio(tcp); 2551 line_ended(); 2552 2553 ret: 2554 tcp->flags &= ~TCB_INSYSCALL; 2555 return 0; 2556} 2557 2558int 2559trace_syscall(struct tcb *tcp) 2560{ 2561 return exiting(tcp) ? 2562 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp); 2563} 2564