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