Home | History | Annotate | Line # | Download | only in ia64
db_trace.c revision 1.2
      1 /*	$NetBSD: db_trace.c,v 1.2 2007/02/21 22:59:45 thorpej Exp $	*/
      2 
      3 /* Inspired by reading alpha/db_trace.c */
      4 
      5 /*-
      6  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      7  * All rights reserved.
      8  *
      9  * Author: Cherry G. Mathew
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 
     41 #include "opt_ddb.h"
     42 
     43 #include <sys/param.h>
     44 #include <sys/proc.h>
     45 #include <sys/systm.h>
     46 
     47 #include <machine/cpufunc.h>
     48 #include <machine/md_var.h>
     49 #include <machine/db_machdep.h>
     50 
     51 #include <ddb/db_sym.h>
     52 #include <ddb/db_access.h>
     53 #include <ddb/db_output.h>
     54 #include <ddb/db_variables.h>
     55 #include <ddb/db_interface.h>
     56 
     57 #include <ia64/unwind/decode.h>
     58 #include <ia64/unwind/stackframe.h>
     59 
     60 #if 0
     61 #define UNWIND_DIAGNOSTIC
     62 #endif
     63 
     64 #define debug_frame_dump_XXX(uwf) \
     65 	printf("Frame Dump: \n bsp = 0x%lx \n pfs = 0x%lx, SOL(pfs) = %lu \n rp = 0x%lx \n",  \
     66 	       uwf->bsp, uwf->pfs, IA64_CFM_SOL(uwf->pfs), uwf->rp);	\
     67 
     68 void
     69 initunwindframe(struct unwind_frame *uwf, struct trapframe *tf);
     70 void
     71 rewindframe(struct unwind_frame *uwf, db_addr_t ip);
     72 
     73 void
     74 db_stack_trace_print(db_expr_t addr, bool have_addr, db_expr_t count,
     75 		     const char *modif, void (*pr)(const char *, ...))
     76 {
     77 	char c;
     78 	const char *cp = modif;
     79 	bool trace_thread = FALSE;
     80 	bool trace_user = FALSE;
     81 	struct trapframe *tf;
     82 	struct unwind_frame current_frame;
     83 	db_addr_t ip;
     84 	const char *name;
     85 	db_sym_t sym;
     86 	db_expr_t offset;
     87 
     88 	while ((c = *cp++) != 0) {
     89 		trace_thread |= c == 't';
     90 		trace_user |= c == 'u';
     91 	}
     92 
     93 	if (trace_user) {
     94 		(*pr)("User-space stack tracing not implemented yet. \n");
     95 		return;
     96 	}
     97 	if (!have_addr) {
     98 		(*pr)("--Kernel Call Trace-- \n");
     99 
    100 		tf = DDB_REGS;
    101 		ip = tf->tf_special.iip + ((tf->tf_special.psr >> 41) & 3);
    102 
    103 		initunwindframe(&current_frame, tf);
    104 
    105 #ifdef UNWIND_DIAGNOSTIC
    106 		struct unwind_frame *uwf = &current_frame;
    107 		debug_frame_dump_XXX(uwf);
    108 #endif
    109 		patchunwindframe(&current_frame, ip - kernstart, kernstart);
    110 #ifdef UNWIND_DIAGNOSTIC
    111 		debug_frame_dump_XXX(uwf);
    112 #endif
    113 		/* Get into unwind loop. */
    114 
    115 		while(ip) {
    116 			sym = db_search_symbol(ip, DB_STGY_ANY, &offset);
    117 			db_symbol_values(sym, &name, NULL);
    118 			(*pr)("%s(...)\n", name);
    119 
    120 			ip = current_frame.rp;
    121 
    122 			if(!ip) break;
    123 
    124 			rewindframe(&current_frame, ip);
    125 		}
    126 
    127 		return;
    128 
    129 
    130 	} else (*pr) ("Unwind from arbitrary addresses unimplemented. \n");
    131 
    132 
    133 		if (trace_thread) {
    134 			(*pr)("trace by pid unimplemented. \n");
    135 			return;
    136 		}
    137 		else {
    138 			(*pr)("trace from arbitrary trap frame address unimplemented. \n");
    139 		}
    140 
    141 }
    142 
    143 extern db_addr_t ia64_unwindtab;
    144 extern vsize_t ia64_unwindtablen;
    145 
    146 
    147 /* Generates initial unwind frame context based on the contents
    148  * of the trap frame, by consulting the Unwind library
    149  * staterecord. If a register is of type enum UNSAVED, we fetch
    150  * the live value of the register from the trapframe.
    151  */
    152 
    153 void
    154 initunwindframe(struct unwind_frame *uwf, struct trapframe *tf)
    155 
    156 {
    157 
    158 	uwf->rp = tf->tf_special.rp;
    159 
    160 	/* ndirty = bsp - bspstore: , not the same as the definition in the spec.
    161 	 * Gave me hell for a day!
    162 	 * see: ia64/exception.S: exception_save_restore: */
    163 
    164 	uwf->bsp = tf->tf_special.bspstore + tf->tf_special.ndirty;
    165 	uwf->bsp = ia64_bsp_adjust_ret(uwf->bsp, IA64_CFM_SOF(tf->tf_special.cfm));
    166 #ifdef UNWIND_DIAGNOSTIC
    167 	printf("inituwframe(): SOF(cfm) = %lu \n", IA64_CFM_SOF(tf->tf_special.cfm));
    168 #endif
    169 	uwf->pfs = tf->tf_special.pfs;
    170 	uwf->sp = uwf->psp = tf->tf_special.sp;
    171 
    172 
    173 }
    174 
    175 
    176 
    177 /* Single step the frame backward.
    178  * Assumes unwind_frame is setup already.
    179  */
    180 
    181 void
    182 rewindframe(struct unwind_frame *uwf, db_addr_t ip)
    183 {
    184 /* XXX: Check for a stack switch */
    185 
    186 	uwf->bsp = ia64_bsp_adjust_ret(uwf->bsp, IA64_CFM_SOL(uwf->pfs));
    187 	uwf->sp = uwf->psp;
    188 
    189 	/* Pre-stomp frame dump */
    190 #ifdef UNWIND_DIAGNOSTIC
    191 	debug_frame_dump_XXX(uwf);
    192 #endif
    193 
    194 	/* Stomp on rp and pfs
    195 	 */
    196 	patchunwindframe(uwf, ip - kernstart, kernstart);
    197 
    198 #ifdef UNWIND_DIAGNOSTIC
    199 	debug_frame_dump_XXX(uwf);
    200 #endif
    201 
    202 }
    203