Release 970112
[wine.git] / debugger / dbg.y
blob8db088559f8c3e0779a189eee6884f11fcf864bd
1 %{
2 /*
3 * Parser for command lines in the Wine debugger
5 * Copyright 1993 Eric Youngdale
6 * Copyright 1995 Morten Welinder
7 */
9 #include <stdio.h>
10 #include <signal.h>
11 #include <unistd.h>
12 #include "class.h"
13 #include "module.h"
14 #include "options.h"
15 #include "queue.h"
16 #include "win.h"
17 #include "debugger.h"
19 #include "expr.h"
21 extern FILE * yyin;
22 unsigned int dbg_mode = 0;
23 int curr_frame = 0;
25 static enum exec_mode dbg_exec_mode = EXEC_CONT;
26 static int dbg_exec_count = 0;
28 void issue_prompt(void);
29 void mode_command(int);
30 void flush_symbols(void);
31 int yylex(void);
32 int yyerror(char *);
36 %union
38 DBG_ADDR address;
39 enum debug_regs reg;
40 char * string;
41 int integer;
42 struct list_id listing;
43 struct expr * expression;
46 %token tCONT tSTEP tLIST tNEXT tQUIT tHELP tBACKTRACE tINFO tWALK tUP tDOWN
47 %token tENABLE tDISABLE tBREAK tDELETE tSET tMODE tPRINT tEXAM tABORT
48 %token tCLASS tMODULE tSTACK tSEGMENTS tREGS tWND tQUEUE tLOCAL
49 %token tEOL tSTRING
50 %token tFRAME tSHARE tCOND tDISPLAY tUNDISPLAY
51 %token tSTEPI tNEXTI tFINISH tSHOW tDIR
52 %token <string> tPATH
53 %token <string> tIDENTIFIER tSTRING
54 %token <integer> tNUM tFORMAT
55 %token <reg> tREG
57 /* %left ',' */
58 /* %left '=' OP_OR_EQUAL OP_XOR_EQUAL OP_AND_EQUAL OP_SHL_EQUAL \
59 OP_SHR_EQUAL OP_PLUS_EQUAL OP_MINUS_EQUAL \
60 OP_TIMES_EQUAL OP_DIVIDE_EQUAL OP_MODULO_EQUAL */
61 /* %left OP_COND */ /* ... ? ... : ... */
62 %left OP_LOR
63 %left OP_LAND
64 %left '|'
65 %left '^'
66 %left '&'
67 %left OP_EQ OP_NE
68 %left '<' '>' OP_LE OP_GE
69 %left OP_SHL OP_SHR OP_DRF
70 %left '+' '-'
71 %left '*' '/' '%'
72 %left OP_SIGN '!' '~' OP_DEREF /* OP_INC OP_DEC OP_ADDR */
73 %left '.' '['
74 %nonassoc ':'
76 %type <expression> expr lval lvalue
77 %type <address> expr_addr lval_addr
78 %type <integer> expr_value
79 %type <string> pathname
81 %type <listing> list_arg
85 input: line { issue_prompt(); }
86 | input line { issue_prompt(); }
88 line: command
89 | tEOL
90 | error tEOL { yyerrok; }
92 command:
93 tQUIT tEOL { exit(0); }
94 | tHELP tEOL { DEBUG_Help(); }
95 | tHELP tINFO tEOL { DEBUG_HelpInfo(); }
96 | tCONT tEOL { dbg_exec_count = 1;
97 dbg_exec_mode = EXEC_CONT; return 0; }
98 | tCONT tNUM tEOL { dbg_exec_count = $2;
99 dbg_exec_mode = EXEC_CONT; return 0; }
100 | tSTEP tEOL { dbg_exec_count = 1;
101 dbg_exec_mode = EXEC_STEP_INSTR; return 0; }
102 | tNEXT tEOL { dbg_exec_count = 1;
103 dbg_exec_mode = EXEC_STEP_OVER; return 0; }
104 | tSTEP tNUM tEOL { dbg_exec_count = $2;
105 dbg_exec_mode = EXEC_STEP_INSTR; return 0; }
106 | tNEXT tNUM tEOL { dbg_exec_count = $2;
107 dbg_exec_mode = EXEC_STEP_OVER; return 0; }
108 | tSTEPI tEOL { dbg_exec_count = 1;
109 dbg_exec_mode = EXEC_STEPI_INSTR; return 0; }
110 | tNEXTI tEOL { dbg_exec_count = 1;
111 dbg_exec_mode = EXEC_STEPI_OVER; return 0; }
112 | tSTEPI tNUM tEOL { dbg_exec_count = $2;
113 dbg_exec_mode = EXEC_STEPI_INSTR; return 0; }
114 | tNEXTI tNUM tEOL { dbg_exec_count = $2;
115 dbg_exec_mode = EXEC_STEPI_OVER; return 0; }
116 | tABORT tEOL { kill(getpid(), SIGABRT); }
117 | tMODE tNUM tEOL { mode_command($2); }
118 | tENABLE tNUM tEOL { DEBUG_EnableBreakpoint( $2, TRUE ); }
119 | tDISABLE tNUM tEOL { DEBUG_EnableBreakpoint( $2, FALSE ); }
120 | tDELETE tBREAK tNUM tEOL { DEBUG_DelBreakpoint( $3 ); }
121 | tBACKTRACE tEOL { DEBUG_BackTrace(); }
122 | tUP tEOL { DEBUG_SetFrame( curr_frame + 1 ); }
123 | tUP tNUM tEOL { DEBUG_SetFrame( curr_frame + $2 ); }
124 | tDOWN tEOL { DEBUG_SetFrame( curr_frame - 1 ); }
125 | tDOWN tNUM tEOL { DEBUG_SetFrame( curr_frame - $2 ); }
126 | tFRAME tNUM tEOL { DEBUG_SetFrame( $2 ); }
127 | tFINISH tEOL { dbg_exec_count = 0;
128 dbg_exec_mode = EXEC_FINISH; return 0; }
129 | tSHOW tDIR tEOL { DEBUG_ShowDir(); }
130 | tDIR pathname tEOL { DEBUG_AddPath( $2 ); }
131 | tDIR tEOL { DEBUG_NukePath(); }
132 | tDISPLAY expr tEOL { DEBUG_AddDisplay($2); }
133 | tDELETE tDISPLAY tNUM tEOL { DEBUG_DelDisplay( $3 ); }
134 | tDELETE tDISPLAY tEOL { DEBUG_DelDisplay( -1 ); }
135 | tUNDISPLAY tNUM tEOL { DEBUG_DelDisplay( $2 ); }
136 | tUNDISPLAY tEOL { DEBUG_DelDisplay( -1 ); }
137 | tCOND tNUM tEOL { DEBUG_AddBPCondition($2, NULL); }
138 | tCOND tNUM expr tEOL { DEBUG_AddBPCondition($2, $3); }
139 | list_command
140 | set_command
141 | x_command
142 | print_command
143 | break_command
144 | info_command
145 | walk_command
147 set_command:
148 tSET tREG '=' expr_value tEOL { DEBUG_SetRegister( $2, $4 );
149 DEBUG_FreeExprMem(); }
150 | tSET lval_addr '=' expr_value tEOL { DEBUG_WriteMemory( &$2, $4 );
151 DEBUG_FreeExprMem(); }
153 pathname:
154 tIDENTIFIER { $$ = $1; }
155 | tPATH { $$ = $1; }
157 list_command:
158 tLIST tEOL { DEBUG_List( NULL, NULL, 10 ); }
159 | tLIST '-' tEOL { DEBUG_List( NULL, NULL, -10 ); }
160 | tLIST list_arg tEOL { DEBUG_List( & $2, NULL, 10 ); }
161 | tLIST ',' list_arg tEOL { DEBUG_List( NULL, & $3, -10 ); }
162 | tLIST list_arg ',' list_arg tEOL { DEBUG_List( & $2, & $4, 0 ); }
164 list_arg:
165 tNUM { $$.sourcefile = NULL; $$.line = $1; }
166 | pathname ':' tNUM { $$.sourcefile = $1; $$.line = $3; }
167 | tIDENTIFIER { DEBUG_GetFuncInfo( & $$, NULL, $1); }
168 | pathname ':' tIDENTIFIER { DEBUG_GetFuncInfo( & $$, $1, $3); }
169 | '*' expr_addr { DEBUG_FindNearestSymbol( & $2, FALSE, NULL,
170 0, & $$ );
171 DEBUG_FreeExprMem(); }
173 x_command:
174 tEXAM expr_addr tEOL { DEBUG_ExamineMemory( &$2, 1, 'x');
175 DEBUG_FreeExprMem(); }
176 | tEXAM tFORMAT expr_addr tEOL { DEBUG_ExamineMemory( &$3, $2>>8, $2&0xff );
177 DEBUG_FreeExprMem(); }
179 print_command:
180 tPRINT expr_addr tEOL { DEBUG_Print( &$2, 1, 0, 0 );
181 DEBUG_FreeExprMem(); }
182 | tPRINT tFORMAT expr_addr tEOL { DEBUG_Print( &$3, $2 >> 8, $2 & 0xff, 0 );
183 DEBUG_FreeExprMem(); }
185 break_command:
186 tBREAK '*' expr_addr tEOL { DEBUG_AddBreakpoint( &$3 );
187 DEBUG_FreeExprMem(); }
188 | tBREAK tIDENTIFIER tEOL { DBG_ADDR addr;
189 if( DEBUG_GetSymbolValue($2, -1, &addr, TRUE) )
191 DEBUG_AddBreakpoint( &addr );
193 else
195 fprintf(stderr,"Unable to add breakpoint\n");
198 | tBREAK tIDENTIFIER ':' tNUM tEOL { DBG_ADDR addr;
199 if( DEBUG_GetSymbolValue($2, $4, &addr, TRUE) )
201 DEBUG_AddBreakpoint( &addr );
203 else
205 fprintf(stderr,"Unable to add breakpoint\n");
208 | tBREAK tNUM tEOL { struct name_hash *nh;
209 DBG_ADDR addr = { NULL,
210 CS_reg(&DEBUG_context),
211 EIP_reg(&DEBUG_context) };
213 DBG_FIX_ADDR_SEG( &addr, CS_reg(&DEBUG_context) );
214 DEBUG_FindNearestSymbol(&addr, TRUE,
215 &nh, 0, NULL);
216 if( nh != NULL )
218 DEBUG_GetLineNumberAddr(nh,
219 $2, &addr, TRUE);
220 DEBUG_AddBreakpoint( &addr );
222 else
224 fprintf(stderr,"Unable to add breakpoint\n");
228 | tBREAK tEOL { DBG_ADDR addr = { NULL,
229 CS_reg(&DEBUG_context),
230 EIP_reg(&DEBUG_context) };
231 DEBUG_AddBreakpoint( &addr );
234 info_command:
235 tINFO tBREAK tEOL { DEBUG_InfoBreakpoints(); }
236 | tINFO tCLASS expr_value tEOL { CLASS_DumpClass( (CLASS *)$3 );
237 DEBUG_FreeExprMem(); }
238 | tINFO tSHARE tEOL { DEBUG_InfoShare(); }
239 | tINFO tMODULE expr_value tEOL { MODULE_DumpModule( $3 );
240 DEBUG_FreeExprMem(); }
241 | tINFO tQUEUE expr_value tEOL { QUEUE_DumpQueue( $3 );
242 DEBUG_FreeExprMem(); }
243 | tINFO tREGS tEOL { DEBUG_InfoRegisters(); }
244 | tINFO tSEGMENTS expr_value tEOL { LDT_Print( SELECTOR_TO_ENTRY($3), 1 );
245 DEBUG_FreeExprMem(); }
246 | tINFO tSEGMENTS tEOL { LDT_Print( 0, -1 ); }
247 | tINFO tSTACK tEOL { DEBUG_InfoStack(); }
248 | tINFO tWND expr_value tEOL { WIN_DumpWindow( $3 );
249 DEBUG_FreeExprMem(); }
250 | tINFO tLOCAL tEOL { DEBUG_InfoLocals(); }
251 | tINFO tDISPLAY tEOL { DEBUG_InfoDisplay(); }
253 walk_command:
254 tWALK tCLASS tEOL { CLASS_WalkClasses(); }
255 | tWALK tMODULE tEOL { MODULE_WalkModules(); }
256 | tWALK tQUEUE tEOL { QUEUE_WalkQueues(); }
257 | tWALK tWND tEOL { WIN_WalkWindows( 0, 0 ); }
258 | tWALK tWND tNUM tEOL { WIN_WalkWindows( $3, 0 ); }
260 expr_addr:
261 expr { $$ = DEBUG_EvalExpr($1) }
263 expr_value:
264 expr { DBG_ADDR addr = DEBUG_EvalExpr($1);
265 $$ = *(unsigned int *) addr.off; }
267 * The expr rule builds an expression tree. When we are done, we call
268 * EvalExpr to evaluate the value of the expression. The advantage of
269 * the two-step approach is that it is possible to save expressions for
270 * use in 'display' commands, and in conditional watchpoints.
272 expr:
273 tNUM { $$ = DEBUG_ConstExpr($1); }
274 | tSTRING { $$ = DEBUG_StringExpr($1); }
275 | tREG { $$ = DEBUG_RegisterExpr($1); }
276 | tIDENTIFIER { $$ = DEBUG_SymbolExpr($1); }
277 | expr OP_DRF tIDENTIFIER { $$ = DEBUG_StructPExpr($1, $3); }
278 | expr '.' tIDENTIFIER { $$ = DEBUG_StructExpr($1, $3); }
279 | tIDENTIFIER '(' ')' { $$ = DEBUG_CallExpr($1, 0); }
280 | tIDENTIFIER '(' expr ')' { $$ = DEBUG_CallExpr($1, 1, $3); }
281 | tIDENTIFIER '(' expr ',' expr ')' { $$ = DEBUG_CallExpr($1, 2, $3,
282 $5); }
283 | tIDENTIFIER '(' expr ',' expr ',' expr ')' { $$ = DEBUG_CallExpr($1, 3, $3, $5, $7); }
284 | tIDENTIFIER '(' expr ',' expr ',' expr ',' expr ')' { $$ = DEBUG_CallExpr($1, 3, $3, $5, $7, $9); }
285 | tIDENTIFIER '(' expr ',' expr ',' expr ',' expr ',' expr ')' { $$ = DEBUG_CallExpr($1, 3, $3, $5, $7, $9, $11); }
286 | expr '[' expr ']' { $$ = DEBUG_BinopExpr(EXP_OP_ARR, $1, $3); }
287 | expr ':' expr { $$ = DEBUG_BinopExpr(EXP_OP_SEG, $1, $3); }
288 | expr OP_LOR expr { $$ = DEBUG_BinopExpr(EXP_OP_LOR, $1, $3); }
289 | expr OP_LAND expr { $$ = DEBUG_BinopExpr(EXP_OP_LAND, $1, $3); }
290 | expr '|' expr { $$ = DEBUG_BinopExpr(EXP_OP_OR, $1, $3); }
291 | expr '&' expr { $$ = DEBUG_BinopExpr(EXP_OP_AND, $1, $3); }
292 | expr '^' expr { $$ = DEBUG_BinopExpr(EXP_OP_XOR, $1, $3); }
293 | expr OP_EQ expr { $$ = DEBUG_BinopExpr(EXP_OP_EQ, $1, $3); }
294 | expr '>' expr { $$ = DEBUG_BinopExpr(EXP_OP_GT, $1, $3); }
295 | expr '<' expr { $$ = DEBUG_BinopExpr(EXP_OP_LT, $1, $3); }
296 | expr OP_GE expr { $$ = DEBUG_BinopExpr(EXP_OP_GE, $1, $3); }
297 | expr OP_LE expr { $$ = DEBUG_BinopExpr(EXP_OP_LE, $1, $3); }
298 | expr OP_NE expr { $$ = DEBUG_BinopExpr(EXP_OP_NE, $1, $3); }
299 | expr OP_SHL expr { $$ = DEBUG_BinopExpr(EXP_OP_SHL, $1, $3); }
300 | expr OP_SHR expr { $$ = DEBUG_BinopExpr(EXP_OP_SHR, $1, $3); }
301 | expr '+' expr { $$ = DEBUG_BinopExpr(EXP_OP_ADD, $1, $3); }
302 | expr '-' expr { $$ = DEBUG_BinopExpr(EXP_OP_SUB, $1, $3); }
303 | expr '*' expr { $$ = DEBUG_BinopExpr(EXP_OP_MUL, $1, $3); }
304 | expr '/' expr { $$ = DEBUG_BinopExpr(EXP_OP_DIV, $1, $3); }
305 | expr '%' expr { $$ = DEBUG_BinopExpr(EXP_OP_REM, $1, $3); }
306 | '-' expr %prec OP_SIGN { $$ = DEBUG_UnopExpr(EXP_OP_NEG, $2); }
307 | '+' expr %prec OP_SIGN { $$ = $2; }
308 | '!' expr { $$ = DEBUG_UnopExpr(EXP_OP_NOT, $2); }
309 | '~' expr { $$ = DEBUG_UnopExpr(EXP_OP_LNOT, $2); }
310 | '(' expr ')' { $$ = $2; }
311 | '*' expr %prec OP_DEREF { $$ = DEBUG_UnopExpr(EXP_OP_DEREF, $2); }
312 | '&' expr %prec OP_DEREF { $$ = DEBUG_UnopExpr(EXP_OP_ADDR, $2); }
315 * The lvalue rule builds an expression tree. This is a limited form
316 * of expression that is suitable to be used as an lvalue.
318 lval_addr:
319 lval { $$ = DEBUG_EvalExpr($1) }
321 lval:
322 lvalue { $$ = $1; }
323 | '*' expr { $$ = DEBUG_UnopExpr(EXP_OP_FORCE_DEREF, $2); }
325 lvalue:
326 tNUM { $$ = DEBUG_ConstExpr($1); }
327 | tREG { $$ = DEBUG_RegisterExpr($1); }
328 | tIDENTIFIER { $$ = DEBUG_SymbolExpr($1); }
329 | lvalue OP_DRF tIDENTIFIER { $$ = DEBUG_StructPExpr($1, $3); }
330 | lvalue '.' tIDENTIFIER { $$ = DEBUG_StructExpr($1, $3); }
331 | lvalue '[' expr ']' { $$ = DEBUG_BinopExpr(EXP_OP_ARR, $1, $3); }
335 void
336 issue_prompt(){
337 #ifdef DONT_USE_READLINE
338 fprintf(stderr,"Wine-dbg>");
339 #endif
342 void mode_command(int newmode)
344 if ((newmode == 16) || (newmode == 32)) dbg_mode = newmode;
345 else fprintf(stderr,"Invalid mode (use 16 or 32)\n");
349 /***********************************************************************
350 * DEBUG_Main
352 * Debugger main loop.
354 static void DEBUG_Main( int signal )
356 static int loaded_symbols = 0;
357 static BOOL32 in_debugger = FALSE;
358 char SymbolTableFile[256];
359 int newmode;
360 BOOL32 ret_ok;
361 #ifdef YYDEBUG
362 yydebug = 0;
363 #endif
365 if (in_debugger)
367 fprintf( stderr, "Segmentation fault inside debugger, exiting.\n" );
368 exit(1);
370 in_debugger = TRUE;
371 yyin = stdin;
373 DEBUG_SetBreakpoints( FALSE );
375 if (!loaded_symbols)
377 loaded_symbols++;
380 * Initialize the type handling stuff.
382 DEBUG_InitTypes();
385 * In some cases we can read the stabs information directly
386 * from the executable. If this is the case, we don't need
387 * to bother with trying to read a symbol file, as the stabs
388 * also have line number and local variable information.
389 * As long as gcc is used for the compiler, stabs will
390 * be the default. On SVr4, DWARF could be used, but we
391 * don't grok that yet, and in this case we fall back to using
392 * the wine.sym file.
394 if( DEBUG_ReadExecutableDbgInfo() == FALSE )
396 PROFILE_GetWineIniString( "wine", "SymbolTableFile", "wine.sym",
397 SymbolTableFile, sizeof(SymbolTableFile));
398 DEBUG_ReadSymbolTable( SymbolTableFile );
402 * Read COFF, MSC, etc debug information that we noted when we
403 * started up the executable.
405 DEBUG_ProcessDeferredDebug();
407 DEBUG_LoadEntryPoints();
410 #if 0
411 fprintf(stderr, "Entering debugger PC=%x, mode=%d, count=%d\n",
412 EIP_reg(&DEBUG_context),
413 dbg_exec_mode, dbg_exec_count);
415 sleep(1);
416 #endif
418 if ((signal != SIGTRAP) || !DEBUG_ShouldContinue( dbg_exec_mode,
419 &dbg_exec_count ))
421 DBG_ADDR addr;
423 addr.seg = CS_reg(&DEBUG_context);
424 addr.off = EIP_reg(&DEBUG_context);
425 addr.type = NULL;
426 DBG_FIX_ADDR_SEG( &addr, 0 );
428 /* Put the display in a correct state */
430 XUngrabPointer( display, CurrentTime );
431 XUngrabServer( display );
432 XFlush( display );
434 if (!addr.seg) newmode = 32;
435 else newmode = (GET_SEL_FLAGS(addr.seg) & LDT_FLAGS_32BIT) ? 32 : 16;
437 if (newmode != dbg_mode)
438 fprintf(stderr,"In %d bit mode.\n", dbg_mode = newmode);
440 DEBUG_DoDisplay();
442 if (signal != SIGTRAP) /* This is a real crash, dump some info */
444 DEBUG_InfoRegisters();
445 DEBUG_InfoStack();
446 if (dbg_mode == 16)
448 LDT_Print( SELECTOR_TO_ENTRY(DS_reg(&DEBUG_context)), 1 );
449 if (ES_reg(&DEBUG_context) != DS_reg(&DEBUG_context))
450 LDT_Print( SELECTOR_TO_ENTRY(ES_reg(&DEBUG_context)), 1 );
452 DEBUG_BackTrace();
454 else
457 * Do a quiet backtrace so that we have an idea of what the situation
458 * is WRT the source files.
460 DEBUG_SilentBackTrace();
463 /* Show where we crashed */
464 curr_frame = 0;
465 DEBUG_PrintAddress( &addr, dbg_mode, TRUE );
466 fprintf(stderr,": ");
467 if (DBG_CHECK_READ_PTR( &addr, 1 ))
469 DEBUG_Disasm( &addr, TRUE );
470 fprintf(stderr,"\n");
473 ret_ok = 0;
476 issue_prompt();
477 yyparse();
478 flush_symbols();
479 addr.seg = CS_reg(&DEBUG_context);
480 addr.off = EIP_reg(&DEBUG_context);
481 DBG_FIX_ADDR_SEG( &addr, 0 );
482 ret_ok = DEBUG_ValidateRegisters();
483 if (ret_ok) ret_ok = DBG_CHECK_READ_PTR( &addr, 1 );
484 } while (!ret_ok);
487 dbg_exec_mode = DEBUG_RestartExecution( dbg_exec_mode, dbg_exec_count );
489 * This will have gotten absorbed into the breakpoint info
490 * if it was used. Otherwise it would have been ignored.
491 * In any case, we don't mess with it any more.
493 if( dbg_exec_mode == EXEC_CONT )
495 dbg_exec_count = 0;
498 in_debugger = FALSE;
502 /***********************************************************************
503 * DEBUG_EnterDebugger
505 * Force an entry into the debugger.
507 void DEBUG_EnterDebugger(void)
509 kill( getpid(), SIGHUP );
513 /***********************************************************************
514 * DebugBreak16 (KERNEL.203)
516 void DebugBreak16( CONTEXT *regs )
518 const char *module = MODULE_GetModuleName( GetExePtr(GetCurrentTask()) );
519 fprintf( stderr, "%s called DebugBreak\n", module ? module : "???" );
520 DEBUG_context = *regs;
521 DEBUG_Main( SIGTRAP );
525 void wine_debug( int signal, SIGCONTEXT *regs )
527 DEBUG_SetSigContext( regs );
528 DEBUG_Main( signal );
529 DEBUG_GetSigContext( regs );
532 int yyerror(char * s)
534 fprintf(stderr,"%s\n", s);
535 return 0;