2 * Debugger break-points handling
4 * Copyright 1994 Martin von Loewis
5 * Copyright 1995 Alexandre Julliard
11 #include <sys/types.h>
12 #ifdef HAVE_SYS_MMAN_H
15 #include "wine/winbase16.h"
25 #define INT3 0xcc /* int 3 opcode */
27 #define MAX_BREAKPOINTS 100
37 struct expr
* condition
;
40 static BREAKPOINT breakpoints
[MAX_BREAKPOINTS
];
42 static int next_bp
= 1; /* breakpoint 0 is reserved for step-over */
45 /***********************************************************************
48 * Change the opcode at segment:addr.
50 static void DEBUG_SetOpcode( const DBG_ADDR
*addr
, BYTE op
)
52 BYTE
*ptr
= DBG_ADDR_TO_LIN(addr
);
54 /* There are a couple of problems with this. On Linux prior to
55 1.1.62, this call fails (ENOACCESS) due to a bug in fs/exec.c.
56 This code is currently not tested at all on BSD.
57 How do I get the old protection in order to restore it later on?
59 if (mprotect((caddr_t
)((int)ptr
& (~4095)), 4096,
60 PROT_READ
| PROT_WRITE
| PROT_EXEC
) == -1)
62 perror( "Can't set break point" );
66 /* mprotect((caddr_t)(addr->off & ~4095), 4096,
67 PROT_READ | PROT_EXEC ); */
71 /***********************************************************************
72 * DEBUG_IsStepOverInstr
74 * Determine if the instruction at CS:EIP is an instruction that
75 * we need to step over (like a call or a repetitive string move).
77 static BOOL
DEBUG_IsStepOverInstr()
79 BYTE
*instr
= (BYTE
*)CTX_SEG_OFF_TO_LIN( &DEBUG_context
,
80 CS_reg(&DEBUG_context
),
81 EIP_reg(&DEBUG_context
) );
87 /* Skip all prefixes */
95 case 0x66: /* opcode size prefix */
96 case 0x67: /* addr size prefix */
98 case 0xf2: /* repne */
103 /* Handle call instructions */
105 case 0xcd: /* int <intno> */
106 case 0xe8: /* call <offset> */
107 case 0x9a: /* lcall <seg>:<off> */
110 case 0xff: /* call <regmodrm> */
111 return (((instr
[1] & 0x38) == 0x10) ||
112 ((instr
[1] & 0x38) == 0x18));
114 /* Handle string instructions */
116 case 0x6c: /* insb */
117 case 0x6d: /* insw */
118 case 0x6e: /* outsb */
119 case 0x6f: /* outsw */
120 case 0xa4: /* movsb */
121 case 0xa5: /* movsw */
122 case 0xa6: /* cmpsb */
123 case 0xa7: /* cmpsw */
124 case 0xaa: /* stosb */
125 case 0xab: /* stosw */
126 case 0xac: /* lodsb */
127 case 0xad: /* lodsw */
128 case 0xae: /* scasb */
129 case 0xaf: /* scasw */
139 /***********************************************************************
142 * Determine if the instruction at CS:EIP is an instruction that
143 * is a function return.
145 BOOL
DEBUG_IsFctReturn(void)
147 BYTE
*instr
= (BYTE
*)CTX_SEG_OFF_TO_LIN( &DEBUG_context
,
148 CS_reg(&DEBUG_context
),
149 EIP_reg(&DEBUG_context
) );
165 /***********************************************************************
166 * DEBUG_SetBreakpoints
168 * Set or remove all the breakpoints.
170 void DEBUG_SetBreakpoints( BOOL set
)
174 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
176 if (breakpoints
[i
].in_use
&& breakpoints
[i
].enabled
)
178 /* Note: we check for read here, because if reading is allowed */
179 /* writing permission will be forced in DEBUG_SetOpcode. */
180 if (DEBUG_IsBadReadPtr( &breakpoints
[i
].addr
, 1 ))
182 fprintf( stderr
, "Invalid address for breakpoint %d, disabling it\n", i
);
183 breakpoints
[i
].enabled
= FALSE
;
185 else DEBUG_SetOpcode( &breakpoints
[i
].addr
,
186 set
? INT3
: breakpoints
[i
].opcode
);
192 /***********************************************************************
193 * DEBUG_FindBreakpoint
195 * Find the breakpoint for a given address. Return the breakpoint
196 * number or -1 if none.
198 int DEBUG_FindBreakpoint( const DBG_ADDR
*addr
)
202 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
204 if (breakpoints
[i
].in_use
&& breakpoints
[i
].enabled
&&
205 breakpoints
[i
].addr
.seg
== addr
->seg
&&
206 breakpoints
[i
].addr
.off
== addr
->off
) return i
;
212 /***********************************************************************
213 * DEBUG_AddBreakpoint
217 void DEBUG_AddBreakpoint( const DBG_ADDR
*address
)
219 DBG_ADDR addr
= *address
;
224 DBG_FIX_ADDR_SEG( &addr
, CS_reg(&DEBUG_context
) );
226 if( addr
.type
!= NULL
&& addr
.type
== DEBUG_TypeIntConst
)
229 * We know that we have the actual offset stored somewhere
230 * else in 32-bit space. Grab it, and we
235 addr
.off
= DEBUG_GetExprValue(&addr
, NULL
);
238 if (!DBG_CHECK_READ_PTR( &addr
, 1 )) return;
240 if (next_bp
< MAX_BREAKPOINTS
)
242 else /* try to find an empty slot */
244 for (num
= 1; num
< MAX_BREAKPOINTS
; num
++)
245 if (!breakpoints
[num
].in_use
) break;
246 if (num
>= MAX_BREAKPOINTS
)
248 fprintf( stderr
, "Too many breakpoints. Please delete some.\n" );
252 p
= DBG_ADDR_TO_LIN( &addr
);
253 breakpoints
[num
].addr
= addr
;
254 breakpoints
[num
].addrlen
= !addr
.seg
? 32 :
255 (GET_SEL_FLAGS(addr
.seg
) & LDT_FLAGS_32BIT
) ? 32 : 16;
256 breakpoints
[num
].opcode
= *p
;
257 breakpoints
[num
].enabled
= TRUE
;
258 breakpoints
[num
].in_use
= TRUE
;
259 breakpoints
[num
].skipcount
= 0;
260 fprintf( stderr
, "Breakpoint %d at ", num
);
261 DEBUG_PrintAddress( &breakpoints
[num
].addr
, breakpoints
[num
].addrlen
,
263 fprintf( stderr
, "\n" );
267 /***********************************************************************
268 * DEBUG_DelBreakpoint
270 * Delete a breakpoint.
272 void DEBUG_DelBreakpoint( int num
)
274 if ((num
<= 0) || (num
>= next_bp
) || !breakpoints
[num
].in_use
)
276 fprintf( stderr
, "Invalid breakpoint number %d\n", num
);
280 if( breakpoints
[num
].condition
!= NULL
)
282 DEBUG_FreeExpr(breakpoints
[num
].condition
);
283 breakpoints
[num
].condition
= NULL
;
286 breakpoints
[num
].enabled
= FALSE
;
287 breakpoints
[num
].in_use
= FALSE
;
288 breakpoints
[num
].skipcount
= 0;
292 /***********************************************************************
293 * DEBUG_EnableBreakpoint
295 * Enable or disable a break point.
297 void DEBUG_EnableBreakpoint( int num
, BOOL enable
)
299 if ((num
<= 0) || (num
>= next_bp
) || !breakpoints
[num
].in_use
)
301 fprintf( stderr
, "Invalid breakpoint number %d\n", num
);
304 breakpoints
[num
].enabled
= enable
;
305 breakpoints
[num
].skipcount
= 0;
309 /***********************************************************************
310 * DEBUG_InfoBreakpoints
312 * Display break points information.
314 void DEBUG_InfoBreakpoints(void)
318 fprintf( stderr
, "Breakpoints:\n" );
319 for (i
= 1; i
< next_bp
; i
++)
321 if (breakpoints
[i
].in_use
)
323 fprintf( stderr
, "%d: %c ", i
, breakpoints
[i
].enabled
? 'y' : 'n');
324 DEBUG_PrintAddress( &breakpoints
[i
].addr
, breakpoints
[i
].addrlen
,
326 fprintf( stderr
, "\n" );
327 if( breakpoints
[i
].condition
!= NULL
)
329 fprintf(stderr
, "\t\tstop when ");
330 DEBUG_DisplayExpr(breakpoints
[i
].condition
);
331 fprintf(stderr
, "\n");
338 /***********************************************************************
339 * DEBUG_AddTaskEntryBreakpoint
341 * Add a breakpoint at the entry point of the given task
343 void DEBUG_AddTaskEntryBreakpoint( HTASK16 hTask
)
345 TDB
*pTask
= (TDB
*)GlobalLock16( hTask
);
347 DBG_ADDR addr
= { NULL
, 0, 0 };
351 if (!(pModule
= NE_GetPtr( pTask
->hModule
))) return;
352 if (pModule
->flags
& NE_FFLAGS_LIBMODULE
) return; /* Library */
354 if (pModule
->lpDosTask
) { /* DOS module */
355 addr
.seg
= pModule
->lpDosTask
->init_cs
| ((DWORD
)pModule
->self
<< 16);
356 addr
.off
= pModule
->lpDosTask
->init_ip
;
357 fprintf( stderr
, "DOS task '%s': ", NE_MODULE_NAME( pModule
) );
358 DEBUG_AddBreakpoint( &addr
);
360 if (!(pModule
->flags
& NE_FFLAGS_WIN32
)) /* NE module */
363 GlobalHandleToSel16(NE_SEG_TABLE(pModule
)[pModule
->cs
-1].hSeg
);
364 addr
.off
= pModule
->ip
;
365 fprintf( stderr
, "Win16 task '%s': ", NE_MODULE_NAME( pModule
) );
366 DEBUG_AddBreakpoint( &addr
);
371 addr
.off
= (DWORD
)RVA_PTR( pModule
->module32
,
372 OptionalHeader
.AddressOfEntryPoint
);
373 fprintf( stderr
, "Win32 task '%s': ", NE_MODULE_NAME( pModule
) );
374 DEBUG_AddBreakpoint( &addr
);
378 DEBUG_SetBreakpoints( TRUE
); /* Setup breakpoints */
382 /***********************************************************************
383 * DEBUG_ShouldContinue
385 * Determine if we should continue execution after a SIGTRAP signal when
386 * executing in the given mode.
388 BOOL
DEBUG_ShouldContinue( enum exec_mode mode
, int * count
)
394 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
396 /* If not single-stepping, back up over the int3 instruction */
397 if (!(EFL_reg(&DEBUG_context
) & STEP_FLAG
)) EIP_reg(&DEBUG_context
)--;
399 addr
.seg
= CS_reg(&DEBUG_context
);
400 addr
.off
= EIP_reg(&DEBUG_context
);
401 if (ISV86(&DEBUG_context
)) addr
.seg
|= (DWORD
)(pTask
?(pTask
->hModule
):0)<<16; else
402 if (IS_SELECTOR_SYSTEM(addr
.seg
)) addr
.seg
= 0;
404 GlobalUnlock16( GetCurrentTask() );
406 bpnum
= DEBUG_FindBreakpoint( &addr
);
407 breakpoints
[0].enabled
= 0; /* disable the step-over breakpoint */
409 if ((bpnum
!= 0) && (bpnum
!= -1))
411 if( breakpoints
[bpnum
].condition
!= NULL
)
413 cond_addr
= DEBUG_EvalExpr(breakpoints
[bpnum
].condition
);
414 if( cond_addr
.type
== NULL
)
417 * Something wrong - unable to evaluate this expression.
419 fprintf(stderr
, "Unable to evaluate expression ");
420 DEBUG_DisplayExpr(breakpoints
[bpnum
].condition
);
421 fprintf(stderr
, "\nTurning off condition\n");
422 DEBUG_AddBPCondition(bpnum
, NULL
);
424 else if( ! DEBUG_GetExprValue( &cond_addr
, NULL
) )
430 if( breakpoints
[bpnum
].skipcount
> 0 )
432 breakpoints
[bpnum
].skipcount
--;
433 if( breakpoints
[bpnum
].skipcount
> 0 )
438 fprintf( stderr
, "Stopped on breakpoint %d at ", bpnum
);
439 DEBUG_PrintAddress( &breakpoints
[bpnum
].addr
,
440 breakpoints
[bpnum
].addrlen
, TRUE
);
441 fprintf( stderr
, "\n" );
444 * See if there is a source file for this bp. If so,
445 * then dig it out and display one line.
447 DEBUG_FindNearestSymbol( &addr
, TRUE
, NULL
, 0, &list
);
448 if( list
.sourcefile
!= NULL
)
450 DEBUG_List(&list
, NULL
, 0);
456 * If our mode indicates that we are stepping line numbers,
457 * get the current function, and figure out if we are exactly
458 * on a line number or not.
460 if( mode
== EXEC_STEP_OVER
461 || mode
== EXEC_STEP_INSTR
)
463 if( DEBUG_CheckLinenoStatus(&addr
) == AT_LINENUMBER
)
468 else if( mode
== EXEC_STEPI_OVER
469 || mode
== EXEC_STEPI_INSTR
)
475 if( *count
> 0 || mode
== EXEC_FINISH
)
478 * We still need to execute more instructions.
484 * If we are about to stop, then print out the source line if we
487 if ((mode
!= EXEC_CONT
&& mode
!= EXEC_PASS
&& mode
!= EXEC_FINISH
))
489 DEBUG_FindNearestSymbol( &addr
, TRUE
, NULL
, 0, &list
);
490 if( list
.sourcefile
!= NULL
)
492 DEBUG_List(&list
, NULL
, 0);
496 /* If there's no breakpoint and we are not single-stepping, then we */
497 /* must have encountered an int3 in the Windows program; let's skip it. */
498 if ((bpnum
== -1) && !(EFL_reg(&DEBUG_context
) & STEP_FLAG
))
499 EIP_reg(&DEBUG_context
)++;
501 /* no breakpoint, continue if in continuous mode */
502 return (mode
== EXEC_CONT
|| mode
== EXEC_PASS
|| mode
== EXEC_FINISH
);
506 /***********************************************************************
507 * DEBUG_RestartExecution
509 * Set the breakpoints to the correct state to restart execution
512 enum exec_mode
DEBUG_RestartExecution( enum exec_mode mode
, int count
)
519 unsigned int * value
;
520 enum exec_mode ret_mode
;
522 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
524 addr
.seg
= CS_reg(&DEBUG_context
);
525 addr
.off
= EIP_reg(&DEBUG_context
);
526 if (ISV86(&DEBUG_context
)) addr
.seg
|= (DWORD
)(pTask
?(pTask
->hModule
):0)<<16; else
527 if (IS_SELECTOR_SYSTEM(addr
.seg
)) addr
.seg
= 0;
529 GlobalUnlock16( GetCurrentTask() );
532 * This is the mode we will be running in after we finish. We would like
533 * to be able to modify this in certain cases.
537 bp
= DEBUG_FindBreakpoint( &addr
);
538 if ( bp
!= -1 && bp
!= 0)
541 * If we have set a new value, then save it in the BP number.
543 if( count
!= 0 && mode
== EXEC_CONT
)
545 breakpoints
[bp
].skipcount
= count
;
547 mode
= EXEC_STEPI_INSTR
; /* If there's a breakpoint, skip it */
551 if( mode
== EXEC_CONT
&& count
> 1 )
553 fprintf(stderr
,"Not stopped at any breakpoint; argument ignored.\n");
557 if( mode
== EXEC_FINISH
&& DEBUG_IsFctReturn() )
559 mode
= ret_mode
= EXEC_STEPI_INSTR
;
562 instr
= (BYTE
*)CTX_SEG_OFF_TO_LIN( &DEBUG_context
,
563 CS_reg(&DEBUG_context
),
564 EIP_reg(&DEBUG_context
) );
566 * See if the function we are stepping into has debug info
567 * and line numbers. If not, then we step over it instead.
568 * FIXME - we need to check for things like thunks or trampolines,
569 * as the actual function may in fact have debug info.
573 delta
= *(unsigned int*) (instr
+ 1);
575 DEBUG_Disasm(&addr2
, FALSE
);
578 status
= DEBUG_CheckLinenoStatus(&addr2
);
580 * Anytime we have a trampoline, step over it.
582 if( ((mode
== EXEC_STEP_OVER
) || (mode
== EXEC_STEPI_OVER
))
583 && status
== FUNC_IS_TRAMPOLINE
)
586 fprintf(stderr
, "Not stepping into trampoline at %x (no lines)\n",
589 mode
= EXEC_STEP_OVER_TRAMPOLINE
;
592 if( mode
== EXEC_STEP_INSTR
&& status
== FUNC_HAS_NO_LINES
)
595 fprintf(stderr
, "Not stepping into function at %x (no lines)\n",
598 mode
= EXEC_STEP_OVER
;
603 if( mode
== EXEC_STEP_INSTR
)
605 if( DEBUG_CheckLinenoStatus(&addr
) == FUNC_HAS_NO_LINES
)
607 fprintf(stderr
, "Single stepping until exit from function, \n");
608 fprintf(stderr
, "which has no line number information.\n");
610 ret_mode
= mode
= EXEC_FINISH
;
616 case EXEC_CONT
: /* Continuous execution */
617 case EXEC_PASS
: /* Continue, passing exception */
618 EFL_reg(&DEBUG_context
) &= ~STEP_FLAG
;
619 DEBUG_SetBreakpoints( TRUE
);
622 case EXEC_STEP_OVER_TRAMPOLINE
:
624 * This is the means by which we step over our conversion stubs
625 * in callfrom*.s and callto*.s. We dig the appropriate address
626 * off the stack, and we set the breakpoint there instead of the
627 * address just after the call.
629 value
= (unsigned int *) ESP_reg(&DEBUG_context
) + 2;
631 EFL_reg(&DEBUG_context
) &= ~STEP_FLAG
;
632 breakpoints
[0].addr
= addr
;
633 breakpoints
[0].enabled
= TRUE
;
634 breakpoints
[0].in_use
= TRUE
;
635 breakpoints
[0].skipcount
= 0;
636 breakpoints
[0].opcode
= *(BYTE
*)DBG_ADDR_TO_LIN( &addr
);
637 DEBUG_SetBreakpoints( TRUE
);
641 case EXEC_STEPI_OVER
: /* Stepping over a call */
642 case EXEC_STEP_OVER
: /* Stepping over a call */
643 if (DEBUG_IsStepOverInstr())
645 EFL_reg(&DEBUG_context
) &= ~STEP_FLAG
;
646 DEBUG_Disasm(&addr
, FALSE
);
647 breakpoints
[0].addr
= addr
;
648 breakpoints
[0].enabled
= TRUE
;
649 breakpoints
[0].in_use
= TRUE
;
650 breakpoints
[0].skipcount
= 0;
651 breakpoints
[0].opcode
= *(BYTE
*)DBG_ADDR_TO_LIN( &addr
);
652 DEBUG_SetBreakpoints( TRUE
);
655 /* else fall through to single-stepping */
657 case EXEC_STEP_INSTR
: /* Single-stepping an instruction */
658 case EXEC_STEPI_INSTR
: /* Single-stepping an instruction */
659 EFL_reg(&DEBUG_context
) |= STEP_FLAG
;
666 DEBUG_AddBPCondition(int num
, struct expr
* exp
)
668 if ((num
<= 0) || (num
>= next_bp
) || !breakpoints
[num
].in_use
)
670 fprintf( stderr
, "Invalid breakpoint number %d\n", num
);
674 if( breakpoints
[num
].condition
!= NULL
)
676 DEBUG_FreeExpr(breakpoints
[num
].condition
);
677 breakpoints
[num
].condition
= NULL
;
682 breakpoints
[num
].condition
= DEBUG_CloneExpr(exp
);