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 if (DEBUG_GetSymbolValue(name
, lineno
, &value
, TRUE
))
426 DEBUG_AddBreakpoint(&value
, NULL
, TRUE
);
430 DEBUG_Printf(DBG_CHN_MESG
, "Unable to add breakpoint, will check again when a new DLL is loaded\n");
431 for (i
= 0; i
< DEBUG_CurrProcess
->num_delayed_bp
; i
++)
433 if (DEBUG_CurrProcess
->delayed_bp
[i
].is_symbol
&&
434 !strcmp(name
, DEBUG_CurrProcess
->delayed_bp
[i
].u
.symbol
.name
) &&
435 lineno
== DEBUG_CurrProcess
->delayed_bp
[i
].u
.symbol
.lineno
)
438 DEBUG_CurrProcess
->delayed_bp
= DBG_realloc(DEBUG_CurrProcess
->delayed_bp
,
439 sizeof(DBG_DELAYED_BP
) * ++DEBUG_CurrProcess
->num_delayed_bp
);
441 DEBUG_CurrProcess
->delayed_bp
[DEBUG_CurrProcess
->num_delayed_bp
- 1].is_symbol
= TRUE
;
442 DEBUG_CurrProcess
->delayed_bp
[DEBUG_CurrProcess
->num_delayed_bp
- 1].u
.symbol
.name
= strcpy(DBG_alloc(strlen(name
) + 1), name
);
443 DEBUG_CurrProcess
->delayed_bp
[DEBUG_CurrProcess
->num_delayed_bp
- 1].u
.symbol
.lineno
= lineno
;
446 /***********************************************************************
447 * DEBUG_AddBreakpointFromLineno
449 * Add a breakpoint from a line number in current file
451 void DEBUG_AddBreakpointFromLineno(int lineno
)
455 DEBUG_GetCurrentAddress(&value
.addr
);
459 struct name_hash
* nh
;
461 DEBUG_FindNearestSymbol(&value
.addr
, TRUE
, &nh
, 0, NULL
);
464 DEBUG_Printf(DBG_CHN_MESG
, "Unable to add breakpoint\n");
467 DEBUG_GetLineNumberAddr(nh
, lineno
, &value
.addr
, TRUE
);
471 value
.cookie
= DV_TARGET
;
472 DEBUG_AddBreakpoint( &value
,NULL
, TRUE
);
475 /***********************************************************************
476 * DEBUG_CheckDelayedBP
478 * Check is a registered delayed BP is now available.
480 void DEBUG_CheckDelayedBP(void)
484 DBG_DELAYED_BP
* dbp
= DEBUG_CurrProcess
->delayed_bp
;
486 for (i
= 0; i
< DEBUG_CurrProcess
->num_delayed_bp
; i
++)
488 if (dbp
[i
].is_symbol
)
490 if (!DEBUG_GetSymbolValue(dbp
[i
].u
.symbol
.name
, dbp
[i
].u
.symbol
.lineno
, &value
, TRUE
))
494 value
= dbp
[i
].u
.value
;
495 DEBUG_Printf(DBG_CHN_MESG
, "trying to add delayed %s-bp\n", dbp
[i
].is_symbol
? "S" : "A");
496 if (!dbp
[i
].is_symbol
)
497 DEBUG_Printf(DBG_CHN_MESG
, "\t%04x %04lx:%08lx\n",
498 dbp
[i
].u
.value
.cookie
,
499 dbp
[i
].u
.value
.addr
.seg
,
500 dbp
[i
].u
.value
.addr
.off
);
502 DEBUG_Printf(DBG_CHN_MESG
, "\t'%s' @ %d\n",
503 dbp
[i
].u
.symbol
.name
, dbp
[i
].u
.symbol
.lineno
);
505 if (DEBUG_AddBreakpoint(&value
, NULL
, FALSE
))
506 memmove(&dbp
[i
], &dbp
[i
+1], (--DEBUG_CurrProcess
->num_delayed_bp
- i
) * sizeof(*dbp
));
510 /***********************************************************************
511 * DEBUG_AddWatchpoint
515 void DEBUG_AddWatchpoint( const DBG_VALUE
*_value
, BOOL is_write
)
517 DBG_VALUE value
= *_value
;
522 assert(_value
->cookie
== DV_TARGET
|| _value
->cookie
== DV_HOST
);
525 DEBUG_FixAddress( &value
.addr
, DEBUG_context
.SegCs
);
528 if ( value
.type
!= NULL
&& value
.type
== DEBUG_GetBasicType(DT_BASIC_CONST_INT
) )
531 * We know that we have the actual offset stored somewhere
532 * else in 32-bit space. Grab it, and we
535 seg2
= value
.addr
.seg
;
537 value
.addr
.off
= DEBUG_GetExprValue(&value
, NULL
);
538 value
.addr
.seg
= seg2
;
541 for (num
= 1; num
< next_bp
; num
++)
543 if (breakpoints
[num
].refcount
&& breakpoints
[num
].enabled
&&
544 breakpoints
[num
].type
== DBG_WATCH
) {
545 mask
|= (1 << breakpoints
[num
].u
.w
.reg
);
549 for (reg
= 0; reg
< 4 && (mask
& (1 << reg
)); reg
++);
552 DEBUG_Printf(DBG_CHN_MESG
, "All i386 hardware watchpoints have been set. Delete some\n");
557 if ((num
= DEBUG_InitXPoint(DBG_WATCH
, &value
.addr
)) == -1)
560 breakpoints
[num
].u
.w
.len
= 4 - 1;
561 if (_value
->type
&& DEBUG_GetObjectSize(_value
->type
) < 4)
562 breakpoints
[num
].u
.w
.len
= 2 - 1;
564 if (!DEBUG_GetWatchedValue( num
, &breakpoints
[num
].u
.w
.oldval
))
566 DEBUG_Printf(DBG_CHN_MESG
, "Bad address. Watchpoint not set\n");
567 breakpoints
[num
].refcount
= 0;
571 breakpoints
[num
].u
.w
.rw
= (is_write
) ? TRUE
: FALSE
;
572 breakpoints
[reg
].u
.w
.reg
= reg
;
574 DEBUG_Printf( DBG_CHN_MESG
, "Watchpoint %d at ", num
);
575 DEBUG_PrintAddress( &breakpoints
[num
].addr
, breakpoints
[num
].is32
? MODE_32
: MODE_16
, TRUE
);
576 DEBUG_Printf( DBG_CHN_MESG
, "\n" );
580 /***********************************************************************
581 * DEBUG_AddWathpointFromId
583 * Add a watchpoint from a symbol name (and eventually a line #)
585 void DEBUG_AddWatchpointFromId(const char *name
)
589 if( DEBUG_GetSymbolValue(name
, -1, &value
, TRUE
) )
590 DEBUG_AddWatchpoint( &value
, 1 );
592 DEBUG_Printf(DBG_CHN_MESG
, "Unable to add watchpoint\n");
595 /***********************************************************************
596 * DEBUG_DelBreakpoint
598 * Delete a breakpoint.
600 void DEBUG_DelBreakpoint( int num
)
602 if ((num
<= 0) || (num
>= next_bp
) || breakpoints
[num
].refcount
== 0)
604 DEBUG_Printf( DBG_CHN_MESG
, "Invalid breakpoint number %d\n", num
);
608 if (--breakpoints
[num
].refcount
> 0)
611 if( breakpoints
[num
].condition
!= NULL
)
613 DEBUG_FreeExpr(breakpoints
[num
].condition
);
614 breakpoints
[num
].condition
= NULL
;
617 breakpoints
[num
].enabled
= FALSE
;
618 breakpoints
[num
].refcount
= 0;
619 breakpoints
[num
].skipcount
= 0;
622 /***********************************************************************
623 * DEBUG_EnableBreakpoint
625 * Enable or disable a break point.
627 void DEBUG_EnableBreakpoint( int num
, BOOL enable
)
629 if ((num
<= 0) || (num
>= next_bp
) || breakpoints
[num
].refcount
== 0)
631 DEBUG_Printf( DBG_CHN_MESG
, "Invalid breakpoint number %d\n", num
);
634 breakpoints
[num
].enabled
= (enable
) ? TRUE
: FALSE
;
635 breakpoints
[num
].skipcount
= 0;
639 /***********************************************************************
640 * DEBUG_FindTriggeredWatchpoint
642 * Lookup the watchpoints to see if one has been triggered
643 * Return >= (watch point index) if one is found and *oldval is set to
644 * the value watched before the TRAP
645 * Return -1 if none found (*oldval is undetermined)
647 * Unfortunately, Linux does *NOT* (A REAL PITA) report with ptrace
648 * the DR6 register value, so we have to look with our own need the
652 static int DEBUG_FindTriggeredWatchpoint(LPDWORD oldval
)
658 /* Method 1 => get triggered watchpoint from context (doesn't work on Linux
659 * 2.2.x). This should be fixed in >= 2.2.16
661 for (i
= 0; i
< next_bp
; i
++)
665 if (breakpoints
[i
].refcount
&& breakpoints
[i
].enabled
&&
666 breakpoints
[i
].type
== DBG_WATCH
&&
667 (DEBUG_context
.Dr6
& (1 << breakpoints
[i
].u
.w
.reg
)))
669 DEBUG_context
.Dr6
&= ~(1 << breakpoints
[i
].u
.w
.reg
);
671 *oldval
= breakpoints
[i
].u
.w
.oldval
;
672 if (DEBUG_GetWatchedValue(i
, &val
)) {
673 breakpoints
[i
].u
.w
.oldval
= val
;
679 /* Method 1 failed, trying method 2 */
681 /* Method 2 => check if value has changed among registered watchpoints
682 * this really sucks, but this is how gdb 4.18 works on my linux box
685 for (i
= 0; i
< next_bp
; i
++)
689 if (breakpoints
[i
].refcount
&& breakpoints
[i
].enabled
&&
690 breakpoints
[i
].type
== DBG_WATCH
&&
691 DEBUG_GetWatchedValue(i
, &val
))
693 *oldval
= breakpoints
[i
].u
.w
.oldval
;
696 DEBUG_context
.Dr6
&= ~(1 << breakpoints
[i
].u
.w
.reg
);
697 breakpoints
[i
].u
.w
.oldval
= val
;
699 /* cannot break, because two watch points may have been triggered on
701 * only one will be reported to the user (FIXME ?)
710 /***********************************************************************
711 * DEBUG_InfoBreakpoints
713 * Display break points information.
715 void DEBUG_InfoBreakpoints(void)
719 DEBUG_Printf( DBG_CHN_MESG
, "Breakpoints:\n" );
720 for (i
= 1; i
< next_bp
; i
++)
722 if (breakpoints
[i
].refcount
&& breakpoints
[i
].type
== DBG_BREAK
)
724 DEBUG_Printf( DBG_CHN_MESG
, "%d: %c ", i
, breakpoints
[i
].enabled
? 'y' : 'n');
725 DEBUG_PrintAddress( &breakpoints
[i
].addr
,
726 breakpoints
[i
].is32
? MODE_32
: MODE_16
, TRUE
);
727 DEBUG_Printf( DBG_CHN_MESG
, " (%u)\n", breakpoints
[i
].refcount
);
728 if( breakpoints
[i
].condition
!= NULL
)
730 DEBUG_Printf(DBG_CHN_MESG
, "\t\tstop when ");
731 DEBUG_DisplayExpr(breakpoints
[i
].condition
);
732 DEBUG_Printf(DBG_CHN_MESG
, "\n");
736 DEBUG_Printf( DBG_CHN_MESG
, "Watchpoints:\n" );
737 for (i
= 1; i
< next_bp
; i
++)
739 if (breakpoints
[i
].refcount
&& breakpoints
[i
].type
== DBG_WATCH
)
741 DEBUG_Printf( DBG_CHN_MESG
, "%d: %c ", i
, breakpoints
[i
].enabled
? 'y' : 'n');
742 DEBUG_PrintAddress( &breakpoints
[i
].addr
,
743 breakpoints
[i
].is32
? MODE_32
: MODE_16
, TRUE
);
744 DEBUG_Printf( DBG_CHN_MESG
, " on %d byte%s (%c)\n",
745 breakpoints
[i
].u
.w
.len
+ 1,
746 breakpoints
[i
].u
.w
.len
> 0 ? "s" : "",
747 breakpoints
[i
].u
.w
.rw
? 'W' : 'R');
748 if( breakpoints
[i
].condition
!= NULL
)
750 DEBUG_Printf(DBG_CHN_MESG
, "\t\tstop when ");
751 DEBUG_DisplayExpr(breakpoints
[i
].condition
);
752 DEBUG_Printf(DBG_CHN_MESG
, "\n");
758 /***********************************************************************
761 * Check whether or not the condition (bp / skipcount) of a break/watch
764 static BOOL
DEBUG_ShallBreak( int bpnum
)
766 if ( breakpoints
[bpnum
].condition
!= NULL
)
768 DBG_VALUE value
= DEBUG_EvalExpr(breakpoints
[bpnum
].condition
);
770 if ( value
.type
== NULL
)
773 * Something wrong - unable to evaluate this expression.
775 DEBUG_Printf(DBG_CHN_MESG
, "Unable to evaluate expression ");
776 DEBUG_DisplayExpr(breakpoints
[bpnum
].condition
);
777 DEBUG_Printf(DBG_CHN_MESG
, "\nTurning off condition\n");
778 DEBUG_AddBPCondition(bpnum
, NULL
);
780 else if( !DEBUG_GetExprValue( &value
, NULL
) )
786 if ( breakpoints
[bpnum
].skipcount
> 0 && --breakpoints
[bpnum
].skipcount
> 0 )
789 if ((breakpoints
[bpnum
].type
== DBG_BREAK
) && breakpoints
[bpnum
].u
.b
.func
)
790 return breakpoints
[bpnum
].u
.b
.func();
794 /***********************************************************************
795 * DEBUG_ShouldContinue
797 * Determine if we should continue execution after a SIGTRAP signal when
798 * executing in the given mode.
800 BOOL
DEBUG_ShouldContinue( DBG_ADDR
*addr
, DWORD code
, int * count
)
805 enum dbg_mode addr_mode
;
806 struct symbol_info syminfo
;
807 enum exec_mode mode
= DEBUG_CurrThread
->exec_mode
;
810 /* If not single-stepping, back up over the int3 instruction */
811 if (code
== EXCEPTION_BREAKPOINT
)
818 bpnum
= DEBUG_FindBreakpoint( addr
, DBG_BREAK
);
819 breakpoints
[0].enabled
= FALSE
; /* disable the step-over breakpoint */
821 if ((bpnum
!= 0) && (bpnum
!= -1))
823 if (!DEBUG_ShallBreak(bpnum
)) return TRUE
;
825 DEBUG_Printf( DBG_CHN_MESG
, "Stopped on breakpoint %d at ", bpnum
);
826 syminfo
= DEBUG_PrintAddress( &breakpoints
[bpnum
].addr
,
827 breakpoints
[bpnum
].is32
? MODE_32
: MODE_16
, TRUE
);
828 DEBUG_Printf( DBG_CHN_MESG
, "\n" );
830 if( syminfo
.list
.sourcefile
!= NULL
)
831 DEBUG_List(&syminfo
.list
, NULL
, 0);
835 wpnum
= DEBUG_FindTriggeredWatchpoint(&oldval
);
836 if ((wpnum
!= 0) && (wpnum
!= -1))
838 /* If not single-stepping, do not back up over the int3 instruction */
839 if (code
== EXCEPTION_BREAKPOINT
)
846 if (!DEBUG_ShallBreak(wpnum
)) return TRUE
;
848 addr_mode
= DEBUG_GetSelectorType( addr
->seg
);
849 DEBUG_Printf(DBG_CHN_MESG
, "Stopped on watchpoint %d at ", wpnum
);
850 syminfo
= DEBUG_PrintAddress( addr
, addr_mode
, TRUE
);
852 DEBUG_Printf(DBG_CHN_MESG
, " values: old=%lu new=%lu\n",
853 oldval
, breakpoints
[wpnum
].u
.w
.oldval
);
854 if (syminfo
.list
.sourcefile
!= NULL
)
855 DEBUG_List(&syminfo
.list
, NULL
, 0);
860 * If our mode indicates that we are stepping line numbers,
861 * get the current function, and figure out if we are exactly
862 * on a line number or not.
864 if( mode
== EXEC_STEP_OVER
|| mode
== EXEC_STEP_INSTR
)
866 if( DEBUG_CheckLinenoStatus(addr
) == AT_LINENUMBER
)
871 else if( mode
== EXEC_STEPI_OVER
|| mode
== EXEC_STEPI_INSTR
)
876 if( *count
> 0 || mode
== EXEC_FINISH
)
879 * We still need to execute more instructions.
885 * If we are about to stop, then print out the source line if we
888 if (mode
!= EXEC_CONT
&& mode
!= EXEC_FINISH
)
890 DEBUG_FindNearestSymbol(addr
, TRUE
, NULL
, 0, &syminfo
.list
);
891 if (syminfo
.list
.sourcefile
!= NULL
)
893 DEBUG_List(&syminfo
.list
, NULL
, 0);
898 /* If there's no breakpoint and we are not single-stepping, then
899 * either we must have encountered an int3 in the Windows program
900 * or someone is trying to stop us
901 * If the later, (no int3 opcode at current address) then stop,
902 * otherwise, let's skip it.
904 if ((bpnum
== -1) && code
== EXCEPTION_BREAKPOINT
)
908 if (!DEBUG_READ_MEM(&addr
, &c
, 1)) c
= 0xCC;
911 if (c
!= 0xCC) return FALSE
;
915 /* no breakpoint, continue if in continuous mode */
916 return (mode
== EXEC_CONT
|| mode
== EXEC_FINISH
);
919 /***********************************************************************
920 * DEBUG_SuspendExecution
922 * Remove all breakpoints before entering the debug loop
924 void DEBUG_SuspendExecution( void )
926 DEBUG_SetBreakpoints( FALSE
);
927 breakpoints
[0] = DEBUG_CurrThread
->stepOverBP
;
930 /***********************************************************************
931 * DEBUG_RestartExecution
933 * Set the breakpoints to the correct state to restart execution
936 void DEBUG_RestartExecution( int count
)
943 enum exec_mode mode
, ret_mode
;
947 DEBUG_GetCurrentAddress( &addr
);
950 * This is the mode we will be running in after we finish. We would like
951 * to be able to modify this in certain cases.
953 ret_mode
= mode
= DEBUG_CurrThread
->exec_mode
;
955 bp
= DEBUG_FindBreakpoint( &addr
, DBG_BREAK
);
956 if ( bp
!= -1 && bp
!= 0)
959 * If we have set a new value, then save it in the BP number.
961 if( count
!= 0 && mode
== EXEC_CONT
)
963 breakpoints
[bp
].skipcount
= count
;
965 mode
= EXEC_STEPI_INSTR
; /* If there's a breakpoint, skip it */
969 if( mode
== EXEC_CONT
&& count
> 1 )
971 DEBUG_Printf(DBG_CHN_MESG
, "Not stopped at any breakpoint; argument ignored.\n");
975 if( mode
== EXEC_FINISH
&& DEBUG_IsFctReturn() )
977 mode
= ret_mode
= EXEC_STEPI_INSTR
;
980 instr
= DEBUG_ToLinear( &addr
);
981 DEBUG_READ_MEM((void*)instr
, &ch
, sizeof(ch
));
983 * See if the function we are stepping into has debug info
984 * and line numbers. If not, then we step over it instead.
985 * FIXME - we need to check for things like thunks or trampolines,
986 * as the actual function may in fact have debug info.
990 DEBUG_READ_MEM((void*)(instr
+ 1), &delta
, sizeof(delta
));
992 DEBUG_Disasm(&addr2
, FALSE
);
995 status
= DEBUG_CheckLinenoStatus(&addr2
);
997 * Anytime we have a trampoline, step over it.
999 if( ((mode
== EXEC_STEP_OVER
) || (mode
== EXEC_STEPI_OVER
))
1000 && status
== FUNC_IS_TRAMPOLINE
)
1003 DEBUG_Printf(DBG_CHN_MESG
, "Not stepping into trampoline at %x (no lines)\n",
1006 mode
= EXEC_STEP_OVER_TRAMPOLINE
;
1009 if( mode
== EXEC_STEP_INSTR
&& status
== FUNC_HAS_NO_LINES
)
1012 DEBUG_Printf(DBG_CHN_MESG
, "Not stepping into function at %x (no lines)\n",
1015 mode
= EXEC_STEP_OVER
;
1020 if( mode
== EXEC_STEP_INSTR
)
1022 if( DEBUG_CheckLinenoStatus(&addr
) == FUNC_HAS_NO_LINES
)
1024 DEBUG_Printf(DBG_CHN_MESG
, "Single stepping until exit from function, \n");
1025 DEBUG_Printf(DBG_CHN_MESG
, "which has no line number information.\n");
1027 ret_mode
= mode
= EXEC_FINISH
;
1033 case EXEC_CONT
: /* Continuous execution */
1035 DEBUG_context
.EFlags
&= ~STEP_FLAG
;
1037 DEBUG_SetBreakpoints( TRUE
);
1040 case EXEC_STEP_OVER_TRAMPOLINE
:
1042 * This is the means by which we step over our conversion stubs
1043 * in callfrom*.s and callto*.s. We dig the appropriate address
1044 * off the stack, and we set the breakpoint there instead of the
1045 * address just after the call.
1048 DEBUG_READ_MEM((void*)(DEBUG_context
.Esp
+
1049 2 * sizeof(unsigned int)),
1050 &addr
.off
, sizeof(addr
.off
));
1051 DEBUG_context
.EFlags
&= ~STEP_FLAG
;
1053 breakpoints
[0].addr
= addr
;
1054 breakpoints
[0].enabled
= TRUE
;
1055 breakpoints
[0].refcount
= 1;
1056 breakpoints
[0].skipcount
= 0;
1057 DEBUG_READ_MEM((void*)DEBUG_ToLinear( &addr
), &breakpoints
[0].u
.b
.opcode
,
1059 DEBUG_SetBreakpoints( TRUE
);
1063 case EXEC_STEPI_OVER
: /* Stepping over a call */
1064 case EXEC_STEP_OVER
: /* Stepping over a call */
1065 if (DEBUG_IsStepOverInstr())
1068 DEBUG_context
.EFlags
&= ~STEP_FLAG
;
1070 DEBUG_Disasm(&addr
, FALSE
);
1071 breakpoints
[0].addr
= addr
;
1072 breakpoints
[0].enabled
= TRUE
;
1073 breakpoints
[0].refcount
= 1;
1074 breakpoints
[0].skipcount
= 0;
1075 DEBUG_READ_MEM((void*)DEBUG_ToLinear( &addr
), &breakpoints
[0].u
.b
.opcode
,
1077 DEBUG_SetBreakpoints( TRUE
);
1080 /* else fall through to single-stepping */
1082 case EXEC_STEP_INSTR
: /* Single-stepping an instruction */
1083 case EXEC_STEPI_INSTR
: /* Single-stepping an instruction */
1085 DEBUG_context
.EFlags
|= STEP_FLAG
;
1089 RaiseException(DEBUG_STATUS_INTERNAL_ERROR
, 0, 0, NULL
);
1091 DEBUG_CurrThread
->stepOverBP
= breakpoints
[0];
1092 DEBUG_CurrThread
->exec_mode
= ret_mode
;
1096 DEBUG_AddBPCondition(int num
, struct expr
* exp
)
1098 if ((num
<= 0) || (num
>= next_bp
) || !breakpoints
[num
].refcount
)
1100 DEBUG_Printf( DBG_CHN_MESG
, "Invalid breakpoint number %d\n", num
);
1104 if( breakpoints
[num
].condition
!= NULL
)
1106 DEBUG_FreeExpr(breakpoints
[num
].condition
);
1107 breakpoints
[num
].condition
= NULL
;
1112 breakpoints
[num
].condition
= DEBUG_CloneExpr(exp
);