trace.c revision f728123bd75a65a6a1536e198c3c30719e494e71
15c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#if HAVE_CONFIG_H 25c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#include "config.h" 35c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#endif 45c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes 55c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#include <sys/types.h> 65c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#include <sys/wait.h> 75c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#include <signal.h> 85c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#include <sys/ptrace.h> 95c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#include <asm/ptrace.h> 105c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes 11f728123bd75a65a6a1536e198c3c30719e494e71Juan Cespedes#include "common.h" 125c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#include "debug.h" 135c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes 145c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#if (!defined(PTRACE_PEEKUSER) && defined(PTRACE_PEEKUSR)) 155c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes# define PTRACE_PEEKUSER PTRACE_PEEKUSR 165c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#endif 175c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes 185c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#if (!defined(PTRACE_POKEUSER) && defined(PTRACE_POKEUSR)) 195c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes# define PTRACE_POKEUSER PTRACE_POKEUSR 205c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#endif 215c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes 22f13505251e6402460f6cc7ec84e0d8ca91607b4fJuan Cespedesvoid 23a8909f71e1421949c960f287217be6c42c286c0fJuan Cespedesget_arch_dep(Process *proc) { 245c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes} 255c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes 265c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes/* Returns 1 if syscall, 2 if sysret, 0 otherwise. 275c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes */ 28f13505251e6402460f6cc7ec84e0d8ca91607b4fJuan Cespedesint 29a8909f71e1421949c960f287217be6c42c286c0fJuan Cespedessyscall_p(Process *proc, int status, int *sysnum) { 302d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand if (WIFSTOPPED(status) 312d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) { 322d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand char *ip = get_instruction_pointer(proc) - 4; 335c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes long x = ptrace(PTRACE_PEEKTEXT, proc->pid, ip, 0); 345c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes debug(2, "instr: %016lx", x); 352d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand if ((x & 0xffffffff) != 0x00000083) 365c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes return 0; 372d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand *sysnum = 382d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand ptrace(PTRACE_PEEKUSER, proc->pid, 0 /* REG_R0 */ , 0); 393e94cbf0d388a5b5b49f8dbc60f4b24900de89aeJuan Cespedes if (proc->callstack_depth > 0 && 403e94cbf0d388a5b5b49f8dbc60f4b24900de89aeJuan Cespedes proc->callstack[proc->callstack_depth - 1].is_syscall && 413e94cbf0d388a5b5b49f8dbc60f4b24900de89aeJuan Cespedes proc->callstack[proc->callstack_depth - 1].c_un.syscall == *sysnum) { 425c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes return 2; 435c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes } 442d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand if (*sysnum >= 0 && *sysnum < 500) { 455c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes return 1; 465c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes } 475c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes } 485c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes return 0; 495c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes} 505c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes 51f13505251e6402460f6cc7ec84e0d8ca91607b4fJuan Cespedeslong 52a8909f71e1421949c960f287217be6c42c286c0fJuan Cespedesgimme_arg(enum tof type, Process *proc, int arg_num, arg_type_info *info) { 532d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand if (arg_num == -1) { /* return value */ 542d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand return ptrace(PTRACE_PEEKUSER, proc->pid, 0 /* REG_R0 */ , 0); 555c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes } 565c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes 572d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand if (type == LT_TOF_FUNCTION || type == LT_TOF_FUNCTIONR) { 582d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand if (arg_num <= 5) 592d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand return ptrace(PTRACE_PEEKUSER, proc->pid, 602d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand arg_num + 16 /* REG_A0 */ , 0); 615c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes else 622d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand return ptrace(PTRACE_PEEKTEXT, proc->pid, 632d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand proc->stack_pointer + 8 * (arg_num - 6), 642d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand 0); 652d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand } else if (type == LT_TOF_SYSCALL || type == LT_TOF_SYSCALLR) { 662d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand return ptrace(PTRACE_PEEKUSER, proc->pid, 672d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand arg_num + 16 /* REG_A0 */ , 0); 685c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes } else { 695c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes fprintf(stderr, "gimme_arg called with wrong arguments\n"); 705c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes exit(1); 715c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes } 725c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes return 0; 735c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes} 742d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand 75f13505251e6402460f6cc7ec84e0d8ca91607b4fJuan Cespedesvoid 76a8909f71e1421949c960f287217be6c42c286c0fJuan Cespedessave_register_args(enum tof type, Process *proc) { 775c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes} 78