1/* 2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28/* 29 * @OSF_COPYRIGHT@ 30 */ 31/* 32 * Mach Operating System 33 * Copyright (c) 1991,1990 Carnegie Mellon University 34 * All Rights Reserved. 35 * 36 * Permission to use, copy, modify and distribute this software and its 37 * documentation is hereby granted, provided that both the copyright 38 * notice and this permission notice appear in all copies of the 39 * software, derivative works or modified versions, and any portions 40 * thereof, and that both notices appear in supporting documentation. 41 * 42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 45 * 46 * Carnegie Mellon requests users of this software to return to 47 * 48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 49 * School of Computer Science 50 * Carnegie Mellon University 51 * Pittsburgh PA 15213-3890 52 * 53 * any improvements or extensions that they make and grant Carnegie Mellon 54 * the rights to redistribute these changes. 55 */ 56/* 57 */ 58/* 59 * Author: David B. Golub, Carnegie Mellon University 60 * Date: 7/90 61 */ 62 63/* 64 * Commands to run process. 65 */ 66#include <mach/boolean.h> 67#include <machine/db_machdep.h> 68 69#include <ddb/db_lex.h> 70#include <ddb/db_break.h> 71#include <ddb/db_access.h> 72#include <ddb/db_run.h> 73#include <ddb/db_cond.h> 74#include <ddb/db_examine.h> 75#include <ddb/db_output.h> /* For db_printf() */ 76#include <ddb/db_watch.h> 77#include <kern/misc_protos.h> 78#include <kern/debug.h> 79 80#include <IOKit/IOPlatformExpert.h> 81 82boolean_t db_sstep_print; 83int db_loop_count; 84int db_call_depth; 85 86int db_inst_count; 87int db_last_inst_count; 88int db_load_count; 89int db_store_count; 90int db_max_inst_count = 1000; 91 92#ifndef db_set_single_step 93void db_set_task_single_step( 94 register db_regs_t *regs, 95 task_t task); 96#else 97#define db_set_task_single_step(regs,task) db_set_single_step(regs) 98#endif 99#ifndef db_clear_single_step 100void db_clear_task_single_step( 101 db_regs_t *regs, 102 task_t task); 103#else 104#define db_clear_task_single_step(regs,task) db_clear_single_step(regs) 105#endif 106 107extern jmp_buf_t *db_recover; 108boolean_t db_step_again(void); 109 110static db_addr_t db_stop_pc; 111boolean_t 112db_stop_at_pc( 113 boolean_t *is_breakpoint, 114 task_t task, 115 task_t space) 116{ 117 register db_thread_breakpoint_t bkpt; 118 119 db_clear_task_single_step(DDB_REGS, space); 120 db_clear_breakpoints(); 121 db_clear_watchpoints(); 122 db_stop_pc = PC_REGS(DDB_REGS); 123 124#ifdef FIXUP_PC_AFTER_BREAK 125 if (*is_breakpoint) { 126 /* 127 * Breakpoint trap. Fix up the PC if the 128 * machine requires it. 129 */ 130 FIXUP_PC_AFTER_BREAK 131 db_stop_pc = PC_REGS(DDB_REGS); 132 } 133#endif 134 135 /* 136 * Now check for a breakpoint at this address. 137 */ 138 bkpt = db_find_thread_breakpoint_here(space, db_stop_pc); 139 if (bkpt) { 140 if (db_cond_check(bkpt)) { 141 *is_breakpoint = TRUE; 142 return (TRUE); /* stop here */ 143 } 144 } 145 *is_breakpoint = FALSE; 146 147 if (db_run_mode == STEP_INVISIBLE) { 148 db_run_mode = STEP_CONTINUE; 149 return (FALSE); /* continue */ 150 } 151 if (db_run_mode == STEP_COUNT) { 152 return (FALSE); /* continue */ 153 } 154 if (db_run_mode == STEP_ONCE) { 155 if (--db_loop_count > 0) { 156 if (db_sstep_print) { 157 db_print_loc_and_inst(db_stop_pc, task); 158 } 159 return (FALSE); /* continue */ 160 } 161 } 162 if (db_run_mode == STEP_RETURN) { 163 jmp_buf_t *prev; 164 jmp_buf_t db_jmpbuf; 165 /* WARNING: the following assumes an instruction fits an int */ 166 db_expr_t ins; 167 168 ins = db_get_task_value(db_stop_pc, sizeof(int), FALSE, space); 169 170 /* continue until matching return */ 171 172 prev = db_recover; 173 if (_setjmp(db_recover = &db_jmpbuf) == 0) { 174 if (!inst_trap_return(ins) && 175 (!inst_return(ins) || --db_call_depth != 0)) { 176 if (db_sstep_print) { 177 if (inst_call(ins) || inst_return(ins)) { 178 register int i; 179 180 db_printf("[after %6d /%4d] ", 181 db_inst_count, 182 db_inst_count - db_last_inst_count); 183 db_last_inst_count = db_inst_count; 184 for (i = db_call_depth; --i > 0; ) 185 db_printf(" "); 186 db_print_loc_and_inst(db_stop_pc, task); 187 db_printf("\n"); 188 } 189 } 190 if (inst_call(ins)) 191 db_call_depth++; 192 db_recover = prev; 193 if (db_step_again()) 194 return (FALSE); /* continue */ 195 } 196 } 197 db_recover = prev; 198 } 199 if (db_run_mode == STEP_CALLT) { 200 /* WARNING: the following assumes an instruction fits an int */ 201 db_expr_t ins; 202 ins = db_get_task_value(db_stop_pc, sizeof(int), FALSE, space); 203 204 /* continue until call or return */ 205 206 if (!inst_call(ins) && 207 !inst_return(ins) && 208 !inst_trap_return(ins)) { 209 if (db_step_again()) 210 return (FALSE); /* continue */ 211 } 212 } 213 if (db_find_breakpoint_here(space, db_stop_pc)) 214 return(FALSE); 215 db_run_mode = STEP_NONE; 216 return (TRUE); 217} 218 219void 220db_restart_at_pc( 221 boolean_t watchpt, 222 task_t task) 223{ 224 db_addr_t pc = PC_REGS(DDB_REGS); 225#ifdef SOFTWARE_SSTEP 226 db_addr_t brpc; 227#endif 228 229 230 if ((db_run_mode == STEP_COUNT) || 231 (db_run_mode == STEP_RETURN) || 232 (db_run_mode == STEP_CALLT)) { 233 db_expr_t ins; 234 235 /* 236 * We are about to execute this instruction, 237 * so count it now. 238 */ 239 240 ins = db_get_task_value(pc, sizeof(int), FALSE, task); 241 db_inst_count++; 242 db_load_count += db_inst_load(ins); 243 db_store_count += db_inst_store(ins); 244#ifdef SOFTWARE_SSTEP 245 /* Account for instructions in delay slots */ 246 brpc = next_instr_address(pc,1,task); 247 if ((brpc != pc) && (inst_branch(ins) || inst_call(ins))) { 248 /* Note: this ~assumes an instruction <= sizeof(int) */ 249 ins = db_get_task_value(brpc, sizeof(int), FALSE, task); 250 db_inst_count++; 251 db_load_count += db_inst_load(ins); 252 db_store_count += db_inst_store(ins); 253 } 254#endif /* SOFTWARE_SSTEP */ 255 } 256 257 if (db_run_mode == STEP_CONTINUE) { 258 if (watchpt || db_find_breakpoint_here(task, pc)) { 259 /* 260 * Step over breakpoint/watchpoint. 261 */ 262 db_run_mode = STEP_INVISIBLE; 263 db_set_task_single_step(DDB_REGS, task); 264 } else { 265 db_set_breakpoints(); 266 db_set_watchpoints(); 267 } 268 } else { 269 db_set_task_single_step(DDB_REGS, task); 270 } 271} 272 273/* 274 * 'n' and 'u' commands might never return. 275 * Limit the maximum number of steps. 276 */ 277 278boolean_t 279db_step_again(void) 280{ 281 if (db_inst_count && !(db_inst_count%db_max_inst_count)) { 282 char c; 283 db_printf("%d instructions, continue ? (y/n) ", 284 db_inst_count); 285 c = cngetc(); 286 db_printf("\n"); 287 if(c == 'n') 288 return(FALSE); 289 } 290 return(TRUE); 291} 292 293void 294db_single_step(db_regs_t *regs, __unused task_t task) 295{ 296 if (db_run_mode == STEP_CONTINUE) { 297 db_run_mode = STEP_INVISIBLE; 298 db_set_task_single_step(regs, task); 299 } 300} 301 302#ifdef SOFTWARE_SSTEP 303/* 304 * Software implementation of single-stepping. 305 * If your machine does not have a trace mode 306 * similar to the vax or sun ones you can use 307 * this implementation, done for the mips. 308 * Just define the above conditional and provide 309 * the functions/macros defined below. 310 * 311 * extern boolean_t 312 * inst_branch(), returns true if the instruction might branch 313 * extern unsigned 314 * branch_taken(), return the address the instruction might 315 * branch to 316 * db_getreg_val(); return the value of a user register, 317 * as indicated in the hardware instruction 318 * encoding, e.g. 8 for r8 319 * 320 * next_instr_address(pc,bd,task) returns the address of the first 321 * instruction following the one at "pc", 322 * which is either in the taken path of 323 * the branch (bd==1) or not. This is 324 * for machines (mips) with branch delays. 325 * 326 * A single-step may involve at most 2 breakpoints - 327 * one for branch-not-taken and one for branch taken. 328 * If one of these addresses does not already have a breakpoint, 329 * we allocate a breakpoint and save it here. 330 * These breakpoints are deleted on return. 331 */ 332db_breakpoint_t db_not_taken_bkpt = 0; 333db_breakpoint_t db_taken_bkpt = 0; 334 335db_breakpoint_t 336db_find_temp_breakpoint( 337 task_t task, 338 db_addr_t addr) 339{ 340 if (db_taken_bkpt && (db_taken_bkpt->address == addr) && 341 db_taken_bkpt->task == task) 342 return db_taken_bkpt; 343 if (db_not_taken_bkpt && (db_not_taken_bkpt->address == addr) && 344 db_not_taken_bkpt->task == task) 345 return db_not_taken_bkpt; 346 return 0; 347} 348 349void 350db_set_task_single_step( 351 register db_regs_t *regs, 352 task_t task) 353{ 354 db_addr_t pc = PC_REGS(regs), brpc; 355 register unsigned int inst; 356 register boolean_t unconditional; 357 358 /* 359 * User was stopped at pc, e.g. the instruction 360 * at pc was not executed. 361 */ 362 inst = db_get_task_value(pc, sizeof(int), FALSE, task); 363 if (inst_branch(inst) || inst_call(inst)) { 364 extern db_expr_t getreg_val(); /* XXX -- need prototype! */ 365 366 brpc = branch_taken(inst, pc, getreg_val, (unsigned char*)regs); 367 if (brpc != pc) { /* self-branches are hopeless */ 368 db_taken_bkpt = db_set_temp_breakpoint(task, brpc); 369 } else 370 db_taken_bkpt = 0; 371 pc = next_instr_address(pc,1,task); 372 } else 373 pc = next_instr_address(pc,0,task); 374 375 /* 376 * check if this control flow instruction is an 377 * unconditional transfer 378 */ 379 380 unconditional = inst_unconditional_flow_transfer(inst); 381 382 /* 383 We only set the sequential breakpoint if previous instruction was not 384 an unconditional change of flow of control. If the previous instruction 385 is an unconditional change of flow of control, setting a breakpoint in the 386 next sequential location may set a breakpoint in data or in another routine, 387 which could screw up either the program or the debugger. 388 (Consider, for instance, that the next sequential instruction is the 389 start of a routine needed by the debugger.) 390 */ 391 if (!unconditional && db_find_breakpoint_here(task, pc) == 0 && 392 (db_taken_bkpt == 0 || db_taken_bkpt->address != pc)) { 393 db_not_taken_bkpt = db_set_temp_breakpoint(task, pc); 394 } else 395 db_not_taken_bkpt = 0; 396} 397 398void 399db_clear_task_single_step( 400 db_regs_t *regs, 401 task_t task) 402{ 403 if (db_taken_bkpt != 0) { 404 db_delete_temp_breakpoint(task, db_taken_bkpt); 405 db_taken_bkpt = 0; 406 } 407 if (db_not_taken_bkpt != 0) { 408 db_delete_temp_breakpoint(task, db_not_taken_bkpt); 409 db_not_taken_bkpt = 0; 410 } 411} 412 413#endif /* SOFTWARE_SSTEP */ 414 415extern int db_cmd_loop_done; 416 417/* single-step */ 418void 419db_single_step_cmd(__unused db_expr_t addr, __unused boolean_t have_addr, 420 db_expr_t count, char *modif) 421{ 422 boolean_t print = FALSE; 423 424 if (count == (db_expr_t)-1) 425 count = 1; 426 427 if (modif[0] == 'p') 428 print = TRUE; 429 430 db_run_mode = STEP_ONCE; 431 db_loop_count = count; 432 db_sstep_print = print; 433 db_inst_count = 0; 434 db_last_inst_count = 0; 435 db_load_count = 0; 436 db_store_count = 0; 437 438 db_cmd_loop_done = 1; 439} 440 441/* trace and print until call/return */ 442void 443db_trace_until_call_cmd(__unused db_expr_t addr, __unused boolean_t have_addr, 444 __unused db_expr_t count, char *modif) 445{ 446 boolean_t print = FALSE; 447 448 if (modif[0] == 'p') 449 print = TRUE; 450 451 db_run_mode = STEP_CALLT; 452 db_sstep_print = print; 453 db_inst_count = 0; 454 db_last_inst_count = 0; 455 db_load_count = 0; 456 db_store_count = 0; 457 458 db_cmd_loop_done = 1; 459} 460 461void 462db_trace_until_matching_cmd(__unused db_expr_t addr, 463 __unused boolean_t have_addr, 464 __unused db_expr_t count, 465 char *modif) 466{ 467 boolean_t print = FALSE; 468 469 if (modif[0] == 'p') 470 print = TRUE; 471 472 db_run_mode = STEP_RETURN; 473 db_call_depth = 1; 474 db_sstep_print = print; 475 db_inst_count = 0; 476 db_last_inst_count = 0; 477 db_load_count = 0; 478 db_store_count = 0; 479 480 db_cmd_loop_done = 1; 481} 482 483/* continue */ 484void 485db_continue_cmd(__unused db_expr_t addr, __unused boolean_t have_addr, 486 __unused db_expr_t count, __unused char *modif) 487{ 488 /* 489 * Though "cont/c" works fairly well, it's not really robust 490 * enough to use in arbitrary situations, so disable it. 491 * (Doesn't seem cost-effective to debug and fix what ails 492 * it.) 493 */ 494#if 0 495 if (modif[0] == 'c') 496 db_run_mode = STEP_COUNT; 497 else 498 db_run_mode = STEP_CONTINUE; 499#else 500 db_run_mode = STEP_CONTINUE; 501#endif 502 db_inst_count = 0; 503 db_last_inst_count = 0; 504 db_load_count = 0; 505 db_store_count = 0; 506 507 db_cmd_loop_done = 1; 508} 509 510 511/* 512 * Switch to gdb 513 */ 514static void 515db_to_gdb(void) 516{ 517 switch_debugger = 1; 518} 519 520/* gdb */ 521void 522db_continue_gdb(__unused db_expr_t addr, __unused boolean_t have_addr, 523 __unused db_expr_t count, __unused char *modif) 524{ 525 db_to_gdb(); 526 db_run_mode = STEP_CONTINUE; 527 db_inst_count = 0; 528 db_last_inst_count = 0; 529 db_load_count = 0; 530 db_store_count = 0; 531 532 db_cmd_loop_done = 1; 533} 534 535 536boolean_t 537db_in_single_step(void) 538{ 539 return(db_run_mode != STEP_NONE && db_run_mode != STEP_CONTINUE); 540} 541 542