1e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata/*
2e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * This file is part of ltrace.
344ae188c8dcd2727e383dcac0d46c3f784d0da5fPetr Machata * Copyright (C) 2013 Petr Machata, Red Hat Inc.
4e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * Copyright (C) 2008,2009 Juan Cespedes
5e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * Copyright (C) 2006 Eric Vaitl, Cisco Systems, Inc.
6e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata *
7e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * This program is free software; you can redistribute it and/or
8e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * modify it under the terms of the GNU General Public License as
9e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * published by the Free Software Foundation; either version 2 of the
10e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * License, or (at your option) any later version.
11e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata *
12e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * This program is distributed in the hope that it will be useful, but
13e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * WITHOUT ANY WARRANTY; without even the implied warranty of
14e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * General Public License for more details.
16e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata *
17e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * You should have received a copy of the GNU General Public License
18e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * along with this program; if not, write to the Free Software
19e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * 02110-1301 USA
21e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata */
22e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata
231228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl#include "config.h"
24f670eea50e959eeb9da53d70cad8d43c19494ef0Juan Cespedes
251228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl#include <stddef.h>
261228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl#include <sys/types.h>
271228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl#include <sys/ptrace.h>
281228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl#include <asm/ptrace.h>
291228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl
30366c2f46d844f040458df9b7e35fc3b8527ed2d3Petr Machata#include "proc.h"
31f728123bd75a65a6a1536e198c3c30719e494e71Juan Cespedes#include "common.h"
3232ea1b1eaad1a07a120811bca9dd9f3524cc9256Oliver Spornitz#include "mips.h"
331228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl
341228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl#if (!defined(PTRACE_PEEKUSER) && defined(PTRACE_PEEKUSR))
351228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl# define PTRACE_PEEKUSER PTRACE_PEEKUSR
361228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl#endif
371228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl
381228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl#if (!defined(PTRACE_POKEUSER) && defined(PTRACE_POKEUSR))
391228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl# define PTRACE_POKEUSER PTRACE_POKEUSR
401228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl#endif
411228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl
421228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl/**
4332ea1b1eaad1a07a120811bca9dd9f3524cc9256Oliver Spornitz   \addtogroup mips
441228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl   @{
451228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl */
461228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl
471228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl
481228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl/**
491228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl   \param proc The process to work on.
501228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl   \return The current instruction pointer.
511228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl */
52f13505251e6402460f6cc7ec84e0d8ca91607b4fJuan Cespedesvoid *
53929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machataget_instruction_pointer(struct process *proc)
54929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata{
551228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl	return (void *)ptrace(PTRACE_PEEKUSER, proc->pid, off_pc, 0);
561228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl}
571228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl
581228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl/**
591228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl   \param proc The process to work on.
601228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl   \param addr The address to set to.
611228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl
62fea4a1285dc7bea02b0203845ad43117f091fc3eJuan Cespedes   Called by \c continue_after_breakpoint().
631228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl
641228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl   \todo Our mips kernel ptrace doesn't support PTRACE_SINGLESTEP, so
651228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl   we \c continue_process() after a breakpoint. Check if this is OK.
661228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl */
67f13505251e6402460f6cc7ec84e0d8ca91607b4fJuan Cespedesvoid
68929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machataset_instruction_pointer(struct process *proc, void *addr)
69929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata{
701228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl	ptrace(PTRACE_POKEUSER, proc->pid, off_pc, addr);
711228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl}
721228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl
731228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl/**
741228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl   \param proc The process to work on.
751228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl   \return The current stack pointer.
761228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl */
77f13505251e6402460f6cc7ec84e0d8ca91607b4fJuan Cespedesvoid *
78929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machataget_stack_pointer(struct process *proc)
79929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata{
801228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl	return (void *)ptrace(PTRACE_PEEKUSER, proc->pid, off_sp, 0);
811228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl}
821228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl
831228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl/**
841228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl   \param proc The process to work on.
851228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl   \param stack_pointer The current stack pointer for proc
86fea4a1285dc7bea02b0203845ad43117f091fc3eJuan Cespedes   \return The current return address.
871228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl
8803192f890af95bf9abf5ea1c3dc0998f6b12550dJuan Cespedes   Called by \c handle_breakpoint().
891228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl
901228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl   Mips uses r31 for the return address, so the stack_pointer is
911228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl   unused.
921228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl */
93f13505251e6402460f6cc7ec84e0d8ca91607b4fJuan Cespedesvoid *
94929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machataget_return_addr(struct process *proc, void *stack_pointer)
95929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata{
961228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl	return (void *)ptrace(PTRACE_PEEKUSER, proc->pid, off_lr, 0);
971228a91e6560c5e5ac4fdd051adc9b34bf9fc047Eric Vaitl}
98