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 41 #include <machine/kdb.h> 42 #include <machine/pcb.h> 43 44 #include <vm/vm.h> 45 46 #include <ddb/ddb.h> 47 #include <ddb/db_break.h> 48 #include <ddb/db_access.h> 49 50 static int db_run_mode; 51 #define STEP_NONE 0 52 #define STEP_ONCE 1 53 #define STEP_RETURN 2 54 #define STEP_CALLT 3 55 #define STEP_CONTINUE 4 56 #define STEP_INVISIBLE 5 57 #define STEP_COUNT 6 58 59 static boolean_t db_sstep_print; 60 static int db_loop_count; 61 static int db_call_depth; 62 63 int db_inst_count; 64 int db_load_count; 65 int db_store_count; 66 67 #ifndef db_set_single_step 68 void db_set_single_step(void); 69 #endif 70 #ifndef db_clear_single_step 71 void db_clear_single_step(void); 72 #endif 73 74 #ifdef SOFTWARE_SSTEP 75 db_breakpoint_t db_not_taken_bkpt = 0; 76 db_breakpoint_t db_taken_bkpt = 0; 77 #endif 78 79 boolean_t 80 db_stop_at_pc(is_breakpoint) 81 boolean_t *is_breakpoint; 82 { 83 register db_addr_t pc; 84 register db_breakpoint_t bkpt; 85 86 pc = PC_REGS(); 87 #ifdef SOFTWARE_SSTEP 88 if ((db_not_taken_bkpt != 0 && pc == db_not_taken_bkpt->address) 89 || (db_taken_bkpt != 0 && pc == db_taken_bkpt->address)) 90 *is_breakpoint = FALSE; 91 #endif 92 93 db_clear_single_step(); 94 db_clear_breakpoints(); 95 db_clear_watchpoints(); 96 97 #ifdef FIXUP_PC_AFTER_BREAK 98 if (*is_breakpoint) { 99 /* 100 * Breakpoint trap. Fix up the PC if the 101 * machine requires it. 102 */ 103 FIXUP_PC_AFTER_BREAK 104 pc = PC_REGS(); 105 } 106 #endif 107 108 /* 109 * Now check for a breakpoint at this address. 110 */ 111 bkpt = db_find_breakpoint_here(pc); 112 if (bkpt) { 113 if (--bkpt->count == 0) { 114 bkpt->count = bkpt->init_count; 115 *is_breakpoint = TRUE; 116 return (TRUE); /* stop here */ 117 } 118 } else if (*is_breakpoint) { 119 #ifdef BKPT_SKIP 120 BKPT_SKIP; 121 #endif 122 } 123 124 *is_breakpoint = FALSE; 125 126 if (db_run_mode == STEP_INVISIBLE) { 127 db_run_mode = STEP_CONTINUE; 128 return (FALSE); /* continue */ 129 } 130 if (db_run_mode == STEP_COUNT) { 131 return (FALSE); /* continue */ 132 } 133 if (db_run_mode == STEP_ONCE) { 134 if (--db_loop_count > 0) { 135 if (db_sstep_print) { 136 db_printf("\t\t"); 137 db_print_loc_and_inst(pc); 138 db_printf("\n"); 139 } 140 return (FALSE); /* continue */ 141 } 142 } 143 if (db_run_mode == STEP_RETURN) { 144 /* continue until matching return */ 145 db_expr_t ins; 146 147 ins = db_get_value(pc, sizeof(int), FALSE); 148 if (!inst_trap_return(ins) && 149 (!inst_return(ins) || --db_call_depth != 0)) { 150 if (db_sstep_print) { 151 if (inst_call(ins) || inst_return(ins)) { 152 register int i; 153 154 db_printf("[after %6d] ", db_inst_count); 155 for (i = db_call_depth; --i > 0; ) 156 db_printf(" "); 157 db_print_loc_and_inst(pc); 158 db_printf("\n"); 159 } 160 } 161 if (inst_call(ins)) 162 db_call_depth++; 163 return (FALSE); /* continue */ 164 } 165 } 166 if (db_run_mode == STEP_CALLT) { 167 /* continue until call or return */ 168 db_expr_t ins; 169 170 ins = db_get_value(pc, sizeof(int), FALSE); 171 if (!inst_call(ins) && 172 !inst_return(ins) && 173 !inst_trap_return(ins)) { 174 return (FALSE); /* continue */ 175 } 176 } 177 db_run_mode = STEP_NONE; 178 return (TRUE); 179 } 180 181 void 182 db_restart_at_pc(watchpt) 183 boolean_t watchpt; 184 { 185 register db_addr_t pc = PC_REGS(); 186 187 if ((db_run_mode == STEP_COUNT) || 188 (db_run_mode == STEP_RETURN) || 189 (db_run_mode == STEP_CALLT)) { 190 db_expr_t ins; 191 192 /* 193 * We are about to execute this instruction, 194 * so count it now. 195 */ 196 197 ins = db_get_value(pc, sizeof(int), FALSE); 198 db_inst_count++; 199 db_load_count += inst_load(ins); 200 db_store_count += inst_store(ins); 201 #ifdef SOFTWARE_SSTEP 202 /* XXX works on mips, but... */ 203 if (inst_branch(ins) || inst_call(ins)) { 204 ins = db_get_value(next_instr_address(pc,1), 205 sizeof(int), FALSE); 206 db_inst_count++; 207 db_load_count += inst_load(ins); 208 db_store_count += inst_store(ins); 209 } 210 #endif /* SOFTWARE_SSTEP */ 211 } 212 213 if (db_run_mode == STEP_CONTINUE) { 214 if (watchpt || db_find_breakpoint_here(pc)) { 215 /* 216 * Step over breakpoint/watchpoint. 217 */ 218 db_run_mode = STEP_INVISIBLE; 219 db_set_single_step(); 220 } else { 221 db_set_breakpoints(); 222 db_set_watchpoints(); 223 } 224 } else { 225 db_set_single_step(); 226 } 227 } 228 229 #ifdef SOFTWARE_SSTEP 230 /* 231 * Software implementation of single-stepping. 232 * If your machine does not have a trace mode 233 * similar to the vax or sun ones you can use 234 * this implementation, done for the mips. 235 * Just define the above conditional and provide 236 * the functions/macros defined below. 237 * 238 * extern boolean_t 239 * inst_branch(), returns true if the instruction might branch 240 * extern unsigned 241 * branch_taken(), return the address the instruction might 242 * branch to 243 * db_getreg_val(); return the value of a user register, 244 * as indicated in the hardware instruction 245 * encoding, e.g. 8 for r8 246 * 247 * next_instr_address(pc,bd) returns the address of the first 248 * instruction following the one at "pc", 249 * which is either in the taken path of 250 * the branch (bd==1) or not. This is 251 * for machines (mips) with branch delays. 252 * 253 * A single-step may involve at most 2 breakpoints - 254 * one for branch-not-taken and one for branch taken. 255 * If one of these addresses does not already have a breakpoint, 256 * we allocate a breakpoint and save it here. 257 * These breakpoints are deleted on return. 258 */ 259 260 void 261 db_set_single_step(void) 262 { 263 db_addr_t pc = PC_REGS(), brpc; 264 unsigned inst; 265 266 /* 267 * User was stopped at pc, e.g. the instruction 268 * at pc was not executed. 269 */ 270 inst = db_get_value(pc, sizeof(int), FALSE); 271 if (inst_branch(inst) || inst_call(inst)) { 272 brpc = branch_taken(inst, pc); 273 if (brpc != pc) { /* self-branches are hopeless */ 274 db_taken_bkpt = db_set_temp_breakpoint(brpc); 275 } 276 pc = next_instr_address(pc, 1); 277 } 278 pc = next_instr_address(pc, 0); 279 db_not_taken_bkpt = db_set_temp_breakpoint(pc); 280 } 281 282 void 283 db_clear_single_step(void) 284 { 285 286 if (db_not_taken_bkpt != 0) { 287 db_delete_temp_breakpoint(db_not_taken_bkpt); 288 db_not_taken_bkpt = 0; 289 } 290 if (db_taken_bkpt != 0) { 291 db_delete_temp_breakpoint(db_taken_bkpt); 292 db_taken_bkpt = 0; 293 } 294 } 295 296 #endif /* SOFTWARE_SSTEP */ 297 298 extern int db_cmd_loop_done; 299 300 /* single-step */ 301 /*ARGSUSED*/ 302 void 303 db_single_step_cmd(addr, have_addr, count, modif) 304 db_expr_t addr; 305 boolean_t have_addr; 306 db_expr_t count; 307 char * modif; 308 { 309 boolean_t print = FALSE; 310 311 if (count == -1) 312 count = 1; 313 314 if (modif[0] == 'p') 315 print = TRUE; 316 317 db_run_mode = STEP_ONCE; 318 db_loop_count = count; 319 db_sstep_print = print; 320 db_inst_count = 0; 321 db_load_count = 0; 322 db_store_count = 0; 323 324 db_cmd_loop_done = 1; 325 } 326 327 /* trace and print until call/return */ 328 /*ARGSUSED*/ 329 void 330 db_trace_until_call_cmd(addr, have_addr, count, modif) 331 db_expr_t addr; 332 boolean_t have_addr; 333 db_expr_t count; 334 char * modif; 335 { 336 boolean_t 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(addr, have_addr, count, modif) 353 db_expr_t addr; 354 boolean_t have_addr; 355 db_expr_t count; 356 char * modif; 357 { 358 boolean_t print = FALSE; 359 360 if (modif[0] == 'p') 361 print = TRUE; 362 363 db_run_mode = STEP_RETURN; 364 db_call_depth = 1; 365 db_sstep_print = print; 366 db_inst_count = 0; 367 db_load_count = 0; 368 db_store_count = 0; 369 370 db_cmd_loop_done = 1; 371 } 372 373 /* continue */ 374 /*ARGSUSED*/ 375 void 376 db_continue_cmd(addr, have_addr, count, modif) 377 db_expr_t addr; 378 boolean_t have_addr; 379 db_expr_t count; 380 char * modif; 381 { 382 if (modif[0] == 'c') 383 db_run_mode = STEP_COUNT; 384 else 385 db_run_mode = STEP_CONTINUE; 386 db_inst_count = 0; 387 db_load_count = 0; 388 db_store_count = 0; 389 390 db_cmd_loop_done = 1; 391 } 392