1dd3cb568SWarner Losh /*-
2796df753SPedro F. Giffuni * SPDX-License-Identifier: MIT-CMU
3796df753SPedro F. Giffuni *
45b81b6b3SRodney W. Grimes * Mach Operating System
55b81b6b3SRodney W. Grimes * Copyright (c) 1991,1990 Carnegie Mellon University
65b81b6b3SRodney W. Grimes * All Rights Reserved.
75b81b6b3SRodney W. Grimes *
85b81b6b3SRodney W. Grimes * Permission to use, copy, modify and distribute this software and its
95b81b6b3SRodney W. Grimes * documentation is hereby granted, provided that both the copyright
105b81b6b3SRodney W. Grimes * notice and this permission notice appear in all copies of the
115b81b6b3SRodney W. Grimes * software, derivative works or modified versions, and any portions
125b81b6b3SRodney W. Grimes * thereof, and that both notices appear in supporting documentation.
135b81b6b3SRodney W. Grimes *
145b81b6b3SRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
155b81b6b3SRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
165b81b6b3SRodney W. Grimes * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
175b81b6b3SRodney W. Grimes *
185b81b6b3SRodney W. Grimes * Carnegie Mellon requests users of this software to return to
195b81b6b3SRodney W. Grimes *
205b81b6b3SRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
215b81b6b3SRodney W. Grimes * School of Computer Science
225b81b6b3SRodney W. Grimes * Carnegie Mellon University
235b81b6b3SRodney W. Grimes * Pittsburgh PA 15213-3890
245b81b6b3SRodney W. Grimes *
255b81b6b3SRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the
265b81b6b3SRodney W. Grimes * rights to redistribute these changes.
275b81b6b3SRodney W. Grimes */
285b81b6b3SRodney W. Grimes /*
295b81b6b3SRodney W. Grimes * Author: David B. Golub, Carnegie Mellon University
305b81b6b3SRodney W. Grimes * Date: 7/90
315b81b6b3SRodney W. Grimes */
325b81b6b3SRodney W. Grimes
335b81b6b3SRodney W. Grimes /*
345b81b6b3SRodney W. Grimes * Commands to run process.
355b81b6b3SRodney W. Grimes */
36753960f7SDavid E. O'Brien
37f540b106SGarrett Wollman #include <sys/param.h>
3837224cd3SMarcel Moolenaar #include <sys/kdb.h>
3920984f2fSPeter Wemm #include <sys/proc.h>
40*b7924341SAndrew Turner #include <sys/reg.h>
413f289c3fSJeff Roberson #include <sys/systm.h>
4237224cd3SMarcel Moolenaar
4337224cd3SMarcel Moolenaar #include <machine/kdb.h>
4437224cd3SMarcel Moolenaar #include <machine/pcb.h>
455b81b6b3SRodney W. Grimes
465ccbc3ccSBruce Evans #include <vm/vm.h>
475ccbc3ccSBruce Evans
485ccbc3ccSBruce Evans #include <ddb/ddb.h>
495b81b6b3SRodney W. Grimes #include <ddb/db_access.h>
503c40d3fcSConrad Meyer #include <ddb/db_break.h>
513c40d3fcSConrad Meyer #include <ddb/db_command.h>
525b81b6b3SRodney W. Grimes
535b81b6b3SRodney W. Grimes #define STEP_ONCE 1
545b81b6b3SRodney W. Grimes #define STEP_RETURN 2
555b81b6b3SRodney W. Grimes #define STEP_CALLT 3
565b81b6b3SRodney W. Grimes #define STEP_CONTINUE 4
575b81b6b3SRodney W. Grimes #define STEP_INVISIBLE 5
585b81b6b3SRodney W. Grimes #define STEP_COUNT 6
591e24fd3bSBruce Evans static int db_run_mode = STEP_CONTINUE;
605b81b6b3SRodney W. Grimes
61bd20334cSBruce Evans static bool db_sstep_multiple;
62cd508278SPedro F. Giffuni static bool db_sstep_print;
6325eb640dSPoul-Henning Kamp static int db_loop_count;
6425eb640dSPoul-Henning Kamp static int db_call_depth;
655b81b6b3SRodney W. Grimes
665b81b6b3SRodney W. Grimes int db_inst_count;
675b81b6b3SRodney W. Grimes int db_load_count;
685b81b6b3SRodney W. Grimes int db_store_count;
695b81b6b3SRodney W. Grimes
70cd508278SPedro F. Giffuni bool
db_stop_at_pc(int type,int code,bool * is_breakpoint,bool * is_watchpoint)715c48342fSBruce Evans db_stop_at_pc(int type, int code, bool *is_breakpoint, bool *is_watchpoint)
725b81b6b3SRodney W. Grimes {
730a95ab74SPedro F. Giffuni db_addr_t pc;
740a95ab74SPedro F. Giffuni db_breakpoint_t bkpt;
755b81b6b3SRodney W. Grimes
765c48342fSBruce Evans *is_breakpoint = IS_BREAKPOINT_TRAP(type, code);
775c48342fSBruce Evans *is_watchpoint = IS_WATCHPOINT_TRAP(type, code);
7875f88c7cSOlivier Houchard pc = PC_REGS();
7975f88c7cSOlivier Houchard
8037224cd3SMarcel Moolenaar db_clear_single_step();
815b81b6b3SRodney W. Grimes db_clear_breakpoints();
825b81b6b3SRodney W. Grimes db_clear_watchpoints();
835b81b6b3SRodney W. Grimes
845b81b6b3SRodney W. Grimes #ifdef FIXUP_PC_AFTER_BREAK
855b81b6b3SRodney W. Grimes if (*is_breakpoint) {
865b81b6b3SRodney W. Grimes /*
875b81b6b3SRodney W. Grimes * Breakpoint trap. Fix up the PC if the
885b81b6b3SRodney W. Grimes * machine requires it.
895b81b6b3SRodney W. Grimes */
905b81b6b3SRodney W. Grimes FIXUP_PC_AFTER_BREAK
9137224cd3SMarcel Moolenaar pc = PC_REGS();
925b81b6b3SRodney W. Grimes }
935b81b6b3SRodney W. Grimes #endif
945b81b6b3SRodney W. Grimes
955b81b6b3SRodney W. Grimes /*
965b81b6b3SRodney W. Grimes * Now check for a breakpoint at this address.
975b81b6b3SRodney W. Grimes */
985b81b6b3SRodney W. Grimes bkpt = db_find_breakpoint_here(pc);
995b81b6b3SRodney W. Grimes if (bkpt) {
1005b81b6b3SRodney W. Grimes if (--bkpt->count == 0) {
1015b81b6b3SRodney W. Grimes bkpt->count = bkpt->init_count;
1022b490bc7SPedro F. Giffuni *is_breakpoint = true;
1032b490bc7SPedro F. Giffuni return (true); /* stop here */
1045b81b6b3SRodney W. Grimes }
10510c458ccSBruce Evans return (false); /* continue the countdown */
1065b81b6b3SRodney W. Grimes } else if (*is_breakpoint) {
1073a0b4f25SDoug Rabson #ifdef BKPT_SKIP
1083a0b4f25SDoug Rabson BKPT_SKIP;
109ecbb00a2SDoug Rabson #endif
1105b81b6b3SRodney W. Grimes }
1115b81b6b3SRodney W. Grimes
112bd20334cSBruce Evans *is_breakpoint = false; /* might be a breakpoint, but not ours */
113bd20334cSBruce Evans
114bd20334cSBruce Evans /*
115e1e554a3SBruce Evans * If not stepping, then silently ignore single-step traps
116e1e554a3SBruce Evans * (except for clearing the single-step-flag above).
117e1e554a3SBruce Evans *
118bd20334cSBruce Evans * If stepping, then abort if the trap type is unexpected.
119bd20334cSBruce Evans * Breakpoints owned by us are expected and were handled above.
120bd20334cSBruce Evans * Single-steps are expected and are handled below. All others
121bd20334cSBruce Evans * are unexpected.
122bd20334cSBruce Evans *
123e1e554a3SBruce Evans * Only do either of these if the MD layer claims to classify
124e1e554a3SBruce Evans * single-step traps unambiguously (by defining IS_SSTEP_TRAP).
125e1e554a3SBruce Evans * Otherwise, fall through to the bad historical behaviour
126e1e554a3SBruce Evans * given by turning unexpected traps into expected traps: if not
127e1e554a3SBruce Evans * stepping, then expect only breakpoints and stop, and if
128e1e554a3SBruce Evans * stepping, then expect only single-steps and step.
129bd20334cSBruce Evans */
130e1e554a3SBruce Evans #ifdef IS_SSTEP_TRAP
131e1e554a3SBruce Evans if (db_run_mode == STEP_CONTINUE && IS_SSTEP_TRAP(type, code))
132e1e554a3SBruce Evans return (false);
133bd20334cSBruce Evans if (db_run_mode != STEP_CONTINUE && !IS_SSTEP_TRAP(type, code)) {
134bd20334cSBruce Evans printf("Stepping aborted\n");
135bd20334cSBruce Evans return (true);
136bd20334cSBruce Evans }
137e1e554a3SBruce Evans #endif
1385b81b6b3SRodney W. Grimes
1395b81b6b3SRodney W. Grimes if (db_run_mode == STEP_INVISIBLE) {
1405b81b6b3SRodney W. Grimes db_run_mode = STEP_CONTINUE;
1412b490bc7SPedro F. Giffuni return (false); /* continue */
1425b81b6b3SRodney W. Grimes }
1435b81b6b3SRodney W. Grimes if (db_run_mode == STEP_COUNT) {
1442b490bc7SPedro F. Giffuni return (false); /* continue */
1455b81b6b3SRodney W. Grimes }
1465b81b6b3SRodney W. Grimes if (db_run_mode == STEP_ONCE) {
1475b81b6b3SRodney W. Grimes if (--db_loop_count > 0) {
1485b81b6b3SRodney W. Grimes if (db_sstep_print) {
1495b81b6b3SRodney W. Grimes db_printf("\t\t");
1505b81b6b3SRodney W. Grimes db_print_loc_and_inst(pc);
1515b81b6b3SRodney W. Grimes }
1522b490bc7SPedro F. Giffuni return (false); /* continue */
1535b81b6b3SRodney W. Grimes }
1545b81b6b3SRodney W. Grimes }
1555b81b6b3SRodney W. Grimes if (db_run_mode == STEP_RETURN) {
1565b81b6b3SRodney W. Grimes /* continue until matching return */
15753b3e912SMaxime Henrion db_expr_t ins;
1585b81b6b3SRodney W. Grimes
1592b490bc7SPedro F. Giffuni ins = db_get_value(pc, sizeof(int), false);
1605b81b6b3SRodney W. Grimes if (!inst_trap_return(ins) &&
1615b81b6b3SRodney W. Grimes (!inst_return(ins) || --db_call_depth != 0)) {
1625b81b6b3SRodney W. Grimes if (db_sstep_print) {
1635b81b6b3SRodney W. Grimes if (inst_call(ins) || inst_return(ins)) {
1640a95ab74SPedro F. Giffuni int i;
1655b81b6b3SRodney W. Grimes
1665b81b6b3SRodney W. Grimes db_printf("[after %6d] ", db_inst_count);
1675b81b6b3SRodney W. Grimes for (i = db_call_depth; --i > 0; )
1685b81b6b3SRodney W. Grimes db_printf(" ");
1695b81b6b3SRodney W. Grimes db_print_loc_and_inst(pc);
1705b81b6b3SRodney W. Grimes }
1715b81b6b3SRodney W. Grimes }
1725b81b6b3SRodney W. Grimes if (inst_call(ins))
1735b81b6b3SRodney W. Grimes db_call_depth++;
1742b490bc7SPedro F. Giffuni return (false); /* continue */
1755b81b6b3SRodney W. Grimes }
1765b81b6b3SRodney W. Grimes }
1775b81b6b3SRodney W. Grimes if (db_run_mode == STEP_CALLT) {
1785b81b6b3SRodney W. Grimes /* continue until call or return */
17953b3e912SMaxime Henrion db_expr_t ins;
1805b81b6b3SRodney W. Grimes
1812b490bc7SPedro F. Giffuni ins = db_get_value(pc, sizeof(int), false);
1825b81b6b3SRodney W. Grimes if (!inst_call(ins) &&
1835b81b6b3SRodney W. Grimes !inst_return(ins) &&
1845b81b6b3SRodney W. Grimes !inst_trap_return(ins)) {
1852b490bc7SPedro F. Giffuni return (false); /* continue */
1865b81b6b3SRodney W. Grimes }
1875b81b6b3SRodney W. Grimes }
1882b490bc7SPedro F. Giffuni return (true);
1895b81b6b3SRodney W. Grimes }
1905b81b6b3SRodney W. Grimes
1915b81b6b3SRodney W. Grimes void
db_restart_at_pc(bool watchpt)192cd508278SPedro F. Giffuni db_restart_at_pc(bool watchpt)
1935b81b6b3SRodney W. Grimes {
1940a95ab74SPedro F. Giffuni db_addr_t pc = PC_REGS();
1955b81b6b3SRodney W. Grimes
1965b81b6b3SRodney W. Grimes if ((db_run_mode == STEP_COUNT) ||
197bd20334cSBruce Evans ((db_run_mode == STEP_ONCE) && db_sstep_multiple) ||
1985b81b6b3SRodney W. Grimes (db_run_mode == STEP_RETURN) ||
1995b81b6b3SRodney W. Grimes (db_run_mode == STEP_CALLT)) {
2005b81b6b3SRodney W. Grimes /*
2015b81b6b3SRodney W. Grimes * We are about to execute this instruction,
2025b81b6b3SRodney W. Grimes * so count it now.
2035b81b6b3SRodney W. Grimes */
2042b490bc7SPedro F. Giffuni db_get_value(pc, sizeof(int), false);
2055b81b6b3SRodney W. Grimes db_inst_count++;
2065b81b6b3SRodney W. Grimes db_load_count += inst_load(ins);
2075b81b6b3SRodney W. Grimes db_store_count += inst_store(ins);
2085b81b6b3SRodney W. Grimes }
2095b81b6b3SRodney W. Grimes
2105b81b6b3SRodney W. Grimes if (db_run_mode == STEP_CONTINUE) {
2115b81b6b3SRodney W. Grimes if (watchpt || db_find_breakpoint_here(pc)) {
2125b81b6b3SRodney W. Grimes /*
2135b81b6b3SRodney W. Grimes * Step over breakpoint/watchpoint.
2145b81b6b3SRodney W. Grimes */
2155b81b6b3SRodney W. Grimes db_run_mode = STEP_INVISIBLE;
21637224cd3SMarcel Moolenaar db_set_single_step();
2175b81b6b3SRodney W. Grimes } else {
2185b81b6b3SRodney W. Grimes db_set_breakpoints();
2195b81b6b3SRodney W. Grimes db_set_watchpoints();
2205b81b6b3SRodney W. Grimes }
2215b81b6b3SRodney W. Grimes } else {
22237224cd3SMarcel Moolenaar db_set_single_step();
2235b81b6b3SRodney W. Grimes }
2245b81b6b3SRodney W. Grimes }
2255b81b6b3SRodney W. Grimes
2265b81b6b3SRodney W. Grimes /* single-step */
2275b81b6b3SRodney W. Grimes /*ARGSUSED*/
2285b81b6b3SRodney W. Grimes void
db_single_step_cmd(db_expr_t addr,bool have_addr,db_expr_t count,char * modif)229cd508278SPedro F. Giffuni db_single_step_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
2305b81b6b3SRodney W. Grimes {
231cd508278SPedro F. Giffuni bool print = false;
2325b81b6b3SRodney W. Grimes
2335b81b6b3SRodney W. Grimes if (count == -1)
2345b81b6b3SRodney W. Grimes count = 1;
2355b81b6b3SRodney W. Grimes
2365b81b6b3SRodney W. Grimes if (modif[0] == 'p')
2372b490bc7SPedro F. Giffuni print = true;
2385b81b6b3SRodney W. Grimes
2395b81b6b3SRodney W. Grimes db_run_mode = STEP_ONCE;
2405b81b6b3SRodney W. Grimes db_loop_count = count;
241bd20334cSBruce Evans db_sstep_multiple = (count != 1);
2425b81b6b3SRodney W. Grimes db_sstep_print = print;
2435b81b6b3SRodney W. Grimes db_inst_count = 0;
2445b81b6b3SRodney W. Grimes db_load_count = 0;
2455b81b6b3SRodney W. Grimes db_store_count = 0;
2465b81b6b3SRodney W. Grimes
2475b81b6b3SRodney W. Grimes db_cmd_loop_done = 1;
2485b81b6b3SRodney W. Grimes }
2495b81b6b3SRodney W. Grimes
2505b81b6b3SRodney W. Grimes /* trace and print until call/return */
2515b81b6b3SRodney W. Grimes /*ARGSUSED*/
2525b81b6b3SRodney W. Grimes void
db_trace_until_call_cmd(db_expr_t addr,bool have_addr,db_expr_t count,char * modif)253cd508278SPedro F. Giffuni db_trace_until_call_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
254a41dd031SPedro F. Giffuni char *modif)
2555b81b6b3SRodney W. Grimes {
256cd508278SPedro F. Giffuni bool print = false;
2575b81b6b3SRodney W. Grimes
2585b81b6b3SRodney W. Grimes if (modif[0] == 'p')
2592b490bc7SPedro F. Giffuni print = true;
2605b81b6b3SRodney W. Grimes
2615b81b6b3SRodney W. Grimes db_run_mode = STEP_CALLT;
2625b81b6b3SRodney W. Grimes db_sstep_print = print;
2635b81b6b3SRodney W. Grimes db_inst_count = 0;
2645b81b6b3SRodney W. Grimes db_load_count = 0;
2655b81b6b3SRodney W. Grimes db_store_count = 0;
2665b81b6b3SRodney W. Grimes
2675b81b6b3SRodney W. Grimes db_cmd_loop_done = 1;
2685b81b6b3SRodney W. Grimes }
2695b81b6b3SRodney W. Grimes
2705b81b6b3SRodney W. Grimes /*ARGSUSED*/
2715b81b6b3SRodney W. Grimes void
db_trace_until_matching_cmd(db_expr_t addr,bool have_addr,db_expr_t count,char * modif)272cd508278SPedro F. Giffuni db_trace_until_matching_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
273cd508278SPedro F. Giffuni char *modif)
2745b81b6b3SRodney W. Grimes {
275cd508278SPedro F. Giffuni bool print = false;
2765b81b6b3SRodney W. Grimes
2775b81b6b3SRodney W. Grimes if (modif[0] == 'p')
2782b490bc7SPedro F. Giffuni print = true;
2795b81b6b3SRodney W. Grimes
2805b81b6b3SRodney W. Grimes db_run_mode = STEP_RETURN;
2815b81b6b3SRodney W. Grimes db_call_depth = 1;
2825b81b6b3SRodney W. Grimes db_sstep_print = print;
2835b81b6b3SRodney W. Grimes db_inst_count = 0;
2845b81b6b3SRodney W. Grimes db_load_count = 0;
2855b81b6b3SRodney W. Grimes db_store_count = 0;
2865b81b6b3SRodney W. Grimes
2875b81b6b3SRodney W. Grimes db_cmd_loop_done = 1;
2885b81b6b3SRodney W. Grimes }
2895b81b6b3SRodney W. Grimes
2905b81b6b3SRodney W. Grimes /* continue */
2915b81b6b3SRodney W. Grimes /*ARGSUSED*/
2925b81b6b3SRodney W. Grimes void
db_continue_cmd(db_expr_t addr,bool have_addr,db_expr_t count,char * modif)293cd508278SPedro F. Giffuni db_continue_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
2945b81b6b3SRodney W. Grimes {
2955b81b6b3SRodney W. Grimes if (modif[0] == 'c')
2965b81b6b3SRodney W. Grimes db_run_mode = STEP_COUNT;
2975b81b6b3SRodney W. Grimes else
2985b81b6b3SRodney W. Grimes db_run_mode = STEP_CONTINUE;
2995b81b6b3SRodney W. Grimes db_inst_count = 0;
3005b81b6b3SRodney W. Grimes db_load_count = 0;
3015b81b6b3SRodney W. Grimes db_store_count = 0;
3025b81b6b3SRodney W. Grimes
3035b81b6b3SRodney W. Grimes db_cmd_loop_done = 1;
3045b81b6b3SRodney W. Grimes }
305