winepulse: Use mmdevdrv structs from mmdevapi.
[wine.git] / programs / winedbg / break.c
blob6cff37eb886de00ba713538d8f172e622830c1b0
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
343 dbg_printf("Unknown line number\n"
344 "(either out of file, or no code at given line number)\n");
347 /***********************************************************************
348 * break_check_delayed_bp
350 * Check is a registered delayed BP is now available.
352 void break_check_delayed_bp(void)
354 struct dbg_lvalue lvalue;
355 int i;
356 struct dbg_delayed_bp* dbp = dbg_curr_process->delayed_bp;
357 char hexbuf[MAX_OFFSET_TO_STR_LEN];
359 for (i = 0; i < dbg_curr_process->num_delayed_bp; i++)
361 if (dbp[i].is_symbol)
363 if (symbol_get_lvalue(dbp[i].u.symbol.name, dbp[i].u.symbol.lineno,
364 &lvalue, TRUE) != sglv_found)
365 continue;
366 if (!lvalue.in_debuggee) continue;
368 else
369 lvalue.addr = dbp[i].u.addr;
370 WINE_TRACE("trying to add delayed %s-bp\n", dbp[i].is_symbol ? "S" : "A");
371 if (!dbp[i].is_symbol)
372 WINE_TRACE("\t%04x:%s\n",
373 dbp[i].u.addr.Segment,
374 memory_offset_to_string(hexbuf, dbp[i].u.addr.Offset, 0));
375 else
376 WINE_TRACE("\t'%s' @ %d\n",
377 dbp[i].u.symbol.name, dbp[i].u.symbol.lineno);
379 if (break_add_break(&lvalue.addr, FALSE, dbp[i].software_bp))
380 memmove(&dbp[i], &dbp[i+1], (--dbg_curr_process->num_delayed_bp - i) * sizeof(*dbp));
384 /***********************************************************************
385 * break_add_watch
387 * Add a watchpoint.
389 static void break_add_watch(const struct dbg_lvalue* lvalue, BOOL is_write)
391 int num;
392 DWORD64 l = 4;
394 if (!lvalue->in_debuggee)
396 dbg_printf("Cannot set a watch point on register or register-based variable\n");
397 return;
399 num = init_xpoint((is_write) ? be_xpoint_watch_write : be_xpoint_watch_read,
400 &lvalue->addr);
401 if (num == -1) return;
403 if (lvalue->type.id != dbg_itype_none)
405 if (types_get_info(&lvalue->type, TI_GET_LENGTH, &l))
407 switch (l)
409 case 4: case 2: case 1: break;
410 default:
411 dbg_printf("Unsupported length (%I64x) for watch-points, defaulting to 4\n", l);
412 break;
415 else dbg_printf("Cannot get watch size, defaulting to 4\n");
417 dbg_curr_process->bp[num].w.len = (DWORD)l - 1;
419 if (!get_watched_value(num, &dbg_curr_process->bp[num].w.oldval))
421 dbg_printf("Bad address. Watchpoint not set\n");
422 dbg_curr_process->bp[num].refcount = 0;
423 return;
425 dbg_printf("Watchpoint %d at ", num);
426 print_address(&dbg_curr_process->bp[num].addr, TRUE);
427 dbg_printf("\n");
430 /******************************************************************
431 * break_add_watch_from_lvalue
433 * Adds a watch point from an address (stored in a lvalue)
435 void break_add_watch_from_lvalue(const struct dbg_lvalue* lvalue,BOOL is_write)
437 struct dbg_lvalue lval;
439 types_extract_as_address(lvalue, &lval.addr);
440 lval.type.id = dbg_itype_none;
442 break_add_watch(&lval, is_write);
445 /***********************************************************************
446 * break_add_watch_from_id
448 * Add a watchpoint from a symbol name
450 void break_add_watch_from_id(const char *name, BOOL is_write)
452 struct dbg_lvalue lvalue;
454 switch (symbol_get_lvalue(name, -1, &lvalue, TRUE))
456 case sglv_found:
457 break_add_watch(&lvalue, is_write);
458 break;
459 case sglv_unknown:
460 dbg_printf("Unable to add watchpoint\n");
461 break;
462 case sglv_aborted: /* user aborted symbol lookup */
463 break;
467 /***********************************************************************
468 * break_delete_xpoint
470 * Delete a breakpoint.
472 void break_delete_xpoint(int num)
474 struct dbg_breakpoint* bp = dbg_curr_process->bp;
476 if ((num <= 0) || (num >= dbg_curr_process->next_bp) ||
477 bp[num].refcount == 0)
479 dbg_printf("Invalid breakpoint number %d\n", num);
480 return;
483 if (--bp[num].refcount > 0)
484 return;
486 if (bp[num].condition != NULL)
488 expr_free(bp[num].condition);
489 bp[num].condition = NULL;
492 bp[num].enabled = FALSE;
493 bp[num].refcount = 0;
494 bp[num].skipcount = 0;
497 /******************************************************************
498 * break_delete_xpoints_from_module
500 * Remove all Xpoints from module which base is 'base'
502 void break_delete_xpoints_from_module(DWORD64 base)
504 IMAGEHLP_MODULE64 im, im_elf;
505 int i;
506 DWORD_PTR linear;
507 struct dbg_breakpoint* bp = dbg_curr_process->bp;
509 /* FIXME: should do it also on the ELF sibling if any */
510 im.SizeOfStruct = sizeof(im);
511 im_elf.SizeOfStruct = sizeof(im_elf);
512 if (!SymGetModuleInfo64(dbg_curr_process->handle, base, &im)) return;
514 /* try to get in fact the underlying ELF module (if any) */
515 if (SymGetModuleInfo64(dbg_curr_process->handle, im.BaseOfImage - 1, &im_elf) &&
516 im_elf.BaseOfImage <= im.BaseOfImage &&
517 im_elf.BaseOfImage + im_elf.ImageSize >= im.BaseOfImage + im.ImageSize)
518 im = im_elf;
520 for (i = 0; i < dbg_curr_process->next_bp; i++)
522 if (bp[i].refcount && bp[i].enabled)
524 linear = (DWORD_PTR)memory_to_linear_addr(&bp[i].addr);
525 if (im.BaseOfImage <= linear && linear < im.BaseOfImage + im.ImageSize)
527 break_delete_xpoint(i);
533 /***********************************************************************
534 * break_enable_xpoint
536 * Enable or disable a break point.
538 void break_enable_xpoint(int num, BOOL enable)
540 if ((num <= 0) || (num >= dbg_curr_process->next_bp) ||
541 dbg_curr_process->bp[num].refcount == 0)
543 dbg_printf("Invalid breakpoint number %d\n", num);
544 return;
546 dbg_curr_process->bp[num].enabled = enable != 0;
547 dbg_curr_process->bp[num].skipcount = 0;
551 /***********************************************************************
552 * find_triggered_watch
554 * Lookup the watchpoints to see if one has been triggered
555 * Return >= (watch point index) if one is found
556 * Return -1 if none found
558 * Unfortunately, Linux used to *NOT* (A REAL PITA) report with ptrace
559 * the DR6 register value, so we have to look with our own need the
560 * cause of the TRAP.
561 * -EP
563 static int find_triggered_watch(void)
565 int found = -1;
566 int i;
567 struct dbg_breakpoint* bp = dbg_curr_process->bp;
569 /* Method 1 => get triggered watchpoint from context (doesn't work on Linux
570 * 2.2.x). This should be fixed in >= 2.2.16
572 for (i = 0; i < dbg_curr_process->next_bp; i++)
574 DWORD64 val = 0;
576 if (bp[i].refcount && bp[i].enabled && !is_xpoint_break(i) &&
577 (dbg_curr_process->be_cpu->is_watchpoint_set(&dbg_context, bp[i].info)))
579 dbg_curr_process->be_cpu->clear_watchpoint(&dbg_context, bp[i].info);
581 if (get_watched_value(i, &val))
583 bp[i].w.oldval = val;
584 return i;
589 /* Method 1 failed, trying method 2 */
591 /* Method 2 => check if value has changed among registered watchpoints
592 * this really sucks, but this is how gdb 4.18 works on my linux box
593 * -EP
595 for (i = 0; i < dbg_curr_process->next_bp; i++)
597 DWORD64 val = 0;
599 if (bp[i].refcount && bp[i].enabled && !is_xpoint_break(i) &&
600 get_watched_value(i, &val))
602 if (val != bp[i].w.oldval)
604 dbg_curr_process->be_cpu->clear_watchpoint(&dbg_context, bp[i].info);
605 bp[i].w.oldval = val;
606 found = i;
607 /* cannot break, because two watch points may have been triggered on
608 * the same access
609 * only one will be reported to the user (FIXME ?)
614 return found;
617 /***********************************************************************
618 * break_info
620 * Display break & watch points information.
622 void break_info(void)
624 int i;
625 int nbp = 0, nwp = 0;
626 struct dbg_delayed_bp* dbp = dbg_curr_process->delayed_bp;
627 struct dbg_breakpoint* bp = dbg_curr_process->bp;
629 for (i = 1; i < dbg_curr_process->next_bp; i++)
631 if (bp[i].refcount)
633 if (is_xpoint_break(i)) nbp++; else nwp++;
637 if (nbp)
639 dbg_printf("Breakpoints:\n");
640 for (i = 1; i < dbg_curr_process->next_bp; i++)
642 if (!bp[i].refcount || !is_xpoint_break(i))
643 continue;
644 dbg_printf("%d: %c ", i, bp[i].enabled ? 'y' : 'n');
645 print_address(&bp[i].addr, TRUE);
646 dbg_printf(" (%u)%s\n", bp[i].refcount,
647 bp[i].xpoint_type == be_xpoint_watch_exec ? " (hardware assisted)" : "");
648 if (bp[i].condition != NULL)
650 dbg_printf("\t\tstop when ");
651 expr_print(bp[i].condition);
652 dbg_printf("\n");
656 else dbg_printf("No breakpoints\n");
657 if (nwp)
659 dbg_printf("Watchpoints:\n");
660 for (i = 1; i < dbg_curr_process->next_bp; i++)
662 if (!bp[i].refcount || is_xpoint_break(i))
663 continue;
664 dbg_printf("%d: %c ", i, bp[i].enabled ? 'y' : 'n');
665 print_address(&bp[i].addr, TRUE);
666 dbg_printf(" on %d byte%s (%c)\n",
667 bp[i].w.len + 1, bp[i].w.len > 0 ? "s" : "",
668 bp[i].xpoint_type == be_xpoint_watch_write ? 'W' : 'R');
669 if (bp[i].condition != NULL)
671 dbg_printf("\t\tstop when ");
672 expr_print(bp[i].condition);
673 dbg_printf("\n");
677 else dbg_printf("No watchpoints\n");
678 if (dbg_curr_process->num_delayed_bp)
680 dbg_printf("Delayed breakpoints:\n");
681 for (i = 0; i < dbg_curr_process->num_delayed_bp; i++)
683 if (dbp[i].is_symbol)
685 dbg_printf("%d: %s", i, dbp[i].u.symbol.name);
686 if (dbp[i].u.symbol.lineno != -1)
687 dbg_printf(" at line %u", dbp[i].u.symbol.lineno);
689 else
691 dbg_printf("%d: ", i);
692 print_address(&dbp[i].u.addr, FALSE);
694 dbg_printf("\n");
699 /***********************************************************************
700 * should_stop
702 * Check whether or not the condition (bp / skipcount) of a break/watch
703 * point are met.
705 static BOOL should_stop(int bpnum)
707 struct dbg_breakpoint* bp = &dbg_curr_process->bp[bpnum];
709 if (bp->condition != NULL)
711 struct dbg_lvalue lvalue = expr_eval(bp->condition);
713 if (lvalue.type.id == dbg_itype_none)
716 * Something wrong - unable to evaluate this expression.
718 dbg_printf("Unable to evaluate expression ");
719 expr_print(bp->condition);
720 dbg_printf("\nTurning off condition\n");
721 break_add_condition(bpnum, NULL);
723 else if (!types_extract_as_integer(&lvalue))
725 return FALSE;
729 if (bp->skipcount > 0) bp->skipcount--;
730 return bp->skipcount == 0;
733 /***********************************************************************
734 * break_should_continue
736 * Determine if we should continue execution after a SIGTRAP signal when
737 * executing in the given mode.
739 BOOL break_should_continue(ADDRESS64* addr, DWORD code)
741 enum dbg_exec_mode mode = dbg_curr_thread->exec_mode;
744 if (dbg_curr_thread->stopped_xpoint > 0)
746 if (!should_stop(dbg_curr_thread->stopped_xpoint)) return TRUE;
748 switch (dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].xpoint_type)
750 case be_xpoint_break:
751 case be_xpoint_watch_exec:
752 dbg_printf("Stopped on breakpoint %d at ", dbg_curr_thread->stopped_xpoint);
753 print_address(&dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].addr, TRUE);
754 dbg_printf("\n");
755 break;
756 case be_xpoint_watch_read:
757 case be_xpoint_watch_write:
758 dbg_printf("Stopped on watchpoint %d at ", dbg_curr_thread->stopped_xpoint);
759 print_address(addr, TRUE);
760 dbg_printf(" new value %I64x\n",
761 dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].w.oldval);
763 return FALSE;
767 * If our mode indicates that we are stepping line numbers,
768 * get the current function, and figure out if we are exactly
769 * on a line number or not.
771 if (mode == dbg_exec_step_over_line || mode == dbg_exec_step_into_line)
773 if (symbol_get_function_line_status(addr) == dbg_on_a_line_number)
774 dbg_curr_thread->exec_count--;
776 else if (mode == dbg_exec_step_over_insn || mode == dbg_exec_step_into_insn)
777 dbg_curr_thread->exec_count--;
779 if (dbg_curr_thread->exec_count > 0 || mode == dbg_exec_finish)
782 * We still need to execute more instructions.
784 return TRUE;
787 /* no breakpoint, continue if in continuous mode */
788 return mode == dbg_exec_cont || mode == dbg_exec_finish;
791 /***********************************************************************
792 * break_adjust_pc
794 * Adjust PC to the address where the trap (if any) actually occurred
795 * Also sets dbg_curr_thread->stopped_xpoint
797 void break_adjust_pc(ADDRESS64* addr, DWORD code, BOOL first_chance, BOOL* is_break)
799 /* break / watch points are handled on first chance */
800 if ( !first_chance )
802 *is_break = TRUE;
803 dbg_curr_thread->stopped_xpoint = -1;
804 return;
806 *is_break = FALSE;
808 /* If not single-stepping, back up to the break instruction */
809 if (code == EXCEPTION_BREAKPOINT)
810 addr->Offset += dbg_curr_process->be_cpu->adjust_pc_for_break(&dbg_context, TRUE);
812 dbg_curr_thread->stopped_xpoint = find_xpoint(addr, be_xpoint_break);
813 dbg_curr_process->bp[0].enabled = FALSE; /* disable the step-over breakpoint */
815 if (dbg_curr_thread->stopped_xpoint > 0) return;
817 if (dbg_curr_thread->stopped_xpoint < 0)
819 dbg_curr_thread->stopped_xpoint = find_xpoint(addr, be_xpoint_watch_exec);
820 if (dbg_curr_thread->stopped_xpoint < 0)
821 dbg_curr_thread->stopped_xpoint = find_triggered_watch();
822 if (dbg_curr_thread->stopped_xpoint > 0)
824 /* If not single-stepping, do not back up over the break instruction */
825 if (code == EXCEPTION_BREAKPOINT)
826 addr->Offset += dbg_curr_process->be_cpu->adjust_pc_for_break(&dbg_context, FALSE);
827 return;
831 /* If there's no breakpoint and we are not single-stepping, then
832 * either we must have encountered a break insn in the Windows program
833 * or someone is trying to stop us
835 if (dbg_curr_thread->stopped_xpoint == -1 && code == EXCEPTION_BREAKPOINT)
837 *is_break = TRUE;
838 addr->Offset += dbg_curr_process->be_cpu->adjust_pc_for_break(&dbg_context, FALSE);
842 /***********************************************************************
843 * break_suspend_execution
845 * Remove all bp before entering the debug loop
847 void break_suspend_execution(void)
849 break_set_xpoints(FALSE);
850 dbg_curr_process->bp[0] = dbg_curr_thread->step_over_bp;
853 /***********************************************************************
854 * break_restart_execution
856 * Set the bp to the correct state to restart execution
857 * in the given mode.
859 void break_restart_execution(int count)
861 ADDRESS64 addr;
862 enum dbg_line_status status;
863 enum dbg_exec_mode mode, ret_mode;
864 ADDRESS64 callee;
865 void* linear;
867 memory_get_current_pc(&addr);
868 linear = memory_to_linear_addr(&addr);
871 * This is the mode we will be running in after we finish. We would like
872 * to be able to modify this in certain cases.
874 ret_mode = mode = dbg_curr_thread->exec_mode;
876 /* we've stopped on a xpoint (other than step over) */
877 if (dbg_curr_thread->stopped_xpoint > 0)
880 * If we have set a new value, then save it in the BP number.
882 if (count != 0 && mode == dbg_exec_cont)
884 dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].skipcount = count;
886 /* If we've stopped on a breakpoint, single step over it (, then run) */
887 if (is_xpoint_break(dbg_curr_thread->stopped_xpoint))
888 mode = dbg_exec_step_into_insn;
890 else if (mode == dbg_exec_cont && count > 1)
892 dbg_printf("Not stopped at any breakpoint; argument ignored.\n");
895 if (mode == dbg_exec_finish && dbg_curr_process->be_cpu->is_function_return(linear))
897 mode = ret_mode = dbg_exec_step_into_insn;
901 * See if the function we are stepping into has debug info
902 * and line numbers. If not, then we step over it instead.
903 * FIXME - we need to check for things like thunks or trampolines,
904 * as the actual function may in fact have debug info.
906 if (dbg_curr_process->be_cpu->is_function_call(linear, &callee))
908 status = symbol_get_function_line_status(&callee);
909 #if 0
910 /* FIXME: we need to get the thunk type */
912 * Anytime we have a trampoline, step over it.
914 if ((mode == EXEC_STEP_OVER || mode == EXEC_STEPI_OVER)
915 && status == dbg_in_a_thunk)
917 WINE_WARN("Not stepping into trampoline at %p (no lines)\n",
918 memory_to_linear_addr(&callee));
919 mode = EXEC_STEP_OVER_TRAMPOLINE;
921 #endif
922 if (mode == dbg_exec_step_into_line && status == dbg_no_line_info)
924 WINE_WARN("Not stepping into function at %p (no lines)\n",
925 memory_to_linear_addr(&callee));
926 mode = dbg_exec_step_over_line;
930 if (mode == dbg_exec_step_into_line &&
931 symbol_get_function_line_status(&addr) == dbg_no_line_info)
933 dbg_printf("Single stepping until exit from function,\n"
934 "which has no line number information.\n");
935 ret_mode = mode = dbg_exec_finish;
938 switch (mode)
940 case dbg_exec_cont: /* Continuous execution */
941 dbg_curr_process->be_cpu->single_step(&dbg_context, FALSE);
942 break_set_xpoints(TRUE);
943 break;
945 #if 0
946 case EXEC_STEP_OVER_TRAMPOLINE:
948 * This is the means by which we step over our conversion stubs
949 * in callfrom*.s and callto*.s. We dig the appropriate address
950 * off the stack, and we set the breakpoint there instead of the
951 * address just after the call.
953 be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
954 be_cpu_addr_stack, &addr);
955 /* FIXME: we assume stack grows as on an i386 */
956 addr.Offset += 2 * sizeof(unsigned int);
957 dbg_read_memory(memory_to_linear_addr(&addr),
958 &addr.Offset, sizeof(addr.Offset));
959 dbg_curr_process->bp[0].addr = addr;
960 dbg_curr_process->bp[0].enabled = TRUE;
961 dbg_curr_process->bp[0].refcount = 1;
962 dbg_curr_process->bp[0].skipcount = 0;
963 dbg_curr_process->bp[0].xpoint_type = be_xpoint_break;
964 dbg_curr_process->bp[0].condition = NULL;
965 be_cpu->single_step(&dbg_context, FALSE);
966 break_set_xpoints(TRUE);
967 break;
968 #endif
970 case dbg_exec_finish:
971 case dbg_exec_step_over_insn: /* Stepping over a call */
972 case dbg_exec_step_over_line: /* Stepping over a call */
973 if (dbg_curr_process->be_cpu->is_step_over_insn(linear))
975 dbg_curr_process->be_cpu->disasm_one_insn(&addr, FALSE);
976 dbg_curr_process->bp[0].addr = addr;
977 dbg_curr_process->bp[0].enabled = TRUE;
978 dbg_curr_process->bp[0].refcount = 1;
979 dbg_curr_process->bp[0].skipcount = 0;
980 dbg_curr_process->bp[0].xpoint_type = be_xpoint_break;
981 dbg_curr_process->bp[0].condition = NULL;
982 dbg_curr_process->be_cpu->single_step(&dbg_context, FALSE);
983 break_set_xpoints(TRUE);
984 break;
986 /* else fall through to single-stepping */
988 case dbg_exec_step_into_line: /* Single-stepping a line */
989 case dbg_exec_step_into_insn: /* Single-stepping an instruction */
990 dbg_curr_process->be_cpu->single_step(&dbg_context, TRUE);
991 break;
992 default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
994 dbg_curr_thread->step_over_bp = dbg_curr_process->bp[0];
995 dbg_curr_thread->exec_mode = ret_mode;
998 BOOL break_add_condition(int num, struct expr* exp)
1000 if (num <= 0 || num >= dbg_curr_process->next_bp ||
1001 !dbg_curr_process->bp[num].refcount)
1003 dbg_printf("Invalid breakpoint number %d\n", num);
1004 return FALSE;
1007 if (dbg_curr_process->bp[num].condition != NULL)
1009 expr_free(dbg_curr_process->bp[num].condition);
1010 dbg_curr_process->bp[num].condition = NULL;
1013 if (exp != NULL) dbg_curr_process->bp[num].condition = expr_clone(exp, NULL);
1015 return TRUE;