1 /*- 2 * Mach Operating System 3 * Copyright (c) 1991,1990 Carnegie Mellon University 4 * All Rights Reserved. 5 * 6 * Permission to use, copy, modify and distribute this software and its 7 * documentation is hereby granted, provided that both the copyright 8 * notice and this permission notice appear in all copies of the 9 * software, derivative works or modified versions, and any portions 10 * thereof, and that both notices appear in supporting documentation. 11 * 12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS 13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 15 * 16 * Carnegie Mellon requests users of this software to return to 17 * 18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 19 * School of Computer Science 20 * Carnegie Mellon University 21 * Pittsburgh PA 15213-3890 22 * 23 * any improvements or extensions that they make and grant Carnegie the 24 * rights to redistribute these changes. 25 */ 26 /* 27 * Author: David B. Golub, Carnegie Mellon University 28 * Date: 7/90 29 */ 30 31 /* 32 * Commands to run process. 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <sys/param.h> 39 #include <sys/kdb.h> 40 #include <sys/proc.h> 41 42 #include <machine/kdb.h> 43 #include <machine/pcb.h> 44 45 #include <vm/vm.h> 46 47 #include <ddb/ddb.h> 48 #include <ddb/db_break.h> 49 #include <ddb/db_access.h> 50 51 static int db_run_mode; 52 #define STEP_NONE 0 53 #define STEP_ONCE 1 54 #define STEP_RETURN 2 55 #define STEP_CALLT 3 56 #define STEP_CONTINUE 4 57 #define STEP_INVISIBLE 5 58 #define STEP_COUNT 6 59 60 static bool db_sstep_print; 61 static int db_loop_count; 62 static int db_call_depth; 63 64 int db_inst_count; 65 int db_load_count; 66 int db_store_count; 67 68 #ifdef SOFTWARE_SSTEP 69 db_breakpoint_t db_not_taken_bkpt = 0; 70 db_breakpoint_t db_taken_bkpt = 0; 71 #endif 72 73 #ifndef db_set_single_step 74 void db_set_single_step(void); 75 #endif 76 #ifndef db_clear_single_step 77 void db_clear_single_step(void); 78 #endif 79 #ifndef db_pc_is_singlestep 80 static bool 81 db_pc_is_singlestep(db_addr_t pc) 82 { 83 #ifdef SOFTWARE_SSTEP 84 if ((db_not_taken_bkpt != 0 && pc == db_not_taken_bkpt->address) 85 || (db_taken_bkpt != 0 && pc == db_taken_bkpt->address)) 86 return (true); 87 #endif 88 return (false); 89 } 90 #endif 91 92 bool 93 db_stop_at_pc(bool *is_breakpoint) 94 { 95 db_addr_t pc; 96 db_breakpoint_t bkpt; 97 98 pc = PC_REGS(); 99 100 if (db_pc_is_singlestep(pc)) 101 *is_breakpoint = false; 102 103 db_clear_single_step(); 104 db_clear_breakpoints(); 105 db_clear_watchpoints(); 106 107 #ifdef FIXUP_PC_AFTER_BREAK 108 if (*is_breakpoint) { 109 /* 110 * Breakpoint trap. Fix up the PC if the 111 * machine requires it. 112 */ 113 FIXUP_PC_AFTER_BREAK 114 pc = PC_REGS(); 115 } 116 #endif 117 118 /* 119 * Now check for a breakpoint at this address. 120 */ 121 bkpt = db_find_breakpoint_here(pc); 122 if (bkpt) { 123 if (--bkpt->count == 0) { 124 bkpt->count = bkpt->init_count; 125 *is_breakpoint = true; 126 return (true); /* stop here */ 127 } 128 } else if (*is_breakpoint) { 129 #ifdef BKPT_SKIP 130 BKPT_SKIP; 131 #endif 132 } 133 134 *is_breakpoint = false; 135 136 if (db_run_mode == STEP_INVISIBLE) { 137 db_run_mode = STEP_CONTINUE; 138 return (false); /* continue */ 139 } 140 if (db_run_mode == STEP_COUNT) { 141 return (false); /* continue */ 142 } 143 if (db_run_mode == STEP_ONCE) { 144 if (--db_loop_count > 0) { 145 if (db_sstep_print) { 146 db_printf("\t\t"); 147 db_print_loc_and_inst(pc); 148 } 149 return (false); /* continue */ 150 } 151 } 152 if (db_run_mode == STEP_RETURN) { 153 /* continue until matching return */ 154 db_expr_t ins; 155 156 ins = db_get_value(pc, sizeof(int), false); 157 if (!inst_trap_return(ins) && 158 (!inst_return(ins) || --db_call_depth != 0)) { 159 if (db_sstep_print) { 160 if (inst_call(ins) || inst_return(ins)) { 161 int i; 162 163 db_printf("[after %6d] ", db_inst_count); 164 for (i = db_call_depth; --i > 0; ) 165 db_printf(" "); 166 db_print_loc_and_inst(pc); 167 } 168 } 169 if (inst_call(ins)) 170 db_call_depth++; 171 return (false); /* continue */ 172 } 173 } 174 if (db_run_mode == STEP_CALLT) { 175 /* continue until call or return */ 176 db_expr_t ins; 177 178 ins = db_get_value(pc, sizeof(int), false); 179 if (!inst_call(ins) && 180 !inst_return(ins) && 181 !inst_trap_return(ins)) { 182 return (false); /* continue */ 183 } 184 } 185 db_run_mode = STEP_NONE; 186 return (true); 187 } 188 189 void 190 db_restart_at_pc(bool watchpt) 191 { 192 db_addr_t pc = PC_REGS(); 193 194 if ((db_run_mode == STEP_COUNT) || 195 (db_run_mode == STEP_RETURN) || 196 (db_run_mode == STEP_CALLT)) { 197 /* 198 * We are about to execute this instruction, 199 * so count it now. 200 */ 201 #ifdef SOFTWARE_SSTEP 202 db_expr_t ins = 203 #endif 204 db_get_value(pc, sizeof(int), false); 205 db_inst_count++; 206 db_load_count += inst_load(ins); 207 db_store_count += inst_store(ins); 208 #ifdef SOFTWARE_SSTEP 209 /* XXX works on mips, but... */ 210 if (inst_branch(ins) || inst_call(ins)) { 211 ins = db_get_value(next_instr_address(pc,1), 212 sizeof(int), false); 213 db_inst_count++; 214 db_load_count += inst_load(ins); 215 db_store_count += inst_store(ins); 216 } 217 #endif /* SOFTWARE_SSTEP */ 218 } 219 220 if (db_run_mode == STEP_CONTINUE) { 221 if (watchpt || db_find_breakpoint_here(pc)) { 222 /* 223 * Step over breakpoint/watchpoint. 224 */ 225 db_run_mode = STEP_INVISIBLE; 226 db_set_single_step(); 227 } else { 228 db_set_breakpoints(); 229 db_set_watchpoints(); 230 } 231 } else { 232 db_set_single_step(); 233 } 234 } 235 236 #ifdef SOFTWARE_SSTEP 237 /* 238 * Software implementation of single-stepping. 239 * If your machine does not have a trace mode 240 * similar to the vax or sun ones you can use 241 * this implementation, done for the mips. 242 * Just define the above conditional and provide 243 * the functions/macros defined below. 244 * 245 * extern bool 246 * inst_branch(), returns true if the instruction might branch 247 * extern unsigned 248 * branch_taken(), return the address the instruction might 249 * branch to 250 * db_getreg_val(); return the value of a user register, 251 * as indicated in the hardware instruction 252 * encoding, e.g. 8 for r8 253 * 254 * next_instr_address(pc,bd) returns the address of the first 255 * instruction following the one at "pc", 256 * which is either in the taken path of 257 * the branch (bd==1) or not. This is 258 * for machines (mips) with branch delays. 259 * 260 * A single-step may involve at most 2 breakpoints - 261 * one for branch-not-taken and one for branch taken. 262 * If one of these addresses does not already have a breakpoint, 263 * we allocate a breakpoint and save it here. 264 * These breakpoints are deleted on return. 265 */ 266 267 void 268 db_set_single_step(void) 269 { 270 db_addr_t pc = PC_REGS(), brpc; 271 unsigned inst; 272 273 /* 274 * User was stopped at pc, e.g. the instruction 275 * at pc was not executed. 276 */ 277 inst = db_get_value(pc, sizeof(int), false); 278 if (inst_branch(inst) || inst_call(inst) || inst_return(inst)) { 279 brpc = branch_taken(inst, pc); 280 if (brpc != pc) { /* self-branches are hopeless */ 281 db_taken_bkpt = db_set_temp_breakpoint(brpc); 282 } 283 pc = next_instr_address(pc, 1); 284 } 285 pc = next_instr_address(pc, 0); 286 db_not_taken_bkpt = db_set_temp_breakpoint(pc); 287 } 288 289 void 290 db_clear_single_step(void) 291 { 292 293 if (db_not_taken_bkpt != 0) { 294 db_delete_temp_breakpoint(db_not_taken_bkpt); 295 db_not_taken_bkpt = 0; 296 } 297 if (db_taken_bkpt != 0) { 298 db_delete_temp_breakpoint(db_taken_bkpt); 299 db_taken_bkpt = 0; 300 } 301 } 302 303 #endif /* SOFTWARE_SSTEP */ 304 305 extern int db_cmd_loop_done; 306 307 /* single-step */ 308 /*ARGSUSED*/ 309 void 310 db_single_step_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif) 311 { 312 bool print = false; 313 314 if (count == -1) 315 count = 1; 316 317 if (modif[0] == 'p') 318 print = true; 319 320 db_run_mode = STEP_ONCE; 321 db_loop_count = count; 322 db_sstep_print = print; 323 db_inst_count = 0; 324 db_load_count = 0; 325 db_store_count = 0; 326 327 db_cmd_loop_done = 1; 328 } 329 330 /* trace and print until call/return */ 331 /*ARGSUSED*/ 332 void 333 db_trace_until_call_cmd(db_expr_t addr, bool have_addr, db_expr_t count, 334 char *modif) 335 { 336 bool print = false; 337 338 if (modif[0] == 'p') 339 print = true; 340 341 db_run_mode = STEP_CALLT; 342 db_sstep_print = print; 343 db_inst_count = 0; 344 db_load_count = 0; 345 db_store_count = 0; 346 347 db_cmd_loop_done = 1; 348 } 349 350 /*ARGSUSED*/ 351 void 352 db_trace_until_matching_cmd(db_expr_t addr, bool have_addr, db_expr_t count, 353 char *modif) 354 { 355 bool print = false; 356 357 if (modif[0] == 'p') 358 print = true; 359 360 db_run_mode = STEP_RETURN; 361 db_call_depth = 1; 362 db_sstep_print = print; 363 db_inst_count = 0; 364 db_load_count = 0; 365 db_store_count = 0; 366 367 db_cmd_loop_done = 1; 368 } 369 370 /* continue */ 371 /*ARGSUSED*/ 372 void 373 db_continue_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif) 374 { 375 if (modif[0] == 'c') 376 db_run_mode = STEP_COUNT; 377 else 378 db_run_mode = STEP_CONTINUE; 379 db_inst_count = 0; 380 db_load_count = 0; 381 db_store_count = 0; 382 383 db_cmd_loop_done = 1; 384 } 385