db_trace.c revision 132482
1129198Scognet/* $NetBSD: db_trace.c,v 1.8 2003/01/17 22:28:48 thorpej Exp $ */ 2129198Scognet 3129198Scognet/* 4129198Scognet * Copyright (c) 2000, 2001 Ben Harris 5129198Scognet * Copyright (c) 1996 Scott K. Stevens 6129198Scognet * 7129198Scognet * Mach Operating System 8129198Scognet * Copyright (c) 1991,1990 Carnegie Mellon University 9129198Scognet * All Rights Reserved. 10129198Scognet * 11129198Scognet * Permission to use, copy, modify and distribute this software and its 12129198Scognet * documentation is hereby granted, provided that both the copyright 13129198Scognet * notice and this permission notice appear in all copies of the 14129198Scognet * software, derivative works or modified versions, and any portions 15129198Scognet * thereof, and that both notices appear in supporting documentation. 16129198Scognet * 17129198Scognet * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 18129198Scognet * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 19129198Scognet * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 20129198Scognet * 21129198Scognet * Carnegie Mellon requests users of this software to return to 22129198Scognet * 23129198Scognet * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 24129198Scognet * School of Computer Science 25129198Scognet * Carnegie Mellon University 26129198Scognet * Pittsburgh PA 15213-3890 27129198Scognet * 28129198Scognet * any improvements or extensions that they make and grant Carnegie the 29129198Scognet * rights to redistribute these changes. 30129198Scognet */ 31129198Scognet 32129198Scognet#include <sys/cdefs.h> 33129198Scognet__FBSDID("$FreeBSD: head/sys/arm/arm/db_trace.c 132482 2004-07-21 05:07:09Z marcel $"); 34129198Scognet#include <sys/param.h> 35132402Scognet#include <sys/systm.h> 36129198Scognet 37129198Scognet 38129198Scognet#include <sys/proc.h> 39129198Scognet#include <sys/user.h> 40132059Scognet#include <sys/kdb.h> 41129198Scognet#include <machine/armreg.h> 42129198Scognet#include <machine/asm.h> 43129198Scognet#include <machine/cpufunc.h> 44129198Scognet#include <machine/db_machdep.h> 45129198Scognet#include <machine/vmparam.h> 46129198Scognet#include <ddb/ddb.h> 47129198Scognet#include <ddb/db_access.h> 48129198Scognet#include <ddb/db_sym.h> 49129198Scognet#include <ddb/db_output.h> 50129198Scognet 51129198Scognet#define INKERNEL(va) (((vm_offset_t)(va)) >= VM_MIN_KERNEL_ADDRESS) 52129198Scognet 53129198Scognetint db_md_set_watchpoint(db_expr_t addr, db_expr_t size); 54129198Scognetint db_md_clr_watchpoint(db_expr_t addr, db_expr_t size); 55129198Scognetvoid db_md_list_watchpoints(void); 56129198Scognet/* 57129198Scognet * APCS stack frames are awkward beasts, so I don't think even trying to use 58129198Scognet * a structure to represent them is a good idea. 59129198Scognet * 60129198Scognet * Here's the diagram from the APCS. Increasing address is _up_ the page. 61129198Scognet * 62129198Scognet * save code pointer [fp] <- fp points to here 63129198Scognet * return link value [fp, #-4] 64129198Scognet * return sp value [fp, #-8] 65129198Scognet * return fp value [fp, #-12] 66129198Scognet * [saved v7 value] 67129198Scognet * [saved v6 value] 68129198Scognet * [saved v5 value] 69129198Scognet * [saved v4 value] 70129198Scognet * [saved v3 value] 71129198Scognet * [saved v2 value] 72129198Scognet * [saved v1 value] 73129198Scognet * [saved a4 value] 74129198Scognet * [saved a3 value] 75129198Scognet * [saved a2 value] 76129198Scognet * [saved a1 value] 77129198Scognet * 78129198Scognet * The save code pointer points twelve bytes beyond the start of the 79129198Scognet * code sequence (usually a single STM) that created the stack frame. 80129198Scognet * We have to disassemble it if we want to know which of the optional 81129198Scognet * fields are actually present. 82129198Scognet */ 83129198Scognet 84129198Scognet#define FR_SCP (0) 85129198Scognet#define FR_RLV (-1) 86129198Scognet#define FR_RSP (-2) 87129198Scognet#define FR_RFP (-3) 88129198Scognet 89132482Smarcelstatic void 90129198Scognetdb_stack_trace_cmd(addr, have_addr, count, modif) 91129198Scognet db_expr_t addr; 92129198Scognet int have_addr; 93129198Scognet db_expr_t count; 94129198Scognet char *modif; 95129198Scognet{ 96129198Scognet u_int32_t *frame, *lastframe; 97129198Scognet c_db_sym_t sym; 98129198Scognet db_expr_t pc; 99129198Scognet char c, *cp = modif; 100129198Scognet const char *name; 101129198Scognet db_expr_t value; 102129198Scognet db_expr_t offset; 103129198Scognet boolean_t kernel_only = TRUE; 104129198Scognet boolean_t trace_thread = FALSE; 105129198Scognet int scp_offset; 106129198Scognet 107132402Scognet if (kdb_frame == NULL && !have_addr) 108132059Scognet return; 109129198Scognet while ((c = *cp++) != 0) { 110129198Scognet if (c == 'u') 111129198Scognet kernel_only = FALSE; 112129198Scognet if (c == 't') 113129198Scognet trace_thread = TRUE; 114129198Scognet } 115129198Scognet 116129198Scognet if (!have_addr) 117132059Scognet frame = (u_int32_t *)(kdb_frame->tf_r11); 118129198Scognet else { 119129198Scognet if (trace_thread) { 120129198Scognet struct proc *p; 121129198Scognet struct thread *td; 122129198Scognet pid_t pid = (pid_t)addr; 123129198Scognet LIST_FOREACH(p, &allproc, p_list) { 124129198Scognet if (p->p_pid == pid) 125129198Scognet break; 126129198Scognet } 127129198Scognet 128129198Scognet if (p == NULL) { 129129198Scognet db_printf("not found\n"); 130129198Scognet return; 131129198Scognet } 132129198Scognet if (!(p->p_sflag & PS_INMEM)) { 133129198Scognet db_printf("swapped out\n"); 134129198Scognet return; 135129198Scognet } 136129198Scognet td = FIRST_THREAD_IN_PROC(p); 137129198Scognet frame = (u_int32_t *)(td->td_pcb->un_32.pcb32_r11); 138129198Scognet db_printf("at %p\n", frame); 139129198Scognet } else 140129198Scognet frame = (u_int32_t *)(addr); 141129198Scognet } 142129198Scognet lastframe = NULL; 143129198Scognet scp_offset = -(get_pc_str_offset() >> 2); 144129198Scognet 145129198Scognet while (count-- && frame != NULL) { 146129198Scognet db_addr_t scp; 147129198Scognet u_int32_t savecode; 148129198Scognet int r; 149129198Scognet u_int32_t *rp; 150129198Scognet const char *sep; 151129198Scognet 152129198Scognet /* 153129198Scognet * In theory, the SCP isn't guaranteed to be in the function 154129198Scognet * that generated the stack frame. We hope for the best. 155129198Scognet */ 156129198Scognet scp = frame[FR_SCP]; 157129198Scognet 158129198Scognet db_printsym(scp, DB_STGY_PROC); 159129198Scognet db_printf("\n\t"); 160132059Scognet pc = kdb_frame->tf_pc; 161129198Scognet sym = db_search_symbol(pc, DB_STGY_ANY, &offset); 162129198Scognet if (sym == C_DB_SYM_NULL) { 163129198Scognet value = 0; 164129198Scognet name = "(null)"; 165129198Scognet } else 166129198Scognet db_symbol_values(sym, &name, &value); 167129198Scognet db_printf("%s() at ", name); 168129198Scognet db_printsym(pc, DB_STGY_PROC); 169129198Scognet db_printf("\n"); 170129198Scognet#ifdef __PROG26 171129198Scognet db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV] & R15_PC); 172129198Scognet db_printsym(frame[FR_RLV] & R15_PC, DB_STGY_PROC); 173129198Scognet db_printf(")\n"); 174129198Scognet#else 175129198Scognet db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV]); 176129198Scognet db_printsym(frame[FR_RLV], DB_STGY_PROC); 177129198Scognet db_printf(")\n"); 178129198Scognet#endif 179129198Scognet db_printf("\trsp=0x%08x rfp=0x%08x", frame[FR_RSP], frame[FR_RFP]); 180129198Scognet 181129198Scognet savecode = ((u_int32_t *)scp)[scp_offset]; 182129198Scognet if ((savecode & 0x0e100000) == 0x08000000) { 183129198Scognet /* Looks like an STM */ 184129198Scognet rp = frame - 4; 185129198Scognet sep = "\n\t"; 186129198Scognet for (r = 10; r >= 0; r--) { 187129198Scognet if (savecode & (1 << r)) { 188129198Scognet db_printf("%sr%d=0x%08x", 189129198Scognet sep, r, *rp--); 190129198Scognet sep = (frame - rp) % 4 == 2 ? 191129198Scognet "\n\t" : " "; 192129198Scognet } 193129198Scognet } 194129198Scognet } 195129198Scognet 196129198Scognet db_printf("\n"); 197129198Scognet 198129198Scognet /* 199129198Scognet * Switch to next frame up 200129198Scognet */ 201129198Scognet if (frame[FR_RFP] == 0) 202129198Scognet break; /* Top of stack */ 203129198Scognet 204129198Scognet lastframe = frame; 205129198Scognet frame = (u_int32_t *)(frame[FR_RFP]); 206129198Scognet 207129198Scognet if (INKERNEL((int)frame)) { 208129198Scognet /* staying in kernel */ 209129198Scognet if (frame <= lastframe) { 210129198Scognet db_printf("Bad frame pointer: %p\n", frame); 211129198Scognet break; 212129198Scognet } 213129198Scognet } else if (INKERNEL((int)lastframe)) { 214129198Scognet /* switch from user to kernel */ 215129198Scognet if (kernel_only) 216129198Scognet break; /* kernel stack only */ 217129198Scognet } else { 218129198Scognet /* in user */ 219129198Scognet if (frame <= lastframe) { 220129198Scognet db_printf("Bad user frame pointer: %p\n", 221129198Scognet frame); 222129198Scognet break; 223129198Scognet } 224129198Scognet } 225129198Scognet } 226129198Scognet} 227129198Scognet 228129198Scognet/* XXX stubs */ 229129198Scognetvoid 230129198Scognetdb_md_list_watchpoints() 231129198Scognet{ 232129198Scognet} 233129198Scognet 234129198Scognetint 235129198Scognetdb_md_clr_watchpoint(db_expr_t addr, db_expr_t size) 236129198Scognet{ 237129198Scognet return (0); 238129198Scognet} 239129198Scognet 240129198Scognetint 241129198Scognetdb_md_set_watchpoint(db_expr_t addr, db_expr_t size) 242129198Scognet{ 243129198Scognet return (0); 244129198Scognet} 245132482Smarcel 246132059Scognetint 247132059Scognetdb_trace_thread(struct thread *thr, int count) 248129198Scognet{ 249132402Scognet uint32_t addr; 250129198Scognet 251132402Scognet if (thr == curthread) 252132402Scognet addr = (uint32_t)__builtin_frame_address(0); 253132402Scognet else 254132402Scognet addr = thr->td_pcb->un_32.pcb32_r11; 255132402Scognet db_stack_trace_cmd(addr, 1, -1, NULL); 256132059Scognet return (0); 257129198Scognet} 258132059Scognet 259132059Scognetvoid 260132059Scognetdb_trace_self(void) 261132059Scognet{ 262132402Scognet db_trace_thread(curthread, -1); 263132059Scognet} 264