1 /*
2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2008,2009 Juan Cespedes
5 * Copyright (C) 2006 Ian Wienand
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 */
22
23 #include "config.h"
24
25 #include <sys/types.h>
26 #include <sys/ptrace.h>
27 #include <errno.h>
28
29 #include <asm/ptrace_offsets.h>
30 #include <asm/rse.h>
31
32 #include <stddef.h>
33 #include "proc.h"
34 #include "common.h"
35
36 void *
get_instruction_pointer(struct process * proc)37 get_instruction_pointer(struct process *proc)
38 {
39 unsigned long ip = ptrace(PTRACE_PEEKUSER, proc->pid, PT_CR_IIP, 0);
40 unsigned long slot =
41 (ptrace(PTRACE_PEEKUSER, proc->pid, PT_CR_IPSR, 0) >> 41) & 3;
42
43 return (void *)(ip | slot);
44 }
45
46 void
set_instruction_pointer(struct process * proc,void * addr)47 set_instruction_pointer(struct process *proc, void *addr)
48 {
49
50 unsigned long newip = (unsigned long)addr;
51 unsigned long slot = (unsigned long)addr & 0xf;
52 unsigned long psr = ptrace(PTRACE_PEEKUSER, proc->pid, PT_CR_IPSR, 0);
53
54 psr &= ~(3UL << 41);
55 psr |= (slot & 0x3) << 41;
56
57 newip &= ~0xfUL;
58
59 ptrace(PTRACE_POKEUSER, proc->pid, PT_CR_IIP, (long)newip);
60 ptrace(PTRACE_POKEUSER, proc->pid, PT_CR_IPSR, psr);
61 }
62
63 void *
get_stack_pointer(struct process * proc)64 get_stack_pointer(struct process *proc)
65 {
66 long l = ptrace(PTRACE_PEEKUSER, proc->pid, PT_R12, 0);
67 if (l == -1 && errno)
68 return NULL;
69 return (void *)l;
70 }
71
72 void *
get_return_addr(struct process * proc,void * stack_pointer)73 get_return_addr(struct process *proc, void *stack_pointer)
74 {
75 long l = ptrace(PTRACE_PEEKUSER, proc->pid, PT_B0, 0);
76 if (l == -1 && errno)
77 return NULL;
78 return (void *)l;
79 }
80