mscoree: Update Wine Mono to 9.2.0.
[wine.git] / programs / winedbg / break.c
blob8001ba1707b299bf73d07a67c82a0fcd6a68f32b
1 /*
2 * Debugger break-points handling
4 * Copyright 1994 Martin von Loewis
5 * Copyright 1995 Alexandre Julliard
6 * Copyright 1999,2000 Eric Pouech
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.
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.
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
23 #include "debugger.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
28 static BOOL is_xpoint_break(int bpnum)
30 int type = dbg_curr_process->bp[bpnum].xpoint_type;
32 if (type == be_xpoint_break || type == be_xpoint_watch_exec) return TRUE;
33 if (type == be_xpoint_watch_read || type == be_xpoint_watch_write) return FALSE;
34 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
35 return FALSE; /* never reached */
38 /***********************************************************************
39 * break_set_xpoints
41 * Set or remove all the breakpoints & watchpoints
43 void break_set_xpoints(BOOL set)
45 static BOOL last; /* = 0 = FALSE */
47 unsigned int i, ret, size;
48 void* addr;
49 struct dbg_breakpoint* bp = dbg_curr_process->bp;
51 if (set == last) return;
52 last = set;
54 for (i = 0; i < dbg_curr_process->next_bp; i++)
56 if (!bp[i].refcount || !bp[i].enabled) continue;
58 if (is_xpoint_break(i))
59 size = 0;
60 else
61 size = bp[i].w.len + 1;
62 addr = memory_to_linear_addr(&bp[i].addr);
64 if (set)
65 ret = dbg_curr_process->be_cpu->insert_Xpoint(dbg_curr_process->handle,
66 dbg_curr_process->process_io, &dbg_context, bp[i].xpoint_type,
67 addr, &bp[i].info, size);
68 else
69 ret = dbg_curr_process->be_cpu->remove_Xpoint(dbg_curr_process->handle,
70 dbg_curr_process->process_io, &dbg_context, bp[i].xpoint_type,
71 addr, bp[i].info, size);
72 if (!ret)
74 dbg_printf("Invalid address (");
75 print_address(&bp[i].addr, FALSE);
76 dbg_printf(") for breakpoint %d, disabling it\n", i);
77 bp[i].enabled = FALSE;
82 /***********************************************************************
83 * find_xpoint
85 * Find the breakpoint for a given address. Return the breakpoint
86 * number or -1 if none.
88 static int find_xpoint(const ADDRESS64* addr, enum be_xpoint_type type)
90 int i;
91 void* lin = memory_to_linear_addr(addr);
92 struct dbg_breakpoint* bp = dbg_curr_process->bp;
94 for (i = 0; i < dbg_curr_process->next_bp; i++)
96 if (bp[i].refcount && bp[i].enabled && bp[i].xpoint_type == type &&
97 memory_to_linear_addr(&bp[i].addr) == lin)
98 return i;
100 return -1;
103 /***********************************************************************
104 * init_xpoint
106 * Find an empty slot in BP table to add a new break/watch point
108 static int init_xpoint(int type, const ADDRESS64* addr)
110 int num;
111 struct dbg_breakpoint* bp = dbg_curr_process->bp;
113 for (num = (dbg_curr_process->next_bp < MAX_BREAKPOINTS) ?
114 dbg_curr_process->next_bp++ : 1;
115 num < MAX_BREAKPOINTS; num++)
117 if (bp[num].refcount == 0)
119 bp[num].refcount = 1;
120 bp[num].enabled = TRUE;
121 bp[num].xpoint_type = type;
122 bp[num].skipcount = 0;
123 bp[num].addr = *addr;
124 return num;
128 dbg_printf("Too many bp. Please delete some.\n");
129 return -1;
132 /***********************************************************************
133 * get_watched_value
135 * Returns the value watched by watch point 'num'.
137 static BOOL get_watched_value(int num, DWORD64* val)
139 DWORD64 buf[1];
141 if (!dbg_read_memory(memory_to_linear_addr(&dbg_curr_process->bp[num].addr),
142 buf, dbg_curr_process->bp[num].w.len + 1))
143 return FALSE;
145 switch (dbg_curr_process->bp[num].w.len + 1)
147 case 8: *val = *(DWORD64*)buf; break;
148 case 4: *val = *(DWORD*)buf; break;
149 case 2: *val = *(WORD*)buf; break;
150 case 1: *val = *(BYTE*)buf; break;
151 default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
153 return TRUE;
156 /***********************************************************************
157 * break_add_break
159 * Add a breakpoint.
161 BOOL break_add_break(const ADDRESS64* addr, BOOL verbose, BOOL swbp)
163 int num;
164 BYTE ch;
165 struct dbg_breakpoint* bp = dbg_curr_process->bp;
166 int type = swbp ? be_xpoint_break : be_xpoint_watch_exec;
168 if ((num = find_xpoint(addr, type)) >= 1)
170 bp[num].refcount++;
171 dbg_printf("Breakpoint %d at ", num);
172 print_address(&bp[num].addr, TRUE);
173 dbg_printf(" (refcount=%d)\n", bp[num].refcount);
174 return TRUE;
177 if (!dbg_read_memory(memory_to_linear_addr(addr), &ch, sizeof(ch)))
179 if (verbose)
181 dbg_printf("Invalid address ");
182 print_bare_address(addr);
183 dbg_printf(", can't set breakpoint\n");
185 return FALSE;
188 if ((num = init_xpoint(type, addr)) == -1)
189 return FALSE;
191 dbg_printf("Breakpoint %d at ", num);
192 print_address(&bp[num].addr, TRUE);
193 dbg_printf("\n");
195 return TRUE;
198 /***********************************************************************
199 * break_add_break_from_lvalue
201 * Add a breakpoint.
203 BOOL break_add_break_from_lvalue(const struct dbg_lvalue* lvalue, BOOL swbp)
205 struct dbg_delayed_bp* new;
206 ADDRESS64 addr;
208 types_extract_as_address(lvalue, &addr);
210 if (!break_add_break(&addr, TRUE, swbp))
212 if (!DBG_IVAR(CanDeferOnBPByAddr))
214 dbg_printf("Invalid address, can't set breakpoint\n"
215 "You can turn on deferring bp by address by setting $CanDeferOnBPByAddr to 1\n");
216 return FALSE;
218 dbg_printf("Unable to add breakpoint, will check again any time a new DLL is loaded\n");
219 new = realloc(dbg_curr_process->delayed_bp,
220 sizeof(struct dbg_delayed_bp) * (dbg_curr_process->num_delayed_bp + 1));
221 if (!new) return FALSE;
222 dbg_curr_process->delayed_bp = new;
224 dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp].is_symbol = FALSE;
225 dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp].software_bp = swbp;
226 dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp].u.addr = addr;
227 dbg_curr_process->num_delayed_bp++;
228 return TRUE;
230 return FALSE;
233 /***********************************************************************
234 * break_add_break_from_id
236 * Add a breakpoint from a function name (and eventually a line #)
238 void break_add_break_from_id(const char *name, int lineno, BOOL swbp)
240 struct dbg_delayed_bp* new;
241 struct dbg_lvalue lvalue;
242 int i;
244 switch (symbol_get_lvalue(name, lineno, &lvalue, TRUE))
246 case sglv_found:
247 break_add_break(&lvalue.addr, TRUE, swbp);
248 return;
249 case sglv_unknown:
250 break;
251 case sglv_aborted: /* user aborted symbol lookup */
252 return;
255 dbg_printf("Unable to add breakpoint, will check again when a new DLL is loaded\n");
256 for (i = 0; i < dbg_curr_process->num_delayed_bp; i++)
258 if (dbg_curr_process->delayed_bp[i].is_symbol &&
259 !strcmp(name, dbg_curr_process->delayed_bp[i].u.symbol.name) &&
260 lineno == dbg_curr_process->delayed_bp[i].u.symbol.lineno)
261 return;
263 new = realloc(dbg_curr_process->delayed_bp,
264 sizeof(struct dbg_delayed_bp) * (dbg_curr_process->num_delayed_bp + 1));
265 if (!new) return;
266 dbg_curr_process->delayed_bp = new;
267 dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp].is_symbol = TRUE;
268 dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp].software_bp = swbp;
269 dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp].u.symbol.name = strdup(name);
270 dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp].u.symbol.lineno = lineno;
271 dbg_curr_process->num_delayed_bp++;
274 struct cb_break_lineno
276 const char* filename;
277 int lineno;
278 ADDRESS64 addr;
281 static BOOL CALLBACK line_cb(SRCCODEINFO* sci, void* user)
283 struct cb_break_lineno* bkln = user;
285 if (bkln->lineno == sci->LineNumber)
287 bkln->addr.Mode = AddrModeFlat;
288 bkln->addr.Offset = sci->Address;
289 return FALSE;
291 return TRUE;
294 static BOOL CALLBACK mcb(PCWSTR module, DWORD64 base, void* user)
296 struct cb_break_lineno* bkln = user;
298 SymEnumLines(dbg_curr_process->handle, base, NULL, bkln->filename, line_cb, bkln);
299 /* continue module enum if no addr found
300 * FIXME: we don't report when several addresses match the same filename/lineno pair
302 return !bkln->addr.Offset;
305 /***********************************************************************
306 * break_add_break_from_lineno
308 * Add a breakpoint from a line number in current file
310 void break_add_break_from_lineno(const char *filename, int lineno, BOOL swbp)
312 struct cb_break_lineno bkln;
313 bkln.addr.Offset = 0;
314 bkln.lineno = lineno;
316 if (!filename)
318 DWORD disp;
319 ADDRESS64 curr;
320 IMAGEHLP_LINE64 il;
321 DWORD_PTR linear;
323 memory_get_current_pc(&curr);
324 linear = (DWORD_PTR)memory_to_linear_addr(&curr);
325 il.SizeOfStruct = sizeof(il);
326 if (!SymGetLineFromAddr64(dbg_curr_process->handle, linear, &disp, &il))
328 dbg_printf("Unable to add breakpoint (unknown address %Ix)\n", linear);
329 return;
331 filename = il.FileName;
332 SymEnumLines(dbg_curr_process->handle, linear, NULL, filename, line_cb, &bkln);
334 else
336 /* we have to enumerate across modules */
337 bkln.filename = filename;
338 SymEnumerateModulesW64(dbg_curr_process->handle, mcb, &bkln);
340 if (bkln.addr.Offset)
341 break_add_break(&bkln.addr, TRUE, swbp);
342 else
344 /* winedbg's lexer can't tell in 'break foo : 3' whether foo shall be interpreted
345 * as a debuggee symbol or as a debuggee source file.
346 * Try now symbol since source file failed.
348 if (filename)
349 break_add_break_from_id(filename, lineno, swbp);
350 else
351 dbg_printf("Unknown line number\n"
352 "(either out of file, or no code at given line number)\n");
356 /***********************************************************************
357 * break_check_delayed_bp
359 * Check is a registered delayed BP is now available.
361 void break_check_delayed_bp(void)
363 struct dbg_lvalue lvalue;
364 int i;
365 struct dbg_delayed_bp* dbp = dbg_curr_process->delayed_bp;
366 char hexbuf[MAX_OFFSET_TO_STR_LEN];
368 for (i = 0; i < dbg_curr_process->num_delayed_bp; i++)
370 if (dbp[i].is_symbol)
372 if (symbol_get_lvalue(dbp[i].u.symbol.name, dbp[i].u.symbol.lineno,
373 &lvalue, TRUE) != sglv_found)
374 continue;
375 if (!lvalue.in_debuggee) continue;
377 else
378 lvalue.addr = dbp[i].u.addr;
379 WINE_TRACE("trying to add delayed %s-bp\n", dbp[i].is_symbol ? "S" : "A");
380 if (!dbp[i].is_symbol)
381 WINE_TRACE("\t%04x:%s\n",
382 dbp[i].u.addr.Segment,
383 memory_offset_to_string(hexbuf, dbp[i].u.addr.Offset, 0));
384 else
385 WINE_TRACE("\t'%s' @ %d\n",
386 dbp[i].u.symbol.name, dbp[i].u.symbol.lineno);
388 if (break_add_break(&lvalue.addr, FALSE, dbp[i].software_bp))
389 memmove(&dbp[i], &dbp[i+1], (--dbg_curr_process->num_delayed_bp - i) * sizeof(*dbp));
393 /***********************************************************************
394 * break_add_watch
396 * Add a watchpoint.
398 void break_add_watch(const struct dbg_lvalue* lvalue, BOOL is_write)
400 int num;
401 DWORD l = ADDRSIZE;
403 if (!lvalue->in_debuggee)
405 dbg_printf("Cannot set a watch point on register or register-based variable\n");
406 return;
408 num = init_xpoint((is_write) ? be_xpoint_watch_write : be_xpoint_watch_read,
409 &lvalue->addr);
410 if (num == -1) return;
412 /* FIXME: this validation part should be moved to CPU backends (likely in a new method) */
413 if (lvalue->type.id != dbg_itype_none)
415 DWORD64 l64;
416 if (types_get_info(&lvalue->type, TI_GET_LENGTH, &l64))
418 /* Only allow size of a power of 2, smaller than CPU's pointer size.
419 * (Validation is done by CPU backend with insert_Xpoint method)
421 if (!(l64 & (l64 - 1)) && l64 <= l)
422 l = l64;
423 else
424 dbg_printf("Unsupported length (%I64x) for watch-points, defaulting to %lu\n", l64, l);
425 /* most CPUs request watch address to be aligned on length */
426 if (lvalue->addr.Offset & (l - 1))
428 dbg_printf("Watchpoint on unaligned address is not supported\n");
429 dbg_curr_process->bp[num].refcount = 0;
430 return;
433 else dbg_printf("Cannot get watch size, defaulting to %lu\n", l);
435 dbg_curr_process->bp[num].w.len = l - 1;
437 if (!get_watched_value(num, &dbg_curr_process->bp[num].w.oldval))
439 dbg_printf("Bad address. Watchpoint not set\n");
440 dbg_curr_process->bp[num].refcount = 0;
441 return;
443 dbg_printf("Watchpoint %d at ", num);
444 print_address(&dbg_curr_process->bp[num].addr, TRUE);
445 dbg_printf("\n");
448 /***********************************************************************
449 * break_delete_xpoint
451 * Delete a breakpoint.
453 void break_delete_xpoint(int num)
455 struct dbg_breakpoint* bp = dbg_curr_process->bp;
457 if ((num <= 0) || (num >= dbg_curr_process->next_bp) ||
458 bp[num].refcount == 0)
460 dbg_printf("Invalid breakpoint number %d\n", num);
461 return;
464 if (--bp[num].refcount > 0)
465 return;
467 if (bp[num].condition != NULL)
469 expr_free(bp[num].condition);
470 bp[num].condition = NULL;
473 bp[num].enabled = FALSE;
474 bp[num].refcount = 0;
475 bp[num].skipcount = 0;
478 /******************************************************************
479 * break_delete_xpoints_from_module
481 * Remove all Xpoints from module which base is 'base'
483 void break_delete_xpoints_from_module(DWORD64 base)
485 IMAGEHLP_MODULE64 im, im_elf;
486 int i;
487 DWORD_PTR linear;
488 struct dbg_breakpoint* bp = dbg_curr_process->bp;
490 /* FIXME: should do it also on the ELF sibling if any */
491 im.SizeOfStruct = sizeof(im);
492 im_elf.SizeOfStruct = sizeof(im_elf);
493 if (!SymGetModuleInfo64(dbg_curr_process->handle, base, &im)) return;
495 /* try to get in fact the underlying ELF module (if any) */
496 if (SymGetModuleInfo64(dbg_curr_process->handle, im.BaseOfImage - 1, &im_elf) &&
497 im_elf.BaseOfImage <= im.BaseOfImage &&
498 im_elf.BaseOfImage + im_elf.ImageSize >= im.BaseOfImage + im.ImageSize)
499 im = im_elf;
501 for (i = 0; i < dbg_curr_process->next_bp; i++)
503 if (bp[i].refcount && bp[i].enabled)
505 linear = (DWORD_PTR)memory_to_linear_addr(&bp[i].addr);
506 if (im.BaseOfImage <= linear && linear < im.BaseOfImage + im.ImageSize)
508 break_delete_xpoint(i);
514 /***********************************************************************
515 * break_enable_xpoint
517 * Enable or disable a break point.
519 void break_enable_xpoint(int num, BOOL enable)
521 if ((num <= 0) || (num >= dbg_curr_process->next_bp) ||
522 dbg_curr_process->bp[num].refcount == 0)
524 dbg_printf("Invalid breakpoint number %d\n", num);
525 return;
527 dbg_curr_process->bp[num].enabled = enable != 0;
528 dbg_curr_process->bp[num].skipcount = 0;
532 /***********************************************************************
533 * find_triggered_watch
535 * Lookup the watchpoints to see if one has been triggered
536 * Return >= (watch point index) if one is found
537 * Return -1 if none found
539 * Unfortunately, Linux used to *NOT* (A REAL PITA) report with ptrace
540 * the DR6 register value, so we have to look with our own need the
541 * cause of the TRAP.
542 * -EP
544 static int find_triggered_watch(void)
546 int found = -1;
547 int i;
548 struct dbg_breakpoint* bp = dbg_curr_process->bp;
550 /* Method 1 => get triggered watchpoint from context (doesn't work on Linux
551 * 2.2.x). This should be fixed in >= 2.2.16
553 for (i = 0; i < dbg_curr_process->next_bp; i++)
555 DWORD64 val = 0;
557 if (bp[i].refcount && bp[i].enabled && !is_xpoint_break(i) &&
558 (dbg_curr_process->be_cpu->is_watchpoint_set(&dbg_context, bp[i].info)))
560 dbg_curr_process->be_cpu->clear_watchpoint(&dbg_context, bp[i].info);
562 if (get_watched_value(i, &val))
564 bp[i].w.oldval = val;
565 return i;
570 /* Method 1 failed, trying method 2 */
572 /* Method 2 => check if value has changed among registered watchpoints
573 * this really sucks, but this is how gdb 4.18 works on my linux box
574 * -EP
576 for (i = 0; i < dbg_curr_process->next_bp; i++)
578 DWORD64 val = 0;
580 if (bp[i].refcount && bp[i].enabled && !is_xpoint_break(i) &&
581 get_watched_value(i, &val))
583 if (val != bp[i].w.oldval)
585 dbg_curr_process->be_cpu->clear_watchpoint(&dbg_context, bp[i].info);
586 bp[i].w.oldval = val;
587 found = i;
588 /* cannot break, because two watch points may have been triggered on
589 * the same access
590 * only one will be reported to the user (FIXME ?)
595 return found;
598 /***********************************************************************
599 * break_info
601 * Display break & watch points information.
603 void break_info(void)
605 int i;
606 int nbp = 0, nwp = 0;
607 struct dbg_delayed_bp* dbp = dbg_curr_process->delayed_bp;
608 struct dbg_breakpoint* bp = dbg_curr_process->bp;
610 for (i = 1; i < dbg_curr_process->next_bp; i++)
612 if (bp[i].refcount)
614 if (is_xpoint_break(i)) nbp++; else nwp++;
618 if (nbp)
620 dbg_printf("Breakpoints:\n");
621 for (i = 1; i < dbg_curr_process->next_bp; i++)
623 if (!bp[i].refcount || !is_xpoint_break(i))
624 continue;
625 dbg_printf("%d: %c ", i, bp[i].enabled ? 'y' : 'n');
626 print_address(&bp[i].addr, TRUE);
627 dbg_printf(" (%u)%s\n", bp[i].refcount,
628 bp[i].xpoint_type == be_xpoint_watch_exec ? " (hardware assisted)" : "");
629 if (bp[i].condition != NULL)
631 dbg_printf("\t\tstop when ");
632 expr_print(bp[i].condition);
633 dbg_printf("\n");
637 else dbg_printf("No breakpoints\n");
638 if (nwp)
640 dbg_printf("Watchpoints:\n");
641 for (i = 1; i < dbg_curr_process->next_bp; i++)
643 if (!bp[i].refcount || is_xpoint_break(i))
644 continue;
645 dbg_printf("%d: %c ", i, bp[i].enabled ? 'y' : 'n');
646 print_address(&bp[i].addr, TRUE);
647 dbg_printf(" on %d byte%s (%c)\n",
648 bp[i].w.len + 1, bp[i].w.len > 0 ? "s" : "",
649 bp[i].xpoint_type == be_xpoint_watch_write ? 'W' : 'R');
650 if (bp[i].condition != NULL)
652 dbg_printf("\t\tstop when ");
653 expr_print(bp[i].condition);
654 dbg_printf("\n");
658 else dbg_printf("No watchpoints\n");
659 if (dbg_curr_process->num_delayed_bp)
661 dbg_printf("Delayed breakpoints:\n");
662 for (i = 0; i < dbg_curr_process->num_delayed_bp; i++)
664 if (dbp[i].is_symbol)
666 dbg_printf("%d: %s", i, dbp[i].u.symbol.name);
667 if (dbp[i].u.symbol.lineno != -1)
668 dbg_printf(" at line %u", dbp[i].u.symbol.lineno);
670 else
672 dbg_printf("%d: ", i);
673 print_address(&dbp[i].u.addr, FALSE);
675 dbg_printf("\n");
680 /***********************************************************************
681 * should_stop
683 * Check whether or not the condition (bp / skipcount) of a break/watch
684 * point are met.
686 static BOOL should_stop(int bpnum)
688 struct dbg_breakpoint* bp = &dbg_curr_process->bp[bpnum];
690 if (bp->condition != NULL)
692 struct dbg_lvalue lvalue = expr_eval(bp->condition);
694 if (lvalue.type.id == dbg_itype_none)
697 * Something wrong - unable to evaluate this expression.
699 dbg_printf("Unable to evaluate expression ");
700 expr_print(bp->condition);
701 dbg_printf("\nTurning off condition\n");
702 break_add_condition(bpnum, NULL);
704 else if (!types_extract_as_integer(&lvalue))
706 return FALSE;
710 if (bp->skipcount > 0) bp->skipcount--;
711 return bp->skipcount == 0;
714 /***********************************************************************
715 * break_should_continue
717 * Determine if we should continue execution after a SIGTRAP signal when
718 * executing in the given mode.
720 BOOL break_should_continue(ADDRESS64* addr, DWORD code)
722 enum dbg_exec_mode mode = dbg_curr_thread->exec_mode;
725 if (dbg_curr_thread->stopped_xpoint > 0)
727 if (!should_stop(dbg_curr_thread->stopped_xpoint)) return TRUE;
729 switch (dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].xpoint_type)
731 case be_xpoint_break:
732 case be_xpoint_watch_exec:
733 dbg_printf("Stopped on breakpoint %d at ", dbg_curr_thread->stopped_xpoint);
734 print_address(&dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].addr, TRUE);
735 dbg_printf("\n");
736 break;
737 case be_xpoint_watch_read:
738 case be_xpoint_watch_write:
739 dbg_printf("Stopped on watchpoint %d at ", dbg_curr_thread->stopped_xpoint);
740 print_address(addr, TRUE);
741 dbg_printf(" new value %I64x\n",
742 dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].w.oldval);
744 return FALSE;
748 * If our mode indicates that we are stepping line numbers,
749 * get the current function, and figure out if we are exactly
750 * on a line number or not.
752 if (mode == dbg_exec_step_over_line || mode == dbg_exec_step_into_line)
754 if (symbol_get_function_line_status(addr) == dbg_on_a_line_number)
755 dbg_curr_thread->exec_count--;
757 else if (mode == dbg_exec_step_over_insn || mode == dbg_exec_step_into_insn)
758 dbg_curr_thread->exec_count--;
760 if (dbg_curr_thread->exec_count > 0 || mode == dbg_exec_finish)
763 * We still need to execute more instructions.
765 return TRUE;
768 /* no breakpoint, continue if in continuous mode */
769 return mode == dbg_exec_cont || mode == dbg_exec_finish;
772 /***********************************************************************
773 * break_adjust_pc
775 * Adjust PC to the address where the trap (if any) actually occurred
776 * Also sets dbg_curr_thread->stopped_xpoint
778 void break_adjust_pc(ADDRESS64* addr, DWORD code, BOOL first_chance, BOOL* is_break)
780 /* break / watch points are handled on first chance */
781 if ( !first_chance )
783 *is_break = TRUE;
784 dbg_curr_thread->stopped_xpoint = -1;
785 return;
787 *is_break = FALSE;
789 /* If not single-stepping, back up to the break instruction */
790 if (code == EXCEPTION_BREAKPOINT)
791 addr->Offset += dbg_curr_process->be_cpu->adjust_pc_for_break(&dbg_context, TRUE);
793 dbg_curr_thread->stopped_xpoint = find_xpoint(addr, be_xpoint_break);
794 dbg_curr_process->bp[0].enabled = FALSE; /* disable the step-over breakpoint */
796 if (dbg_curr_thread->stopped_xpoint > 0) return;
798 if (dbg_curr_thread->stopped_xpoint < 0)
800 dbg_curr_thread->stopped_xpoint = find_xpoint(addr, be_xpoint_watch_exec);
801 if (dbg_curr_thread->stopped_xpoint < 0)
802 dbg_curr_thread->stopped_xpoint = find_triggered_watch();
803 if (dbg_curr_thread->stopped_xpoint > 0)
805 /* If not single-stepping, do not back up over the break instruction */
806 if (code == EXCEPTION_BREAKPOINT)
807 addr->Offset += dbg_curr_process->be_cpu->adjust_pc_for_break(&dbg_context, FALSE);
808 return;
812 /* If there's no breakpoint and we are not single-stepping, then
813 * either we must have encountered a break insn in the Windows program
814 * or someone is trying to stop us
816 if (dbg_curr_thread->stopped_xpoint == -1 && code == EXCEPTION_BREAKPOINT)
818 *is_break = TRUE;
819 addr->Offset += dbg_curr_process->be_cpu->adjust_pc_for_break(&dbg_context, FALSE);
823 /***********************************************************************
824 * break_suspend_execution
826 * Remove all bp before entering the debug loop
828 void break_suspend_execution(void)
830 break_set_xpoints(FALSE);
831 dbg_curr_process->bp[0] = dbg_curr_thread->step_over_bp;
834 /***********************************************************************
835 * break_restart_execution
837 * Set the bp to the correct state to restart execution
838 * in the given mode.
840 void break_restart_execution(int count)
842 ADDRESS64 addr;
843 enum dbg_line_status status;
844 enum dbg_exec_mode mode, ret_mode;
845 ADDRESS64 callee;
846 void* linear;
848 memory_get_current_pc(&addr);
849 linear = memory_to_linear_addr(&addr);
852 * This is the mode we will be running in after we finish. We would like
853 * to be able to modify this in certain cases.
855 ret_mode = mode = dbg_curr_thread->exec_mode;
857 /* we've stopped on a xpoint (other than step over) */
858 if (dbg_curr_thread->stopped_xpoint > 0)
861 * If we have set a new value, then save it in the BP number.
863 if (count != 0 && mode == dbg_exec_cont)
865 dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].skipcount = count;
867 /* If we've stopped on a breakpoint, single step over it (, then run) */
868 if (is_xpoint_break(dbg_curr_thread->stopped_xpoint))
869 mode = dbg_exec_step_into_insn;
871 else if (mode == dbg_exec_cont && count > 1)
873 dbg_printf("Not stopped at any breakpoint; argument ignored.\n");
876 if (mode == dbg_exec_finish && dbg_curr_process->be_cpu->is_function_return(linear))
878 mode = ret_mode = dbg_exec_step_into_insn;
882 * See if the function we are stepping into has debug info
883 * and line numbers. If not, then we step over it instead.
884 * FIXME - we need to check for things like thunks or trampolines,
885 * as the actual function may in fact have debug info.
887 if (dbg_curr_process->be_cpu->is_function_call(linear, &callee))
889 status = symbol_get_function_line_status(&callee);
890 #if 0
891 /* FIXME: we need to get the thunk type */
893 * Anytime we have a trampoline, step over it.
895 if ((mode == EXEC_STEP_OVER || mode == EXEC_STEPI_OVER)
896 && status == dbg_in_a_thunk)
898 WINE_WARN("Not stepping into trampoline at %p (no lines)\n",
899 memory_to_linear_addr(&callee));
900 mode = EXEC_STEP_OVER_TRAMPOLINE;
902 #endif
903 if (mode == dbg_exec_step_into_line && status == dbg_no_line_info)
905 WINE_WARN("Not stepping into function at %p (no lines)\n",
906 memory_to_linear_addr(&callee));
907 mode = dbg_exec_step_over_line;
911 if (mode == dbg_exec_step_into_line &&
912 symbol_get_function_line_status(&addr) == dbg_no_line_info)
914 dbg_printf("Single stepping until exit from function,\n"
915 "which has no line number information.\n");
916 ret_mode = mode = dbg_exec_finish;
919 switch (mode)
921 case dbg_exec_cont: /* Continuous execution */
922 dbg_curr_process->be_cpu->single_step(&dbg_context, FALSE);
923 break_set_xpoints(TRUE);
924 break;
926 #if 0
927 case EXEC_STEP_OVER_TRAMPOLINE:
929 * This is the means by which we step over our conversion stubs
930 * in callfrom*.s and callto*.s. We dig the appropriate address
931 * off the stack, and we set the breakpoint there instead of the
932 * address just after the call.
934 be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
935 be_cpu_addr_stack, &addr);
936 /* FIXME: we assume stack grows as on an i386 */
937 addr.Offset += 2 * sizeof(unsigned int);
938 dbg_read_memory(memory_to_linear_addr(&addr),
939 &addr.Offset, sizeof(addr.Offset));
940 dbg_curr_process->bp[0].addr = addr;
941 dbg_curr_process->bp[0].enabled = TRUE;
942 dbg_curr_process->bp[0].refcount = 1;
943 dbg_curr_process->bp[0].skipcount = 0;
944 dbg_curr_process->bp[0].xpoint_type = be_xpoint_break;
945 dbg_curr_process->bp[0].condition = NULL;
946 be_cpu->single_step(&dbg_context, FALSE);
947 break_set_xpoints(TRUE);
948 break;
949 #endif
951 case dbg_exec_finish:
952 case dbg_exec_step_over_insn: /* Stepping over a call */
953 case dbg_exec_step_over_line: /* Stepping over a call */
954 if (dbg_curr_process->be_cpu->is_step_over_insn(linear))
956 dbg_curr_process->be_cpu->disasm_one_insn(&addr, FALSE);
957 dbg_curr_process->bp[0].addr = addr;
958 dbg_curr_process->bp[0].enabled = TRUE;
959 dbg_curr_process->bp[0].refcount = 1;
960 dbg_curr_process->bp[0].skipcount = 0;
961 dbg_curr_process->bp[0].xpoint_type = be_xpoint_break;
962 dbg_curr_process->bp[0].condition = NULL;
963 dbg_curr_process->be_cpu->single_step(&dbg_context, FALSE);
964 break_set_xpoints(TRUE);
965 break;
967 /* else fall through to single-stepping */
969 case dbg_exec_step_into_line: /* Single-stepping a line */
970 case dbg_exec_step_into_insn: /* Single-stepping an instruction */
971 dbg_curr_process->be_cpu->single_step(&dbg_context, TRUE);
972 break;
973 default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
975 dbg_curr_thread->step_over_bp = dbg_curr_process->bp[0];
976 dbg_curr_thread->exec_mode = ret_mode;
979 BOOL break_add_condition(int num, struct expr* exp)
981 if (num <= 0 || num >= dbg_curr_process->next_bp ||
982 !dbg_curr_process->bp[num].refcount)
984 dbg_printf("Invalid breakpoint number %d\n", num);
985 return FALSE;
988 if (dbg_curr_process->bp[num].condition != NULL)
990 expr_free(dbg_curr_process->bp[num].condition);
991 dbg_curr_process->bp[num].condition = NULL;
994 if (exp != NULL) dbg_curr_process->bp[num].condition = expr_clone(exp, NULL);
996 return TRUE;