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 (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2006 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
44void
45kmdb_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#ifndef sun4v
82			    "(o)bp, "
83#endif /* sun4v */
84			    "(p)anic"
85#else
86			    "reboo(t)"
87#endif
88			    ", or (d)ebug with self? ", mdb.m_pname);
89			mdb_iob_flush(mdb.m_err);
90
91			if (IOP_READ(mdb.m_term, &c, sizeof (c)) != sizeof (c))
92				goto fault_obp;
93
94			mdb_iob_printf(mdb.m_err, "\n");
95
96			switch (c) {
97#ifdef __sparc
98			case 'p':
99				kmdb_dpi_kernpanic(cpuid);
100				/*NOTREACHED*/
101				continue;
102#endif
103
104#ifndef sun4v
105			case 'o':
106			case 'O':
107#endif /* sun4v */
108			case 't':
109			case 'T':
110				kmdb_dpi_enter_mon();
111				continue;
112
113			case 'd':
114			case 'D':
115				/*
116				 * Debug self - get confirmation, because they
117				 * can't go back to their running system if
118				 * they choose this one.
119				 */
120				if (debug_self_confirm == 0) {
121					mdb_iob_printf(mdb.m_err,
122					    "NOTE: You will not be able to "
123					    "resume your system if you "
124					    "choose this option.\nPlease "
125					    "select 'd' again to confirm.\n");
126					debug_self_confirm = 1;
127					continue;
128				}
129
130				kmdb_dpi_set_state(DPI_STATE_LOST, 0);
131				return;
132			}
133		}
134	}
135
136fault_obp:
137	exit(1);
138	/*NOTREACHED*/
139}
140