blob: c474c833af580798cf9a54eaa720571ee37e19ae [file] [log] [blame]
Petr Machatae99af272012-10-26 00:29:52 +02001/*
2 * This file is part of ltrace.
Petr Machata44ae1882013-01-30 17:38:37 +01003 * Copyright (C) 2013 Petr Machata, Red Hat Inc.
Petr Machatae99af272012-10-26 00:29:52 +02004 * Copyright (C) 2004,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
Juan Cespedes5c3fe062004-06-14 18:08:37 +020023#include "config.h"
Juan Cespedes5c3fe062004-06-14 18:08:37 +020024
25#include <sys/types.h>
26#include "ptrace.h"
Petr Machata366c2f42012-02-09 19:34:36 +010027#include "proc.h"
Juan Cespedesf7281232009-06-25 16:11:21 +020028#include "common.h"
Juan Cespedes5c3fe062004-06-14 18:08:37 +020029
Juan Cespedesf1350522008-12-16 18:19:58 +010030void *
Petr Machata929bd572012-12-17 03:20:34 +010031get_instruction_pointer(struct process *proc)
32{
Ian Wienand2d45b1a2006-02-20 22:48:07 +010033 proc_archdep *a = (proc_archdep *) (proc->arch_ptr);
Juan Cespedes5c3fe062004-06-14 18:08:37 +020034 if (a->valid)
Juan Cespedes34584562009-07-25 16:10:39 +020035 return (void *)a->regs.pc;
Juan Cespedes5c3fe062004-06-14 18:08:37 +020036 return (void *)-1;
37}
38
Juan Cespedesf1350522008-12-16 18:19:58 +010039void
Petr Machata929bd572012-12-17 03:20:34 +010040set_instruction_pointer(struct process *proc, void *addr)
41{
Ian Wienand2d45b1a2006-02-20 22:48:07 +010042 proc_archdep *a = (proc_archdep *) (proc->arch_ptr);
Juan Cespedes5c3fe062004-06-14 18:08:37 +020043 if (a->valid)
Juan Cespedes34584562009-07-25 16:10:39 +020044 a->regs.pc = (long)addr;
Juan Cespedes5c3fe062004-06-14 18:08:37 +020045}
46
Juan Cespedesf1350522008-12-16 18:19:58 +010047void *
Petr Machata929bd572012-12-17 03:20:34 +010048get_stack_pointer(struct process *proc)
49{
Ian Wienand2d45b1a2006-02-20 22:48:07 +010050 proc_archdep *a = (proc_archdep *) (proc->arch_ptr);
Juan Cespedes5c3fe062004-06-14 18:08:37 +020051 if (a->valid)
Juan Cespedes34584562009-07-25 16:10:39 +020052 return (void *)a->regs.u_regs[UREG_I5];
Juan Cespedes5c3fe062004-06-14 18:08:37 +020053 return (void *)-1;
54}
55
Juan Cespedesf1350522008-12-16 18:19:58 +010056void *
Petr Machata929bd572012-12-17 03:20:34 +010057get_return_addr(struct process *proc, void *stack_pointer)
58{
Ian Wienand2d45b1a2006-02-20 22:48:07 +010059 proc_archdep *a = (proc_archdep *) (proc->arch_ptr);
Juan Cespedes5c3fe062004-06-14 18:08:37 +020060 unsigned int t;
61 if (!a->valid)
62 return (void *)-1;
63 /* Work around structure returns */
Juan Cespedes34584562009-07-25 16:10:39 +020064 t = ptrace(PTRACE_PEEKTEXT, proc->pid, a->regs.u_regs[UREG_I6] + 8, 0);
Juan Cespedes5c3fe062004-06-14 18:08:37 +020065 if (t < 0x400000)
Juan Cespedes34584562009-07-25 16:10:39 +020066 return (void *)a->regs.u_regs[UREG_I6] + 12;
67 return (void *)a->regs.u_regs[UREG_I6] + 8;
Juan Cespedes5c3fe062004-06-14 18:08:37 +020068}