2 * Debugger break-points handling
4 * Copyright 1994 Martin von Loewis
5 * Copyright 1995 Alexandre Julliard
10 #include <sys/types.h>
15 #define INT3 0xcc /* int 3 opcode */
17 #define MAX_BREAKPOINTS 100
27 struct expr * condition;
30 static BREAKPOINT breakpoints[MAX_BREAKPOINTS];
32 static int next_bp = 1; /* breakpoint 0 is reserved for step-over */
35 /***********************************************************************
38 * Change the opcode at segment:addr.
40 static void DEBUG_SetOpcode( const DBG_ADDR *addr, BYTE op )
44 if (addr->seg) ptr = (BYTE *)PTR_SEG_OFF_TO_LIN( addr->seg, addr->off );
45 else ptr = (BYTE *)addr->off;
47 /* There are a couple of problems with this. On Linux prior to
48 1.1.62, this call fails (ENOACCESS) due to a bug in fs/exec.c.
49 This code is currently not tested at all on BSD.
50 How do I get the old protection in order to restore it later on?
52 if (mprotect((caddr_t)((int)ptr & (~4095)), 4096,
53 PROT_READ | PROT_WRITE | PROT_EXEC) == -1)
55 perror( "Can't set break point" );
59 /* mprotect((caddr_t)(addr->off & ~4095), 4096,
60 PROT_READ | PROT_EXEC ); */
64 /***********************************************************************
65 * DEBUG_IsStepOverInstr
67 * Determine if the instruction at CS:EIP is an instruction that
68 * we need to step over (like a call or a repetitive string move).
70 static BOOL32 DEBUG_IsStepOverInstr()
72 BYTE *instr = (BYTE *)PTR_SEG_OFF_TO_LIN( CS_reg(&DEBUG_context),
73 EIP_reg(&DEBUG_context) );
79 /* Skip all prefixes */
87 case 0x66: /* opcode size prefix */
88 case 0x67: /* addr size prefix */
90 case 0xf2: /* repne */
95 /* Handle call instructions */
97 case 0xe8: /* call <offset> */
98 case 0x9a: /* lcall <seg>:<off> */
101 case 0xff: /* call <regmodrm> */
102 return (((instr[1] & 0x38) == 0x10) ||
103 ((instr[1] & 0x38) == 0x18));
105 /* Handle string instructions */
107 case 0x6c: /* insb */
108 case 0x6d: /* insw */
109 case 0x6e: /* outsb */
110 case 0x6f: /* outsw */
111 case 0xa4: /* movsb */
112 case 0xa5: /* movsw */
113 case 0xa6: /* cmpsb */
114 case 0xa7: /* cmpsw */
115 case 0xaa: /* stosb */
116 case 0xab: /* stosw */
117 case 0xac: /* lodsb */
118 case 0xad: /* lodsw */
119 case 0xae: /* scasb */
120 case 0xaf: /* scasw */
130 /***********************************************************************
133 * Determine if the instruction at CS:EIP is an instruction that
134 * is a function return.
136 BOOL32 DEBUG_IsFctReturn(void)
138 BYTE *instr = (BYTE *)PTR_SEG_OFF_TO_LIN( CS_reg(&DEBUG_context),
139 EIP_reg(&DEBUG_context) );
155 /***********************************************************************
156 * DEBUG_SetBreakpoints
158 * Set or remove all the breakpoints.
160 void DEBUG_SetBreakpoints( BOOL32 set )
164 for (i = 0; i < MAX_BREAKPOINTS; i++)
166 if (breakpoints[i].in_use && breakpoints[i].enabled)
168 /* Note: we check for read here, because if reading is allowed */
169 /* writing permission will be forced in DEBUG_SetOpcode. */
170 if (DEBUG_IsBadReadPtr( &breakpoints[i].addr, 1 ))
172 fprintf( stderr, "Invalid address for breakpoint %d, disabling it\n", i );
173 breakpoints[i].enabled = FALSE;
175 else DEBUG_SetOpcode( &breakpoints[i].addr,
176 set ? INT3 : breakpoints[i].opcode );
182 /***********************************************************************
183 * DEBUG_FindBreakpoint
185 * Find the breakpoint for a given address. Return the breakpoint
186 * number or -1 if none.
188 int DEBUG_FindBreakpoint( const DBG_ADDR *addr )
192 for (i = 0; i < MAX_BREAKPOINTS; i++)
194 if (breakpoints[i].in_use && breakpoints[i].enabled &&
195 breakpoints[i].addr.seg == addr->seg &&
196 breakpoints[i].addr.off == addr->off) return i;
202 /***********************************************************************
203 * DEBUG_AddBreakpoint
207 void DEBUG_AddBreakpoint( const DBG_ADDR *address )
209 DBG_ADDR addr = *address;
213 DBG_FIX_ADDR_SEG( &addr, CS_reg(&DEBUG_context) );
215 if (next_bp < MAX_BREAKPOINTS)
217 else /* try to find an empty slot */
219 for (num = 1; num < MAX_BREAKPOINTS; num++)
220 if (!breakpoints[num].in_use) break;
221 if (num >= MAX_BREAKPOINTS)
223 fprintf( stderr, "Too many breakpoints. Please delete some.\n" );
227 if (!DBG_CHECK_READ_PTR( &addr, 1 )) return;
228 p = DBG_ADDR_TO_LIN( &addr );
229 breakpoints[num].addr = addr;
230 breakpoints[num].addrlen = !addr.seg ? 32 :
231 (GET_SEL_FLAGS(addr.seg) & LDT_FLAGS_32BIT) ? 32 : 16;
232 breakpoints[num].opcode = *p;
233 breakpoints[num].enabled = TRUE;
234 breakpoints[num].in_use = TRUE;
235 breakpoints[num].skipcount = 0;
236 fprintf( stderr, "Breakpoint %d at ", num );
237 DEBUG_PrintAddress( &breakpoints[num].addr, breakpoints[num].addrlen,
239 fprintf( stderr, "\n" );
243 /***********************************************************************
244 * DEBUG_DelBreakpoint
246 * Delete a breakpoint.
248 void DEBUG_DelBreakpoint( int num )
250 if ((num <= 0) || (num >= next_bp) || !breakpoints[num].in_use)
252 fprintf( stderr, "Invalid breakpoint number %d\n", num );
255 breakpoints[num].enabled = FALSE;
256 breakpoints[num].in_use = FALSE;
257 breakpoints[num].skipcount = 0;
261 /***********************************************************************
262 * DEBUG_EnableBreakpoint
264 * Enable or disable a break point.
266 void DEBUG_EnableBreakpoint( int num, BOOL32 enable )
268 if ((num <= 0) || (num >= next_bp) || !breakpoints[num].in_use)
270 fprintf( stderr, "Invalid breakpoint number %d\n", num );
273 breakpoints[num].enabled = enable;
274 breakpoints[num].skipcount = 0;
278 /***********************************************************************
279 * DEBUG_InfoBreakpoints
281 * Display break points information.
283 void DEBUG_InfoBreakpoints(void)
287 fprintf( stderr, "Breakpoints:\n" );
288 for (i = 1; i < next_bp; i++)
290 if (breakpoints[i].in_use)
292 fprintf( stderr, "%d: %c ", i, breakpoints[i].enabled ? 'y' : 'n');
293 DEBUG_PrintAddress( &breakpoints[i].addr, breakpoints[i].addrlen,
295 fprintf( stderr, "\n" );
296 if( breakpoints[i].condition != NULL )
298 fprintf(stderr, "\t\tstop when ");
299 DEBUG_DisplayExpr(breakpoints[i].condition);
300 fprintf(stderr, "\n");
307 /***********************************************************************
308 * DEBUG_ShouldContinue
310 * Determine if we should continue execution after a SIGTRAP signal when
311 * executing in the given mode.
313 BOOL32 DEBUG_ShouldContinue( enum exec_mode mode, int * count )
320 /* If not single-stepping, back up over the int3 instruction */
321 if (!(EFL_reg(&DEBUG_context) & STEP_FLAG)) EIP_reg(&DEBUG_context)--;
323 addr.seg = (CS_reg(&DEBUG_context) == WINE_CODE_SELECTOR) ?
324 0 : CS_reg(&DEBUG_context);
325 addr.off = EIP_reg(&DEBUG_context);
327 bpnum = DEBUG_FindBreakpoint( &addr );
328 breakpoints[0].enabled = 0; /* disable the step-over breakpoint */
330 if ((bpnum != 0) && (bpnum != -1))
332 if( breakpoints[bpnum].condition != NULL )
334 cond_addr = DEBUG_EvalExpr(breakpoints[bpnum].condition);
335 if( cond_addr.type == NULL )
338 * Something wrong - unable to evaluate this expression.
340 fprintf(stderr, "Unable to evaluate expression ");
341 DEBUG_DisplayExpr(breakpoints[bpnum].condition);
342 fprintf(stderr, "\nTurning off condition\n");
343 DEBUG_AddBPCondition(bpnum, NULL);
345 else if( ! DEBUG_GetExprValue( &cond_addr, NULL) )
351 if( breakpoints[bpnum].skipcount > 0 )
353 breakpoints[bpnum].skipcount--;
354 if( breakpoints[bpnum].skipcount > 0 )
359 fprintf( stderr, "Stopped on breakpoint %d at ", bpnum );
360 DEBUG_PrintAddress( &breakpoints[bpnum].addr,
361 breakpoints[bpnum].addrlen, TRUE );
362 fprintf( stderr, "\n" );
365 * See if there is a source file for this bp. If so,
366 * then dig it out and display one line.
368 DEBUG_FindNearestSymbol( &addr, TRUE, NULL, 0, &list);
369 if( list.sourcefile != NULL )
371 DEBUG_List(&list, NULL, 0);
377 * If our mode indicates that we are stepping line numbers,
378 * get the current function, and figure out if we are exactly
379 * on a line number or not.
381 if( mode == EXEC_STEP_OVER
382 || mode == EXEC_STEP_INSTR )
384 if( DEBUG_CheckLinenoStatus(&addr) == AT_LINENUMBER )
389 else if( mode == EXEC_STEPI_OVER
390 || mode == EXEC_STEPI_INSTR )
396 if( *count > 0 || mode == EXEC_FINISH )
399 * We still need to execute more instructions.
405 * If we are about to stop, then print out the source line if we
408 if( (mode != EXEC_CONT && mode != EXEC_FINISH) )
410 DEBUG_FindNearestSymbol( &addr, TRUE, NULL, 0, &list);
411 if( list.sourcefile != NULL )
413 DEBUG_List(&list, NULL, 0);
417 /* If there's no breakpoint and we are not single-stepping, then we */
418 /* must have encountered an int3 in the Windows program; let's skip it. */
419 if ((bpnum == -1) && !(EFL_reg(&DEBUG_context) & STEP_FLAG))
420 EIP_reg(&DEBUG_context)++;
422 /* no breakpoint, continue if in continuous mode */
423 return (mode == EXEC_CONT || mode == EXEC_FINISH);
427 /***********************************************************************
428 * DEBUG_RestartExecution
430 * Set the breakpoints to the correct state to restart execution
433 enum exec_mode DEBUG_RestartExecution( enum exec_mode mode, int count )
440 unsigned int * value;
441 enum exec_mode ret_mode;
444 addr.seg = (CS_reg(&DEBUG_context) == WINE_CODE_SELECTOR) ?
445 0 : CS_reg(&DEBUG_context);
446 addr.off = EIP_reg(&DEBUG_context);
449 * This is the mode we will be running in after we finish. We would like
450 * to be able to modify this in certain cases.
454 bp = DEBUG_FindBreakpoint( &addr );
455 if ( bp != -1 && bp != 0)
458 * If we have set a new value, then save it in the BP number.
460 if( count != 0 && mode == EXEC_CONT )
462 breakpoints[bp].skipcount = count;
464 mode = EXEC_STEPI_INSTR; /* If there's a breakpoint, skip it */
468 if( mode == EXEC_CONT && count > 1 )
470 fprintf(stderr,"Not stopped at any breakpoint; argument ignored.\n");
474 if( mode == EXEC_FINISH && DEBUG_IsFctReturn() )
476 mode = ret_mode = EXEC_STEPI_INSTR;
479 instr = (BYTE *)PTR_SEG_OFF_TO_LIN( CS_reg(&DEBUG_context),
480 EIP_reg(&DEBUG_context) );
482 * See if the function we are stepping into has debug info
483 * and line numbers. If not, then we step over it instead.
484 * FIXME - we need to check for things like thunks or trampolines,
485 * as the actual function may in fact have debug info.
489 delta = *(unsigned int*) (instr + 1);
491 DEBUG_Disasm(&addr2, FALSE);
494 status = DEBUG_CheckLinenoStatus(&addr2);
496 * Anytime we have a trampoline, step over it.
498 if( ((mode == EXEC_STEP_OVER) || (mode == EXEC_STEPI_OVER))
499 && status == FUNC_IS_TRAMPOLINE )
502 fprintf(stderr, "Not stepping into trampoline at %x (no lines)\n",
505 mode = EXEC_STEP_OVER_TRAMPOLINE;
508 if( mode == EXEC_STEP_INSTR && status == FUNC_HAS_NO_LINES )
511 fprintf(stderr, "Not stepping into function at %x (no lines)\n",
514 mode = EXEC_STEP_OVER;
519 if( mode == EXEC_STEP_INSTR )
521 if( DEBUG_CheckLinenoStatus(&addr) == FUNC_HAS_NO_LINES )
523 fprintf(stderr, "Single stepping until exit from function, \n");
524 fprintf(stderr, "which has no line number information.\n");
526 ret_mode = mode = EXEC_FINISH;
532 case EXEC_CONT: /* Continuous execution */
533 EFL_reg(&DEBUG_context) &= ~STEP_FLAG;
534 DEBUG_SetBreakpoints( TRUE );
537 case EXEC_STEP_OVER_TRAMPOLINE:
539 * This is the means by which we step over our conversion stubs
540 * in callfrom*.s and callto*.s. We dig the appropriate address
541 * off the stack, and we set the breakpoint there instead of the
542 * address just after the call.
544 value = (unsigned int *) ESP_reg(&DEBUG_context) + 2;
546 EFL_reg(&DEBUG_context) &= ~STEP_FLAG;
547 breakpoints[0].addr = addr;
548 breakpoints[0].enabled = TRUE;
549 breakpoints[0].in_use = TRUE;
550 breakpoints[0].skipcount = 0;
551 breakpoints[0].opcode = *(BYTE *)DBG_ADDR_TO_LIN( &addr );
552 DEBUG_SetBreakpoints( TRUE );
556 case EXEC_STEPI_OVER: /* Stepping over a call */
557 case EXEC_STEP_OVER: /* Stepping over a call */
558 if (DEBUG_IsStepOverInstr())
560 EFL_reg(&DEBUG_context) &= ~STEP_FLAG;
561 DEBUG_Disasm(&addr, FALSE);
562 breakpoints[0].addr = addr;
563 breakpoints[0].enabled = TRUE;
564 breakpoints[0].in_use = TRUE;
565 breakpoints[0].skipcount = 0;
566 breakpoints[0].opcode = *(BYTE *)DBG_ADDR_TO_LIN( &addr );
567 DEBUG_SetBreakpoints( TRUE );
570 /* else fall through to single-stepping */
572 case EXEC_STEP_INSTR: /* Single-stepping an instruction */
573 case EXEC_STEPI_INSTR: /* Single-stepping an instruction */
574 EFL_reg(&DEBUG_context) |= STEP_FLAG;
581 DEBUG_AddBPCondition(int num, struct expr * exp)
583 if ((num <= 0) || (num >= next_bp) || !breakpoints[num].in_use)
585 fprintf( stderr, "Invalid breakpoint number %d\n", num );
589 if( breakpoints[num].condition != NULL )
591 DEBUG_FreeExpr(breakpoints[num].condition);
592 breakpoints[num].condition = NULL;
597 breakpoints[num].condition = DEBUG_CloneExpr(exp);