| 1 | /* $Header$ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved. |
| 5 | * |
| 6 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 7 | * on using and copying this software. |
| 8 | */ |
| 9 | /* |
| 10 | Name |
| 11 | vmdbg.h - T3 VM debugging API |
| 12 | Function |
| 13 | Provides an interface to debugging operations in the VM |
| 14 | Notes |
| 15 | |
| 16 | Modified |
| 17 | 11/23/99 MJRoberts - Creation |
| 18 | */ |
| 19 | |
| 20 | #ifndef VMDBG_H |
| 21 | #define VMDBG_H |
| 22 | |
| 23 | #include "vmglob.h" |
| 24 | #include "vmfunc.h" |
| 25 | #include "vmhash.h" |
| 26 | #include "vmobj.h" |
| 27 | #include "tcprstyp.h" |
| 28 | #include "osifcnet.h" |
| 29 | |
| 30 | |
| 31 | /* ------------------------------------------------------------------------ */ |
| 32 | /* |
| 33 | * Internal breakpoint tracking record |
| 34 | */ |
| 35 | struct CVmDebugBp |
| 36 | { |
| 37 | /* create */ |
| 38 | CVmDebugBp(); |
| 39 | |
| 40 | /* delete */ |
| 41 | ~CVmDebugBp(); |
| 42 | |
| 43 | /* notify of VM termination */ |
| 44 | void do_terminate(VMG0_); |
| 45 | |
| 46 | /* get/set the in-use flag */ |
| 47 | int is_in_use() const { return in_use_; } |
| 48 | void set_in_use(int f) { in_use_ = f; } |
| 49 | |
| 50 | /* get/set the disabled flag */ |
| 51 | int is_disabled() const { return disabled_; } |
| 52 | void set_disabled(int f) { disabled_ = f; } |
| 53 | |
| 54 | /* |
| 55 | * set the breakpoint's information - returns zero on success, |
| 56 | * non-zero if an error occurs (such as compiling the condition |
| 57 | * expression) |
| 58 | */ |
| 59 | int set_info(VMG_ const uchar *code_addr, |
| 60 | const char *cond, int change, |
| 61 | int disabled, char *errbuf, size_t errbuflen); |
| 62 | |
| 63 | /* get my code address */ |
| 64 | const uchar *get_code_addr() const { return code_addr_; } |
| 65 | |
| 66 | /* |
| 67 | * Set the condition text - returns zero on success, non-zero if an |
| 68 | * error occurs compiling the expression. If 'change' is true, then we |
| 69 | * break when the condition changes; otherwise, we break when the |
| 70 | * condition becomes true. |
| 71 | */ |
| 72 | int set_condition(VMG_ const char *cond, int change, |
| 73 | char *errbuf, size_t errbuflen); |
| 74 | |
| 75 | /* |
| 76 | * delete the breakpoint - remove the breakpoint from the code and |
| 77 | * mark it as unused |
| 78 | */ |
| 79 | void do_delete(VMG0_); |
| 80 | |
| 81 | /* |
| 82 | * Set or remove the BP instruction at my code address. If 'always' |
| 83 | * is set, we'll update the instruction regardless of whether the |
| 84 | * debugger has control or not; normally, we'll only update the |
| 85 | * instruction when the debugger doesn't have control, since we |
| 86 | * don't leave breakpoint instructions in the code at other times. |
| 87 | */ |
| 88 | void set_bp_instr(VMG_ int set, int always); |
| 89 | |
| 90 | /* check to see if I have a condition */ |
| 91 | int has_condition() const |
| 92 | { return has_cond_ && compiled_cond_ != 0; } |
| 93 | |
| 94 | /* |
| 95 | * Is our condition a stop-on-change condition? This returns true if |
| 96 | * so, false if this we instead have a stop-when-true condition. Note |
| 97 | * that this information is not meaningful unless has_condition() |
| 98 | * returns true. |
| 99 | */ |
| 100 | int stop_on_change() const { return stop_on_change_; } |
| 101 | |
| 102 | /* |
| 103 | * evaluate the condition - return true if the condition evaluates |
| 104 | * to true (non-zero integer, true, or any object, string, or list |
| 105 | * value), false if not |
| 106 | */ |
| 107 | int eval_cond(VMG0_); |
| 108 | |
| 109 | /* |
| 110 | * determine if this is a global breakpoint - a global breakpoint is |
| 111 | * one that is not associated with a code location (indicated by a |
| 112 | * code address of zero) |
| 113 | */ |
| 114 | int is_global() const { return code_addr_ == 0; } |
| 115 | |
| 116 | private: |
| 117 | /* code address of breakpoint */ |
| 118 | const uchar *code_addr_; |
| 119 | |
| 120 | /* condition expression */ |
| 121 | char *cond_; |
| 122 | |
| 123 | /* length of buffer allocated for cond, if any */ |
| 124 | size_t cond_buf_len_; |
| 125 | |
| 126 | /* code object with compiled expression */ |
| 127 | vm_globalvar_t *compiled_cond_; |
| 128 | |
| 129 | /* |
| 130 | * The previous value of the condition. If this is a stop-on-change |
| 131 | * condition, we'll stop as soon as the current value of the expression |
| 132 | * differs from this saved value. If we have a condition, but we |
| 133 | * haven't yet computed the "old" value, this will have the 'empty' |
| 134 | * type. |
| 135 | */ |
| 136 | vm_globalvar_t *prv_val_; |
| 137 | |
| 138 | /* |
| 139 | * original instruction byte at this breakpoint (we replace the |
| 140 | * instruction byte with the BP instruction, but we must remember |
| 141 | * the original for when we clear or disable the breakpoint) |
| 142 | */ |
| 143 | uchar orig_instr_; |
| 144 | |
| 145 | /* flag: breakpoint is in use */ |
| 146 | uint in_use_ : 1; |
| 147 | |
| 148 | /* flag: breakpoint has a conditional expression */ |
| 149 | uint has_cond_ : 1; |
| 150 | |
| 151 | /* flag: our condition is stop-on-change, not stop-when-true */ |
| 152 | uint stop_on_change_ : 1; |
| 153 | |
| 154 | /* flag: breakpoint is disabled */ |
| 155 | uint disabled_ : 1; |
| 156 | |
| 157 | }; |
| 158 | |
| 159 | /* maximum number of breakpoints */ |
| 160 | const size_t VMDBG_BP_MAX = 100; |
| 161 | |
| 162 | /* ------------------------------------------------------------------------ */ |
| 163 | /* |
| 164 | * Structure for saving debugger step modes. This is used to save and |
| 165 | * restore the step modes before doing a recursive evaluation. |
| 166 | */ |
| 167 | struct vmdbg_step_save_t |
| 168 | { |
| 169 | int old_step; |
| 170 | int old_step_in; |
| 171 | int old_step_out; |
| 172 | }; |
| 173 | |
| 174 | |
| 175 | /* ------------------------------------------------------------------------ */ |
| 176 | /* |
| 177 | * Debugger API object |
| 178 | */ |
| 179 | class CVmDebug |
| 180 | { |
| 181 | friend class CVmRun; |
| 182 | friend struct CVmDebugBp; |
| 183 | |
| 184 | public: |
| 185 | CVmDebug(VMG0_); |
| 186 | ~CVmDebug(); |
| 187 | |
| 188 | /* |
| 189 | * Initialize. The VM will call this after setting up all of the VM |
| 190 | * globals, but before loading the image file. |
| 191 | */ |
| 192 | void init(VMG_ const char *image_filename); |
| 193 | |
| 194 | /* |
| 195 | * Initialization phase 2 - this is called just after we finish |
| 196 | * loading the image file. The debugger can perform any final |
| 197 | * set-up that can't be handled until after the image is loaded. |
| 198 | */ |
| 199 | void init_after_load(VMG0_); |
| 200 | |
| 201 | /* |
| 202 | * Terminate. The VM will call this when shutting down, before |
| 203 | * deleting any of the globals. |
| 204 | */ |
| 205 | void terminate(VMG0_); |
| 206 | |
| 207 | /* |
| 208 | * Determine if the loaded program was compiled for debugging. |
| 209 | * We'll check to see if the image loader found a GSYM (global |
| 210 | * symbol table) block in the file. |
| 211 | */ |
| 212 | int image_has_debug_info(VMG0_) const; |
| 213 | |
| 214 | /* determine if the debugger has control */ |
| 215 | int is_in_debugger() const { return in_debugger_ != 0; } |
| 216 | |
| 217 | /* |
| 218 | * Get the break event object. Routines that wait for OS_Event list |
| 219 | * should include this in their event lists, so that they unblock |
| 220 | * immediately if the user requests a manual debugger break. |
| 221 | */ |
| 222 | class OS_Event *get_break_event() |
| 223 | { |
| 224 | break_event_->add_ref(); |
| 225 | return break_event_; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | /* -------------------------------------------------------------------- */ |
| 230 | /* |
| 231 | * Add an entry to a reverse-lookup hash table. The image file |
| 232 | * loader must call this function for each object, function, and |
| 233 | * property symbol it loads from the debug records in an image file. |
| 234 | */ |
| 235 | void add_rev_sym(const char *sym, size_t sym_len, |
| 236 | tc_symtype_t sym_type, ulong sym_val); |
| 237 | |
| 238 | /* |
| 239 | * Look up a symbol given the type and identifier. Returns a |
| 240 | * pointer to a null-terminated string giving the name of the |
| 241 | * symbol, or null if the given identifier doesn't have an |
| 242 | * associated symbol. |
| 243 | */ |
| 244 | const char *objid_to_sym(vm_obj_id_t objid) const |
| 245 | { return find_rev_sym(obj_rev_table_, (ulong)objid); } |
| 246 | |
| 247 | const char *propid_to_sym(vm_prop_id_t propid) const |
| 248 | { return find_rev_sym(prop_rev_table_, (ulong)propid); } |
| 249 | |
| 250 | const char *funcaddr_to_sym(pool_ofs_t func_addr) const |
| 251 | { return find_rev_sym(func_rev_table_, (ulong)func_addr); } |
| 252 | |
| 253 | const char *funchdr_to_sym(VMG_ const uchar *func_addr, char *buf) const; |
| 254 | |
| 255 | const char *enum_to_sym(ulong enum_id) const |
| 256 | { return find_rev_sym(enum_rev_table_, enum_id); } |
| 257 | |
| 258 | const char *bif_to_sym(uint setidx, uint funcidx) const |
| 259 | { |
| 260 | ulong idx = ((ulong)setidx << 16) | funcidx; |
| 261 | return find_rev_sym(bif_rev_table_, idx); |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Given a symbol name, get the final modifying object. If the symbol |
| 266 | * is a synthesized modified base object, we'll find the actual symbol |
| 267 | * that was used in the source code with 'modify.' |
| 268 | */ |
| 269 | const char *get_modifying_sym(const char *base_sym) const; |
| 270 | |
| 271 | |
| 272 | /* -------------------------------------------------------------------- */ |
| 273 | /* |
| 274 | * Method header list |
| 275 | */ |
| 276 | |
| 277 | /* allocate the method header list */ |
| 278 | void alloc_method_header_list(ulong cnt); |
| 279 | |
| 280 | /* |
| 281 | * Given a code pool address, find the method header containing the |
| 282 | * address. This searches the method header list for the nearest |
| 283 | * method header whose address is less than the given address. |
| 284 | */ |
| 285 | const uchar *find_method_header(VMG_ const uchar *addr); |
| 286 | |
| 287 | /* get the number of method headers */ |
| 288 | ulong get_method_header_cnt() const { return method_hdr_cnt_; } |
| 289 | |
| 290 | /* set the given method header list entry */ |
| 291 | void set_method_header(ulong idx, ulong addr) |
| 292 | { |
| 293 | /* store the given entry */ |
| 294 | method_hdr_[idx] = addr; |
| 295 | } |
| 296 | |
| 297 | /* get the given method header entry */ |
| 298 | ulong get_method_header(ulong idx) const { return method_hdr_[idx]; } |
| 299 | |
| 300 | |
| 301 | /* -------------------------------------------------------------------- */ |
| 302 | /* |
| 303 | * Get information on the source location at a given stack level. |
| 304 | * If successful, fills in the filename pointer and line number and |
| 305 | * returns zero. If no source information is available at the stack |
| 306 | * level, returns non-zero to indicate failure. |
| 307 | * |
| 308 | * Level 0 is the current stack level, 1 is the enclosing frame, and |
| 309 | * so on. |
| 310 | */ |
| 311 | int get_source_info(VMG_ const char **fname, unsigned long *linenum, |
| 312 | int level) const; |
| 313 | |
| 314 | |
| 315 | /* |
| 316 | * Enumerate local variables at the given stack level - 0 is the |
| 317 | * current stack level, 1 is the enclosing frame, and so on. |
| 318 | */ |
| 319 | void enum_locals(VMG_ void (*cbfunc)(void *, const char *, size_t), |
| 320 | void *cbctx, int level); |
| 321 | |
| 322 | /* |
| 323 | * Build a stack trace listing, invoking the callback for each level |
| 324 | * of the stack trace. If 'source_info' is true, we'll include the |
| 325 | * source file name and line number for each point in the trace. |
| 326 | */ |
| 327 | void build_stack_listing(VMG_ |
| 328 | void (*cbfunc)(void *ctx, |
| 329 | const char *str, int strl), |
| 330 | void *cbctx, int source_info); |
| 331 | |
| 332 | |
| 333 | /* -------------------------------------------------------------------- */ |
| 334 | /* |
| 335 | * Breakpoints |
| 336 | */ |
| 337 | |
| 338 | /* |
| 339 | * Toggle a breakpoint at the given code location. Returns zero on |
| 340 | * success, non-zero on failure. If successful, fills in *bpnum |
| 341 | * with the breakpoint ID, and fills in *did_set with true if we set |
| 342 | * a breakpoint, false if we deleted an existing breakpoint at the |
| 343 | * location. cond is null if the breakpoint is unconditional, or a |
| 344 | * pointer to a character string giving the source code for an |
| 345 | * expression that must evaluate to true when the breakpoint is |
| 346 | * encountered for the breakpoint to suspend execution. |
| 347 | */ |
| 348 | int toggle_breakpoint(VMG_ const uchar *code_addr, |
| 349 | const char *cond, int change, |
| 350 | int *bpnum, int *did_set, |
| 351 | char *errbuf, size_t errbuflen); |
| 352 | |
| 353 | /* toggle the disabled status of a breakpoint */ |
| 354 | void toggle_breakpoint_disable(VMG_ int bpnum); |
| 355 | |
| 356 | /* set a breakpoint's disabled status */ |
| 357 | void set_breakpoint_disable(VMG_ int bpnum, int disable); |
| 358 | |
| 359 | /* determine if a breakpoint is disabled - returns true if so */ |
| 360 | int is_breakpoint_disabled(VMG_ int bpnum); |
| 361 | |
| 362 | /* set a breakpoint's condition expression - returns 0 on success */ |
| 363 | int set_breakpoint_condition(VMG_ int bpnum, const char *cond, int change, |
| 364 | char *errbuf, size_t errbuflen); |
| 365 | |
| 366 | /* delete a breakpoint given the breakpoint ID */ |
| 367 | void delete_breakpoint(VMG_ int bpnum); |
| 368 | |
| 369 | /* -------------------------------------------------------------------- */ |
| 370 | /* |
| 371 | * Set execution mode. These calls do not immediately resume |
| 372 | * execution, but merely set the mode that will be in effect when |
| 373 | * execution resumes. These calls can only be made while the |
| 374 | * program is stopped. |
| 375 | */ |
| 376 | |
| 377 | /* |
| 378 | * Set single-step mode to STEP IN. In this mode, we will stop as |
| 379 | * soon as we reach a new statement. |
| 380 | */ |
| 381 | void set_step_in() |
| 382 | { |
| 383 | /* set single-step and step-in modes */ |
| 384 | single_step_ = TRUE; |
| 385 | step_in_ = TRUE; |
| 386 | step_out_ = FALSE; |
| 387 | |
| 388 | /* we're in run mode again */ |
| 389 | break_event_->reset(); |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * Set DEBUG TRACE mode. This activates single-step mode from within a |
| 394 | * VM intrinsic. |
| 395 | */ |
| 396 | void set_debug_trace() |
| 397 | { |
| 398 | /* |
| 399 | * set single-step mode so that we stop as soon as we're back in |
| 400 | * byte code |
| 401 | */ |
| 402 | set_step_in(); |
| 403 | |
| 404 | /* |
| 405 | * clear any current statement location, so that we stop |
| 406 | * immediately as soon as we get back to byte code, even if our |
| 407 | * last debugger entry was in the same line of code |
| 408 | */ |
| 409 | cur_stm_start_ = cur_stm_end_ = 0; |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * Set single-step mode for a 'break' key (Ctrl-C, Ctrl+Break, etc., |
| 414 | * according to local conventions). This asynchronously interrupts the |
| 415 | * interpreter and breaks into the debugger while the program is |
| 416 | * running, which is useful to break out of infinite loops or very |
| 417 | * lengthy processing. |
| 418 | */ |
| 419 | void set_break_stop() |
| 420 | { |
| 421 | /* set single-step-in mode */ |
| 422 | set_step_in(); |
| 423 | |
| 424 | /* break out of any event wait */ |
| 425 | break_event_->signal(); |
| 426 | |
| 427 | /* |
| 428 | * Forget our last execution location, so that we'll stop again |
| 429 | * even if we haven't moved from our last break location. Since |
| 430 | * we're explicitly breaking execution, we want to stop whether |
| 431 | * we've gone anywhere or not. |
| 432 | */ |
| 433 | cur_stm_start_ = cur_stm_end_ = 0; |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Set single-step mode to STEP OVER. In this mode, we will stop as |
| 438 | * soon as we reach a new statement at the same stack level as the |
| 439 | * current statement or at an enclosing stack level. |
| 440 | */ |
| 441 | void set_step_over(VMG0_); |
| 442 | |
| 443 | /* |
| 444 | * Set single-step mode to STEP OUT. In this mode, we will stop as |
| 445 | * soon as we reach a new statement at a stack level enclosing the |
| 446 | * current statement's stack level. |
| 447 | */ |
| 448 | void set_step_out(VMG0_); |
| 449 | |
| 450 | /* |
| 451 | * Set single-step mode to GO. In this mode, we won't stop until we |
| 452 | * reach a breakpoint. |
| 453 | */ |
| 454 | void set_go() |
| 455 | { |
| 456 | /* clear single-step mode */ |
| 457 | single_step_ = FALSE; |
| 458 | step_in_ = FALSE; |
| 459 | step_out_ = FALSE; |
| 460 | break_event_->reset(); |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * Save the step mode in preparation for a recursive execution (of a |
| 465 | * debugger expression), and set the mode for the recursive |
| 466 | * execution. Turns off stepping modes. |
| 467 | */ |
| 468 | void prepare_for_eval(vmdbg_step_save_t *info) |
| 469 | { |
| 470 | /* save the original execution mode */ |
| 471 | info->old_step = single_step_; |
| 472 | info->old_step_in = step_in_; |
| 473 | info->old_step_out = step_out_; |
| 474 | |
| 475 | /* set execution mode to RUN */ |
| 476 | single_step_ = FALSE; |
| 477 | step_in_ = FALSE; |
| 478 | step_out_ = FALSE; |
| 479 | } |
| 480 | |
| 481 | /* restore original execution modes after recursive execution */ |
| 482 | void restore_from_eval(vmdbg_step_save_t *info) |
| 483 | { |
| 484 | single_step_ = info->old_step; |
| 485 | step_in_ = info->old_step_in; |
| 486 | step_out_ = info->old_step_out; |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * format a value, allocating space (the caller must free the space, |
| 491 | * using t3free) |
| 492 | */ |
| 493 | char *format_val(VMG_ const struct vm_val_t *val); |
| 494 | |
| 495 | /* format a value into a buffer */ |
| 496 | size_t format_val(VMG_ char *buf, size_t buflen, |
| 497 | const struct vm_val_t *val); |
| 498 | |
| 499 | /* format a value in the special __value# format */ |
| 500 | void format_special(VMG_ char *buf, size_t buflen, |
| 501 | const struct vm_val_t *val); |
| 502 | |
| 503 | |
| 504 | /* -------------------------------------------------------------------- */ |
| 505 | /* |
| 506 | * Set the execution pointer to a new location. The new code |
| 507 | * address is given as an absolute code pool address. The new |
| 508 | * location must be within the current method. Updates |
| 509 | * *exec_ofs_ptr with the method offset of the new location. |
| 510 | */ |
| 511 | int set_exec_ofs(const uchar **exec_ptr, const uchar *code_addr) |
| 512 | { |
| 513 | /* set the method offset pointer */ |
| 514 | *exec_ptr = code_addr; |
| 515 | |
| 516 | /* success */ |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | /* |
| 521 | * Determine if a code location is within the current active method. |
| 522 | * Returns true if so, false if not. |
| 523 | */ |
| 524 | int is_in_current_method(VMG_ const uchar *code_addr); |
| 525 | |
| 526 | /* -------------------------------------------------------------------- */ |
| 527 | /* |
| 528 | * Evaluate an expression. Returns zero on success, non-zero on |
| 529 | * failure. 'level' is the stack level at which to evaluate the |
| 530 | * expression - 0 is the currently active method, 1 is its caller, |
| 531 | * and so on. |
| 532 | */ |
| 533 | int eval_expr(VMG_ char *result, size_t result_len, const char *expr, |
| 534 | int level, int *is_lval, int *is_openable, |
| 535 | void (*aggcb)(void *, const char *, int, const char *), |
| 536 | void *aggctx, int speculative); |
| 537 | |
| 538 | /* |
| 539 | * Compile an expression. Fills in *code_obj with a handle to the |
| 540 | * code pool object containing the compiled byte-code. If dst_buf |
| 541 | * is non-null, we will format a message describing any error that |
| 542 | * occurs into the buffer. |
| 543 | * |
| 544 | * We'll compile the expression using the given local symbol table |
| 545 | * and the given active stack level. Note that the local symbol |
| 546 | * table specified need not be the local symbol table for the given |
| 547 | * active stack level, since we're only compiling, not evaluating, |
| 548 | * the expression; for example, when compiling a condition |
| 549 | * expression for a breakpoint, we'll normally compile the |
| 550 | * expression in the context of the breakpoint's source location, |
| 551 | * which might not even be in the active stack when the condition is |
| 552 | * compiled. |
| 553 | */ |
| 554 | void compile_expr(VMG_ const char *expr, |
| 555 | int level, class CVmDbgSymtab *local_symtab, |
| 556 | int self_valid, int speculative, int *is_lval, |
| 557 | vm_obj_id_t *code_obj, |
| 558 | struct CVmDynCompResults *results); |
| 559 | |
| 560 | /* -------------------------------------------------------------------- */ |
| 561 | /* |
| 562 | * Get/set the UI context. This information is for use by the UI |
| 563 | * code. We don't use the UI context for anything; we just maintain |
| 564 | * it so that the UI can keep track of what's going on between |
| 565 | * calls. |
| 566 | */ |
| 567 | void *get_ui_ctx() const { return ui_ctx_; } |
| 568 | void set_ui_ctx(void *ctx) { ui_ctx_ = ctx; } |
| 569 | |
| 570 | protected: |
| 571 | /* -------------------------------------------------------------------- */ |
| 572 | /* |
| 573 | * CVmRun API - the byte-code execution loop uses these routines |
| 574 | */ |
| 575 | |
| 576 | /* |
| 577 | * Step into the debugger - the byte-code execution loop calls this |
| 578 | * just before executing each instruction when the interpreter is in |
| 579 | * single-step mode, or when a breakpoint is encountered. We'll |
| 580 | * activate the debugger user interface if necessary. This returns |
| 581 | * in order to resume execution. bp is true if we encountered a |
| 582 | * breakpoint, false if we're single-stepping. |
| 583 | */ |
| 584 | void step(VMG_ const uchar **pc_ptr, const uchar *entry, int bp, |
| 585 | int error_code); |
| 586 | |
| 587 | /* |
| 588 | * Step through a return. This doesn't actually stop in the debugger, |
| 589 | * but for the purposes of STEP OVER, notes that we're leaving a frame |
| 590 | * level. This is unnecessary in most cases, since we'd do a step() at |
| 591 | * the next instruction in the caller anyway. However, if the caller |
| 592 | * is native code that will turn around and call another byte-code |
| 593 | * routine, this ensures that we notice that we've left one byte-code |
| 594 | * routine and entered another. If we didn't do this, and the next |
| 595 | * routine called had more arguments, we'd miss single-stepping into |
| 596 | * the next routine because we'd think we were at an enclosing level |
| 597 | * rather than a peer level. |
| 598 | */ |
| 599 | void step_return(VMG0_); |
| 600 | |
| 601 | /* |
| 602 | * synchronize the internal execution point - acts like a step() |
| 603 | * without actually stopping |
| 604 | */ |
| 605 | void sync_exec_pos(VMG_ const uchar *pc_ptr, const uchar *entry); |
| 606 | |
| 607 | /* |
| 608 | * Get single-step mode - returns true if we're stopping at each |
| 609 | * instruction, false if we're running until we hit a breakpoint. |
| 610 | * The byte-code execution loop should call step() on each |
| 611 | * instruction if this returns true. |
| 612 | * |
| 613 | * We must also single-step through code if there are any active |
| 614 | * global breakpoints, even when the user is not interactively |
| 615 | * single-stepping. |
| 616 | */ |
| 617 | int is_single_step() const |
| 618 | { return single_step_ || global_bp_cnt_ != 0; } |
| 619 | |
| 620 | |
| 621 | /* -------------------------------------------------------------------- */ |
| 622 | /* |
| 623 | * Internal operations |
| 624 | */ |
| 625 | |
| 626 | /* |
| 627 | * Get information on the execution location at the given stack |
| 628 | * level |
| 629 | */ |
| 630 | int get_stack_level_info(VMG_ int level, class CVmFuncPtr *func_ptr, |
| 631 | class CVmDbgLinePtr *line_ptr, |
| 632 | const uchar **stm_start, const uchar **stm_end) |
| 633 | const; |
| 634 | |
| 635 | /* look up a symbol in one of our reverse mapping tables */ |
| 636 | const char *find_rev_sym( |
| 637 | const class CVmHashTable *hashtab, ulong val) const; |
| 638 | |
| 639 | /* find a breakpoint given a code address */ |
| 640 | CVmDebugBp *find_bp(const uchar *code_addr); |
| 641 | |
| 642 | /* allocate a new breakpoint record */ |
| 643 | CVmDebugBp *alloc_bp(); |
| 644 | |
| 645 | /* |
| 646 | * suspend/resume breakpoints - this doesn't affect the permanent |
| 647 | * status of any breakpoint, but simply allows for removal of BP |
| 648 | * instructions from the code while the debugger has control |
| 649 | */ |
| 650 | void suspend_all_bps(VMG0_); |
| 651 | void restore_all_bps(VMG0_); |
| 652 | |
| 653 | private: |
| 654 | /* flag: debugger has control */ |
| 655 | unsigned int in_debugger_ : 1; |
| 656 | |
| 657 | /* |
| 658 | * single-step mode - if this is true, we're stepping through code; |
| 659 | * otherwise, we're running until we hit a breakpoint |
| 660 | */ |
| 661 | unsigned int single_step_ : 1; |
| 662 | |
| 663 | /* |
| 664 | * step-in mode - if this is true, and single_step_ is true, we'll |
| 665 | * break as soon as we reach a new statement anywhere; otherwise, |
| 666 | * we'll stop only if we reach a new statement at the same stack |
| 667 | * level as the current statement (in other words, we're stepping |
| 668 | * over subroutine calls made by the current statement) |
| 669 | */ |
| 670 | unsigned int step_in_ : 1 ; |
| 671 | |
| 672 | /* |
| 673 | * Step-out mode - if this is true, and single_step_ is true, we'll |
| 674 | * break as soon as we reach a new statement outside the current |
| 675 | * call level. (This mode flag is actually only important for |
| 676 | * native code interaction, because we actually know to stop on a |
| 677 | * step-out using step-over with an enclosing stack level.) |
| 678 | */ |
| 679 | unsigned int step_out_ : 1; |
| 680 | |
| 681 | /* |
| 682 | * step-over-breakpoint mode - if this is true, we're stepping over |
| 683 | * a breakpoint |
| 684 | */ |
| 685 | unsigned int step_over_bp_ : 1; |
| 686 | |
| 687 | /* |
| 688 | * Original step flags - these store the step flags that will be in |
| 689 | * effect after we finish a step_over_bp operation |
| 690 | */ |
| 691 | unsigned int orig_single_step_ : 1; |
| 692 | unsigned int orig_step_in_ : 1; |
| 693 | unsigned int orig_step_out_ : 1; |
| 694 | |
| 695 | /* |
| 696 | * flag: we've been initialized during program load; when this flag is |
| 697 | * set, we'll have to make corresponding uninitializations when the |
| 698 | * program terminates |
| 699 | */ |
| 700 | unsigned int program_inited_ : 1; |
| 701 | |
| 702 | /* breakpoint being stepped over */ |
| 703 | CVmDebugBp *step_over_bp_bp_; |
| 704 | |
| 705 | /* |
| 706 | * Step-resume stack frame level. When step_in_ is false, we won't |
| 707 | * resume stepping code until the frame pointer is at this level or |
| 708 | * an enclosing level. |
| 709 | */ |
| 710 | size_t step_frame_depth_; |
| 711 | |
| 712 | /* |
| 713 | * Method header for the current function. We set this each time we |
| 714 | * enter the debugger via step(). |
| 715 | */ |
| 716 | CVmFuncPtr func_ptr_; |
| 717 | |
| 718 | /* |
| 719 | * Debug records pointer for the current function. We set this each |
| 720 | * time we enter the debugger via step(). Note that we must keep |
| 721 | * track of whether the debug pointer is valid, because some |
| 722 | * functions might not have debug tables at all. |
| 723 | */ |
| 724 | CVmDbgTablePtr dbg_ptr_; |
| 725 | unsigned int dbg_ptr_valid_ : 1; |
| 726 | |
| 727 | /* function header pointer for current function */ |
| 728 | const uchar *entry_; |
| 729 | |
| 730 | /* current program counter */ |
| 731 | const uchar *pc_; |
| 732 | |
| 733 | /* debugger line records for current statement */ |
| 734 | CVmDbgLinePtr cur_stm_line_; |
| 735 | |
| 736 | /* |
| 737 | * Current statement boundaries. We set this each time we enter the |
| 738 | * debugger via step(). We usually step through code until we reach |
| 739 | * the beginning of a new statement; we use these boundaries to |
| 740 | * determine when we leave the current statement. As long as the |
| 741 | * current byte-code offset is within these boundaries, (inclusive), |
| 742 | * we know we're within the same statement. |
| 743 | */ |
| 744 | const uchar *cur_stm_start_; |
| 745 | const uchar *cur_stm_end_; |
| 746 | |
| 747 | /* |
| 748 | * User interface context. We maintain this location for use by the |
| 749 | * UI code to store its context information. |
| 750 | */ |
| 751 | void *ui_ctx_; |
| 752 | |
| 753 | /* |
| 754 | * Reverse-mapping hash tables for various symbol types. These hash |
| 755 | * tables allow us to find the symbol for a given object ID, |
| 756 | * property ID, or function address. |
| 757 | */ |
| 758 | class CVmHashTable *obj_rev_table_; |
| 759 | class CVmHashTable *prop_rev_table_; |
| 760 | class CVmHashTable *func_rev_table_; |
| 761 | class CVmHashTable *enum_rev_table_; |
| 762 | class CVmHashTable *bif_rev_table_; |
| 763 | |
| 764 | /* breakpoints */ |
| 765 | CVmDebugBp bp_[VMDBG_BP_MAX]; |
| 766 | |
| 767 | /* |
| 768 | * Number of global breakpoints in effect (when this is non-zero, we |
| 769 | * must trace through code even in 'go' mode, so we can evaluate the |
| 770 | * global breakpoints repeatedly and thereby catch when the first |
| 771 | * one hits). This only counts enabled global breakpoints - if a |
| 772 | * global breakpoint is disabled it must be removed from this count, |
| 773 | * since we won't need to consider it when checking for hits. |
| 774 | */ |
| 775 | int global_bp_cnt_; |
| 776 | |
| 777 | /* host interface (for the compiler's use) */ |
| 778 | class CTcHostIfcDebug *hostifc_; |
| 779 | |
| 780 | /* |
| 781 | * method header list - this is a list of the method headers in the |
| 782 | * program, sorted by address |
| 783 | */ |
| 784 | ulong *method_hdr_; |
| 785 | ulong method_hdr_cnt_; |
| 786 | |
| 787 | /* |
| 788 | * Debugger break event. We'll signal this whenever the user tells us |
| 789 | * to break into the program. Event wait routines (such as |
| 790 | * getNetEvent()) can include this in their wait list so that they're |
| 791 | * interrupted immediately on a debug break. |
| 792 | */ |
| 793 | class OS_Event *break_event_; |
| 794 | }; |
| 795 | |
| 796 | /* ------------------------------------------------------------------------ */ |
| 797 | /* |
| 798 | * Debugger programmatic interface to the user interface. This is to be |
| 799 | * implemented by each UI subsystem. |
| 800 | */ |
| 801 | class CVmDebugUI |
| 802 | { |
| 803 | public: |
| 804 | /* |
| 805 | * Initialize. We'll call this once before entering any other |
| 806 | * functions, so that the UI code can set up its private |
| 807 | * information. The UI can create a private context and store a |
| 808 | * pointer via G_debugger->set_ui_ctx(). |
| 809 | */ |
| 810 | static void init(VMG_ const char *image_filename); |
| 811 | |
| 812 | /* |
| 813 | * Initialization, phase 2 - this is called just after we've |
| 814 | * finished loading the image file. |
| 815 | */ |
| 816 | static void init_after_load(VMG0_); |
| 817 | |
| 818 | /* |
| 819 | * Terminate. We'll call this before terminating, to allow the UI |
| 820 | * code to release any resources it allocated. |
| 821 | */ |
| 822 | static void terminate(VMG0_); |
| 823 | |
| 824 | /* |
| 825 | * Invoke the UI main command loop entrypoint. The engine calls |
| 826 | * this whenever we hit a breakpoint or reach a single-step point. |
| 827 | * This routine should not return until it is ready to let the |
| 828 | * program continue execution. |
| 829 | */ |
| 830 | static void cmd_loop(VMG_ int bp_number, int error_code, const uchar **pc); |
| 831 | }; |
| 832 | |
| 833 | |
| 834 | /* ------------------------------------------------------------------------ */ |
| 835 | /* |
| 836 | * Hash table entry for reverse-mapping hash tables. These tables allow |
| 837 | * the debugger to look up a symbol name given the symbol's value: an |
| 838 | * object ID, a property ID, or a function address. We map each of |
| 839 | * these types of values to a ulong value, which we use as the hash key. |
| 840 | */ |
| 841 | class CVmHashEntryDbgRev: public CVmHashEntry |
| 842 | { |
| 843 | public: |
| 844 | /* |
| 845 | * Construct the entry. sym_val is the symbol's value (an object |
| 846 | * ID, a property ID, or a function address) coerced to a ulong. |
| 847 | */ |
| 848 | CVmHashEntryDbgRev(ulong sym_val, const char *sym, size_t len); |
| 849 | ~CVmHashEntryDbgRev(); |
| 850 | |
| 851 | /* check for a match */ |
| 852 | virtual int matches(const char *str, size_t len) const; |
| 853 | |
| 854 | /* get the symbol name for this entry */ |
| 855 | const char *get_sym() const { return sym_; } |
| 856 | size_t get_sym_len() const { return sym_len_; } |
| 857 | |
| 858 | protected: |
| 859 | char *sym_; |
| 860 | size_t sym_len_; |
| 861 | }; |
| 862 | |
| 863 | /* |
| 864 | * Hash function for reverse mapping tables. This hash function doesn't |
| 865 | * make any assumptions about the range of character values, since we're |
| 866 | * using sequences of raw binary bytes for hash keys. |
| 867 | */ |
| 868 | class CVmHashFuncDbgRev: public CVmHashFunc |
| 869 | { |
| 870 | public: |
| 871 | unsigned int compute_hash(const char *str, size_t len) const; |
| 872 | }; |
| 873 | |
| 874 | |
| 875 | /* ------------------------------------------------------------------------ */ |
| 876 | /* |
| 877 | * Condition compilation macros. Some files can be compiled for |
| 878 | * stand-alone use or use within a debugger application; when compiled |
| 879 | * stand-alone, certain debugger-related operations are removed, which |
| 880 | * can improve performance over the debugger-enabled version. |
| 881 | */ |
| 882 | #ifdef VM_DEBUGGER |
| 883 | /* |
| 884 | * DEBUGGER-ENABLED VERSION |
| 885 | */ |
| 886 | |
| 887 | /* include debugger-only code */ |
| 888 | #define VM_IF_DEBUGGER(x) x |
| 889 | |
| 890 | /* do NOT include non-debugger-only code */ |
| 891 | #define VM_IF_NOT_DEBUGGER(x) |
| 892 | |
| 893 | #else /* VM_DEBUGGER */ |
| 894 | /* |
| 895 | * STAND-ALONE (NON-DEBUGGER) VERSION |
| 896 | */ |
| 897 | |
| 898 | /* do NOT include debugger-only code in a stand-alone version */ |
| 899 | #define VM_IF_DEBUGGER(x) |
| 900 | |
| 901 | /* include non-debugger-only code */ |
| 902 | #define VM_IF_NOT_DEBUGGER(x) x |
| 903 | |
| 904 | #endif /* VM_DEBUGGER */ |
| 905 | |
| 906 | #endif /* VMDBG_H */ |
| 907 |