1 1.1 christos /* 2 1.1 christos * Copyright (c) 2010 The FreeBSD Foundation 3 1.1 christos * All rights reserved. 4 1.1 christos * 5 1.1 christos * This software was developed by Rui Paulo under sponsorship from the 6 1.1 christos * FreeBSD Foundation. 7 1.1 christos * 8 1.1 christos * Redistribution and use in source and binary forms, with or without 9 1.1 christos * modification, are permitted provided that the following conditions 10 1.1 christos * are met: 11 1.1 christos * 1. Redistributions of source code must retain the above copyright 12 1.1 christos * notice, this list of conditions and the following disclaimer. 13 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 christos * notice, this list of conditions and the following disclaimer in the 15 1.1 christos * documentation and/or other materials provided with the distribution. 16 1.1 christos * 17 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 1.1 christos * SUCH DAMAGE. 28 1.1 christos */ 29 1.1 christos 30 1.1 christos #include <sys/cdefs.h> 31 1.2 christos #ifdef __FBSDID 32 1.1 christos __FBSDID("$FreeBSD: head/lib/libproc/proc_regs.c 285003 2015-07-01 13:59:26Z br $"); 33 1.2 christos #else 34 1.4 christos __RCSID("$NetBSD: proc_regs.c,v 1.4 2015/09/25 19:09:38 christos Exp $"); 35 1.2 christos #endif 36 1.1 christos 37 1.1 christos #include <sys/types.h> 38 1.1 christos #include <sys/ptrace.h> 39 1.1 christos 40 1.1 christos #include <err.h> 41 1.1 christos #include <stdio.h> 42 1.1 christos #include <string.h> 43 1.1 christos #include <errno.h> 44 1.2 christos #include <sys/ucontext.h> 45 1.1 christos #include "_libproc.h" 46 1.1 christos 47 1.1 christos int 48 1.1 christos proc_regget(struct proc_handle *phdl, proc_reg_t reg, unsigned long *regvalue) 49 1.1 christos { 50 1.1 christos struct reg regs; 51 1.1 christos 52 1.1 christos if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD || 53 1.1 christos phdl->status == PS_IDLE) { 54 1.1 christos errno = ENOENT; 55 1.1 christos return (-1); 56 1.1 christos } 57 1.1 christos memset(®s, 0, sizeof(regs)); 58 1.4 christos if (ptrace(PT_GETREGS, proc_getpid(phdl), ®s, 0) < 0) 59 1.1 christos return (-1); 60 1.1 christos switch (reg) { 61 1.1 christos case REG_PC: 62 1.2 christos #ifdef PTRACE_REG_PC 63 1.2 christos *regvalue = PTRACE_REG_PC(®s); 64 1.2 christos #else 65 1.3 christos # error "Add support for your architecture" 66 1.2 christos #endif 67 1.1 christos break; 68 1.1 christos case REG_SP: 69 1.2 christos #ifdef PTRACE_REG_SP 70 1.2 christos *regvalue = PTRACE_REG_SP(®s); 71 1.2 christos #else 72 1.3 christos # error "Add support for your architecture" 73 1.2 christos #endif 74 1.1 christos break; 75 1.1 christos default: 76 1.1 christos DPRINTFX("ERROR: no support for reg number %d", reg); 77 1.1 christos return (-1); 78 1.1 christos } 79 1.1 christos 80 1.1 christos return (0); 81 1.1 christos } 82 1.1 christos 83 1.1 christos int 84 1.1 christos proc_regset(struct proc_handle *phdl, proc_reg_t reg, unsigned long regvalue) 85 1.1 christos { 86 1.1 christos struct reg regs; 87 1.1 christos 88 1.1 christos if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD || 89 1.1 christos phdl->status == PS_IDLE) { 90 1.1 christos errno = ENOENT; 91 1.1 christos return (-1); 92 1.1 christos } 93 1.4 christos if (ptrace(PT_GETREGS, proc_getpid(phdl), ®s, 0) < 0) 94 1.1 christos return (-1); 95 1.1 christos switch (reg) { 96 1.1 christos case REG_PC: 97 1.2 christos #ifdef PTRACE_REG_SET_PC 98 1.2 christos PTRACE_REG_SET_PC(®s, regvalue); 99 1.2 christos #else 100 1.3 christos # error "Add support for your architecture" 101 1.2 christos #endif 102 1.1 christos break; 103 1.1 christos case REG_SP: 104 1.2 christos #ifdef PTRACE_REG_SP 105 1.2 christos PTRACE_REG_SP(®s) = regvalue; 106 1.2 christos #else 107 1.3 christos # error "Add support for your architecture" 108 1.2 christos #endif 109 1.1 christos break; 110 1.1 christos default: 111 1.1 christos DPRINTFX("ERROR: no support for reg number %d", reg); 112 1.1 christos return (-1); 113 1.1 christos } 114 1.4 christos if (ptrace(PT_SETREGS, proc_getpid(phdl), ®s, 0) < 0) 115 1.1 christos return (-1); 116 1.1 christos 117 1.1 christos return (0); 118 1.1 christos } 119