Reverse the order for deleting the items in resetcontent to correctly
[wine] / programs / winedbg / 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  * 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.
12  *
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.
17  *
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
21  */
22
23 #include "config.h"
24 #include "debugger.h"
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
28
29 /***********************************************************************
30  *           break_set_xpoints
31  *
32  * Set or remove all the breakpoints & watchpoints
33  */
34 void  break_set_xpoints(BOOL set)
35 {
36     static BOOL                 last; /* = 0 = FALSE */
37
38     unsigned int                i, ret, size;
39     void*                       addr;
40     struct dbg_breakpoint*      bp = dbg_curr_process->bp;
41
42     if (set == last) return;
43     last = set;
44
45     for (i = 0; i < dbg_curr_process->next_bp; i++)
46     {
47         if (!bp[i].refcount || !bp[i].enabled) continue;
48
49         if (bp[i].xpoint_type == be_xpoint_break)
50             size = 0;
51         else
52             size = bp[i].w.len + 1;
53         addr = (void*)memory_to_linear_addr(&bp[i].addr);
54
55         if (set)
56             ret = be_cpu->insert_Xpoint(dbg_curr_process->handle, &dbg_context, 
57                                         bp[i].xpoint_type, addr,
58                                         &bp[i].info, size);
59         else
60             ret = be_cpu->remove_Xpoint(dbg_curr_process->handle, &dbg_context, 
61                                         bp[i].xpoint_type, addr,
62                                         bp[i].info, size);
63         if (!ret)
64         {
65             dbg_printf("Invalid address (");
66             print_address(&bp[i].addr, FALSE);
67             dbg_printf(") for breakpoint %d, disabling it\n", i);
68             bp[i].enabled = FALSE;
69         }
70     }
71 }
72
73 /***********************************************************************
74  *           find_xpoint
75  *
76  * Find the breakpoint for a given address. Return the breakpoint
77  * number or -1 if none.
78  */
79 static int find_xpoint(const ADDRESS* addr, enum be_xpoint_type type)
80 {
81     int                         i;
82     void*                       lin = memory_to_linear_addr(addr);
83     struct dbg_breakpoint*      bp = dbg_curr_process->bp;
84
85     for (i = 0; i < dbg_curr_process->next_bp; i++)
86     {
87         if (bp[i].refcount && bp[i].enabled && bp[i].xpoint_type == type &&
88             memory_to_linear_addr(&bp[i].addr) == lin)
89             return i;
90     }
91     return -1;
92 }
93
94 /***********************************************************************
95  *           init_xpoint
96  *
97  * Find an empty slot in BP table to add a new break/watch point
98  */
99 static  int init_xpoint(int type, const ADDRESS* addr)
100 {
101     int                         num;
102     struct dbg_breakpoint*      bp = dbg_curr_process->bp;
103
104     for (num = (dbg_curr_process->next_bp < MAX_BREAKPOINTS) ? 
105              dbg_curr_process->next_bp++ : 1;
106          num < MAX_BREAKPOINTS; num++)
107     {
108         if (bp[num].refcount == 0)
109         {
110             bp[num].refcount    = 1;
111             bp[num].enabled     = TRUE;
112             bp[num].xpoint_type = type;
113             bp[num].skipcount   = 0;
114             bp[num].addr        = *addr;
115             return num;
116         }
117     }
118
119     dbg_printf("Too many bp. Please delete some.\n");
120     return -1;
121 }
122
123 /***********************************************************************
124  *           get_watched_value
125  *
126  * Returns the value watched by watch point 'num'.
127  */
128 static  BOOL    get_watched_value(int num, LPDWORD val)
129 {
130     BYTE        buf[4];
131
132     if (!dbg_read_memory(memory_to_linear_addr(&dbg_curr_process->bp[num].addr),
133                          buf, dbg_curr_process->bp[num].w.len + 1))
134         return FALSE;
135
136     switch (dbg_curr_process->bp[num].w.len + 1)
137     {
138     case 4:     *val = *(DWORD*)buf;    break;
139     case 2:     *val = *(WORD*)buf;     break;
140     case 1:     *val = *(BYTE*)buf;     break;
141     default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
142     }
143     return TRUE;
144 }
145
146 /***********************************************************************
147  *           break_add_break
148  *
149  * Add a breakpoint.
150  */
151 BOOL break_add_break(const ADDRESS* addr, BOOL verbose)
152 {
153     int                         num;
154     BYTE                        ch;
155     struct dbg_breakpoint*      bp = dbg_curr_process->bp;
156
157     if ((num = find_xpoint(addr, be_xpoint_break)) >= 1)
158     {
159         bp[num].refcount++;
160         dbg_printf("Breakpoint %d at ", num);
161         print_address(&bp[num].addr, TRUE);
162         dbg_printf(" (refcount=%d)\n", bp[num].refcount);
163         return TRUE;
164     }
165
166     if (!dbg_read_memory(memory_to_linear_addr(addr), &ch, sizeof(ch)))
167     {
168         if (verbose)
169         {
170             dbg_printf("Invalid address ");
171             print_bare_address(addr);
172             dbg_printf(", can't set breakpoint\n");
173         }
174         return FALSE;
175     }
176
177     if ((num = init_xpoint(be_xpoint_break, addr)) == -1)
178         return FALSE;
179
180     dbg_printf("Breakpoint %d at ", num);
181     print_address(&bp[num].addr, TRUE);
182     dbg_printf("\n");
183
184     return TRUE;
185 }
186
187 /***********************************************************************
188  *           break_add_break_from_lvalue
189  *
190  * Add a breakpoint.
191  */
192 BOOL break_add_break_from_lvalue(const struct dbg_lvalue* lvalue)
193 {
194     ADDRESS     addr;
195
196     addr.Mode = AddrModeFlat;
197     addr.Offset = types_extract_as_integer(lvalue);
198
199     if (!break_add_break(&addr, TRUE))
200     {
201         if (!DBG_IVAR(CanDeferOnBPByAddr))
202         {
203             dbg_printf("Invalid address, can't set breakpoint\n"
204                        "You can turn on deferring bp by address by setting $CanDeferOnBPByAddr to 1\n");
205             return FALSE;
206         }
207         dbg_printf("Unable to add breakpoint, will check again any time a new DLL is loaded\n");
208         dbg_curr_process->delayed_bp = 
209             dbg_heap_realloc(dbg_curr_process->delayed_bp,
210                              sizeof(struct dbg_delayed_bp) * ++dbg_curr_process->num_delayed_bp);
211
212         dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].is_symbol = FALSE;
213         dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].u.addr    = addr;
214         return TRUE;
215     }
216     return FALSE;
217 }
218
219 /***********************************************************************
220  *           break_add_break_from_id
221  *
222  * Add a breakpoint from a function name (and eventually a line #)
223  */
224 void    break_add_break_from_id(const char *name, int lineno)
225 {
226     struct dbg_lvalue   lvalue;
227     int                 i;
228
229     switch (symbol_get_lvalue(name, lineno, &lvalue, TRUE))
230     {
231     case sglv_found:
232         break_add_break(&lvalue.addr, TRUE);
233         return;
234     case sglv_unknown:
235         break;
236     case sglv_aborted: /* user aborted symbol lookup */
237         return;
238     }
239
240     dbg_printf("Unable to add breakpoint, will check again when a new DLL is loaded\n");
241     for (i = 0; i < dbg_curr_process->num_delayed_bp; i++)
242     {
243         if (dbg_curr_process->delayed_bp[i].is_symbol &&
244             !strcmp(name, dbg_curr_process->delayed_bp[i].u.symbol.name) &&
245             lineno == dbg_curr_process->delayed_bp[i].u.symbol.lineno)
246             return;
247     }
248     dbg_curr_process->delayed_bp = dbg_heap_realloc(dbg_curr_process->delayed_bp,
249                                                     sizeof(struct dbg_delayed_bp) * ++dbg_curr_process->num_delayed_bp);
250
251     dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].is_symbol = TRUE;
252     dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].u.symbol.name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(name) + 1), name);
253     dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].u.symbol.lineno = lineno;
254 }
255
256 /***********************************************************************
257  *           break_add_break_from_lineno
258  *
259  * Add a breakpoint from a line number in current file
260  */
261 void    break_add_break_from_lineno(int lineno)
262 {
263     ADDRESS             addr;
264
265     memory_get_current_pc(&addr);
266
267     if (lineno != -1)
268     {
269         IMAGEHLP_LINE   il;
270         IMAGEHLP_LINE   iil;
271         BOOL            found = FALSE;
272         DWORD           disp;
273
274         il.SizeOfStruct = sizeof(il);
275         if (!SymGetLineFromAddr(dbg_curr_process->handle, 
276                                 (DWORD)memory_to_linear_addr(&addr), &disp, &il))
277         {
278             dbg_printf("Unable to add breakpoint (unknown address)\n");
279             return;
280         }
281
282         iil = il;
283         while (SymGetLinePrev(dbg_curr_process->handle, &iil))
284         {
285             if (lineno == iil.LineNumber && !strcmp(il.FileName, iil.FileName))
286             {
287                 addr.Mode = AddrModeFlat;
288                 addr.Offset = iil.Address;
289                 found = TRUE;
290                 break;
291             }
292         }
293         iil = il;
294         if (!found) while (SymGetLineNext(dbg_curr_process->handle, &iil))
295         {
296             if (lineno == iil.LineNumber && !strcmp(il.FileName, iil.FileName))
297             {
298                 addr.Mode = AddrModeFlat;
299                 addr.Offset = iil.Address;
300                 found = TRUE;
301                 break;
302             }
303         }
304         if (!found)
305         {
306             dbg_printf("Unknown line number\n"
307                        "(either out of file, or no code at given line number)\n");
308             return;
309         }
310     }
311
312     break_add_break(&addr, TRUE);
313 }
314
315 /***********************************************************************
316  *           break_check_delayed_bp
317  *
318  * Check is a registered delayed BP is now available.
319  */
320 void break_check_delayed_bp(void)
321 {
322     struct dbg_lvalue           lvalue;
323     int                         i;
324     struct dbg_delayed_bp*      dbp = dbg_curr_process->delayed_bp;
325
326     for (i = 0; i < dbg_curr_process->num_delayed_bp; i++)
327     {
328         if (dbp[i].is_symbol)
329         {
330             if (symbol_get_lvalue(dbp[i].u.symbol.name, dbp[i].u.symbol.lineno,
331                                   &lvalue, TRUE) != sglv_found)
332                 continue;
333             if (lvalue.cookie != DLV_TARGET) continue;
334         }
335         else
336             lvalue.addr = dbp[i].u.addr;
337         WINE_TRACE("trying to add delayed %s-bp\n", dbp[i].is_symbol ? "S" : "A");
338         if (!dbp[i].is_symbol)
339             WINE_TRACE("\t%04x:%08lx\n", 
340                        dbp[i].u.addr.Segment, dbp[i].u.addr.Offset);
341         else
342             WINE_TRACE("\t'%s' @ %d\n", 
343                        dbp[i].u.symbol.name, dbp[i].u.symbol.lineno);
344
345         if (break_add_break(&lvalue.addr, FALSE))
346             memmove(&dbp[i], &dbp[i+1], (--dbg_curr_process->num_delayed_bp - i) * sizeof(*dbp));
347     }
348 }
349
350 /***********************************************************************
351  *           break_add_watch
352  *
353  * Add a watchpoint.
354  */
355 static void break_add_watch(const struct dbg_lvalue* lvalue, BOOL is_write)
356 {
357     int         num;
358     DWORD       l = 4;
359
360     num = init_xpoint((is_write) ? be_xpoint_watch_write : be_xpoint_watch_read,
361                       &lvalue->addr);
362     if (num == -1) return;
363
364     if (lvalue->type.id != dbg_itype_none)
365     {
366         if (types_get_info(&lvalue->type, TI_GET_LENGTH, &l))
367         {
368             switch (l)
369             {
370             case 4: case 2: case 1: break;
371             default:
372                 dbg_printf("Unsupported length (%lu) for watch-points, defaulting to 4\n", l);
373                 break;
374             }
375         }
376         else dbg_printf("Cannot get watch size, defaulting to 4\n");
377     }
378     dbg_curr_process->bp[num].w.len = l - 1;
379
380     if (!get_watched_value(num, &dbg_curr_process->bp[num].w.oldval))
381     {
382         dbg_printf("Bad address. Watchpoint not set\n");
383         dbg_curr_process->bp[num].refcount = 0;
384         return;
385     }
386     dbg_printf("Watchpoint %d at ", num);
387     print_address(&dbg_curr_process->bp[num].addr, TRUE);
388     dbg_printf("\n");
389 }
390
391 /******************************************************************
392  *              break_add_watch_from_lvalue
393  *
394  * Adds a watch point from an address (stored in a lvalue)
395  */
396 void break_add_watch_from_lvalue(const struct dbg_lvalue* lvalue)
397 {
398     struct dbg_lvalue   lval;
399
400     lval.addr.Mode = AddrModeFlat;
401     lval.addr.Offset = types_extract_as_integer(lvalue);
402     lval.type.id = dbg_itype_none;
403
404     break_add_watch(&lval, TRUE);
405 }
406
407 /***********************************************************************
408  *           break_add_watch_from_id
409  *
410  * Add a watchpoint from a symbol name
411  */
412 void    break_add_watch_from_id(const char *name)
413 {
414     struct dbg_lvalue    lvalue;
415
416     switch (symbol_get_lvalue(name, -1, &lvalue, TRUE))
417     {
418     case sglv_found:
419         break_add_watch(&lvalue, 1);
420         break;
421     case sglv_unknown:
422         dbg_printf("Unable to add watchpoint\n");
423         break;
424     case sglv_aborted: /* user aborted symbol lookup */
425         break;
426     }
427 }
428
429 /***********************************************************************
430  *           break_delete_xpoint
431  *
432  * Delete a breakpoint.
433  */
434 void break_delete_xpoint(int num)
435 {
436     struct dbg_breakpoint*      bp = dbg_curr_process->bp;
437
438     if ((num <= 0) || (num >= dbg_curr_process->next_bp) ||
439         bp[num].refcount == 0)
440     {
441         dbg_printf("Invalid breakpoint number %d\n", num);
442         return;
443     }
444
445     if (--bp[num].refcount > 0)
446         return;
447
448     if (bp[num].condition != NULL)
449     {
450         expr_free(bp[num].condition);
451         bp[num].condition = NULL;
452     }
453
454     bp[num].enabled = FALSE;
455     bp[num].refcount = 0;
456     bp[num].skipcount = 0;
457 }
458
459 static inline BOOL module_is_container(const IMAGEHLP_MODULE* wmod_cntnr,
460                                      const IMAGEHLP_MODULE* wmod_child)
461 {
462     return wmod_cntnr->BaseOfImage <= wmod_child->BaseOfImage &&
463         (DWORD)wmod_cntnr->BaseOfImage + wmod_cntnr->ImageSize >=
464         (DWORD)wmod_child->BaseOfImage + wmod_child->ImageSize;
465 }
466
467 /******************************************************************
468  *              break_delete_xpoints_from_module
469  *
470  * Remove all Xpoints from module which base is 'base'
471  */
472 void break_delete_xpoints_from_module(unsigned long base)
473 {
474     IMAGEHLP_MODULE             im, im_elf;
475     int                         i;
476     DWORD                       linear;
477     struct dbg_breakpoint*      bp = dbg_curr_process->bp;
478
479     /* FIXME: should do it also on the ELF sibbling if any */
480     im.SizeOfStruct = sizeof(im);
481     im_elf.SizeOfStruct = sizeof(im_elf);
482     if (!SymGetModuleInfo(dbg_curr_process->handle, base, &im)) return;
483
484     /* try to get in fact the underlying ELF module (if any) */
485     if (SymGetModuleInfo(dbg_curr_process->handle, im.BaseOfImage - 1, &im_elf) &&
486         im_elf.BaseOfImage <= im.BaseOfImage &&
487         (DWORD)im_elf.BaseOfImage + im_elf.ImageSize >= (DWORD)im.BaseOfImage + im.ImageSize)
488         im = im_elf;
489
490     for (i = 0; i < dbg_curr_process->next_bp; i++)
491     {
492         linear = (DWORD)memory_to_linear_addr(&bp[i].addr);
493         if (bp[i].refcount && bp[i].enabled &&
494             im.BaseOfImage <= linear && linear < im.BaseOfImage + im.ImageSize)
495         {
496             break_delete_xpoint(i);
497         }
498     }
499 }
500
501 /***********************************************************************
502  *           break_enable_xpoint
503  *
504  * Enable or disable a break point.
505  */
506 void break_enable_xpoint(int num, BOOL enable)
507 {
508     if ((num <= 0) || (num >= dbg_curr_process->next_bp) || 
509         dbg_curr_process->bp[num].refcount == 0)
510     {
511         dbg_printf("Invalid breakpoint number %d\n", num);
512         return;
513     }
514     dbg_curr_process->bp[num].enabled = (enable) ? TRUE : FALSE;
515     dbg_curr_process->bp[num].skipcount = 0;
516 }
517
518
519 /***********************************************************************
520  *           find_triggered_watch
521  *
522  * Lookup the watchpoints to see if one has been triggered
523  * Return >= (watch point index) if one is found and *oldval is set to
524  *      the value watched before the TRAP
525  * Return -1 if none found (*oldval is undetermined)
526  *
527  * Unfortunately, Linux does *NOT* (A REAL PITA) report with ptrace
528  * the DR6 register value, so we have to look with our own need the
529  * cause of the TRAP.
530  * -EP
531  */
532 static int find_triggered_watch(LPDWORD oldval)
533 {
534     int                         found = -1;
535     int                         i;
536     struct dbg_breakpoint*      bp = dbg_curr_process->bp;
537
538     /* Method 1 => get triggered watchpoint from context (doesn't work on Linux
539      * 2.2.x). This should be fixed in >= 2.2.16
540      */
541     for (i = 0; i < dbg_curr_process->next_bp; i++)
542     {
543         DWORD val = 0;
544
545         if (bp[i].refcount && bp[i].enabled && bp[i].xpoint_type != be_xpoint_break &&
546             (be_cpu->is_watchpoint_set(&dbg_context, bp[i].info)))
547         {
548             be_cpu->clear_watchpoint(&dbg_context, bp[i].info);
549
550             *oldval = bp[i].w.oldval;
551             if (get_watched_value(i, &val))
552             {
553                 bp[i].w.oldval = val;
554                 return i;
555             }
556         }
557     }
558
559     /* Method 1 failed, trying method 2 */
560
561     /* Method 2 => check if value has changed among registered watchpoints
562      * this really sucks, but this is how gdb 4.18 works on my linux box
563      * -EP
564      */
565     for (i = 0; i < dbg_curr_process->next_bp; i++)
566     {
567         DWORD val = 0;
568
569         if (bp[i].refcount && bp[i].enabled && bp[i].xpoint_type != be_xpoint_break &&
570             get_watched_value(i, &val))
571         {
572             *oldval = bp[i].w.oldval;
573             if (val != *oldval)
574             {
575                 be_cpu->clear_watchpoint(&dbg_context, bp[i].info);
576                 bp[i].w.oldval = val;
577                 found = i;
578                 /* cannot break, because two watch points may have been triggered on
579                  * the same access
580                  * only one will be reported to the user (FIXME ?)
581                  */
582             }
583         }
584     }
585     return found;
586 }
587
588 /***********************************************************************
589  *           break_info
590  *
591  * Display break & watch points information.
592  */
593 void break_info(void)
594 {
595     int                         i;
596     int                         nbp = 0, nwp = 0;
597     struct dbg_delayed_bp*      dbp = dbg_curr_process->delayed_bp;
598     struct dbg_breakpoint*      bp = dbg_curr_process->bp;
599
600     for (i = 1; i < dbg_curr_process->next_bp; i++)
601     {
602         if (bp[i].refcount)
603         {
604             if (bp[i].xpoint_type == be_xpoint_break) nbp++; else nwp++;
605         }
606     }
607
608     if (nbp)
609     {
610         dbg_printf("Breakpoints:\n");
611         for (i = 1; i < dbg_curr_process->next_bp; i++)
612         {
613             if (!bp[i].refcount || bp[i].xpoint_type != be_xpoint_break)
614                 continue;
615             dbg_printf("%d: %c ", i, bp[i].enabled ? 'y' : 'n');
616             print_address(&bp[i].addr, TRUE);
617             dbg_printf(" (%u)\n", bp[i].refcount);
618             if (bp[i].condition != NULL)
619             {
620                 dbg_printf("\t\tstop when  ");
621                 expr_print(bp[i].condition);
622                 dbg_printf("\n");
623             }
624         }
625     }
626     else dbg_printf("No breakpoints\n");
627     if (nwp)
628     {
629         dbg_printf("Watchpoints:\n");
630         for (i = 1; i < dbg_curr_process->next_bp; i++)
631         {
632             if (!bp[i].refcount || bp[i].xpoint_type == be_xpoint_break)
633                 continue;
634             dbg_printf("%d: %c ", i, bp[i].enabled ? 'y' : 'n');
635             print_address(&bp[i].addr, TRUE);
636             dbg_printf(" on %d byte%s (%c)\n",
637                        bp[i].w.len + 1, bp[i].w.len > 0 ? "s" : "",
638                        bp[i].xpoint_type == be_xpoint_watch_write ? 'W' : 'R');
639             if (bp[i].condition != NULL)
640             {
641                 dbg_printf("\t\tstop when ");
642                 expr_print(bp[i].condition);
643                 dbg_printf("\n");
644             }
645         }
646     }
647     else dbg_printf("No watchpoints\n");
648     if (dbg_curr_process->num_delayed_bp)
649     {
650         dbg_printf("Delayed breakpoints:\n");
651         for (i = 0; i < dbg_curr_process->num_delayed_bp; i++)
652         {
653             if (dbp[i].is_symbol)
654             {
655                 dbg_printf("%d: %s", i, dbp[i].u.symbol.name);
656                 if (dbp[i].u.symbol.lineno != -1)
657                     dbg_printf(" at line %u", dbp[i].u.symbol.lineno);
658             }
659             else
660             {
661                 dbg_printf("%d: ", i);
662                 print_address(&dbp[i].u.addr, FALSE);
663             }
664             dbg_printf("\n");
665         }
666     }
667 }
668
669 /***********************************************************************
670  *           should_stop
671  *
672  * Check whether or not the condition (bp / skipcount) of a break/watch
673  * point are met.
674  */
675 static  BOOL should_stop(int bpnum)
676 {
677     struct dbg_breakpoint*      bp = &dbg_curr_process->bp[bpnum];
678
679     if (bp->condition != NULL)
680     {
681         struct dbg_lvalue lvalue = expr_eval(bp->condition);
682
683         if (lvalue.type.id == dbg_itype_none)
684         {
685             /*
686              * Something wrong - unable to evaluate this expression.
687              */
688             dbg_printf("Unable to evaluate expression ");
689             expr_print(bp->condition);
690             dbg_printf("\nTurning off condition\n");
691             break_add_condition(bpnum, NULL);
692         }
693         else if (!types_extract_as_integer(&lvalue))
694         {
695             return FALSE;
696         }
697     }
698
699     if (bp->skipcount > 0) bp->skipcount--;
700     return bp->skipcount == 0;
701 }
702
703 /***********************************************************************
704  *           break_should_continue
705  *
706  * Determine if we should continue execution after a SIGTRAP signal when
707  * executing in the given mode.
708  */
709 BOOL break_should_continue(ADDRESS* addr, DWORD code, int* count, BOOL* is_break)
710 {
711     int                 bpnum;
712     DWORD               oldval;
713     int                 wpnum;
714     enum dbg_exec_mode  mode = dbg_curr_thread->exec_mode;
715
716     *is_break = FALSE;
717     /* If not single-stepping, back up to the break instruction */
718     if (code == EXCEPTION_BREAKPOINT)
719         addr->Offset += be_cpu->adjust_pc_for_break(&dbg_context, TRUE);
720
721     bpnum = find_xpoint(addr, be_xpoint_break);
722     dbg_curr_process->bp[0].enabled = FALSE;  /* disable the step-over breakpoint */
723
724     if (bpnum > 0)
725     {
726         if (!should_stop(bpnum)) return TRUE;
727
728         dbg_printf("Stopped on breakpoint %d at ", bpnum);
729         print_address(&dbg_curr_process->bp[bpnum].addr, TRUE);
730         dbg_printf("\n");
731         return FALSE;
732     }
733
734     wpnum = find_triggered_watch(&oldval);
735     if (wpnum > 0)
736     {
737         /* If not single-stepping, do not back up over the break instruction */
738         if (code == EXCEPTION_BREAKPOINT)
739             addr->Offset += be_cpu->adjust_pc_for_break(&dbg_context, FALSE);
740
741         if (!should_stop(wpnum)) return TRUE;
742
743         dbg_printf("Stopped on watchpoint %d at ", wpnum);
744         print_address(addr, TRUE);
745         dbg_printf(" values: old=%lu new=%lu\n",
746                      oldval, dbg_curr_process->bp[wpnum].w.oldval);
747         return FALSE;
748     }
749
750     /*
751      * If our mode indicates that we are stepping line numbers,
752      * get the current function, and figure out if we are exactly
753      * on a line number or not.
754      */
755     if (mode == dbg_exec_step_over_line || mode == dbg_exec_step_into_line)
756     {
757         if (symbol_get_function_line_status(addr) == dbg_on_a_line_number)
758         {
759             (*count)--;
760         }
761     }
762     else if (mode == dbg_exec_step_over_insn || mode == dbg_exec_step_into_insn)
763     {
764         (*count)--;
765     }
766
767     if (*count > 0 || mode == dbg_exec_finish)
768     {
769         /*
770          * We still need to execute more instructions.
771          */
772         return TRUE;
773     }
774
775     /* If there's no breakpoint and we are not single-stepping, then
776      * either we must have encountered a break insn in the Windows program
777      * or someone is trying to stop us
778      */
779     if (bpnum == -1 && code == EXCEPTION_BREAKPOINT)
780     {
781         *is_break = TRUE;
782         addr->Offset += be_cpu->adjust_pc_for_break(&dbg_context, FALSE);
783         return FALSE;
784     }
785
786     /* no breakpoint, continue if in continuous mode */
787     return mode == dbg_exec_cont || mode == dbg_exec_finish;
788 }
789
790 /***********************************************************************
791  *           break_suspend_execution
792  *
793  * Remove all bp before entering the debug loop
794  */
795 void    break_suspend_execution(void)
796 {
797     break_set_xpoints(FALSE);
798     dbg_curr_process->bp[0] = dbg_curr_thread->step_over_bp;
799 }
800
801 /***********************************************************************
802  *           break_restart_execution
803  *
804  * Set the bp to the correct state to restart execution
805  * in the given mode.
806  */
807 void break_restart_execution(int count)
808 {
809     ADDRESS                     addr;
810     int                         bp;
811     enum dbg_line_status        status;
812     enum dbg_exec_mode          mode, ret_mode;
813     ADDRESS                     callee;
814     void*                       linear;
815
816     memory_get_current_pc(&addr);
817     linear = memory_to_linear_addr(&addr);
818
819     /*
820      * This is the mode we will be running in after we finish.  We would like
821      * to be able to modify this in certain cases.
822      */
823     ret_mode = mode = dbg_curr_thread->exec_mode;
824
825     bp = find_xpoint(&addr, be_xpoint_break);
826     if (bp != -1 && bp != 0)
827     {
828         /*
829          * If we have set a new value, then save it in the BP number.
830          */
831         if (count != 0 && mode == dbg_exec_cont)
832         {
833             dbg_curr_process->bp[bp].skipcount = count;
834         }
835         mode = dbg_exec_step_into_insn;  /* If there's a breakpoint, skip it */
836     }
837     else if (mode == dbg_exec_cont && count > 1)
838     {
839         dbg_printf("Not stopped at any breakpoint; argument ignored.\n");
840     }
841
842     if (mode == dbg_exec_finish && be_cpu->is_function_return(linear))
843     {
844         mode = ret_mode = dbg_exec_step_into_insn;
845     }
846
847     /*
848      * See if the function we are stepping into has debug info
849      * and line numbers.  If not, then we step over it instead.
850      * FIXME - we need to check for things like thunks or trampolines,
851      * as the actual function may in fact have debug info.
852      */
853     if (be_cpu->is_function_call(linear, &callee))
854     {
855         status = symbol_get_function_line_status(&callee);
856 #if 0
857         /* FIXME: we need to get the thunk type */
858         /*
859          * Anytime we have a trampoline, step over it.
860          */
861         if ((mode == EXEC_STEP_OVER || mode == EXEC_STEPI_OVER)
862             && status == dbg_in_a_thunk)
863         {
864             WINE_WARN("Not stepping into trampoline at %p (no lines)\n", 
865                       memory_to_linear_addr(&callee));
866             mode = EXEC_STEP_OVER_TRAMPOLINE;
867         }
868 #endif
869         if (mode == dbg_exec_step_into_line && status == dbg_no_line_info)
870         {
871             WINE_WARN("Not stepping into function at %p (no lines)\n",
872                       memory_to_linear_addr(&callee));
873             mode = dbg_exec_step_over_line;
874         }
875     }
876
877     if (mode == dbg_exec_step_into_line && 
878         symbol_get_function_line_status(&addr) == dbg_no_line_info)
879     {
880         dbg_printf("Single stepping until exit from function, \n"
881                    "which has no line number information.\n");
882         ret_mode = mode = dbg_exec_finish;
883     }
884
885     switch (mode)
886     {
887     case dbg_exec_cont: /* Continuous execution */
888         be_cpu->single_step(&dbg_context, FALSE);
889         break_set_xpoints(TRUE);
890         break;
891
892 #if 0
893     case EXEC_STEP_OVER_TRAMPOLINE:
894         /*
895          * This is the means by which we step over our conversion stubs
896          * in callfrom*.s and callto*.s.  We dig the appropriate address
897          * off the stack, and we set the breakpoint there instead of the
898          * address just after the call.
899          */
900         be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
901                          be_cpu_addr_stack, &addr);
902         /* FIXME: we assume stack grows as on a i386 */
903         addr.Offset += 2 * sizeof(unsigned int);
904         dbg_read_memory(memory_to_linear_addr(&addr),
905                         &addr.Offset, sizeof(addr.Offset));
906         dbg_curr_process->bp[0].addr = addr;
907         dbg_curr_process->bp[0].enabled = TRUE;
908         dbg_curr_process->bp[0].refcount = 1;
909         dbg_curr_process->bp[0].skipcount = 0;
910         dbg_curr_process->bp[0].xpoint_type = be_xpoint_break;
911         dbg_curr_process->bp[0].condition = NULL;
912         be_cpu->single_step(&dbg_context, FALSE);
913         break_set_xpoints(TRUE);
914         break;
915 #endif
916
917     case dbg_exec_finish:
918     case dbg_exec_step_over_insn:  /* Stepping over a call */
919     case dbg_exec_step_over_line:  /* Stepping over a call */
920         if (be_cpu->is_step_over_insn(linear))
921         {
922             be_cpu->disasm_one_insn(&addr, FALSE);
923             dbg_curr_process->bp[0].addr = addr;
924             dbg_curr_process->bp[0].enabled = TRUE;
925             dbg_curr_process->bp[0].refcount = 1;
926             dbg_curr_process->bp[0].skipcount = 0;
927             dbg_curr_process->bp[0].xpoint_type = be_xpoint_break;
928             dbg_curr_process->bp[0].condition = NULL;
929             be_cpu->single_step(&dbg_context, FALSE);
930             break_set_xpoints(TRUE);
931             break;
932         }
933         /* else fall through to single-stepping */
934
935     case dbg_exec_step_into_line: /* Single-stepping a line */
936     case dbg_exec_step_into_insn: /* Single-stepping an instruction */
937         be_cpu->single_step(&dbg_context, TRUE);
938         break;
939     default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
940     }
941     dbg_curr_thread->step_over_bp = dbg_curr_process->bp[0];
942     dbg_curr_thread->exec_mode = ret_mode;
943 }
944
945 int break_add_condition(int num, struct expr* exp)
946 {
947     if (num <= 0 || num >= dbg_curr_process->next_bp || 
948         !dbg_curr_process->bp[num].refcount)
949     {
950         dbg_printf("Invalid breakpoint number %d\n", num);
951         return FALSE;
952     }
953
954     if (dbg_curr_process->bp[num].condition != NULL)
955     {
956         expr_free(dbg_curr_process->bp[num].condition);
957         dbg_curr_process->bp[num].condition = NULL;
958     }
959
960     if (exp != NULL) dbg_curr_process->bp[num].condition = expr_clone(exp, NULL);
961
962     return TRUE;
963 }