1 /*
2  * This file is part of ltrace.
3  * Copyright (C) 2013 Petr Machata, Red Hat Inc.
4  * Copyright (C) 2002,2008,2009 Juan Cespedes
5  * Copyright (C) 2009 Juan Cespedes
6  * Copyright (C) 2008 Luis Machado, IBM Corporation
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  */
23 
24 #include "config.h"
25 
26 #include <sys/types.h>
27 #include <sys/ptrace.h>
28 #include <asm/ptrace.h>
29 #include <errno.h>
30 #include <error.h>
31 
32 #include "proc.h"
33 #include "common.h"
34 
35 #if (!defined(PTRACE_PEEKUSER) && defined(PTRACE_PEEKUSR))
36 # define PTRACE_PEEKUSER PTRACE_PEEKUSR
37 #endif
38 
39 #if (!defined(PTRACE_POKEUSER) && defined(PTRACE_POKEUSR))
40 # define PTRACE_POKEUSER PTRACE_POKEUSR
41 #endif
42 
43 void *
get_instruction_pointer(struct process * proc)44 get_instruction_pointer(struct process *proc)
45 {
46 	return (void *)ptrace(PTRACE_PEEKUSER, proc->pid, sizeof(long)*PT_NIP, 0);
47 }
48 
49 void
set_instruction_pointer(struct process * proc,void * addr)50 set_instruction_pointer(struct process *proc, void *addr)
51 {
52 	if (ptrace(PTRACE_POKEUSER, proc->pid, sizeof(long)*PT_NIP, addr) != 0)
53 		error(0, errno, "set_instruction_pointer");
54 }
55 
56 void *
get_stack_pointer(struct process * proc)57 get_stack_pointer(struct process *proc)
58 {
59 	return (void *)ptrace(PTRACE_PEEKUSER, proc->pid, sizeof(long)*PT_R1, 0);
60 }
61 
62 void *
get_return_addr(struct process * proc,void * stack_pointer)63 get_return_addr(struct process *proc, void *stack_pointer)
64 {
65 	return (void *)ptrace(PTRACE_PEEKUSER, proc->pid, sizeof(long)*PT_LNK, 0);
66 }
67