2 * Debugger break-points handling
4 * Copyright 1994 Martin von Loewis
5 * Copyright 1995 Alexandre Julliard
10 #include <sys/types.h>
18 #define INT3 0xcc /* int 3 opcode */
20 #define MAX_BREAKPOINTS 100
30 struct expr
* condition
;
33 static BREAKPOINT breakpoints
[MAX_BREAKPOINTS
];
35 static int next_bp
= 1; /* breakpoint 0 is reserved for step-over */
38 /***********************************************************************
41 * Change the opcode at segment:addr.
43 static void DEBUG_SetOpcode( const DBG_ADDR
*addr
, BYTE op
)
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" );
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
) );
82 /* Skip all prefixes */
90 case 0x66: /* opcode size prefix */
91 case 0x67: /* addr size prefix */
93 case 0xf2: /* repne */
98 /* Handle call instructions */
100 case 0xe8: /* call <offset> */
101 case 0x9a: /* lcall <seg>:<off> */
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 */
133 /***********************************************************************
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
) );
158 /***********************************************************************
159 * DEBUG_SetBreakpoints
161 * Set or remove all the breakpoints.
163 void DEBUG_SetBreakpoints( BOOL32 set
)
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
)
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
;
205 /***********************************************************************
206 * DEBUG_AddBreakpoint
210 void DEBUG_AddBreakpoint( const DBG_ADDR
*address
)
212 DBG_ADDR addr
= *address
;
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
228 addr
.off
= DEBUG_GetExprValue(&addr
, NULL
);
231 if (!DBG_CHECK_READ_PTR( &addr
, 1 )) return;
233 if (next_bp
< MAX_BREAKPOINTS
)
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" );
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
,
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
);
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
);
297 breakpoints
[num
].enabled
= enable
;
298 breakpoints
[num
].skipcount
= 0;
302 /***********************************************************************
303 * DEBUG_InfoBreakpoints
305 * Display break points information.
307 void DEBUG_InfoBreakpoints(void)
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
,
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)
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 */
351 if (!pCurrentProcess
) continue;
352 pem
= pCurrentProcess
->modref_list
;
355 if (pem
->module
== pModule
->module32
) break;
360 addr
.off
= (DWORD
)RVA_PTR( pem
->module
,
361 OptionalHeader
.AddressOfEntryPoint
);
362 fprintf( stderr
, "Win32 task '%s': ", entry
.szModule
);
363 DEBUG_AddBreakpoint( &addr
);
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
)
392 /* If not single-stepping, back up over the int3 instruction */
393 if (!(EFL_reg(&DEBUG_context
) & STEP_FLAG
)) EIP_reg(&DEBUG_context
)--;
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
) )
423 if( breakpoints
[bpnum
].skipcount
> 0 )
425 breakpoints
[bpnum
].skipcount
--;
426 if( breakpoints
[bpnum
].skipcount
> 0 )
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);
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
)
461 else if( mode
== EXEC_STEPI_OVER
462 || mode
== EXEC_STEPI_INSTR
)
468 if( *count
> 0 || mode
== EXEC_FINISH
)
471 * We still need to execute more instructions.
477 * If we are about to stop, then print out the source line if we
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
505 enum exec_mode
DEBUG_RestartExecution( enum exec_mode mode
, int count
)
512 unsigned int * value
;
513 enum exec_mode ret_mode
;
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.
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 */
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.
562 delta
= *(unsigned int*) (instr
+ 1);
564 DEBUG_Disasm(&addr2
, FALSE
);
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
)
575 fprintf(stderr
, "Not stepping into trampoline at %x (no lines)\n",
578 mode
= EXEC_STEP_OVER_TRAMPOLINE
;
581 if( mode
== EXEC_STEP_INSTR
&& status
== FUNC_HAS_NO_LINES
)
584 fprintf(stderr
, "Not stepping into function at %x (no lines)\n",
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
;
605 case EXEC_CONT
: /* Continuous execution */
606 EFL_reg(&DEBUG_context
) &= ~STEP_FLAG
;
607 DEBUG_SetBreakpoints( TRUE
);
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;
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
);
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
);
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
;
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
);
662 if( breakpoints
[num
].condition
!= NULL
)
664 DEBUG_FreeExpr(breakpoints
[num
].condition
);
665 breakpoints
[num
].condition
= NULL
;
670 breakpoints
[num
].condition
= DEBUG_CloneExpr(exp
);