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