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