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