xref: /illumos-gate/usr/src/cmd/mdb/intel/kmdb/kmdb_fault_isadep.c (revision 9164a50bf932130cbb5097a16f6986873ce0e6e5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Intel-specific portions of the debugger fault routines
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stack.h>
33 #include <sys/frame.h>
34 
35 #include <kmdb/kmdb_fault.h>
36 #include <kmdb/kmdb_asmutil.h>
37 #include <mdb/mdb_kreg.h>
38 #include <mdb/mdb.h>
39 
40 #define	MAX_STACK_FRAMES	30
41 
42 static void
43 print_frame(uintptr_t pc, int fnum, int safe)
44 {
45 	if (safe) {
46 		/*
47 		 * We exploded the first time around, so we want to try again,
48 		 * this time without symbol names
49 		 */
50 		mdb_iob_printf(mdb.m_err, "    [%2d] %?p()\n", fnum, pc);
51 	} else {
52 		mdb_iob_printf(mdb.m_err, "    [%2d] %?p %A()\n", fnum, pc, pc);
53 	}
54 }
55 
56 static int
57 valid_frame(struct frame *fr)
58 {
59 	uintptr_t addr = (uintptr_t)fr;
60 
61 	if (addr & (STACK_ALIGN - 1)) {
62 		mdb_iob_printf(mdb.m_err, "    mis-aligned frame (%p)\n", fr);
63 		return (0);
64 	}
65 
66 	return (1);
67 }
68 
69 static void
70 print_stack(kreg_t sp, int safe)
71 {
72 	struct frame *fr = (struct frame *)sp;
73 	int frnum = 1;
74 
75 	while (fr != NULL && valid_frame(fr) && fr->fr_savpc != 0 &&
76 	    frnum < MAX_STACK_FRAMES) {
77 		print_frame(fr->fr_savpc, frnum, safe);
78 
79 		fr = (struct frame *)fr->fr_savfp;
80 		frnum++;
81 	}
82 }
83 
84 void
85 kmdb_print_stack(void)
86 {
87 	print_stack(get_fp(), FALSE); /* show sym names */
88 }
89 
90 void
91 kmdb_fault_display(kreg_t trapno, kreg_t pc, kreg_t sp, int safe)
92 {
93 	mdb_iob_printf(mdb.m_err, "    trapno: %d, sp: %p, pc: %p", trapno,
94 	    sp, pc);
95 	if (!safe)
96 		mdb_iob_printf(mdb.m_err, " %A", pc);
97 	mdb_iob_printf(mdb.m_err, "\n\n");
98 
99 	if (mdb.m_dseg == NULL || mdb.m_dsegsz == 0) {
100 		mdb_iob_printf(mdb.m_err,
101 		    "\t*** Stack trace omitted because debugger segment size\n"
102 		    "\t*** and/or length not set.\n");
103 		return;
104 	}
105 
106 	if (!(sp - (uintptr_t)mdb.m_dseg < mdb.m_dsegsz)) {
107 		mdb_iob_printf(mdb.m_err,
108 		    "\t*** Stack trace omitted because sp (%p) isn't in the\n"
109 		    "\t*** debugger segment.\n", sp);
110 		return;
111 	}
112 
113 	/*
114 	 * We were on a kmdb stack when we took this fault.  We're going to
115 	 * assume that there's nothing weird on the stack, and that, therefore,
116 	 * we can use a simple algorithm to dump it.
117 	 */
118 
119 	print_stack(sp, safe);
120 }
121