Introduced DBG_VALUE struct to manipulate debugger/debuggee address space.
[wine] / debugger / break.c
1 /*
2  * Debugger break-points handling
3  *
4  * Copyright 1994 Martin von Loewis
5  * Copyright 1995 Alexandre Julliard
6  * Copyright 1999,2000 Eric Pouech
7  */
8
9 #include "config.h"
10 #include <stdio.h>
11 #include "debugger.h"
12
13 #ifdef __i386__
14 #define DR7_CONTROL_SHIFT       16
15 #define DR7_CONTROL_SIZE        4
16
17 #define DR7_RW_EXECUTE          (0x0)
18 #define DR7_RW_WRITE            (0x1)
19 #define DR7_RW_READ             (0x3)
20
21 #define DR7_LEN_1               (0x0)
22 #define DR7_LEN_2               (0x4)
23 #define DR7_LEN_4               (0xC)
24
25 #define DR7_LOCAL_ENABLE_SHIFT  0
26 #define DR7_GLOBAL_ENABLE_SHIFT 1
27 #define DR7_ENABLE_SIZE         2
28
29 #define DR7_LOCAL_ENABLE_MASK   (0x55)
30 #define DR7_GLOBAL_ENABLE_MASK  (0xAA)
31
32 #define DR7_CONTROL_RESERVED    (0xFC00)
33 #define DR7_LOCAL_SLOWDOWN      (0x100)
34 #define DR7_GLOBAL_SLOWDOWN     (0x200)
35
36 #define DR7_ENABLE_MASK(dr)     (1<<(DR7_LOCAL_ENABLE_SHIFT+DR7_ENABLE_SIZE*(dr)))
37 #define IS_DR7_SET(ctrl,dr)     ((ctrl)&DR7_ENABLE_MASK(dr))
38 #define INT3          0xcc   /* int 3 opcode */
39 #endif
40
41 #define MAX_BREAKPOINTS 100
42
43 static BREAKPOINT breakpoints[MAX_BREAKPOINTS];
44
45 static int next_bp = 1;  /* breakpoint 0 is reserved for step-over */
46
47 /***********************************************************************
48  *           DEBUG_IsStepOverInstr
49  *
50  * Determine if the instruction at CS:EIP is an instruction that
51  * we need to step over (like a call or a repetitive string move).
52  */
53 static BOOL DEBUG_IsStepOverInstr(void)
54 {
55 #ifdef __i386__
56     BYTE*       instr;
57     BYTE        ch;
58     DBG_ADDR    addr;
59
60     addr.seg = DEBUG_context.SegCs;
61     addr.off = DEBUG_context.Eip;
62     /* FIXME: old code was using V86BASE(DEBUG_context)
63      * instead of passing thru DOSMEM_MemoryBase
64      */
65     instr = (BYTE*)DEBUG_ToLinear(&addr);
66
67     for (;;)
68     {
69         if (!DEBUG_READ_MEM(instr, &ch, sizeof(ch)))
70             return FALSE;
71
72         switch (ch)
73         {
74           /* Skip all prefixes */
75
76         case 0x2e:  /* cs: */
77         case 0x36:  /* ss: */
78         case 0x3e:  /* ds: */
79         case 0x26:  /* es: */
80         case 0x64:  /* fs: */
81         case 0x65:  /* gs: */
82         case 0x66:  /* opcode size prefix */
83         case 0x67:  /* addr size prefix */
84         case 0xf0:  /* lock */
85         case 0xf2:  /* repne */
86         case 0xf3:  /* repe */
87             instr++;
88             continue;
89
90           /* Handle call instructions */
91
92         case 0xcd:  /* int <intno> */
93         case 0xe8:  /* call <offset> */
94         case 0x9a:  /* lcall <seg>:<off> */
95             return TRUE;
96
97         case 0xff:  /* call <regmodrm> */
98             if (!DEBUG_READ_MEM(instr + 1, &ch, sizeof(ch)))
99                 return FALSE;
100             return (((ch & 0x38) == 0x10) || ((ch & 0x38) == 0x18));
101
102           /* Handle string instructions */
103
104         case 0x6c:  /* insb */
105         case 0x6d:  /* insw */
106         case 0x6e:  /* outsb */
107         case 0x6f:  /* outsw */
108         case 0xa4:  /* movsb */
109         case 0xa5:  /* movsw */
110         case 0xa6:  /* cmpsb */
111         case 0xa7:  /* cmpsw */
112         case 0xaa:  /* stosb */
113         case 0xab:  /* stosw */
114         case 0xac:  /* lodsb */
115         case 0xad:  /* lodsw */
116         case 0xae:  /* scasb */
117         case 0xaf:  /* scasw */
118             return TRUE;
119
120         default:
121             return FALSE;
122         }
123     }
124 #else
125     return FALSE;
126 #endif
127 }
128
129
130 /***********************************************************************
131  *           DEBUG_IsFctReturn
132  *
133  * Determine if the instruction at CS:EIP is an instruction that
134  * is a function return.
135  */
136 BOOL DEBUG_IsFctReturn(void)
137 {
138 #ifdef __i386__
139     BYTE*       instr;
140     BYTE        ch;
141     DBG_ADDR    addr;
142
143     addr.seg = DEBUG_context.SegCs;
144     addr.off = DEBUG_context.Eip;
145     /* FIXME: old code was using V86BASE(DEBUG_context)
146      * instead of passing thru DOSMEM_MemoryBase
147      */
148     instr = (BYTE*)DEBUG_ToLinear(&addr);
149
150     if (!DEBUG_READ_MEM(instr, &ch, sizeof(ch)))
151         return FALSE;
152
153     return (ch == 0xc2) || (ch == 0xc3);
154 #else
155     return FALSE;
156 #endif
157 }
158
159
160 /***********************************************************************
161  *           DEBUG_SetBreakpoints
162  *
163  * Set or remove all the breakpoints.
164  */
165 void DEBUG_SetBreakpoints( BOOL set )
166 {
167    int          i;
168    
169 #ifdef __i386__
170    DEBUG_context.Dr7 &= ~DR7_LOCAL_ENABLE_MASK;
171 #endif
172    
173    for (i = 0; i < next_bp; i++)
174    {
175       if (!(breakpoints[i].refcount && breakpoints[i].enabled))
176          continue;
177       
178       switch (breakpoints[i].type) {
179       case DBG_BREAK:
180          {
181 #ifdef __i386__
182             char ch = set ? INT3 : breakpoints[i].u.opcode;
183 #endif
184             
185             if (!DEBUG_WRITE_MEM( (void*)DEBUG_ToLinear(&breakpoints[i].addr), 
186                                   &ch, sizeof(ch) ))
187             {
188                fprintf(stderr, "Invalid address for breakpoint %d, disabling it\n", i);
189                breakpoints[i].enabled = FALSE;
190             }
191          }
192          break;
193       case DBG_WATCH:
194          if (set) 
195          {
196 #ifdef __i386__
197             DWORD       bits;
198             int         reg = breakpoints[i].u.w.reg;
199             LPDWORD     lpdr = NULL;
200
201             switch (reg) 
202             {
203                case 0: lpdr = &DEBUG_context.Dr0; break;
204                case 1: lpdr = &DEBUG_context.Dr1; break;
205                case 2: lpdr = &DEBUG_context.Dr2; break;
206                case 3: lpdr = &DEBUG_context.Dr3; break;
207                default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
208             }
209             
210             *lpdr = DEBUG_ToLinear(&breakpoints[i].addr);
211             fprintf(stderr, "Setting DR%d %08lx\n", (lpdr - &DEBUG_context.Dr0) / 4, *lpdr);
212             bits = (breakpoints[i].u.w.rw) ? DR7_RW_WRITE : DR7_RW_READ;
213             switch (breakpoints[i].u.w.len + 1)
214             {
215                case 4: bits |= DR7_LEN_4;       break;
216                case 2: bits |= DR7_LEN_2;       break;
217                case 1: bits |= DR7_LEN_1;       break;
218                default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
219             }
220             
221             DEBUG_context.Dr7 &= ~(0x0F << (DR7_CONTROL_SHIFT + DR7_CONTROL_SIZE * reg));
222             DEBUG_context.Dr7 |= bits << (DR7_CONTROL_SHIFT + DR7_CONTROL_SIZE * reg);
223             DEBUG_context.Dr7 |= DR7_ENABLE_MASK(reg) | DR7_LOCAL_SLOWDOWN;
224 #endif
225          }
226          break;
227       }
228    }
229    fprintf(stderr, "Setting DR7 %08lx\n", DEBUG_context.Dr7);
230 }
231
232 /***********************************************************************
233  *           DEBUG_FindBreakpoint
234  *
235  * Find the breakpoint for a given address. Return the breakpoint
236  * number or -1 if none.
237  * If type is DBG_BREAKPOINT, addr is a complete addr
238  * If type is DBG_WATCHPOINT, only addr.off is meaningful and contains 
239  * linear address
240  */
241 static int DEBUG_FindBreakpoint( const DBG_ADDR *addr, int type )
242 {
243    int i;
244    
245    for (i = 0; i < next_bp; i++)
246    {
247       if (breakpoints[i].refcount && breakpoints[i].enabled &&
248           breakpoints[i].type == type )
249       {
250          if ((type == DBG_BREAK && 
251               breakpoints[i].addr.seg == addr->seg &&
252               breakpoints[i].addr.off == addr->off) ||
253              (type == DBG_WATCH && 
254               DEBUG_ToLinear(&breakpoints[i].addr) == addr->off))
255             return i;
256       }
257    }
258    return -1;
259 }
260
261 /***********************************************************************
262  *           DEBUG_InitXPoint
263  *
264  * Find an empty slot in BP table to add a new break/watch point
265  */
266 static  int     DEBUG_InitXPoint(int type, DBG_ADDR* addr)
267 {
268    int  num;
269    
270    for (num = (next_bp < MAX_BREAKPOINTS) ? next_bp++ : 1; 
271         num < MAX_BREAKPOINTS; num++) 
272    {
273       if (breakpoints[num].refcount == 0) 
274       {
275          breakpoints[num].refcount = 1;
276          breakpoints[num].enabled = TRUE;
277          breakpoints[num].type    = type;
278          breakpoints[num].skipcount = 0;
279          breakpoints[num].addr = *addr;
280          breakpoints[num].is32 = 1;
281 #ifdef __i386__
282          if (addr->seg) 
283          {
284             switch (DEBUG_GetSelectorType( addr->seg )) 
285             {
286                case 32: break;
287                case 16: breakpoints[num].is32 = 0; break;
288                default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
289             }
290          }
291 #endif
292          return num;
293       }
294    }
295    
296    fprintf( stderr, "Too many breakpoints. Please delete some.\n" );
297    return -1;
298 }
299
300 /***********************************************************************
301  *           DEBUG_GetWatchedValue
302  *
303  * Returns the value watched by watch point 'num'.
304  */
305 static  BOOL    DEBUG_GetWatchedValue( int num, LPDWORD val )
306 {
307    BYTE         buf[4];
308    
309    if (!DEBUG_READ_MEM((void*)DEBUG_ToLinear(&breakpoints[num].addr), 
310                        buf, breakpoints[num].u.w.len + 1))
311       return FALSE;
312    
313    switch (breakpoints[num].u.w.len + 1) 
314    {
315       case 4:   *val = *(DWORD*)buf;    break;
316       case 2:   *val = *(WORD*)buf;     break;
317       case 1:   *val = *(BYTE*)buf;     break;
318       default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
319    }
320    return TRUE;
321 }
322
323 /***********************************************************************
324  *           DEBUG_AddBreakpoint
325  *
326  * Add a breakpoint.
327  */
328 void DEBUG_AddBreakpoint( const DBG_VALUE *_value )
329 {
330     DBG_VALUE value = *_value;
331     int num;
332     unsigned int seg2;
333     BYTE ch;
334
335     assert(_value->cookie == DV_TARGET || _value->cookie == DV_HOST);
336
337     DEBUG_FixAddress( &value.addr, DEBUG_context.SegCs );
338
339     if( value.type != NULL && value.type == DEBUG_TypeIntConst )
340     {
341        /*
342         * We know that we have the actual offset stored somewhere
343         * else in 32-bit space.  Grab it, and we
344         * should be all set.
345         */
346        seg2 = value.addr.seg;
347        value.addr.seg = 0;
348        value.addr.off = DEBUG_GetExprValue(&value, NULL);
349        value.addr.seg = seg2;
350     }
351
352     if ((num = DEBUG_FindBreakpoint(&value.addr, DBG_BREAK)) >= 1) 
353     {
354        breakpoints[num].refcount++;
355        return;
356     }
357
358     if (!DEBUG_READ_MEM_VERBOSE((void*)DEBUG_ToLinear( &value.addr ), &ch, sizeof(ch)))
359         return;
360
361     if ((num = DEBUG_InitXPoint(DBG_BREAK, &value.addr)) == -1)
362        return;
363
364     breakpoints[num].u.opcode  = ch;
365
366     fprintf( stderr, "Breakpoint %d at ", num );
367     DEBUG_PrintAddress( &breakpoints[num].addr, breakpoints[num].is32 ? 32 : 16,
368                         TRUE );
369     fprintf( stderr, "\n" );
370 }
371
372
373  /***********************************************************************
374  *           DEBUG_AddWatchpoint
375  *
376  * Add a watchpoint.
377  */
378 void DEBUG_AddWatchpoint( const DBG_VALUE *_value, BOOL is_write )
379 {
380    DBG_VALUE    value = *_value;
381    int          num, reg;
382    unsigned     seg2;
383    DWORD        mask = 0;
384    
385    assert(_value->cookie == DV_TARGET || _value->cookie == DV_HOST);
386
387    DEBUG_FixAddress( &value.addr, CS_reg(&DEBUG_context) );
388    
389    if ( value.type != NULL && value.type == DEBUG_TypeIntConst )
390    {
391       /*
392        * We know that we have the actual offset stored somewhere
393        * else in 32-bit space.  Grab it, and we
394        * should be all set.
395        */
396       seg2 = value.addr.seg;
397       value.addr.seg = 0;
398       value.addr.off = DEBUG_GetExprValue(&value, NULL);
399       value.addr.seg = seg2;
400    }
401    
402    for (num = 1; num < next_bp; num++) 
403    {
404       if (breakpoints[num].refcount && breakpoints[num].enabled && 
405           breakpoints[num].type == DBG_WATCH) {
406          mask |= (1 << breakpoints[num].u.w.reg);
407       }
408    }
409 #ifdef __i386__
410    for (reg = 0; reg < 4 && (mask & (1 << reg)); reg++);
411    if (reg == 4)
412    {
413       fprintf(stderr, "All i386 hardware watchpoints have been set. Delete some\n");
414       return;
415    }
416 #endif
417
418    if ((num = DEBUG_InitXPoint(DBG_WATCH, &value.addr)) == -1)
419       return;
420    
421    breakpoints[num].u.w.len = 4 - 1;
422    if (_value->type && DEBUG_GetObjectSize(_value->type) < 4)
423       breakpoints[num].u.w.len = 2 - 1;
424    
425    if (!DEBUG_GetWatchedValue( num, &breakpoints[num].u.w.oldval)) 
426    {
427       fprintf(stderr, "Bad address. Watchpoint not set\n");
428       breakpoints[num].refcount = 0;
429    }
430    
431    breakpoints[num].u.w.rw = (is_write) ? TRUE : FALSE;
432    breakpoints[reg].u.w.reg = reg;
433    
434    fprintf( stderr, "Watchpoint %d at ", num );
435    DEBUG_PrintAddress( &breakpoints[num].addr, breakpoints[num].is32 ? 32:16, TRUE );
436    fprintf( stderr, "\n" );
437 }
438
439 /***********************************************************************
440  *           DEBUG_DelBreakpoint
441  *
442  * Delete a breakpoint.
443  */
444 void DEBUG_DelBreakpoint( int num )
445 {
446     if ((num <= 0) || (num >= next_bp) || breakpoints[num].refcount == 0)
447     {
448         fprintf( stderr, "Invalid breakpoint number %d\n", num );
449         return;
450     }
451
452     if (--breakpoints[num].refcount > 0)
453        return;
454
455     if( breakpoints[num].condition != NULL )
456     {
457        DEBUG_FreeExpr(breakpoints[num].condition);
458        breakpoints[num].condition = NULL;
459     }
460
461     breakpoints[num].enabled = FALSE;
462     breakpoints[num].refcount = 0;
463     breakpoints[num].skipcount = 0;
464 }
465
466 /***********************************************************************
467  *           DEBUG_EnableBreakpoint
468  *
469  * Enable or disable a break point.
470  */
471 void DEBUG_EnableBreakpoint( int num, BOOL enable )
472 {
473     if ((num <= 0) || (num >= next_bp) || breakpoints[num].refcount == 0)
474     {
475         fprintf( stderr, "Invalid breakpoint number %d\n", num );
476         return;
477     }
478     breakpoints[num].enabled = (enable) ? TRUE : FALSE;
479     breakpoints[num].skipcount = 0;
480 }
481
482
483 /***********************************************************************
484  *           DEBUG_FindTriggeredWatchpoint
485  *
486  * Lookup the watchpoints to see if one has been triggered
487  * Return >= (watch point index) if one is found and *oldval is set to
488  *      the value watched before the TRAP
489  * Return -1 if none found (*oldval is undetermined)
490  *
491  * Unfortunately, Linux does *NOT* (A REAL PITA) report with ptrace 
492  * the DR6 register value, so we have to look with our own need the
493  * cause of the TRAP.
494  * -EP
495  */
496 static int DEBUG_FindTriggeredWatchpoint(LPDWORD oldval)
497 {
498    int                          i;
499    int                          found = -1;
500    DWORD                        val = 0;
501    
502    /* Method 1 => get triggered watchpoint from context (doesn't work on Linux
503     * 2.2.x)
504     */
505    for (i = 0; i < next_bp; i++) 
506    {
507 #ifdef __i386__
508       if (breakpoints[i].refcount && breakpoints[i].enabled && 
509           breakpoints[i].type == DBG_WATCH &&
510           (DEBUG_context.Dr6 & (1 << breakpoints[i].u.w.reg)))
511       {
512          DEBUG_context.Dr6 &= ~(1 << breakpoints[i].u.w.reg);
513          
514          *oldval = breakpoints[i].u.w.oldval;
515          if (DEBUG_GetWatchedValue(i, &val)) {
516             breakpoints[i].u.w.oldval = val;
517             return i;
518          }
519       }
520 #endif
521    }
522    
523    /* Method 1 failed, trying method2 */
524    
525    /* Method 2 => check if value has changed among registered watchpoints
526     * this really sucks, but this is how gdb 4.18 works on my linux box
527     * -EP
528     */
529    for (i = 0; i < next_bp; i++) 
530    {
531 #ifdef __i386__
532       if (breakpoints[i].refcount && breakpoints[i].enabled && 
533           breakpoints[i].type == DBG_WATCH && 
534           DEBUG_GetWatchedValue(i, &val)) 
535       {
536          *oldval = breakpoints[i].u.w.oldval;
537          if (val != *oldval) 
538          {
539             DEBUG_context.Dr6 &= ~(1 << breakpoints[i].u.w.reg);
540             breakpoints[i].u.w.oldval = val;
541             found = i;
542             /* cannot break, because two watch points may have been triggered on
543              * the same acces
544              * only one will be reported to the user (FIXME ?)
545              */
546          }
547       }
548 #endif
549    }
550    return found;
551 }
552
553 /***********************************************************************
554  *           DEBUG_InfoBreakpoints
555  *
556  * Display break points information.
557  */
558 void DEBUG_InfoBreakpoints(void)
559 {
560     int i;
561
562     fprintf( stderr, "Breakpoints:\n" );
563     for (i = 1; i < next_bp; i++)
564     {
565         if (breakpoints[i].refcount && breakpoints[i].type == DBG_BREAK)
566         {
567             fprintf( stderr, "%d: %c ", i, breakpoints[i].enabled ? 'y' : 'n');
568             DEBUG_PrintAddress( &breakpoints[i].addr, 
569                                 breakpoints[i].is32 ? 32 : 16, TRUE);
570             fprintf( stderr, " (%u)\n", breakpoints[i].refcount );
571             if( breakpoints[i].condition != NULL )
572             {
573                 fprintf(stderr, "\t\tstop when  ");
574                 DEBUG_DisplayExpr(breakpoints[i].condition);
575                 fprintf(stderr, "\n");
576             }
577         }
578     }
579     fprintf( stderr, "Watchpoints:\n" );
580     for (i = 1; i < next_bp; i++)
581     {
582         if (breakpoints[i].refcount && breakpoints[i].type == DBG_WATCH)
583         {
584             fprintf( stderr, "%d: %c ", i, breakpoints[i].enabled ? 'y' : 'n');
585             DEBUG_PrintAddress( &breakpoints[i].addr, 
586                                 breakpoints[i].is32 ? 32 : 16, TRUE);
587             fprintf( stderr, " on %d byte%s (%c)\n", 
588                      breakpoints[i].u.w.len + 1, 
589                      breakpoints[i].u.w.len > 0 ? "s" : "",
590                      breakpoints[i].u.w.rw ? 'W' : 'R');
591             if( breakpoints[i].condition != NULL )
592             {
593                 fprintf(stderr, "\t\tstop when  ");
594                 DEBUG_DisplayExpr(breakpoints[i].condition);
595                 fprintf(stderr, "\n");
596             }
597         }
598     }
599 }
600
601 /***********************************************************************
602  *           DEBUG_ShallBreak
603  *
604  * Check whether or not the condition (bp / skipcount) of a break/watch
605  * point are met.
606  */
607 static  BOOL DEBUG_ShallBreak( int bpnum )
608 {
609    if ( breakpoints[bpnum].condition != NULL )
610    {
611       DBG_VALUE value = DEBUG_EvalExpr(breakpoints[bpnum].condition);
612
613       if ( value.type == NULL )
614       {
615          /*
616           * Something wrong - unable to evaluate this expression.
617           */
618          fprintf(stderr, "Unable to evaluate expression ");
619          DEBUG_DisplayExpr(breakpoints[bpnum].condition);
620          fprintf(stderr, "\nTurning off condition\n");
621          DEBUG_AddBPCondition(bpnum, NULL);
622       }
623       else if( !DEBUG_GetExprValue( &value, NULL) )
624       {
625          return FALSE;
626       }
627    }
628    
629    if ( breakpoints[bpnum].skipcount > 0 && --breakpoints[bpnum].skipcount > 0 )
630       return FALSE;
631
632    return TRUE;
633 }
634
635 /***********************************************************************
636  *           DEBUG_ShouldContinue
637  *
638  * Determine if we should continue execution after a SIGTRAP signal when
639  * executing in the given mode.
640  */
641 BOOL DEBUG_ShouldContinue( DWORD code, enum exec_mode mode, int * count )
642 {
643     DBG_ADDR    addr;
644     int         bpnum;
645     DWORD       oldval;
646     int         wpnum;
647     struct symbol_info syminfo;
648
649 #ifdef __i386__
650     /* If not single-stepping, back up over the int3 instruction */
651     if (code == EXCEPTION_BREAKPOINT) 
652        DEBUG_context.Eip--;
653 #endif
654
655     DEBUG_GetCurrentAddress( &addr );
656     bpnum = DEBUG_FindBreakpoint( &addr, DBG_BREAK );
657     breakpoints[0].enabled = FALSE;  /* disable the step-over breakpoint */
658
659     if ((bpnum != 0) && (bpnum != -1))
660     {
661         if (!DEBUG_ShallBreak(bpnum)) return TRUE;
662
663         fprintf( stderr, "Stopped on breakpoint %d at ", bpnum );
664         syminfo = DEBUG_PrintAddress( &breakpoints[bpnum].addr,
665                                       breakpoints[bpnum].is32 ? 32 : 16, TRUE );
666         fprintf( stderr, "\n" );
667         
668         if( syminfo.list.sourcefile != NULL )
669             DEBUG_List(&syminfo.list, NULL, 0);
670         return FALSE;
671     }
672
673     wpnum = DEBUG_FindTriggeredWatchpoint(&oldval);
674     if ((wpnum != 0) && (wpnum != -1))
675     {
676        /* If not single-stepping, do not back up over the int3 instruction */
677        if (code == EXCEPTION_BREAKPOINT) 
678        {
679            EIP_reg(&DEBUG_context)++;
680            addr.off++;
681        }
682        if (!DEBUG_ShallBreak(wpnum)) return TRUE;
683        
684        fprintf(stderr, "Stopped on watchpoint %d at ", wpnum);
685        syminfo = DEBUG_PrintAddress( &addr, !addr.seg ? 32 :
686                                      DEBUG_GetSelectorType( addr.seg ), TRUE );
687        
688        fprintf(stderr, " values: old=%lu new=%lu\n", 
689                oldval, breakpoints[wpnum].u.w.oldval);
690        if (syminfo.list.sourcefile != NULL)
691           DEBUG_List(&syminfo.list, NULL, 0);
692        return FALSE;
693     }
694
695     /*
696      * If our mode indicates that we are stepping line numbers,
697      * get the current function, and figure out if we are exactly
698      * on a line number or not.
699      */
700     if( mode == EXEC_STEP_OVER || mode == EXEC_STEP_INSTR )
701     {
702         if( DEBUG_CheckLinenoStatus(&addr) == AT_LINENUMBER )
703         {
704             (*count)--;
705         }
706     }
707     else if( mode == EXEC_STEPI_OVER || mode == EXEC_STEPI_INSTR )
708     {
709         (*count)--;
710     }
711
712     if( *count > 0 || mode == EXEC_FINISH )
713     {
714         /*
715          * We still need to execute more instructions.
716          */
717         return TRUE;
718     }
719     
720     /*
721      * If we are about to stop, then print out the source line if we
722      * have it.
723      */
724     if (mode != EXEC_CONT && mode != EXEC_PASS && mode != EXEC_FINISH)
725     {
726         DEBUG_FindNearestSymbol( &addr, TRUE, NULL, 0, &syminfo.list);
727         if( syminfo.list.sourcefile != NULL )
728         {
729             DEBUG_List(&syminfo.list, NULL, 0);
730         }
731     }
732
733 #ifdef __i386__
734     /* If there's no breakpoint and we are not single-stepping, then we     */
735     /* must have encountered an int3 in the Windows program; let's skip it. */
736     if ((bpnum == -1) && code == EXCEPTION_BREAKPOINT)
737         DEBUG_context.Eip++;
738 #endif
739
740     /* no breakpoint, continue if in continuous mode */
741     return (mode == EXEC_CONT || mode == EXEC_PASS || mode == EXEC_FINISH);
742 }
743
744 /***********************************************************************
745  *           DEBUG_RestartExecution
746  *
747  * Remove all breakpoints before entering the debug loop
748  */
749 void    DEBUG_SuspendExecution( void )
750 {
751    DEBUG_SetBreakpoints( FALSE );
752    breakpoints[0] = DEBUG_CurrThread->stepOverBP;
753 }
754
755 /***********************************************************************
756  *           DEBUG_RestartExecution
757  *
758  * Set the breakpoints to the correct state to restart execution
759  * in the given mode.
760  */
761 enum exec_mode DEBUG_RestartExecution( enum exec_mode mode, int count )
762 {
763     DBG_ADDR addr;
764     DBG_ADDR addr2;
765     int bp;
766     int delta;
767     int status;
768     enum exec_mode ret_mode;
769     DWORD instr;
770     unsigned char ch;
771
772     DEBUG_GetCurrentAddress( &addr );
773
774     /*
775      * This is the mode we will be running in after we finish.  We would like
776      * to be able to modify this in certain cases.
777      */
778     ret_mode = mode;
779
780     bp = DEBUG_FindBreakpoint( &addr, DBG_BREAK ); 
781     if ( bp != -1 && bp != 0)
782       {
783         /*
784          * If we have set a new value, then save it in the BP number.
785          */
786         if( count != 0 && mode == EXEC_CONT )
787           {
788             breakpoints[bp].skipcount = count;
789           }
790         mode = EXEC_STEPI_INSTR;  /* If there's a breakpoint, skip it */
791       }
792     else
793       {
794         if( mode == EXEC_CONT && count > 1 )
795           {
796             fprintf(stderr, "Not stopped at any breakpoint; argument ignored.\n");
797           }
798       }
799     
800     if( mode == EXEC_FINISH && DEBUG_IsFctReturn() )
801       {
802         mode = ret_mode = EXEC_STEPI_INSTR;
803       }
804
805     instr = DEBUG_ToLinear( &addr );
806     DEBUG_READ_MEM((void*)instr, &ch, sizeof(ch));
807     /*
808      * See if the function we are stepping into has debug info
809      * and line numbers.  If not, then we step over it instead.
810      * FIXME - we need to check for things like thunks or trampolines,
811      * as the actual function may in fact have debug info.
812      */
813     if( ch == 0xe8 )
814       {
815         DEBUG_READ_MEM((void*)(instr + 1), &delta, sizeof(delta));
816         addr2 = addr;
817         DEBUG_Disasm(&addr2, FALSE);
818         addr2.off += delta;
819         
820         status = DEBUG_CheckLinenoStatus(&addr2);
821         /*
822          * Anytime we have a trampoline, step over it.
823          */
824         if( ((mode == EXEC_STEP_OVER) || (mode == EXEC_STEPI_OVER))
825             && status == FUNC_IS_TRAMPOLINE )
826           {
827 #if 0
828             fprintf(stderr, "Not stepping into trampoline at %x (no lines)\n",
829                     addr2.off);
830 #endif
831             mode = EXEC_STEP_OVER_TRAMPOLINE;
832           }
833         
834         if( mode == EXEC_STEP_INSTR && status == FUNC_HAS_NO_LINES )
835           {
836 #if 0
837             fprintf(stderr, "Not stepping into function at %x (no lines)\n",
838                     addr2.off);
839 #endif
840             mode = EXEC_STEP_OVER;
841           }
842       }
843
844
845     if( mode == EXEC_STEP_INSTR )
846       {
847         if( DEBUG_CheckLinenoStatus(&addr) == FUNC_HAS_NO_LINES )
848           {
849             fprintf(stderr, "Single stepping until exit from function, \n");
850             fprintf(stderr, "which has no line number information.\n");
851             
852             ret_mode = mode = EXEC_FINISH;
853           }
854       }
855
856     switch(mode)
857     {
858     case EXEC_CONT: /* Continuous execution */
859     case EXEC_PASS: /* Continue, passing exception */
860 #ifdef __i386__
861         DEBUG_context.EFlags &= ~STEP_FLAG;
862 #endif
863         DEBUG_SetBreakpoints( TRUE );
864         break;
865
866     case EXEC_STEP_OVER_TRAMPOLINE:
867       /*
868        * This is the means by which we step over our conversion stubs
869        * in callfrom*.s and callto*.s.  We dig the appropriate address
870        * off the stack, and we set the breakpoint there instead of the
871        * address just after the call.
872        */
873 #ifdef __i386__
874       DEBUG_READ_MEM((void*)(DEBUG_context.Esp + 
875                              2 * sizeof(unsigned int)), 
876                      &addr.off, sizeof(addr.off));
877       DEBUG_context.EFlags &= ~STEP_FLAG;
878 #endif
879       breakpoints[0].addr    = addr;
880       breakpoints[0].enabled = TRUE;
881       breakpoints[0].refcount = 1;
882       breakpoints[0].skipcount = 0;
883       DEBUG_READ_MEM((void*)DEBUG_ToLinear( &addr ), &breakpoints[0].u.opcode, 
884                      sizeof(char));
885       DEBUG_SetBreakpoints( TRUE );
886       break;
887
888     case EXEC_FINISH:
889     case EXEC_STEPI_OVER:  /* Stepping over a call */
890     case EXEC_STEP_OVER:  /* Stepping over a call */
891         if (DEBUG_IsStepOverInstr())
892         {
893 #ifdef __i386__
894             DEBUG_context.EFlags &= ~STEP_FLAG;
895 #endif
896             DEBUG_Disasm(&addr, FALSE);
897             breakpoints[0].addr    = addr;
898             breakpoints[0].enabled = TRUE;
899             breakpoints[0].refcount = 1;
900             breakpoints[0].skipcount = 0;
901             DEBUG_READ_MEM((void*)DEBUG_ToLinear( &addr ), &breakpoints[0].u.opcode, 
902                            sizeof(char));
903             DEBUG_SetBreakpoints( TRUE );
904             break;
905         }
906         /* else fall through to single-stepping */
907
908     case EXEC_STEP_INSTR: /* Single-stepping an instruction */
909     case EXEC_STEPI_INSTR: /* Single-stepping an instruction */
910 #ifdef __i386__
911         DEBUG_context.EFlags |= STEP_FLAG;
912 #endif
913         break;
914     default:
915         RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
916     }
917     DEBUG_CurrThread->stepOverBP = breakpoints[0];
918     return ret_mode;
919 }
920
921 int
922 DEBUG_AddBPCondition(int num, struct expr * exp)
923 {
924     if ((num <= 0) || (num >= next_bp) || !breakpoints[num].refcount)
925     {
926         fprintf( stderr, "Invalid breakpoint number %d\n", num );
927         return FALSE;
928     }
929
930     if( breakpoints[num].condition != NULL )
931       {
932         DEBUG_FreeExpr(breakpoints[num].condition);
933         breakpoints[num].condition = NULL;
934       }
935
936     if( exp != NULL )
937       {
938         breakpoints[num].condition = DEBUG_CloneExpr(exp);
939       }
940
941    return TRUE;
942 }