wined3d: Pass a wined3d_state structure to device_preload_texture().
[wine/multimedia.git] / programs / winedbg / break.c
blob2f502cef349f9239ecf8c4cebfd1133bfa704778
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 "config.h"
24 #include "debugger.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
29 static int is_xpoint_break(int bpnum)
31 int type = dbg_curr_process->bp[bpnum].xpoint_type;
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 */
39 /***********************************************************************
40 * break_set_xpoints
42 * Set or remove all the breakpoints & watchpoints
44 void break_set_xpoints(BOOL set)
46 static BOOL last; /* = 0 = FALSE */
48 unsigned int i, ret, size;
49 void* addr;
50 struct dbg_breakpoint* bp = dbg_curr_process->bp;
52 if (set == last) return;
53 last = set;
55 for (i = 0; i < dbg_curr_process->next_bp; i++)
57 if (!bp[i].refcount || !bp[i].enabled) continue;
59 if (is_xpoint_break(i))
60 size = 0;
61 else
62 size = bp[i].w.len + 1;
63 addr = memory_to_linear_addr(&bp[i].addr);
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)
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;
85 /***********************************************************************
86 * find_xpoint
88 * Find the breakpoint for a given address. Return the breakpoint
89 * number or -1 if none.
91 static int find_xpoint(const ADDRESS64* addr, enum be_xpoint_type type)
93 int i;
94 void* lin = memory_to_linear_addr(addr);
95 struct dbg_breakpoint* bp = dbg_curr_process->bp;
97 for (i = 0; i < dbg_curr_process->next_bp; i++)
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;
103 return -1;
106 /***********************************************************************
107 * init_xpoint
109 * Find an empty slot in BP table to add a new break/watch point
111 static int init_xpoint(int type, const ADDRESS64* addr)
113 int num;
114 struct dbg_breakpoint* bp = dbg_curr_process->bp;
116 for (num = (dbg_curr_process->next_bp < MAX_BREAKPOINTS) ?
117 dbg_curr_process->next_bp++ : 1;
118 num < MAX_BREAKPOINTS; num++)
120 if (bp[num].refcount == 0)
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;
131 dbg_printf("Too many bp. Please delete some.\n");
132 return -1;
135 /***********************************************************************
136 * get_watched_value
138 * Returns the value watched by watch point 'num'.
140 static BOOL get_watched_value(int num, DWORD64* val)
142 DWORD64 buf[1];
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;
148 switch (dbg_curr_process->bp[num].w.len + 1)
150 case 8: *val = *(DWORD64*)buf; break;
151 case 4: *val = *(DWORD*)buf; break;
152 case 2: *val = *(WORD*)buf; break;
153 case 1: *val = *(BYTE*)buf; break;
154 default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
156 return TRUE;
159 /***********************************************************************
160 * break_add_break
162 * Add a breakpoint.
164 BOOL break_add_break(const ADDRESS64* addr, BOOL verbose, BOOL swbp)
166 int num;
167 BYTE ch;
168 struct dbg_breakpoint* bp = dbg_curr_process->bp;
169 int type = swbp ? be_xpoint_break : be_xpoint_watch_exec;
171 if ((num = find_xpoint(addr, type)) >= 1)
173 bp[num].refcount++;
174 dbg_printf("Breakpoint %d at ", num);
175 print_address(&bp[num].addr, TRUE);
176 dbg_printf(" (refcount=%d)\n", bp[num].refcount);
177 return TRUE;
180 if (!dbg_read_memory(memory_to_linear_addr(addr), &ch, sizeof(ch)))
182 if (verbose)
184 dbg_printf("Invalid address ");
185 print_bare_address(addr);
186 dbg_printf(", can't set breakpoint\n");
188 return FALSE;
191 if ((num = init_xpoint(type, addr)) == -1)
192 return FALSE;
194 dbg_printf("Breakpoint %d at ", num);
195 print_address(&bp[num].addr, TRUE);
196 dbg_printf("\n");
198 return TRUE;
201 /***********************************************************************
202 * break_add_break_from_lvalue
204 * Add a breakpoint.
206 BOOL break_add_break_from_lvalue(const struct dbg_lvalue* lvalue, BOOL swbp)
208 ADDRESS64 addr;
210 types_extract_as_address(lvalue, &addr);
212 if (!break_add_break(&addr, TRUE, swbp))
214 if (!DBG_IVAR(CanDeferOnBPByAddr))
216 dbg_printf("Invalid address, can't set breakpoint\n"
217 "You can turn on deferring bp by address by setting $CanDeferOnBPByAddr to 1\n");
218 return FALSE;
220 dbg_printf("Unable to add breakpoint, will check again any time a new DLL is loaded\n");
221 dbg_curr_process->delayed_bp =
222 dbg_heap_realloc(dbg_curr_process->delayed_bp,
223 sizeof(struct dbg_delayed_bp) * ++dbg_curr_process->num_delayed_bp);
225 dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].is_symbol = FALSE;
226 dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].software_bp = swbp;
227 dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].u.addr = addr;
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_lvalue lvalue;
241 int i;
243 switch (symbol_get_lvalue(name, lineno, &lvalue, TRUE))
245 case sglv_found:
246 break_add_break(&lvalue.addr, TRUE, swbp);
247 return;
248 case sglv_unknown:
249 break;
250 case sglv_aborted: /* user aborted symbol lookup */
251 return;
254 dbg_printf("Unable to add breakpoint, will check again when a new DLL is loaded\n");
255 for (i = 0; i < dbg_curr_process->num_delayed_bp; i++)
257 if (dbg_curr_process->delayed_bp[i].is_symbol &&
258 !strcmp(name, dbg_curr_process->delayed_bp[i].u.symbol.name) &&
259 lineno == dbg_curr_process->delayed_bp[i].u.symbol.lineno)
260 return;
262 dbg_curr_process->delayed_bp = dbg_heap_realloc(dbg_curr_process->delayed_bp,
263 sizeof(struct dbg_delayed_bp) * ++dbg_curr_process->num_delayed_bp);
265 dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].is_symbol = TRUE;
266 dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].software_bp = swbp;
267 dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].u.symbol.name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(name) + 1), name);
268 dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].u.symbol.lineno = lineno;
271 struct cb_break_lineno
273 int lineno;
274 ADDRESS64 addr;
277 static BOOL CALLBACK line_cb(SRCCODEINFO* sci, void* user)
279 struct cb_break_lineno* bkln = user;
281 if (bkln->lineno == sci->LineNumber)
283 bkln->addr.Mode = AddrModeFlat;
284 bkln->addr.Offset = sci->Address;
285 return FALSE;
287 return TRUE;
290 /***********************************************************************
291 * break_add_break_from_lineno
293 * Add a breakpoint from a line number in current file
295 void break_add_break_from_lineno(int lineno, BOOL swbp)
297 struct cb_break_lineno bkln;
299 memory_get_current_pc(&bkln.addr);
301 if (lineno != -1)
303 IMAGEHLP_LINE64 il;
306 DWORD disp;
307 DWORD_PTR linear = (DWORD_PTR)memory_to_linear_addr(&bkln.addr);
309 il.SizeOfStruct = sizeof(il);
310 if (!SymGetLineFromAddr64(dbg_curr_process->handle, linear, &disp, &il))
312 dbg_printf("Unable to add breakpoint (unknown address %lx)\n", linear);
313 return;
315 bkln.addr.Offset = 0;
316 bkln.lineno = lineno;
317 SymEnumLines(dbg_curr_process->handle, linear, NULL, il.FileName, line_cb, &bkln);
318 if (!bkln.addr.Offset)
320 dbg_printf("Unknown line number\n"
321 "(either out of file, or no code at given line number)\n");
322 return;
326 break_add_break(&bkln.addr, TRUE, swbp);
329 /***********************************************************************
330 * break_check_delayed_bp
332 * Check is a registered delayed BP is now available.
334 void break_check_delayed_bp(void)
336 struct dbg_lvalue lvalue;
337 int i;
338 struct dbg_delayed_bp* dbp = dbg_curr_process->delayed_bp;
339 char hexbuf[MAX_OFFSET_TO_STR_LEN];
341 for (i = 0; i < dbg_curr_process->num_delayed_bp; i++)
343 if (dbp[i].is_symbol)
345 if (symbol_get_lvalue(dbp[i].u.symbol.name, dbp[i].u.symbol.lineno,
346 &lvalue, TRUE) != sglv_found)
347 continue;
348 if (lvalue.cookie != DLV_TARGET) continue;
350 else
351 lvalue.addr = dbp[i].u.addr;
352 WINE_TRACE("trying to add delayed %s-bp\n", dbp[i].is_symbol ? "S" : "A");
353 if (!dbp[i].is_symbol)
354 WINE_TRACE("\t%04x:%s\n",
355 dbp[i].u.addr.Segment,
356 memory_offset_to_string(hexbuf, dbp[i].u.addr.Offset, 0));
357 else
358 WINE_TRACE("\t'%s' @ %d\n",
359 dbp[i].u.symbol.name, dbp[i].u.symbol.lineno);
361 if (break_add_break(&lvalue.addr, FALSE, dbp[i].software_bp))
362 memmove(&dbp[i], &dbp[i+1], (--dbg_curr_process->num_delayed_bp - i) * sizeof(*dbp));
366 /***********************************************************************
367 * break_add_watch
369 * Add a watchpoint.
371 static void break_add_watch(const struct dbg_lvalue* lvalue, BOOL is_write)
373 int num;
374 DWORD64 l = 4;
376 if (lvalue->cookie == DLV_HOST)
378 dbg_printf("Cannot set a watch point on register or register-based variable\n");
379 return;
381 num = init_xpoint((is_write) ? be_xpoint_watch_write : be_xpoint_watch_read,
382 &lvalue->addr);
383 if (num == -1) return;
385 if (lvalue->type.id != dbg_itype_none)
387 if (types_get_info(&lvalue->type, TI_GET_LENGTH, &l))
389 switch (l)
391 case 4: case 2: case 1: break;
392 default:
393 dbg_printf("Unsupported length (%s) for watch-points, defaulting to 4\n",
394 wine_dbgstr_longlong(l));
395 break;
398 else dbg_printf("Cannot get watch size, defaulting to 4\n");
400 dbg_curr_process->bp[num].w.len = (DWORD)l - 1;
402 if (!get_watched_value(num, &dbg_curr_process->bp[num].w.oldval))
404 dbg_printf("Bad address. Watchpoint not set\n");
405 dbg_curr_process->bp[num].refcount = 0;
406 return;
408 dbg_printf("Watchpoint %d at ", num);
409 print_address(&dbg_curr_process->bp[num].addr, TRUE);
410 dbg_printf("\n");
413 /******************************************************************
414 * break_add_watch_from_lvalue
416 * Adds a watch point from an address (stored in a lvalue)
418 void break_add_watch_from_lvalue(const struct dbg_lvalue* lvalue)
420 struct dbg_lvalue lval;
422 types_extract_as_address(lvalue, &lval.addr);
423 lval.type.id = dbg_itype_none;
425 break_add_watch(&lval, TRUE);
428 /***********************************************************************
429 * break_add_watch_from_id
431 * Add a watchpoint from a symbol name
433 void break_add_watch_from_id(const char *name)
435 struct dbg_lvalue lvalue;
437 switch (symbol_get_lvalue(name, -1, &lvalue, TRUE))
439 case sglv_found:
440 break_add_watch(&lvalue, 1);
441 break;
442 case sglv_unknown:
443 dbg_printf("Unable to add watchpoint\n");
444 break;
445 case sglv_aborted: /* user aborted symbol lookup */
446 break;
450 /***********************************************************************
451 * break_delete_xpoint
453 * Delete a breakpoint.
455 void break_delete_xpoint(int num)
457 struct dbg_breakpoint* bp = dbg_curr_process->bp;
459 if ((num <= 0) || (num >= dbg_curr_process->next_bp) ||
460 bp[num].refcount == 0)
462 dbg_printf("Invalid breakpoint number %d\n", num);
463 return;
466 if (--bp[num].refcount > 0)
467 return;
469 if (bp[num].condition != NULL)
471 expr_free(bp[num].condition);
472 bp[num].condition = NULL;
475 bp[num].enabled = FALSE;
476 bp[num].refcount = 0;
477 bp[num].skipcount = 0;
480 static inline BOOL module_is_container(const IMAGEHLP_MODULE* wmod_cntnr,
481 const IMAGEHLP_MODULE* wmod_child)
483 return wmod_cntnr->BaseOfImage <= wmod_child->BaseOfImage &&
484 wmod_cntnr->BaseOfImage + wmod_cntnr->ImageSize >=
485 wmod_child->BaseOfImage + wmod_child->ImageSize;
488 /******************************************************************
489 * break_delete_xpoints_from_module
491 * Remove all Xpoints from module which base is 'base'
493 void break_delete_xpoints_from_module(DWORD64 base)
495 IMAGEHLP_MODULE64 im, im_elf;
496 int i;
497 DWORD_PTR linear;
498 struct dbg_breakpoint* bp = dbg_curr_process->bp;
500 /* FIXME: should do it also on the ELF sibbling if any */
501 im.SizeOfStruct = sizeof(im);
502 im_elf.SizeOfStruct = sizeof(im_elf);
503 if (!SymGetModuleInfo64(dbg_curr_process->handle, base, &im)) return;
505 /* try to get in fact the underlying ELF module (if any) */
506 if (SymGetModuleInfo64(dbg_curr_process->handle, im.BaseOfImage - 1, &im_elf) &&
507 im_elf.BaseOfImage <= im.BaseOfImage &&
508 im_elf.BaseOfImage + im_elf.ImageSize >= im.BaseOfImage + im.ImageSize)
509 im = im_elf;
511 for (i = 0; i < dbg_curr_process->next_bp; i++)
513 if (bp[i].refcount && bp[i].enabled)
515 linear = (DWORD_PTR)memory_to_linear_addr(&bp[i].addr);
516 if (im.BaseOfImage <= linear && linear < im.BaseOfImage + im.ImageSize)
518 break_delete_xpoint(i);
524 /***********************************************************************
525 * break_enable_xpoint
527 * Enable or disable a break point.
529 void break_enable_xpoint(int num, BOOL enable)
531 if ((num <= 0) || (num >= dbg_curr_process->next_bp) ||
532 dbg_curr_process->bp[num].refcount == 0)
534 dbg_printf("Invalid breakpoint number %d\n", num);
535 return;
537 dbg_curr_process->bp[num].enabled = (enable) ? TRUE : FALSE;
538 dbg_curr_process->bp[num].skipcount = 0;
542 /***********************************************************************
543 * find_triggered_watch
545 * Lookup the watchpoints to see if one has been triggered
546 * Return >= (watch point index) if one is found
547 * Return -1 if none found
549 * Unfortunately, Linux used to *NOT* (A REAL PITA) report with ptrace
550 * the DR6 register value, so we have to look with our own need the
551 * cause of the TRAP.
552 * -EP
554 static int find_triggered_watch(void)
556 int found = -1;
557 int i;
558 struct dbg_breakpoint* bp = dbg_curr_process->bp;
560 /* Method 1 => get triggered watchpoint from context (doesn't work on Linux
561 * 2.2.x). This should be fixed in >= 2.2.16
563 for (i = 0; i < dbg_curr_process->next_bp; i++)
565 DWORD64 val = 0;
567 if (bp[i].refcount && bp[i].enabled && !is_xpoint_break(i) &&
568 (be_cpu->is_watchpoint_set(&dbg_context, bp[i].info)))
570 be_cpu->clear_watchpoint(&dbg_context, bp[i].info);
572 if (get_watched_value(i, &val))
574 bp[i].w.oldval = val;
575 return i;
580 /* Method 1 failed, trying method 2 */
582 /* Method 2 => check if value has changed among registered watchpoints
583 * this really sucks, but this is how gdb 4.18 works on my linux box
584 * -EP
586 for (i = 0; i < dbg_curr_process->next_bp; i++)
588 DWORD64 val = 0;
590 if (bp[i].refcount && bp[i].enabled && !is_xpoint_break(i) &&
591 get_watched_value(i, &val))
593 if (val != bp[i].w.oldval)
595 be_cpu->clear_watchpoint(&dbg_context, bp[i].info);
596 bp[i].w.oldval = val;
597 found = i;
598 /* cannot break, because two watch points may have been triggered on
599 * the same access
600 * only one will be reported to the user (FIXME ?)
605 return found;
608 /***********************************************************************
609 * break_info
611 * Display break & watch points information.
613 void break_info(void)
615 int i;
616 int nbp = 0, nwp = 0;
617 struct dbg_delayed_bp* dbp = dbg_curr_process->delayed_bp;
618 struct dbg_breakpoint* bp = dbg_curr_process->bp;
620 for (i = 1; i < dbg_curr_process->next_bp; i++)
622 if (bp[i].refcount)
624 if (is_xpoint_break(i)) nbp++; else nwp++;
628 if (nbp)
630 dbg_printf("Breakpoints:\n");
631 for (i = 1; i < dbg_curr_process->next_bp; i++)
633 if (!bp[i].refcount || !is_xpoint_break(i))
634 continue;
635 dbg_printf("%d: %c ", i, bp[i].enabled ? 'y' : 'n');
636 print_address(&bp[i].addr, TRUE);
637 dbg_printf(" (%u)%s\n", bp[i].refcount,
638 bp[i].xpoint_type == be_xpoint_watch_exec ? " (hardware assisted)" : "");
639 if (bp[i].condition != NULL)
641 dbg_printf("\t\tstop when ");
642 expr_print(bp[i].condition);
643 dbg_printf("\n");
647 else dbg_printf("No breakpoints\n");
648 if (nwp)
650 dbg_printf("Watchpoints:\n");
651 for (i = 1; i < dbg_curr_process->next_bp; i++)
653 if (!bp[i].refcount || is_xpoint_break(i))
654 continue;
655 dbg_printf("%d: %c ", i, bp[i].enabled ? 'y' : 'n');
656 print_address(&bp[i].addr, TRUE);
657 dbg_printf(" on %d byte%s (%c)\n",
658 bp[i].w.len + 1, bp[i].w.len > 0 ? "s" : "",
659 bp[i].xpoint_type == be_xpoint_watch_write ? 'W' : 'R');
660 if (bp[i].condition != NULL)
662 dbg_printf("\t\tstop when ");
663 expr_print(bp[i].condition);
664 dbg_printf("\n");
668 else dbg_printf("No watchpoints\n");
669 if (dbg_curr_process->num_delayed_bp)
671 dbg_printf("Delayed breakpoints:\n");
672 for (i = 0; i < dbg_curr_process->num_delayed_bp; i++)
674 if (dbp[i].is_symbol)
676 dbg_printf("%d: %s", i, dbp[i].u.symbol.name);
677 if (dbp[i].u.symbol.lineno != -1)
678 dbg_printf(" at line %u", dbp[i].u.symbol.lineno);
680 else
682 dbg_printf("%d: ", i);
683 print_address(&dbp[i].u.addr, FALSE);
685 dbg_printf("\n");
690 /***********************************************************************
691 * should_stop
693 * Check whether or not the condition (bp / skipcount) of a break/watch
694 * point are met.
696 static BOOL should_stop(int bpnum)
698 struct dbg_breakpoint* bp = &dbg_curr_process->bp[bpnum];
700 if (bp->condition != NULL)
702 struct dbg_lvalue lvalue = expr_eval(bp->condition);
704 if (lvalue.type.id == dbg_itype_none)
707 * Something wrong - unable to evaluate this expression.
709 dbg_printf("Unable to evaluate expression ");
710 expr_print(bp->condition);
711 dbg_printf("\nTurning off condition\n");
712 break_add_condition(bpnum, NULL);
714 else if (!types_extract_as_integer(&lvalue))
716 return FALSE;
720 if (bp->skipcount > 0) bp->skipcount--;
721 return bp->skipcount == 0;
724 /***********************************************************************
725 * break_should_continue
727 * Determine if we should continue execution after a SIGTRAP signal when
728 * executing in the given mode.
730 BOOL break_should_continue(ADDRESS64* addr, DWORD code)
732 enum dbg_exec_mode mode = dbg_curr_thread->exec_mode;
735 if (dbg_curr_thread->stopped_xpoint > 0)
737 if (!should_stop(dbg_curr_thread->stopped_xpoint)) return TRUE;
739 switch (dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].xpoint_type)
741 case be_xpoint_break:
742 case be_xpoint_watch_exec:
743 dbg_printf("Stopped on breakpoint %d at ", dbg_curr_thread->stopped_xpoint);
744 print_address(&dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].addr, TRUE);
745 dbg_printf("\n");
746 break;
747 case be_xpoint_watch_read:
748 case be_xpoint_watch_write:
749 dbg_printf("Stopped on watchpoint %d at ", dbg_curr_thread->stopped_xpoint);
750 print_address(addr, TRUE);
751 dbg_printf(" new value %s\n",
752 wine_dbgstr_longlong(dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].w.oldval));
754 return FALSE;
758 * If our mode indicates that we are stepping line numbers,
759 * get the current function, and figure out if we are exactly
760 * on a line number or not.
762 if (mode == dbg_exec_step_over_line || mode == dbg_exec_step_into_line)
764 if (symbol_get_function_line_status(addr) == dbg_on_a_line_number)
765 dbg_curr_thread->exec_count--;
767 else if (mode == dbg_exec_step_over_insn || mode == dbg_exec_step_into_insn)
768 dbg_curr_thread->exec_count--;
770 if (dbg_curr_thread->exec_count > 0 || mode == dbg_exec_finish)
773 * We still need to execute more instructions.
775 return TRUE;
778 /* no breakpoint, continue if in continuous mode */
779 return mode == dbg_exec_cont || mode == dbg_exec_finish;
782 /***********************************************************************
783 * break_adjust_pc
785 * Adjust PC to the address where the trap (if any) actually occurred
786 * Also sets dbg_curr_thread->stopped_xpoint
788 void break_adjust_pc(ADDRESS64* addr, DWORD code, BOOL first_chance, BOOL* is_break)
790 /* break / watch points are handled on first chance */
791 if ( !first_chance )
793 *is_break = TRUE;
794 dbg_curr_thread->stopped_xpoint = -1;
795 return;
797 *is_break = FALSE;
799 /* If not single-stepping, back up to the break instruction */
800 if (code == EXCEPTION_BREAKPOINT)
801 addr->Offset += be_cpu->adjust_pc_for_break(&dbg_context, TRUE);
803 dbg_curr_thread->stopped_xpoint = find_xpoint(addr, be_xpoint_break);
804 dbg_curr_process->bp[0].enabled = FALSE; /* disable the step-over breakpoint */
806 if (dbg_curr_thread->stopped_xpoint > 0) return;
808 if (dbg_curr_thread->stopped_xpoint < 0)
810 dbg_curr_thread->stopped_xpoint = find_xpoint(addr, be_xpoint_watch_exec);
811 if (dbg_curr_thread->stopped_xpoint < 0)
812 dbg_curr_thread->stopped_xpoint = find_triggered_watch();
813 if (dbg_curr_thread->stopped_xpoint > 0)
815 /* If not single-stepping, do not back up over the break instruction */
816 if (code == EXCEPTION_BREAKPOINT)
817 addr->Offset += be_cpu->adjust_pc_for_break(&dbg_context, FALSE);
818 return;
822 /* If there's no breakpoint and we are not single-stepping, then
823 * either we must have encountered a break insn in the Windows program
824 * or someone is trying to stop us
826 if (dbg_curr_thread->stopped_xpoint == -1 && code == EXCEPTION_BREAKPOINT)
828 *is_break = TRUE;
829 addr->Offset += be_cpu->adjust_pc_for_break(&dbg_context, FALSE);
833 /***********************************************************************
834 * break_suspend_execution
836 * Remove all bp before entering the debug loop
838 void break_suspend_execution(void)
840 break_set_xpoints(FALSE);
841 dbg_curr_process->bp[0] = dbg_curr_thread->step_over_bp;
844 /***********************************************************************
845 * break_restart_execution
847 * Set the bp to the correct state to restart execution
848 * in the given mode.
850 void break_restart_execution(int count)
852 ADDRESS64 addr;
853 enum dbg_line_status status;
854 enum dbg_exec_mode mode, ret_mode;
855 ADDRESS64 callee;
856 void* linear;
858 memory_get_current_pc(&addr);
859 linear = memory_to_linear_addr(&addr);
862 * This is the mode we will be running in after we finish. We would like
863 * to be able to modify this in certain cases.
865 ret_mode = mode = dbg_curr_thread->exec_mode;
867 /* we've stopped on a xpoint (other than step over) */
868 if (dbg_curr_thread->stopped_xpoint > 0)
871 * If we have set a new value, then save it in the BP number.
873 if (count != 0 && mode == dbg_exec_cont)
875 dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].skipcount = count;
877 /* If we've stopped on a breakpoint, single step over it (, then run) */
878 if (is_xpoint_break(dbg_curr_thread->stopped_xpoint))
879 mode = dbg_exec_step_into_insn;
881 else if (mode == dbg_exec_cont && count > 1)
883 dbg_printf("Not stopped at any breakpoint; argument ignored.\n");
886 if (mode == dbg_exec_finish && be_cpu->is_function_return(linear))
888 mode = ret_mode = dbg_exec_step_into_insn;
892 * See if the function we are stepping into has debug info
893 * and line numbers. If not, then we step over it instead.
894 * FIXME - we need to check for things like thunks or trampolines,
895 * as the actual function may in fact have debug info.
897 if (be_cpu->is_function_call(linear, &callee))
899 status = symbol_get_function_line_status(&callee);
900 #if 0
901 /* FIXME: we need to get the thunk type */
903 * Anytime we have a trampoline, step over it.
905 if ((mode == EXEC_STEP_OVER || mode == EXEC_STEPI_OVER)
906 && status == dbg_in_a_thunk)
908 WINE_WARN("Not stepping into trampoline at %p (no lines)\n",
909 memory_to_linear_addr(&callee));
910 mode = EXEC_STEP_OVER_TRAMPOLINE;
912 #endif
913 if (mode == dbg_exec_step_into_line && status == dbg_no_line_info)
915 WINE_WARN("Not stepping into function at %p (no lines)\n",
916 memory_to_linear_addr(&callee));
917 mode = dbg_exec_step_over_line;
921 if (mode == dbg_exec_step_into_line &&
922 symbol_get_function_line_status(&addr) == dbg_no_line_info)
924 dbg_printf("Single stepping until exit from function,\n"
925 "which has no line number information.\n");
926 ret_mode = mode = dbg_exec_finish;
929 switch (mode)
931 case dbg_exec_cont: /* Continuous execution */
932 be_cpu->single_step(&dbg_context, FALSE);
933 break_set_xpoints(TRUE);
934 break;
936 #if 0
937 case EXEC_STEP_OVER_TRAMPOLINE:
939 * This is the means by which we step over our conversion stubs
940 * in callfrom*.s and callto*.s. We dig the appropriate address
941 * off the stack, and we set the breakpoint there instead of the
942 * address just after the call.
944 be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
945 be_cpu_addr_stack, &addr);
946 /* FIXME: we assume stack grows as on an i386 */
947 addr.Offset += 2 * sizeof(unsigned int);
948 dbg_read_memory(memory_to_linear_addr(&addr),
949 &addr.Offset, sizeof(addr.Offset));
950 dbg_curr_process->bp[0].addr = addr;
951 dbg_curr_process->bp[0].enabled = TRUE;
952 dbg_curr_process->bp[0].refcount = 1;
953 dbg_curr_process->bp[0].skipcount = 0;
954 dbg_curr_process->bp[0].xpoint_type = be_xpoint_break;
955 dbg_curr_process->bp[0].condition = NULL;
956 be_cpu->single_step(&dbg_context, FALSE);
957 break_set_xpoints(TRUE);
958 break;
959 #endif
961 case dbg_exec_finish:
962 case dbg_exec_step_over_insn: /* Stepping over a call */
963 case dbg_exec_step_over_line: /* Stepping over a call */
964 if (be_cpu->is_step_over_insn(linear))
966 be_cpu->disasm_one_insn(&addr, FALSE);
967 dbg_curr_process->bp[0].addr = addr;
968 dbg_curr_process->bp[0].enabled = TRUE;
969 dbg_curr_process->bp[0].refcount = 1;
970 dbg_curr_process->bp[0].skipcount = 0;
971 dbg_curr_process->bp[0].xpoint_type = be_xpoint_break;
972 dbg_curr_process->bp[0].condition = NULL;
973 be_cpu->single_step(&dbg_context, FALSE);
974 break_set_xpoints(TRUE);
975 break;
977 /* else fall through to single-stepping */
979 case dbg_exec_step_into_line: /* Single-stepping a line */
980 case dbg_exec_step_into_insn: /* Single-stepping an instruction */
981 be_cpu->single_step(&dbg_context, TRUE);
982 break;
983 default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
985 dbg_curr_thread->step_over_bp = dbg_curr_process->bp[0];
986 dbg_curr_thread->exec_mode = ret_mode;
989 int break_add_condition(int num, struct expr* exp)
991 if (num <= 0 || num >= dbg_curr_process->next_bp ||
992 !dbg_curr_process->bp[num].refcount)
994 dbg_printf("Invalid breakpoint number %d\n", num);
995 return FALSE;
998 if (dbg_curr_process->bp[num].condition != NULL)
1000 expr_free(dbg_curr_process->bp[num].condition);
1001 dbg_curr_process->bp[num].condition = NULL;
1004 if (exp != NULL) dbg_curr_process->bp[num].condition = expr_clone(exp, NULL);
1006 return TRUE;