Tue Nov 21 20:40:24 2023 UTC ()
Reduce db_trace.c's exposure to proc/lwp.


(thorpej)
diff -r1.36 -r1.37 src/sys/arch/alpha/alpha/db_trace.c

cvs diff -r1.36 -r1.37 src/sys/arch/alpha/alpha/db_trace.c (expand / switch to unified diff)

--- src/sys/arch/alpha/alpha/db_trace.c 2023/11/21 20:29:47 1.36
+++ src/sys/arch/alpha/alpha/db_trace.c 2023/11/21 20:40:24 1.37
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: db_trace.c,v 1.36 2023/11/21 20:29:47 thorpej Exp $ */ 1/* $NetBSD: db_trace.c,v 1.37 2023/11/21 20:40:24 thorpej Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc. 4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center. 9 * NASA Ames Research Center.
10 * 10 *
11 * This code is derived from software contributed to The NetBSD Foundation 11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Ross Harvey. 12 * by Ross Harvey.
13 * 13 *
14 * Redistribution and use in source and binary forms, with or without 14 * Redistribution and use in source and binary forms, with or without
@@ -25,27 +25,27 @@ @@ -25,27 +25,27 @@
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE. 33 * POSSIBILITY OF SUCH DAMAGE.
34 */ 34 */
35 35
36#include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 36#include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
37 37
38__KERNEL_RCSID(0, "$NetBSD: db_trace.c,v 1.36 2023/11/21 20:29:47 thorpej Exp $"); 38__KERNEL_RCSID(0, "$NetBSD: db_trace.c,v 1.37 2023/11/21 20:40:24 thorpej Exp $");
39 39
40#include <sys/param.h> 40#include <sys/param.h>
41#include <sys/systm.h> 41#include <sys/systm.h>
42#include <sys/proc.h> 42#include <sys/proc.h>
43 43
44#include <machine/alpha.h> 44#include <machine/alpha.h>
45#include <machine/db_machdep.h> 45#include <machine/db_machdep.h>
46 46
47#include <alpha/alpha/db_instruction.h> 47#include <alpha/alpha/db_instruction.h>
48 48
49#include <ddb/db_sym.h> 49#include <ddb/db_sym.h>
50#include <ddb/db_access.h> 50#include <ddb/db_access.h>
51#include <ddb/db_variables.h> 51#include <ddb/db_variables.h>
@@ -123,29 +123,28 @@ do { \ @@ -123,29 +123,28 @@ do { \
123 pi->pi_frame_size += ins.operate_lit_format.literal; 123 pi->pi_frame_size += ins.operate_lit_format.literal;
124 } else if (ins.mem_format.opcode == op_stq && 124 } else if (ins.mem_format.opcode == op_stq &&
125 ins.mem_format.rb == 30 && 125 ins.mem_format.rb == 30 &&
126 ins.mem_format.ra != 31) { 126 ins.mem_format.ra != 31) {
127 /* Store of (non-zero) register onto the stack. */ 127 /* Store of (non-zero) register onto the stack. */
128 signed_immediate = (long)ins.mem_format.displacement; 128 signed_immediate = (long)ins.mem_format.displacement;
129 pi->pi_regmask |= 1 << ins.mem_format.ra; 129 pi->pi_regmask |= 1 << ins.mem_format.ra;
130 pi->pi_reg_offset[ins.mem_format.ra] = signed_immediate; 130 pi->pi_reg_offset[ins.mem_format.ra] = signed_immediate;
131 } 131 }
132 } 132 }
133} 133}
134 134
135static void 135static void
136decode_syscall(int number, struct proc *p, void (*pr)(const char *, ...)) 136decode_syscall(int number, void (*pr)(const char *, ...))
137{ 137{
138 
139 (*pr)(" (%d)", number); 138 (*pr)(" (%d)", number);
140} 139}
141 140
142static unsigned long 141static unsigned long
143db_alpha_tf_reg(struct trapframe *tf, unsigned int regno) 142db_alpha_tf_reg(struct trapframe *tf, unsigned int regno)
144{ 143{
145 unsigned long reg; 144 unsigned long reg;
146 145
147 db_read_bytes((db_addr_t)&tf->tf_regs[regno], sizeof(reg), 146 db_read_bytes((db_addr_t)&tf->tf_regs[regno], sizeof(reg),
148 (char *)&reg); 147 (char *)&reg);
149 return reg; 148 return reg;
150} 149}
151 150
@@ -165,46 +164,46 @@ db_stack_trace_print_ra(db_expr_t ra, bo @@ -165,46 +164,46 @@ db_stack_trace_print_ra(db_expr_t ra, bo
165 const char *modif, void (*pr)(const char *, ...)) 164 const char *modif, void (*pr)(const char *, ...))
166{ 165{
167 db_addr_t callpc, frame, symval; 166 db_addr_t callpc, frame, symval;
168 struct prologue_info pi; 167 struct prologue_info pi;
169 db_expr_t diff; 168 db_expr_t diff;
170 db_sym_t sym; 169 db_sym_t sym;
171 u_long tfps; 170 u_long tfps;
172 const char *symname; 171 const char *symname;
173 struct pcb *pcbp; 172 struct pcb *pcbp;
174 const char *cp = modif; 173 const char *cp = modif;
175 struct trapframe *tf; 174 struct trapframe *tf;
176 bool ra_from_tf; 175 bool ra_from_tf;
177 u_long last_ipl = ~0L; 176 u_long last_ipl = ~0L;
178 struct proc *p = NULL; 
179 struct lwp *l = NULL; 
180 char c; 177 char c;
181 bool trace_thread = false; 178 bool trace_thread = false;
182 bool lwpaddr = false; 179 bool lwpaddr = false;
183 180
184 while ((c = *cp++) != 0) { 181 while ((c = *cp++) != 0) {
185 trace_thread |= c == 't'; 182 trace_thread |= c == 't';
186 trace_thread |= c == 'a'; 183 trace_thread |= c == 'a';
187 lwpaddr |= c == 'a'; 184 lwpaddr |= c == 'a';
188 } 185 }
189 186
190 if (!have_addr) { 187 if (!have_addr) {
191 p = curproc; 
192 addr = DDB_REGS->tf_regs[FRAME_SP] - FRAME_SIZE * 8; 188 addr = DDB_REGS->tf_regs[FRAME_SP] - FRAME_SIZE * 8;
193 tf = (struct trapframe *)addr; 189 tf = (struct trapframe *)addr;
194 callpc = db_alpha_tf_reg(tf, FRAME_PC); 190 callpc = db_alpha_tf_reg(tf, FRAME_PC);
195 frame = (db_addr_t)tf + FRAME_SIZE * 8; 191 frame = (db_addr_t)tf + FRAME_SIZE * 8;
196 ra_from_tf = true; 192 ra_from_tf = true;
197 } else { 193 } else {
 194 struct proc *p = NULL;
 195 struct lwp *l = NULL;
 196
198 if (trace_thread) { 197 if (trace_thread) {
199 if (lwpaddr) { 198 if (lwpaddr) {
200 l = (struct lwp *)addr; 199 l = (struct lwp *)addr;
201 p = l->l_proc; 200 p = l->l_proc;
202 (*pr)("trace: pid %d ", p->p_pid); 201 (*pr)("trace: pid %d ", p->p_pid);
203 } else { 202 } else {
204 (*pr)("trace: pid %d ", (int)addr); 203 (*pr)("trace: pid %d ", (int)addr);
205 p = proc_find_raw(addr); 204 p = proc_find_raw(addr);
206 if (p == NULL) { 205 if (p == NULL) {
207 (*pr)("not found\n"); 206 (*pr)("not found\n");
208 return; 207 return;
209 } 208 }
210 l = LIST_FIRST(&p->p_lwps); 209 l = LIST_FIRST(&p->p_lwps);
@@ -283,27 +282,27 @@ db_stack_trace_print_ra(db_expr_t ra, bo @@ -283,27 +282,27 @@ db_stack_trace_print_ra(db_expr_t ra, bo
283 282
284 /* 283 /*
285 * If we are in a trap vector, frame points to a 284 * If we are in a trap vector, frame points to a
286 * trapframe. 285 * trapframe.
287 */ 286 */
288 if (db_alpha_sym_is_trap(symval)) { 287 if (db_alpha_sym_is_trap(symval)) {
289 tf = (struct trapframe *)frame; 288 tf = (struct trapframe *)frame;
290 289
291 (*pr)("--- %s", db_alpha_trapsym_description(symval)); 290 (*pr)("--- %s", db_alpha_trapsym_description(symval));
292 291
293 tfps = db_alpha_tf_reg(tf, FRAME_PS); 292 tfps = db_alpha_tf_reg(tf, FRAME_PS);
294 if (db_alpha_sym_is_syscall(symval)) { 293 if (db_alpha_sym_is_syscall(symval)) {
295 decode_syscall(db_alpha_tf_reg(tf, FRAME_V0), 294 decode_syscall(db_alpha_tf_reg(tf, FRAME_V0),
296 p, pr); 295 pr);
297 } 296 }
298 if ((tfps & ALPHA_PSL_IPL_MASK) != last_ipl) { 297 if ((tfps & ALPHA_PSL_IPL_MASK) != last_ipl) {
299 last_ipl = tfps & ALPHA_PSL_IPL_MASK; 298 last_ipl = tfps & ALPHA_PSL_IPL_MASK;
300 if (! db_alpha_sym_is_syscall(symval)) { 299 if (! db_alpha_sym_is_syscall(symval)) {
301 (*pr)(" (from ipl %ld)", last_ipl); 300 (*pr)(" (from ipl %ld)", last_ipl);
302 } 301 }
303 } 302 }
304 (*pr)(" ---\n"); 303 (*pr)(" ---\n");
305 if (tfps & ALPHA_PSL_USERMODE) { 304 if (tfps & ALPHA_PSL_USERMODE) {
306 (*pr)("--- user mode ---\n"); 305 (*pr)("--- user mode ---\n");
307 break; /* Terminate search. */ 306 break; /* Terminate search. */
308 } 307 }
309 callpc = db_alpha_tf_reg(tf, FRAME_PC); 308 callpc = db_alpha_tf_reg(tf, FRAME_PC);