Release 971130
[wine/wine-kai.git] / debugger / break.c
blobd842f500db866ad3ee202a6663b7bf760df37ab7
1 /*
2 * Debugger break-points handling
4 * Copyright 1994 Martin von Loewis
5 * Copyright 1995 Alexandre Julliard
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <sys/types.h>
11 #include <sys/mman.h>
12 #include "module.h"
13 #include "process.h"
14 #include "toolhelp.h"
15 #include "windows.h"
16 #include "debugger.h"
18 #define INT3 0xcc /* int 3 opcode */
20 #define MAX_BREAKPOINTS 100
22 typedef struct
24 DBG_ADDR addr;
25 BYTE addrlen;
26 BYTE opcode;
27 BOOL16 enabled;
28 WORD skipcount;
29 BOOL16 in_use;
30 struct expr * condition;
31 } BREAKPOINT;
33 static BREAKPOINT breakpoints[MAX_BREAKPOINTS];
35 static int next_bp = 1; /* breakpoint 0 is reserved for step-over */
38 /***********************************************************************
39 * DEBUG_ChangeOpcode
41 * Change the opcode at segment:addr.
43 static void DEBUG_SetOpcode( const DBG_ADDR *addr, BYTE op )
45 BYTE *ptr;
47 if (addr->seg) ptr = (BYTE *)PTR_SEG_OFF_TO_LIN( addr->seg, addr->off );
48 else ptr = (BYTE *)addr->off;
50 /* There are a couple of problems with this. On Linux prior to
51 1.1.62, this call fails (ENOACCESS) due to a bug in fs/exec.c.
52 This code is currently not tested at all on BSD.
53 How do I get the old protection in order to restore it later on?
55 if (mprotect((caddr_t)((int)ptr & (~4095)), 4096,
56 PROT_READ | PROT_WRITE | PROT_EXEC) == -1)
58 perror( "Can't set break point" );
59 return;
61 *ptr = op;
62 /* mprotect((caddr_t)(addr->off & ~4095), 4096,
63 PROT_READ | PROT_EXEC ); */
67 /***********************************************************************
68 * DEBUG_IsStepOverInstr
70 * Determine if the instruction at CS:EIP is an instruction that
71 * we need to step over (like a call or a repetitive string move).
73 static BOOL32 DEBUG_IsStepOverInstr()
75 BYTE *instr = (BYTE *)PTR_SEG_OFF_TO_LIN( CS_reg(&DEBUG_context),
76 EIP_reg(&DEBUG_context) );
78 for (;;)
80 switch(*instr)
82 /* Skip all prefixes */
84 case 0x2e: /* cs: */
85 case 0x36: /* ss: */
86 case 0x3e: /* ds: */
87 case 0x26: /* es: */
88 case 0x64: /* fs: */
89 case 0x65: /* gs: */
90 case 0x66: /* opcode size prefix */
91 case 0x67: /* addr size prefix */
92 case 0xf0: /* lock */
93 case 0xf2: /* repne */
94 case 0xf3: /* repe */
95 instr++;
96 continue;
98 /* Handle call instructions */
100 case 0xe8: /* call <offset> */
101 case 0x9a: /* lcall <seg>:<off> */
102 return TRUE;
104 case 0xff: /* call <regmodrm> */
105 return (((instr[1] & 0x38) == 0x10) ||
106 ((instr[1] & 0x38) == 0x18));
108 /* Handle string instructions */
110 case 0x6c: /* insb */
111 case 0x6d: /* insw */
112 case 0x6e: /* outsb */
113 case 0x6f: /* outsw */
114 case 0xa4: /* movsb */
115 case 0xa5: /* movsw */
116 case 0xa6: /* cmpsb */
117 case 0xa7: /* cmpsw */
118 case 0xaa: /* stosb */
119 case 0xab: /* stosw */
120 case 0xac: /* lodsb */
121 case 0xad: /* lodsw */
122 case 0xae: /* scasb */
123 case 0xaf: /* scasw */
124 return TRUE;
126 default:
127 return FALSE;
133 /***********************************************************************
134 * DEBUG_IsFctReturn
136 * Determine if the instruction at CS:EIP is an instruction that
137 * is a function return.
139 BOOL32 DEBUG_IsFctReturn(void)
141 BYTE *instr = (BYTE *)PTR_SEG_OFF_TO_LIN( CS_reg(&DEBUG_context),
142 EIP_reg(&DEBUG_context) );
144 for (;;)
146 switch(*instr)
148 case 0xc2:
149 case 0xc3:
150 return TRUE;
151 default:
152 return FALSE;
158 /***********************************************************************
159 * DEBUG_SetBreakpoints
161 * Set or remove all the breakpoints.
163 void DEBUG_SetBreakpoints( BOOL32 set )
165 int i;
167 for (i = 0; i < MAX_BREAKPOINTS; i++)
169 if (breakpoints[i].in_use && breakpoints[i].enabled)
171 /* Note: we check for read here, because if reading is allowed */
172 /* writing permission will be forced in DEBUG_SetOpcode. */
173 if (DEBUG_IsBadReadPtr( &breakpoints[i].addr, 1 ))
175 fprintf( stderr, "Invalid address for breakpoint %d, disabling it\n", i );
176 breakpoints[i].enabled = FALSE;
178 else DEBUG_SetOpcode( &breakpoints[i].addr,
179 set ? INT3 : breakpoints[i].opcode );
185 /***********************************************************************
186 * DEBUG_FindBreakpoint
188 * Find the breakpoint for a given address. Return the breakpoint
189 * number or -1 if none.
191 int DEBUG_FindBreakpoint( const DBG_ADDR *addr )
193 int i;
195 for (i = 0; i < MAX_BREAKPOINTS; i++)
197 if (breakpoints[i].in_use && breakpoints[i].enabled &&
198 breakpoints[i].addr.seg == addr->seg &&
199 breakpoints[i].addr.off == addr->off) return i;
201 return -1;
205 /***********************************************************************
206 * DEBUG_AddBreakpoint
208 * Add a breakpoint.
210 void DEBUG_AddBreakpoint( const DBG_ADDR *address )
212 DBG_ADDR addr = *address;
213 int num;
214 unsigned int seg2;
215 BYTE *p;
217 DBG_FIX_ADDR_SEG( &addr, CS_reg(&DEBUG_context) );
219 if( addr.type != NULL && addr.type == DEBUG_TypeIntConst )
222 * We know that we have the actual offset stored somewhere
223 * else in 32-bit space. Grab it, and we
224 * should be all set.
226 seg2 = addr.seg;
227 addr.seg = 0;
228 addr.off = DEBUG_GetExprValue(&addr, NULL);
229 addr.seg = seg2;
231 if (!DBG_CHECK_READ_PTR( &addr, 1 )) return;
233 if (next_bp < MAX_BREAKPOINTS)
234 num = next_bp++;
235 else /* try to find an empty slot */
237 for (num = 1; num < MAX_BREAKPOINTS; num++)
238 if (!breakpoints[num].in_use) break;
239 if (num >= MAX_BREAKPOINTS)
241 fprintf( stderr, "Too many breakpoints. Please delete some.\n" );
242 return;
245 p = DBG_ADDR_TO_LIN( &addr );
246 breakpoints[num].addr = addr;
247 breakpoints[num].addrlen = !addr.seg ? 32 :
248 (GET_SEL_FLAGS(addr.seg) & LDT_FLAGS_32BIT) ? 32 : 16;
249 breakpoints[num].opcode = *p;
250 breakpoints[num].enabled = TRUE;
251 breakpoints[num].in_use = TRUE;
252 breakpoints[num].skipcount = 0;
253 fprintf( stderr, "Breakpoint %d at ", num );
254 DEBUG_PrintAddress( &breakpoints[num].addr, breakpoints[num].addrlen,
255 TRUE );
256 fprintf( stderr, "\n" );
260 /***********************************************************************
261 * DEBUG_DelBreakpoint
263 * Delete a breakpoint.
265 void DEBUG_DelBreakpoint( int num )
267 if ((num <= 0) || (num >= next_bp) || !breakpoints[num].in_use)
269 fprintf( stderr, "Invalid breakpoint number %d\n", num );
270 return;
273 if( breakpoints[num].condition != NULL )
275 DEBUG_FreeExpr(breakpoints[num].condition);
276 breakpoints[num].condition = NULL;
279 breakpoints[num].enabled = FALSE;
280 breakpoints[num].in_use = FALSE;
281 breakpoints[num].skipcount = 0;
285 /***********************************************************************
286 * DEBUG_EnableBreakpoint
288 * Enable or disable a break point.
290 void DEBUG_EnableBreakpoint( int num, BOOL32 enable )
292 if ((num <= 0) || (num >= next_bp) || !breakpoints[num].in_use)
294 fprintf( stderr, "Invalid breakpoint number %d\n", num );
295 return;
297 breakpoints[num].enabled = enable;
298 breakpoints[num].skipcount = 0;
302 /***********************************************************************
303 * DEBUG_InfoBreakpoints
305 * Display break points information.
307 void DEBUG_InfoBreakpoints(void)
309 int i;
311 fprintf( stderr, "Breakpoints:\n" );
312 for (i = 1; i < next_bp; i++)
314 if (breakpoints[i].in_use)
316 fprintf( stderr, "%d: %c ", i, breakpoints[i].enabled ? 'y' : 'n');
317 DEBUG_PrintAddress( &breakpoints[i].addr, breakpoints[i].addrlen,
318 TRUE);
319 fprintf( stderr, "\n" );
320 if( breakpoints[i].condition != NULL )
322 fprintf(stderr, "\t\tstop when ");
323 DEBUG_DisplayExpr(breakpoints[i].condition);
324 fprintf(stderr, "\n");
331 /***********************************************************************
332 * DEBUG_AddModuleBreakpoints
334 * Add a breakpoint at the start of every loaded module.
336 void DEBUG_AddModuleBreakpoints(void)
338 MODULEENTRY entry;
339 NE_MODULE *pModule;
340 BOOL32 ok;
341 DBG_ADDR addr = { NULL, 0, 0 };
343 for (ok = ModuleFirst(&entry); ok; ok = ModuleNext(&entry))
345 if (!(pModule = MODULE_GetPtr( entry.hModule ))) continue;
346 if (pModule->flags & NE_FFLAGS_LIBMODULE) continue; /* Library */
348 if (pModule->flags & NE_FFLAGS_WIN32) /* PE module */
350 PE_MODREF *pem;
351 if (!pCurrentProcess) continue;
352 pem = pCurrentProcess->modref_list;
353 while (pem)
355 if (pem->module == pModule->module32) break;
356 pem = pem->next;
358 if (!pem) continue;
359 addr.seg = 0;
360 addr.off = (DWORD)RVA_PTR( pem->module,
361 OptionalHeader.AddressOfEntryPoint );
362 fprintf( stderr, "Win32 task '%s': ", entry.szModule );
363 DEBUG_AddBreakpoint( &addr );
365 else /* NE module */
367 addr.seg = NE_SEG_TABLE(pModule)[pModule->cs-1].selector;
368 addr.off = pModule->ip;
369 fprintf( stderr, "Win16 task '%s': ", entry.szModule );
370 DEBUG_AddBreakpoint( &addr );
374 DEBUG_SetBreakpoints( TRUE ); /* Setup breakpoints */
378 /***********************************************************************
379 * DEBUG_ShouldContinue
381 * Determine if we should continue execution after a SIGTRAP signal when
382 * executing in the given mode.
384 BOOL32 DEBUG_ShouldContinue( enum exec_mode mode, int * count )
386 DBG_ADDR addr;
387 DBG_ADDR cond_addr;
388 int bpnum;
389 struct list_id list;
390 WORD cs;
392 /* If not single-stepping, back up over the int3 instruction */
393 if (!(EFL_reg(&DEBUG_context) & STEP_FLAG)) EIP_reg(&DEBUG_context)--;
395 GET_CS(cs);
396 addr.seg = (CS_reg(&DEBUG_context) == cs) ? 0 : CS_reg(&DEBUG_context);
397 addr.off = EIP_reg(&DEBUG_context);
399 bpnum = DEBUG_FindBreakpoint( &addr );
400 breakpoints[0].enabled = 0; /* disable the step-over breakpoint */
402 if ((bpnum != 0) && (bpnum != -1))
404 if( breakpoints[bpnum].condition != NULL )
406 cond_addr = DEBUG_EvalExpr(breakpoints[bpnum].condition);
407 if( cond_addr.type == NULL )
410 * Something wrong - unable to evaluate this expression.
412 fprintf(stderr, "Unable to evaluate expression ");
413 DEBUG_DisplayExpr(breakpoints[bpnum].condition);
414 fprintf(stderr, "\nTurning off condition\n");
415 DEBUG_AddBPCondition(bpnum, NULL);
417 else if( ! DEBUG_GetExprValue( &cond_addr, NULL) )
419 return TRUE;
423 if( breakpoints[bpnum].skipcount > 0 )
425 breakpoints[bpnum].skipcount--;
426 if( breakpoints[bpnum].skipcount > 0 )
428 return TRUE;
431 fprintf( stderr, "Stopped on breakpoint %d at ", bpnum );
432 DEBUG_PrintAddress( &breakpoints[bpnum].addr,
433 breakpoints[bpnum].addrlen, TRUE );
434 fprintf( stderr, "\n" );
437 * See if there is a source file for this bp. If so,
438 * then dig it out and display one line.
440 DEBUG_FindNearestSymbol( &addr, TRUE, NULL, 0, &list);
441 if( list.sourcefile != NULL )
443 DEBUG_List(&list, NULL, 0);
445 return FALSE;
449 * If our mode indicates that we are stepping line numbers,
450 * get the current function, and figure out if we are exactly
451 * on a line number or not.
453 if( mode == EXEC_STEP_OVER
454 || mode == EXEC_STEP_INSTR )
456 if( DEBUG_CheckLinenoStatus(&addr) == AT_LINENUMBER )
458 (*count)--;
461 else if( mode == EXEC_STEPI_OVER
462 || mode == EXEC_STEPI_INSTR )
465 (*count)--;
468 if( *count > 0 || mode == EXEC_FINISH )
471 * We still need to execute more instructions.
473 return TRUE;
477 * If we are about to stop, then print out the source line if we
478 * have it.
480 if( (mode != EXEC_CONT && mode != EXEC_FINISH) )
482 DEBUG_FindNearestSymbol( &addr, TRUE, NULL, 0, &list);
483 if( list.sourcefile != NULL )
485 DEBUG_List(&list, NULL, 0);
489 /* If there's no breakpoint and we are not single-stepping, then we */
490 /* must have encountered an int3 in the Windows program; let's skip it. */
491 if ((bpnum == -1) && !(EFL_reg(&DEBUG_context) & STEP_FLAG))
492 EIP_reg(&DEBUG_context)++;
494 /* no breakpoint, continue if in continuous mode */
495 return (mode == EXEC_CONT || mode == EXEC_FINISH);
499 /***********************************************************************
500 * DEBUG_RestartExecution
502 * Set the breakpoints to the correct state to restart execution
503 * in the given mode.
505 enum exec_mode DEBUG_RestartExecution( enum exec_mode mode, int count )
507 DBG_ADDR addr;
508 DBG_ADDR addr2;
509 int bp;
510 int delta;
511 int status;
512 unsigned int * value;
513 enum exec_mode ret_mode;
514 BYTE *instr;
515 WORD cs;
517 GET_CS(cs);
518 addr.seg = (CS_reg(&DEBUG_context) == cs) ? 0 : CS_reg(&DEBUG_context);
519 addr.off = EIP_reg(&DEBUG_context);
522 * This is the mode we will be running in after we finish. We would like
523 * to be able to modify this in certain cases.
525 ret_mode = mode;
527 bp = DEBUG_FindBreakpoint( &addr );
528 if ( bp != -1 && bp != 0)
531 * If we have set a new value, then save it in the BP number.
533 if( count != 0 && mode == EXEC_CONT )
535 breakpoints[bp].skipcount = count;
537 mode = EXEC_STEPI_INSTR; /* If there's a breakpoint, skip it */
539 else
541 if( mode == EXEC_CONT && count > 1 )
543 fprintf(stderr,"Not stopped at any breakpoint; argument ignored.\n");
547 if( mode == EXEC_FINISH && DEBUG_IsFctReturn() )
549 mode = ret_mode = EXEC_STEPI_INSTR;
552 instr = (BYTE *)PTR_SEG_OFF_TO_LIN( CS_reg(&DEBUG_context),
553 EIP_reg(&DEBUG_context) );
555 * See if the function we are stepping into has debug info
556 * and line numbers. If not, then we step over it instead.
557 * FIXME - we need to check for things like thunks or trampolines,
558 * as the actual function may in fact have debug info.
560 if( *instr == 0xe8 )
562 delta = *(unsigned int*) (instr + 1);
563 addr2 = addr;
564 DEBUG_Disasm(&addr2, FALSE);
565 addr2.off += delta;
567 status = DEBUG_CheckLinenoStatus(&addr2);
569 * Anytime we have a trampoline, step over it.
571 if( ((mode == EXEC_STEP_OVER) || (mode == EXEC_STEPI_OVER))
572 && status == FUNC_IS_TRAMPOLINE )
574 #if 0
575 fprintf(stderr, "Not stepping into trampoline at %x (no lines)\n",
576 addr2.off);
577 #endif
578 mode = EXEC_STEP_OVER_TRAMPOLINE;
581 if( mode == EXEC_STEP_INSTR && status == FUNC_HAS_NO_LINES )
583 #if 0
584 fprintf(stderr, "Not stepping into function at %x (no lines)\n",
585 addr2.off);
586 #endif
587 mode = EXEC_STEP_OVER;
592 if( mode == EXEC_STEP_INSTR )
594 if( DEBUG_CheckLinenoStatus(&addr) == FUNC_HAS_NO_LINES )
596 fprintf(stderr, "Single stepping until exit from function, \n");
597 fprintf(stderr, "which has no line number information.\n");
599 ret_mode = mode = EXEC_FINISH;
603 switch(mode)
605 case EXEC_CONT: /* Continuous execution */
606 EFL_reg(&DEBUG_context) &= ~STEP_FLAG;
607 DEBUG_SetBreakpoints( TRUE );
608 break;
610 case EXEC_STEP_OVER_TRAMPOLINE:
612 * This is the means by which we step over our conversion stubs
613 * in callfrom*.s and callto*.s. We dig the appropriate address
614 * off the stack, and we set the breakpoint there instead of the
615 * address just after the call.
617 value = (unsigned int *) ESP_reg(&DEBUG_context) + 2;
618 addr.off = *value;
619 EFL_reg(&DEBUG_context) &= ~STEP_FLAG;
620 breakpoints[0].addr = addr;
621 breakpoints[0].enabled = TRUE;
622 breakpoints[0].in_use = TRUE;
623 breakpoints[0].skipcount = 0;
624 breakpoints[0].opcode = *(BYTE *)DBG_ADDR_TO_LIN( &addr );
625 DEBUG_SetBreakpoints( TRUE );
626 break;
628 case EXEC_FINISH:
629 case EXEC_STEPI_OVER: /* Stepping over a call */
630 case EXEC_STEP_OVER: /* Stepping over a call */
631 if (DEBUG_IsStepOverInstr())
633 EFL_reg(&DEBUG_context) &= ~STEP_FLAG;
634 DEBUG_Disasm(&addr, FALSE);
635 breakpoints[0].addr = addr;
636 breakpoints[0].enabled = TRUE;
637 breakpoints[0].in_use = TRUE;
638 breakpoints[0].skipcount = 0;
639 breakpoints[0].opcode = *(BYTE *)DBG_ADDR_TO_LIN( &addr );
640 DEBUG_SetBreakpoints( TRUE );
641 break;
643 /* else fall through to single-stepping */
645 case EXEC_STEP_INSTR: /* Single-stepping an instruction */
646 case EXEC_STEPI_INSTR: /* Single-stepping an instruction */
647 EFL_reg(&DEBUG_context) |= STEP_FLAG;
648 break;
650 return ret_mode;
654 DEBUG_AddBPCondition(int num, struct expr * exp)
656 if ((num <= 0) || (num >= next_bp) || !breakpoints[num].in_use)
658 fprintf( stderr, "Invalid breakpoint number %d\n", num );
659 return FALSE;
662 if( breakpoints[num].condition != NULL )
664 DEBUG_FreeExpr(breakpoints[num].condition);
665 breakpoints[num].condition = NULL;
668 if( exp != NULL )
670 breakpoints[num].condition = DEBUG_CloneExpr(exp);
673 return TRUE;