Debugger is now called by exception handling.
[wine] / debugger / dbg.y
1 %{
2 /*
3  * Parser for command lines in the Wine debugger
4  *
5  * Copyright 1993 Eric Youngdale
6  * Copyright 1995 Morten Welinder
7  */
8
9 #include "config.h"
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <signal.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #ifdef HAVE_ALLOCA_H
18 #include <alloca.h>
19 #endif
20
21 #include "winbase.h"
22 #include "module.h"
23 #include "task.h"
24 #include "options.h"
25 #include "queue.h"
26 #include "wine/winbase16.h"
27 #include "winnt.h"
28 #include "x11drv.h"
29 #include "win.h"
30 #include "debugger.h"
31 #include "neexe.h"
32 #include "process.h"
33 #include "server.h"
34 #include "main.h"
35 #include "expr.h"
36 #include "user.h"
37
38 extern FILE * yyin;
39 unsigned int dbg_mode = 0;
40 HANDLE dbg_heap = 0;
41 int curr_frame = 0;
42
43 static enum exec_mode dbg_exec_mode = EXEC_CONT;
44 static int dbg_exec_count = 0;
45
46 void issue_prompt(void);
47 void mode_command(int);
48 void flush_symbols(void);
49 int yylex(void);
50 int yyerror(char *);
51
52 #ifdef DBG_need_heap
53 #define malloc(x) DBG_alloc(x)
54 #define realloc(x,y) DBG_realloc(x,y)
55 #define free(x) DBG_free(x)
56 #endif
57
58 extern void VIRTUAL_Dump(void);  /* memory/virtual.c */
59
60 %}
61
62 %union
63 {
64     DBG_ADDR         address;
65     enum debug_regs  reg;
66     char *           string;
67     int              integer;
68     struct list_id   listing;
69     struct expr *    expression;
70     struct datatype * type;
71 }
72
73 %token tCONT tPASS tSTEP tLIST tNEXT tQUIT tHELP tBACKTRACE tINFO tWALK tUP tDOWN
74 %token tENABLE tDISABLE tBREAK tDELETE tSET tMODE tPRINT tEXAM tABORT tDEBUGMSG
75 %token tCLASS tMAPS tMODULE tSTACK tSEGMENTS tREGS tWND tQUEUE tLOCAL
76 %token tPROCESS tMODREF
77 %token tEOL tSTRING tDEBUGSTR
78 %token tFRAME tSHARE tCOND tDISPLAY tUNDISPLAY tDISASSEMBLE
79 %token tSTEPI tNEXTI tFINISH tSHOW tDIR
80 %token <string> tPATH
81 %token <string> tIDENTIFIER tSTRING tDEBUGSTR
82 %token <integer> tNUM tFORMAT
83 %token <reg> tREG
84 %token tSYMBOLFILE
85
86 %token tCHAR tSHORT tINT tLONG tFLOAT tDOUBLE tUNSIGNED tSIGNED 
87 %token tSTRUCT tUNION tENUM
88
89 /* %left ',' */
90 /* %left '=' OP_OR_EQUAL OP_XOR_EQUAL OP_AND_EQUAL OP_SHL_EQUAL \
91          OP_SHR_EQUAL OP_PLUS_EQUAL OP_MINUS_EQUAL \
92          OP_TIMES_EQUAL OP_DIVIDE_EQUAL OP_MODULO_EQUAL */
93 /* %left OP_COND */ /* ... ? ... : ... */
94 %left OP_LOR
95 %left OP_LAND
96 %left '|'
97 %left '^'
98 %left '&'
99 %left OP_EQ OP_NE
100 %left '<' '>' OP_LE OP_GE
101 %left OP_SHL OP_SHR
102 %left '+' '-'
103 %left '*' '/' '%'
104 %left OP_SIGN '!' '~' OP_DEREF /* OP_INC OP_DEC OP_ADDR */
105 %left '.' '[' OP_DRF
106 %nonassoc ':'
107
108 %type <expression> expr lval lvalue 
109 %type <type> type_cast type_expr
110 %type <address> expr_addr lval_addr
111 %type <integer> expr_value
112 %type <string> pathname
113
114 %type <listing> list_arg
115
116 %%
117
118 input: line                    { issue_prompt(); }
119     | input line               { issue_prompt(); }
120
121 line: command 
122     | tEOL
123     | error tEOL               { yyerrok; }
124
125 command:
126       tQUIT tEOL               { exit(0); }
127     | tHELP tEOL               { DEBUG_Help(); }
128     | tHELP tINFO tEOL         { DEBUG_HelpInfo(); }
129     | tCONT tEOL               { dbg_exec_count = 1; 
130                                  dbg_exec_mode = EXEC_CONT; return 0; }
131     | tPASS tEOL               { dbg_exec_count = 1; 
132                                  dbg_exec_mode = EXEC_PASS; return 0; }
133     | tCONT tNUM tEOL          { dbg_exec_count = $2; 
134                                  dbg_exec_mode = EXEC_CONT; return 0; }
135     | tSTEP tEOL               { dbg_exec_count = 1; 
136                                  dbg_exec_mode = EXEC_STEP_INSTR; return 0; }
137     | tNEXT tEOL               { dbg_exec_count = 1; 
138                                  dbg_exec_mode = EXEC_STEP_OVER; return 0; }
139     | tSTEP tNUM tEOL          { dbg_exec_count = $2; 
140                                  dbg_exec_mode = EXEC_STEP_INSTR; return 0; }
141     | tNEXT tNUM tEOL          { dbg_exec_count = $2; 
142                                  dbg_exec_mode = EXEC_STEP_OVER; return 0; }
143     | tSTEPI tEOL              { dbg_exec_count = 1; 
144                                  dbg_exec_mode = EXEC_STEPI_INSTR; return 0; }
145     | tNEXTI tEOL              { dbg_exec_count = 1; 
146                                  dbg_exec_mode = EXEC_STEPI_OVER; return 0; }
147     | tSTEPI tNUM tEOL         { dbg_exec_count = $2; 
148                                  dbg_exec_mode = EXEC_STEPI_INSTR; return 0; }
149     | tNEXTI tNUM tEOL         { dbg_exec_count = $2; 
150                                  dbg_exec_mode = EXEC_STEPI_OVER; return 0; }
151     | tABORT tEOL              { kill(getpid(), SIGABRT); }
152     | tMODE tNUM tEOL          { mode_command($2); }
153     | tENABLE tNUM tEOL        { DEBUG_EnableBreakpoint( $2, TRUE ); }
154     | tDISABLE tNUM tEOL       { DEBUG_EnableBreakpoint( $2, FALSE ); }
155     | tDELETE tBREAK tNUM tEOL { DEBUG_DelBreakpoint( $3 ); }
156     | tBACKTRACE tEOL          { DEBUG_BackTrace(); }
157     | tUP tEOL                 { DEBUG_SetFrame( curr_frame + 1 );  }
158     | tUP tNUM tEOL            { DEBUG_SetFrame( curr_frame + $2 ); }
159     | tDOWN tEOL               { DEBUG_SetFrame( curr_frame - 1 );  }
160     | tDOWN tNUM tEOL          { DEBUG_SetFrame( curr_frame - $2 ); }
161     | tFRAME tNUM tEOL         { DEBUG_SetFrame( $2 ); }
162     | tFINISH tEOL             { dbg_exec_count = 0;
163                                  dbg_exec_mode = EXEC_FINISH; return 0; }
164     | tSHOW tDIR tEOL          { DEBUG_ShowDir(); }
165     | tDIR pathname tEOL       { DEBUG_AddPath( $2 ); }
166     | tDIR tEOL                { DEBUG_NukePath(); }
167     | tDISPLAY tEOL            { DEBUG_InfoDisplay(); }
168     | tDISPLAY expr tEOL       { DEBUG_AddDisplay($2, 1, 0); }
169     | tDISPLAY tFORMAT expr tEOL { DEBUG_AddDisplay($3, $2 >> 8, $2 & 0xff); }
170     | tDELETE tDISPLAY tNUM tEOL { DEBUG_DelDisplay( $3 ); }
171     | tDELETE tDISPLAY tEOL    { DEBUG_DelDisplay( -1 ); }
172     | tUNDISPLAY tNUM tEOL     { DEBUG_DelDisplay( $2 ); }
173     | tUNDISPLAY tEOL          { DEBUG_DelDisplay( -1 ); }
174     | tCOND tNUM tEOL          { DEBUG_AddBPCondition($2, NULL); }
175     | tCOND tNUM expr tEOL     { DEBUG_AddBPCondition($2, $3); }
176     | tDEBUGMSG tDEBUGSTR tEOL { MAIN_ParseDebugOptions($2); }
177     | tSYMBOLFILE pathname tEOL{ DEBUG_ReadSymbolTable($2); }
178     | list_command
179     | disassemble_command
180     | set_command
181     | x_command
182     | print_command
183     | break_command
184     | info_command
185     | walk_command
186
187 set_command:
188       tSET tREG '=' expr_value tEOL        { DEBUG_SetRegister( $2, $4 ); 
189                                              DEBUG_FreeExprMem(); }
190     | tSET lval_addr '=' expr_value tEOL   { DEBUG_WriteMemory( &$2, $4 ); 
191                                              DEBUG_FreeExprMem(); }
192
193 pathname:
194       tIDENTIFIER                    { $$ = $1; }
195     | tPATH                          { $$ = $1; }
196
197 disassemble_command:
198       tDISASSEMBLE tEOL              { DEBUG_Disassemble( NULL, NULL, 10 ); }
199     | tDISASSEMBLE expr_addr tEOL    { DEBUG_Disassemble( & $2, NULL, 10 ); }
200     | tDISASSEMBLE expr_addr ',' expr_addr tEOL { DEBUG_Disassemble( & $2, & $4, 0 ); }
201
202 list_command:
203       tLIST tEOL               { DEBUG_List( NULL, NULL, 10 ); }
204     | tLIST '-' tEOL           { DEBUG_List( NULL, NULL, -10 ); }
205     | tLIST list_arg tEOL      { DEBUG_List( & $2, NULL, 10 ); }
206     | tLIST ',' list_arg tEOL  { DEBUG_List( NULL, & $3, -10 ); }
207     | tLIST list_arg ',' list_arg tEOL { DEBUG_List( & $2, & $4, 0 ); }
208
209 list_arg:
210       tNUM                     { $$.sourcefile = NULL; $$.line = $1; }
211     | pathname ':' tNUM        { $$.sourcefile = $1; $$.line = $3; }
212     | tIDENTIFIER              { DEBUG_GetFuncInfo( & $$, NULL, $1); }
213     | pathname ':' tIDENTIFIER { DEBUG_GetFuncInfo( & $$, $1, $3); }
214     | '*' expr_addr            { DEBUG_FindNearestSymbol( & $2, FALSE, NULL, 
215                                                         0, & $$ ); 
216                                              DEBUG_FreeExprMem(); }
217
218 x_command:
219       tEXAM expr_addr tEOL     { DEBUG_ExamineMemory( &$2, 1, 'x'); 
220                                              DEBUG_FreeExprMem(); }
221     | tEXAM tFORMAT expr_addr tEOL  { DEBUG_ExamineMemory( &$3, $2>>8, $2&0xff ); 
222                                              DEBUG_FreeExprMem(); }
223
224 print_command:
225       tPRINT expr_addr tEOL    { DEBUG_Print( &$2, 1, 0, 0 ); 
226                                              DEBUG_FreeExprMem(); }
227     | tPRINT tFORMAT expr_addr tEOL { DEBUG_Print( &$3, $2 >> 8, $2 & 0xff, 0 ); 
228                                              DEBUG_FreeExprMem(); }
229
230 break_command:
231       tBREAK '*' expr_addr tEOL { DEBUG_AddBreakpoint( &$3 ); 
232                                              DEBUG_FreeExprMem(); }
233     | tBREAK tIDENTIFIER tEOL  { DBG_ADDR addr;
234                                  if( DEBUG_GetSymbolValue($2, -1, &addr, TRUE) )
235                                    {
236                                      DEBUG_AddBreakpoint( &addr );
237                                    }
238                                  else
239                                    {
240                                      fprintf(stderr,"Unable to add breakpoint\n");
241                                    }
242                                 }
243     | tBREAK tIDENTIFIER ':' tNUM tEOL  { DBG_ADDR addr;
244                                  if( DEBUG_GetSymbolValue($2, $4, &addr, TRUE) )
245                                    {
246                                      DEBUG_AddBreakpoint( &addr );
247                                    }
248                                  else
249                                    {
250                                      fprintf(stderr,"Unable to add breakpoint\n");
251                                    }
252                                 }
253     | tBREAK tNUM tEOL         { struct name_hash *nh;
254                                  DBG_ADDR addr;
255                                  TDB *pTask = (TDB*)GlobalLock16( GetCurrentTask() );
256
257                                  addr.type = NULL;
258                                  addr.seg = CS_reg(&DEBUG_context);
259                                  addr.off = EIP_reg(&DEBUG_context);
260
261                                  if (ISV86(&DEBUG_context))
262                                      addr.seg |= (DWORD)(pTask?(pTask->hModule):0)<<16;
263                                  DBG_FIX_ADDR_SEG( &addr, CS_reg(&DEBUG_context) );
264                                  GlobalUnlock16( GetCurrentTask() );
265                                  DEBUG_FindNearestSymbol(&addr, TRUE,
266                                                          &nh, 0, NULL);
267                                  if( nh != NULL )
268                                    {
269                                      DEBUG_GetLineNumberAddr(nh, 
270                                                       $2, &addr, TRUE);
271                                      DEBUG_AddBreakpoint( &addr );
272                                    }
273                                  else
274                                    {
275                                      fprintf(stderr,"Unable to add breakpoint\n");
276                                    }
277                                }
278
279     | tBREAK tEOL              { DBG_ADDR addr;
280                                  TDB *pTask = (TDB*)GlobalLock16( GetCurrentTask() );
281
282                                  addr.type = NULL;
283                                  addr.seg = CS_reg(&DEBUG_context);
284                                  addr.off = EIP_reg(&DEBUG_context);
285
286                                  if (ISV86(&DEBUG_context))
287                                      addr.seg |= (DWORD)(pTask?(pTask->hModule):0)<<16;
288                                  GlobalUnlock16( GetCurrentTask() );
289                                  DEBUG_AddBreakpoint( &addr );
290                                }
291
292 info_command:
293       tINFO tBREAK tEOL         { DEBUG_InfoBreakpoints(); }
294     | tINFO tCLASS expr_value tEOL    { CLASS_DumpClass( (struct tagCLASS *)$3 ); 
295                                              DEBUG_FreeExprMem(); }
296     | tINFO tSHARE tEOL         { DEBUG_InfoShare(); }
297     | tINFO tMODULE expr_value tEOL   { NE_DumpModule( $3 ); 
298                                              DEBUG_FreeExprMem(); }
299     | tINFO tQUEUE expr_value tEOL    { QUEUE_DumpQueue( $3 ); 
300                                              DEBUG_FreeExprMem(); }
301     | tINFO tREGS tEOL          { DEBUG_InfoRegisters(); }
302     | tINFO tSEGMENTS expr_value tEOL { LDT_Print( SELECTOR_TO_ENTRY($3), 1 ); 
303                                              DEBUG_FreeExprMem(); }
304     | tINFO tSEGMENTS tEOL      { LDT_Print( 0, -1 ); }
305     | tINFO tSTACK tEOL         { DEBUG_InfoStack(); }
306     | tINFO tMAPS tEOL          { VIRTUAL_Dump(); }
307     | tINFO tWND expr_value tEOL      { WIN_DumpWindow( $3 ); 
308                                              DEBUG_FreeExprMem(); }
309     | tINFO tLOCAL tEOL         { DEBUG_InfoLocals(); }
310     | tINFO tDISPLAY tEOL       { DEBUG_InfoDisplay(); }
311
312 walk_command:
313       tWALK tCLASS tEOL         { CLASS_WalkClasses(); }
314     | tWALK tMODULE tEOL        { NE_WalkModules(); }
315     | tWALK tQUEUE tEOL         { QUEUE_WalkQueues(); }
316     | tWALK tWND tEOL           { WIN_WalkWindows( 0, 0 ); }
317     | tWALK tWND tNUM tEOL      { WIN_WalkWindows( $3, 0 ); }
318     | tWALK tPROCESS tEOL       { PROCESS_WalkProcess(); }
319     | tWALK tMODREF expr_value tEOL   { MODULE_WalkModref( $3 ); }
320
321
322 type_cast: 
323       '(' type_expr ')'         { $$ = $2; }
324
325 type_expr:
326       type_expr '*'             { $$ = DEBUG_FindOrMakePointerType($1); }
327     |  tINT                     { $$ = DEBUG_TypeCast(DT_BASIC, "int"); }
328     | tCHAR                     { $$ = DEBUG_TypeCast(DT_BASIC, "char"); }
329     | tLONG tINT                { $$ = DEBUG_TypeCast(DT_BASIC, "long int"); }
330     | tUNSIGNED tINT            { $$ = DEBUG_TypeCast(DT_BASIC, "unsigned int"); }
331     | tLONG tUNSIGNED tINT      { $$ = DEBUG_TypeCast(DT_BASIC, "long unsigned int"); }
332     | tLONG tLONG tINT          { $$ = DEBUG_TypeCast(DT_BASIC, "long long int"); }
333     | tLONG tLONG tUNSIGNED tINT { $$ = DEBUG_TypeCast(DT_BASIC, "long long unsigned int"); }
334     | tSHORT tINT               { $$ = DEBUG_TypeCast(DT_BASIC, "short int"); }
335     | tSHORT tUNSIGNED tINT     { $$ = DEBUG_TypeCast(DT_BASIC, "short unsigned int"); }
336     | tSIGNED tCHAR             { $$ = DEBUG_TypeCast(DT_BASIC, "signed char"); }
337     | tUNSIGNED tCHAR           { $$ = DEBUG_TypeCast(DT_BASIC, "unsigned char"); }
338     | tFLOAT                    { $$ = DEBUG_TypeCast(DT_BASIC, "float"); }
339     | tDOUBLE                   { $$ = DEBUG_TypeCast(DT_BASIC, "double"); }
340     | tLONG tDOUBLE             { $$ = DEBUG_TypeCast(DT_BASIC, "long double"); }
341     | tSTRUCT tIDENTIFIER       { $$ = DEBUG_TypeCast(DT_STRUCT, $2); }
342     | tUNION tIDENTIFIER        { $$ = DEBUG_TypeCast(DT_STRUCT, $2); }
343     | tENUM tIDENTIFIER         { $$ = DEBUG_TypeCast(DT_ENUM, $2); }
344
345 expr_addr:
346     expr                         { $$ = DEBUG_EvalExpr($1); }
347
348 expr_value:
349       expr        { DBG_ADDR addr  = DEBUG_EvalExpr($1);
350                     $$ = addr.off ? *(unsigned int *) addr.off : 0; }
351 /*
352  * The expr rule builds an expression tree.  When we are done, we call
353  * EvalExpr to evaluate the value of the expression.  The advantage of
354  * the two-step approach is that it is possible to save expressions for
355  * use in 'display' commands, and in conditional watchpoints.
356  */
357 expr:
358       tNUM                       { $$ = DEBUG_ConstExpr($1); }
359     | tSTRING                    { $$ = DEBUG_StringExpr($1); }
360     | tREG                       { $$ = DEBUG_RegisterExpr($1); }
361     | tIDENTIFIER                { $$ = DEBUG_SymbolExpr($1); }
362     | expr OP_DRF tIDENTIFIER    { $$ = DEBUG_StructPExpr($1, $3); } 
363     | expr '.' tIDENTIFIER       { $$ = DEBUG_StructExpr($1, $3); } 
364     | tIDENTIFIER '(' ')'        { $$ = DEBUG_CallExpr($1, 0); } 
365     | tIDENTIFIER '(' expr ')'   { $$ = DEBUG_CallExpr($1, 1, $3); } 
366     | tIDENTIFIER '(' expr ',' expr ')'  { $$ = DEBUG_CallExpr($1, 2, $3, 
367                                                                $5); } 
368     | tIDENTIFIER '(' expr ',' expr ',' expr ')'         { $$ = DEBUG_CallExpr($1, 3, $3, $5, $7); } 
369     | tIDENTIFIER '(' expr ',' expr ',' expr ',' expr ')'        { $$ = DEBUG_CallExpr($1, 3, $3, $5, $7, $9); } 
370     | tIDENTIFIER '(' expr ',' expr ',' expr ',' expr ',' expr ')'       { $$ = DEBUG_CallExpr($1, 3, $3, $5, $7, $9, $11); } 
371     | expr '[' expr ']'          { $$ = DEBUG_BinopExpr(EXP_OP_ARR, $1, $3); } 
372     | expr ':' expr              { $$ = DEBUG_BinopExpr(EXP_OP_SEG, $1, $3); } 
373     | expr OP_LOR expr           { $$ = DEBUG_BinopExpr(EXP_OP_LOR, $1, $3); }
374     | expr OP_LAND expr          { $$ = DEBUG_BinopExpr(EXP_OP_LAND, $1, $3); }
375     | expr '|' expr              { $$ = DEBUG_BinopExpr(EXP_OP_OR, $1, $3); }
376     | expr '&' expr              { $$ = DEBUG_BinopExpr(EXP_OP_AND, $1, $3); }
377     | expr '^' expr              { $$ = DEBUG_BinopExpr(EXP_OP_XOR, $1, $3); }
378     | expr OP_EQ expr            { $$ = DEBUG_BinopExpr(EXP_OP_EQ, $1, $3); }
379     | expr '>' expr              { $$ = DEBUG_BinopExpr(EXP_OP_GT, $1, $3); }
380     | expr '<' expr              { $$ = DEBUG_BinopExpr(EXP_OP_LT, $1, $3); }
381     | expr OP_GE expr            { $$ = DEBUG_BinopExpr(EXP_OP_GE, $1, $3); }
382     | expr OP_LE expr            { $$ = DEBUG_BinopExpr(EXP_OP_LE, $1, $3); }
383     | expr OP_NE expr            { $$ = DEBUG_BinopExpr(EXP_OP_NE, $1, $3); }
384     | expr OP_SHL expr           { $$ = DEBUG_BinopExpr(EXP_OP_SHL, $1, $3); }
385     | expr OP_SHR expr           { $$ = DEBUG_BinopExpr(EXP_OP_SHR, $1, $3); }
386     | expr '+' expr              { $$ = DEBUG_BinopExpr(EXP_OP_ADD, $1, $3); }
387     | expr '-' expr              { $$ = DEBUG_BinopExpr(EXP_OP_SUB, $1, $3); }
388     | expr '*' expr              { $$ = DEBUG_BinopExpr(EXP_OP_MUL, $1, $3); }
389     | expr '/' expr              { $$ = DEBUG_BinopExpr(EXP_OP_DIV, $1, $3); }
390     | expr '%' expr              { $$ = DEBUG_BinopExpr(EXP_OP_REM, $1, $3); }
391     | '-' expr %prec OP_SIGN     { $$ = DEBUG_UnopExpr(EXP_OP_NEG, $2); }
392     | '+' expr %prec OP_SIGN     { $$ = $2; }
393     | '!' expr                   { $$ = DEBUG_UnopExpr(EXP_OP_NOT, $2); }
394     | '~' expr                   { $$ = DEBUG_UnopExpr(EXP_OP_LNOT, $2); }
395     | '(' expr ')'               { $$ = $2; }
396     | '*' expr %prec OP_DEREF    { $$ = DEBUG_UnopExpr(EXP_OP_DEREF, $2); }
397     | '&' expr %prec OP_DEREF    { $$ = DEBUG_UnopExpr(EXP_OP_ADDR, $2); }
398     | type_cast expr %prec OP_DEREF { $$ = DEBUG_TypeCastExpr($1, $2); } 
399         
400 /*
401  * The lvalue rule builds an expression tree.  This is a limited form
402  * of expression that is suitable to be used as an lvalue.
403  */
404 lval_addr:
405     lval                         { $$ = DEBUG_EvalExpr($1); }
406
407 lval:
408       lvalue                     { $$ = $1; }
409     | '*' expr                   { $$ = DEBUG_UnopExpr(EXP_OP_FORCE_DEREF, $2); }
410         
411 lvalue:
412       tNUM                       { $$ = DEBUG_ConstExpr($1); }
413     | tREG                       { $$ = DEBUG_RegisterExpr($1); }
414     | tIDENTIFIER                { $$ = DEBUG_SymbolExpr($1); }
415     | lvalue OP_DRF tIDENTIFIER  { $$ = DEBUG_StructPExpr($1, $3); } 
416     | lvalue '.' tIDENTIFIER     { $$ = DEBUG_StructExpr($1, $3); } 
417     | lvalue '[' expr ']'        { $$ = DEBUG_BinopExpr(EXP_OP_ARR, $1, $3); } 
418         
419 %%
420
421 void 
422 issue_prompt(){
423 #ifdef DONT_USE_READLINE
424         fprintf(stderr,"Wine-dbg>");
425 #endif
426 }
427
428 void mode_command(int newmode)
429 {
430     if ((newmode == 16) || (newmode == 32)) dbg_mode = newmode;
431     else fprintf(stderr,"Invalid mode (use 16 or 32)\n");
432 }
433
434
435 /***********************************************************************
436  *           DEBUG_Main
437  *
438  * Debugger main loop.
439  */
440 static void DEBUG_Main( BOOL is_debug )
441 {
442     static int loaded_symbols = 0;
443     static BOOL frozen = FALSE;
444     static BOOL in_debugger = FALSE;
445     char SymbolTableFile[256];
446     int newmode;
447     BOOL ret_ok;
448 #ifdef YYDEBUG
449     yydebug = 0;
450 #endif
451
452     if (in_debugger)
453     {
454         fprintf( stderr, " inside debugger, exiting.\n" );
455         exit(1);
456     }
457     in_debugger = TRUE;
458     yyin = stdin;
459
460     DEBUG_SetBreakpoints( FALSE );
461
462     if (!is_debug)
463     {
464         if (IS_SELECTOR_SYSTEM(CS_reg(&DEBUG_context)))
465             fprintf( stderr, " in 32-bit code (0x%08lx).\n", EIP_reg(&DEBUG_context));
466         else
467             fprintf( stderr, " in 16-bit code (%04x:%04lx).\n",
468                      (WORD)CS_reg(&DEBUG_context), EIP_reg(&DEBUG_context) );
469     }
470
471     if (!loaded_symbols)
472     {
473         loaded_symbols++;
474
475         if ( !frozen )
476         {
477             /* Don't freeze thread currently holding the X crst! */
478             EnterCriticalSection( &X11DRV_CritSection );
479             CLIENT_DebuggerRequest( DEBUGGER_FREEZE_ALL );
480             LeaveCriticalSection( &X11DRV_CritSection );
481             frozen = TRUE;
482         }
483
484 #ifdef DBG_need_heap
485         /*
486          * Initialize the debugger heap.
487          */
488         dbg_heap = HeapCreate(HEAP_NO_SERIALIZE, 0x1000, 0x8000000); /* 128MB */
489 #endif
490
491         /*
492          * Initialize the type handling stuff.
493          */
494         DEBUG_InitTypes();
495
496         /*
497          * In some cases we can read the stabs information directly
498          * from the executable.  If this is the case, we don't need
499          * to bother with trying to read a symbol file, as the stabs
500          * also have line number and local variable information.
501          * As long as gcc is used for the compiler, stabs will
502          * be the default.  On SVr4, DWARF could be used, but we
503          * don't grok that yet, and in this case we fall back to using
504          * the wine.sym file.
505          */
506         if( DEBUG_ReadExecutableDbgInfo() == FALSE )
507         {
508             char *symfilename = "wine.sym";
509             struct stat statbuf;
510             if (-1 == stat(symfilename, &statbuf) )
511                 symfilename = LIBDIR "wine.sym";
512
513             PROFILE_GetWineIniString( "wine", "SymbolTableFile", symfilename,
514                                      SymbolTableFile, sizeof(SymbolTableFile));
515             DEBUG_ReadSymbolTable( SymbolTableFile );
516         }
517
518         DEBUG_LoadEntryPoints();
519         DEBUG_ProcessDeferredDebug();
520     }
521
522 #if 0
523     fprintf(stderr, "Entering debugger  PC=%x, mode=%d, count=%d\n",
524             EIP_reg(&DEBUG_context),
525             dbg_exec_mode, dbg_exec_count);
526     
527     sleep(1);
528 #endif
529
530     if (!is_debug || !DEBUG_ShouldContinue( dbg_exec_mode, &dbg_exec_count ))
531     {
532         DBG_ADDR addr;
533         TDB *pTask = (TDB*)GlobalLock16( GetCurrentTask() );
534
535         addr.seg = CS_reg(&DEBUG_context);
536         addr.off = EIP_reg(&DEBUG_context);
537         if (ISV86(&DEBUG_context)) addr.seg |= (DWORD)(pTask?(pTask->hModule):0)<<16;
538         addr.type = NULL;
539         DBG_FIX_ADDR_SEG( &addr, 0 );
540
541         GlobalUnlock16( GetCurrentTask() );
542
543         if ( !frozen )
544         {
545             /* Don't freeze thread currently holding the X crst! */
546             EnterCriticalSection( &X11DRV_CritSection );
547             CLIENT_DebuggerRequest( DEBUGGER_FREEZE_ALL );
548             LeaveCriticalSection( &X11DRV_CritSection );
549             frozen = TRUE;
550         }
551
552         /* Put the display in a correct state */
553         USER_Driver->pBeginDebugging();
554
555         newmode = ISV86(&DEBUG_context) ? 16 : IS_SELECTOR_32BIT(addr.seg) ? 32 : 16;
556         if (newmode != dbg_mode)
557             fprintf(stderr,"In %d bit mode.\n", dbg_mode = newmode);
558
559         DEBUG_DoDisplay();
560
561         if (!is_debug)  /* This is a real crash, dump some info */
562         {
563             DEBUG_InfoRegisters();
564             DEBUG_InfoStack();
565             if (dbg_mode == 16)
566             {
567                 LDT_Print( SELECTOR_TO_ENTRY(DS_reg(&DEBUG_context)), 1 );
568                 if (ES_reg(&DEBUG_context) != DS_reg(&DEBUG_context))
569                     LDT_Print( SELECTOR_TO_ENTRY(ES_reg(&DEBUG_context)), 1 );
570             }
571             LDT_Print( SELECTOR_TO_ENTRY(FS_reg(&DEBUG_context)), 1 );
572             DEBUG_BackTrace();
573         }
574         else
575         {
576           /*
577            * Do a quiet backtrace so that we have an idea of what the situation
578            * is WRT the source files.
579            */
580             DEBUG_SilentBackTrace();
581         }
582
583         if (!is_debug ||
584             (dbg_exec_mode == EXEC_STEPI_OVER) ||
585             (dbg_exec_mode == EXEC_STEPI_INSTR))
586         {
587             /* Show where we crashed */
588             curr_frame = 0;
589             DEBUG_PrintAddress( &addr, dbg_mode, TRUE );
590             fprintf(stderr,":  ");
591             if (DBG_CHECK_READ_PTR( &addr, 1 ))
592               {
593                 DEBUG_Disasm( &addr, TRUE );
594                 fprintf(stderr,"\n");
595               }
596         }
597
598         ret_ok = 0;
599         do
600         {
601             issue_prompt();
602             yyparse();
603             flush_symbols();
604             addr.seg = CS_reg(&DEBUG_context) | (addr.seg&0xffff0000);
605             addr.off = EIP_reg(&DEBUG_context);
606             DBG_FIX_ADDR_SEG( &addr, 0 );
607             ret_ok = DEBUG_ValidateRegisters();
608             if (ret_ok) ret_ok = DBG_CHECK_READ_PTR( &addr, 1 );
609         } while (!ret_ok);
610     }
611
612     dbg_exec_mode = DEBUG_RestartExecution( dbg_exec_mode, dbg_exec_count );
613     /*
614      * This will have gotten absorbed into the breakpoint info
615      * if it was used.  Otherwise it would have been ignored.
616      * In any case, we don't mess with it any more.
617      */
618     if ((dbg_exec_mode == EXEC_CONT) || (dbg_exec_mode == EXEC_PASS))
619       {
620         dbg_exec_count = 0;
621
622         if ( frozen )
623         {
624             CLIENT_DebuggerRequest( DEBUGGER_UNFREEZE_ALL );
625             frozen = FALSE;
626         }
627       }
628
629     in_debugger = FALSE;
630
631     USER_Driver->pEndDebugging();
632 }
633
634
635 DWORD wine_debugger( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
636 {
637     BOOL is_debug = FALSE;
638
639     if (first_chance && !Options.debug) return 0;  /* pass to app first */
640
641     switch(rec->ExceptionCode)
642     {
643     case EXCEPTION_BREAKPOINT:
644     case EXCEPTION_SINGLE_STEP:
645         is_debug = TRUE;
646         break;
647     case CONTROL_C_EXIT:
648         if (!Options.debug) exit(0);
649         break;
650     }
651
652     if (!is_debug)
653     {
654         /* print some infos */
655         fprintf( stderr, "%s: ",
656                  first_chance ? "First chance exception" : "Unhandled exception" );
657         switch(rec->ExceptionCode)
658         {
659         case EXCEPTION_INT_DIVIDE_BY_ZERO:
660             fprintf( stderr, "divide by zero" );
661             break;
662         case EXCEPTION_INT_OVERFLOW:
663             fprintf( stderr, "overflow" );
664             break;
665         case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
666             fprintf( stderr, "array bounds " );
667             break;
668         case EXCEPTION_ILLEGAL_INSTRUCTION:
669             fprintf( stderr, "illegal instruction" );
670             break;
671         case EXCEPTION_STACK_OVERFLOW:
672             fprintf( stderr, "stack overflow" );
673             break;
674         case EXCEPTION_PRIV_INSTRUCTION:
675             fprintf( stderr, "priviledged instruction" );
676             break;
677         case EXCEPTION_ACCESS_VIOLATION:
678             if (rec->NumberParameters == 2)
679                 fprintf( stderr, "page fault on %s access to 0x%08lx", 
680                          rec->ExceptionInformation[0] ? "write" : "read",
681                          rec->ExceptionInformation[1] );
682             else
683                 fprintf( stderr, "page fault" );
684             break;
685         case EXCEPTION_DATATYPE_MISALIGNMENT:
686             fprintf( stderr, "Alignment" );
687             break;
688         case CONTROL_C_EXIT:
689             fprintf( stderr, "^C" );
690             break;
691         default:
692             fprintf( stderr, "%08lx", rec->ExceptionCode );
693             break;
694         }
695     }
696
697     DEBUG_context = *context;
698     DEBUG_Main( is_debug );
699     *context = DEBUG_context;
700     return (dbg_exec_mode == EXEC_PASS) ? 0 : DBG_CONTINUE;
701 }
702
703 int yyerror(char * s)
704 {
705         fprintf(stderr,"%s\n", s);
706         return 0;
707 }