2 * Debugger break-points handling
4 * Copyright 1994 Martin von Loewis
5 * Copyright 1995 Alexandre Julliard
10 #include <sys/types.h>
21 #define INT3 0xcc /* int 3 opcode */
23 #define MAX_BREAKPOINTS 100
33 struct expr
* condition
;
36 static BREAKPOINT breakpoints
[MAX_BREAKPOINTS
];
38 static int next_bp
= 1; /* breakpoint 0 is reserved for step-over */
41 /***********************************************************************
44 * Change the opcode at segment:addr.
46 static void DEBUG_SetOpcode( const DBG_ADDR
*addr
, BYTE op
)
48 BYTE
*ptr
= DBG_ADDR_TO_LIN(addr
);
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" );
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
*)CTX_SEG_OFF_TO_LIN( &DEBUG_context
,
76 CS_reg(&DEBUG_context
),
77 EIP_reg(&DEBUG_context
) );
83 /* Skip all prefixes */
91 case 0x66: /* opcode size prefix */
92 case 0x67: /* addr size prefix */
94 case 0xf2: /* repne */
99 /* Handle call instructions */
101 case 0xcd: /* int <intno> */
102 case 0xe8: /* call <offset> */
103 case 0x9a: /* lcall <seg>:<off> */
106 case 0xff: /* call <regmodrm> */
107 return (((instr
[1] & 0x38) == 0x10) ||
108 ((instr
[1] & 0x38) == 0x18));
110 /* Handle string instructions */
112 case 0x6c: /* insb */
113 case 0x6d: /* insw */
114 case 0x6e: /* outsb */
115 case 0x6f: /* outsw */
116 case 0xa4: /* movsb */
117 case 0xa5: /* movsw */
118 case 0xa6: /* cmpsb */
119 case 0xa7: /* cmpsw */
120 case 0xaa: /* stosb */
121 case 0xab: /* stosw */
122 case 0xac: /* lodsb */
123 case 0xad: /* lodsw */
124 case 0xae: /* scasb */
125 case 0xaf: /* scasw */
135 /***********************************************************************
138 * Determine if the instruction at CS:EIP is an instruction that
139 * is a function return.
141 BOOL32
DEBUG_IsFctReturn(void)
143 BYTE
*instr
= (BYTE
*)CTX_SEG_OFF_TO_LIN( &DEBUG_context
,
144 CS_reg(&DEBUG_context
),
145 EIP_reg(&DEBUG_context
) );
161 /***********************************************************************
162 * DEBUG_SetBreakpoints
164 * Set or remove all the breakpoints.
166 void DEBUG_SetBreakpoints( BOOL32 set
)
170 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
172 if (breakpoints
[i
].in_use
&& breakpoints
[i
].enabled
)
174 /* Note: we check for read here, because if reading is allowed */
175 /* writing permission will be forced in DEBUG_SetOpcode. */
176 if (DEBUG_IsBadReadPtr( &breakpoints
[i
].addr
, 1 ))
178 fprintf( stderr
, "Invalid address for breakpoint %d, disabling it\n", i
);
179 breakpoints
[i
].enabled
= FALSE
;
181 else DEBUG_SetOpcode( &breakpoints
[i
].addr
,
182 set
? INT3
: breakpoints
[i
].opcode
);
188 /***********************************************************************
189 * DEBUG_FindBreakpoint
191 * Find the breakpoint for a given address. Return the breakpoint
192 * number or -1 if none.
194 int DEBUG_FindBreakpoint( const DBG_ADDR
*addr
)
198 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
200 if (breakpoints
[i
].in_use
&& breakpoints
[i
].enabled
&&
201 breakpoints
[i
].addr
.seg
== addr
->seg
&&
202 breakpoints
[i
].addr
.off
== addr
->off
) return i
;
208 /***********************************************************************
209 * DEBUG_AddBreakpoint
213 void DEBUG_AddBreakpoint( const DBG_ADDR
*address
)
215 DBG_ADDR addr
= *address
;
220 DBG_FIX_ADDR_SEG( &addr
, CS_reg(&DEBUG_context
) );
222 if( addr
.type
!= NULL
&& addr
.type
== DEBUG_TypeIntConst
)
225 * We know that we have the actual offset stored somewhere
226 * else in 32-bit space. Grab it, and we
231 addr
.off
= DEBUG_GetExprValue(&addr
, NULL
);
234 if (!DBG_CHECK_READ_PTR( &addr
, 1 )) return;
236 if (next_bp
< MAX_BREAKPOINTS
)
238 else /* try to find an empty slot */
240 for (num
= 1; num
< MAX_BREAKPOINTS
; num
++)
241 if (!breakpoints
[num
].in_use
) break;
242 if (num
>= MAX_BREAKPOINTS
)
244 fprintf( stderr
, "Too many breakpoints. Please delete some.\n" );
248 p
= DBG_ADDR_TO_LIN( &addr
);
249 breakpoints
[num
].addr
= addr
;
250 breakpoints
[num
].addrlen
= !addr
.seg
? 32 :
251 (GET_SEL_FLAGS(addr
.seg
) & LDT_FLAGS_32BIT
) ? 32 : 16;
252 breakpoints
[num
].opcode
= *p
;
253 breakpoints
[num
].enabled
= TRUE
;
254 breakpoints
[num
].in_use
= TRUE
;
255 breakpoints
[num
].skipcount
= 0;
256 fprintf( stderr
, "Breakpoint %d at ", num
);
257 DEBUG_PrintAddress( &breakpoints
[num
].addr
, breakpoints
[num
].addrlen
,
259 fprintf( stderr
, "\n" );
263 /***********************************************************************
264 * DEBUG_DelBreakpoint
266 * Delete a breakpoint.
268 void DEBUG_DelBreakpoint( int num
)
270 if ((num
<= 0) || (num
>= next_bp
) || !breakpoints
[num
].in_use
)
272 fprintf( stderr
, "Invalid breakpoint number %d\n", num
);
276 if( breakpoints
[num
].condition
!= NULL
)
278 DEBUG_FreeExpr(breakpoints
[num
].condition
);
279 breakpoints
[num
].condition
= NULL
;
282 breakpoints
[num
].enabled
= FALSE
;
283 breakpoints
[num
].in_use
= FALSE
;
284 breakpoints
[num
].skipcount
= 0;
288 /***********************************************************************
289 * DEBUG_EnableBreakpoint
291 * Enable or disable a break point.
293 void DEBUG_EnableBreakpoint( int num
, BOOL32 enable
)
295 if ((num
<= 0) || (num
>= next_bp
) || !breakpoints
[num
].in_use
)
297 fprintf( stderr
, "Invalid breakpoint number %d\n", num
);
300 breakpoints
[num
].enabled
= enable
;
301 breakpoints
[num
].skipcount
= 0;
305 /***********************************************************************
306 * DEBUG_InfoBreakpoints
308 * Display break points information.
310 void DEBUG_InfoBreakpoints(void)
314 fprintf( stderr
, "Breakpoints:\n" );
315 for (i
= 1; i
< next_bp
; i
++)
317 if (breakpoints
[i
].in_use
)
319 fprintf( stderr
, "%d: %c ", i
, breakpoints
[i
].enabled
? 'y' : 'n');
320 DEBUG_PrintAddress( &breakpoints
[i
].addr
, breakpoints
[i
].addrlen
,
322 fprintf( stderr
, "\n" );
323 if( breakpoints
[i
].condition
!= NULL
)
325 fprintf(stderr
, "\t\tstop when ");
326 DEBUG_DisplayExpr(breakpoints
[i
].condition
);
327 fprintf(stderr
, "\n");
334 /***********************************************************************
335 * DEBUG_AddModuleBreakpoints
337 * Add a breakpoint at the start of every loaded module.
339 void DEBUG_AddModuleBreakpoints(void)
344 DBG_ADDR addr
= { NULL
, 0, 0 };
347 for (ok
= ModuleFirst(&entry
); ok
; ok
= ModuleNext(&entry
))
349 if (!(pModule
= NE_GetPtr( entry
.hModule
))) continue;
350 if (pModule
->flags
& NE_FFLAGS_LIBMODULE
) continue; /* Library */
352 if (pModule
->lpDosTask
) { /* DOS module */
353 addr
.seg
= pModule
->lpDosTask
->init_cs
| ((DWORD
)pModule
->self
<< 16);
354 addr
.off
= pModule
->lpDosTask
->init_ip
;
355 fprintf( stderr
, "DOS task '%s': ", entry
.szModule
);
356 DEBUG_AddBreakpoint( &addr
);
358 if (!(pModule
->flags
& NE_FFLAGS_WIN32
)) /* NE module */
361 GlobalHandleToSel(NE_SEG_TABLE(pModule
)[pModule
->cs
-1].hSeg
);
362 addr
.off
= pModule
->ip
;
363 fprintf( stderr
, "Win16 task '%s': ", entry
.szModule
);
364 DEBUG_AddBreakpoint( &addr
);
369 if (!(wm
= PROCESS_Current()->modref_list
))
372 addr
.off
= (DWORD
)RVA_PTR( pModule
->module32
,
373 OptionalHeader
.AddressOfEntryPoint
);
379 if (wm
->module
== pModule
->module32
) break;
384 addr
.off
= (DWORD
)RVA_PTR( wm
->module
,
385 OptionalHeader
.AddressOfEntryPoint
);
387 fprintf( stderr
, "Win32 task '%s': ", entry
.szModule
);
388 DEBUG_AddBreakpoint( &addr
);
392 DEBUG_SetBreakpoints( TRUE
); /* Setup breakpoints */
396 /***********************************************************************
397 * DEBUG_ShouldContinue
399 * Determine if we should continue execution after a SIGTRAP signal when
400 * executing in the given mode.
402 BOOL32
DEBUG_ShouldContinue( enum exec_mode mode
, int * count
)
408 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
410 /* If not single-stepping, back up over the int3 instruction */
411 if (!(EFL_reg(&DEBUG_context
) & STEP_FLAG
)) EIP_reg(&DEBUG_context
)--;
413 addr
.seg
= CS_reg(&DEBUG_context
);
414 addr
.off
= EIP_reg(&DEBUG_context
);
415 if (ISV86(&DEBUG_context
)) addr
.seg
|= (DWORD
)(pTask
?(pTask
->hModule
):0)<<16; else
416 if (IS_SELECTOR_SYSTEM(addr
.seg
)) addr
.seg
= 0;
418 GlobalUnlock16( GetCurrentTask() );
420 bpnum
= DEBUG_FindBreakpoint( &addr
);
421 breakpoints
[0].enabled
= 0; /* disable the step-over breakpoint */
423 if ((bpnum
!= 0) && (bpnum
!= -1))
425 if( breakpoints
[bpnum
].condition
!= NULL
)
427 cond_addr
= DEBUG_EvalExpr(breakpoints
[bpnum
].condition
);
428 if( cond_addr
.type
== NULL
)
431 * Something wrong - unable to evaluate this expression.
433 fprintf(stderr
, "Unable to evaluate expression ");
434 DEBUG_DisplayExpr(breakpoints
[bpnum
].condition
);
435 fprintf(stderr
, "\nTurning off condition\n");
436 DEBUG_AddBPCondition(bpnum
, NULL
);
438 else if( ! DEBUG_GetExprValue( &cond_addr
, NULL
) )
444 if( breakpoints
[bpnum
].skipcount
> 0 )
446 breakpoints
[bpnum
].skipcount
--;
447 if( breakpoints
[bpnum
].skipcount
> 0 )
452 fprintf( stderr
, "Stopped on breakpoint %d at ", bpnum
);
453 DEBUG_PrintAddress( &breakpoints
[bpnum
].addr
,
454 breakpoints
[bpnum
].addrlen
, TRUE
);
455 fprintf( stderr
, "\n" );
458 * See if there is a source file for this bp. If so,
459 * then dig it out and display one line.
461 DEBUG_FindNearestSymbol( &addr
, TRUE
, NULL
, 0, &list
);
462 if( list
.sourcefile
!= NULL
)
464 DEBUG_List(&list
, NULL
, 0);
470 * If our mode indicates that we are stepping line numbers,
471 * get the current function, and figure out if we are exactly
472 * on a line number or not.
474 if( mode
== EXEC_STEP_OVER
475 || mode
== EXEC_STEP_INSTR
)
477 if( DEBUG_CheckLinenoStatus(&addr
) == AT_LINENUMBER
)
482 else if( mode
== EXEC_STEPI_OVER
483 || mode
== EXEC_STEPI_INSTR
)
489 if( *count
> 0 || mode
== EXEC_FINISH
)
492 * We still need to execute more instructions.
498 * If we are about to stop, then print out the source line if we
501 if( (mode
!= EXEC_CONT
&& mode
!= EXEC_FINISH
) )
503 DEBUG_FindNearestSymbol( &addr
, TRUE
, NULL
, 0, &list
);
504 if( list
.sourcefile
!= NULL
)
506 DEBUG_List(&list
, NULL
, 0);
510 /* If there's no breakpoint and we are not single-stepping, then we */
511 /* must have encountered an int3 in the Windows program; let's skip it. */
512 if ((bpnum
== -1) && !(EFL_reg(&DEBUG_context
) & STEP_FLAG
))
513 EIP_reg(&DEBUG_context
)++;
515 /* no breakpoint, continue if in continuous mode */
516 return (mode
== EXEC_CONT
|| mode
== EXEC_FINISH
);
520 /***********************************************************************
521 * DEBUG_RestartExecution
523 * Set the breakpoints to the correct state to restart execution
526 enum exec_mode
DEBUG_RestartExecution( enum exec_mode mode
, int count
)
533 unsigned int * value
;
534 enum exec_mode ret_mode
;
536 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
538 addr
.seg
= CS_reg(&DEBUG_context
);
539 addr
.off
= EIP_reg(&DEBUG_context
);
540 if (ISV86(&DEBUG_context
)) addr
.seg
|= (DWORD
)(pTask
?(pTask
->hModule
):0)<<16; else
541 if (IS_SELECTOR_SYSTEM(addr
.seg
)) addr
.seg
= 0;
543 GlobalUnlock16( GetCurrentTask() );
546 * This is the mode we will be running in after we finish. We would like
547 * to be able to modify this in certain cases.
551 bp
= DEBUG_FindBreakpoint( &addr
);
552 if ( bp
!= -1 && bp
!= 0)
555 * If we have set a new value, then save it in the BP number.
557 if( count
!= 0 && mode
== EXEC_CONT
)
559 breakpoints
[bp
].skipcount
= count
;
561 mode
= EXEC_STEPI_INSTR
; /* If there's a breakpoint, skip it */
565 if( mode
== EXEC_CONT
&& count
> 1 )
567 fprintf(stderr
,"Not stopped at any breakpoint; argument ignored.\n");
571 if( mode
== EXEC_FINISH
&& DEBUG_IsFctReturn() )
573 mode
= ret_mode
= EXEC_STEPI_INSTR
;
576 instr
= (BYTE
*)CTX_SEG_OFF_TO_LIN( &DEBUG_context
,
577 CS_reg(&DEBUG_context
),
578 EIP_reg(&DEBUG_context
) );
580 * See if the function we are stepping into has debug info
581 * and line numbers. If not, then we step over it instead.
582 * FIXME - we need to check for things like thunks or trampolines,
583 * as the actual function may in fact have debug info.
587 delta
= *(unsigned int*) (instr
+ 1);
589 DEBUG_Disasm(&addr2
, FALSE
);
592 status
= DEBUG_CheckLinenoStatus(&addr2
);
594 * Anytime we have a trampoline, step over it.
596 if( ((mode
== EXEC_STEP_OVER
) || (mode
== EXEC_STEPI_OVER
))
597 && status
== FUNC_IS_TRAMPOLINE
)
600 fprintf(stderr
, "Not stepping into trampoline at %x (no lines)\n",
603 mode
= EXEC_STEP_OVER_TRAMPOLINE
;
606 if( mode
== EXEC_STEP_INSTR
&& status
== FUNC_HAS_NO_LINES
)
609 fprintf(stderr
, "Not stepping into function at %x (no lines)\n",
612 mode
= EXEC_STEP_OVER
;
617 if( mode
== EXEC_STEP_INSTR
)
619 if( DEBUG_CheckLinenoStatus(&addr
) == FUNC_HAS_NO_LINES
)
621 fprintf(stderr
, "Single stepping until exit from function, \n");
622 fprintf(stderr
, "which has no line number information.\n");
624 ret_mode
= mode
= EXEC_FINISH
;
630 case EXEC_CONT
: /* Continuous execution */
631 EFL_reg(&DEBUG_context
) &= ~STEP_FLAG
;
632 DEBUG_SetBreakpoints( TRUE
);
635 case EXEC_STEP_OVER_TRAMPOLINE
:
637 * This is the means by which we step over our conversion stubs
638 * in callfrom*.s and callto*.s. We dig the appropriate address
639 * off the stack, and we set the breakpoint there instead of the
640 * address just after the call.
642 value
= (unsigned int *) ESP_reg(&DEBUG_context
) + 2;
644 EFL_reg(&DEBUG_context
) &= ~STEP_FLAG
;
645 breakpoints
[0].addr
= addr
;
646 breakpoints
[0].enabled
= TRUE
;
647 breakpoints
[0].in_use
= TRUE
;
648 breakpoints
[0].skipcount
= 0;
649 breakpoints
[0].opcode
= *(BYTE
*)DBG_ADDR_TO_LIN( &addr
);
650 DEBUG_SetBreakpoints( TRUE
);
654 case EXEC_STEPI_OVER
: /* Stepping over a call */
655 case EXEC_STEP_OVER
: /* Stepping over a call */
656 if (DEBUG_IsStepOverInstr())
658 EFL_reg(&DEBUG_context
) &= ~STEP_FLAG
;
659 DEBUG_Disasm(&addr
, FALSE
);
660 breakpoints
[0].addr
= addr
;
661 breakpoints
[0].enabled
= TRUE
;
662 breakpoints
[0].in_use
= TRUE
;
663 breakpoints
[0].skipcount
= 0;
664 breakpoints
[0].opcode
= *(BYTE
*)DBG_ADDR_TO_LIN( &addr
);
665 DEBUG_SetBreakpoints( TRUE
);
668 /* else fall through to single-stepping */
670 case EXEC_STEP_INSTR
: /* Single-stepping an instruction */
671 case EXEC_STEPI_INSTR
: /* Single-stepping an instruction */
672 EFL_reg(&DEBUG_context
) |= STEP_FLAG
;
679 DEBUG_AddBPCondition(int num
, struct expr
* exp
)
681 if ((num
<= 0) || (num
>= next_bp
) || !breakpoints
[num
].in_use
)
683 fprintf( stderr
, "Invalid breakpoint number %d\n", num
);
687 if( breakpoints
[num
].condition
!= NULL
)
689 DEBUG_FreeExpr(breakpoints
[num
].condition
);
690 breakpoints
[num
].condition
= NULL
;
695 breakpoints
[num
].condition
= DEBUG_CloneExpr(exp
);