Debian Codegraph @

terps/tads/tads3/vmdbg.cpp

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header$";
4 #endif
5
6 /*
7 * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 vmdbg.cpp - T3 VM debugger API
15 Function
16
17 Notes
18
19 Modified
20 11/23/99 MJRoberts - Creation
21 */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "t3std.h"
27 #include "vmuni.h"
28 #include "os.h"
29 #include "charmap.h"
30 #include "vmglob.h"
31 #include "vmtype.h"
32 #include "vmdbg.h"
33 #include "vmrun.h"
34 #include "vmsrcf.h"
35 #include "vmhash.h"
36 #include "vmpool.h"
37 #include "vmop.h"
38 #include "vmlst.h"
39 #include "tctarg.h"
40 #include "tcglob.h"
41 #include "tctok.h"
42 #include "tcprs.h"
43 #include "tcmain.h"
44 #include "resload.h"
45 #include "tchostsi.h"
46 #include "tchost.h"
47 #include "tcgen.h"
48 #include "vmimage.h"
49 #include "vmhost.h"
50 #include "vmvec.h"
51 #include "vmbignum.h"
52 #include "vmdate.h"
53 #include "vmtzobj.h"
54 #include "vmtz.h"
55 #include "vmfilnam.h"
56 #include "vmstr.h"
57 #include "vmstrbuf.h"
58 #include "vmpat.h"
59 #include "vmanonfn.h"
60 #include "vmlookup.h"
61 #include "vmdynfunc.h"
62 #include "vmmeta.h"
63 #include "osifcnet.h"
64
65
66 /* ------------------------------------------------------------------------ */
67 /*
68 * Special override host interface for compiling expressions in the
69 * debugger. This host interface allows us to capture error messages
70 * generated during compilation for display in the debugger user
71 * interface.
72 */
73 class CTcHostIfcDebug: public CTcHostIfc
74 {
75 public:
76 CTcHostIfcDebug()
77 {
78 /* initially clear the message buffer */
79 reset_messages();
80 }
81
82 /* clear the message buffer */
83 void reset_messages()
84 {
85 errmsg_[0] = '\0';
86 errmsg_free_ = errmsg_;
87 have_errmsg_ = FALSE;
88 }
89
90 /* get the text of the first error message since we cleared the buffer */
91 const char *get_error_msg() const { return errmsg_; }
92
93 /*
94 * CTcHostIfc interface
95 */
96
97 /* display an informational message */
98 virtual void v_print_msg(const char *, va_list)
99 {
100 /* ignore informational messages */
101 }
102
103 /* display a process step message */
104 virtual void v_print_step(const char *, va_list)
105 {
106 /* ignore process step messages */
107 }
108
109 /* display an error message */
110 virtual void v_print_err(const char *msg, va_list args)
111 {
112 /* if we haven't already captured an error message, capture it */
113 if (!have_errmsg_)
114 {
115 char buf[1024];
116
117 /* format the message */
118 t3vsprintf(buf, sizeof(buf), msg, args);
119
120 /* append as much of it as possible to our buffer */
121 lib_strcpy(errmsg_free_,
122 sizeof(errmsg_) - (errmsg_free_ - errmsg_) - 1,
123 buf);
124
125 /* advance the free pointer past what we just added */
126 errmsg_free_ += strlen(errmsg_free_);
127
128 /*
129 * if there's a newline in the appended message, we're done
130 * gathering this message line
131 */
132 if (strchr(buf, '\n') != 0)
133 {
134 char *p;
135
136 /* note that we have the complete first error line now */
137 have_errmsg_ = TRUE;
138
139 /* terminate the message at the newline */
140 p = strchr(errmsg_, '\n');
141 if (p != 0)
142 *p = '\0';
143 }
144 }
145 }
146
147 private:
148 /* our error message buffer */
149 char errmsg_[256];
150
151 /* pointer to next available byte of message buffer */
152 char *errmsg_free_;
153
154 /* flag: we've captured an error message */
155 uint have_errmsg_ : 1;
156 };
157
158
159 /* ------------------------------------------------------------------------ */
160 /*
161 * Compiler parser symbol table interface. This provides an
162 * implementation of the compiler parser's debugger symbol table
163 * interface, which allows the compiler to look up symbols in the image
164 * file's debug records.
165 */
166 class CVmDbgSymtab: public CTcPrsDbgSymtab
167 {
168 public:
169 CVmDbgSymtab()
170 {
171 globals_ = 0;
172 frame_id_ = 0;
173 }
174
175 /* initialize in a given scope */
176 CVmDbgSymtab(VMG_ const CVmDbgTablePtr *debug_table, uint frame_id,
177 uint stack_level)
178 {
179 /* initialize with the given parameters */
180 init(vmg_ debug_table, frame_id, stack_level);
181 }
182
183 /* find a symbol - implementation of CTcPrsDbgSymtab interface */
184 virtual int find_symbol(const textchar_t *sym, size_t len,
185 tcprsdbg_sym_info *info);
186
187 void init(VMG_ const CVmDbgTablePtr *debug_table, uint frame_id,
188 uint stack_level)
189 {
190 /* save the globals if necessary */
191 globals_ = VMGLOB_ADDR;
192
193 /* save my debug table pointer */
194 debug_ptr_.copy_from(debug_table);
195
196 /* save my frame ID */
197 frame_id_ = frame_id;
198
199 /* save my stack level */
200 stack_level_ = stack_level;
201 }
202
203 private:
204 /* VM globals */
205 vm_globals *globals_;
206
207 /* my debug records table */
208 CVmDbgTablePtr debug_ptr_;
209
210 /* active frame ID for this scope */
211 uint frame_id_;
212
213 /* stack frame level */
214 uint stack_level_;
215 };
216
217 /*
218 * Find a symbol
219 */
220 int CVmDbgSymtab::find_symbol(const textchar_t *sym, size_t len,
221 tcprsdbg_sym_info *info)
222 {
223 CVmDbgFramePtr frame;
224 VMGLOB_PTR(globals_);
225
226 /* if I have no local frame, there's nothing to do */
227 if (frame_id_ == 0)
228 return FALSE;
229
230 /*
231 * search the enclosing frames, starting with my innermost frame and
232 * working outwards
233 */
234 debug_ptr_.set_frame_ptr(vmg_ &frame, frame_id_);
235 for (;;)
236 {
237 CVmDbgFrameSymPtr sym_ptr;
238 uint i;
239
240 /* iterate over the symbols in this frame */
241 for (i = frame.get_sym_count(),
242 frame.set_first_sym_ptr(vmg_ &sym_ptr) ;
243 i != 0 ; --i, sym_ptr.inc(vmg0_))
244 {
245 /* if this one matches, we found it */
246 if (sym_ptr.get_sym_len(vmg0_) == len
247 && memcmp(sym_ptr.get_sym(vmg0_), sym, len) == 0)
248 {
249 /* fill in the type information for the caller */
250 if (sym_ptr.is_ctx_local())
251 {
252 /* it's a context local */
253 info->sym_type = TC_SYM_LOCAL;
254 info->ctx_arr_idx = sym_ptr.get_ctx_arr_idx();
255
256 /* fill in the local variable ID and stack level */
257 info->var_id = sym_ptr.get_var_num();
258 info->frame_idx = stack_level_;
259 }
260 else if (sym_ptr.is_local())
261 {
262 /* it's a local */
263 info->sym_type = TC_SYM_LOCAL;
264 info->ctx_arr_idx = 0;
265
266 /* fill in the local variable ID and stack level */
267 info->var_id = sym_ptr.get_var_num();
268 info->frame_idx = stack_level_;
269 }
270 else if (sym_ptr.is_param())
271 {
272 /* it's a parameter variable */
273 info->sym_type = TC_SYM_PARAM;
274 info->ctx_arr_idx = 0;
275
276 /* fill in the parameter ID and stack level */
277 info->var_id = sym_ptr.get_var_num();
278 info->frame_idx = stack_level_;
279 }
280 else
281 {
282 /* unknown symbol type */
283 info->sym_type = TC_SYM_UNKNOWN;
284 }
285
286 /* tell the caller we found it */
287 return TRUE;
288 }
289 }
290
291 /* if there's no enclosing frame, we're done */
292 if (frame.get_enclosing_frame() == 0)
293 break;
294
295 /* move on to the enclosing frame */
296 debug_ptr_.set_frame_ptr(vmg_ &frame, frame.get_enclosing_frame());
297 }
298
299 /* failure */
300 return FALSE;
301 }
302
303
304 /* ------------------------------------------------------------------------ */
305 /*
306 * Creation
307 */
308 CVmDebug::CVmDebug(VMG0_)
309 {
310 /* create our debugger break event object */
311 break_event_ = new OS_Event(TRUE);
312
313 /* we have not yet been initialized during program load */
314 program_inited_ = FALSE;
315
316 /* we have no UI context yet */
317 ui_ctx_ = 0;
318
319 /* not currently in the debugger */
320 in_debugger_ = FALSE;
321
322 /* there's no valid debug pointer yet */
323 dbg_ptr_valid_ = FALSE;
324
325 /*
326 * start in step-in mode, so that we break as soon as we start
327 * executing the program
328 */
329 set_step_in();
330
331 /* note in step-over-breakpoint mode yet */
332 step_over_bp_ = FALSE;
333 step_frame_depth_ = 0;
334
335 /* no global breakpoints yet */
336 global_bp_cnt_ = 0;
337
338 /*
339 * we have no valid source line bounds yet, so set both ends to zero
340 * - zero is never a valid PC address (even if a function is at
341 * address zero, its method header will precede any executable code
342 * in the function, hence the program counter could never be at
343 * zero)
344 */
345 cur_stm_start_ = cur_stm_end_ = 0;
346 entry_ = 0;
347
348 /* create our reverse-lookup hash tables */
349 obj_rev_table_ = new CVmHashTable(256, new CVmHashFuncDbgRev, TRUE);
350 prop_rev_table_ = new CVmHashTable(256, new CVmHashFuncDbgRev, TRUE);
351 func_rev_table_ = new CVmHashTable(256, new CVmHashFuncDbgRev, TRUE);
352 enum_rev_table_ = new CVmHashTable(256, new CVmHashFuncDbgRev, TRUE);
353 bif_rev_table_ = new CVmHashTable(256, new CVmHashFuncDbgRev, TRUE);
354
355 /* create a host interface for the compiler */
356 hostifc_ = new CTcHostIfcDebug();
357
358 /* no halt requested yet */
359 G_interpreter->set_halt_vm(FALSE);
360
361 /* we haven't allocated a method header list yet */
362 method_hdr_ = 0;
363 method_hdr_cnt_ = 0;
364 }
365
366 /*
367 * Deletion
368 */
369 CVmDebug::~CVmDebug()
370 {
371 /* delete the host interface object */
372 delete hostifc_;
373
374 /* delete our reverse-lookup hash tables */
375 delete obj_rev_table_;
376 delete prop_rev_table_;
377 delete func_rev_table_;
378 delete enum_rev_table_;
379 delete bif_rev_table_;
380
381 /* delete the method header list */
382 if (method_hdr_ != 0)
383 t3free(method_hdr_);
384
385 /* release the debugger break event */
386 break_event_->release_ref();
387 }
388
389 /* ------------------------------------------------------------------------ */
390 /*
391 * VM initialization
392 */
393 void CVmDebug::init(VMG_ const char *image_fname)
394 {
395 /* note that the program has been initialized */
396 program_inited_ = TRUE;
397
398 /* tell the UI to initialize */
399 CVmDebugUI::init(vmg_ image_fname);
400
401 /* initialize the dynamic compiler */
402 CVmDynamicCompiler::get(vmg0_);
403 }
404
405 /*
406 * Initialization phase 2 - after loading the image file
407 */
408 void CVmDebug::init_after_load(VMG0_)
409 {
410 /* tell the UI to initialize */
411 CVmDebugUI::init_after_load(vmg0_);
412 }
413
414 /*
415 * VM termination
416 */
417 void CVmDebug::terminate(VMG0_)
418 {
419 CVmDebugBp *bp;
420 size_t i;
421
422 /* if we were never initialized, there's nothing to terminate */
423 if (!program_inited_)
424 return;
425
426 /* tell the UI to terminate */
427 CVmDebugUI::terminate(vmg0_);
428
429 /* tell the breakpoint objects we're about to terminate */
430 for (i = 0, bp = bp_ ; i < VMDBG_BP_MAX ; ++i, ++bp)
431 bp->do_terminate(vmg0_);
432 }
433
434
435 /* ------------------------------------------------------------------------ */
436 /*
437 * Determine if the image file has debugger information
438 */
439 int CVmDebug::image_has_debug_info(VMG0_) const
440 {
441 /* ask the image loader if there's a GSYM block */
442 return G_image_loader->has_gsym();
443 }
444
445
446 /* ------------------------------------------------------------------------ */
447 /*
448 * Add an entry to the reverse-lookup hash tables
449 */
450 void CVmDebug::add_rev_sym(const char *sym, size_t sym_len,
451 tc_symtype_t sym_type, ulong sym_val)
452 {
453 CVmHashTable *table;
454 CVmHashEntryDbgRev *entry;
455
456 /* figure out which table to use, based on the symbol type */
457 switch(sym_type)
458 {
459 case TC_SYM_FUNC:
460 /* function */
461 table = func_rev_table_;
462 break;
463
464 case TC_SYM_OBJ:
465 /* object */
466 table = obj_rev_table_;
467 break;
468
469 case TC_SYM_PROP:
470 /* property */
471 table = prop_rev_table_;
472 break;
473
474 case TC_SYM_ENUM:
475 /* enumerator */
476 table = enum_rev_table_;
477 break;
478
479 case TC_SYM_BIF:
480 /* built-in function */
481 table = bif_rev_table_;
482 break;
483
484 default:
485 /*
486 * ignore other symbol types - we don't maintain reverse-lookup
487 * tables for anything else
488 */
489 return;
490 }
491
492 /* create the symbol */
493 entry = new CVmHashEntryDbgRev(sym_val, sym, sym_len);
494
495 /* add it to the table */
496 table->add(entry);
497 }
498
499 /*
500 * Look up a symbol in one of our reverse-mapping tables
501 */
502 const char *CVmDebug::find_rev_sym(const CVmHashTable *tab,
503 ulong val) const
504 {
505 /* look up the symbol */
506 CVmHashEntryDbgRev *entry =
507 (CVmHashEntryDbgRev *)tab->find((char *)&val, sizeof(val));
508
509 /* if we found it, return the name pointer, otherwise null */
510 return (entry != 0 ? entry->get_sym() : 0);
511 }
512
513 /*
514 * Find the actual symbol for a 'modify' chain.
515 */
516 const char *CVmDebug::get_modifying_sym(const char *sym_name) const
517 {
518 /* if there's no original symbol, there's obviously no modifier */
519 if (sym_name == 0)
520 return 0;
521
522 /* keep going until we find the base object */
523 for (;;)
524 {
525 CTcSymObj *sym;
526
527 /* look up the symbol in the global symbol table */
528 sym = (CTcSymObj *)G_prs->get_global_symtab()
529 ->find(sym_name, strlen(sym_name));
530
531 /*
532 * if we found it, and it has a modifier object, proceed up to the
533 * modifier object
534 */
535 if (sym != 0
536 && sym->get_type() == TC_SYM_OBJ
537 && sym->get_modifying_obj_id() != VM_INVALID_OBJ)
538 {
539 const char *new_sym_name;
540
541 /*
542 * Forget the current object, and use the modification object
543 * instead. Look up the symbol name for the modification
544 * object, and replace the previous symbol - which was just a
545 * fake symbol that the compiler synthesized anyway - with the
546 * modifying object. At the top of the chain of modifying
547 * objects is the actual symbol name used in the source code,
548 * which is what we're after.
549 */
550 new_sym_name = objid_to_sym(sym->get_modifying_obj_id());
551
552 /*
553 * if we can't find the modifying object name, stop at the
554 * previous symbol
555 */
556 if (new_sym_name == 0)
557 return sym_name;
558
559 /* we got a symbol - use it */
560 sym_name = new_sym_name;
561 }
562 else
563 {
564 /*
565 * There's either no symbol or no modifier, so we can stop
566 * looking. Simply return what we have.
567 */
568 return sym_name;
569 }
570 }
571 }
572
573
574 /* ------------------------------------------------------------------------ */
575 /*
576 * Given a function header address, get the associated symbol
577 */
578 const char *CVmDebug::funchdr_to_sym(
579 VMG_ const uchar *func_addr, char *buf) const
580 {
581 /* convert the function address to a value */
582 CVmFuncPtr fp((const char *)func_addr);
583 vm_val_t v;
584 if (fp.get_fnptr(vmg_ &v))
585 {
586 /* check the type */
587 const char *ret;
588 switch (v.typ)
589 {
590 case VM_OBJ:
591 /* code object - return the symbol for the object, if any */
592 if ((ret = objid_to_sym(v.val.obj)) == 0)
593 {
594 ret = buf;
595 sprintf(buf, "DynamicFunc#%lx", (unsigned long)v.val.obj);
596 }
597 return ret;
598
599 case VM_FUNCPTR:
600 case VM_CODEOFS:
601 /* function pointer or code offset - return the function */
602 if ((ret = funcaddr_to_sym(v.val.ofs)) == 0)
603 {
604 ret = buf;
605 sprintf(buf, "func#%lx", (unsigned long)v.val.ofs);
606 }
607 return ret;
608
609 default:
610 break;
611 }
612 }
613
614 /* there's no symbolic form of the address */
615 return 0;
616 }
617
618
619 /* ------------------------------------------------------------------------ */
620 /*
621 * Get information on the source location at a given stack level
622 */
623 int CVmDebug::get_source_info(VMG_ const char **fname,
624 unsigned long *linenum, int level) const
625 {
626 CVmFuncPtr func_ptr;
627 CVmDbgLinePtr line_ptr;
628 CVmSrcfEntry *srcf_entry;
629 const uchar *stm_start, *stm_end;
630
631 /* if there's no source file table, we can't get any information */
632 if (G_srcf_table == 0)
633 return 1;
634
635 /* get information on the execution location at the requested level */
636 if (get_stack_level_info(vmg_ level, &func_ptr, &line_ptr,
637 &stm_start, &stm_end))
638 return 1;
639
640 /* get the source file entry for this line's source file index */
641 srcf_entry = G_srcf_table->get_entry(line_ptr.get_source_id());
642
643 /* if we didn't find an entry, we can't return any information */
644 if (srcf_entry == 0)
645 return 1;
646
647 /* fill in the caller's filename pointer */
648 *fname = srcf_entry->get_name();
649
650 /* fill in the caller's line number record */
651 *linenum = line_ptr.get_source_line();
652
653 /* success */
654 return 0;
655 }
656
657 /* ------------------------------------------------------------------------ */
658 /*
659 * Enumerate local variables at the given stack level
660 */
661 void CVmDebug::enum_locals(VMG_ void (*cbfunc)(void *, const char *, size_t),
662 void *cbctx, int level)
663 {
664 CVmFuncPtr func_ptr;
665 CVmDbgLinePtr line_ptr;
666 CVmDbgTablePtr dbg_ptr;
667 CVmDbgFramePtr frame_ptr;
668 CVmDbgFrameSymPtr sym;
669 const uchar *stm_start, *stm_end;
670 uint i;
671 uint frame_id;
672
673 /* get information on the execution location at the requested level */
674 if (get_stack_level_info(vmg_ level, &func_ptr, &line_ptr,
675 &stm_start, &stm_end))
676 return;
677
678 /* set up a debug table pointer for the function */
679 if (!func_ptr.set_dbg_ptr(&dbg_ptr))
680 return;
681
682 /* if there's a 'self' in this context, add it to the list */
683 if (G_interpreter->get_self_at_level(vmg_ level) != VM_INVALID_OBJ)
684 (*cbfunc)(cbctx, "self", 4);
685
686 /* iterate over the enclosing frames, starting with the innermost */
687 for (frame_id = line_ptr.get_frame_id() ; frame_id != 0 ;
688 frame_id = frame_ptr.get_enclosing_frame())
689 {
690 /* set up a frame table pointer for the current local frame */
691 dbg_ptr.set_frame_ptr(vmg_ &frame_ptr, frame_id);
692
693 /* set up a pointer to the first symbol */
694 frame_ptr.set_first_sym_ptr(vmg_ &sym);
695
696 /* iterate through the frame and call the callback for each symbol */
697 for (i = frame_ptr.get_sym_count() ; i != 0 ; --i, sym.inc(vmg0_))
698 (*cbfunc)(cbctx, sym.get_sym(vmg0_), sym.get_sym_len(vmg0_));
699 }
700 }
701
702 /* ------------------------------------------------------------------------ */
703 /*
704 * Callback for enumerating the named argument table, to add the named
705 * arguments to the stack trace.
706 */
707 struct named_arg_stack_ctx
708 {
709 named_arg_stack_ctx(CVmDebug *dbg, char *p, size_t rem, int argc)
710 {
711 this->dbg = dbg;
712 this->p = p;
713 this->rem = rem;
714 this->argc = argc;
715 }
716
717 /* debugger object */
718 CVmDebug *dbg;
719
720 /* current write buffer position */
721 char *p;
722 size_t rem;
723
724 /* number of arguments so far, including previous position arguments */
725 int argc;
726
727 void append(const char *txt, size_t len)
728 {
729 if (len < rem)
730 {
731 memcpy(p, txt, len);
732 p += len;
733 rem -= len;
734 *p = '\0';
735 }
736 else if (rem > 3)
737 {
738 strcpy(p, "...");
739 rem = 0;
740 }
741 else
742 {
743 rem = 0;
744 }
745 }
746 void append(const char *txt) { append(txt, strlen(txt)); }
747
748 void add(VMG_ const char *name, size_t len, const vm_val_t *val)
749 {
750 /* add "<comma> name: val" */
751 if (argc++ > 0)
752 append(", ");
753
754 /* add the name */
755 append(name, len);
756
757 /* add the colon */
758 append(":");
759
760 /* add the value */
761 dbg->format_val(vmg_ p, rem, val);
762
763 /* advance past it */
764 size_t l = strlen(p);
765 p += l;
766 rem -= l;
767 }
768 };
769
770
771 /* ------------------------------------------------------------------------ */
772 /*
773 * Build a stack traceback listing
774 */
775 void CVmDebug::build_stack_listing(VMG_
776 void (*cbfunc)(void *, const char *, int),
777 void *cbctx, int source_info)
778 {
779 const uchar *methodp;
780 vm_val_t *fp;
781 vm_obj_id_t self_obj;
782 const uchar *entry;
783 const vm_rcdesc *rc;
784 int level;
785
786 /* start at the current function */
787 fp = G_interpreter->get_frame_ptr();
788 self_obj = G_interpreter->get_self(vmg0_);
789 entry = entry_;
790 methodp = pc_;
791
792 /* iterate through the frames */
793 for (level = 0, rc = 0 ; fp != 0 ; ++level)
794 {
795 vm_obj_id_t def_obj;
796 int argc;
797 const vm_val_t *argp = 0;
798 char buf[256];
799 int i;
800 char *p;
801 char *open_paren_ptr = 0;
802 char *close_paren_ptr = 0;
803 size_t rem;
804
805 /* get the current 'self' object */
806 self_obj = G_interpreter->get_self_from_frame(vmg_ fp);
807
808 /* get the current number of arguments */
809 argc = G_interpreter->get_argc_from_frame(vmg_ fp);
810
811 /* get the current function's defining object */
812 def_obj = G_interpreter->get_defining_obj_from_frame(vmg_ fp);
813
814 /* determine whether we have an object.method or a function call */
815 if (methodp == 0)
816 {
817 /*
818 * A zero method offset indicates a native caller making a
819 * recursive entry into the byte-code interpreter. If we have
820 * a recursive caller context, we can get the information from
821 * it; otherwise show a generic "System" caller.
822 */
823 sprintf(buf, "<System>");
824 argc = 0;
825 if (rc != 0)
826 {
827 /* check what kind of native caller we have */
828 if (rc->bifptr.typ != VM_NIL)
829 {
830 /* we have a built-in function at this level */
831 const char *func_sym = bif_to_sym(
832 rc->bifptr.val.bifptr.set_idx,
833 rc->bifptr.val.bifptr.func_idx);
834 if (func_sym != 0)
835 sprintf(buf, "%.255s", func_sym);
836 else
837 sprintf(buf, "intrinsicFunction#%d.%d",
838 (rc->bifptr.val.intval >> 16) & 0xffff,
839 rc->bifptr.val.intval & 0xffff);
840
841 /* use the arguments from the native descriptor */
842 argc = rc->argc;
843 argp = rc->argp;
844 }
845 else if (rc->self.typ != VM_NIL)
846 {
847 const char *ic_sym = 0;
848 const char *prop_sym = 0;
849
850 /* intrinsic class method - get the metaclass */
851 CVmMetaclass *mc;
852 switch (rc->self.typ)
853 {
854 case VM_OBJ:
855 /* get the metaclass from the object */
856 mc = vm_objp(vmg_ rc->self.val.obj)
857 ->get_metaclass_reg();
858 break;
859
860 case VM_LIST:
861 /* list constant - use the List metaclass */
862 mc = CVmObjList::metaclass_reg_;
863 break;
864
865 case VM_SSTRING:
866 /* string constant - use the String metaclass */
867 mc = CVmObjString::metaclass_reg_;
868 break;
869
870 default:
871 /* other types don't have metaclasses */
872 mc = 0;
873 break;
874 }
875
876 /* get the registration table entry */
877 vm_meta_entry_t *me =
878 mc == 0 ? 0 :
879 G_meta_table->get_entry_from_reg(mc->get_reg_idx());
880
881 /* get the metaclass object and property from the entry */
882 if (me != 0)
883 {
884 /* get the IntrinsicClass object */
885 ic_sym = objid_to_sym(me->class_obj_);
886
887 /* get the property ID */
888 prop_sym = propid_to_sym(me->xlat_func(rc->method_idx));
889 }
890
891 /* build the object.property name */
892 if (ic_sym != 0)
893 sprintf(buf, "%.255s [", ic_sym);
894 else if (me != 0)
895 sprintf(buf, "IntrinsicClass#%lx [",
896 (unsigned long)me->class_obj_);
897 else
898 sprintf(buf, "IntrinsicClass [");
899
900 /* add the 'self' object */
901 size_t cur_len = strlen(buf);
902 p = buf + cur_len;
903 rem = sizeof(buf) - cur_len;
904 format_val(vmg_ p, rem - 10, &rc->self);
905
906 /* add the property separator */
907 cur_len = strlen(p);
908 rem -= cur_len;
909 p += cur_len;
910 if (rem > 10)
911 {
912 *p++ = ']';
913 *p++ = '.';
914 *p = '\0';
915 rem -= 2;
916 }
917
918 /* add the property name */
919 if (rem > 15)
920 {
921 if (prop_sym != 0)
922 sprintf(p, "%.*s", rem - 1, prop_sym);
923 else if (me != 0)
924 sprintf(p, "prop#%x",
925 (int)me->xlat_func(rc->method_idx));
926 else
927 sprintf(p, "prop#?");
928 }
929
930 /* use the arguments from the native descriptor */
931 argc = rc->argc;
932 argp = rc->argp;
933 }
934 }
935 }
936 else if (def_obj != VM_INVALID_OBJ)
937 {
938 const char *def_obj_sym;
939 const char *self_obj_sym;
940 const char *prop_sym;
941 vm_prop_id_t prop_id;
942 int is_anon_fn = FALSE;
943
944 /* it's an object.method call */
945 def_obj_sym = objid_to_sym(def_obj);
946 self_obj_sym = objid_to_sym(self_obj);
947
948 /* translate the object to the original 'modify' object name */
949 def_obj_sym = get_modifying_sym(def_obj_sym);
950
951 /*
952 * Add the object name. If we have a name for 'self', use that
953 * name; otherwise, if we have a name for the defining object,
954 * use that, and show the self object number after it;
955 * otherwise, if this is an anonymous function object, note
956 * that and show it specially; otherwise just show the object
957 * number.
958 */
959 if (def_obj != VM_INVALID_OBJ
960 && CVmObjAnonFn::is_anonfn_obj(vmg_ def_obj))
961 {
962 /*
963 * it's an anonymous function - note this, and use a
964 * special format for the display
965 */
966 is_anon_fn = TRUE;
967 sprintf(buf, "{anonfn:%lx}", (unsigned long)def_obj);
968 }
969 else if (self_obj_sym != 0)
970 {
971 /* 'self' has a name, so show that */
972 strcpy(buf, self_obj_sym);
973 }
974 else if (def_obj_sym != 0)
975 {
976 /*
977 * 'self' has no name, but the defining object does, so
978 * show the defining object name and add the 'self' object
979 * number
980 */
981 sprintf(buf, "%.240s [%lx]",
982 def_obj_sym, (unsigned long)self_obj);
983 }
984 else
985 {
986 /* there's no name to be had, so use the object number */
987 sprintf(buf, "[%lx]", (unsigned long)self_obj);
988 }
989
990 /* prepare to add to the buffer */
991 p = buf + strlen(buf);
992
993 /* get the property ID */
994 prop_id = G_interpreter->get_target_prop_from_frame(vmg_ fp);
995 if (is_anon_fn)
996 {
997 /* it's an anonymous function - there's no property part */
998 *p = '\0';
999 }
1000 else if (prop_id != VM_INVALID_PROP
1001 && (prop_sym = propid_to_sym(prop_id)) != 0)
1002 {
1003 /* we got the property symbol - add it */
1004 sprintf(p, ".%.255s", prop_sym);
1005 }
1006 else
1007 {
1008 /* unknown property name */
1009 sprintf(p, ".prop#%x", (uint)prop_id);
1010 }
1011 }
1012 else if (entry != 0)
1013 {
1014 const char *func_sym;
1015 char fbuf[256];
1016
1017 /* no object, so it's a function - find the symbol */
1018 if ((func_sym = funchdr_to_sym(vmg_ entry, fbuf)) != 0)
1019 sprintf(buf, "%.255s", func_sym);
1020 else
1021 sprintf(buf, "bytecode#%08lx", (unsigned long)entry);
1022 }
1023
1024 /* get the remainder of the buffer */
1025 p = buf + strlen(buf);
1026 rem = sizeof(buf) - (p - buf);
1027
1028 /* add the open paren */
1029 open_paren_ptr = p;
1030 *p++ = '(';
1031 --rem;
1032
1033 /* add the arguments */
1034 for (i = 0 ; i < argc ; ++i)
1035 {
1036 size_t cur_len;
1037
1038 /*
1039 * if we're running out of space, just add an indication
1040 * that more arguments follow, and stop here
1041 */
1042 if (rem <= 10)
1043 {
1044 strcpy(p, " ...");
1045 p += 4;
1046 rem -= 4;
1047 break;
1048 }
1049
1050 /*
1051 * add this argument, making sure we leave room over for the
1052 * closing paren, newline, null terminator, and if necessary
1053 * an ellipsis indicating we ran out of room
1054 */
1055 format_val(vmg_ p, rem - 10,
1056 argp != 0 ? argp - i :
1057 G_interpreter->get_param_from_frame(vmg_ fp, i));
1058
1059 /* move past the latest addition */
1060 cur_len = strlen(p);
1061 rem -= cur_len;
1062 p += cur_len;
1063
1064 /* add a comma if this isn't the last argument */
1065 if (i + 1 < argc)
1066 {
1067 *p++ = ',';
1068 *p++ = ' ';
1069 *p = '\0';
1070 rem -= 2;
1071 }
1072 }
1073
1074 /* check for named arguments */
1075 vm_val_t *nargp;
1076 const uchar *t = CVmRun::get_named_args_from_frame(vmg_ fp, &nargp);
1077 if (t != 0)
1078 {
1079 /* get the number of named arguments in the table */
1080 int cnt = osrp2(t);
1081 t += 2;
1082
1083 /*
1084 * Display the named arguments. The compiler builds the table
1085 * in right-to-left order, reflecting the order of pushing the
1086 * arguments onto the stack. For readability, reverse this for
1087 * display, so that the display order matches the original
1088 * source code order.
1089 */
1090 named_arg_stack_ctx ctx(this, p, rem, argc);
1091 nargp += (cnt - 1);
1092 for (int i = (cnt-1)*2 ; i >= 0 ; i -= 2, --nargp)
1093 {
1094 /* get this item's name and figure its length */
1095 uint ofs = osrp2(t + i);
1096 uint len = osrp2(t + i + 2) - ofs;
1097
1098 /* add the name */
1099 ctx.add(vmg_ (const char *)t + ofs, len, nargp);
1100 }
1101
1102 /* advance past the data we wrote */
1103 p = ctx.p;
1104 rem = ctx.rem;
1105 }
1106
1107 /* add a close paren */
1108 close_paren_ptr = p;
1109 *p++ = ')';
1110
1111 /* if we have room, add the method offset */
1112 if (methodp != 0 && entry != 0 && rem > 12)
1113 {
1114 sprintf(p, " + 0x%lX", (unsigned long)(methodp - entry));
1115 p += strlen(p);
1116 }
1117
1118 /* add a newline and a null terminator */
1119 *p++ = '\n';
1120 *p = '\0';
1121
1122 /* if they want source line information, add it */
1123 if (methodp != 0 && source_info)
1124 {
1125 const char *fname;
1126 unsigned long linenum;
1127
1128 /* get the source information */
1129 if (!get_source_info(vmg_ &fname, &linenum, level))
1130 {
1131 char numbuf[32];
1132 size_t fname_len;
1133 size_t num_len;
1134
1135 /* convert the line number to a string */
1136 sprintf(numbuf, " line %ld]\n", linenum);
1137
1138 /* get the lengths of what we're adding */
1139 fname_len = strlen(fname);
1140 num_len = strlen(numbuf);
1141
1142 /*
1143 * if we don't have room, try getting the root of the
1144 * filename, in case it has a long path prefix
1145 */
1146 if (fname_len + num_len + 2 >= rem)
1147 {
1148 /* skip to the root of the filename */
1149 fname = os_get_root_name((char *)fname);
1150
1151 /* get the new length */
1152 fname_len = strlen(fname);
1153 }
1154
1155 /*
1156 * If it's still too long, we must have a really long
1157 * argument list - try removing some of the argument list,
1158 * adding "..." in place of the removed bits, to make room
1159 * for the line number. Don't bother if we can't free up
1160 * enough space even doing this.
1161 */
1162 if (fname_len + num_len + 2 >= rem
1163 && close_paren_ptr != 0
1164 && (fname_len + num_len + 2
1165 < rem + (close_paren_ptr - open_paren_ptr - 4)))
1166 {
1167 char *dst;
1168 size_t adjust;
1169
1170 /*
1171 * get the adjustment size - back up by as much as we
1172 * need to make the filename/number fit, plus space
1173 * for the terminating null and the ellipsis
1174 */
1175 adjust = (fname_len + num_len + 2 - rem + 1 + 3);
1176
1177 /* back up by the adjustment */
1178 dst = close_paren_ptr - adjust;
1179
1180 /* insert the ellipsis */
1181 memcpy(dst, "...", 3);
1182
1183 /* move the part after the close paren */
1184 memmove(dst + 3, close_paren_ptr,
1185 strlen(close_paren_ptr));
1186
1187 /*
1188 * adjust by the amount we moved, not counting the
1189 * ellipsis we added
1190 */
1191 rem += adjust - 3;
1192 p -= adjust - 3;
1193 }
1194
1195 /* add it if there's room */
1196 if (fname_len + num_len + 2 < rem)
1197 {
1198 /* change the newline to a space */
1199 *(p-1) = ' ';
1200
1201 /* add the filename and line number in brackets */
1202 *p = '[';
1203 memcpy(p + 1, fname, fname_len);
1204 memcpy(p + 1 + fname_len, numbuf, num_len + 1);
1205 }
1206 }
1207 }
1208
1209 /* invoke the callback for this level */
1210 (*cbfunc)(cbctx, buf, strlen(buf));
1211
1212 /* move on to the enclosing frame */
1213 if (rc != 0 && rc->has_return_addr())
1214 {
1215 /*
1216 * recursive context - stay in the same frame, but move to the
1217 * bytecode caller
1218 */
1219 methodp = rc->get_return_addr();
1220 rc = 0;
1221 }
1222 else
1223 {
1224 /* no recursive context - move to the next stack frame */
1225 entry = G_interpreter
1226 ->get_enclosing_entry_ptr_from_frame(vmg_ fp);
1227 methodp = G_interpreter->get_return_addr_from_frame(vmg_ fp);
1228 rc = G_interpreter->get_rcdesc_from_frame(vmg_ fp);
1229 fp = G_interpreter->get_enclosing_frame_ptr(vmg_ fp);
1230 }
1231 }
1232 }
1233
1234 /* ------------------------------------------------------------------------ */
1235 /*
1236 * Toggle a breakpoint
1237 */
1238 int CVmDebug::toggle_breakpoint(VMG_ const uchar *code_addr,
1239 const char *cond, int change,
1240 int *bpnum, int *did_set,
1241 char *errbuf, size_t errbuflen)
1242 {
1243 char *code_ptr;
1244 CVmDebugBp *bp;
1245
1246 /* if there's a code address, get a pointer to it */
1247 if (code_addr != 0)
1248 {
1249 /* get a writable pointer to the code location */
1250 code_ptr = (char *)code_addr;
1251 }
1252 else
1253 {
1254 /* global breakpoint - no code address is required */
1255 code_ptr = 0;
1256 }
1257
1258 /* search for a breakpoint at this same address */
1259 bp = find_bp(code_addr);
1260
1261 /*
1262 * if we found it, we're deleting the old one; otherwise, we're
1263 * creating a new one
1264 */
1265 if (bp == 0)
1266 {
1267 int err;
1268
1269 /* setting a new breakpoint - allocate a new record */
1270 bp = alloc_bp();
1271 if (bp == 0)
1272 {
1273 /* generate a message */
1274 if (errbuf != 0 && errbuflen != 0)
1275 lib_strcpy(errbuf, errbuflen,
1276 "out of internal breakpoint records");
1277
1278 /* return failure */
1279 return 2;
1280 }
1281
1282 /* initialize the breakpoint */
1283 if ((err = bp->set_info(vmg_ code_addr, cond, change, FALSE,
1284 errbuf, errbuflen)) != 0)
1285 {
1286 /* un-allocate the breakpoint */
1287 bp->set_in_use(FALSE);
1288
1289 /* return the error */
1290 return err;
1291 }
1292
1293 /* indicate to the caller that we're creating a breakpoint */
1294 *did_set = TRUE;
1295
1296 /*
1297 * fill in the breakpoint ID for the caller - this is just the
1298 * 1-based index into our breakpoint array of the breakpoint
1299 * record
1300 */
1301 *bpnum = (int)(bp - bp_) + 1;
1302
1303 /* if it's global, count it */
1304 if (bp->is_global())
1305 ++global_bp_cnt_;
1306 }
1307 else
1308 {
1309 /* if it's global, count the deletion */
1310 if (bp->is_global() && !bp->is_disabled())
1311 --global_bp_cnt_;
1312
1313 /* free the breakpoint record */
1314 bp->do_delete(vmg0_);
1315
1316 /* indicate to the caller that we're deleting the breakpoint */
1317 *did_set = FALSE;
1318
1319 /* let the caller know which breakpoint we deleted */
1320 *bpnum = (int)(bp - bp_) + 1;
1321 }
1322
1323 /* success */
1324 return 0;
1325 }
1326
1327 /*
1328 * Toggle a breakpoint's enabled/disabled status
1329 */
1330 void CVmDebug::toggle_breakpoint_disable(VMG_ int bpnum)
1331 {
1332 CVmDebugBp *bp;
1333
1334 /* if the breakpoint ID is invalid, ignore the request */
1335 if (bpnum < 1 || bpnum > VMDBG_BP_MAX)
1336 return;
1337
1338 /* get the breakpoint record (the breakpoint ID is a 1-based index) */
1339 bp = bp_ + (bpnum - 1);
1340
1341 /* toggle the disabled flag */
1342 set_breakpoint_disable(vmg_ bpnum, !bp->is_disabled());
1343 }
1344
1345 /*
1346 * Set a breakpoint's disabled status
1347 */
1348 void CVmDebug::set_breakpoint_disable(VMG_ int bpnum, int disable)
1349 {
1350 CVmDebugBp *bp;
1351
1352 /* if the breakpoint ID is invalid, ignore the request */
1353 if (bpnum < 1 || bpnum > VMDBG_BP_MAX)
1354 return;
1355
1356 /* get the breakpoint record (the breakpoint ID is a 1-based index) */
1357 bp = bp_ + (bpnum - 1);
1358
1359 /* set the new state */
1360 bp->set_disabled(disable);
1361
1362 /* add or remove the breakpoint from the code if necessary */
1363 bp->set_bp_instr(vmg_ !bp->is_disabled(), FALSE);
1364
1365 /* if it's global, count the change */
1366 if (bp->is_global())
1367 {
1368 if (bp->is_disabled())
1369 --global_bp_cnt_;
1370 else
1371 ++global_bp_cnt_;
1372 }
1373 }
1374
1375 /*
1376 * Determine if a breakpoint is disabled
1377 */
1378 int CVmDebug::is_breakpoint_disabled(VMG_ int bpnum)
1379 {
1380 CVmDebugBp *bp;
1381
1382 /* if the breakpoint ID is invalid, ignore the request */
1383 if (bpnum < 1 || bpnum > VMDBG_BP_MAX)
1384 return FALSE;
1385
1386 /* get the breakpoint record (the breakpoint ID is a 1-based index) */
1387 bp = bp_ + (bpnum - 1);
1388
1389 /* return the status */
1390 return (bp->is_disabled() != 0);
1391 }
1392
1393 /*
1394 * Set a breakpoint's condition expression
1395 */
1396 int CVmDebug::set_breakpoint_condition(VMG_ int bpnum,
1397 const char *cond, int change,
1398 char *errbuf, size_t errbuflen)
1399 {
1400 CVmDebugBp *bp;
1401 int err;
1402
1403 /* if the breakpoint ID is invalid, ignore the request */
1404 if (bpnum < 1 || bpnum > VMDBG_BP_MAX)
1405 {
1406 /* set the error message if needed */
1407 if (errbuf != 0 && errbuflen != 0)
1408 lib_strcpy(errbuf, errbuflen, "invalid breakpoint");
1409
1410 /* return failure */
1411 return 1;
1412 }
1413
1414 /* get the breakpoint record (the breakpoint ID is a 1-based index) */
1415 bp = bp_ + (bpnum - 1);
1416
1417 /* set the condition */
1418 if ((err = bp->set_condition(vmg_ cond, change, errbuf, errbuflen)) != 0)
1419 return err;
1420
1421 /* success */
1422 return 0;
1423 }
1424
1425 /*
1426 * Delete a breakpoint
1427 */
1428 void CVmDebug::delete_breakpoint(VMG_ int bpnum)
1429 {
1430 CVmDebugBp *bp;
1431
1432 /* if the breakpoint ID is invalid, ignore the request */
1433 if (bpnum < 1 || bpnum > VMDBG_BP_MAX)
1434 return;
1435
1436 /* get the breakpoint record (the breakpoint ID is a 1-based index) */
1437 bp = bp_ + (bpnum - 1);
1438
1439 /*
1440 * If the breakpoint is global, update the global bp count. (Don't
1441 * bother if the breakpoint is disabled, as we don't include disabled
1442 * breakpoints in the count in the first place.)
1443 */
1444 if (bp->is_global() && !bp->is_disabled())
1445 --global_bp_cnt_;
1446
1447 /* clear the breakpoint */
1448 bp->do_delete(vmg0_);
1449 }
1450
1451 /*
1452 * Allocate a new breakpoint record
1453 */
1454 CVmDebugBp *CVmDebug::alloc_bp()
1455 {
1456 size_t i;
1457 CVmDebugBp *bp;
1458
1459 /* scan our list for an entry not currently in use */
1460 for (i = 0, bp = bp_ ; i < VMDBG_BP_MAX ; ++i, ++bp)
1461 {
1462 /* if this one isn't in use, allocate it */
1463 if (!bp->is_in_use())
1464 {
1465 /* mark it in use */
1466 bp->set_in_use(TRUE);
1467
1468 /* return the new record */
1469 return bp;
1470 }
1471 }
1472
1473 /* there are no free records - return failure */
1474 return 0;
1475 }
1476
1477 /*
1478 * Find a breakpoint at a given address
1479 */
1480 CVmDebugBp *CVmDebug::find_bp(const uchar *code_addr)
1481 {
1482 size_t i;
1483 CVmDebugBp *bp;
1484
1485 /*
1486 * if the code address is zero, we know there can't be a breakpoint
1487 * there
1488 */
1489 if (code_addr == 0)
1490 return 0;
1491
1492 /* scan our list */
1493 for (i = 0, bp = bp_ ; i < VMDBG_BP_MAX ; ++i, ++bp)
1494 {
1495 /* if this one is in use, and it matches the address, it's the one */
1496 if (bp->is_in_use() && bp->get_code_addr() == code_addr)
1497 return bp;
1498 }
1499
1500 /* we didn't find it */
1501 return 0;
1502 }
1503
1504
1505 /* ------------------------------------------------------------------------ */
1506 /*
1507 * Format a value, allocating space
1508 */
1509 char *CVmDebug::format_val(VMG_ const vm_val_t *val)
1510 {
1511 /* first, do the formatting to figure the space we need */
1512 size_t len = format_val(vmg_ 0, 0, val);
1513
1514 /* allocate space */
1515 char *buf = (char *)t3malloc(len + 1);
1516
1517 /* do the formatting for real this time */
1518 format_val(vmg_ buf, len + 1, val);
1519
1520 /* return the allocated buffer */
1521 return buf;
1522 }
1523
1524 /* ------------------------------------------------------------------------ */
1525 /*
1526 * Generic string traverser - for utf-8 or ucs-2 strings
1527 */
1528 class StrSource
1529 {
1530 public:
1531 virtual wchar_t nextch() = 0;
1532 size_t rem;
1533 };
1534
1535 class StrSourceUTF: public StrSource
1536 {
1537 public:
1538 StrSourceUTF *init(const char *p)
1539 {
1540 rem = vmb_get_len(p);
1541 this->p.set((char *)p + VMB_LEN);
1542 return this;
1543 }
1544
1545 virtual wchar_t nextch()
1546 {
1547 if (rem > 0)
1548 {
1549 wchar_t ch = p.getch();
1550 p.inc(&rem);
1551 return ch;
1552 }
1553 else return 0;
1554 }
1555
1556 utf8_ptr p;
1557 };
1558
1559 class StrSourceWide: public StrSource
1560 {
1561 public:
1562 StrSourceWide *init(const wchar_t *p, size_t len)
1563 {
1564 this->p = p;
1565 this->rem = len;
1566 return this;
1567 }
1568
1569 virtual wchar_t nextch()
1570 {
1571 if (rem > 0)
1572 {
1573 --rem;
1574 return *p++;
1575 }
1576 else
1577 return 0;
1578 }
1579
1580 const wchar_t *p;
1581 };
1582
1583
1584 /* ------------------------------------------------------------------------ */
1585 /*
1586 * Lookup table enumerator for building an inline value display
1587 */
1588
1589 /* callback context */
1590 struct lookup_lister_ctx
1591 {
1592 /* debugger object */
1593 CVmDebug *dbg;
1594
1595 /* the output buffer */
1596 char *dst;
1597 size_t dstlen;
1598
1599 /* the total space we need so far */
1600 size_t retlen;
1601
1602 /* number of items so far */
1603 int cnt;
1604
1605 /* enumerator callback, static version */
1606 static void scb(
1607 VMG_ const vm_val_t *key, const vm_val_t *val, void *ctx0)
1608 {
1609 /* get our context, properly cast, and call its handler */
1610 lookup_lister_ctx *ctx = (lookup_lister_ctx *)ctx0;
1611 ctx->cb(vmg_ key, val);
1612 }
1613
1614 /* enumerator callback, method version */
1615 void cb(VMG_ const vm_val_t *key, const vm_val_t *val)
1616 {
1617 /* if this isn't the first element, add a comma */
1618 if (cnt++ != 0)
1619 append(",");
1620
1621 /* format this key (or just "*" if this is the default value) */
1622 if (key != 0)
1623 consume(dbg->format_val(vmg_ dst, dstlen, key));
1624 else
1625 append("*");
1626
1627 /* add the -> */
1628 append("->");
1629
1630 /* format the value */
1631 consume(dbg->format_val(vmg_ dst, dstlen, val));
1632 }
1633
1634 /* append text */
1635 void append(const char *txt) { append(txt, strlen(txt)); }
1636 void append(const char *txt, size_t len)
1637 {
1638 /* copy up to the available space */
1639 size_t copylen = (len < dstlen ? len : dstlen > 0 ? dstlen - 1 : 0);
1640
1641 /* copy as much as we can */
1642 if (copylen != 0)
1643 memcpy(dst, txt, copylen);
1644
1645 /* consume the space */
1646 consume(len);
1647 }
1648
1649 /* consume space */
1650 void consume(size_t len)
1651 {
1652 /* count the full text length in the target size */
1653 retlen += len;
1654
1655 /* skip the space, but always leave room for a null terminator */
1656 if (dst != 0 && dstlen > 0)
1657 {
1658 if (len < dstlen)
1659 {
1660 dst += len;
1661 dstlen -= len;
1662 }
1663 else
1664 {
1665 dst += dstlen - 1;
1666 dstlen = 1;
1667 }
1668 }
1669 }
1670 };
1671
1672
1673
1674 /* ------------------------------------------------------------------------ */
1675 /*
1676 * Format a value into the given buffer
1677 */
1678 size_t CVmDebug::format_val(VMG_ char *dst, size_t dstlen, const vm_val_t *val)
1679 {
1680 const char *p;
1681 char buf[128];
1682 size_t retlen = 0;
1683 StrSource *ps = 0;
1684 StrSourceUTF psUTF;
1685 StrSourceWide psWide;
1686
1687 /* format the value based on the type */
1688 switch(val->typ)
1689 {
1690 case VM_NIL:
1691 /* constant 'nil' value */
1692 p = "nil";
1693 break;
1694
1695 case VM_TRUE:
1696 /* constant 'true' value */
1697 p = "true";
1698 break;
1699
1700 case VM_OBJ:
1701 /* object reference - get the object's name */
1702 p = objid_to_sym(val->val.obj);
1703
1704 /* if there's no symbol name, try alternative display formats */
1705 if (p == 0)
1706 {
1707 /* try it as a string */
1708 if ((p = vm_objp(vmg_ val->val.obj)->get_as_string(vmg0_)) != 0)
1709 {
1710 ps = psUTF.init(p);
1711 goto format_string;
1712 }
1713
1714 /* try it as a list */
1715 if (vm_objp(vmg_ val->val.obj)->get_as_list() != 0)
1716 goto format_list;
1717
1718 /* try it as a BigNumber */
1719 if (CVmObjBigNum::is_bignum_obj(vmg_ val->val.obj))
1720 {
1721 /* cast it to a BigNumber object */
1722 CVmObjBigNum *bn = (CVmObjBigNum *)vm_objp(vmg_ val->val.obj);
1723
1724 /* try formatting it as a numeric value */
1725 if (bn->cvt_to_string_buf(
1726 buf, sizeof(buf), -1, -1, -1, -1,
1727 VMBN_FORMAT_POINT | VMBN_FORMAT_COMPACT) != 0)
1728 {
1729 /*
1730 * We got a string, but it's in length-prefix notation.
1731 * Change it to C notation by moving the string
1732 * contents to the bottom of the buffer and
1733 * null-terminating.
1734 */
1735 size_t len = vmb_get_len(buf);
1736 memmove(buf, buf + VMB_LEN, len);
1737 buf[len] = '\0';
1738 }
1739 else
1740 {
1741 /* couldn't format it - show it as an obj# value */
1742 sprintf(buf, "obj#%lx (BigNumber)",
1743 (unsigned long)val->val.obj);
1744 p = buf;
1745 }
1746
1747 /* use the buffer */
1748 p = buf;
1749
1750 /* handled */
1751 break;
1752 }
1753
1754 /* try it as a Date object */
1755 if (CVmObjDate::is_date_obj(vmg_ val->val.obj))
1756 {
1757 /* cast it to a Date */
1758 CVmObjDate *date = (CVmObjDate *)vm_objp(vmg_ val->val.obj);
1759
1760 /* get the formatted date */
1761 char buf2[64];
1762 date->format_string_buf(vmg_ buf2, sizeof(buf2));
1763
1764 /* store it as "Date(m/d/y...)" */
1765 t3sprintf(buf, sizeof(buf), "Date(%s)", buf2);
1766
1767 /* use the formatted text */
1768 p = buf;
1769 break;
1770 }
1771
1772 /* try it as a TimeZone object */
1773 if (CVmObjTimeZone::is_CVmObjTimeZone_obj(vmg_ val->val.obj))
1774 {
1775 /* cast it to a TimeZone */
1776 CVmObjTimeZone *tz =
1777 (CVmObjTimeZone *)vm_objp(vmg_ val->val.obj);
1778
1779 /* store it as "TimeZone(name)" */
1780 size_t len;
1781 const char *name = tz->get_tz()->get_name(len);
1782 t3sprintf(buf, sizeof(buf), "TimeZone(%.*s)", (int)len, name);
1783
1784 /* use the formatted text */
1785 p = buf;
1786 break;
1787 }
1788
1789 /* try it as a FileName object */
1790 if (CVmObjFileName::is_vmfilnam_obj(vmg_ val->val.obj))
1791 {
1792 /* cast it */
1793 CVmObjFileName *fn = vm_val_cast(CVmObjFileName, val);
1794
1795 /* store it as "FileName(name)" */
1796 const char *name = fn->get_path_string() + VMB_LEN;
1797 t3sprintf(buf, sizeof(buf), "FileName(%s)", name);
1798
1799 /* use the formatted text */
1800 p = buf;
1801 break;
1802 }
1803
1804 /*
1805 * It's a dynamically-created object with no name. Show it
1806 * by object it.
1807 */
1808 sprintf(buf, "obj#%lx", (unsigned long)val->val.obj);
1809
1810 /* if possible, add the name of the of the superclass */
1811 if (vm_objp(vmg_ val->val.obj)
1812 ->get_superclass_count(vmg_ val->val.obj) > 0)
1813 {
1814 vm_obj_id_t sc_obj;
1815
1816 /* get the superclass */
1817 sc_obj = vm_objp(vmg_ val->val.obj)
1818 ->get_superclass(vmg_ val->val.obj, 0);
1819
1820 /* if it's valid, look up the superclass name */
1821 if (sc_obj != 0 && (p = objid_to_sym(sc_obj)) != 0)
1822 {
1823 /* add "(superclass name)" after the numeric ID */
1824 sprintf(buf + strlen(buf), " (%.100s)", p);
1825 }
1826 }
1827
1828 /*
1829 * if it's a StringBuffer, Vector, or LookupTable, start the
1830 * result buffer with the object ID string, but also add an
1831 * inline expansion of the contents
1832 */
1833 if (CVmObjStringBuffer::is_string_buffer_obj(vmg_ val->val.obj)
1834 || (CVmObjVector::is_vector_obj(vmg_ val->val.obj)
1835 && !CVmObjAnonFn::is_anonfn_obj(vmg_ val->val.obj))
1836 || CVmObjLookupTable::is_lookup_table_obj(vmg_ val->val.obj)
1837 || CVmObjPattern::is_pattern_obj(vmg_ val->val.obj))
1838 {
1839 /* figure the length we need to add for the type name */
1840 size_t len = strlen(buf);
1841 retlen += len + 1;
1842
1843 /* if there's room, add the type name */
1844 if (dstlen > len + 1)
1845 {
1846 memcpy(dst, buf, len);
1847 dst[len] = ' ';
1848 dst += len + 1;
1849 dstlen -= len + 1;
1850 }
1851
1852 /* if it's a StringBuffer, add the string contents */
1853 if (CVmObjStringBuffer::is_string_buffer_obj(vmg_ val->val.obj))
1854 {
1855 CVmObjStringBuffer *strbuf =
1856 ((CVmObjStringBuffer *)vm_objp(vmg_ val->val.obj));
1857 ps = psWide.init(strbuf->get_buf(), strbuf->get_len());
1858 goto format_string;
1859 }
1860
1861 /* if it's a RexPattern object, add the pattern string */
1862 if (CVmObjPattern::is_pattern_obj(vmg_ val->val.obj))
1863 {
1864 /* get the pattern */
1865 CVmObjPattern *pat =
1866 (CVmObjPattern *)vm_objp(vmg_ val->val.obj);
1867
1868 /* get the source string, if it has one */
1869 const vm_val_t *patval;
1870 if ((patval = pat->get_orig_str()) != 0
1871 && (p = patval->get_as_string(vmg0_)) != 0)
1872 {
1873 /* got it - go add the string to the display */
1874 ps = psUTF.init(p);
1875 goto format_string;
1876 }
1877 }
1878
1879 /* if it's a Vector, add the contents in list format */
1880 if (CVmObjVector::is_vector_obj(vmg_ val->val.obj))
1881 goto format_list;
1882
1883 /* if it's a lookup table, add the table contents */
1884 if (CVmObjLookupTable::is_lookup_table_obj(vmg_ val->val.obj))
1885 {
1886 /* get the lookup table object, properly cast */
1887 CVmObjLookupTable *lt =
1888 (CVmObjLookupTable *)vm_objp(vmg_ val->val.obj);
1889
1890 /* set up our enumerator context */
1891 lookup_lister_ctx lctx;
1892 lctx.dbg = this;
1893 lctx.dst = dst;
1894 lctx.dstlen = dstlen;
1895 lctx.retlen = retlen;
1896 lctx.cnt = 0;
1897
1898 /* start the list */
1899 lctx.append("[");
1900
1901 /* enumerate the table's contents */
1902 lt->for_each(vmg_ &lctx.scb, &lctx);;
1903
1904 /* if there's a default value, add it */
1905 vm_val_t defval;
1906 lt->get_default_val(&defval);
1907 if (defval.typ != VM_NIL)
1908 lctx.cb(vmg_ 0, &defval);
1909
1910 /* end the list */
1911 lctx.append("]");
1912
1913 /* null-terminate the table */
1914 if (lctx.dst != 0 && lctx.dstlen > 0)
1915 *lctx.dst = '\0';
1916
1917 /* we've handled this value fully */
1918 return lctx.retlen;
1919 }
1920 }
1921
1922 /* use the buffer we formatted */
1923 p = buf;
1924 }
1925 break;
1926
1927 case VM_PROP:
1928 /* property ID - get the property name */
1929 p = propid_to_sym(val->val.prop);
1930 if (p != 0)
1931 {
1932 /* we have a symbol - use it with an ampersand operator */
1933 sprintf(buf, "&%.127s", p);
1934 }
1935 else
1936 {
1937 /* there's no symbol, so use the numeric property ID */
1938 sprintf(buf, "prop#%x", (uint)val->val.prop);
1939 }
1940
1941 /* use the text we built in the buffer */
1942 p = buf;
1943 break;
1944
1945 case VM_ENUM:
1946 /* enum ID - get the enum name */
1947 p = enum_to_sym(val->val.enumval);
1948
1949 /* if there's no symbol, use the numeric enum ID */
1950 if (p == 0)
1951 {
1952 sprintf(buf, "enum#%x", (uint)val->val.enumval);
1953 p = buf;
1954 }
1955 break;
1956
1957 case VM_BIFPTR:
1958 /* built-in function pointer - get the function name */
1959 p = bif_to_sym(val->val.bifptr.set_idx, val->val.bifptr.func_idx);
1960 if (p != 0)
1961 {
1962 /* we have a symbol - use it with an ampersand operator */
1963 sprintf(buf, "&%.127s", p);
1964 p = buf;
1965 }
1966 else
1967 {
1968 /* there's no symbol, so use the numeric ID */
1969 sprintf(buf, "&intrinsic_function#%u:%u",
1970 val->val.bifptr.set_idx, val->val.bifptr.func_idx);
1971 p = buf;
1972 }
1973 break;
1974
1975 case VM_INT:
1976 /* integer */
1977 sprintf(buf, "%ld", (long)val->val.intval);
1978 p = buf;
1979 break;
1980
1981 case VM_SSTRING:
1982 /* length-prefixed string - get a pointer to the data */
1983 ps = psUTF.init(G_const_pool->get_ptr(val->val.ofs));
1984
1985 format_string:
1986 /* add an open quote, if we have room for it */
1987 ++retlen;
1988 if (dstlen > 1)
1989 {
1990 *dst++ = '\'';
1991 --dstlen;
1992 }
1993
1994 /*
1995 * keep going until we run out of source text or space in our
1996 * output buffer - leave ourselves 5 extra bytes (close quote,
1997 * "..." if we run out of space, and null terminator)
1998 */
1999 while (ps->rem != 0)
2000 {
2001 /* get the current unicode character code */
2002 wchar_t uni = ps->nextch();
2003
2004 /*
2005 * determine if the character is mappable to the local
2006 * character set
2007 */
2008 int mappable = G_cmap_to_ui->is_mappable(uni);
2009
2010 /*
2011 * If there's no mapping, insert a '\u' sequence for the
2012 * character. If the character is in the non-printable control
2013 * character range, use a backslash representation as well. If
2014 * the character is a backslash or single quote, also represent
2015 * it as a backslash sequence, since it would need to be quoted
2016 * in a string submitted to the compiler.
2017 */
2018 if (uni < 32 || uni == '\\' || uni == '\'')
2019 {
2020 char esc;
2021 unsigned int dig;
2022
2023 switch(uni)
2024 {
2025 case 9:
2026 /* tab - '\t' */
2027 esc = 't';
2028
2029 do_escape:
2030 /* add the escape sequence */
2031 retlen += 2;
2032 if (dstlen > 2)
2033 {
2034 *dst++ = '\\';
2035 *dst++ = esc;
2036 dstlen -= 2;
2037 }
2038 break;
2039
2040 case 10:
2041 /* newline - '\n' */
2042 esc = 'n';
2043 goto do_escape;
2044
2045 case 13:
2046 /* return - '\r' */
2047 esc = 'r';
2048 goto do_escape;
2049
2050 case 0x000F:
2051 /* caps - '\^' */
2052 esc = '^';
2053 goto do_escape;
2054
2055 case 0x000E:
2056 /* uncaps - '\v' */
2057 esc = 'v';
2058 goto do_escape;
2059
2060 case 0x000B:
2061 /* blank - '\b' */
2062 esc = 'b';
2063 goto do_escape;
2064
2065 case 0x0015:
2066 /* quoted space - '\ ' */
2067 esc = ' ';
2068 goto do_escape;
2069
2070 case '\\':
2071 case '\'':
2072 esc = (char)uni;
2073 goto do_escape;
2074
2075 default:
2076 /* represent anything else as a hex sequence */
2077 retlen += 4;
2078 if (dstlen > 4)
2079 {
2080 /* add the backslash and 'x' */
2081 *dst++ = '\\';
2082 *dst++ = 'x';
2083
2084 /* add the the first hex digit */
2085 dig = (uni >> 4) & 0xF;
2086 *dst++ = dig + (dig < 10 ? '0' : 'A' - 10);
2087
2088 /* add the second hex digit */
2089 dig = (uni & 0xF);
2090 *dst++ = dig + (dig < 10 ? '0' : 'A' - 10);
2091
2092 /* consume the space */
2093 dstlen -= 4;
2094 }
2095 break;
2096 }
2097 }
2098 else if (!mappable)
2099 {
2100 int i;
2101 unsigned int c;
2102
2103 /*
2104 * the '\u' sequence requires six bytes (backslash, 'u',
2105 * and the four hex digits of the character code)
2106 */
2107 retlen += 6;
2108 if (dstlen > 6)
2109 {
2110 /* add the '\u' */
2111 *dst++ = '\\';
2112 *dst++ = 'u';
2113
2114 /* add the four hex digits */
2115 for (c = (unsigned int)uni, i = 0 ; i < 4 ; ++i)
2116 {
2117 unsigned int dig;
2118
2119 /* get the current most significant digit's value */
2120 dig = (c >> 12) & 0xf;
2121
2122 /* generate the next hex digit */
2123 *dst++ = dig + (dig < 10 ? '0' : 'A' - 10);
2124
2125 /* shift up so the next digit is in current */
2126 c <<= 4;
2127 }
2128
2129 /* consume the space */
2130 dstlen -= 6;
2131 }
2132 }
2133 else
2134 {
2135 char buf[20];
2136 size_t xlat_len;
2137 size_t copy_len;
2138
2139 /* get the translation of this character */
2140 xlat_len = G_cmap_to_ui->map_char(uni, buf, sizeof(buf));
2141 copy_len = (dstlen <= 1 ? 0 :
2142 xlat_len < dstlen ? xlat_len :
2143 dstlen - 1);
2144
2145 /* copy the translation */
2146 memcpy(dst, buf, copy_len);
2147
2148 /* advance past the space we've used */
2149 retlen += xlat_len;
2150 dst += copy_len;
2151 dstlen -= copy_len;
2152 }
2153 }
2154
2155 /* add the close quote */
2156 retlen += 1;
2157 if (dstlen > 1)
2158 *dst++ = '\'';
2159
2160 /* add the null terminator */
2161 if (dstlen > 0)
2162 *dst++ = '\0';
2163
2164 /* we're done - don't bother with the normal copying */
2165 return retlen;
2166
2167 case VM_LIST:
2168 format_list:
2169 {
2170 /* get the element count */
2171 size_t cnt = val->ll_length(vmg0_);
2172
2173 /* add the open bracket */
2174 retlen += 1;
2175 if (dstlen > 1)
2176 {
2177 *dst++ = '[';
2178 --dstlen;
2179 }
2180
2181 /* scan through the elements (using a 1-based counter) */
2182 for (size_t i = 1 ; i <= cnt ; ++i)
2183 {
2184 /* get this element */
2185 vm_val_t ele;
2186 val->ll_index(vmg_ &ele, i);
2187
2188 /* format the element */
2189 size_t cur_len = format_val(vmg_ dst, dstlen, &ele);
2190 retlen += cur_len;
2191
2192 /* consume the space */
2193 if (dst != 0 && dstlen > 0)
2194 {
2195 if (cur_len < dstlen)
2196 {
2197 dstlen -= cur_len;
2198 dst += cur_len;
2199 }
2200 else
2201 {
2202 dst += dstlen - 1;
2203 dstlen = 1;
2204 }
2205 }
2206
2207 /* add a comma between elements if possible */
2208 if (i != cnt)
2209 {
2210 retlen += 1;
2211 if (dstlen > 1)
2212 {
2213 *dst++ = ',';
2214 --dstlen;
2215 }
2216 }
2217 }
2218
2219 /* add the closing bracket if possible */
2220 retlen += 1;
2221 if (dstlen > 1)
2222 {
2223 *dst++ = ']';
2224 --dstlen;
2225 }
2226
2227 /* add the null terminator */
2228 if (dstlen > 0)
2229 *dst = '\0';
2230 }
2231
2232 /* we're done - don't bother with normal copying */
2233 return retlen;
2234
2235 case VM_FUNCPTR:
2236 /* function pointer - get the function name */
2237 p = funcaddr_to_sym(val->val.ofs);
2238
2239 /* if there's no symbol, use the numeric function address */
2240 if (p == 0)
2241 {
2242 sprintf(buf, "function#%08lx", (unsigned long)val->val.ofs);
2243 p = buf;
2244 }
2245 break;
2246
2247 default:
2248 p = "?";
2249 break;
2250 }
2251
2252 /* copy as much of the value as possible into the buffer */
2253 retlen = strlen(p);
2254 lib_strcpy(dst, dstlen, p);
2255
2256 /* make sure the buffer is null-terminated */
2257 if (dstlen > 0)
2258 dst[dstlen - 1] = '\0';
2259
2260 /* return the total length required */
2261 return retlen;
2262 }
2263
2264 /* ------------------------------------------------------------------------ */
2265 /*
2266 * Format a value as a special
2267 */
2268 void CVmDebug::format_special(VMG_ char *buf, size_t buflen,
2269 const vm_val_t *val)
2270 {
2271 /* we need a fairly fixed size for the buffer: "#__type xxxxxxxx" */
2272 if (buflen < 25)
2273 {
2274 buf[0] = '\0';
2275 return;
2276 }
2277
2278 switch (val->typ)
2279 {
2280 case VM_NIL:
2281 strcpy(buf, "nil");
2282 return;
2283
2284 case VM_TRUE:
2285 strcpy(buf, "true");
2286 return;
2287
2288 case VM_OBJ:
2289 sprintf(buf, "#__obj %lu", (unsigned long)val->val.obj);
2290 return;
2291
2292 case VM_PROP:
2293 sprintf(buf, "#__prop %u", (unsigned int)val->val.prop);
2294 break;
2295
2296 case VM_INT:
2297 sprintf(buf, "%ld", (long)val->val.intval);
2298 return;
2299
2300 case VM_SSTRING:
2301 sprintf(buf, "#__sstr %lu", (unsigned long)val->val.ofs);
2302 return;
2303
2304 case VM_LIST:
2305 sprintf(buf, "#__list %lu", (unsigned long)val->val.ofs);
2306 return;
2307
2308 case VM_FUNCPTR:
2309 sprintf(buf, "#__func %lu", (unsigned long)val->val.ofs);
2310 return;
2311
2312 case VM_ENUM:
2313 sprintf(buf, "#__enum %lu", (unsigned long)val->val.enumval);
2314 return;
2315
2316 case VM_BIFPTR:
2317 sprintf(buf, "#__bifptr %u %u",
2318 val->val.bifptr.set_idx, val->val.bifptr.func_idx);
2319 return;
2320
2321 default:
2322 strcpy(buf, "#__invalid");
2323 return;
2324 }
2325 }
2326
2327 /* ------------------------------------------------------------------------ */
2328 /*
2329 * Callback context structure for aggregation enumerations
2330 */
2331 struct eval_enum_cb_ctx
2332 {
2333 /* the UI callback function to invoke and its own context */
2334 void (*ui_cb)(void *, const char *, int, const char *);
2335 void *ui_cb_ctx;
2336
2337 /* the original object whose properties we're enumerating */
2338 vm_obj_id_t obj;
2339
2340 /* debugger object */
2341 CVmDebug *dbg;
2342 };
2343
2344 /*
2345 * Lookup table enumeration callback for expression evaluation
2346 */
2347 static void enum_lookup_keys_cb(
2348 VMG_ const vm_val_t *key, const vm_val_t *val, void *ctx0)
2349 {
2350 /* get our context, properly cast */
2351 eval_enum_cb_ctx *ctx = (eval_enum_cb_ctx *)ctx0;
2352
2353 /* format the key value */
2354 char keybuf[256];
2355 keybuf[0] = '[';
2356 ctx->dbg->format_val(vmg_ keybuf+1, sizeof(keybuf)-3, key);
2357 strcat(keybuf, "]");
2358
2359 /*
2360 * For the relationship, generate the special __type# notation for the
2361 * key. This is a special debugger-only compact format with full
2362 * round-trip fidelity for all value types. Some values have no
2363 * ordinary source code representation, such as objects without symbol
2364 * names; the __type# notation ensures that every value is
2365 * representable in source. Note that we also use the "!" notation to
2366 * let the debugger know that this is the whole child expression rather
2367 * than an operator that we join with the key expression.
2368 */
2369 char relbuf[128], valbuf[120];
2370 ctx->dbg->format_special(vmg_ valbuf, sizeof(valbuf), key);
2371 sprintf(relbuf, "![%s]", valbuf);
2372
2373 /* send the formatted values to the debugger UI */
2374 (*ctx->ui_cb)(ctx->ui_cb_ctx, keybuf, strlen(keybuf), relbuf);
2375 }
2376
2377 /*
2378 * Object property enumeration callback for evaluating an object-valued
2379 * expression.
2380 */
2381 static void enum_props_eval_cb(VMG_ void *ctx0,
2382 vm_obj_id_t self, vm_prop_id_t prop,
2383 const vm_val_t *val)
2384 {
2385 /* get our context, properly cast */
2386 eval_enum_cb_ctx *ctx = (eval_enum_cb_ctx *)ctx0;
2387
2388 /*
2389 * The property enumerator tells us about all of the properties
2390 * throughout the entire inheritance tree. Make sure this one isn't
2391 * overridden - if it is, we'll already have shown (or suppressed
2392 * showing) the overriding copy of the property. Since overridden
2393 * properties can't be seen in the actual object, we don't want to show
2394 * them here.
2395 */
2396 vm_val_t ov_val;
2397 vm_obj_id_t src_obj;
2398 if (vm_objp(vmg_ ctx->obj)
2399 ->get_prop(vmg_ prop, &ov_val, ctx->obj, &src_obj, 0)
2400 && src_obj != self)
2401 {
2402 /*
2403 * we found the property, but it was defined in a different object
2404 * than the object whose properties we're enumerating - this must
2405 * be an inherited version of the property which the object
2406 * overrides, so we don't want to show it among the original
2407 * object's properties
2408 */
2409 return;
2410 }
2411
2412 /* get the name of this property */
2413 const char *prop_name = ctx->dbg->propid_to_sym(prop);
2414
2415 /*
2416 * if we couldn't get a name for the property, don't bother invoking
2417 * the UI callback, since it won't be able to use the property
2418 * anyway
2419 */
2420 if (prop_name == 0)
2421 return;
2422
2423 /* ignore methods and properties containing self-printing strings */
2424 switch(val->typ)
2425 {
2426 case VM_NIL:
2427 case VM_TRUE:
2428 case VM_OBJ:
2429 case VM_PROP:
2430 case VM_INT:
2431 case VM_ENUM:
2432 case VM_SSTRING:
2433 case VM_LIST:
2434 case VM_FUNCPTR:
2435 case VM_BIFPTR:
2436 /*
2437 * these types are all valid for enumeration - invoke the UI
2438 * callback
2439 */
2440 (*ctx->ui_cb)(ctx->ui_cb_ctx, prop_name, strlen(prop_name), ".");
2441 break;
2442
2443 default:
2444 /* do not enumerate any other types */
2445 break;
2446 }
2447 }
2448
2449 /* ------------------------------------------------------------------------ */
2450 /*
2451 * Evaluate an expression
2452 */
2453 int CVmDebug::eval_expr(VMG_ char *result, size_t resultlen,
2454 const char *expr,
2455 int level, int *is_lval, int *is_openable,
2456 void (*aggcb)(void *, const char *,
2457 int, const char *),
2458 void *aggctx, int speculative)
2459 {
2460 vm_obj_id_t code_obj;
2461 vm_val_t expr_val;
2462 CVmDbgSymtab local_symtab;
2463 vmdbg_step_save_t old_step;
2464 vm_obj_id_t self_obj;
2465 vm_obj_id_t orig_target_obj;
2466 vm_obj_id_t defining_obj;
2467 vm_prop_id_t target_prop;
2468 vmrun_save_ctx run_ctx;
2469
2470 /* presume it won't be an lvalue or openable */
2471 if (is_lval != 0)
2472 *is_lval = FALSE;
2473 if (is_openable != 0)
2474 *is_openable = FALSE;
2475
2476 /* we don't have a result string yet */
2477 if (result != 0 && resultlen > 0)
2478 *result = '\0';
2479
2480 /* get information on the indicated stack level */
2481 {
2482 CVmFuncPtr func_ptr;
2483 CVmDbgLinePtr line_ptr;
2484 CVmDbgTablePtr dbg_ptr;
2485 const uchar *stm_start, *stm_end;
2486
2487 if (get_stack_level_info(vmg_ level, &func_ptr, &line_ptr,
2488 &stm_start, &stm_end)
2489 || !func_ptr.set_dbg_ptr(&dbg_ptr))
2490 return 1;
2491
2492 /* set up our local symbol table interface for this context */
2493 local_symtab.init(vmg_ &dbg_ptr, line_ptr.get_frame_id(), level);
2494 }
2495
2496 /* get the 'self' object for the selected level */
2497 self_obj = G_interpreter->get_self_at_level(vmg_ level);
2498
2499 /* get the target property for the level */
2500 target_prop = G_interpreter->get_target_prop_at_level(vmg_ level);
2501
2502 /* get the original target object and defining object at the level */
2503 orig_target_obj = G_interpreter->get_orig_target_obj_at_level(vmg_ level);
2504 defining_obj = G_interpreter->get_defining_obj_at_level(vmg_ level);
2505
2506 /* compile the expression */
2507 CVmDynCompResults comp_results;
2508 compile_expr(vmg_ expr, level, &local_symtab,
2509 self_obj != VM_INVALID_OBJ
2510 && target_prop != VM_INVALID_PROP,
2511 speculative, is_lval, &code_obj, &comp_results);
2512
2513 /* if we got a compilation error, return failure */
2514 if (comp_results.err != 0)
2515 {
2516 /* copy the result message, if desired */
2517 if (comp_results.msgbuf != 0)
2518 {
2519 /* copy the message to the caller's buffer (if there is one) */
2520 if (result != 0)
2521 lib_strcpy(result, resultlen, comp_results.msgbuf);
2522 }
2523
2524 /* return the error */
2525 return comp_results.err;
2526 }
2527
2528 /* note the pre-call stack depth */
2529 G_interpreter->save_context(vmg_ &run_ctx);
2530
2531 /* set up for the recursive execution */
2532 prepare_for_eval(&old_step);
2533
2534 /* push the code object onto the stack for gc protection */
2535 G_stk->push()->set_obj(code_obj);
2536
2537 /* execute the code in a protected block */
2538 int err = 0;
2539 err_try
2540 {
2541 /* set up the invocation frame */
2542 vm_val_t *fp = G_stk->push(5);
2543 (fp++)->set_propid(target_prop);
2544 (fp++)->set_obj_or_nil(orig_target_obj);
2545 (fp++)->set_obj_or_nil(defining_obj);
2546 (fp++)->set_obj_or_nil(self_obj);
2547 (fp++)->set_obj(code_obj);
2548
2549 /* execute the code in a recursive call to the VM */
2550 vm_rcdesc rc("dbg eval");
2551 G_interpreter->call_func_ptr_fr(vmg_ G_stk->get(0), 0, &rc, 0);
2552 }
2553 err_catch(exc)
2554 {
2555 /* note the error */
2556 err = exc->get_error_code();
2557
2558 /*
2559 * if it's "unhandled exception," get the message from the
2560 * exception object itself; otherwise, format the message for
2561 * the error
2562 */
2563 if (err == VMERR_UNHANDLED_EXC)
2564 {
2565 /* get the message from the exception */
2566 size_t msg_len;
2567 const char *msg = CVmRun::get_exc_message(vmg_ exc, &msg_len);
2568 if (msg == 0)
2569 msg = "Unhandled program exception";
2570
2571 /* limit the size to the caller's buffer size */
2572 t3sprintf(result, resultlen, "error: %.*s", (int)msg_len, msg);
2573 }
2574 else
2575 {
2576 /* format the VM error message into the caller's result buffer */
2577 const char *msg = err_get_msg(
2578 vm_messages, vm_message_count, err, FALSE);
2579 char *msgf = err_format_msg(msg, exc);
2580
2581 /* format the full copy for the caller */
2582 t3sprintf(result, resultlen, "error: %s", msgf);
2583
2584 /* free the base message */
2585 t3free(msgf);
2586 }
2587 }
2588 err_end;
2589
2590 /* discard the code object */
2591 G_stk->discard();
2592
2593 /* restore the original execution mode */
2594 restore_from_eval(&old_step);
2595
2596 /* restore the interpreter context */
2597 G_interpreter->restore_context(vmg_ &run_ctx);
2598
2599 /* get the return value, if any */
2600 if (err == 0)
2601 {
2602 /* get the value from R0 */
2603 expr_val = *G_interpreter->get_r0();
2604
2605 /* leave the value on the stack for gc protection */
2606 G_stk->push(&expr_val);
2607
2608 /* format the value into an allocated buffer */
2609 format_val(vmg_ result, resultlen, &expr_val);
2610 }
2611 else
2612 {
2613 /* return failure */
2614 return 1;
2615 }
2616
2617 /* if an aggregation callback is provided, use it */
2618 if (aggcb != 0)
2619 {
2620 size_t cnt;
2621 size_t i;
2622
2623 /* determine if the object has contents to be iterated */
2624 switch(expr_val.typ)
2625 {
2626 case VM_LIST:
2627 agg_list:
2628 /* get the element count */
2629 cnt = expr_val.ll_length(vmg0_);
2630
2631 /* iterate through the elements */
2632 for (i = 1 ; i <= cnt ; ++i)
2633 {
2634 /* format an index operator for this index */
2635 char idxbuf[30];
2636 sprintf(idxbuf, "[%u]", (unsigned int)i);
2637
2638 /* invoke the callback with this subitem */
2639 (*aggcb)(aggctx, idxbuf, strlen(idxbuf), "");
2640 }
2641 break;
2642
2643 case VM_OBJ:
2644 /* if it's a list object, use the list contents */
2645 if (vm_objp(vmg_ expr_val.val.obj)->get_as_list() != 0
2646 || (CVmObjVector::is_vector_obj(vmg_ expr_val.val.obj)
2647 && !CVmObjAnonFn::is_anonfn_obj(vmg_ expr_val.val.obj)))
2648 goto agg_list;
2649
2650 /* set up our callback context in case we need it */
2651 eval_enum_cb_ctx cb_ctx;
2652 cb_ctx.ui_cb = aggcb;
2653 cb_ctx.ui_cb_ctx = aggctx;
2654 cb_ctx.dbg = this;
2655 cb_ctx.obj = expr_val.val.obj;
2656
2657 /* handle lookup tables specially */
2658 if (CVmObjLookupTable::is_lookup_table_obj(vmg_ expr_val.val.obj))
2659 {
2660 /* get the lookup table object, properly cast */
2661 CVmObjLookupTable *lt =
2662 (CVmObjLookupTable *)vm_objp(vmg_ expr_val.val.obj);
2663
2664 /* enumerate the table's keys */
2665 lt->for_each(vmg_ enum_lookup_keys_cb, &cb_ctx);
2666
2667 /* if it has a default value, include it in the listing */
2668 vm_val_t defval;
2669 lt->get_default_val(&defval);
2670 if (defval.typ != VM_NIL)
2671 (*aggcb)(aggctx, "*", 3, "!.getDefaultValue()");
2672
2673 /* done */
2674 break;
2675 }
2676
2677 /* determine if the object provides a property list */
2678 if (vm_objp(vmg_ expr_val.val.obj)->provides_props(vmg0_))
2679 {
2680 /* enumerate the properties */
2681 vm_objp(vmg_ expr_val.val.obj)
2682 ->enum_props(vmg_ expr_val.val.obj, &enum_props_eval_cb,
2683 &cb_ctx);
2684 }
2685 break;
2686
2687 default:
2688 /* other types don't have sub-parts to iterate */
2689 break;
2690 }
2691 }
2692
2693 /* check the openable status if the caller wants to know */
2694 if (is_openable != 0)
2695 {
2696 /*
2697 * check to see if it's openable - it is if it's a TADS object
2698 * or a list
2699 */
2700 switch(expr_val.typ)
2701 {
2702 case VM_LIST:
2703 /* lists are always openable */
2704 *is_openable = TRUE;
2705 break;
2706
2707 case VM_OBJ:
2708 /*
2709 * object - if it's a list or TADS object, it's openable;
2710 * otherwise it's not
2711 */
2712 if (vm_objp(vmg_ expr_val.val.obj)->get_as_list() != 0
2713 || (CVmObjVector::is_vector_obj(vmg_ expr_val.val.obj)
2714 && !CVmObjAnonFn::is_anonfn_obj(vmg_ expr_val.val.obj))
2715 || CVmObjLookupTable::is_lookup_table_obj(vmg_ expr_val.val.obj)
2716 || vm_objp(vmg_ expr_val.val.obj)->provides_props(vmg0_))
2717 *is_openable = TRUE;
2718 else
2719 *is_openable = FALSE;
2720 break;
2721
2722 default:
2723 /* other types aren't openable */
2724 *is_openable = FALSE;
2725 break;
2726 }
2727 }
2728
2729 /* discard the gc protection */
2730 G_stk->discard();
2731
2732 /* success */
2733 return 0;
2734 }
2735
2736
2737 /*
2738 * Compile an expression
2739 */
2740 void CVmDebug::compile_expr(VMG_ const char *expr,
2741 int level, CVmDbgSymtab *local_symtab,
2742 int self_valid, int speculative,
2743 int *is_lval, vm_obj_id_t *code_obj,
2744 CVmDynCompResults *results)
2745 {
2746 /* set up our compilation mode descriptors */
2747 tcpn_dyncomp_info adjust_info(TRUE, speculative, level);
2748 CVmDynCompDebug dbg_info(local_symtab, adjust_info, self_valid);
2749
2750 /* compile the code via the dynamic compiler */
2751 *code_obj = CVmDynamicCompiler::get(vmg0_)->compile(
2752 vmg_ FALSE, VM_INVALID_OBJ, VM_INVALID_OBJ, VM_INVALID_OBJ, 0,
2753 expr, strlen(expr), DCModeExpression, &dbg_info, results);
2754
2755 /* exclude debugger objects from save/restore/undo */
2756 if (*code_obj != VM_INVALID_OBJ)
2757 G_obj_table->set_obj_transient(*code_obj);
2758
2759 /* pass the lvalue information back to the caller */
2760 if (is_lval != 0)
2761 *is_lval = dbg_info.is_lval;
2762 }
2763
2764 /* ------------------------------------------------------------------------ */
2765 /*
2766 * Set step-over mode
2767 */
2768 void CVmDebug::set_step_over(VMG0_)
2769 {
2770 /* set the mode flags */
2771 single_step_ = TRUE;
2772 step_in_ = FALSE;
2773 step_out_ = FALSE;
2774
2775 /* we're in run mode again */
2776 break_event_->reset();
2777
2778 /*
2779 * note the current stack frame depth - we won't stop until the
2780 * stack frame is back at this same depth, or at an enclosing level
2781 */
2782 step_frame_depth_ = G_interpreter->get_frame_depth(vmg0_);
2783 }
2784
2785 /*
2786 * Set step-out mode
2787 */
2788 void CVmDebug::set_step_out(VMG0_)
2789 {
2790 /* set the mode flags for stepping over source lines */
2791 single_step_ = TRUE;
2792 step_in_ = FALSE;
2793 step_out_ = TRUE;
2794
2795 /* we're in run mode again */
2796 break_event_->reset();
2797
2798 /*
2799 * Keep stepping until we're at an enclosing stack frame level - to
2800 * do this, note the current frame depth, but decrement it, since we
2801 * don't want to stop again until the frame depth is lower than it
2802 * is now.
2803 */
2804 step_frame_depth_ = G_interpreter->get_frame_depth(vmg0_) - 1;
2805 }
2806
2807
2808 /* ------------------------------------------------------------------------ */
2809 /*
2810 * Note a RETURN for single-step purposes
2811 */
2812 void CVmDebug::step_return(VMG0_)
2813 {
2814 /*
2815 * Note the frame depth for single-stepping purposes. If we're in STEP
2816 * OVER mode, and we're now in a frame where we can once again stop for
2817 * stepping, clear the step-over flag. Don't actually stop now; that
2818 * can wait until the next steppable instruction. But do note that we
2819 * *should* stop at the next steppable instruction.
2820 *
2821 * This check usually isn't required, since in most cases we'd match
2822 * the frame-depth criterion anyway at the next instruction in the
2823 * caller. However, if the caller isn't steppable (e.g., it's native
2824 * code), and the caller turns around and calls another routine, we
2825 * could incorrectly think that the new routine was being called from
2826 * the routine that's returning. This can happen if the new routine
2827 * has more arguments than the old one, or the caller pushes some
2828 * temporary state onto the stack. In either case, the frame depth in
2829 * the new routine will look deeper than it did in the old routine, so
2830 * we'll think it's a callee of the old routine rather than a peer. By
2831 * explicitly tracing through the return, we ensure that we realize
2832 * that we should resume stepping in the new routine.
2833 */
2834 if (single_step_ && !step_in_
2835 && G_interpreter->get_frame_depth(vmg0_) < step_frame_depth_)
2836 {
2837 /* we're in a caller - stop at the next steppable instruction */
2838 if (step_over_bp_)
2839 orig_step_in_ = TRUE;
2840 else
2841 step_in_ = TRUE;
2842
2843 /* we're in run mode again */
2844 break_event_->reset();
2845 }
2846 }
2847
2848
2849 /* ------------------------------------------------------------------------ */
2850 /*
2851 * Synchronize our internal memory of the last execution point
2852 */
2853 void CVmDebug::sync_exec_pos(VMG_ const uchar *pc_ptr, const uchar *entry)
2854 {
2855 CVmFuncPtr func_ptr;
2856 CVmDbgLinePtr line_ptr;
2857 const uchar *stm_start, *stm_end;
2858
2859 /*
2860 * if we're already in the debugger, don't change anything - this must
2861 * be a recursive invocation due to expression evaluation
2862 */
2863 if (in_debugger_)
2864 return;
2865
2866 /* set up a pointer to the current function's header */
2867 G_interpreter->set_current_func_ptr(vmg_ &func_ptr);
2868
2869 /* get the byte-code offset of this instruction */
2870 pc_ = pc_ptr;
2871 entry_ = entry;
2872
2873 /* get the boundaries of the current source code statement */
2874 if (CVmRun::get_stm_bounds(vmg_ &func_ptr, pc_ - entry_, &line_ptr,
2875 &stm_start, &stm_end))
2876 {
2877 /* remember the current function pointer */
2878 func_ptr_.copy_from(&func_ptr);
2879
2880 /* remember the new line pointer */
2881 cur_stm_line_.copy_from(&line_ptr);
2882
2883 /* remember the method start offset */
2884 entry_ = entry;
2885
2886 /* remember the statement bounds */
2887 cur_stm_start_ = stm_start;
2888 cur_stm_end_ = stm_end;
2889
2890 /* ask the function pointer to set up the debug table pointer */
2891 dbg_ptr_valid_ = func_ptr_.set_dbg_ptr(&dbg_ptr_);
2892 }
2893 else
2894 {
2895 /* the statement has no valid bounds; clear the debug info */
2896 entry_ = 0;
2897 cur_stm_start_ = 0;
2898 cur_stm_end_ = 0;
2899 }
2900 }
2901
2902 /* ------------------------------------------------------------------------ */
2903 /*
2904 * Single-step interrupt
2905 */
2906 void CVmDebug::step(VMG_ const uchar **pc_ptr, const uchar *entry,
2907 int hit_bp, int error_code)
2908 {
2909 int line_ptr_valid;
2910 CVmFuncPtr func_ptr;
2911 CVmDbgLinePtr line_ptr;
2912 int bpnum;
2913 int hit_global_bp;
2914 int stop_for_step;
2915 const uchar *stm_start, *stm_end;
2916 int trace_over_bp;
2917 vm_val_t orig_r0;
2918 int released_stack_reserve = FALSE;
2919
2920 /*
2921 * If we're stepping over a breakpoint, put the breakpoint back into
2922 * effect and restore the actual execution mode
2923 */
2924 if (step_over_bp_)
2925 {
2926 /* no longer stepping over a breakpoint */
2927 step_over_bp_ = FALSE;
2928
2929 /* restore the original execution mode */
2930 single_step_ = orig_single_step_;
2931 step_in_ = orig_step_in_;
2932 step_out_ = orig_step_out_;
2933
2934 /* restore the breakpoint */
2935 step_over_bp_bp_->set_bp_instr(vmg_ TRUE, TRUE);
2936 }
2937
2938 /* if we're already in the debugger, don't re-enter */
2939 if (in_debugger_)
2940 return;
2941
2942 /* note that we're in the debugger so that we don't recurse into here */
2943 in_debugger_ = TRUE;
2944
2945 /*
2946 * if this is a stack overflow error, release the debugger reserve, so
2947 * that we can look at expressions and otherwise go about our business
2948 * without encountering additional stack overflows
2949 */
2950 if (error_code == VMERR_STACK_OVERFLOW)
2951 released_stack_reserve = G_stk->release_reserve();
2952
2953 /*
2954 * Remember the value of R0, so we can restore it when we leave; also
2955 * push it onto the stack, so that it's protected against any garbage
2956 * collection that occurs while the debugger has control.
2957 */
2958 orig_r0 = *G_interpreter->get_r0();
2959 G_stk->push(&orig_r0);
2960
2961 /* presume we will not trace over a breakpoint at exit */
2962 trace_over_bp = FALSE;
2963
2964 /* get the byte-code offset of this instruction */
2965 pc_ = (pc_ptr != 0 ? *pc_ptr : 0);
2966 entry_ = entry;
2967
2968 /*
2969 * If we're in single-step mode, assume we'll stop for stepping. If
2970 * we're stopping for an error, also set single-step mode for now.
2971 */
2972 stop_for_step = single_step_ || (error_code != 0);
2973
2974 /* if we hit a breakpoint, find it */
2975 if (hit_bp)
2976 {
2977 CVmDebugBp *bp;
2978
2979 /* find the breakpoint */
2980 bp = find_bp(pc_);
2981
2982 /* get the breakpoint number */
2983 bpnum = (bp == 0 ? 0 : (int)(bp - bp_) + 1);
2984
2985 /*
2986 * If the breakpoint has a condition attached to it, stop only
2987 * if the condition is true.
2988 */
2989 if (bp != 0 && bp->has_condition() && !bp->eval_cond(vmg0_))
2990 {
2991 /*
2992 * The condition is false - this means we must ignore the
2993 * breakpoint; forget that we hit the breakpoint. (We can't
2994 * just decide to keep running now because there are
2995 * numerous other reasons we might want to stop now, and we
2996 * must consider the other possibilities before we jump back
2997 * into the program.)
2998 */
2999 hit_bp = FALSE;
3000
3001 /* we don't have a valid breakpoint number after all */
3002 bpnum = 0;
3003
3004 /*
3005 * Since we're skipping this breakpoint, we need to trace
3006 * over it to continue running.
3007 */
3008 trace_over_bp = TRUE;
3009 }
3010 }
3011 else
3012 {
3013 /* we don't have a valid breakpoint */
3014 bpnum = 0;
3015 }
3016
3017 /* presume we won't hit a global breakpoint */
3018 hit_global_bp = FALSE;
3019
3020 /*
3021 * If there are any global breakpoints, and we didn't hit a code
3022 * breakpoint, check for a hit. Don't bother with this if we've hit
3023 * a real breakpoint, since we'll certainly stop in that case.
3024 * Also, don't bother checking global breakpoints if we're in native
3025 * code (i.e., pc_ptr == 0), since we can't stop right now if we
3026 * are.
3027 */
3028 if (!hit_bp && pc_ptr != 0 && global_bp_cnt_ != 0)
3029 {
3030 CVmDebugBp *bp;
3031 size_t i;
3032
3033 /* scan all of the breakpoints and look for a global breakpoint */
3034 for (i = 0, bp = bp_ ; i < VMDBG_BP_MAX ; ++i, ++bp)
3035 {
3036 /*
3037 * if this breakpoint is enabled and global, evaluate its
3038 * condition
3039 */
3040 if (bp->is_in_use() && !bp->is_disabled() && bp->is_global())
3041 {
3042 /* evaluate the condition */
3043 if (bp->eval_cond(vmg0_))
3044 {
3045 /*
3046 * we've hit a global breakpoint - note it, and
3047 * compute the breakpoint number (it's just the
3048 * breakpoint index adjusted to a 1-based index)
3049 */
3050 hit_global_bp = TRUE;
3051 bpnum = (int)(i + 1);
3052
3053 /*
3054 * If this is a stop-when-true condition, automatically
3055 * disable the breakpoint. Once a global
3056 * stop-when-true breakpoint hits, its condition will
3057 * probably remain true for some time, and it would be
3058 * pointless to have it keep hitting over and over
3059 * again now. Note that this isn't necessary for
3060 * stop-on-change breakpoints, as the fact that the
3061 * value just changed doesn't tell us anything about
3062 * the likelihood of future changes.
3063 */
3064 if (!bp->stop_on_change())
3065 bp->set_disabled(TRUE);
3066
3067 /*
3068 * there's no need to look further - we only need to
3069 * have one global breakpoint hit in order to stop
3070 */
3071 break;
3072 }
3073 }
3074 }
3075 }
3076
3077 /*
3078 * Check to see if we're within the same source code statement that
3079 * we were in the last time we were in this routine. If so, we
3080 * already have most of the information we need about the execution
3081 * location, so we can avoid a little set-up work. If we're not in
3082 * the same statement any more, we have to figure out where we are.
3083 * Note that we will also break if we're at a different stack level
3084 * than we were last time, because in this case we're stepping out
3085 * of a recursive invocation.
3086 */
3087 if (stop_for_step
3088 && pc_ >= cur_stm_start_ && pc_ < cur_stm_end_
3089 && G_interpreter->get_frame_depth(vmg0_) == step_frame_depth_)
3090 {
3091 /*
3092 * No matter what source-stepping mode we're in, we never stop
3093 * twice consecutively at the same source statement. Make a
3094 * note that we don't need to stop here. However, don't just
3095 * return now, since we might need to stop for a breakpoint or a
3096 * global breakpoint.
3097 */
3098 stop_for_step = FALSE;
3099 }
3100
3101 /*
3102 * If we're in step-over mode, there's no need to stop if we're
3103 * executing at any stack level nested within the original stack frame.
3104 * If this is the case, we can simply return now. Of course, if we're
3105 * stopping because we hit an error, don't worry about our step mode.
3106 */
3107 if (stop_for_step
3108 && error_code == 0
3109 && single_step_ && !step_in_
3110 && G_interpreter->get_frame_depth(vmg0_) > step_frame_depth_)
3111 {
3112 /* we don't need to stop for stepping */
3113 stop_for_step = FALSE;
3114 }
3115
3116 /* figure out where we're tracing, if we're in byte code */
3117 if (pc_ptr != 0)
3118 {
3119 /* set up a pointer to the current function's header */
3120 G_interpreter->set_current_func_ptr(vmg_ &func_ptr);
3121
3122 /* get the boundaries of the current source code statement */
3123 line_ptr_valid =
3124 CVmRun::get_stm_bounds(vmg_ &func_ptr, pc_ - entry_, &line_ptr,
3125 &stm_start, &stm_end);
3126 }
3127 else
3128 {
3129 /* we're in native code, so there is no valid byte code location */
3130 line_ptr_valid = FALSE;
3131 stm_start = stm_end = 0;
3132 }
3133
3134 /*
3135 * If we're within the same source line and at the same stack depth
3136 * that we were at last time - even if the apparent byte-code location
3137 * has us in a different line - don't stop for a single-step
3138 * operation. The same source line can generate byte code at
3139 * different places in certain types of complex statements, and we
3140 * don't want to confuse the user by stopping twice in a row at the
3141 * same line (thus showing no apparent change in the execution
3142 * location) in such cases.
3143 *
3144 * However, if we're stopping due to a run-time error, do stop even if
3145 * we're in the same location - it just means we got another error at
3146 * the same place.
3147 */
3148 if (stop_for_step
3149 && error_code == 0
3150 && G_interpreter->get_frame_depth(vmg0_) == step_frame_depth_
3151 && line_ptr_valid
3152 && cur_stm_start_ != 0
3153 && line_ptr.get_source_id() == cur_stm_line_.get_source_id()
3154 && line_ptr.get_source_line() == cur_stm_line_.get_source_line())
3155 {
3156 /* note that we don't want to stop for single-step mode */
3157 stop_for_step = FALSE;
3158 }
3159
3160 /*
3161 * If we're in native code and we decided that we should stop,
3162 * remember the current code location as the last stop location.
3163 * Even though we can't actually stop here, we want to act as though
3164 * we did, so that next time we step through actual byte code we'll
3165 * notice that we are at a different location than we were for the
3166 * last stop.
3167 */
3168 if (pc_ptr == 0 && stop_for_step)
3169 {
3170 /*
3171 * set the current statement bounds to invalid, to ensure that
3172 * we will notice we are in a different location for the next
3173 * valid byte code location
3174 */
3175 cur_stm_start_ = cur_stm_end_ = 0;
3176 }
3177
3178 /*
3179 * If we're stopping for any reason (single-stepping, breakpoint, or
3180 * global breakpoint), enter the interactive debugger user
3181 * interface. In any case, only stop if we have a valid line
3182 * pointer, since we can't do anything at a location without a valid
3183 * source line.
3184 *
3185 * Note that we can never take control when we're stepping through
3186 * native code (i.e., pc_ptr == 0). We're called for native code
3187 * execution only to advise us of the change in stack levels so that
3188 * we can properly track step in/over/out modes properly through
3189 * native code traversal.
3190 */
3191 if (line_ptr_valid && (stop_for_step || hit_bp || hit_global_bp))
3192 {
3193 /* remember the current function pointer */
3194 func_ptr_.copy_from(&func_ptr);
3195
3196 /* remember the current frame depth */
3197 step_frame_depth_ = G_interpreter->get_frame_depth(vmg0_);
3198
3199 /* remember the new line pointer */
3200 cur_stm_line_.copy_from(&line_ptr);
3201
3202 /* remember the method start offset */
3203 entry_ = entry;
3204
3205 /* remember the statement bounds */
3206 cur_stm_start_ = stm_start;
3207 cur_stm_end_ = stm_end;
3208
3209 /* ask the function pointer to set up the debug table pointer */
3210 dbg_ptr_valid_ = func_ptr_.set_dbg_ptr(&dbg_ptr_);
3211
3212 /*
3213 * remove all breakpoints from the code while we have control,
3214 * so that the code is all the original instructions while the
3215 * user is looking at it
3216 */
3217 suspend_all_bps(vmg0_);
3218
3219 /*
3220 * call the UI interactive command loop - this won't return
3221 * until the user tells us to resume execution
3222 */
3223 const uchar *pc = pc_;
3224 CVmDebugUI::cmd_loop(vmg_ bpnum, error_code, &pc);
3225
3226 /* restore breakpoints now that we're resuming execution */
3227 restore_all_bps(vmg0_);
3228
3229 /* update our execution pointer in case the UI moved it */
3230 pc_ = pc;
3231
3232 /* update the caller's execution pointer as well */
3233 *pc_ptr = pc;
3234
3235 /*
3236 * get the boundaries of the current statement, in case it
3237 * changed - we need this for the next time we enter the
3238 * debugger, so we can tell (if we're stepping) whether we've
3239 * left the most recent statement or not
3240 */
3241 if (!CVmRun::get_stm_bounds(vmg_ &func_ptr_, pc - entry,
3242 &cur_stm_line_,
3243 &cur_stm_start_, &cur_stm_end_))
3244 {
3245 /* we have no information for this statement */
3246 cur_stm_start_ = cur_stm_end_ = 0;
3247 }
3248
3249 /*
3250 * note that we must trace over any breakpoint at this new
3251 * location, since we want to execute the original instruction
3252 * rather than just stopping again
3253 */
3254 trace_over_bp = TRUE;
3255 }
3256
3257 /*
3258 * if we released the stack reserve, put it back in reserve now that
3259 * we're leaving the debugger - the reserve is for the debugger's use
3260 * only, so we don't need it once we resume execution
3261 */
3262 if (released_stack_reserve)
3263 G_stk->recover_reserve();
3264
3265 /* note that we're leaving the debugger */
3266 in_debugger_ = FALSE;
3267
3268 /*
3269 * If appropriate, check the current code location for a breakpoint;
3270 * if there's a breakpoint, we need to go through a couple of extra
3271 * steps to resume execution so that we don't jump right back into
3272 * the debugger.
3273 *
3274 * The problem is that if we go or step after hitting a breakpoint,
3275 * and we were to simply leave the breakpoint in effect, we'd just
3276 * hit the same breakpoint again. What we actually want to do is
3277 * execute the original instruction. To do this, we must first
3278 * remove the breakpoint, restoring the original instruction, then
3279 * single-step the instruction, then, when the debugger regains
3280 * control after the single-step, put the breakpoint back. Finally,
3281 * we can resume with whatever kind of stepping we were going to do
3282 * in the first place.
3283 */
3284 if (trace_over_bp && **pc_ptr == (uchar)OPC_BP)
3285 {
3286 CVmDebugBp *bp;
3287
3288 /* find the breakpoint */
3289 bp = find_bp(pc_);
3290
3291 /* if we found the breakpoint, remove it temporarily */
3292 if (bp != 0)
3293 {
3294 /* restore the original instruction */
3295 bp->set_bp_instr(vmg_ FALSE, TRUE);
3296
3297 /* note that we're in step-over-bp mode */
3298 step_over_bp_ = TRUE;
3299
3300 /* remember the breakpoint we're stepping over */
3301 step_over_bp_bp_ = bp;
3302
3303 /* remember the original execution mode */
3304 orig_single_step_ = single_step_;
3305 orig_step_in_ = step_in_;
3306 orig_step_out_ = step_out_;
3307
3308 /*
3309 * temporary set single-step mode, so that we get control
3310 * immediately after executing this single instruction
3311 */
3312 single_step_ = TRUE;
3313 step_in_ = TRUE;
3314 step_out_ = FALSE;
3315 }
3316 }
3317
3318 /*
3319 * restore the original value of R0, and discard the copy we pushed
3320 * onto the stack for protection against garbage collection
3321 */
3322 *G_interpreter->get_r0() = orig_r0;
3323 G_stk->discard();
3324 }
3325
3326 /* ------------------------------------------------------------------------ */
3327 /*
3328 * Suspend all breakpoints - removes all BP instructions from the code
3329 * and restores the original instructions
3330 */
3331 void CVmDebug::suspend_all_bps(VMG0_)
3332 {
3333 CVmDebugBp *bp;
3334 size_t i;
3335
3336 /* loop over all breakpoints */
3337 for (i = 0, bp = bp_ ; i < VMDBG_BP_MAX ; ++i, ++bp)
3338 {
3339 /*
3340 * if this breakpoint is active, restore its original
3341 * instruction (since this routine sets up for switching in and
3342 * out of the debugger, force the instruction update even if
3343 * we're in debugger mode)
3344 */
3345 if (bp->is_in_use() && !bp->is_disabled())
3346 bp->set_bp_instr(vmg_ FALSE, TRUE);
3347 }
3348 }
3349
3350 /*
3351 * Restore all breakpoints - restores all BP instructions removed by
3352 * suspend_all_bps()
3353 */
3354 void CVmDebug::restore_all_bps(VMG0_)
3355 {
3356 CVmDebugBp *bp;
3357 size_t i;
3358
3359 /* loop over all breakpoints */
3360 for (i = 0, bp = bp_ ; i < VMDBG_BP_MAX ; ++i, ++bp)
3361 {
3362 /* if this breakpoint is active, put it back */
3363 if (bp->is_in_use() && !bp->is_disabled())
3364 bp->set_bp_instr(vmg_ TRUE, TRUE);
3365 }
3366 }
3367
3368 /* ------------------------------------------------------------------------ */
3369 /*
3370 * Determine if a code location is within the current active method
3371 */
3372 int CVmDebug::is_in_current_method(VMG_ const uchar *code_addr)
3373 {
3374 /*
3375 * if the current debug pointer isn't valid, we can't make this
3376 * determination, so indicate that we're not in the active method
3377 */
3378 if (!dbg_ptr_valid_ || code_addr == 0)
3379 return FALSE;
3380
3381 /*
3382 * Determine the endpoint of the method's code. If the method has
3383 * an exception table, it's the endpoint; otherwise, the debug table
3384 * is the endpoint.
3385 */
3386 const uchar *end_addr = entry_
3387 + (func_ptr_.get_exc_ofs() != 0
3388 ? func_ptr_.get_exc_ofs()
3389 : func_ptr_.get_debug_ofs());
3390
3391 /*
3392 * if the code address is between the entrypoint address for the
3393 * active method and the ending address we just calculated, it's in
3394 * the active method; otherwise, it's somewhere else
3395 */
3396 return (code_addr >= entry_ && code_addr < end_addr);
3397 }
3398
3399
3400
3401 /* ------------------------------------------------------------------------ */
3402 /*
3403 * Set up a function pointer and line pointer, and get the statement
3404 * bounds, for the function at the given stack level. Level 0 is the
3405 * currently executing function, 1 is the first enclosing stack level
3406 * (which called the current function), and so on. Returns true if
3407 * successful, false if the information isn't available.
3408 */
3409 int CVmDebug::get_stack_level_info(VMG_ int level, CVmFuncPtr *func_ptr,
3410 CVmDbgLinePtr *line_ptr,
3411 const uchar **stm_start,
3412 const uchar **stm_end) const
3413 {
3414 /*
3415 * if we're at level 0, it's the current method; otherwise, get the
3416 * given enclosing frame from the stack
3417 */
3418 if (level == 0)
3419 {
3420 /*
3421 * if we don't have a valid debug information pointer, we can't
3422 * get any information on the current source location
3423 */
3424 if (!dbg_ptr_valid_)
3425 return 1;
3426
3427 /* use the current statement */
3428 func_ptr->copy_from(&func_ptr_);
3429 line_ptr->copy_from(&cur_stm_line_);
3430 *stm_start = cur_stm_start_;
3431 *stm_end = cur_stm_end_;
3432
3433 /* success */
3434 return 0;
3435 }
3436 else
3437 {
3438 vm_val_t *fp = G_interpreter->get_frame_ptr();
3439 const vm_rcdesc *rc = G_interpreter->get_rcdesc_from_frame(vmg_ fp);
3440 const uchar *retp = G_interpreter->get_return_addr_from_frame(vmg_ fp);
3441
3442 /*
3443 * Enclosing level - walk up the stack to the desired level.
3444 * Note that we are actually looking for the return address, so
3445 * we want to find the frame just within the desired level - for
3446 * the first enclosing level, we actually want the current
3447 * frame.
3448 */
3449 for ( ; level > 1 && fp != 0 ; --level)
3450 {
3451 /*
3452 * if there's a native caller context at this level, the level
3453 * counts twice, since the native caller counts as its own
3454 * stack level
3455 */
3456 if (rc != 0)
3457 {
3458 /* move to the recursive caller's return address */
3459 retp = rc->get_return_addr();
3460
3461 /* move up to the bytecode caller by clearing 'rc' */
3462 rc = 0;
3463
3464 /* count the level */
3465 if (--level <= 1)
3466 break;
3467 }
3468
3469 /* move up to the enclosing level */
3470 fp = G_interpreter->get_enclosing_frame_ptr(vmg_ fp);
3471 rc = G_interpreter->get_rcdesc_from_frame(vmg_ fp);
3472 retp = G_interpreter->get_return_addr_from_frame(vmg_ fp);
3473 }
3474
3475 /*
3476 * If we didn't reach the desired level, or the frame pointer is
3477 * null, we didn't find the requested frame - return failure.
3478 */
3479 if (level > 1 || fp == 0)
3480 return 1;
3481
3482 /* if we stopped at a native frame, there's no source location */
3483 if (rc != 0)
3484 return 1;
3485
3486 /* get the entry address from the frame */
3487 const uchar *entry =
3488 G_interpreter->get_enclosing_entry_ptr_from_frame(vmg_ fp);
3489
3490 /* set up a function pointer for the method */
3491 G_interpreter->set_return_funcptr_from_frame(vmg_ func_ptr, fp);
3492
3493 /*
3494 * Find the source line information for the return address in
3495 * the frame. Return failure if there's no debug information
3496 * for the method.
3497 */
3498 if (!CVmRun::get_stm_bounds(vmg_ func_ptr, retp - entry,
3499 line_ptr, stm_start, stm_end))
3500 return 1;
3501
3502 /* success */
3503 return 0;
3504 }
3505 }
3506
3507 /* ------------------------------------------------------------------------ */
3508 /*
3509 * Allocate the method header list
3510 */
3511 void CVmDebug::alloc_method_header_list(ulong cnt)
3512 {
3513 /*
3514 * if we already have a method header list, expand it; otherwise,
3515 * allocate a new one
3516 */
3517 if (method_hdr_ != 0)
3518 {
3519 /*
3520 * if we're growing the list, expand it; otherwise ignore the new
3521 * allocation
3522 */
3523 if (cnt > method_hdr_cnt_)
3524 {
3525 /* expand the list */
3526 method_hdr_ = (ulong *)t3realloc(method_hdr_,
3527 cnt * sizeof(method_hdr_[0]));
3528
3529 /* note the new size */
3530 method_hdr_cnt_ = cnt;
3531 }
3532 }
3533 else
3534 {
3535 /* allocate the new list */
3536 method_hdr_ = (ulong *)t3malloc(cnt * sizeof(method_hdr_[0]));
3537
3538 /* note the size */
3539 method_hdr_cnt_ = cnt;
3540 }
3541 }
3542
3543 /*
3544 * Given a code pool address, find the method header containing the
3545 * address. This searches the method header list for the nearest method
3546 * header whose address is less than the given address.
3547 */
3548 const uchar *CVmDebug::find_method_header(VMG_ const uchar *addr)
3549 {
3550 ulong lo;
3551 ulong hi;
3552 pool_ofs_t ofs;
3553
3554 /* if there are no method headers, there's nothing to find */
3555 if (method_hdr_cnt_ == 0)
3556 return 0;
3557
3558 /* if this isn't a valid pool address, we won't have an entry for it */
3559 if (!G_code_pool->get_ofs((const char *)addr, &ofs))
3560 return 0;
3561
3562 /* perform a binary search of the method header list */
3563 lo = 0;
3564 hi = method_hdr_cnt_ - 1;
3565 while (lo <= hi)
3566 {
3567 ulong cur;
3568 pool_ofs_t addr;
3569 pool_ofs_t next_addr;
3570
3571 /* split the difference to get the current entry */
3572 cur = lo + (hi - lo)/2;
3573
3574 /* get this entry's address */
3575 addr = (pool_ofs_t)get_method_header(cur);
3576
3577 /*
3578 * get the next entry's address - if this is the last entry, the
3579 * next entry's address is the highest possible address
3580 */
3581 next_addr = (cur + 1 >= method_hdr_cnt_
3582 ? (pool_ofs_t)0xffffffff
3583 : (pool_ofs_t)get_method_header(cur + 1));
3584
3585 /* check how this value compares */
3586 if (ofs >= next_addr)
3587 {
3588 /* we need to go higher */
3589 lo = (cur == lo ? cur + 1 : cur);
3590 }
3591 else if (ofs < addr)
3592 {
3593 /* we need to go lower */
3594 hi = (cur == hi ? hi - 1 : cur);
3595 }
3596 else
3597 {
3598 /* found it - return this start address */
3599 return (const uchar *)G_code_pool->get_ptr(addr);
3600 }
3601 }
3602
3603 /*
3604 * didn't find anything - we don't have any way to indicate this, so
3605 * just return the lowest code pool address
3606 */
3607 return 0;
3608 }
3609
3610
3611 /* ------------------------------------------------------------------------ */
3612 /*
3613 * Reverse-mapping hash entry
3614 */
3615
3616 /*
3617 * Construct. Note that we don't bother to convert the ulong key value
3618 * to a portable representation, since this entry is never written to a
3619 * file - it's purely an in-memory construct. Note also that we don't
3620 * care whether there are any embedded null bytes in the value, since
3621 * the hash entry base class works entirely with counted-length strings.
3622 *
3623 * Treating a ulong value as a character buffer might seem suspicious
3624 * from a portability standpoint, but the C/C++ standards make this
3625 * perfectly legitimate as long as we don't make assumptions about the
3626 * size or byte ordering of the underlying representation, which we're
3627 * not - the byte ordering is irrelevant, since we're simply treating
3628 * this value as an identifying string of bytes (i.e., a hash key), and
3629 * we're using sizeof() to ensure we use the correct local size for the
3630 * type. What we're doing is exactly the same thing that memcpy() would
3631 * do if asked to copy a ulong value, and equally safe.
3632 */
3633 CVmHashEntryDbgRev::CVmHashEntryDbgRev(ulong sym_val, const char *sym,
3634 size_t len)
3635 : CVmHashEntry((char *)&sym_val, sizeof(sym_val), TRUE)
3636 {
3637 /* make a copy of the symbol name */
3638 sym_ = lib_copy_str(sym, len);
3639 sym_len_ = len;
3640 }
3641
3642 /*
3643 * Destruct
3644 */
3645 CVmHashEntryDbgRev::~CVmHashEntryDbgRev()
3646 {
3647 /* delete our copy of the symbol name */
3648 lib_free_str(sym_);
3649 }
3650
3651 /*
3652 * check for a match
3653 */
3654 int CVmHashEntryDbgRev::matches(const char *str, size_t len) const
3655 {
3656 /*
3657 * it's a match if the strings are the same length and all
3658 * characters match, treating case as significant
3659 */
3660 return (len == len_
3661 && memcmp(str, str_, len * sizeof(*str)) == 0);
3662 }
3663
3664 /*
3665 * Reverse-lookup hash function
3666 */
3667 unsigned int CVmHashFuncDbgRev::compute_hash(const char *s, size_t l) const
3668 {
3669 uint acc;
3670
3671 /* add up all the byte values in the string */
3672 for (acc = 0 ; l != 0 ; ++s, --l)
3673 {
3674 uchar c;
3675
3676 c = (uchar)*s;
3677 acc += c;
3678 }
3679
3680 /* return the accumulated value */
3681 return acc;
3682 }
3683
3684 /* ------------------------------------------------------------------------ */
3685 /*
3686 * Breakpoint object
3687 */
3688
3689 /*
3690 * instantiate
3691 */
3692 CVmDebugBp::CVmDebugBp()
3693 {
3694 /* not yet in use */
3695 in_use_ = FALSE;
3696
3697 /* no code address yet */
3698 code_addr_ = 0;
3699
3700 /* not disabled */
3701 disabled_ = FALSE;
3702
3703 /* no original instruction yet */
3704 orig_instr_ = OPC_NOP;
3705
3706 /* no condition expression or byte code object yet */
3707 cond_ = 0;
3708 cond_buf_len_ = 0;
3709 compiled_cond_ = 0;
3710 has_cond_ = FALSE;
3711 stop_on_change_ = FALSE;
3712 prv_val_ = 0;
3713 }
3714
3715
3716 /*
3717 * delete
3718 */
3719 CVmDebugBp::~CVmDebugBp()
3720 {
3721 /* delete any condition text buffer */
3722 if (cond_ != 0)
3723 t3free(cond_);
3724 }
3725
3726 /*
3727 * terminate - this is called during VM termination so that we can
3728 * delete our compiled condition code objects (we can't do this in the
3729 * destructor because we need access to the VM globals)
3730 */
3731 void CVmDebugBp::do_terminate(VMG0_)
3732 {
3733 /* release our object table global variables */
3734 if (prv_val_ != 0)
3735 {
3736 G_obj_table->delete_global_var(prv_val_);
3737 prv_val_ = 0;
3738 }
3739 if (compiled_cond_ != 0)
3740 {
3741 G_obj_table->delete_global_var(compiled_cond_);
3742 compiled_cond_ = 0;
3743 }
3744 }
3745
3746 /*
3747 * Set the breakpoint's information
3748 */
3749 int CVmDebugBp::set_info(VMG_ const uchar *code_addr,
3750 const char *cond, int change,
3751 int disabled, char *errbuf, size_t errbuflen)
3752 {
3753 int err;
3754
3755 /* remember the code address */
3756 code_addr_ = code_addr;
3757
3758 /* remember the disabled status */
3759 disabled_ = disabled;
3760
3761 /* remember the expression */
3762 if ((err = set_condition(vmg_ cond, change, errbuf, errbuflen)) != 0)
3763 return err;
3764
3765 /* remember the original instruction at the code address */
3766 if (code_addr != 0)
3767 {
3768 /* get a writable pointer to the code location */
3769 uchar *code_ptr = (uchar *)code_addr;
3770
3771 /*
3772 * remember the original instruction at this address so that we can
3773 * restore it when we remove the BP instruction
3774 */
3775 orig_instr_ = *code_ptr;
3776
3777 /* set the BP instruction if appropriate */
3778 set_bp_instr(vmg_ TRUE, FALSE);
3779 }
3780
3781 /* success */
3782 return 0;
3783 }
3784
3785 /*
3786 * Set the condition text
3787 */
3788 int CVmDebugBp::set_condition(VMG_ const char *new_cond, int change,
3789 char *errbuf, size_t errbuflen)
3790 {
3791 /* delete any existing condition variable */
3792 if (compiled_cond_ != 0)
3793 {
3794 G_obj_table->delete_global_var(compiled_cond_);
3795 compiled_cond_ = 0;
3796 }
3797
3798 /*
3799 * check for an empty condition string - treat it as no condition if
3800 * it's all blanks
3801 */
3802 if (new_cond != 0)
3803 {
3804 const char *p;
3805
3806 /* scan for a non-blank character */
3807 for (p = new_cond ; t3_is_whitespace(*p) ; ++p) ;
3808
3809 /*
3810 * if we scanned the entire string without finding anything but
3811 * spaces, treat this is no condition
3812 */
3813 if (*p == '\0')
3814 new_cond = 0;
3815 }
3816
3817 /* if there's a new condition, set it up */
3818 if (new_cond != 0)
3819 {
3820 size_t new_cond_len;
3821 CVmDbgSymtab *symtab;
3822 CVmDbgSymtab local_symtab;
3823
3824 /* presume we won't find a local symbol table */
3825 symtab = 0;
3826
3827 /*
3828 * if there's a code address, set up a local symbol table to
3829 * compile the expression in the scope of the code location;
3830 * otherwise, compile with no local scope
3831 */
3832 if (code_addr_ != 0)
3833 {
3834 CVmFuncPtr func_ptr;
3835 CVmDbgLinePtr line_ptr;
3836 CVmDbgTablePtr dbg_ptr;
3837 const uchar *stm_start, *stm_end;
3838
3839 /* get the method header for the breakpoint location */
3840 func_ptr.set(G_debugger->find_method_header(vmg_ code_addr_));
3841
3842 /* set up the line pointer */
3843 if (func_ptr.get() != 0
3844 && CVmRun::get_stm_bounds(vmg_ &func_ptr,
3845 code_addr_ - func_ptr.get(),
3846 &line_ptr, &stm_start, &stm_end)
3847 && func_ptr.set_dbg_ptr(&dbg_ptr))
3848 {
3849 /*
3850 * We have a valid debug table for the code location -
3851 * set up a local symbol table object for the code.
3852 * Compile the expression for stack level zero, since we
3853 * will always evaluate the expression when this code is
3854 * active.
3855 */
3856 local_symtab.init(vmg_ &dbg_ptr, line_ptr.get_frame_id(), 0);
3857
3858 /* we now have a local symbol table for the code - use it */
3859 symtab = &local_symtab;
3860 }
3861 }
3862
3863 /*
3864 * Compile the expression. Compile at stack level zero, since
3865 * we'll always execute this code immediately upon hitting the
3866 * breakpoint, which means that the breakpoint location will be
3867 * the active stack level whenever this code is executed.
3868 *
3869 * Presume that 'self' will be valid in this context. If it
3870 * isn't, 'self' will be nil at run-time, so no harm will be done.
3871 */
3872 CVmDynCompResults results;
3873 vm_obj_id_t co;
3874 G_debugger->compile_expr(vmg_ new_cond, 0, symtab, TRUE,
3875 FALSE, 0, &co, &results);
3876
3877 /* if an error occurred, return the error code */
3878 if (results.err != 0)
3879 {
3880 if (results.msgbuf != 0)
3881 lib_strcpy(errbuf, errbuflen, results.msgbuf);
3882
3883 return results.err;
3884 }
3885
3886 /* create a global variable to hold the condition object reference */
3887 compiled_cond_ = G_obj_table->create_global_var();
3888 compiled_cond_->val.set_obj(co);
3889
3890 /* note that we have a condition */
3891 has_cond_ = TRUE;
3892
3893 /* note whether or not it's a stop-on-change condition */
3894 stop_on_change_ = change;
3895
3896 /* set the size of the new condition, including the trailing null */
3897 new_cond_len = strlen(new_cond) + 1;
3898
3899 /* allocate or expand the condition buffer if required */
3900 if (cond_buf_len_ < new_cond_len)
3901 {
3902 /* allocate or reallocate the buffer */
3903 if (cond_ == 0)
3904 cond_ = (char *)t3malloc(new_cond_len);
3905 else
3906 cond_ = (char *)t3realloc(cond_, new_cond_len);
3907
3908 /* note the new size of our condition buffer */
3909 cond_buf_len_ = new_cond_len;
3910 }
3911
3912 /* copy the new condition text into our buffer */
3913 memcpy(cond_, new_cond, new_cond_len);
3914
3915 /* if it's a stop-on-change condition, initialize the value */
3916 if (change)
3917 {
3918 /*
3919 * Allocate an object table global variable to hold the
3920 * previous value, if we haven't already. We allocate a global
3921 * for this so that our value is tracked in the garbage
3922 * collector.
3923 */
3924 if (prv_val_ == 0)
3925 prv_val_ = G_obj_table->create_global_var();
3926
3927 /*
3928 * set the value to 'empty' to indicate that we haven't
3929 * evaluated our expression for the first time yet
3930 */
3931 prv_val_->val.set_empty();
3932
3933 /*
3934 * evaluate the condition to initialize our memory of the
3935 * current value; we'll break the next time we reach this
3936 * breakpoint and the new value of the expression differs from
3937 * this saved value
3938 */
3939 eval_cond(vmg0_);
3940 }
3941 }
3942 else
3943 {
3944 /* note that there's no condition */
3945 has_cond_ = FALSE;
3946
3947 /* if we had a global variable for the condition, delete it */
3948 if (prv_val_ != 0)
3949 {
3950 G_obj_table->delete_global_var(prv_val_);
3951 prv_val_ = 0;
3952 }
3953 }
3954
3955 /* success */
3956 return 0;
3957 }
3958
3959 /*
3960 * Evaluate my condition
3961 */
3962 int CVmDebugBp::eval_cond(VMG0_)
3963 {
3964 int ret;
3965 vmrun_save_ctx run_ctx;
3966 vmdbg_step_save_t old_step;
3967
3968 /* save the interpreter context */
3969 G_interpreter->save_context(vmg_ &run_ctx);
3970
3971 /* set the debugger to non-stepping mode */
3972 G_debugger->prepare_for_eval(&old_step);
3973
3974 /*
3975 * if there's no condition, just return false, since there's nothing
3976 * to evaluate
3977 */
3978 if (compiled_cond_ == 0)
3979 return FALSE;
3980
3981 /* execute the code in a protected block */
3982 err_try
3983 {
3984 vm_obj_id_t self_obj;
3985 vm_obj_id_t orig_target_obj;
3986 vm_obj_id_t defining_obj;
3987 vm_prop_id_t target_prop;
3988 vm_val_t *valp;
3989 vm_val_t *fp;
3990
3991 /* get the 'self' object at the current stack level */
3992 self_obj = G_interpreter->get_self_at_level(vmg_ 0);
3993
3994 /* get the target property for the level */
3995 target_prop = G_interpreter->get_target_prop_at_level(vmg_ 0);
3996
3997 /* get the original target object and defining object at the level */
3998 orig_target_obj =
3999 G_interpreter->get_orig_target_obj_at_level(vmg_ 0);
4000 defining_obj = G_interpreter->get_defining_obj_at_level(vmg_ 0);
4001
4002 /* set up the frame context */
4003 fp = G_stk->push(5);
4004 (fp++)->set_propid(target_prop);
4005 (fp++)->set_obj_or_nil(orig_target_obj);
4006 (fp++)->set_obj_or_nil(defining_obj);
4007 (fp++)->set_obj_or_nil(self_obj);
4008 *(fp++) = compiled_cond_->val;
4009
4010 /* execute the code */
4011 vm_rcdesc rc("dbg cond");
4012 G_interpreter->call_func_ptr(vmg_ &compiled_cond_->val, 0, &rc, 0);
4013
4014 /* get the return value */
4015 valp = G_interpreter->get_r0();
4016
4017 /* check what kind of condition we have */
4018 if (stop_on_change_)
4019 {
4020 /*
4021 * We're a stop-on-change condition - this means that we stop
4022 * if the value of the expression differs from the previous
4023 * value. Check to see if the value differs, and return true
4024 * (to indicate that the breakpoint condition has been met) if
4025 * so. If the previous value is 'empty', this means we've
4026 * never evaluated the value before, so the value can't have
4027 * changed; simply remember the new value in this case.
4028 */
4029 ret = (prv_val_->val.typ != VM_EMPTY
4030 && !prv_val_->val.equals(vmg_ valp));
4031
4032 /* remember the new value for next time */
4033 prv_val_->val = *valp;
4034 }
4035 else
4036 {
4037 /* see what we have */
4038 switch(valp->typ)
4039 {
4040 case VM_INT:
4041 /* if the number is zero, it's false, otherwise it's true */
4042 ret = (valp->val.intval != 0);
4043 break;
4044
4045 case VM_NIL:
4046 /* condition is false */
4047 ret = FALSE;
4048 break;
4049
4050 default:
4051 /* anything else is a true condition value */
4052 ret = TRUE;
4053 break;
4054 }
4055 }
4056 }
4057 err_catch(exc)
4058 {
4059 /*
4060 * an error occurred - treat the condition as true so that we
4061 * stop here
4062 */
4063 ret = TRUE;
4064 }
4065 err_end;
4066
4067 /* restore the original execution mode */
4068 G_debugger->restore_from_eval(&old_step);
4069
4070 /* rerstore the interpreter context */
4071 G_interpreter->restore_context(vmg_ &run_ctx);
4072
4073 /* return the result */
4074 return ret;
4075 }
4076
4077
4078 /*
4079 * Delete the breakpoint
4080 */
4081 void CVmDebugBp::do_delete(VMG0_)
4082 {
4083 /* remove the BP instruction from the code if necessary */
4084 set_bp_instr(vmg_ FALSE, FALSE);
4085
4086 /* mark myself as no longer active */
4087 in_use_ = FALSE;
4088 }
4089
4090 /*
4091 * Set or remove the BP instruction in the code
4092 */
4093 void CVmDebugBp::set_bp_instr(VMG_ int set, int always)
4094 {
4095 /* if I have a code address, set or remove the BP instruction */
4096 if (code_addr_ != 0)
4097 {
4098 /* get a writable pointer to the code location */
4099 uchar *code_ptr = (uchar *)code_addr_;
4100
4101 /* check to see if we're setting or removing the BP instruction */
4102 if (set)
4103 {
4104 /*
4105 * if we're not in the debugger, write the BP instruction to
4106 * the address - don't do this if we're in the debugger, since
4107 * we remove all BP instructions from the code while the
4108 * debugger has control to prevent any recursive debugger
4109 * invocations from taking place
4110 */
4111 if (always || !G_debugger->is_in_debugger())
4112 *code_ptr = OPC_BP;
4113 }
4114 else
4115 {
4116 /*
4117 * Restore the original instruction to that location. This
4118 * isn't necessary if we're in the debugger, since we remove
4119 * all BP's when the debugger takes control.
4120 */
4121 if (always || !G_debugger->is_in_debugger())
4122 *code_ptr = orig_instr_;
4123 }
4124 }
4125 }