Fixed regression in break command.
[wine.git] / debugger / break.c
blob311746516b01ed300b2a78c760e7a3a2020cc2b0
1 /*
2 * Debugger break-points handling
4 * Copyright 1994 Martin von Loewis
5 * Copyright 1995 Alexandre Julliard
6 * Copyright 1999,2000 Eric Pouech
7 */
9 #include "config.h"
10 #include "debugger.h"
12 #ifdef __i386__
13 #define DR7_CONTROL_SHIFT 16
14 #define DR7_CONTROL_SIZE 4
16 #define DR7_RW_EXECUTE (0x0)
17 #define DR7_RW_WRITE (0x1)
18 #define DR7_RW_READ (0x3)
20 #define DR7_LEN_1 (0x0)
21 #define DR7_LEN_2 (0x4)
22 #define DR7_LEN_4 (0xC)
24 #define DR7_LOCAL_ENABLE_SHIFT 0
25 #define DR7_GLOBAL_ENABLE_SHIFT 1
26 #define DR7_ENABLE_SIZE 2
28 #define DR7_LOCAL_ENABLE_MASK (0x55)
29 #define DR7_GLOBAL_ENABLE_MASK (0xAA)
31 #define DR7_CONTROL_RESERVED (0xFC00)
32 #define DR7_LOCAL_SLOWDOWN (0x100)
33 #define DR7_GLOBAL_SLOWDOWN (0x200)
35 #define DR7_ENABLE_MASK(dr) (1<<(DR7_LOCAL_ENABLE_SHIFT+DR7_ENABLE_SIZE*(dr)))
36 #define IS_DR7_SET(ctrl,dr) ((ctrl)&DR7_ENABLE_MASK(dr))
37 #define INT3 0xcc /* int 3 opcode */
38 #endif
40 #define MAX_BREAKPOINTS 100
42 static DBG_BREAKPOINT breakpoints[MAX_BREAKPOINTS];
44 static int next_bp = 1; /* breakpoint 0 is reserved for step-over */
46 /***********************************************************************
47 * DEBUG_IsStepOverInstr
49 * Determine if the instruction at CS:EIP is an instruction that
50 * we need to step over (like a call or a repetitive string move).
52 static BOOL DEBUG_IsStepOverInstr(void)
54 #ifdef __i386__
55 BYTE* instr;
56 BYTE ch;
57 DBG_ADDR addr;
59 addr.seg = DEBUG_context.SegCs;
60 addr.off = DEBUG_context.Eip;
61 /* FIXME: old code was using V86BASE(DEBUG_context)
62 * instead of passing thru DOSMEM_MemoryBase
64 instr = (BYTE*)DEBUG_ToLinear(&addr);
66 for (;;)
68 if (!DEBUG_READ_MEM(instr, &ch, sizeof(ch)))
69 return FALSE;
71 switch (ch)
73 /* Skip all prefixes */
75 case 0x2e: /* cs: */
76 case 0x36: /* ss: */
77 case 0x3e: /* ds: */
78 case 0x26: /* es: */
79 case 0x64: /* fs: */
80 case 0x65: /* gs: */
81 case 0x66: /* opcode size prefix */
82 case 0x67: /* addr size prefix */
83 case 0xf0: /* lock */
84 case 0xf2: /* repne */
85 case 0xf3: /* repe */
86 instr++;
87 continue;
89 /* Handle call instructions */
91 case 0xcd: /* int <intno> */
92 case 0xe8: /* call <offset> */
93 case 0x9a: /* lcall <seg>:<off> */
94 return TRUE;
96 case 0xff: /* call <regmodrm> */
97 if (!DEBUG_READ_MEM(instr + 1, &ch, sizeof(ch)))
98 return FALSE;
99 return (((ch & 0x38) == 0x10) || ((ch & 0x38) == 0x18));
101 /* Handle string instructions */
103 case 0x6c: /* insb */
104 case 0x6d: /* insw */
105 case 0x6e: /* outsb */
106 case 0x6f: /* outsw */
107 case 0xa4: /* movsb */
108 case 0xa5: /* movsw */
109 case 0xa6: /* cmpsb */
110 case 0xa7: /* cmpsw */
111 case 0xaa: /* stosb */
112 case 0xab: /* stosw */
113 case 0xac: /* lodsb */
114 case 0xad: /* lodsw */
115 case 0xae: /* scasb */
116 case 0xaf: /* scasw */
117 return TRUE;
119 default:
120 return FALSE;
123 #else
124 return FALSE;
125 #endif
129 /***********************************************************************
130 * DEBUG_IsFctReturn
132 * Determine if the instruction at CS:EIP is an instruction that
133 * is a function return.
135 BOOL DEBUG_IsFctReturn(void)
137 #ifdef __i386__
138 BYTE* instr;
139 BYTE ch;
140 DBG_ADDR addr;
142 addr.seg = DEBUG_context.SegCs;
143 addr.off = DEBUG_context.Eip;
144 /* FIXME: old code was using V86BASE(DEBUG_context)
145 * instead of passing thru DOSMEM_MemoryBase
147 instr = (BYTE*)DEBUG_ToLinear(&addr);
149 if (!DEBUG_READ_MEM(instr, &ch, sizeof(ch)))
150 return FALSE;
152 return (ch == 0xc2) || (ch == 0xc3);
153 #else
154 return FALSE;
155 #endif
159 /***********************************************************************
160 * DEBUG_SetBreakpoints
162 * Set or remove all the breakpoints.
164 void DEBUG_SetBreakpoints( BOOL set )
166 int i;
168 #ifdef __i386__
169 DEBUG_context.Dr7 &= ~DR7_LOCAL_ENABLE_MASK;
170 #endif
172 for (i = 0; i < next_bp; i++)
174 if (!(breakpoints[i].refcount && breakpoints[i].enabled))
175 continue;
177 switch (breakpoints[i].type) {
178 case DBG_BREAK:
180 #ifdef __i386__
181 char ch = set ? INT3 : breakpoints[i].u.b.opcode;
183 if (!DEBUG_WRITE_MEM( (void*)DEBUG_ToLinear(&breakpoints[i].addr),
184 &ch, sizeof(ch) ))
186 DEBUG_Printf(DBG_CHN_MESG, "Invalid address for breakpoint %d, disabling it\n", i);
187 breakpoints[i].enabled = FALSE;
189 #endif
191 break;
192 case DBG_WATCH:
193 if (set)
195 #ifdef __i386__
196 DWORD bits;
197 int reg = breakpoints[i].u.w.reg;
198 LPDWORD lpdr = NULL;
200 switch (reg)
202 case 0: lpdr = &DEBUG_context.Dr0; break;
203 case 1: lpdr = &DEBUG_context.Dr1; break;
204 case 2: lpdr = &DEBUG_context.Dr2; break;
205 case 3: lpdr = &DEBUG_context.Dr3; break;
206 default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
209 *lpdr = DEBUG_ToLinear(&breakpoints[i].addr);
210 bits = (breakpoints[i].u.w.rw) ? DR7_RW_WRITE : DR7_RW_READ;
211 switch (breakpoints[i].u.w.len + 1)
213 case 4: bits |= DR7_LEN_4; break;
214 case 2: bits |= DR7_LEN_2; break;
215 case 1: bits |= DR7_LEN_1; break;
216 default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
219 DEBUG_context.Dr7 &= ~(0x0F << (DR7_CONTROL_SHIFT + DR7_CONTROL_SIZE * reg));
220 DEBUG_context.Dr7 |= bits << (DR7_CONTROL_SHIFT + DR7_CONTROL_SIZE * reg);
221 DEBUG_context.Dr7 |= DR7_ENABLE_MASK(reg) | DR7_LOCAL_SLOWDOWN;
222 #endif
224 break;
229 /***********************************************************************
230 * DEBUG_FindBreakpoint
232 * Find the breakpoint for a given address. Return the breakpoint
233 * number or -1 if none.
234 * If type is DBG_BREAKPOINT, addr is a complete addr
235 * If type is DBG_WATCHPOINT, only addr.off is meaningful and contains
236 * linear address
238 static int DEBUG_FindBreakpoint( const DBG_ADDR *addr, int type )
240 int i;
242 for (i = 0; i < next_bp; i++)
244 if (breakpoints[i].refcount && breakpoints[i].enabled &&
245 breakpoints[i].type == type )
247 if ((type == DBG_BREAK &&
248 breakpoints[i].addr.seg == addr->seg &&
249 breakpoints[i].addr.off == addr->off) ||
250 (type == DBG_WATCH &&
251 DEBUG_ToLinear(&breakpoints[i].addr) == addr->off))
252 return i;
255 return -1;
258 /***********************************************************************
259 * DEBUG_InitXPoint
261 * Find an empty slot in BP table to add a new break/watch point
263 static int DEBUG_InitXPoint(int type, DBG_ADDR* addr)
265 int num;
267 for (num = (next_bp < MAX_BREAKPOINTS) ? next_bp++ : 1;
268 num < MAX_BREAKPOINTS; num++)
270 if (breakpoints[num].refcount == 0)
272 breakpoints[num].refcount = 1;
273 breakpoints[num].enabled = TRUE;
274 breakpoints[num].type = type;
275 breakpoints[num].skipcount = 0;
276 breakpoints[num].addr = *addr;
277 breakpoints[num].is32 = 1;
278 #ifdef __i386__
279 if (addr->seg)
281 switch (DEBUG_GetSelectorType( addr->seg ))
283 case 32: break;
284 case 16: breakpoints[num].is32 = 0; break;
285 default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
288 #endif
289 return num;
293 DEBUG_Printf( DBG_CHN_MESG, "Too many breakpoints. Please delete some.\n" );
294 return -1;
297 /***********************************************************************
298 * DEBUG_GetWatchedValue
300 * Returns the value watched by watch point 'num'.
302 static BOOL DEBUG_GetWatchedValue( int num, LPDWORD val )
304 BYTE buf[4];
306 if (!DEBUG_READ_MEM((void*)DEBUG_ToLinear(&breakpoints[num].addr),
307 buf, breakpoints[num].u.w.len + 1))
308 return FALSE;
310 switch (breakpoints[num].u.w.len + 1)
312 case 4: *val = *(DWORD*)buf; break;
313 case 2: *val = *(WORD*)buf; break;
314 case 1: *val = *(BYTE*)buf; break;
315 default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
317 return TRUE;
320 /***********************************************************************
321 * DEBUG_AddBreakpoint
323 * Add a breakpoint.
325 void DEBUG_AddBreakpoint( const DBG_VALUE *_value, BOOL (*func)(void) )
327 DBG_VALUE value = *_value;
328 int num;
329 BYTE ch;
331 if( value.type != NULL && value.type == DEBUG_TypeIntConst )
334 * We know that we have the actual offset stored somewhere
335 * else in 32-bit space. Grab it, and we
336 * should be all set.
338 unsigned int seg2 = value.addr.seg;
339 value.addr.seg = 0;
340 value.addr.off = DEBUG_GetExprValue(&value, NULL);
341 value.addr.seg = seg2;
344 if ((num = DEBUG_FindBreakpoint(&value.addr, DBG_BREAK)) >= 1)
346 breakpoints[num].refcount++;
347 return;
350 if (!DEBUG_READ_MEM_VERBOSE((void*)DEBUG_ToLinear( &value.addr ), &ch, sizeof(ch)))
352 DEBUG_Printf( DBG_CHN_MESG, "Invalid address, can't set breakpoint\n");
353 return;
356 if ((num = DEBUG_InitXPoint(DBG_BREAK, &value.addr)) == -1)
357 return;
359 breakpoints[num].u.b.opcode = ch;
360 breakpoints[num].u.b.func = func;
362 DEBUG_Printf( DBG_CHN_MESG, "Breakpoint %d at ", num );
363 DEBUG_PrintAddress( &breakpoints[num].addr, breakpoints[num].is32 ? 32 : 16,
364 TRUE );
365 DEBUG_Printf( DBG_CHN_MESG, "\n" );
368 /***********************************************************************
369 * DEBUG_AddBreakpointFromId
371 * Add a breakpoint from a function name (and eventually a line #)
373 void DEBUG_AddBreakpointFromId(const char *name, int lineno)
375 DBG_VALUE value;
377 if (DEBUG_GetSymbolValue(name, lineno, &value, TRUE))
378 DEBUG_AddBreakpoint(&value, NULL);
379 else
380 DEBUG_Printf(DBG_CHN_MESG, "Unable to add breakpoint\n");
383 /***********************************************************************
384 * DEBUG_AddBreakpointFromLineno
386 * Add a breakpoint from a line number in current file
388 void DEBUG_AddBreakpointFromLineno(int lineno)
390 DBG_VALUE value;
392 DEBUG_GetCurrentAddress(&value.addr);
394 if (lineno != -1) {
395 struct name_hash* nh;
397 DEBUG_FindNearestSymbol(&value.addr, TRUE, &nh, 0, NULL);
398 if (nh == NULL) {
399 DEBUG_Printf(DBG_CHN_MESG,"Unable to add breakpoint\n");
400 return;
402 DEBUG_GetLineNumberAddr(nh, lineno, &value.addr, TRUE);
405 value.type = NULL;
406 value.cookie = DV_TARGET;
407 DEBUG_AddBreakpoint( &value, NULL );
410 /***********************************************************************
411 * DEBUG_AddWatchpoint
413 * Add a watchpoint.
415 void DEBUG_AddWatchpoint( const DBG_VALUE *_value, BOOL is_write )
417 DBG_VALUE value = *_value;
418 int num, reg = -1;
419 unsigned seg2;
420 DWORD mask = 0;
422 assert(_value->cookie == DV_TARGET || _value->cookie == DV_HOST);
424 #ifdef __i386__
425 DEBUG_FixAddress( &value.addr, DEBUG_context.SegCs );
426 #endif
428 if ( value.type != NULL && value.type == DEBUG_TypeIntConst )
431 * We know that we have the actual offset stored somewhere
432 * else in 32-bit space. Grab it, and we
433 * should be all set.
435 seg2 = value.addr.seg;
436 value.addr.seg = 0;
437 value.addr.off = DEBUG_GetExprValue(&value, NULL);
438 value.addr.seg = seg2;
441 for (num = 1; num < next_bp; num++)
443 if (breakpoints[num].refcount && breakpoints[num].enabled &&
444 breakpoints[num].type == DBG_WATCH) {
445 mask |= (1 << breakpoints[num].u.w.reg);
448 #ifdef __i386__
449 for (reg = 0; reg < 4 && (mask & (1 << reg)); reg++);
450 if (reg == 4)
452 DEBUG_Printf(DBG_CHN_MESG, "All i386 hardware watchpoints have been set. Delete some\n");
453 return;
455 #endif
457 if ((num = DEBUG_InitXPoint(DBG_WATCH, &value.addr)) == -1)
458 return;
460 breakpoints[num].u.w.len = 4 - 1;
461 if (_value->type && DEBUG_GetObjectSize(_value->type) < 4)
462 breakpoints[num].u.w.len = 2 - 1;
464 if (!DEBUG_GetWatchedValue( num, &breakpoints[num].u.w.oldval))
466 DEBUG_Printf(DBG_CHN_MESG, "Bad address. Watchpoint not set\n");
467 breakpoints[num].refcount = 0;
470 breakpoints[num].u.w.rw = (is_write) ? TRUE : FALSE;
471 breakpoints[reg].u.w.reg = reg;
473 DEBUG_Printf( DBG_CHN_MESG, "Watchpoint %d at ", num );
474 DEBUG_PrintAddress( &breakpoints[num].addr, breakpoints[num].is32 ? 32:16, TRUE );
475 DEBUG_Printf( DBG_CHN_MESG, "\n" );
478 /***********************************************************************
479 * DEBUG_AddWathpointFromId
481 * Add a watchpoint from a symbol name (and eventually a line #)
483 void DEBUG_AddWatchpointFromId(const char *name, int lineno)
485 DBG_VALUE value;
487 if( DEBUG_GetSymbolValue(name, lineno, &value, TRUE) )
488 DEBUG_AddWatchpoint( &value, 1 );
489 else
490 DEBUG_Printf(DBG_CHN_MESG, "Unable to add watchpoint\n");
493 /***********************************************************************
494 * DEBUG_DelBreakpoint
496 * Delete a breakpoint.
498 void DEBUG_DelBreakpoint( int num )
500 if ((num <= 0) || (num >= next_bp) || breakpoints[num].refcount == 0)
502 DEBUG_Printf( DBG_CHN_MESG, "Invalid breakpoint number %d\n", num );
503 return;
506 if (--breakpoints[num].refcount > 0)
507 return;
509 if( breakpoints[num].condition != NULL )
511 DEBUG_FreeExpr(breakpoints[num].condition);
512 breakpoints[num].condition = NULL;
515 breakpoints[num].enabled = FALSE;
516 breakpoints[num].refcount = 0;
517 breakpoints[num].skipcount = 0;
520 /***********************************************************************
521 * DEBUG_EnableBreakpoint
523 * Enable or disable a break point.
525 void DEBUG_EnableBreakpoint( int num, BOOL enable )
527 if ((num <= 0) || (num >= next_bp) || breakpoints[num].refcount == 0)
529 DEBUG_Printf( DBG_CHN_MESG, "Invalid breakpoint number %d\n", num );
530 return;
532 breakpoints[num].enabled = (enable) ? TRUE : FALSE;
533 breakpoints[num].skipcount = 0;
537 /***********************************************************************
538 * DEBUG_FindTriggeredWatchpoint
540 * Lookup the watchpoints to see if one has been triggered
541 * Return >= (watch point index) if one is found and *oldval is set to
542 * the value watched before the TRAP
543 * Return -1 if none found (*oldval is undetermined)
545 * Unfortunately, Linux does *NOT* (A REAL PITA) report with ptrace
546 * the DR6 register value, so we have to look with our own need the
547 * cause of the TRAP.
548 * -EP
550 static int DEBUG_FindTriggeredWatchpoint(LPDWORD oldval)
552 int found = -1;
553 #ifdef __i386__
554 int i;
556 /* Method 1 => get triggered watchpoint from context (doesn't work on Linux
557 * 2.2.x). This should be fixed in >= 2.2.16
559 for (i = 0; i < next_bp; i++)
561 DWORD val = 0;
563 if (breakpoints[i].refcount && breakpoints[i].enabled &&
564 breakpoints[i].type == DBG_WATCH &&
565 (DEBUG_context.Dr6 & (1 << breakpoints[i].u.w.reg)))
567 DEBUG_context.Dr6 &= ~(1 << breakpoints[i].u.w.reg);
569 *oldval = breakpoints[i].u.w.oldval;
570 if (DEBUG_GetWatchedValue(i, &val)) {
571 breakpoints[i].u.w.oldval = val;
572 return i;
577 /* Method 1 failed, trying method 2 */
579 /* Method 2 => check if value has changed among registered watchpoints
580 * this really sucks, but this is how gdb 4.18 works on my linux box
581 * -EP
583 for (i = 0; i < next_bp; i++)
585 DWORD val = 0;
587 if (breakpoints[i].refcount && breakpoints[i].enabled &&
588 breakpoints[i].type == DBG_WATCH &&
589 DEBUG_GetWatchedValue(i, &val))
591 *oldval = breakpoints[i].u.w.oldval;
592 if (val != *oldval)
594 DEBUG_context.Dr6 &= ~(1 << breakpoints[i].u.w.reg);
595 breakpoints[i].u.w.oldval = val;
596 found = i;
597 /* cannot break, because two watch points may have been triggered on
598 * the same access
599 * only one will be reported to the user (FIXME ?)
604 #endif
605 return found;
608 /***********************************************************************
609 * DEBUG_InfoBreakpoints
611 * Display break points information.
613 void DEBUG_InfoBreakpoints(void)
615 int i;
617 DEBUG_Printf( DBG_CHN_MESG, "Breakpoints:\n" );
618 for (i = 1; i < next_bp; i++)
620 if (breakpoints[i].refcount && breakpoints[i].type == DBG_BREAK)
622 DEBUG_Printf( DBG_CHN_MESG, "%d: %c ", i, breakpoints[i].enabled ? 'y' : 'n');
623 DEBUG_PrintAddress( &breakpoints[i].addr,
624 breakpoints[i].is32 ? 32 : 16, TRUE);
625 DEBUG_Printf( DBG_CHN_MESG, " (%u)\n", breakpoints[i].refcount );
626 if( breakpoints[i].condition != NULL )
628 DEBUG_Printf(DBG_CHN_MESG, "\t\tstop when ");
629 DEBUG_DisplayExpr(breakpoints[i].condition);
630 DEBUG_Printf(DBG_CHN_MESG, "\n");
634 DEBUG_Printf( DBG_CHN_MESG, "Watchpoints:\n" );
635 for (i = 1; i < next_bp; i++)
637 if (breakpoints[i].refcount && breakpoints[i].type == DBG_WATCH)
639 DEBUG_Printf( DBG_CHN_MESG, "%d: %c ", i, breakpoints[i].enabled ? 'y' : 'n');
640 DEBUG_PrintAddress( &breakpoints[i].addr,
641 breakpoints[i].is32 ? 32 : 16, TRUE);
642 DEBUG_Printf( DBG_CHN_MESG, " on %d byte%s (%c)\n",
643 breakpoints[i].u.w.len + 1,
644 breakpoints[i].u.w.len > 0 ? "s" : "",
645 breakpoints[i].u.w.rw ? 'W' : 'R');
646 if( breakpoints[i].condition != NULL )
648 DEBUG_Printf(DBG_CHN_MESG, "\t\tstop when ");
649 DEBUG_DisplayExpr(breakpoints[i].condition);
650 DEBUG_Printf(DBG_CHN_MESG, "\n");
656 /***********************************************************************
657 * DEBUG_ShallBreak
659 * Check whether or not the condition (bp / skipcount) of a break/watch
660 * point are met.
662 static BOOL DEBUG_ShallBreak( int bpnum )
664 if ( breakpoints[bpnum].condition != NULL )
666 DBG_VALUE value = DEBUG_EvalExpr(breakpoints[bpnum].condition);
668 if ( value.type == NULL )
671 * Something wrong - unable to evaluate this expression.
673 DEBUG_Printf(DBG_CHN_MESG, "Unable to evaluate expression ");
674 DEBUG_DisplayExpr(breakpoints[bpnum].condition);
675 DEBUG_Printf(DBG_CHN_MESG, "\nTurning off condition\n");
676 DEBUG_AddBPCondition(bpnum, NULL);
678 else if( !DEBUG_GetExprValue( &value, NULL) )
680 return FALSE;
684 if ( breakpoints[bpnum].skipcount > 0 && --breakpoints[bpnum].skipcount > 0 )
685 return FALSE;
687 if ((breakpoints[bpnum].type == DBG_BREAK) && breakpoints[bpnum].u.b.func)
688 return breakpoints[bpnum].u.b.func();
689 return TRUE;
692 /***********************************************************************
693 * DEBUG_ShouldContinue
695 * Determine if we should continue execution after a SIGTRAP signal when
696 * executing in the given mode.
698 BOOL DEBUG_ShouldContinue( DWORD code, enum exec_mode mode, int * count )
700 DBG_ADDR addr;
701 int bpnum;
702 DWORD oldval;
703 int wpnum;
704 int addrlen = 32;
705 struct symbol_info syminfo;
707 #ifdef __i386__
708 /* If not single-stepping, back up over the int3 instruction */
709 if (code == EXCEPTION_BREAKPOINT)
710 DEBUG_context.Eip--;
711 #endif
713 DEBUG_GetCurrentAddress( &addr );
714 bpnum = DEBUG_FindBreakpoint( &addr, DBG_BREAK );
715 breakpoints[0].enabled = FALSE; /* disable the step-over breakpoint */
717 if ((bpnum != 0) && (bpnum != -1))
719 if (!DEBUG_ShallBreak(bpnum)) return TRUE;
721 DEBUG_Printf( DBG_CHN_MESG, "Stopped on breakpoint %d at ", bpnum );
722 syminfo = DEBUG_PrintAddress( &breakpoints[bpnum].addr,
723 breakpoints[bpnum].is32 ? 32 : 16, TRUE );
724 DEBUG_Printf( DBG_CHN_MESG, "\n" );
726 if( syminfo.list.sourcefile != NULL )
727 DEBUG_List(&syminfo.list, NULL, 0);
728 return FALSE;
731 wpnum = DEBUG_FindTriggeredWatchpoint(&oldval);
732 if ((wpnum != 0) && (wpnum != -1))
734 /* If not single-stepping, do not back up over the int3 instruction */
735 if (code == EXCEPTION_BREAKPOINT)
737 #ifdef __i386__
738 DEBUG_context.Eip++;
739 addr.off++;
740 #endif
742 if (!DEBUG_ShallBreak(wpnum)) return TRUE;
744 #ifdef __i386__
745 if (addr.seg) addrlen = DEBUG_GetSelectorType( addr.seg );
746 #endif
747 DEBUG_Printf(DBG_CHN_MESG, "Stopped on watchpoint %d at ", wpnum);
748 syminfo = DEBUG_PrintAddress( &addr, addrlen, TRUE );
750 DEBUG_Printf(DBG_CHN_MESG, " values: old=%lu new=%lu\n",
751 oldval, breakpoints[wpnum].u.w.oldval);
752 if (syminfo.list.sourcefile != NULL)
753 DEBUG_List(&syminfo.list, NULL, 0);
754 return FALSE;
758 * If our mode indicates that we are stepping line numbers,
759 * get the current function, and figure out if we are exactly
760 * on a line number or not.
762 if( mode == EXEC_STEP_OVER || mode == EXEC_STEP_INSTR )
764 if( DEBUG_CheckLinenoStatus(&addr) == AT_LINENUMBER )
766 (*count)--;
769 else if( mode == EXEC_STEPI_OVER || mode == EXEC_STEPI_INSTR )
771 (*count)--;
774 if( *count > 0 || mode == EXEC_FINISH )
777 * We still need to execute more instructions.
779 return TRUE;
783 * If we are about to stop, then print out the source line if we
784 * have it.
786 if (mode != EXEC_CONT && mode != EXEC_PASS && mode != EXEC_FINISH)
788 DEBUG_FindNearestSymbol( &addr, TRUE, NULL, 0, &syminfo.list);
789 if( syminfo.list.sourcefile != NULL )
791 DEBUG_List(&syminfo.list, NULL, 0);
795 #ifdef __i386__
796 /* If there's no breakpoint and we are not single-stepping, then we */
797 /* must have encountered an int3 in the Windows program; let's skip it. */
798 if ((bpnum == -1) && code == EXCEPTION_BREAKPOINT)
799 DEBUG_context.Eip++;
800 #endif
802 /* no breakpoint, continue if in continuous mode */
803 return (mode == EXEC_CONT || mode == EXEC_PASS || mode == EXEC_FINISH);
806 /***********************************************************************
807 * DEBUG_SuspendExecution
809 * Remove all breakpoints before entering the debug loop
811 void DEBUG_SuspendExecution( void )
813 DEBUG_SetBreakpoints( FALSE );
814 breakpoints[0] = DEBUG_CurrThread->stepOverBP;
817 /***********************************************************************
818 * DEBUG_RestartExecution
820 * Set the breakpoints to the correct state to restart execution
821 * in the given mode.
823 enum exec_mode DEBUG_RestartExecution( enum exec_mode mode, int count )
825 DBG_ADDR addr;
826 DBG_ADDR addr2;
827 int bp;
828 int delta;
829 int status;
830 enum exec_mode ret_mode;
831 DWORD instr;
832 unsigned char ch;
834 DEBUG_GetCurrentAddress( &addr );
837 * This is the mode we will be running in after we finish. We would like
838 * to be able to modify this in certain cases.
840 ret_mode = mode;
842 bp = DEBUG_FindBreakpoint( &addr, DBG_BREAK );
843 if ( bp != -1 && bp != 0)
846 * If we have set a new value, then save it in the BP number.
848 if( count != 0 && mode == EXEC_CONT )
850 breakpoints[bp].skipcount = count;
852 mode = EXEC_STEPI_INSTR; /* If there's a breakpoint, skip it */
854 else
856 if( mode == EXEC_CONT && count > 1 )
858 DEBUG_Printf(DBG_CHN_MESG, "Not stopped at any breakpoint; argument ignored.\n");
862 if( mode == EXEC_FINISH && DEBUG_IsFctReturn() )
864 mode = ret_mode = EXEC_STEPI_INSTR;
867 instr = DEBUG_ToLinear( &addr );
868 DEBUG_READ_MEM((void*)instr, &ch, sizeof(ch));
870 * See if the function we are stepping into has debug info
871 * and line numbers. If not, then we step over it instead.
872 * FIXME - we need to check for things like thunks or trampolines,
873 * as the actual function may in fact have debug info.
875 if( ch == 0xe8 )
877 DEBUG_READ_MEM((void*)(instr + 1), &delta, sizeof(delta));
878 addr2 = addr;
879 DEBUG_Disasm(&addr2, FALSE);
880 addr2.off += delta;
882 status = DEBUG_CheckLinenoStatus(&addr2);
884 * Anytime we have a trampoline, step over it.
886 if( ((mode == EXEC_STEP_OVER) || (mode == EXEC_STEPI_OVER))
887 && status == FUNC_IS_TRAMPOLINE )
889 #if 0
890 DEBUG_Printf(DBG_CHN_MESG, "Not stepping into trampoline at %x (no lines)\n",
891 addr2.off);
892 #endif
893 mode = EXEC_STEP_OVER_TRAMPOLINE;
896 if( mode == EXEC_STEP_INSTR && status == FUNC_HAS_NO_LINES )
898 #if 0
899 DEBUG_Printf(DBG_CHN_MESG, "Not stepping into function at %x (no lines)\n",
900 addr2.off);
901 #endif
902 mode = EXEC_STEP_OVER;
907 if( mode == EXEC_STEP_INSTR )
909 if( DEBUG_CheckLinenoStatus(&addr) == FUNC_HAS_NO_LINES )
911 DEBUG_Printf(DBG_CHN_MESG, "Single stepping until exit from function, \n");
912 DEBUG_Printf(DBG_CHN_MESG, "which has no line number information.\n");
914 ret_mode = mode = EXEC_FINISH;
918 switch(mode)
920 case EXEC_CONT: /* Continuous execution */
921 case EXEC_PASS: /* Continue, passing exception */
922 #ifdef __i386__
923 DEBUG_context.EFlags &= ~STEP_FLAG;
924 #endif
925 DEBUG_SetBreakpoints( TRUE );
926 break;
928 case EXEC_STEP_OVER_TRAMPOLINE:
930 * This is the means by which we step over our conversion stubs
931 * in callfrom*.s and callto*.s. We dig the appropriate address
932 * off the stack, and we set the breakpoint there instead of the
933 * address just after the call.
935 #ifdef __i386__
936 DEBUG_READ_MEM((void*)(DEBUG_context.Esp +
937 2 * sizeof(unsigned int)),
938 &addr.off, sizeof(addr.off));
939 DEBUG_context.EFlags &= ~STEP_FLAG;
940 #endif
941 breakpoints[0].addr = addr;
942 breakpoints[0].enabled = TRUE;
943 breakpoints[0].refcount = 1;
944 breakpoints[0].skipcount = 0;
945 DEBUG_READ_MEM((void*)DEBUG_ToLinear( &addr ), &breakpoints[0].u.b.opcode,
946 sizeof(char));
947 DEBUG_SetBreakpoints( TRUE );
948 break;
950 case EXEC_FINISH:
951 case EXEC_STEPI_OVER: /* Stepping over a call */
952 case EXEC_STEP_OVER: /* Stepping over a call */
953 if (DEBUG_IsStepOverInstr())
955 #ifdef __i386__
956 DEBUG_context.EFlags &= ~STEP_FLAG;
957 #endif
958 DEBUG_Disasm(&addr, FALSE);
959 breakpoints[0].addr = addr;
960 breakpoints[0].enabled = TRUE;
961 breakpoints[0].refcount = 1;
962 breakpoints[0].skipcount = 0;
963 DEBUG_READ_MEM((void*)DEBUG_ToLinear( &addr ), &breakpoints[0].u.b.opcode,
964 sizeof(char));
965 DEBUG_SetBreakpoints( TRUE );
966 break;
968 /* else fall through to single-stepping */
970 case EXEC_STEP_INSTR: /* Single-stepping an instruction */
971 case EXEC_STEPI_INSTR: /* Single-stepping an instruction */
972 #ifdef __i386__
973 DEBUG_context.EFlags |= STEP_FLAG;
974 #endif
975 break;
976 default:
977 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
979 DEBUG_CurrThread->stepOverBP = breakpoints[0];
980 return ret_mode;
984 DEBUG_AddBPCondition(int num, struct expr * exp)
986 if ((num <= 0) || (num >= next_bp) || !breakpoints[num].refcount)
988 DEBUG_Printf( DBG_CHN_MESG, "Invalid breakpoint number %d\n", num );
989 return FALSE;
992 if( breakpoints[num].condition != NULL )
994 DEBUG_FreeExpr(breakpoints[num].condition);
995 breakpoints[num].condition = NULL;
998 if( exp != NULL )
1000 breakpoints[num].condition = DEBUG_CloneExpr(exp);
1003 return TRUE;