summaryrefslogtreecommitdiff
path: root/usr/src/cmd/mdb/common/kmdb/kmdb_fault.c
blob: 06f7c2927f9e317cb5b5aae4b40c24192bb5c234 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Handling of unintentional faults (i.e. bugs) in the debugger.
 */

#include <stdlib.h>

#include <kmdb/kmdb_fault.h>
#include <kmdb/kmdb_promif.h>
#include <kmdb/kmdb_kdi.h>
#include <kmdb/kmdb_dpi.h>
#include <mdb/mdb_debug.h>
#include <mdb/mdb_kreg.h>
#include <mdb/mdb_io_impl.h>
#include <mdb/mdb.h>

void
kmdb_fault(kreg_t tt, kreg_t pc, kreg_t sp, int cpuid)
{
	int debug_self_confirm = 0, try;
	jmp_buf pcb, *old;
	char c;

	/* Make absolutely sure */
	kmdb_kdi_system_claim();

	try = 1;
	if (setjmp(pcb) != 0) {
		if (++try == 2) {
			mdb_iob_printf(mdb.m_err,
			    "\n*** First stack trace attempt failed.  "
			    "Trying safe mode.\n\n");

			kmdb_fault_display(tt, pc, sp, 1);
		} else {
			mdb_iob_printf(mdb.m_err,
			    "\n*** Unable to print stack trace.\n");
		}

	} else {
		old = kmdb_dpi_set_fault_hdlr(&pcb);

		mdb_iob_printf(mdb.m_err, "\n*** Debugger Fault (CPU %d)\n\n",
		    cpuid);
		kmdb_fault_display(tt, pc, sp, 0);
	}

	kmdb_dpi_restore_fault_hdlr(old);

	if (mdb.m_term != NULL) {
		for (;;) {
			mdb_iob_printf(mdb.m_err, "\n%s: "
#if defined(__sparc)
#ifndef sun4v
			    "(o)bp, "
#endif /* sun4v */
			    "(p)anic"
#else
			    "reboo(t)"
#endif
			    ", or (d)ebug with self? ", mdb.m_pname);
			mdb_iob_flush(mdb.m_err);

			if (IOP_READ(mdb.m_term, &c, sizeof (c)) != sizeof (c))
				goto fault_obp;

			mdb_iob_printf(mdb.m_err, "\n");

			switch (c) {
#ifdef __sparc
			case 'p':
				kmdb_dpi_kernpanic(cpuid);
				/*NOTREACHED*/
				continue;
#endif

#ifndef sun4v
			case 'o':
			case 'O':
#endif /* sun4v */
			case 't':
			case 'T':
				kmdb_dpi_enter_mon();
				continue;

			case 'd':
			case 'D':
				/*
				 * Debug self - get confirmation, because they
				 * can't go back to their running system if
				 * they choose this one.
				 */
				if (debug_self_confirm == 0) {
					mdb_iob_printf(mdb.m_err,
					    "NOTE: You will not be able to "
					    "resume your system if you "
					    "choose this option.\nPlease "
					    "select 'd' again to confirm.\n");
					debug_self_confirm = 1;
					continue;
				}

				kmdb_dpi_set_state(DPI_STATE_LOST, 0);
				return;
			}
		}
	}

fault_obp:
	exit(1);
	/*NOTREACHED*/
}