File: break.c

package info (click to toggle)
wine 0.0.20020411-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 43,012 kB
  • ctags: 104,265
  • sloc: ansic: 550,196; perl: 21,747; yacc: 3,990; sh: 3,904; makefile: 3,297; tcl: 2,616; lex: 2,443
file content (1057 lines) | stat: -rw-r--r-- 29,981 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
/*
 * Debugger break-points handling
 *
 * Copyright 1994 Martin von Loewis
 * Copyright 1995 Alexandre Julliard
 * Copyright 1999,2000 Eric Pouech
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "config.h"
#include "debugger.h"

#ifdef __i386__
#define DR7_CONTROL_SHIFT	16
#define DR7_CONTROL_SIZE 	4

#define DR7_RW_EXECUTE 		(0x0)
#define DR7_RW_WRITE		(0x1)
#define DR7_RW_READ		(0x3)

#define DR7_LEN_1		(0x0)
#define DR7_LEN_2		(0x4)
#define DR7_LEN_4		(0xC)

#define DR7_LOCAL_ENABLE_SHIFT	0
#define DR7_GLOBAL_ENABLE_SHIFT 1
#define DR7_ENABLE_SIZE 	2

#define DR7_LOCAL_ENABLE_MASK	(0x55)
#define DR7_GLOBAL_ENABLE_MASK	(0xAA)

#define DR7_CONTROL_RESERVED	(0xFC00)
#define DR7_LOCAL_SLOWDOWN	(0x100)
#define DR7_GLOBAL_SLOWDOWN	(0x200)

#define	DR7_ENABLE_MASK(dr)	(1<<(DR7_LOCAL_ENABLE_SHIFT+DR7_ENABLE_SIZE*(dr)))
#define	IS_DR7_SET(ctrl,dr) 	((ctrl)&DR7_ENABLE_MASK(dr))
#define INT3          0xcc   /* int 3 opcode */
#endif

#define MAX_BREAKPOINTS 100

static DBG_BREAKPOINT breakpoints[MAX_BREAKPOINTS];

static int next_bp = 1;  /* breakpoint 0 is reserved for step-over */

/***********************************************************************
 *           DEBUG_IsStepOverInstr
 *
 * Determine if the instruction at CS:EIP is an instruction that
 * we need to step over (like a call or a repetitive string move).
 */
static BOOL DEBUG_IsStepOverInstr(void)
{
#ifdef __i386__
    BYTE*	instr;
    BYTE	ch;
    DBG_ADDR	addr;

    addr.seg = DEBUG_context.SegCs;
    addr.off = DEBUG_context.Eip;
    /* FIXME: old code was using V86BASE(DEBUG_context)
     * instead of passing through DOSMEM_MemoryBase
     */
    instr = (BYTE*)DEBUG_ToLinear(&addr);

    for (;;)
    {
        if (!DEBUG_READ_MEM(instr, &ch, sizeof(ch)))
	    return FALSE;

        switch (ch)
        {
          /* Skip all prefixes */

        case 0x2e:  /* cs: */
        case 0x36:  /* ss: */
        case 0x3e:  /* ds: */
        case 0x26:  /* es: */
        case 0x64:  /* fs: */
        case 0x65:  /* gs: */
        case 0x66:  /* opcode size prefix */
        case 0x67:  /* addr size prefix */
        case 0xf0:  /* lock */
        case 0xf2:  /* repne */
        case 0xf3:  /* repe */
            instr++;
            continue;

          /* Handle call instructions */

        case 0xcd:  /* int <intno> */
        case 0xe8:  /* call <offset> */
        case 0x9a:  /* lcall <seg>:<off> */
            return TRUE;

        case 0xff:  /* call <regmodrm> */
	    if (!DEBUG_READ_MEM(instr + 1, &ch, sizeof(ch)))
	        return FALSE;
	    return (((ch & 0x38) == 0x10) || ((ch & 0x38) == 0x18));

          /* Handle string instructions */

        case 0x6c:  /* insb */
        case 0x6d:  /* insw */
        case 0x6e:  /* outsb */
        case 0x6f:  /* outsw */
        case 0xa4:  /* movsb */
        case 0xa5:  /* movsw */
        case 0xa6:  /* cmpsb */
        case 0xa7:  /* cmpsw */
        case 0xaa:  /* stosb */
        case 0xab:  /* stosw */
        case 0xac:  /* lodsb */
        case 0xad:  /* lodsw */
        case 0xae:  /* scasb */
        case 0xaf:  /* scasw */
            return TRUE;

        default:
            return FALSE;
        }
    }
#else
    return FALSE;
#endif
}


/***********************************************************************
 *           DEBUG_IsFctReturn
 *
 * Determine if the instruction at CS:EIP is an instruction that
 * is a function return.
 */
BOOL DEBUG_IsFctReturn(void)
{
#ifdef __i386__
    BYTE*	instr;
    BYTE  	ch;
    DBG_ADDR	addr;

    addr.seg = DEBUG_context.SegCs;
    addr.off = DEBUG_context.Eip;
    /* FIXME: old code was using V86BASE(DEBUG_context)
     * instead of passing through DOSMEM_MemoryBase
     */
    instr = (BYTE*)DEBUG_ToLinear(&addr);

    if (!DEBUG_READ_MEM(instr, &ch, sizeof(ch)))
        return FALSE;

    return (ch == 0xc2) || (ch == 0xc3);
#else
    return FALSE;
#endif
}


/***********************************************************************
 *           DEBUG_SetBreakpoints
 *
 * Set or remove all the breakpoints.
 */
void DEBUG_SetBreakpoints( BOOL set )
{
   int		i;
   
#ifdef __i386__
   DEBUG_context.Dr7 &= ~DR7_LOCAL_ENABLE_MASK;
#endif
   
   for (i = 0; i < next_bp; i++)
   {
      if (!(breakpoints[i].refcount && breakpoints[i].enabled))
	 continue;
      
      switch (breakpoints[i].type) {
      case DBG_BREAK:
	 {
#ifdef __i386__
	    char ch = set ? INT3 : breakpoints[i].u.b.opcode;
	    
	    if (!DEBUG_WRITE_MEM( (void*)DEBUG_ToLinear(&breakpoints[i].addr), 
				  &ch, sizeof(ch) ))
	    {
	       DEBUG_Printf(DBG_CHN_MESG, "Invalid address for breakpoint %d, disabling it\n", i);
	       breakpoints[i].enabled = FALSE;
	    }
#endif
	 }
	 break;
      case DBG_WATCH:
	 if (set) 
	 {
#ifdef __i386__
	    DWORD	bits;
	    int		reg = breakpoints[i].u.w.reg;
	    LPDWORD	lpdr = NULL;

	    switch (reg) 
	    {
	       case 0: lpdr = &DEBUG_context.Dr0; break;
	       case 1: lpdr = &DEBUG_context.Dr1; break;
	       case 2: lpdr = &DEBUG_context.Dr2; break;
	       case 3: lpdr = &DEBUG_context.Dr3; break;
	       default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
	    }
	    
	    *lpdr = DEBUG_ToLinear(&breakpoints[i].addr);
	    bits = (breakpoints[i].u.w.rw) ? DR7_RW_WRITE : DR7_RW_READ;
	    switch (breakpoints[i].u.w.len + 1)
	    {
	       case 4: bits |= DR7_LEN_4;	break;
	       case 2: bits |= DR7_LEN_2;	break;
	       case 1: bits |= DR7_LEN_1;	break;
	       default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
	    }
	    
	    DEBUG_context.Dr7 &= ~(0x0F << (DR7_CONTROL_SHIFT + DR7_CONTROL_SIZE * reg));
	    DEBUG_context.Dr7 |= bits << (DR7_CONTROL_SHIFT + DR7_CONTROL_SIZE * reg);
	    DEBUG_context.Dr7 |= DR7_ENABLE_MASK(reg) | DR7_LOCAL_SLOWDOWN;
#endif
	 }
	 break;
      }
   }
}

/***********************************************************************
 *           DEBUG_FindBreakpoint
 *
 * Find the breakpoint for a given address. Return the breakpoint
 * number or -1 if none.
 * If type is DBG_BREAKPOINT, addr is a complete addr
 * If type is DBG_WATCHPOINT, only addr.off is meaningful and contains 
 * linear address
 */
static int DEBUG_FindBreakpoint( const DBG_ADDR *addr, int type )
{
   int i;
   
   for (i = 0; i < next_bp; i++)
   {
      if (breakpoints[i].refcount && breakpoints[i].enabled &&
	  breakpoints[i].type == type )
      {
	 if ((type == DBG_BREAK && 
	      breakpoints[i].addr.seg == addr->seg &&
	      breakpoints[i].addr.off == addr->off) ||
	     (type == DBG_WATCH && 
	      DEBUG_ToLinear(&breakpoints[i].addr) == addr->off))
	    return i;
      }
   }
   return -1;
}

/***********************************************************************
 *           DEBUG_InitXPoint
 *
 * Find an empty slot in BP table to add a new break/watch point
 */
static	int	DEBUG_InitXPoint(int type, DBG_ADDR* addr)
{
   int	num;
   
   for (num = (next_bp < MAX_BREAKPOINTS) ? next_bp++ : 1; 
	num < MAX_BREAKPOINTS; num++) 
   {
      if (breakpoints[num].refcount == 0) 
      {
          breakpoints[num].refcount = 1;
          breakpoints[num].enabled = TRUE;
          breakpoints[num].type    = type;
          breakpoints[num].skipcount = 0;
          breakpoints[num].addr = *addr;
          switch (DEBUG_GetSelectorType( addr->seg ))
          {
          case MODE_32:
              breakpoints[num].is32 = 1;
              break;
          case MODE_VM86:
          case MODE_16:
              breakpoints[num].is32 = 0;
              break;
          default:
              RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
          }
          return num;
      }
   }

   DEBUG_Printf( DBG_CHN_MESG, "Too many breakpoints. Please delete some.\n" );
   return -1;
}

/***********************************************************************
 *           DEBUG_GetWatchedValue
 *
 * Returns the value watched by watch point 'num'.
 */
static	BOOL	DEBUG_GetWatchedValue( int num, LPDWORD val )
{
   BYTE		buf[4];
   
   if (!DEBUG_READ_MEM((void*)DEBUG_ToLinear(&breakpoints[num].addr), 
		       buf, breakpoints[num].u.w.len + 1))
      return FALSE;
   
   switch (breakpoints[num].u.w.len + 1) 
   {
      case 4:	*val = *(DWORD*)buf;	break;
      case 2:	*val = *(WORD*)buf;	break;
      case 1:	*val = *(BYTE*)buf;	break;
      default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
   }
   return TRUE;
}

/***********************************************************************
 *           DEBUG_AddBreakpoint
 *
 * Add a breakpoint.
 */
void DEBUG_AddBreakpoint( const DBG_VALUE *_value, BOOL (*func)(void) )
{
    DBG_VALUE value = *_value;
    int num;
    BYTE ch;

    if( value.type != NULL && value.type == DEBUG_GetBasicType(DT_BASIC_CONST_INT) )
    {
        /*
         * We know that we have the actual offset stored somewhere
         * else in 32-bit space.  Grab it, and we
         * should be all set.
         */
        unsigned int seg2 = value.addr.seg;
        value.addr.seg = 0;
        value.addr.off = DEBUG_GetExprValue(&value, NULL);
        value.addr.seg = seg2;
    }

    if ((num = DEBUG_FindBreakpoint(&value.addr, DBG_BREAK)) >= 1) 
    {
       breakpoints[num].refcount++;
       return;
    }

    if (!DEBUG_READ_MEM_VERBOSE((void*)DEBUG_ToLinear( &value.addr ), &ch, sizeof(ch)))
    {
       DEBUG_Printf( DBG_CHN_MESG, "Invalid address, can't set breakpoint\n");
       return;
    }

    if ((num = DEBUG_InitXPoint(DBG_BREAK, &value.addr)) == -1)
       return;

    breakpoints[num].u.b.opcode = ch;
    breakpoints[num].u.b.func = func;

    DEBUG_Printf( DBG_CHN_MESG, "Breakpoint %d at ", num );
    DEBUG_PrintAddress( &breakpoints[num].addr, breakpoints[num].is32 ? MODE_32 : MODE_16,
			TRUE );
    DEBUG_Printf( DBG_CHN_MESG, "\n" );
}

/***********************************************************************
 *           DEBUG_AddBreakpointFromId
 *
 * Add a breakpoint from a function name (and eventually a line #)
 */
void	DEBUG_AddBreakpointFromId(const char *name, int lineno)
{
   DBG_VALUE 	value;
   int		i;

   if (DEBUG_GetSymbolValue(name, lineno, &value, TRUE)) {
      DEBUG_AddBreakpoint(&value, NULL);
      return;
   }

   DEBUG_Printf(DBG_CHN_MESG, "Unable to add breakpoint, will check again when a new DLL is loaded\n");
   for (i = 0; i < DEBUG_CurrProcess->num_delayed_bp; i++) {
      if (!strcmp(name, DEBUG_CurrProcess->delayed_bp[i].name) &&
	  lineno == DEBUG_CurrProcess->delayed_bp[i].lineno) {
	 return;
      }
   }
   DEBUG_CurrProcess->delayed_bp = DBG_realloc(DEBUG_CurrProcess->delayed_bp, 
					       sizeof(DBG_DELAYED_BP) * ++DEBUG_CurrProcess->num_delayed_bp);

   DEBUG_CurrProcess->delayed_bp[DEBUG_CurrProcess->num_delayed_bp - 1].name = strcpy(DBG_alloc(strlen(name) + 1), name);
   DEBUG_CurrProcess->delayed_bp[DEBUG_CurrProcess->num_delayed_bp - 1].lineno = lineno;
}

/***********************************************************************
 *           DEBUG_AddBreakpointFromLineno
 *
 * Add a breakpoint from a line number in current file
 */
void	DEBUG_AddBreakpointFromLineno(int lineno)
{
   DBG_VALUE 			value;
   
   DEBUG_GetCurrentAddress(&value.addr);
   
   if (lineno != -1) {
      struct name_hash*	nh;
      
      DEBUG_FindNearestSymbol(&value.addr, TRUE, &nh, 0, NULL);
      if (nh == NULL) {
	 DEBUG_Printf(DBG_CHN_MESG, "Unable to add breakpoint\n");
	 return;
      }
      DEBUG_GetLineNumberAddr(nh, lineno, &value.addr, TRUE);
   }
   
   value.type = NULL;
   value.cookie = DV_TARGET;
   DEBUG_AddBreakpoint( &value, NULL );
}

/***********************************************************************
 *           DEBUG_CheckDelayedBP
 *
 * Check is a registered delayed BP is now available.
 */
void		DEBUG_CheckDelayedBP(void)
{
   DBG_VALUE		value;
   int			i = 0;
   DBG_DELAYED_BP*	dbp = DEBUG_CurrProcess->delayed_bp;

   while (i < DEBUG_CurrProcess->num_delayed_bp) {
      if (DEBUG_GetSymbolValue(dbp[i].name, dbp[i].lineno, &value, TRUE)) {
	 DEBUG_AddBreakpoint(&value, NULL);
	 memmove(&dbp[i], &dbp[i+1], (--DEBUG_CurrProcess->num_delayed_bp - i) * sizeof(*dbp));
      } else {
	 i++;
      }
   }
}

/***********************************************************************
 *           DEBUG_AddWatchpoint
 *
 * Add a watchpoint.
 */
void DEBUG_AddWatchpoint( const DBG_VALUE *_value, BOOL is_write )
{
   DBG_VALUE	value = *_value;
   int		num, reg = -1;
   unsigned	seg2;
   DWORD	mask = 0;
   
   assert(_value->cookie == DV_TARGET || _value->cookie == DV_HOST);

#ifdef __i386__
   DEBUG_FixAddress( &value.addr, DEBUG_context.SegCs );
#endif
   
   if ( value.type != NULL && value.type == DEBUG_GetBasicType(DT_BASIC_CONST_INT) )
   {
      /*
       * We know that we have the actual offset stored somewhere
       * else in 32-bit space.  Grab it, and we
       * should be all set.
       */
      seg2 = value.addr.seg;
      value.addr.seg = 0;
      value.addr.off = DEBUG_GetExprValue(&value, NULL);
      value.addr.seg = seg2;
   }
   
   for (num = 1; num < next_bp; num++) 
   {
      if (breakpoints[num].refcount && breakpoints[num].enabled && 
	  breakpoints[num].type == DBG_WATCH) {
	 mask |= (1 << breakpoints[num].u.w.reg);
      }
   }
#ifdef __i386__
   for (reg = 0; reg < 4 && (mask & (1 << reg)); reg++);
   if (reg == 4)
   {
      DEBUG_Printf(DBG_CHN_MESG, "All i386 hardware watchpoints have been set. Delete some\n");
      return;
   }
#endif

   if ((num = DEBUG_InitXPoint(DBG_WATCH, &value.addr)) == -1)
      return;
   
   breakpoints[num].u.w.len = 4 - 1;
   if (_value->type && DEBUG_GetObjectSize(_value->type) < 4)
      breakpoints[num].u.w.len = 2 - 1;
   
   if (!DEBUG_GetWatchedValue( num, &breakpoints[num].u.w.oldval)) 
   {
      DEBUG_Printf(DBG_CHN_MESG, "Bad address. Watchpoint not set\n");
      breakpoints[num].refcount = 0;
   }
   else
   {
      breakpoints[num].u.w.rw = (is_write) ? TRUE : FALSE;
      breakpoints[reg].u.w.reg = reg;
   
      DEBUG_Printf( DBG_CHN_MESG, "Watchpoint %d at ", num );
      DEBUG_PrintAddress( &breakpoints[num].addr, breakpoints[num].is32 ? MODE_32 : MODE_16, TRUE );
      DEBUG_Printf( DBG_CHN_MESG, "\n" );
   }
}

/***********************************************************************
 *           DEBUG_AddWathpointFromId
 *
 * Add a watchpoint from a symbol name (and eventually a line #)
 */
void	DEBUG_AddWatchpointFromId(const char *name)
{
   DBG_VALUE value;
   
   if( DEBUG_GetSymbolValue(name, -1, &value, TRUE) )
      DEBUG_AddWatchpoint( &value, 1 );
   else
      DEBUG_Printf(DBG_CHN_MESG, "Unable to add watchpoint\n");
}

/***********************************************************************
 *           DEBUG_DelBreakpoint
 *
 * Delete a breakpoint.
 */
void DEBUG_DelBreakpoint( int num )
{
    if ((num <= 0) || (num >= next_bp) || breakpoints[num].refcount == 0)
    {
        DEBUG_Printf( DBG_CHN_MESG, "Invalid breakpoint number %d\n", num );
        return;
    }

    if (--breakpoints[num].refcount > 0)
       return;

    if( breakpoints[num].condition != NULL )
    {
       DEBUG_FreeExpr(breakpoints[num].condition);
       breakpoints[num].condition = NULL;
    }

    breakpoints[num].enabled = FALSE;
    breakpoints[num].refcount = 0;
    breakpoints[num].skipcount = 0;
}

/***********************************************************************
 *           DEBUG_EnableBreakpoint
 *
 * Enable or disable a break point.
 */
void DEBUG_EnableBreakpoint( int num, BOOL enable )
{
    if ((num <= 0) || (num >= next_bp) || breakpoints[num].refcount == 0)
    {
        DEBUG_Printf( DBG_CHN_MESG, "Invalid breakpoint number %d\n", num );
        return;
    }
    breakpoints[num].enabled = (enable) ? TRUE : FALSE;
    breakpoints[num].skipcount = 0;
}


/***********************************************************************
 *           DEBUG_FindTriggeredWatchpoint
 *
 * Lookup the watchpoints to see if one has been triggered
 * Return >= (watch point index) if one is found and *oldval is set to
 * 	the value watched before the TRAP
 * Return -1 if none found (*oldval is undetermined)
 *
 * Unfortunately, Linux does *NOT* (A REAL PITA) report with ptrace 
 * the DR6 register value, so we have to look with our own need the
 * cause of the TRAP.
 * -EP
 */
static int DEBUG_FindTriggeredWatchpoint(LPDWORD oldval)
{
   int				found = -1;
#ifdef __i386__
   int 				i;
   
   /* Method 1 => get triggered watchpoint from context (doesn't work on Linux
    * 2.2.x). This should be fixed in >= 2.2.16
    */
   for (i = 0; i < next_bp; i++) 
   {
      DWORD val = 0;

      if (breakpoints[i].refcount && breakpoints[i].enabled && 
	  breakpoints[i].type == DBG_WATCH &&
	  (DEBUG_context.Dr6 & (1 << breakpoints[i].u.w.reg)))
      {
	 DEBUG_context.Dr6 &= ~(1 << breakpoints[i].u.w.reg);
	 
	 *oldval = breakpoints[i].u.w.oldval;
	 if (DEBUG_GetWatchedValue(i, &val)) {
	    breakpoints[i].u.w.oldval = val;
	    return i;
	 }
      }
   }
   
   /* Method 1 failed, trying method 2 */
   
   /* Method 2 => check if value has changed among registered watchpoints
    * this really sucks, but this is how gdb 4.18 works on my linux box
    * -EP
    */
   for (i = 0; i < next_bp; i++) 
   {
      DWORD val = 0;

      if (breakpoints[i].refcount && breakpoints[i].enabled && 
	  breakpoints[i].type == DBG_WATCH && 
	  DEBUG_GetWatchedValue(i, &val)) 
      {
	 *oldval = breakpoints[i].u.w.oldval;
	 if (val != *oldval) 
	 {
	    DEBUG_context.Dr6 &= ~(1 << breakpoints[i].u.w.reg);
	    breakpoints[i].u.w.oldval = val;
	    found = i;
	    /* cannot break, because two watch points may have been triggered on
	     * the same access
	     * only one will be reported to the user (FIXME ?)
	     */
	 }
      }
   }
#endif
   return found;
}

/***********************************************************************
 *           DEBUG_InfoBreakpoints
 *
 * Display break points information.
 */
void DEBUG_InfoBreakpoints(void)
{
    int i;

    DEBUG_Printf( DBG_CHN_MESG, "Breakpoints:\n" );
    for (i = 1; i < next_bp; i++)
    {
        if (breakpoints[i].refcount && breakpoints[i].type == DBG_BREAK)
        {
            DEBUG_Printf( DBG_CHN_MESG, "%d: %c ", i, breakpoints[i].enabled ? 'y' : 'n');
            DEBUG_PrintAddress( &breakpoints[i].addr, 
				breakpoints[i].is32 ? MODE_32 : MODE_16, TRUE);
            DEBUG_Printf( DBG_CHN_MESG, " (%u)\n", breakpoints[i].refcount );
	    if( breakpoints[i].condition != NULL )
	    {
	        DEBUG_Printf(DBG_CHN_MESG, "\t\tstop when  ");
 		DEBUG_DisplayExpr(breakpoints[i].condition);
		DEBUG_Printf(DBG_CHN_MESG, "\n");
	    }
        }
    }
    DEBUG_Printf( DBG_CHN_MESG, "Watchpoints:\n" );
    for (i = 1; i < next_bp; i++)
    {
        if (breakpoints[i].refcount && breakpoints[i].type == DBG_WATCH)
        {
            DEBUG_Printf( DBG_CHN_MESG, "%d: %c ", i, breakpoints[i].enabled ? 'y' : 'n');
            DEBUG_PrintAddress( &breakpoints[i].addr, 
				breakpoints[i].is32 ? MODE_32 : MODE_16, TRUE);
            DEBUG_Printf( DBG_CHN_MESG, " on %d byte%s (%c)\n", 
		     breakpoints[i].u.w.len + 1, 
		     breakpoints[i].u.w.len > 0 ? "s" : "",
		     breakpoints[i].u.w.rw ? 'W' : 'R');
	    if( breakpoints[i].condition != NULL )
	    {
	        DEBUG_Printf(DBG_CHN_MESG, "\t\tstop when  ");
 		DEBUG_DisplayExpr(breakpoints[i].condition);
		DEBUG_Printf(DBG_CHN_MESG, "\n");
	    }
        }
    }
}

/***********************************************************************
 *           DEBUG_ShallBreak
 *
 * Check whether or not the condition (bp / skipcount) of a break/watch
 * point are met.
 */
static	BOOL DEBUG_ShallBreak( int bpnum )
{
    if ( breakpoints[bpnum].condition != NULL )
    {
        DBG_VALUE value = DEBUG_EvalExpr(breakpoints[bpnum].condition);

        if ( value.type == NULL )
        {
	    /*
	     * Something wrong - unable to evaluate this expression.
	     */
	    DEBUG_Printf(DBG_CHN_MESG, "Unable to evaluate expression ");
	    DEBUG_DisplayExpr(breakpoints[bpnum].condition);
	    DEBUG_Printf(DBG_CHN_MESG, "\nTurning off condition\n");
	    DEBUG_AddBPCondition(bpnum, NULL);
        }
        else if( !DEBUG_GetExprValue( &value, NULL) )
        {
	    return FALSE;
        }
    }
   
    if ( breakpoints[bpnum].skipcount > 0 && --breakpoints[bpnum].skipcount > 0 )
        return FALSE;

   if ((breakpoints[bpnum].type == DBG_BREAK) && breakpoints[bpnum].u.b.func)
       return breakpoints[bpnum].u.b.func();
   return TRUE;
}

/***********************************************************************
 *           DEBUG_ShouldContinue
 *
 * Determine if we should continue execution after a SIGTRAP signal when
 * executing in the given mode.
 */
BOOL DEBUG_ShouldContinue( DBG_ADDR *addr, DWORD code, int * count )
{
    int 	        bpnum;
    DWORD	        oldval;
    int 	        wpnum;
    enum dbg_mode       addr_mode;
    struct symbol_info  syminfo;
    enum exec_mode      mode = DEBUG_CurrThread->exec_mode;

#ifdef __i386__
    /* If not single-stepping, back up over the int3 instruction */
    if (code == EXCEPTION_BREAKPOINT)
    {
       DEBUG_context.Eip--;
       addr->off--;
    }
#endif

    bpnum = DEBUG_FindBreakpoint( addr, DBG_BREAK );
    breakpoints[0].enabled = FALSE;  /* disable the step-over breakpoint */

    if ((bpnum != 0) && (bpnum != -1))
    {
        if (!DEBUG_ShallBreak(bpnum)) return TRUE;

        DEBUG_Printf( DBG_CHN_MESG, "Stopped on breakpoint %d at ", bpnum );
        syminfo = DEBUG_PrintAddress( &breakpoints[bpnum].addr,
				      breakpoints[bpnum].is32 ? MODE_32 : MODE_16, TRUE );
        DEBUG_Printf( DBG_CHN_MESG, "\n" );
	
	if( syminfo.list.sourcefile != NULL )
	    DEBUG_List(&syminfo.list, NULL, 0);
        return FALSE;
    }

    wpnum = DEBUG_FindTriggeredWatchpoint(&oldval);
    if ((wpnum != 0) && (wpnum != -1))
    {
       /* If not single-stepping, do not back up over the int3 instruction */
       if (code == EXCEPTION_BREAKPOINT) 
       {
#ifdef __i386__
	   DEBUG_context.Eip++;
	   addr->off++;
#endif
       }
       if (!DEBUG_ShallBreak(wpnum)) return TRUE;
       
       addr_mode = DEBUG_GetSelectorType( addr->seg );
       DEBUG_Printf(DBG_CHN_MESG, "Stopped on watchpoint %d at ", wpnum);
       syminfo = DEBUG_PrintAddress( addr, addr_mode, TRUE );
       
       DEBUG_Printf(DBG_CHN_MESG, " values: old=%lu new=%lu\n", 
	       oldval, breakpoints[wpnum].u.w.oldval);
       if (syminfo.list.sourcefile != NULL)
	  DEBUG_List(&syminfo.list, NULL, 0);
       return FALSE;
    }

    /*
     * If our mode indicates that we are stepping line numbers,
     * get the current function, and figure out if we are exactly
     * on a line number or not.
     */
    if( mode == EXEC_STEP_OVER || mode == EXEC_STEP_INSTR )
    {
	if( DEBUG_CheckLinenoStatus(addr) == AT_LINENUMBER )
	{
	    (*count)--;
	}
    }
    else if( mode == EXEC_STEPI_OVER || mode == EXEC_STEPI_INSTR )
    {
	(*count)--;
    }

    if( *count > 0 || mode == EXEC_FINISH )
    {
	/*
	 * We still need to execute more instructions.
	 */
	return TRUE;
    }
    
    /*
     * If we are about to stop, then print out the source line if we
     * have it.
     */
    if (mode != EXEC_CONT && mode != EXEC_FINISH)
    {
	DEBUG_FindNearestSymbol( addr, TRUE, NULL, 0, &syminfo.list);
	if( syminfo.list.sourcefile != NULL )
	{
	    DEBUG_List(&syminfo.list, NULL, 0);
	}
    }

#ifdef __i386__
    /* If there's no breakpoint and we are not single-stepping, then we     */
    /* must have encountered an int3 in the Windows program; let's skip it. */
    if ((bpnum == -1) && code == EXCEPTION_BREAKPOINT)
    {
        DEBUG_context.Eip++;
	addr->off++;
    }
#endif

    /* no breakpoint, continue if in continuous mode */
    return (mode == EXEC_CONT || mode == EXEC_FINISH);
}

/***********************************************************************
 *           DEBUG_SuspendExecution
 *
 * Remove all breakpoints before entering the debug loop
 */
void	DEBUG_SuspendExecution( void )
{
   DEBUG_SetBreakpoints( FALSE );
   breakpoints[0] = DEBUG_CurrThread->stepOverBP;
}

/***********************************************************************
 *           DEBUG_RestartExecution
 *
 * Set the breakpoints to the correct state to restart execution
 * in the given mode.
 */
void DEBUG_RestartExecution( int count )
{
    DBG_ADDR addr;
    DBG_ADDR addr2;
    int bp;
    int	delta;
    int status;
    enum exec_mode mode, ret_mode;
    DWORD instr;
    unsigned char ch;

    DEBUG_GetCurrentAddress( &addr );

    /*
     * This is the mode we will be running in after we finish.  We would like
     * to be able to modify this in certain cases.
     */
    ret_mode = mode = DEBUG_CurrThread->exec_mode;

    bp = DEBUG_FindBreakpoint( &addr, DBG_BREAK ); 
    if ( bp != -1 && bp != 0)
      {
	/*
	 * If we have set a new value, then save it in the BP number.
	 */
	if( count != 0 && mode == EXEC_CONT )
	  {
	    breakpoints[bp].skipcount = count;
	  }
        mode = EXEC_STEPI_INSTR;  /* If there's a breakpoint, skip it */
      }
    else
      {
	if( mode == EXEC_CONT && count > 1 )
	  {
	    DEBUG_Printf(DBG_CHN_MESG, "Not stopped at any breakpoint; argument ignored.\n");
	  }
      }
    
    if( mode == EXEC_FINISH && DEBUG_IsFctReturn() )
      {
	mode = ret_mode = EXEC_STEPI_INSTR;
      }

    instr = DEBUG_ToLinear( &addr );
    DEBUG_READ_MEM((void*)instr, &ch, sizeof(ch));
    /*
     * See if the function we are stepping into has debug info
     * and line numbers.  If not, then we step over it instead.
     * FIXME - we need to check for things like thunks or trampolines,
     * as the actual function may in fact have debug info.
     */
    if ( ch == 0xe8 )
      {
	DEBUG_READ_MEM((void*)(instr + 1), &delta, sizeof(delta));
	addr2 = addr;
	DEBUG_Disasm(&addr2, FALSE);
	addr2.off += delta;
	
	status = DEBUG_CheckLinenoStatus(&addr2);
	/*
	 * Anytime we have a trampoline, step over it.
	 */
	if( ((mode == EXEC_STEP_OVER) || (mode == EXEC_STEPI_OVER))
	    && status == FUNC_IS_TRAMPOLINE )
	  {
#if 0
	    DEBUG_Printf(DBG_CHN_MESG, "Not stepping into trampoline at %x (no lines)\n",
		    addr2.off);
#endif
	    mode = EXEC_STEP_OVER_TRAMPOLINE;
	  }
	
	if( mode == EXEC_STEP_INSTR && status == FUNC_HAS_NO_LINES )
	  {
#if 0
	    DEBUG_Printf(DBG_CHN_MESG, "Not stepping into function at %x (no lines)\n",
		    addr2.off);
#endif
	    mode = EXEC_STEP_OVER;
	  }
      }


    if( mode == EXEC_STEP_INSTR )
      {
	if( DEBUG_CheckLinenoStatus(&addr) == FUNC_HAS_NO_LINES )
	  {
	    DEBUG_Printf(DBG_CHN_MESG, "Single stepping until exit from function, \n");
	    DEBUG_Printf(DBG_CHN_MESG, "which has no line number information.\n");
	    
	    ret_mode = mode = EXEC_FINISH;
	  }
      }

    switch(mode)
    {
    case EXEC_CONT: /* Continuous execution */
#ifdef __i386__
        DEBUG_context.EFlags &= ~STEP_FLAG;
#endif
        DEBUG_SetBreakpoints( TRUE );
        break;

    case EXEC_STEP_OVER_TRAMPOLINE:
      /*
       * This is the means by which we step over our conversion stubs
       * in callfrom*.s and callto*.s.  We dig the appropriate address
       * off the stack, and we set the breakpoint there instead of the
       * address just after the call.
       */
#ifdef __i386__
      DEBUG_READ_MEM((void*)(DEBUG_context.Esp + 
			     2 * sizeof(unsigned int)), 
		     &addr.off, sizeof(addr.off));
      DEBUG_context.EFlags &= ~STEP_FLAG;
#endif
      breakpoints[0].addr    = addr;
      breakpoints[0].enabled = TRUE;
      breakpoints[0].refcount = 1;
      breakpoints[0].skipcount = 0;
      DEBUG_READ_MEM((void*)DEBUG_ToLinear( &addr ), &breakpoints[0].u.b.opcode, 
		     sizeof(char));
      DEBUG_SetBreakpoints( TRUE );
      break;

    case EXEC_FINISH:
    case EXEC_STEPI_OVER:  /* Stepping over a call */
    case EXEC_STEP_OVER:  /* Stepping over a call */
        if (DEBUG_IsStepOverInstr())
        {
#ifdef __i386__
            DEBUG_context.EFlags &= ~STEP_FLAG;
#endif
            DEBUG_Disasm(&addr, FALSE);
            breakpoints[0].addr    = addr;
            breakpoints[0].enabled = TRUE;
            breakpoints[0].refcount = 1;
	    breakpoints[0].skipcount = 0;
	    DEBUG_READ_MEM((void*)DEBUG_ToLinear( &addr ), &breakpoints[0].u.b.opcode,
			   sizeof(char));
            DEBUG_SetBreakpoints( TRUE );
            break;
        }
        /* else fall through to single-stepping */

    case EXEC_STEP_INSTR: /* Single-stepping an instruction */
    case EXEC_STEPI_INSTR: /* Single-stepping an instruction */
#ifdef __i386__
        DEBUG_context.EFlags |= STEP_FLAG;
#endif
        break;
    default:
        RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
    }
    DEBUG_CurrThread->stepOverBP = breakpoints[0];
    DEBUG_CurrThread->exec_mode = ret_mode;
}

int
DEBUG_AddBPCondition(int num, struct expr * exp)
{
    if ((num <= 0) || (num >= next_bp) || !breakpoints[num].refcount)
    {
        DEBUG_Printf( DBG_CHN_MESG, "Invalid breakpoint number %d\n", num );
        return FALSE;
    }

    if( breakpoints[num].condition != NULL )
      {
	DEBUG_FreeExpr(breakpoints[num].condition);
	breakpoints[num].condition = NULL;
      }

    if( exp != NULL )
      {
	breakpoints[num].condition = DEBUG_CloneExpr(exp);
      }

   return TRUE;
}