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