regs.c revision 929bd57ca202fd2f2e8485ebf65d683e664f67b5
1/*
2 * This file is part of ltrace.
3 * Copyright (C) 2004,2008,2009 Juan Cespedes
4 * Copyright (C) 2006 Ian Wienand
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 "ptrace.h"
26#include "proc.h"
27#include "common.h"
28
29void *
30get_instruction_pointer(struct process *proc)
31{
32	proc_archdep *a = (proc_archdep *) (proc->arch_ptr);
33	if (a->valid)
34		return (void *)a->regs.pc;
35	return (void *)-1;
36}
37
38void
39set_instruction_pointer(struct process *proc, void *addr)
40{
41	proc_archdep *a = (proc_archdep *) (proc->arch_ptr);
42	if (a->valid)
43		a->regs.pc = (long)addr;
44}
45
46void *
47get_stack_pointer(struct process *proc)
48{
49	proc_archdep *a = (proc_archdep *) (proc->arch_ptr);
50	if (a->valid)
51		return (void *)a->regs.u_regs[UREG_I5];
52	return (void *)-1;
53}
54
55void *
56get_return_addr(struct process *proc, void *stack_pointer)
57{
58	proc_archdep *a = (proc_archdep *) (proc->arch_ptr);
59	unsigned int t;
60	if (!a->valid)
61		return (void *)-1;
62	/* Work around structure returns */
63	t = ptrace(PTRACE_PEEKTEXT, proc->pid, a->regs.u_regs[UREG_I6] + 8, 0);
64	if (t < 0x400000)
65		return (void *)a->regs.u_regs[UREG_I6] + 12;
66	return (void *)a->regs.u_regs[UREG_I6] + 8;
67}
68
69void
70set_return_addr(struct process *proc, void *addr)
71{
72	proc_archdep *a = (proc_archdep *) (proc->arch_ptr);
73	if (!a->valid)
74		return;
75	ptrace(PTRACE_POKETEXT, proc->pid, a->regs.u_regs[UREG_I6] + 8, addr);
76}
77