regs.c revision e99af270a60891e68d465c4cd97dbe29cd1a05e4
1/* 2 * This file is part of ltrace. 3 * Copyright (C) 1998,2002,2004,2008,2009 Juan Cespedes 4 * Copyright (C) 2009 Juan Cespedes 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of the 9 * License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 19 * 02110-1301 USA 20 */ 21 22#include "config.h" 23 24#include <sys/types.h> 25#include <sys/ptrace.h> 26#include <asm/ptrace.h> 27 28#include "proc.h" 29#include "common.h" 30 31#if (!defined(PTRACE_PEEKUSER) && defined(PTRACE_PEEKUSR)) 32# define PTRACE_PEEKUSER PTRACE_PEEKUSR 33#endif 34 35#if (!defined(PTRACE_POKEUSER) && defined(PTRACE_POKEUSR)) 36# define PTRACE_POKEUSER PTRACE_POKEUSR 37#endif 38 39#define off_pc ((void *)60) 40#define off_lr ((void *)56) 41#define off_sp ((void *)52) 42 43void * 44get_instruction_pointer(Process *proc) { 45 return (void *)ptrace(PTRACE_PEEKUSER, proc->pid, off_pc, 0); 46} 47 48void 49set_instruction_pointer(Process *proc, void *addr) { 50 ptrace(PTRACE_POKEUSER, proc->pid, off_pc, addr); 51} 52 53void * 54get_stack_pointer(Process *proc) { 55 return (void *)ptrace(PTRACE_PEEKUSER, proc->pid, off_sp, 0); 56} 57 58/* really, this is given the *stack_pointer expecting 59 * a CISC architecture; in our case, we don't need that */ 60void * 61get_return_addr(Process *proc, void *stack_pointer) { 62 long addr = ptrace(PTRACE_PEEKUSER, proc->pid, off_lr, 0); 63 64 /* Remember & unset the thumb mode bit. XXX This is really a 65 * bit of a hack, as we assume that the following 66 * insert_breakpoint call will be related to this address. 67 * This interface should really be get_return_breakpoint, or 68 * maybe install_return_breakpoint. */ 69 proc->thumb_mode = addr & 1; 70 if (proc->thumb_mode) 71 addr &= ~1; 72 73 return (void *)addr; 74} 75 76void 77set_return_addr(Process *proc, void *addr) { 78 long iaddr = (int)addr | proc->thumb_mode; 79 ptrace(PTRACE_POKEUSER, proc->pid, off_lr, (void *)iaddr); 80} 81