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 * Intel-specific portions of the debugger fault routines
31 */
32
33#include <sys/types.h>
34#include <sys/stack.h>
35#include <sys/frame.h>
36
37#include <kmdb/kmdb_fault.h>
38#include <kmdb/kmdb_asmutil.h>
39#include <mdb/mdb_kreg.h>
40#include <mdb/mdb.h>
41
42#define	MAX_STACK_FRAMES	30
43
44static void
45print_frame(uintptr_t pc, int fnum, int safe)
46{
47	if (safe) {
48		/*
49		 * We exploded the first time around, so we want to try again,
50		 * this time without symbol names
51		 */
52		mdb_iob_printf(mdb.m_err, "    [%2d] %?p()\n", fnum, pc);
53	} else {
54		mdb_iob_printf(mdb.m_err, "    [%2d] %?p %A()\n", fnum, pc, pc);
55	}
56}
57
58static int
59valid_frame(struct frame *fr)
60{
61	uintptr_t addr = (uintptr_t)fr;
62
63	if (addr & (STACK_ALIGN - 1)) {
64		mdb_iob_printf(mdb.m_err, "    mis-aligned frame (%p)\n", fr);
65		return (0);
66	}
67
68	return (1);
69}
70
71static void
72print_stack(kreg_t sp, int safe)
73{
74	struct frame *fr = (struct frame *)sp;
75	int frnum = 1;
76
77	while (fr != NULL && valid_frame(fr) && fr->fr_savpc != 0 &&
78	    frnum < MAX_STACK_FRAMES) {
79		print_frame(fr->fr_savpc, frnum, safe);
80
81		fr = (struct frame *)fr->fr_savfp;
82		frnum++;
83	}
84}
85
86void
87kmdb_print_stack(void)
88{
89	print_stack(get_fp(), FALSE); /* show sym names */
90}
91
92void
93kmdb_fault_display(kreg_t trapno, kreg_t pc, kreg_t sp, int safe)
94{
95	mdb_iob_printf(mdb.m_err, "    trapno: %d, sp: %p, pc: %p", trapno,
96	    sp, pc);
97	if (!safe)
98		mdb_iob_printf(mdb.m_err, " %A", pc);
99	mdb_iob_printf(mdb.m_err, "\n\n");
100
101	if (mdb.m_dseg == NULL || mdb.m_dsegsz == 0) {
102		mdb_iob_printf(mdb.m_err,
103		    "\t*** Stack trace omitted because debugger segment size\n"
104		    "\t*** and/or length not set.\n");
105		return;
106	}
107
108	if (!(sp - (uintptr_t)mdb.m_dseg < mdb.m_dsegsz)) {
109		mdb_iob_printf(mdb.m_err,
110		    "\t*** Stack trace omitted because sp (%p) isn't in the\n"
111		    "\t*** debugger segment.\n", sp);
112		return;
113	}
114
115	/*
116	 * We were on a kmdb stack when we took this fault.  We're going to
117	 * assume that there's nothing weird on the stack, and that, therefore,
118	 * we can use a simple algorithm to dump it.
119	 */
120
121	print_stack(sp, safe);
122}
123