2 * Debugger break-points handling
4 * Copyright 1994 Martin von Loewis
5 * Copyright 1995 Alexandre Julliard
6 * Copyright 1999,2000 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #define DR7_CONTROL_SHIFT 16
28 #define DR7_CONTROL_SIZE 4
30 #define DR7_RW_EXECUTE (0x0)
31 #define DR7_RW_WRITE (0x1)
32 #define DR7_RW_READ (0x3)
34 #define DR7_LEN_1 (0x0)
35 #define DR7_LEN_2 (0x4)
36 #define DR7_LEN_4 (0xC)
38 #define DR7_LOCAL_ENABLE_SHIFT 0
39 #define DR7_GLOBAL_ENABLE_SHIFT 1
40 #define DR7_ENABLE_SIZE 2
42 #define DR7_LOCAL_ENABLE_MASK (0x55)
43 #define DR7_GLOBAL_ENABLE_MASK (0xAA)
45 #define DR7_CONTROL_RESERVED (0xFC00)
46 #define DR7_LOCAL_SLOWDOWN (0x100)
47 #define DR7_GLOBAL_SLOWDOWN (0x200)
49 #define DR7_ENABLE_MASK(dr) (1<<(DR7_LOCAL_ENABLE_SHIFT+DR7_ENABLE_SIZE*(dr)))
50 #define IS_DR7_SET(ctrl,dr) ((ctrl)&DR7_ENABLE_MASK(dr))
51 #define INT3 0xcc /* int 3 opcode */
54 #define MAX_BREAKPOINTS 100
56 static DBG_BREAKPOINT breakpoints[MAX_BREAKPOINTS];
58 static int next_bp = 1; /* breakpoint 0 is reserved for step-over */
60 /***********************************************************************
61 * DEBUG_IsStepOverInstr
63 * Determine if the instruction at CS:EIP is an instruction that
64 * we need to step over (like a call or a repetitive string move).
66 static BOOL DEBUG_IsStepOverInstr(void)
73 addr.seg = DEBUG_context.SegCs;
74 addr.off = DEBUG_context.Eip;
75 /* FIXME: old code was using V86BASE(DEBUG_context)
76 * instead of passing through DOSMEM_MemoryBase
78 instr = (BYTE*)DEBUG_ToLinear(&addr);
82 if (!DEBUG_READ_MEM(instr, &ch, sizeof(ch)))
87 /* Skip all prefixes */
95 case 0x66: /* opcode size prefix */
96 case 0x67: /* addr size prefix */
98 case 0xf2: /* repne */
103 /* Handle call instructions */
105 case 0xcd: /* int <intno> */
106 case 0xe8: /* call <offset> */
107 case 0x9a: /* lcall <seg>:<off> */
110 case 0xff: /* call <regmodrm> */
111 if (!DEBUG_READ_MEM(instr + 1, &ch, sizeof(ch)))
113 return (((ch & 0x38) == 0x10) || ((ch & 0x38) == 0x18));
115 /* Handle string instructions */
117 case 0x6c: /* insb */
118 case 0x6d: /* insw */
119 case 0x6e: /* outsb */
120 case 0x6f: /* outsw */
121 case 0xa4: /* movsb */
122 case 0xa5: /* movsw */
123 case 0xa6: /* cmpsb */
124 case 0xa7: /* cmpsw */
125 case 0xaa: /* stosb */
126 case 0xab: /* stosw */
127 case 0xac: /* lodsb */
128 case 0xad: /* lodsw */
129 case 0xae: /* scasb */
130 case 0xaf: /* scasw */
143 /***********************************************************************
146 * Determine if the instruction at CS:EIP is an instruction that
147 * is a function return.
149 BOOL DEBUG_IsFctReturn(void)
156 addr.seg = DEBUG_context.SegCs;
157 addr.off = DEBUG_context.Eip;
158 /* FIXME: old code was using V86BASE(DEBUG_context)
159 * instead of passing through DOSMEM_MemoryBase
161 instr = (BYTE*)DEBUG_ToLinear(&addr);
163 if (!DEBUG_READ_MEM(instr, &ch, sizeof(ch)))
166 return (ch == 0xc2) || (ch == 0xc3);
173 /***********************************************************************
174 * DEBUG_SetBreakpoints
176 * Set or remove all the breakpoints.
178 void DEBUG_SetBreakpoints( BOOL set )
183 DEBUG_context.Dr7 &= ~DR7_LOCAL_ENABLE_MASK;
186 for (i = 0; i < next_bp; i++)
188 if (!(breakpoints[i].refcount && breakpoints[i].enabled))
191 switch (breakpoints[i].type) {
195 char ch = set ? INT3 : breakpoints[i].u.b.opcode;
197 if (!DEBUG_WRITE_MEM( (void*)DEBUG_ToLinear(&breakpoints[i].addr),
200 DEBUG_Printf(DBG_CHN_MESG, "Invalid address for breakpoint %d, disabling it\n", i);
201 breakpoints[i].enabled = FALSE;
211 int reg = breakpoints[i].u.w.reg;
216 case 0: lpdr = &DEBUG_context.Dr0; break;
217 case 1: lpdr = &DEBUG_context.Dr1; break;
218 case 2: lpdr = &DEBUG_context.Dr2; break;
219 case 3: lpdr = &DEBUG_context.Dr3; break;
220 default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
223 *lpdr = DEBUG_ToLinear(&breakpoints[i].addr);
224 bits = (breakpoints[i].u.w.rw) ? DR7_RW_WRITE : DR7_RW_READ;
225 switch (breakpoints[i].u.w.len + 1)
227 case 4: bits |= DR7_LEN_4; break;
228 case 2: bits |= DR7_LEN_2; break;
229 case 1: bits |= DR7_LEN_1; break;
230 default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
233 DEBUG_context.Dr7 &= ~(0x0F << (DR7_CONTROL_SHIFT + DR7_CONTROL_SIZE * reg));
234 DEBUG_context.Dr7 |= bits << (DR7_CONTROL_SHIFT + DR7_CONTROL_SIZE * reg);
235 DEBUG_context.Dr7 |= DR7_ENABLE_MASK(reg) | DR7_LOCAL_SLOWDOWN;
243 /***********************************************************************
244 * DEBUG_FindBreakpoint
246 * Find the breakpoint for a given address. Return the breakpoint
247 * number or -1 if none.
248 * If type is DBG_BREAKPOINT, addr is a complete addr
249 * If type is DBG_WATCHPOINT, only addr.off is meaningful and contains
252 static int DEBUG_FindBreakpoint( const DBG_ADDR *addr, int type )
256 for (i = 0; i < next_bp; i++)
258 if (breakpoints[i].refcount && breakpoints[i].enabled &&
259 breakpoints[i].type == type )
261 if ((type == DBG_BREAK &&
262 breakpoints[i].addr.seg == addr->seg &&
263 breakpoints[i].addr.off == addr->off) ||
264 (type == DBG_WATCH &&
265 DEBUG_ToLinear(&breakpoints[i].addr) == addr->off))
272 /***********************************************************************
275 * Find an empty slot in BP table to add a new break/watch point
277 static int DEBUG_InitXPoint(int type, const DBG_ADDR* addr)
281 for (num = (next_bp < MAX_BREAKPOINTS) ? next_bp++ : 1;
282 num < MAX_BREAKPOINTS; num++)
284 if (breakpoints[num].refcount == 0)
286 breakpoints[num].refcount = 1;
287 breakpoints[num].enabled = TRUE;
288 breakpoints[num].type = type;
289 breakpoints[num].skipcount = 0;
290 breakpoints[num].addr = *addr;
291 switch (DEBUG_GetSelectorType( addr->seg ))
294 breakpoints[num].is32 = 1;
298 breakpoints[num].is32 = 0;
301 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
307 DEBUG_Printf( DBG_CHN_MESG, "Too many breakpoints. Please delete some.\n" );
311 /***********************************************************************
312 * DEBUG_GetWatchedValue
314 * Returns the value watched by watch point 'num'.
316 static BOOL DEBUG_GetWatchedValue( int num, LPDWORD val )
320 if (!DEBUG_READ_MEM((void*)DEBUG_ToLinear(&breakpoints[num].addr),
321 buf, breakpoints[num].u.w.len + 1))
324 switch (breakpoints[num].u.w.len + 1)
326 case 4: *val = *(DWORD*)buf; break;
327 case 2: *val = *(WORD*)buf; break;
328 case 1: *val = *(BYTE*)buf; break;
329 default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
334 /***********************************************************************
335 * DEBUG_AddBreakpoint
339 BOOL DEBUG_AddBreakpoint( const DBG_VALUE *value, BOOL (*func)(void), BOOL verbose )
344 if ((num = DEBUG_FindBreakpoint(&value->addr, DBG_BREAK)) >= 1)
346 breakpoints[num].refcount++;
350 if (!DEBUG_READ_MEM((void*)DEBUG_ToLinear( &value->addr ), &ch, sizeof(ch)))
353 DEBUG_Printf( DBG_CHN_MESG, "Invalid address, can't set breakpoint\n");
357 if ((num = DEBUG_InitXPoint(DBG_BREAK, &value->addr)) == -1)
360 breakpoints[num].u.b.opcode = ch;
361 breakpoints[num].u.b.func = func;
363 DEBUG_Printf( DBG_CHN_MESG, "Breakpoint %d at ", num );
364 DEBUG_PrintAddress( &breakpoints[num].addr, breakpoints[num].is32 ? MODE_32 : MODE_16,
366 DEBUG_Printf( DBG_CHN_MESG, "\n" );
371 /***********************************************************************
372 * DEBUG_AddBreakpointFromValue
376 BOOL DEBUG_AddBreakpointFromValue( const DBG_VALUE *_value )
378 DBG_VALUE value = *_value;
380 if (value.type != NULL && value.type == DEBUG_GetBasicType(DT_BASIC_CONST_INT) && value.cookie == DV_HOST)
383 * We know that we have the actual offset stored somewhere
384 * else in 32-bit space. Grab it, and we
387 unsigned int seg2 = value.addr.seg;
389 value.addr.off = DEBUG_GetExprValue(&value, NULL);
390 value.addr.seg = seg2;
391 value.cookie = DV_TARGET;
394 if (!DEBUG_AddBreakpoint( &value, NULL, TRUE ))
396 if (!DBG_IVAR(CanDeferOnBPByAddr))
398 DEBUG_Printf( DBG_CHN_MESG, "Invalid address, can't set breakpoint\n"
399 "You can turn on deferring breakpoints by address by setting $CanDeferOnBPByAddr to 1\n");
402 DEBUG_Printf(DBG_CHN_MESG, "Unable to add breakpoint, will check again any time a new DLL is loaded\n");
403 DEBUG_CurrProcess->delayed_bp = DBG_realloc(DEBUG_CurrProcess->delayed_bp,
404 sizeof(DBG_DELAYED_BP) * ++DEBUG_CurrProcess->num_delayed_bp);
406 DEBUG_CurrProcess->delayed_bp[DEBUG_CurrProcess->num_delayed_bp - 1].is_symbol = FALSE;
407 DEBUG_CurrProcess->delayed_bp[DEBUG_CurrProcess->num_delayed_bp - 1].u.value = value;
414 /***********************************************************************
415 * DEBUG_AddBreakpointFromId
417 * Add a breakpoint from a function name (and eventually a line #)
419 void DEBUG_AddBreakpointFromId(const char *name, int lineno)
424 switch (DEBUG_GetSymbolValue(name, lineno, &value, TRUE))
427 DEBUG_AddBreakpoint(&value, NULL, TRUE);
431 case gsv_aborted: /* user aborted symbol lookup */
435 DEBUG_Printf(DBG_CHN_MESG, "Unable to add breakpoint, will check again when a new DLL is loaded\n");
436 for (i = 0; i < DEBUG_CurrProcess->num_delayed_bp; i++)
438 if (DEBUG_CurrProcess->delayed_bp[i].is_symbol &&
439 !strcmp(name, DEBUG_CurrProcess->delayed_bp[i].u.symbol.name) &&
440 lineno == DEBUG_CurrProcess->delayed_bp[i].u.symbol.lineno)
443 DEBUG_CurrProcess->delayed_bp = DBG_realloc(DEBUG_CurrProcess->delayed_bp,
444 sizeof(DBG_DELAYED_BP) * ++DEBUG_CurrProcess->num_delayed_bp);
446 DEBUG_CurrProcess->delayed_bp[DEBUG_CurrProcess->num_delayed_bp - 1].is_symbol = TRUE;
447 DEBUG_CurrProcess->delayed_bp[DEBUG_CurrProcess->num_delayed_bp - 1].u.symbol.name = strcpy(DBG_alloc(strlen(name) + 1), name);
448 DEBUG_CurrProcess->delayed_bp[DEBUG_CurrProcess->num_delayed_bp - 1].u.symbol.lineno = lineno;
451 /***********************************************************************
452 * DEBUG_AddBreakpointFromLineno
454 * Add a breakpoint from a line number in current file
456 void DEBUG_AddBreakpointFromLineno(int lineno)
460 DEBUG_GetCurrentAddress(&value.addr);
464 struct name_hash* nh;
466 DEBUG_FindNearestSymbol(&value.addr, TRUE, &nh, 0, NULL);
469 DEBUG_Printf(DBG_CHN_MESG, "Unable to add breakpoint\n");
472 DEBUG_GetLineNumberAddr(nh, lineno, &value.addr, TRUE);
476 value.cookie = DV_TARGET;
477 DEBUG_AddBreakpoint( &value,NULL, TRUE );
480 /***********************************************************************
481 * DEBUG_CheckDelayedBP
483 * Check is a registered delayed BP is now available.
485 void DEBUG_CheckDelayedBP(void)
489 DBG_DELAYED_BP* dbp = DEBUG_CurrProcess->delayed_bp;
491 for (i = 0; i < DEBUG_CurrProcess->num_delayed_bp; i++)
493 if (dbp[i].is_symbol)
495 if (DEBUG_GetSymbolValue(dbp[i].u.symbol.name, dbp[i].u.symbol.lineno, &value, TRUE) != gsv_found)
499 value = dbp[i].u.value;
500 DEBUG_Printf(DBG_CHN_MESG, "trying to add delayed %s-bp\n", dbp[i].is_symbol ? "S" : "A");
501 if (!dbp[i].is_symbol)
502 DEBUG_Printf(DBG_CHN_MESG, "\t%04x %04lx:%08lx\n",
503 dbp[i].u.value.cookie,
504 dbp[i].u.value.addr.seg,
505 dbp[i].u.value.addr.off);
507 DEBUG_Printf(DBG_CHN_MESG, "\t'%s' @ %d\n",
508 dbp[i].u.symbol.name, dbp[i].u.symbol.lineno);
510 if (DEBUG_AddBreakpoint(&value, NULL, FALSE))
511 memmove(&dbp[i], &dbp[i+1], (--DEBUG_CurrProcess->num_delayed_bp - i) * sizeof(*dbp));
515 /***********************************************************************
516 * DEBUG_AddWatchpoint
520 void DEBUG_AddWatchpoint( const DBG_VALUE *_value, BOOL is_write )
522 DBG_VALUE value = *_value;
527 assert(_value->cookie == DV_TARGET || _value->cookie == DV_HOST);
530 DEBUG_FixAddress( &value.addr, DEBUG_context.SegCs );
533 if ( value.type != NULL && value.type == DEBUG_GetBasicType(DT_BASIC_CONST_INT) )
536 * We know that we have the actual offset stored somewhere
537 * else in 32-bit space. Grab it, and we
540 seg2 = value.addr.seg;
542 value.addr.off = DEBUG_GetExprValue(&value, NULL);
543 value.addr.seg = seg2;
546 for (num = 1; num < next_bp; num++)
548 if (breakpoints[num].refcount && breakpoints[num].enabled &&
549 breakpoints[num].type == DBG_WATCH) {
550 mask |= (1 << breakpoints[num].u.w.reg);
554 for (reg = 0; reg < 4 && (mask & (1 << reg)); reg++);
557 DEBUG_Printf(DBG_CHN_MESG, "All i386 hardware watchpoints have been set. Delete some\n");
562 if ((num = DEBUG_InitXPoint(DBG_WATCH, &value.addr)) == -1)
565 breakpoints[num].u.w.len = 4 - 1;
566 if (_value->type && DEBUG_GetObjectSize(_value->type) < 4)
567 breakpoints[num].u.w.len = 2 - 1;
569 if (!DEBUG_GetWatchedValue( num, &breakpoints[num].u.w.oldval))
571 DEBUG_Printf(DBG_CHN_MESG, "Bad address. Watchpoint not set\n");
572 breakpoints[num].refcount = 0;
576 breakpoints[num].u.w.rw = (is_write) ? TRUE : FALSE;
577 breakpoints[reg].u.w.reg = reg;
579 DEBUG_Printf( DBG_CHN_MESG, "Watchpoint %d at ", num );
580 DEBUG_PrintAddress( &breakpoints[num].addr, breakpoints[num].is32 ? MODE_32 : MODE_16, TRUE );
581 DEBUG_Printf( DBG_CHN_MESG, "\n" );
585 /***********************************************************************
586 * DEBUG_AddWathpointFromId
588 * Add a watchpoint from a symbol name (and eventually a line #)
590 void DEBUG_AddWatchpointFromId(const char *name)
594 switch (DEBUG_GetSymbolValue(name, -1, &value, TRUE))
597 DEBUG_AddWatchpoint( &value, 1 );
600 DEBUG_Printf(DBG_CHN_MESG, "Unable to add watchpoint\n");
602 case gsv_aborted: /* user aborted symbol lookup */
607 /***********************************************************************
608 * DEBUG_DelBreakpoint
610 * Delete a breakpoint.
612 void DEBUG_DelBreakpoint( int num )
614 if ((num <= 0) || (num >= next_bp) || breakpoints[num].refcount == 0)
616 DEBUG_Printf( DBG_CHN_MESG, "Invalid breakpoint number %d\n", num );
620 if (--breakpoints[num].refcount > 0)
623 if( breakpoints[num].condition != NULL )
625 DEBUG_FreeExpr(breakpoints[num].condition);
626 breakpoints[num].condition = NULL;
629 breakpoints[num].enabled = FALSE;
630 breakpoints[num].refcount = 0;
631 breakpoints[num].skipcount = 0;
634 /***********************************************************************
635 * DEBUG_EnableBreakpoint
637 * Enable or disable a break point.
639 void DEBUG_EnableBreakpoint( int num, BOOL enable )
641 if ((num <= 0) || (num >= next_bp) || breakpoints[num].refcount == 0)
643 DEBUG_Printf( DBG_CHN_MESG, "Invalid breakpoint number %d\n", num );
646 breakpoints[num].enabled = (enable) ? TRUE : FALSE;
647 breakpoints[num].skipcount = 0;
651 /***********************************************************************
652 * DEBUG_FindTriggeredWatchpoint
654 * Lookup the watchpoints to see if one has been triggered
655 * Return >= (watch point index) if one is found and *oldval is set to
656 * the value watched before the TRAP
657 * Return -1 if none found (*oldval is undetermined)
659 * Unfortunately, Linux does *NOT* (A REAL PITA) report with ptrace
660 * the DR6 register value, so we have to look with our own need the
664 static int DEBUG_FindTriggeredWatchpoint(LPDWORD oldval)
670 /* Method 1 => get triggered watchpoint from context (doesn't work on Linux
671 * 2.2.x). This should be fixed in >= 2.2.16
673 for (i = 0; i < next_bp; i++)
677 if (breakpoints[i].refcount && breakpoints[i].enabled &&
678 breakpoints[i].type == DBG_WATCH &&
679 (DEBUG_context.Dr6 & (1 << breakpoints[i].u.w.reg)))
681 DEBUG_context.Dr6 &= ~(1 << breakpoints[i].u.w.reg);
683 *oldval = breakpoints[i].u.w.oldval;
684 if (DEBUG_GetWatchedValue(i, &val)) {
685 breakpoints[i].u.w.oldval = val;
691 /* Method 1 failed, trying method 2 */
693 /* Method 2 => check if value has changed among registered watchpoints
694 * this really sucks, but this is how gdb 4.18 works on my linux box
697 for (i = 0; i < next_bp; i++)
701 if (breakpoints[i].refcount && breakpoints[i].enabled &&
702 breakpoints[i].type == DBG_WATCH &&
703 DEBUG_GetWatchedValue(i, &val))
705 *oldval = breakpoints[i].u.w.oldval;
708 DEBUG_context.Dr6 &= ~(1 << breakpoints[i].u.w.reg);
709 breakpoints[i].u.w.oldval = val;
711 /* cannot break, because two watch points may have been triggered on
713 * only one will be reported to the user (FIXME ?)
722 /***********************************************************************
723 * DEBUG_InfoBreakpoints
725 * Display break points information.
727 void DEBUG_InfoBreakpoints(void)
731 DEBUG_Printf( DBG_CHN_MESG, "Breakpoints:\n" );
732 for (i = 1; i < next_bp; i++)
734 if (breakpoints[i].refcount && breakpoints[i].type == DBG_BREAK)
736 DEBUG_Printf( DBG_CHN_MESG, "%d: %c ", i, breakpoints[i].enabled ? 'y' : 'n');
737 DEBUG_PrintAddress( &breakpoints[i].addr,
738 breakpoints[i].is32 ? MODE_32 : MODE_16, TRUE);
739 DEBUG_Printf( DBG_CHN_MESG, " (%u)\n", breakpoints[i].refcount );
740 if( breakpoints[i].condition != NULL )
742 DEBUG_Printf(DBG_CHN_MESG, "\t\tstop when ");
743 DEBUG_DisplayExpr(breakpoints[i].condition);
744 DEBUG_Printf(DBG_CHN_MESG, "\n");
748 DEBUG_Printf( DBG_CHN_MESG, "Watchpoints:\n" );
749 for (i = 1; i < next_bp; i++)
751 if (breakpoints[i].refcount && breakpoints[i].type == DBG_WATCH)
753 DEBUG_Printf( DBG_CHN_MESG, "%d: %c ", i, breakpoints[i].enabled ? 'y' : 'n');
754 DEBUG_PrintAddress( &breakpoints[i].addr,
755 breakpoints[i].is32 ? MODE_32 : MODE_16, TRUE);
756 DEBUG_Printf( DBG_CHN_MESG, " on %d byte%s (%c)\n",
757 breakpoints[i].u.w.len + 1,
758 breakpoints[i].u.w.len > 0 ? "s" : "",
759 breakpoints[i].u.w.rw ? 'W' : 'R');
760 if( breakpoints[i].condition != NULL )
762 DEBUG_Printf(DBG_CHN_MESG, "\t\tstop when ");
763 DEBUG_DisplayExpr(breakpoints[i].condition);
764 DEBUG_Printf(DBG_CHN_MESG, "\n");
770 /***********************************************************************
773 * Check whether or not the condition (bp / skipcount) of a break/watch
776 static BOOL DEBUG_ShallBreak( int bpnum )
778 if ( breakpoints[bpnum].condition != NULL )
780 DBG_VALUE value = DEBUG_EvalExpr(breakpoints[bpnum].condition);
782 if ( value.type == NULL )
785 * Something wrong - unable to evaluate this expression.
787 DEBUG_Printf(DBG_CHN_MESG, "Unable to evaluate expression ");
788 DEBUG_DisplayExpr(breakpoints[bpnum].condition);
789 DEBUG_Printf(DBG_CHN_MESG, "\nTurning off condition\n");
790 DEBUG_AddBPCondition(bpnum, NULL);
792 else if( !DEBUG_GetExprValue( &value, NULL) )
798 if ( breakpoints[bpnum].skipcount > 0 && --breakpoints[bpnum].skipcount > 0 )
801 if ((breakpoints[bpnum].type == DBG_BREAK) && breakpoints[bpnum].u.b.func)
802 return breakpoints[bpnum].u.b.func();
806 /***********************************************************************
807 * DEBUG_ShouldContinue
809 * Determine if we should continue execution after a SIGTRAP signal when
810 * executing in the given mode.
812 BOOL DEBUG_ShouldContinue( DBG_ADDR *addr, DWORD code, int * count )
817 enum dbg_mode addr_mode;
818 struct symbol_info syminfo;
819 enum exec_mode mode = DEBUG_CurrThread->exec_mode;
822 /* If not single-stepping, back up over the int3 instruction */
823 if (code == EXCEPTION_BREAKPOINT)
830 bpnum = DEBUG_FindBreakpoint( addr, DBG_BREAK );
831 breakpoints[0].enabled = FALSE; /* disable the step-over breakpoint */
833 if ((bpnum != 0) && (bpnum != -1))
835 if (!DEBUG_ShallBreak(bpnum)) return TRUE;
837 DEBUG_Printf( DBG_CHN_MESG, "Stopped on breakpoint %d at ", bpnum );
838 syminfo = DEBUG_PrintAddress( &breakpoints[bpnum].addr,
839 breakpoints[bpnum].is32 ? MODE_32 : MODE_16, TRUE );
840 DEBUG_Printf( DBG_CHN_MESG, "\n" );
842 if( syminfo.list.sourcefile != NULL )
843 DEBUG_List(&syminfo.list, NULL, 0);
847 wpnum = DEBUG_FindTriggeredWatchpoint(&oldval);
848 if ((wpnum != 0) && (wpnum != -1))
850 /* If not single-stepping, do not back up over the int3 instruction */
851 if (code == EXCEPTION_BREAKPOINT)
858 if (!DEBUG_ShallBreak(wpnum)) return TRUE;
860 addr_mode = DEBUG_GetSelectorType( addr->seg );
861 DEBUG_Printf(DBG_CHN_MESG, "Stopped on watchpoint %d at ", wpnum);
862 syminfo = DEBUG_PrintAddress( addr, addr_mode, TRUE );
864 DEBUG_Printf(DBG_CHN_MESG, " values: old=%lu new=%lu\n",
865 oldval, breakpoints[wpnum].u.w.oldval);
866 if (syminfo.list.sourcefile != NULL)
867 DEBUG_List(&syminfo.list, NULL, 0);
872 * If our mode indicates that we are stepping line numbers,
873 * get the current function, and figure out if we are exactly
874 * on a line number or not.
876 if( mode == EXEC_STEP_OVER || mode == EXEC_STEP_INSTR )
878 if( DEBUG_CheckLinenoStatus(addr) == AT_LINENUMBER )
883 else if( mode == EXEC_STEPI_OVER || mode == EXEC_STEPI_INSTR )
888 if( *count > 0 || mode == EXEC_FINISH )
891 * We still need to execute more instructions.
897 * If we are about to stop, then print out the source line if we
900 if (mode != EXEC_CONT && mode != EXEC_FINISH)
902 DEBUG_FindNearestSymbol(addr, TRUE, NULL, 0, &syminfo.list);
903 if (syminfo.list.sourcefile != NULL)
905 DEBUG_List(&syminfo.list, NULL, 0);
910 /* If there's no breakpoint and we are not single-stepping, then
911 * either we must have encountered an int3 in the Windows program
912 * or someone is trying to stop us
913 * If the later, (no int3 opcode at current address) then stop,
914 * otherwise, let's skip it.
916 if ((bpnum == -1) && code == EXCEPTION_BREAKPOINT)
920 if (!DEBUG_READ_MEM(&addr, &c, 1)) c = 0xCC;
923 if (c != 0xCC) return FALSE;
927 /* no breakpoint, continue if in continuous mode */
928 return (mode == EXEC_CONT || mode == EXEC_FINISH);
931 /***********************************************************************
932 * DEBUG_SuspendExecution
934 * Remove all breakpoints before entering the debug loop
936 void DEBUG_SuspendExecution( void )
938 DEBUG_SetBreakpoints( FALSE );
939 breakpoints[0] = DEBUG_CurrThread->stepOverBP;
942 /***********************************************************************
943 * DEBUG_RestartExecution
945 * Set the breakpoints to the correct state to restart execution
948 void DEBUG_RestartExecution( int count )
955 enum exec_mode mode, ret_mode;
959 DEBUG_GetCurrentAddress( &addr );
962 * This is the mode we will be running in after we finish. We would like
963 * to be able to modify this in certain cases.
965 ret_mode = mode = DEBUG_CurrThread->exec_mode;
967 bp = DEBUG_FindBreakpoint( &addr, DBG_BREAK );
968 if ( bp != -1 && bp != 0)
971 * If we have set a new value, then save it in the BP number.
973 if( count != 0 && mode == EXEC_CONT )
975 breakpoints[bp].skipcount = count;
977 mode = EXEC_STEPI_INSTR; /* If there's a breakpoint, skip it */
981 if( mode == EXEC_CONT && count > 1 )
983 DEBUG_Printf(DBG_CHN_MESG, "Not stopped at any breakpoint; argument ignored.\n");
987 if( mode == EXEC_FINISH && DEBUG_IsFctReturn() )
989 mode = ret_mode = EXEC_STEPI_INSTR;
992 instr = DEBUG_ToLinear( &addr );
993 DEBUG_READ_MEM((void*)instr, &ch, sizeof(ch));
995 * See if the function we are stepping into has debug info
996 * and line numbers. If not, then we step over it instead.
997 * FIXME - we need to check for things like thunks or trampolines,
998 * as the actual function may in fact have debug info.
1002 DEBUG_READ_MEM((void*)(instr + 1), &delta, sizeof(delta));
1004 DEBUG_Disasm(&addr2, FALSE);
1007 status = DEBUG_CheckLinenoStatus(&addr2);
1009 * Anytime we have a trampoline, step over it.
1011 if( ((mode == EXEC_STEP_OVER) || (mode == EXEC_STEPI_OVER))
1012 && status == FUNC_IS_TRAMPOLINE )
1015 DEBUG_Printf(DBG_CHN_MESG, "Not stepping into trampoline at %x (no lines)\n",
1018 mode = EXEC_STEP_OVER_TRAMPOLINE;
1021 if( mode == EXEC_STEP_INSTR && status == FUNC_HAS_NO_LINES )
1024 DEBUG_Printf(DBG_CHN_MESG, "Not stepping into function at %x (no lines)\n",
1027 mode = EXEC_STEP_OVER;
1032 if( mode == EXEC_STEP_INSTR )
1034 if( DEBUG_CheckLinenoStatus(&addr) == FUNC_HAS_NO_LINES )
1036 DEBUG_Printf(DBG_CHN_MESG, "Single stepping until exit from function, \n");
1037 DEBUG_Printf(DBG_CHN_MESG, "which has no line number information.\n");
1039 ret_mode = mode = EXEC_FINISH;
1045 case EXEC_CONT: /* Continuous execution */
1047 DEBUG_context.EFlags &= ~STEP_FLAG;
1049 DEBUG_SetBreakpoints( TRUE );
1052 case EXEC_STEP_OVER_TRAMPOLINE:
1054 * This is the means by which we step over our conversion stubs
1055 * in callfrom*.s and callto*.s. We dig the appropriate address
1056 * off the stack, and we set the breakpoint there instead of the
1057 * address just after the call.
1060 DEBUG_READ_MEM((void*)(DEBUG_context.Esp +
1061 2 * sizeof(unsigned int)),
1062 &addr.off, sizeof(addr.off));
1063 DEBUG_context.EFlags &= ~STEP_FLAG;
1065 breakpoints[0].addr = addr;
1066 breakpoints[0].enabled = TRUE;
1067 breakpoints[0].refcount = 1;
1068 breakpoints[0].skipcount = 0;
1069 DEBUG_READ_MEM((void*)DEBUG_ToLinear( &addr ), &breakpoints[0].u.b.opcode,
1071 DEBUG_SetBreakpoints( TRUE );
1075 case EXEC_STEPI_OVER: /* Stepping over a call */
1076 case EXEC_STEP_OVER: /* Stepping over a call */
1077 if (DEBUG_IsStepOverInstr())
1080 DEBUG_context.EFlags &= ~STEP_FLAG;
1082 DEBUG_Disasm(&addr, FALSE);
1083 breakpoints[0].addr = addr;
1084 breakpoints[0].enabled = TRUE;
1085 breakpoints[0].refcount = 1;
1086 breakpoints[0].skipcount = 0;
1087 DEBUG_READ_MEM((void*)DEBUG_ToLinear( &addr ), &breakpoints[0].u.b.opcode,
1089 DEBUG_SetBreakpoints( TRUE );
1092 /* else fall through to single-stepping */
1094 case EXEC_STEP_INSTR: /* Single-stepping an instruction */
1095 case EXEC_STEPI_INSTR: /* Single-stepping an instruction */
1097 DEBUG_context.EFlags |= STEP_FLAG;
1101 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
1103 DEBUG_CurrThread->stepOverBP = breakpoints[0];
1104 DEBUG_CurrThread->exec_mode = ret_mode;
1108 DEBUG_AddBPCondition(int num, struct expr * exp)
1110 if ((num <= 0) || (num >= next_bp) || !breakpoints[num].refcount)
1112 DEBUG_Printf( DBG_CHN_MESG, "Invalid breakpoint number %d\n", num );
1116 if( breakpoints[num].condition != NULL )
1118 DEBUG_FreeExpr(breakpoints[num].condition);
1119 breakpoints[num].condition = NULL;
1124 breakpoints[num].condition = DEBUG_CloneExpr(exp);