17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate/*
307c478bdstevel@tonic-gate * Intel-specific portions of the debugger fault routines
317c478bdstevel@tonic-gate */
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate#include <sys/types.h>
347c478bdstevel@tonic-gate#include <sys/stack.h>
357c478bdstevel@tonic-gate#include <sys/frame.h>
367c478bdstevel@tonic-gate
377c478bdstevel@tonic-gate#include <kmdb/kmdb_fault.h>
387c478bdstevel@tonic-gate#include <kmdb/kmdb_asmutil.h>
397c478bdstevel@tonic-gate#include <mdb/mdb_kreg.h>
407c478bdstevel@tonic-gate#include <mdb/mdb.h>
417c478bdstevel@tonic-gate
427c478bdstevel@tonic-gate#define	MAX_STACK_FRAMES	30
437c478bdstevel@tonic-gate
447c478bdstevel@tonic-gatestatic void
457c478bdstevel@tonic-gateprint_frame(uintptr_t pc, int fnum, int safe)
467c478bdstevel@tonic-gate{
477c478bdstevel@tonic-gate	if (safe) {
487c478bdstevel@tonic-gate		/*
497c478bdstevel@tonic-gate		 * We exploded the first time around, so we want to try again,
507c478bdstevel@tonic-gate		 * this time without symbol names
517c478bdstevel@tonic-gate		 */
527c478bdstevel@tonic-gate		mdb_iob_printf(mdb.m_err, "    [%2d] %?p()\n", fnum, pc);
537c478bdstevel@tonic-gate	} else {
547c478bdstevel@tonic-gate		mdb_iob_printf(mdb.m_err, "    [%2d] %?p %A()\n", fnum, pc, pc);
557c478bdstevel@tonic-gate	}
567c478bdstevel@tonic-gate}
577c478bdstevel@tonic-gate
587c478bdstevel@tonic-gatestatic int
597c478bdstevel@tonic-gatevalid_frame(struct frame *fr)
607c478bdstevel@tonic-gate{
617c478bdstevel@tonic-gate	uintptr_t addr = (uintptr_t)fr;
627c478bdstevel@tonic-gate
637c478bdstevel@tonic-gate	if (addr & (STACK_ALIGN - 1)) {
647c478bdstevel@tonic-gate		mdb_iob_printf(mdb.m_err, "    mis-aligned frame (%p)\n", fr);
657c478bdstevel@tonic-gate		return (0);
667c478bdstevel@tonic-gate	}
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gate	return (1);
697c478bdstevel@tonic-gate}
707c478bdstevel@tonic-gate
717c478bdstevel@tonic-gatestatic void
727c478bdstevel@tonic-gateprint_stack(kreg_t sp, int safe)
737c478bdstevel@tonic-gate{
747c478bdstevel@tonic-gate	struct frame *fr = (struct frame *)sp;
757c478bdstevel@tonic-gate	int frnum = 1;
767c478bdstevel@tonic-gate
777c478bdstevel@tonic-gate	while (fr != NULL && valid_frame(fr) && fr->fr_savpc != 0 &&
787c478bdstevel@tonic-gate	    frnum < MAX_STACK_FRAMES) {
797c478bdstevel@tonic-gate		print_frame(fr->fr_savpc, frnum, safe);
807c478bdstevel@tonic-gate
817c478bdstevel@tonic-gate		fr = (struct frame *)fr->fr_savfp;
827c478bdstevel@tonic-gate		frnum++;
837c478bdstevel@tonic-gate	}
847c478bdstevel@tonic-gate}
857c478bdstevel@tonic-gate
867c478bdstevel@tonic-gatevoid
877c478bdstevel@tonic-gatekmdb_print_stack(void)
887c478bdstevel@tonic-gate{
897c478bdstevel@tonic-gate	print_stack(get_fp(), FALSE); /* show sym names */
907c478bdstevel@tonic-gate}
917c478bdstevel@tonic-gate
927c478bdstevel@tonic-gatevoid
937c478bdstevel@tonic-gatekmdb_fault_display(kreg_t trapno, kreg_t pc, kreg_t sp, int safe)
947c478bdstevel@tonic-gate{
957c478bdstevel@tonic-gate	mdb_iob_printf(mdb.m_err, "    trapno: %d, sp: %p, pc: %p", trapno,
967c478bdstevel@tonic-gate	    sp, pc);
977c478bdstevel@tonic-gate	if (!safe)
987c478bdstevel@tonic-gate		mdb_iob_printf(mdb.m_err, " %A", pc);
997c478bdstevel@tonic-gate	mdb_iob_printf(mdb.m_err, "\n\n");
1007c478bdstevel@tonic-gate
1017c478bdstevel@tonic-gate	if (mdb.m_dseg == NULL || mdb.m_dsegsz == 0) {
1027c478bdstevel@tonic-gate		mdb_iob_printf(mdb.m_err,
1037c478bdstevel@tonic-gate		    "\t*** Stack trace omitted because debugger segment size\n"
1047c478bdstevel@tonic-gate		    "\t*** and/or length not set.\n");
1057c478bdstevel@tonic-gate		return;
1067c478bdstevel@tonic-gate	}
1077c478bdstevel@tonic-gate
1087c478bdstevel@tonic-gate	if (!(sp - (uintptr_t)mdb.m_dseg < mdb.m_dsegsz)) {
1097c478bdstevel@tonic-gate		mdb_iob_printf(mdb.m_err,
1107c478bdstevel@tonic-gate		    "\t*** Stack trace omitted because sp (%p) isn't in the\n"
1117c478bdstevel@tonic-gate		    "\t*** debugger segment.\n", sp);
1127c478bdstevel@tonic-gate		return;
1137c478bdstevel@tonic-gate	}
1147c478bdstevel@tonic-gate
1157c478bdstevel@tonic-gate	/*
1167c478bdstevel@tonic-gate	 * We were on a kmdb stack when we took this fault.  We're going to
1177c478bdstevel@tonic-gate	 * assume that there's nothing weird on the stack, and that, therefore,
1187c478bdstevel@tonic-gate	 * we can use a simple algorithm to dump it.
1197c478bdstevel@tonic-gate	 */
1207c478bdstevel@tonic-gate
1217c478bdstevel@tonic-gate	print_stack(sp, safe);
1227c478bdstevel@tonic-gate}
123