xref: /freebsd/lib/libproc/proc_regs.c (revision 7a7741af18d6c8a804cc643cb7ecda9d730c6aa6)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010 The FreeBSD Foundation
5  *
6  * This software was developed by Rui Paulo under sponsorship from the
7  * FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/ptrace.h>
33 
34 #include <err.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <errno.h>
38 
39 #include "_libproc.h"
40 
41 int
42 proc_regget(struct proc_handle *phdl, proc_reg_t reg, unsigned long *regvalue)
43 {
44 	struct reg regs;
45 
46 	if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
47 	    phdl->status == PS_IDLE) {
48 		errno = ENOENT;
49 		return (-1);
50 	}
51 	memset(&regs, 0, sizeof(regs));
52 	if (ptrace(PT_GETREGS, proc_getpid(phdl), (caddr_t)&regs, 0) < 0)
53 		return (-1);
54 	switch (reg) {
55 	case REG_PC:
56 #if defined(__aarch64__)
57 		*regvalue = regs.elr;
58 #elif defined(__amd64__)
59 		*regvalue = regs.r_rip;
60 #elif defined(__arm__)
61 		*regvalue = regs.r_pc;
62 #elif defined(__i386__)
63 		*regvalue = regs.r_eip;
64 #elif defined(__powerpc__)
65 		*regvalue = regs.pc;
66 #elif defined(__riscv)
67 		*regvalue = regs.sepc;
68 #endif
69 		break;
70 	case REG_SP:
71 #if defined(__aarch64__)
72 		*regvalue = regs.sp;
73 #elif defined(__amd64__)
74 		*regvalue = regs.r_rsp;
75 #elif defined(__arm__)
76 		*regvalue = regs.r_sp;
77 #elif defined(__i386__)
78 		*regvalue = regs.r_esp;
79 #elif defined(__powerpc__)
80 		*regvalue = regs.fixreg[1];
81 #elif defined(__riscv)
82 		*regvalue = regs.sp;
83 #endif
84 		break;
85 	default:
86 		DPRINTFX("ERROR: no support for reg number %d", reg);
87 		return (-1);
88 	}
89 
90 	return (0);
91 }
92 
93 int
94 proc_regset(struct proc_handle *phdl, proc_reg_t reg, unsigned long regvalue)
95 {
96 	struct reg regs;
97 
98 	if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
99 	    phdl->status == PS_IDLE) {
100 		errno = ENOENT;
101 		return (-1);
102 	}
103 	if (ptrace(PT_GETREGS, proc_getpid(phdl), (caddr_t)&regs, 0) < 0)
104 		return (-1);
105 	switch (reg) {
106 	case REG_PC:
107 #if defined(__aarch64__)
108 		regs.elr = regvalue;
109 #elif defined(__amd64__)
110 		regs.r_rip = regvalue;
111 #elif defined(__arm__)
112 		regs.r_pc = regvalue;
113 #elif defined(__i386__)
114 		regs.r_eip = regvalue;
115 #elif defined(__powerpc__)
116 		regs.pc = regvalue;
117 #elif defined(__riscv)
118 		regs.sepc = regvalue;
119 #endif
120 		break;
121 	case REG_SP:
122 #if defined(__aarch64__)
123 		regs.sp = regvalue;
124 #elif defined(__amd64__)
125 		regs.r_rsp = regvalue;
126 #elif defined(__arm__)
127 		regs.r_sp = regvalue;
128 #elif defined(__i386__)
129 		regs.r_esp = regvalue;
130 #elif defined(__powerpc__)
131 		regs.fixreg[1] = regvalue;
132 #elif defined(__riscv)
133 		regs.sp = regvalue;
134 #endif
135 		break;
136 	default:
137 		DPRINTFX("ERROR: no support for reg number %d", reg);
138 		return (-1);
139 	}
140 	if (ptrace(PT_SETREGS, proc_getpid(phdl), (caddr_t)&regs, 0) < 0)
141 		return (-1);
142 
143 	return (0);
144 }
145