xref: /illumos-gate/usr/src/cmd/mdb/common/kmdb/kmdb_fault.c (revision 88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Handling of unintentional faults (i.e. bugs) in the debugger.
31  */
32 
33 #include <stdlib.h>
34 
35 #include <kmdb/kmdb_fault.h>
36 #include <kmdb/kmdb_promif.h>
37 #include <kmdb/kmdb_kdi.h>
38 #include <kmdb/kmdb_dpi.h>
39 #include <mdb/mdb_debug.h>
40 #include <mdb/mdb_kreg.h>
41 #include <mdb/mdb_io_impl.h>
42 #include <mdb/mdb.h>
43 
44 void
45 kmdb_fault(kreg_t tt, kreg_t pc, kreg_t sp, int cpuid)
46 {
47 	int debug_self_confirm = 0, try;
48 	jmp_buf pcb, *old;
49 	char c;
50 
51 	/* Make absolutely sure */
52 	kmdb_kdi_system_claim();
53 
54 	try = 1;
55 	if (setjmp(pcb) != 0) {
56 		if (++try == 2) {
57 			mdb_iob_printf(mdb.m_err,
58 			    "\n*** First stack trace attempt failed.  "
59 			    "Trying safe mode.\n\n");
60 
61 			kmdb_fault_display(tt, pc, sp, 1);
62 		} else {
63 			mdb_iob_printf(mdb.m_err,
64 			    "\n*** Unable to print stack trace.\n");
65 		}
66 
67 	} else {
68 		old = kmdb_dpi_set_fault_hdlr(&pcb);
69 
70 		mdb_iob_printf(mdb.m_err, "\n*** Debugger Fault (CPU %d)\n\n",
71 		    cpuid);
72 		kmdb_fault_display(tt, pc, sp, 0);
73 	}
74 
75 	kmdb_dpi_restore_fault_hdlr(old);
76 
77 	if (mdb.m_term != NULL) {
78 		for (;;) {
79 			mdb_iob_printf(mdb.m_err, "\n%s: "
80 #if defined(__sparc)
81 			    "(o)bp, (p)anic"
82 #else
83 			    "reboo(t)"
84 #endif
85 			    ", or (d)ebug with self? ", mdb.m_pname);
86 			mdb_iob_flush(mdb.m_err);
87 
88 			if (IOP_READ(mdb.m_term, &c, sizeof (c)) != sizeof (c))
89 				goto fault_obp;
90 
91 			mdb_iob_printf(mdb.m_err, "\n");
92 
93 			switch (c) {
94 #ifdef __sparc
95 			case 'p':
96 				kmdb_dpi_kernpanic(cpuid);
97 				/*NOTREACHED*/
98 				continue;
99 #endif
100 
101 			case 'o':
102 			case 'O':
103 			case 't':
104 			case 'T':
105 				kmdb_dpi_enter_mon();
106 				continue;
107 
108 			case 'd':
109 			case 'D':
110 				/*
111 				 * Debug self - get confirmation, because they
112 				 * can't go back to their running system if
113 				 * they choose this one.
114 				 */
115 				if (debug_self_confirm == 0) {
116 					mdb_iob_printf(mdb.m_err,
117 					    "NOTE: You will not be able to "
118 					    "resume your system if you "
119 					    "choose this option.\nPlease "
120 					    "select 'd' again to confirm.\n");
121 					debug_self_confirm = 1;
122 					continue;
123 				}
124 
125 				kmdb_dpi_set_state(DPI_STATE_LOST, 0);
126 				return;
127 			}
128 		}
129 	}
130 
131 fault_obp:
132 	exit(1);
133 	/*NOTREACHED*/
134 }
135