1/*	$NetBSD: db_trap.c,v 1.26 2019/03/12 07:44:58 skrll Exp $	*/
2
3/*
4 * Mach Operating System
5 * Copyright (c) 1991,1990 Carnegie Mellon University
6 * All Rights Reserved.
7 *
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
13 *
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 *
18 * Carnegie Mellon requests users of this software to return to
19 *
20 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
21 *  School of Computer Science
22 *  Carnegie Mellon University
23 *  Pittsburgh PA 15213-3890
24 *
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
27 *
28 * 	Author: David B. Golub, Carnegie Mellon University
29 *	Date:	7/90
30 */
31
32/*
33 * Trap entry point to kernel debugger.
34 */
35
36#include <sys/cdefs.h>
37__KERNEL_RCSID(0, "$NetBSD: db_trap.c,v 1.26 2019/03/12 07:44:58 skrll Exp $");
38
39#include <sys/param.h>
40#include <sys/proc.h>
41
42#include <machine/db_machdep.h>
43
44#include <ddb/db_run.h>
45#include <ddb/db_command.h>
46#include <ddb/db_break.h>
47#include <ddb/db_output.h>
48#include <ddb/db_sym.h>
49#include <ddb/db_extern.h>
50
51/*
52 * db_trap_callback can be hooked by MD port code to handle special
53 * cases such as disabling hardware watchdogs while in DDB.
54 */
55void (*db_trap_callback)(int);
56
57int db_trap_type;
58
59void
60db_trap(int type, int code)
61{
62	bool	bkpt;
63	bool	watchpt;
64
65	db_trap_type = type;
66
67	bkpt = IS_BREAKPOINT_TRAP(type, code);
68	watchpt = IS_WATCHPOINT_TRAP(type, code);
69
70	if (db_trap_callback)
71		db_trap_callback(1);
72
73	if (db_stop_at_pc(DDB_REGS, &bkpt)) {
74		if (db_inst_count) {
75			db_printf("After %d instructions "
76			    "(%d loads, %d stores),\n",
77			    db_inst_count, db_load_count, db_store_count);
78		}
79		if (bkpt)
80			db_printf("Breakpoint");
81		else if (watchpt)
82			db_printf("Watchpoint");
83		else
84			db_printf("Stopped");
85		db_printf(" in pid %d.%d (%s) at\t",
86		    curproc->p_pid, curlwp->l_lid,
87		    curproc->p_comm);
88		db_dot = PC_REGS(DDB_REGS);
89		db_print_loc_and_inst(db_dot);
90
91		db_command_loop();
92	}
93
94	db_restart_at_pc(DDB_REGS, watchpt);
95
96	if (db_trap_callback)
97		db_trap_callback(0);
98}
99