Replaced a X call with a TSX call in XDRV_MOUSE_SetCursor().
[wine/wine64.git] / debugger / break.c
blob5f445aa1c1ac7d6ec8d5f769f3fb6885beb0c871
1 /*
2 * Debugger break-points handling
4 * Copyright 1994 Martin von Loewis
5 * Copyright 1995 Alexandre Julliard
6 */
8 #include "config.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/types.h>
12 #ifdef HAVE_SYS_MMAN_H
13 #include <sys/mman.h>
14 #endif
15 #include "wine/winbase16.h"
16 #include "module.h"
17 #include "neexe.h"
18 #include "process.h"
19 #include "task.h"
20 #include "miscemu.h"
21 #include "toolhelp.h"
22 #include "debugger.h"
23 #include "dosexe.h"
25 #define INT3 0xcc /* int 3 opcode */
27 #define MAX_BREAKPOINTS 100
29 typedef struct
31 DBG_ADDR addr;
32 BYTE addrlen;
33 BYTE opcode;
34 BOOL16 enabled;
35 WORD skipcount;
36 BOOL16 in_use;
37 struct expr * condition;
38 } BREAKPOINT;
40 static BREAKPOINT breakpoints[MAX_BREAKPOINTS];
42 static int next_bp = 1; /* breakpoint 0 is reserved for step-over */
45 /***********************************************************************
46 * DEBUG_ChangeOpcode
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" );
63 return;
65 *ptr = op;
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) );
83 for (;;)
85 switch(*instr)
87 /* Skip all prefixes */
89 case 0x2e: /* cs: */
90 case 0x36: /* ss: */
91 case 0x3e: /* ds: */
92 case 0x26: /* es: */
93 case 0x64: /* fs: */
94 case 0x65: /* gs: */
95 case 0x66: /* opcode size prefix */
96 case 0x67: /* addr size prefix */
97 case 0xf0: /* lock */
98 case 0xf2: /* repne */
99 case 0xf3: /* repe */
100 instr++;
101 continue;
103 /* Handle call instructions */
105 case 0xcd: /* int <intno> */
106 case 0xe8: /* call <offset> */
107 case 0x9a: /* lcall <seg>:<off> */
108 return TRUE;
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 */
130 return TRUE;
132 default:
133 return FALSE;
139 /***********************************************************************
140 * DEBUG_IsFctReturn
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) );
151 for (;;)
153 switch(*instr)
155 case 0xc2:
156 case 0xc3:
157 return TRUE;
158 default:
159 return FALSE;
165 /***********************************************************************
166 * DEBUG_SetBreakpoints
168 * Set or remove all the breakpoints.
170 void DEBUG_SetBreakpoints( BOOL set )
172 int i;
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 )
200 int i;
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;
208 return -1;
212 /***********************************************************************
213 * DEBUG_AddBreakpoint
215 * Add a breakpoint.
217 void DEBUG_AddBreakpoint( const DBG_ADDR *address )
219 DBG_ADDR addr = *address;
220 int num;
221 unsigned int seg2;
222 BYTE *p;
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
231 * should be all set.
233 seg2 = addr.seg;
234 addr.seg = 0;
235 addr.off = DEBUG_GetExprValue(&addr, NULL);
236 addr.seg = seg2;
238 if (!DBG_CHECK_READ_PTR( &addr, 1 )) return;
240 if (next_bp < MAX_BREAKPOINTS)
241 num = next_bp++;
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" );
249 return;
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,
262 TRUE );
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 );
277 return;
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 );
302 return;
304 breakpoints[num].enabled = enable;
305 breakpoints[num].skipcount = 0;
309 /***********************************************************************
310 * DEBUG_InfoBreakpoints
312 * Display break points information.
314 void DEBUG_InfoBreakpoints(void)
316 int i;
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,
325 TRUE);
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 );
346 NE_MODULE *pModule;
347 DBG_ADDR addr = { NULL, 0, 0 };
349 if ( pTask )
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 );
359 } else
360 if (!(pModule->flags & NE_FFLAGS_WIN32)) /* NE module */
362 addr.seg =
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 );
368 else /* PE module */
370 addr.seg = 0;
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 )
390 DBG_ADDR addr;
391 DBG_ADDR cond_addr;
392 int bpnum;
393 struct list_id list;
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) )
426 return TRUE;
430 if( breakpoints[bpnum].skipcount > 0 )
432 breakpoints[bpnum].skipcount--;
433 if( breakpoints[bpnum].skipcount > 0 )
435 return TRUE;
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);
452 return FALSE;
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 )
465 (*count)--;
468 else if( mode == EXEC_STEPI_OVER
469 || mode == EXEC_STEPI_INSTR )
472 (*count)--;
475 if( *count > 0 || mode == EXEC_FINISH )
478 * We still need to execute more instructions.
480 return TRUE;
484 * If we are about to stop, then print out the source line if we
485 * have it.
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
510 * in the given mode.
512 enum exec_mode DEBUG_RestartExecution( enum exec_mode mode, int count )
514 DBG_ADDR addr;
515 DBG_ADDR addr2;
516 int bp;
517 int delta;
518 int status;
519 unsigned int * value;
520 enum exec_mode ret_mode;
521 BYTE *instr;
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.
535 ret_mode = mode;
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 */
549 else
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.
571 if( *instr == 0xe8 )
573 delta = *(unsigned int*) (instr + 1);
574 addr2 = addr;
575 DEBUG_Disasm(&addr2, FALSE);
576 addr2.off += delta;
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 )
585 #if 0
586 fprintf(stderr, "Not stepping into trampoline at %x (no lines)\n",
587 addr2.off);
588 #endif
589 mode = EXEC_STEP_OVER_TRAMPOLINE;
592 if( mode == EXEC_STEP_INSTR && status == FUNC_HAS_NO_LINES )
594 #if 0
595 fprintf(stderr, "Not stepping into function at %x (no lines)\n",
596 addr2.off);
597 #endif
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;
614 switch(mode)
616 case EXEC_CONT: /* Continuous execution */
617 case EXEC_PASS: /* Continue, passing exception */
618 EFL_reg(&DEBUG_context) &= ~STEP_FLAG;
619 DEBUG_SetBreakpoints( TRUE );
620 break;
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;
630 addr.off = *value;
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 );
638 break;
640 case EXEC_FINISH:
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 );
653 break;
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;
660 break;
662 return ret_mode;
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 );
671 return FALSE;
674 if( breakpoints[num].condition != NULL )
676 DEBUG_FreeExpr(breakpoints[num].condition);
677 breakpoints[num].condition = NULL;
680 if( exp != NULL )
682 breakpoints[num].condition = DEBUG_CloneExpr(exp);
685 return TRUE;