Remove the last remaining wine options when running programs.
[wine/hacks.git] / programs / winedbg / break.c
blob55c1544a7d98204cd1b3cf104adf182eb6a9030e
1 /*
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
23 #include "config.h"
24 #include "debugger.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
29 #ifdef __i386__
30 #define DR7_CONTROL_SHIFT 16
31 #define DR7_CONTROL_SIZE 4
33 #define DR7_RW_EXECUTE (0x0)
34 #define DR7_RW_WRITE (0x1)
35 #define DR7_RW_READ (0x3)
37 #define DR7_LEN_1 (0x0)
38 #define DR7_LEN_2 (0x4)
39 #define DR7_LEN_4 (0xC)
41 #define DR7_LOCAL_ENABLE_SHIFT 0
42 #define DR7_GLOBAL_ENABLE_SHIFT 1
43 #define DR7_ENABLE_SIZE 2
45 #define DR7_LOCAL_ENABLE_MASK (0x55)
46 #define DR7_GLOBAL_ENABLE_MASK (0xAA)
48 #define DR7_CONTROL_RESERVED (0xFC00)
49 #define DR7_LOCAL_SLOWDOWN (0x100)
50 #define DR7_GLOBAL_SLOWDOWN (0x200)
52 #define DR7_ENABLE_MASK(dr) (1<<(DR7_LOCAL_ENABLE_SHIFT+DR7_ENABLE_SIZE*(dr)))
53 #define IS_DR7_SET(ctrl,dr) ((ctrl)&DR7_ENABLE_MASK(dr))
54 #define INT3 0xcc /* int 3 opcode */
55 #endif
57 #define MAX_BREAKPOINTS 100
59 static DBG_BREAKPOINT breakpoints[MAX_BREAKPOINTS];
61 static int next_bp = 1; /* breakpoint 0 is reserved for step-over */
63 /***********************************************************************
64 * DEBUG_IsStepOverInstr
66 * Determine if the instruction at CS:EIP is an instruction that
67 * we need to step over (like a call or a repetitive string move).
69 static BOOL DEBUG_IsStepOverInstr(void)
71 #ifdef __i386__
72 BYTE* instr;
73 BYTE ch;
74 DBG_ADDR addr;
76 addr.seg = DEBUG_context.SegCs;
77 addr.off = DEBUG_context.Eip;
78 /* FIXME: old code was using V86BASE(DEBUG_context)
79 * instead of passing through DOSMEM_MemoryBase
81 instr = (BYTE*)DEBUG_ToLinear(&addr);
83 for (;;)
85 if (!DEBUG_READ_MEM(instr, &ch, sizeof(ch)))
86 return FALSE;
88 switch (ch)
90 /* Skip all prefixes */
92 case 0x2e: /* cs: */
93 case 0x36: /* ss: */
94 case 0x3e: /* ds: */
95 case 0x26: /* es: */
96 case 0x64: /* fs: */
97 case 0x65: /* gs: */
98 case 0x66: /* opcode size prefix */
99 case 0x67: /* addr size prefix */
100 case 0xf0: /* lock */
101 case 0xf2: /* repne */
102 case 0xf3: /* repe */
103 instr++;
104 continue;
106 /* Handle call instructions */
108 case 0xcd: /* int <intno> */
109 case 0xe8: /* call <offset> */
110 case 0x9a: /* lcall <seg>:<off> */
111 return TRUE;
113 case 0xff: /* call <regmodrm> */
114 if (!DEBUG_READ_MEM(instr + 1, &ch, sizeof(ch)))
115 return FALSE;
116 return (((ch & 0x38) == 0x10) || ((ch & 0x38) == 0x18));
118 /* Handle string instructions */
120 case 0x6c: /* insb */
121 case 0x6d: /* insw */
122 case 0x6e: /* outsb */
123 case 0x6f: /* outsw */
124 case 0xa4: /* movsb */
125 case 0xa5: /* movsw */
126 case 0xa6: /* cmpsb */
127 case 0xa7: /* cmpsw */
128 case 0xaa: /* stosb */
129 case 0xab: /* stosw */
130 case 0xac: /* lodsb */
131 case 0xad: /* lodsw */
132 case 0xae: /* scasb */
133 case 0xaf: /* scasw */
134 return TRUE;
136 default:
137 return FALSE;
140 #else
141 return FALSE;
142 #endif
146 /***********************************************************************
147 * DEBUG_IsFctReturn
149 * Determine if the instruction at CS:EIP is an instruction that
150 * is a function return.
152 BOOL DEBUG_IsFctReturn(void)
154 #ifdef __i386__
155 BYTE* instr;
156 BYTE ch;
157 DBG_ADDR addr;
159 addr.seg = DEBUG_context.SegCs;
160 addr.off = DEBUG_context.Eip;
161 /* FIXME: old code was using V86BASE(DEBUG_context)
162 * instead of passing through DOSMEM_MemoryBase
164 instr = (BYTE*)DEBUG_ToLinear(&addr);
166 if (!DEBUG_READ_MEM(instr, &ch, sizeof(ch)))
167 return FALSE;
169 return (ch == 0xc2) || (ch == 0xc3);
170 #else
171 return FALSE;
172 #endif
176 /***********************************************************************
177 * DEBUG_SetBreakpoints
179 * Set or remove all the breakpoints.
181 void DEBUG_SetBreakpoints( BOOL set )
183 int i;
185 #ifdef __i386__
186 DEBUG_context.Dr7 &= ~DR7_LOCAL_ENABLE_MASK;
187 #endif
189 for (i = 0; i < next_bp; i++)
191 if (!(breakpoints[i].refcount && breakpoints[i].enabled))
192 continue;
194 switch (breakpoints[i].type) {
195 case DBG_BREAK:
197 #ifdef __i386__
198 char ch = set ? INT3 : breakpoints[i].u.b.opcode;
200 if (!DEBUG_WRITE_MEM( (void*)DEBUG_ToLinear(&breakpoints[i].addr),
201 &ch, sizeof(ch) ))
203 DEBUG_Printf("Invalid address for breakpoint %d, disabling it\n", i);
204 breakpoints[i].enabled = FALSE;
206 #endif
208 break;
209 case DBG_WATCH:
210 if (set)
212 #ifdef __i386__
213 DWORD bits;
214 int reg = breakpoints[i].u.w.reg;
215 LPDWORD lpdr = NULL;
217 switch (reg)
219 case 0: lpdr = &DEBUG_context.Dr0; break;
220 case 1: lpdr = &DEBUG_context.Dr1; break;
221 case 2: lpdr = &DEBUG_context.Dr2; break;
222 case 3: lpdr = &DEBUG_context.Dr3; break;
223 default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
226 *lpdr = DEBUG_ToLinear(&breakpoints[i].addr);
227 bits = (breakpoints[i].u.w.rw) ? DR7_RW_WRITE : DR7_RW_READ;
228 switch (breakpoints[i].u.w.len + 1)
230 case 4: bits |= DR7_LEN_4; break;
231 case 2: bits |= DR7_LEN_2; break;
232 case 1: bits |= DR7_LEN_1; break;
233 default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
236 DEBUG_context.Dr7 &= ~(0x0F << (DR7_CONTROL_SHIFT + DR7_CONTROL_SIZE * reg));
237 DEBUG_context.Dr7 |= bits << (DR7_CONTROL_SHIFT + DR7_CONTROL_SIZE * reg);
238 DEBUG_context.Dr7 |= DR7_ENABLE_MASK(reg) | DR7_LOCAL_SLOWDOWN;
239 #endif
241 break;
246 /***********************************************************************
247 * DEBUG_FindBreakpoint
249 * Find the breakpoint for a given address. Return the breakpoint
250 * number or -1 if none.
251 * If type is DBG_BREAKPOINT, addr is a complete addr
252 * If type is DBG_WATCHPOINT, only addr.off is meaningful and contains
253 * linear address
255 static int DEBUG_FindBreakpoint( const DBG_ADDR *addr, int type )
257 int i;
259 for (i = 0; i < next_bp; i++)
261 if (breakpoints[i].refcount && breakpoints[i].enabled &&
262 breakpoints[i].type == type )
264 if ((type == DBG_BREAK &&
265 breakpoints[i].addr.seg == addr->seg &&
266 breakpoints[i].addr.off == addr->off) ||
267 (type == DBG_WATCH &&
268 DEBUG_ToLinear(&breakpoints[i].addr) == addr->off))
269 return i;
272 return -1;
275 /***********************************************************************
276 * DEBUG_InitXPoint
278 * Find an empty slot in BP table to add a new break/watch point
280 static int DEBUG_InitXPoint(int type, const DBG_ADDR* addr)
282 int num;
284 for (num = (next_bp < MAX_BREAKPOINTS) ? next_bp++ : 1;
285 num < MAX_BREAKPOINTS; num++)
287 if (breakpoints[num].refcount == 0)
289 breakpoints[num].refcount = 1;
290 breakpoints[num].enabled = TRUE;
291 breakpoints[num].type = type;
292 breakpoints[num].skipcount = 0;
293 breakpoints[num].addr = *addr;
294 switch (DEBUG_GetSelectorType( addr->seg ))
296 case MODE_32:
297 breakpoints[num].is32 = 1;
298 break;
299 case MODE_VM86:
300 case MODE_16:
301 breakpoints[num].is32 = 0;
302 break;
303 default:
304 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
306 return num;
310 DEBUG_Printf("Too many breakpoints. Please delete some.\n");
311 return -1;
314 /***********************************************************************
315 * DEBUG_GetWatchedValue
317 * Returns the value watched by watch point 'num'.
319 static BOOL DEBUG_GetWatchedValue( int num, LPDWORD val )
321 BYTE buf[4];
323 if (!DEBUG_READ_MEM((void*)DEBUG_ToLinear(&breakpoints[num].addr),
324 buf, breakpoints[num].u.w.len + 1))
325 return FALSE;
327 switch (breakpoints[num].u.w.len + 1)
329 case 4: *val = *(DWORD*)buf; break;
330 case 2: *val = *(WORD*)buf; break;
331 case 1: *val = *(BYTE*)buf; break;
332 default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
334 return TRUE;
337 /***********************************************************************
338 * DEBUG_AddBreakpoint
340 * Add a breakpoint.
342 BOOL DEBUG_AddBreakpoint( const DBG_VALUE *value, BOOL (*func)(void), BOOL verbose )
344 int num;
345 BYTE ch;
347 if ((num = DEBUG_FindBreakpoint(&value->addr, DBG_BREAK)) >= 1)
349 breakpoints[num].refcount++;
350 return TRUE;
353 if (!DEBUG_READ_MEM((void*)DEBUG_ToLinear( &value->addr ), &ch, sizeof(ch)))
355 if (verbose)
356 DEBUG_Printf("Invalid address, can't set breakpoint\n");
357 return FALSE;
360 if ((num = DEBUG_InitXPoint(DBG_BREAK, &value->addr)) == -1)
361 return FALSE;
363 breakpoints[num].u.b.opcode = ch;
364 breakpoints[num].u.b.func = func;
366 DEBUG_Printf("Breakpoint %d at ", num);
367 DEBUG_PrintAddress( &breakpoints[num].addr, breakpoints[num].is32 ? MODE_32 : MODE_16,
368 TRUE );
369 DEBUG_Printf("\n");
371 return TRUE;
374 /***********************************************************************
375 * DEBUG_AddBreakpointFromValue
377 * Add a breakpoint.
379 BOOL DEBUG_AddBreakpointFromValue( const DBG_VALUE *_value )
381 DBG_VALUE value = *_value;
383 if (value.type != NULL && value.type == DEBUG_GetBasicType(DT_BASIC_CONST_INT) && value.cookie == DV_HOST)
386 * We know that we have the actual offset stored somewhere
387 * else in 32-bit space. Grab it, and we
388 * should be all set.
390 unsigned int seg2 = value.addr.seg;
391 value.addr.seg = 0;
392 value.addr.off = DEBUG_GetExprValue(&value, NULL);
393 value.addr.seg = seg2;
394 value.cookie = DV_TARGET;
397 if (!DEBUG_AddBreakpoint( &value, NULL, TRUE ))
399 if (!DBG_IVAR(CanDeferOnBPByAddr))
401 DEBUG_Printf("Invalid address, can't set breakpoint\n"
402 "You can turn on deferring breakpoints by address by setting $CanDeferOnBPByAddr to 1\n");
403 return FALSE;
405 DEBUG_Printf("Unable to add breakpoint, will check again any time a new DLL is loaded\n");
406 DEBUG_CurrProcess->delayed_bp = DBG_realloc(DEBUG_CurrProcess->delayed_bp,
407 sizeof(DBG_DELAYED_BP) * ++DEBUG_CurrProcess->num_delayed_bp);
409 DEBUG_CurrProcess->delayed_bp[DEBUG_CurrProcess->num_delayed_bp - 1].is_symbol = FALSE;
410 DEBUG_CurrProcess->delayed_bp[DEBUG_CurrProcess->num_delayed_bp - 1].u.value = value;
411 return TRUE;
414 return TRUE;
417 /***********************************************************************
418 * DEBUG_AddBreakpointFromId
420 * Add a breakpoint from a function name (and eventually a line #)
422 void DEBUG_AddBreakpointFromId(const char *name, int lineno)
424 DBG_VALUE value;
425 int i;
427 switch (DEBUG_GetSymbolValue(name, lineno, &value, TRUE))
429 case gsv_found:
430 DEBUG_AddBreakpoint(&value, NULL, TRUE);
431 return;
432 case gsv_unknown:
433 break;
434 case gsv_aborted: /* user aborted symbol lookup */
435 return;
438 DEBUG_Printf("Unable to add breakpoint, will check again when a new DLL is loaded\n");
439 for (i = 0; i < DEBUG_CurrProcess->num_delayed_bp; i++)
441 if (DEBUG_CurrProcess->delayed_bp[i].is_symbol &&
442 !strcmp(name, DEBUG_CurrProcess->delayed_bp[i].u.symbol.name) &&
443 lineno == DEBUG_CurrProcess->delayed_bp[i].u.symbol.lineno)
444 return;
446 DEBUG_CurrProcess->delayed_bp = DBG_realloc(DEBUG_CurrProcess->delayed_bp,
447 sizeof(DBG_DELAYED_BP) * ++DEBUG_CurrProcess->num_delayed_bp);
449 DEBUG_CurrProcess->delayed_bp[DEBUG_CurrProcess->num_delayed_bp - 1].is_symbol = TRUE;
450 DEBUG_CurrProcess->delayed_bp[DEBUG_CurrProcess->num_delayed_bp - 1].u.symbol.name = strcpy(DBG_alloc(strlen(name) + 1), name);
451 DEBUG_CurrProcess->delayed_bp[DEBUG_CurrProcess->num_delayed_bp - 1].u.symbol.lineno = lineno;
454 /***********************************************************************
455 * DEBUG_AddBreakpointFromLineno
457 * Add a breakpoint from a line number in current file
459 void DEBUG_AddBreakpointFromLineno(int lineno)
461 DBG_VALUE value;
463 DEBUG_GetCurrentAddress(&value.addr);
465 if (lineno != -1)
467 struct name_hash* nh;
469 DEBUG_FindNearestSymbol(&value.addr, TRUE, &nh, 0, NULL);
470 if (nh == NULL)
472 DEBUG_Printf("Unable to add breakpoint\n");
473 return;
475 if (!DEBUG_GetLineNumberAddr(nh, lineno, &value.addr, TRUE))
477 DEBUG_Printf("Unknown line number\n"
478 "(either out of file, or no code at given line number)\n");
479 return;
483 value.type = NULL;
484 value.cookie = DV_TARGET;
485 DEBUG_AddBreakpoint( &value, NULL, TRUE );
488 /***********************************************************************
489 * DEBUG_CheckDelayedBP
491 * Check is a registered delayed BP is now available.
493 void DEBUG_CheckDelayedBP(void)
495 DBG_VALUE value;
496 int i;
497 DBG_DELAYED_BP* dbp = DEBUG_CurrProcess->delayed_bp;
499 for (i = 0; i < DEBUG_CurrProcess->num_delayed_bp; i++)
501 if (dbp[i].is_symbol)
503 if (DEBUG_GetSymbolValue(dbp[i].u.symbol.name, dbp[i].u.symbol.lineno, &value, TRUE) != gsv_found)
504 continue;
506 else
507 value = dbp[i].u.value;
508 WINE_TRACE("trying to add delayed %s-bp\n", dbp[i].is_symbol ? "S" : "A");
509 if (!dbp[i].is_symbol)
510 WINE_TRACE("\t%04x %04lx:%08lx\n",
511 dbp[i].u.value.cookie,
512 dbp[i].u.value.addr.seg,
513 dbp[i].u.value.addr.off);
514 else
515 WINE_TRACE("\t'%s' @ %d\n",
516 dbp[i].u.symbol.name, dbp[i].u.symbol.lineno);
518 if (DEBUG_AddBreakpoint(&value, NULL, FALSE))
519 memmove(&dbp[i], &dbp[i+1], (--DEBUG_CurrProcess->num_delayed_bp - i) * sizeof(*dbp));
523 /***********************************************************************
524 * DEBUG_AddWatchpoint
526 * Add a watchpoint.
528 void DEBUG_AddWatchpoint( const DBG_VALUE *_value, BOOL is_write )
530 DBG_VALUE value = *_value;
531 int num, reg = -1;
532 unsigned seg2;
533 DWORD mask = 0;
535 assert(_value->cookie == DV_TARGET || _value->cookie == DV_HOST);
537 #ifdef __i386__
538 DEBUG_FixAddress( &value.addr, DEBUG_context.SegCs );
539 #endif
541 if ( value.type != NULL && value.type == DEBUG_GetBasicType(DT_BASIC_CONST_INT) )
544 * We know that we have the actual offset stored somewhere
545 * else in 32-bit space. Grab it, and we
546 * should be all set.
548 seg2 = value.addr.seg;
549 value.addr.seg = 0;
550 value.addr.off = DEBUG_GetExprValue(&value, NULL);
551 value.addr.seg = seg2;
554 for (num = 1; num < next_bp; num++)
556 if (breakpoints[num].refcount && breakpoints[num].enabled &&
557 breakpoints[num].type == DBG_WATCH) {
558 mask |= (1 << breakpoints[num].u.w.reg);
561 #ifdef __i386__
562 for (reg = 0; reg < 4 && (mask & (1 << reg)); reg++);
563 if (reg == 4)
565 DEBUG_Printf("All i386 hardware watchpoints have been set. Delete some\n");
566 return;
568 #endif
570 if ((num = DEBUG_InitXPoint(DBG_WATCH, &value.addr)) == -1)
571 return;
573 breakpoints[num].u.w.len = 4 - 1;
574 if (_value->type && DEBUG_GetObjectSize(_value->type) < 4)
575 breakpoints[num].u.w.len = 2 - 1;
577 if (!DEBUG_GetWatchedValue( num, &breakpoints[num].u.w.oldval))
579 DEBUG_Printf("Bad address. Watchpoint not set\n");
580 breakpoints[num].refcount = 0;
582 else
584 breakpoints[num].u.w.rw = (is_write) ? TRUE : FALSE;
585 breakpoints[reg].u.w.reg = reg;
587 DEBUG_Printf("Watchpoint %d at ", num);
588 DEBUG_PrintAddress( &breakpoints[num].addr, breakpoints[num].is32 ? MODE_32 : MODE_16, TRUE );
589 DEBUG_Printf("\n");
593 /***********************************************************************
594 * DEBUG_AddWathpointFromId
596 * Add a watchpoint from a symbol name (and eventually a line #)
598 void DEBUG_AddWatchpointFromId(const char *name)
600 DBG_VALUE value;
602 switch (DEBUG_GetSymbolValue(name, -1, &value, TRUE))
604 case gsv_found:
605 DEBUG_AddWatchpoint( &value, 1 );
606 break;
607 case gsv_unknown:
608 DEBUG_Printf("Unable to add watchpoint\n");
609 break;
610 case gsv_aborted: /* user aborted symbol lookup */
611 break;
615 /***********************************************************************
616 * DEBUG_DelBreakpoint
618 * Delete a breakpoint.
620 void DEBUG_DelBreakpoint( int num )
622 if ((num <= 0) || (num >= next_bp) || breakpoints[num].refcount == 0)
624 DEBUG_Printf("Invalid breakpoint number %d\n", num);
625 return;
628 if (--breakpoints[num].refcount > 0)
629 return;
631 if (breakpoints[num].condition != NULL)
633 DEBUG_FreeExpr(breakpoints[num].condition);
634 breakpoints[num].condition = NULL;
637 breakpoints[num].enabled = FALSE;
638 breakpoints[num].refcount = 0;
639 breakpoints[num].skipcount = 0;
642 /***********************************************************************
643 * DEBUG_EnableBreakpoint
645 * Enable or disable a break point.
647 void DEBUG_EnableBreakpoint( int num, BOOL enable )
649 if ((num <= 0) || (num >= next_bp) || breakpoints[num].refcount == 0)
651 DEBUG_Printf("Invalid breakpoint number %d\n", num);
652 return;
654 breakpoints[num].enabled = (enable) ? TRUE : FALSE;
655 breakpoints[num].skipcount = 0;
659 /***********************************************************************
660 * DEBUG_FindTriggeredWatchpoint
662 * Lookup the watchpoints to see if one has been triggered
663 * Return >= (watch point index) if one is found and *oldval is set to
664 * the value watched before the TRAP
665 * Return -1 if none found (*oldval is undetermined)
667 * Unfortunately, Linux does *NOT* (A REAL PITA) report with ptrace
668 * the DR6 register value, so we have to look with our own need the
669 * cause of the TRAP.
670 * -EP
672 static int DEBUG_FindTriggeredWatchpoint(LPDWORD oldval)
674 int found = -1;
675 #ifdef __i386__
676 int i;
678 /* Method 1 => get triggered watchpoint from context (doesn't work on Linux
679 * 2.2.x). This should be fixed in >= 2.2.16
681 for (i = 0; i < next_bp; i++)
683 DWORD val = 0;
685 if (breakpoints[i].refcount && breakpoints[i].enabled &&
686 breakpoints[i].type == DBG_WATCH &&
687 (DEBUG_context.Dr6 & (1 << breakpoints[i].u.w.reg)))
689 DEBUG_context.Dr6 &= ~(1 << breakpoints[i].u.w.reg);
691 *oldval = breakpoints[i].u.w.oldval;
692 if (DEBUG_GetWatchedValue(i, &val)) {
693 breakpoints[i].u.w.oldval = val;
694 return i;
699 /* Method 1 failed, trying method 2 */
701 /* Method 2 => check if value has changed among registered watchpoints
702 * this really sucks, but this is how gdb 4.18 works on my linux box
703 * -EP
705 for (i = 0; i < next_bp; i++)
707 DWORD val = 0;
709 if (breakpoints[i].refcount && breakpoints[i].enabled &&
710 breakpoints[i].type == DBG_WATCH &&
711 DEBUG_GetWatchedValue(i, &val))
713 *oldval = breakpoints[i].u.w.oldval;
714 if (val != *oldval)
716 DEBUG_context.Dr6 &= ~(1 << breakpoints[i].u.w.reg);
717 breakpoints[i].u.w.oldval = val;
718 found = i;
719 /* cannot break, because two watch points may have been triggered on
720 * the same access
721 * only one will be reported to the user (FIXME ?)
726 #endif
727 return found;
730 /***********************************************************************
731 * DEBUG_InfoBreakpoints
733 * Display break points information.
735 void DEBUG_InfoBreakpoints(void)
737 int i;
739 DEBUG_Printf("Breakpoints:\n");
740 for (i = 1; i < next_bp; i++)
742 if (breakpoints[i].refcount && breakpoints[i].type == DBG_BREAK)
744 DEBUG_Printf("%d: %c ", i, breakpoints[i].enabled ? 'y' : 'n');
745 DEBUG_PrintAddress( &breakpoints[i].addr,
746 breakpoints[i].is32 ? MODE_32 : MODE_16, TRUE);
747 DEBUG_Printf(" (%u)\n", breakpoints[i].refcount );
748 if( breakpoints[i].condition != NULL )
750 DEBUG_Printf("\t\tstop when ");
751 DEBUG_DisplayExpr(breakpoints[i].condition);
752 DEBUG_Printf("\n");
756 DEBUG_Printf("Watchpoints:\n");
757 for (i = 1; i < next_bp; i++)
759 if (breakpoints[i].refcount && breakpoints[i].type == DBG_WATCH)
761 DEBUG_Printf("%d: %c ", i, breakpoints[i].enabled ? 'y' : 'n');
762 DEBUG_PrintAddress( &breakpoints[i].addr,
763 breakpoints[i].is32 ? MODE_32 : MODE_16, TRUE);
764 DEBUG_Printf(" on %d byte%s (%c)\n",
765 breakpoints[i].u.w.len + 1,
766 breakpoints[i].u.w.len > 0 ? "s" : "",
767 breakpoints[i].u.w.rw ? 'W' : 'R');
768 if( breakpoints[i].condition != NULL )
770 DEBUG_Printf("\t\tstop when ");
771 DEBUG_DisplayExpr(breakpoints[i].condition);
772 DEBUG_Printf("\n");
778 /***********************************************************************
779 * DEBUG_ShallBreak
781 * Check whether or not the condition (bp / skipcount) of a break/watch
782 * point are met.
784 static BOOL DEBUG_ShallBreak( int bpnum )
786 if ( breakpoints[bpnum].condition != NULL )
788 DBG_VALUE value = DEBUG_EvalExpr(breakpoints[bpnum].condition);
790 if ( value.type == NULL )
793 * Something wrong - unable to evaluate this expression.
795 DEBUG_Printf("Unable to evaluate expression ");
796 DEBUG_DisplayExpr(breakpoints[bpnum].condition);
797 DEBUG_Printf("\nTurning off condition\n");
798 DEBUG_AddBPCondition(bpnum, NULL);
800 else if( !DEBUG_GetExprValue( &value, NULL) )
802 return FALSE;
806 if ( breakpoints[bpnum].skipcount > 0 && --breakpoints[bpnum].skipcount > 0 )
807 return FALSE;
809 if ((breakpoints[bpnum].type == DBG_BREAK) && breakpoints[bpnum].u.b.func)
810 return breakpoints[bpnum].u.b.func();
811 return TRUE;
814 /***********************************************************************
815 * DEBUG_ShouldContinue
817 * Determine if we should continue execution after a SIGTRAP signal when
818 * executing in the given mode.
820 BOOL DEBUG_ShouldContinue( DBG_ADDR *addr, DWORD code, int * count )
822 int bpnum;
823 DWORD oldval;
824 int wpnum;
825 enum dbg_mode addr_mode;
826 struct symbol_info syminfo;
827 enum exec_mode mode = DEBUG_CurrThread->exec_mode;
829 #ifdef __i386__
830 /* If not single-stepping, back up over the int3 instruction */
831 if (code == EXCEPTION_BREAKPOINT)
833 DEBUG_context.Eip--;
834 addr->off--;
836 #endif
838 bpnum = DEBUG_FindBreakpoint( addr, DBG_BREAK );
839 breakpoints[0].enabled = FALSE; /* disable the step-over breakpoint */
841 if ((bpnum != 0) && (bpnum != -1))
843 if (!DEBUG_ShallBreak(bpnum)) return TRUE;
845 DEBUG_Printf("Stopped on breakpoint %d at ", bpnum);
846 syminfo = DEBUG_PrintAddress( &breakpoints[bpnum].addr,
847 breakpoints[bpnum].is32 ? MODE_32 : MODE_16, TRUE );
848 DEBUG_Printf("\n");
850 if( syminfo.list.sourcefile != NULL )
851 DEBUG_List(&syminfo.list, NULL, 0);
852 return FALSE;
855 wpnum = DEBUG_FindTriggeredWatchpoint(&oldval);
856 if ((wpnum != 0) && (wpnum != -1))
858 /* If not single-stepping, do not back up over the int3 instruction */
859 if (code == EXCEPTION_BREAKPOINT)
861 #ifdef __i386__
862 DEBUG_context.Eip++;
863 addr->off++;
864 #endif
866 if (!DEBUG_ShallBreak(wpnum)) return TRUE;
868 addr_mode = DEBUG_GetSelectorType( addr->seg );
869 DEBUG_Printf("Stopped on watchpoint %d at ", wpnum);
870 syminfo = DEBUG_PrintAddress( addr, addr_mode, TRUE );
872 DEBUG_Printf(" values: old=%lu new=%lu\n",
873 oldval, breakpoints[wpnum].u.w.oldval);
874 if (syminfo.list.sourcefile != NULL)
875 DEBUG_List(&syminfo.list, NULL, 0);
876 return FALSE;
880 * If our mode indicates that we are stepping line numbers,
881 * get the current function, and figure out if we are exactly
882 * on a line number or not.
884 if( mode == EXEC_STEP_OVER || mode == EXEC_STEP_INSTR )
886 if( DEBUG_CheckLinenoStatus(addr) == AT_LINENUMBER )
888 (*count)--;
891 else if( mode == EXEC_STEPI_OVER || mode == EXEC_STEPI_INSTR )
893 (*count)--;
896 if( *count > 0 || mode == EXEC_FINISH )
899 * We still need to execute more instructions.
901 return TRUE;
905 * If we are about to stop, then print out the source line if we
906 * have it.
908 if (mode != EXEC_CONT && mode != EXEC_FINISH)
910 DEBUG_FindNearestSymbol(addr, TRUE, NULL, 0, &syminfo.list);
911 if (syminfo.list.sourcefile != NULL)
913 DEBUG_List(&syminfo.list, NULL, 0);
917 #ifdef __i386__
918 /* If there's no breakpoint and we are not single-stepping, then
919 * either we must have encountered an int3 in the Windows program
920 * or someone is trying to stop us
921 * If the later, (no int3 opcode at current address) then stop,
922 * otherwise, let's skip it.
924 if ((bpnum == -1) && code == EXCEPTION_BREAKPOINT)
926 unsigned char c;
928 if (!DEBUG_READ_MEM(&addr, &c, 1)) c = 0xCC;
929 DEBUG_context.Eip++;
930 addr->off++;
931 if (c != 0xCC) return FALSE;
933 #endif
935 /* no breakpoint, continue if in continuous mode */
936 return (mode == EXEC_CONT || mode == EXEC_FINISH);
939 /***********************************************************************
940 * DEBUG_SuspendExecution
942 * Remove all breakpoints before entering the debug loop
944 void DEBUG_SuspendExecution( void )
946 DEBUG_SetBreakpoints( FALSE );
947 breakpoints[0] = DEBUG_CurrThread->stepOverBP;
950 /***********************************************************************
951 * DEBUG_RestartExecution
953 * Set the breakpoints to the correct state to restart execution
954 * in the given mode.
956 void DEBUG_RestartExecution( int count )
958 DBG_ADDR addr;
959 DBG_ADDR addr2;
960 int bp;
961 int delta;
962 int status;
963 enum exec_mode mode, ret_mode;
964 DWORD instr;
965 unsigned char ch;
967 DEBUG_GetCurrentAddress( &addr );
970 * This is the mode we will be running in after we finish. We would like
971 * to be able to modify this in certain cases.
973 ret_mode = mode = DEBUG_CurrThread->exec_mode;
975 bp = DEBUG_FindBreakpoint( &addr, DBG_BREAK );
976 if ( bp != -1 && bp != 0)
979 * If we have set a new value, then save it in the BP number.
981 if( count != 0 && mode == EXEC_CONT )
983 breakpoints[bp].skipcount = count;
985 mode = EXEC_STEPI_INSTR; /* If there's a breakpoint, skip it */
987 else
989 if( mode == EXEC_CONT && count > 1 )
991 DEBUG_Printf("Not stopped at any breakpoint; argument ignored.\n");
995 if( mode == EXEC_FINISH && DEBUG_IsFctReturn() )
997 mode = ret_mode = EXEC_STEPI_INSTR;
1000 instr = DEBUG_ToLinear( &addr );
1001 DEBUG_READ_MEM((void*)instr, &ch, sizeof(ch));
1003 * See if the function we are stepping into has debug info
1004 * and line numbers. If not, then we step over it instead.
1005 * FIXME - we need to check for things like thunks or trampolines,
1006 * as the actual function may in fact have debug info.
1008 if ( ch == 0xe8 )
1010 DEBUG_READ_MEM((void*)(instr + 1), &delta, sizeof(delta));
1011 addr2 = addr;
1012 DEBUG_Disasm(&addr2, FALSE);
1013 addr2.off += delta;
1015 status = DEBUG_CheckLinenoStatus(&addr2);
1017 * Anytime we have a trampoline, step over it.
1019 if( ((mode == EXEC_STEP_OVER) || (mode == EXEC_STEPI_OVER))
1020 && status == FUNC_IS_TRAMPOLINE )
1022 WINE_TRACE("Not stepping into trampoline at %lx (no lines)\n",
1023 addr2.off);
1024 mode = EXEC_STEP_OVER_TRAMPOLINE;
1027 if( mode == EXEC_STEP_INSTR && status == FUNC_HAS_NO_LINES )
1029 WINE_TRACE("Not stepping into function at %lx (no lines)\n",
1030 addr2.off);
1031 mode = EXEC_STEP_OVER;
1036 if( mode == EXEC_STEP_INSTR )
1038 if( DEBUG_CheckLinenoStatus(&addr) == FUNC_HAS_NO_LINES )
1040 DEBUG_Printf("Single stepping until exit from function, \n"
1041 "which has no line number information.\n");
1043 ret_mode = mode = EXEC_FINISH;
1047 switch(mode)
1049 case EXEC_CONT: /* Continuous execution */
1050 #ifdef __i386__
1051 DEBUG_context.EFlags &= ~STEP_FLAG;
1052 #endif
1053 DEBUG_SetBreakpoints( TRUE );
1054 break;
1056 case EXEC_STEP_OVER_TRAMPOLINE:
1058 * This is the means by which we step over our conversion stubs
1059 * in callfrom*.s and callto*.s. We dig the appropriate address
1060 * off the stack, and we set the breakpoint there instead of the
1061 * address just after the call.
1063 #ifdef __i386__
1064 DEBUG_READ_MEM((void*)(DEBUG_context.Esp +
1065 2 * sizeof(unsigned int)),
1066 &addr.off, sizeof(addr.off));
1067 DEBUG_context.EFlags &= ~STEP_FLAG;
1068 #endif
1069 breakpoints[0].addr = addr;
1070 breakpoints[0].enabled = TRUE;
1071 breakpoints[0].refcount = 1;
1072 breakpoints[0].skipcount = 0;
1073 DEBUG_READ_MEM((void*)DEBUG_ToLinear( &addr ), &breakpoints[0].u.b.opcode,
1074 sizeof(char));
1075 DEBUG_SetBreakpoints( TRUE );
1076 break;
1078 case EXEC_FINISH:
1079 case EXEC_STEPI_OVER: /* Stepping over a call */
1080 case EXEC_STEP_OVER: /* Stepping over a call */
1081 if (DEBUG_IsStepOverInstr())
1083 #ifdef __i386__
1084 DEBUG_context.EFlags &= ~STEP_FLAG;
1085 #endif
1086 DEBUG_Disasm(&addr, FALSE);
1087 breakpoints[0].addr = addr;
1088 breakpoints[0].enabled = TRUE;
1089 breakpoints[0].refcount = 1;
1090 breakpoints[0].skipcount = 0;
1091 DEBUG_READ_MEM((void*)DEBUG_ToLinear( &addr ), &breakpoints[0].u.b.opcode,
1092 sizeof(char));
1093 DEBUG_SetBreakpoints( TRUE );
1094 break;
1096 /* else fall through to single-stepping */
1098 case EXEC_STEP_INSTR: /* Single-stepping an instruction */
1099 case EXEC_STEPI_INSTR: /* Single-stepping an instruction */
1100 #ifdef __i386__
1101 DEBUG_context.EFlags |= STEP_FLAG;
1102 #endif
1103 break;
1104 default:
1105 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
1107 DEBUG_CurrThread->stepOverBP = breakpoints[0];
1108 DEBUG_CurrThread->exec_mode = ret_mode;
1112 DEBUG_AddBPCondition(int num, struct expr * exp)
1114 if ((num <= 0) || (num >= next_bp) || !breakpoints[num].refcount)
1116 DEBUG_Printf("Invalid breakpoint number %d\n", num);
1117 return FALSE;
1120 if( breakpoints[num].condition != NULL )
1122 DEBUG_FreeExpr(breakpoints[num].condition);
1123 breakpoints[num].condition = NULL;
1126 if( exp != NULL )
1128 breakpoints[num].condition = DEBUG_CloneExpr(exp);
1131 return TRUE;