improved exception handling
[wine] / debugger / memory.c
1 /*
2  * Debugger memory handling
3  *
4  * Copyright 1993 Eric Youngdale
5  * Copyright 1995 Alexandre Julliard
6  * Copyright 2000 Eric Pouech
7  */
8
9 #include "config.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "debugger.h"
15 #include "miscemu.h"
16 #include "winbase.h"
17
18 #ifdef __i386__
19 #include "wine/winbase16.h"
20
21 #define DBG_V86_MODULE(seg) ((seg)>>16)
22 #define IS_SELECTOR_V86(seg) DBG_V86_MODULE(seg)
23
24 static  void    DEBUG_Die(const char* msg)
25 {
26    fprintf(stderr, msg);
27    exit(1);
28 }
29
30 void*   DEBUG_XMalloc(size_t size)
31 {
32    void *res = malloc(size ? size : 1);
33    if (res == NULL)
34       DEBUG_Die("Memory exhausted.\n");
35    memset(res, 0, size);
36    return res;
37 }
38
39 void* DEBUG_XReAlloc(void *ptr, size_t size)
40 {
41    void* res = realloc(ptr, size);
42    if ((res == NULL) && size)
43       DEBUG_Die("Memory exhausted.\n");
44    return res;
45 }
46
47 char*   DEBUG_XStrDup(const char *str)
48 {
49    char *res = strdup(str);
50    if (!res)
51       DEBUG_Die("Memory exhausted.\n");
52    return res;
53 }
54
55 void DEBUG_FixAddress( DBG_ADDR *addr, DWORD def) 
56 {
57    if (addr->seg == 0xffffffff) addr->seg = def;
58    if (!IS_SELECTOR_V86(addr->seg) && DEBUG_IsSelectorSystem(addr->seg)) addr->seg = 0;
59 }
60
61 BOOL  DEBUG_FixSegment( DBG_ADDR* addr )
62 {
63    /* V86 mode ? */
64    if (DEBUG_context.EFlags & V86_FLAG) {
65       addr->seg |= (DWORD)(GetExePtr(GetCurrentTask())) << 16;
66       return TRUE;
67    }
68    return FALSE;
69 }
70
71 DWORD DEBUG_ToLinear( const DBG_ADDR *addr )
72 {
73    LDT_ENTRY    le;
74    
75    if (IS_SELECTOR_V86(addr->seg))
76       return (DWORD) DOSMEM_MemoryBase(DBG_V86_MODULE(addr->seg)) + (((addr->seg)&0xFFFF)<<4) + addr->off;
77    if (DEBUG_IsSelectorSystem(addr->seg))
78       return addr->off;
79    
80    if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, addr->seg, &le)) {
81       return (le.HighWord.Bits.BaseHi << 24) + (le.HighWord.Bits.BaseMid << 16) + le.BaseLow + addr->off;
82    }
83    return 0;
84 }
85
86 int     DEBUG_GetSelectorType( WORD sel )
87 {
88     LDT_ENTRY   le;
89
90     if (sel == 0)
91         return 32;
92     if (IS_SELECTOR_V86(sel))
93         return 16;
94     if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, sel, &le)) 
95         return le.HighWord.Bits.Default_Big ? 32 : 16;
96          /* selector doesn't exist */
97     return 0;
98 }
99
100 /* Determine if sel is a system selector (i.e. not managed by Wine) */
101 BOOL    DEBUG_IsSelectorSystem(WORD sel)
102 {
103    return !(sel & 4) || (((sel & 0xFFFF) >> 3) < 17);
104 }
105 #endif /* __i386__ */
106
107 void DEBUG_GetCurrentAddress( DBG_ADDR *addr )
108 {
109 #ifdef __i386__
110     addr->seg  = DEBUG_context.SegCs;
111
112     if (!DEBUG_FixSegment( addr ) && DEBUG_IsSelectorSystem(addr->seg)) 
113        addr->seg = 0;
114     addr->off  = DEBUG_context.Eip;
115 #else
116     addr->seg  = 0;
117     addr->off  = 0;
118 #endif
119 }
120
121 void    DEBUG_InvalLinAddr( void* addr )
122 {
123    DBG_ADDR address;
124
125    address.seg = 0;
126    address.off = (unsigned long)addr;
127
128    fprintf(stderr,"*** Invalid address ");
129    DEBUG_PrintAddress(&address, DEBUG_CurrThread->dbg_mode, FALSE);
130    fprintf(stderr,"\n");
131 }
132
133 /***********************************************************************
134  *           DEBUG_ReadMemory
135  *
136  * Read a memory value.
137  */
138 int DEBUG_ReadMemory( const DBG_ADDR *address )
139 {
140     DBG_ADDR    addr = *address;
141     void*       lin;
142     int         value;
143         
144     DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
145     lin = (void*)DEBUG_ToLinear( &addr );
146     if (!DEBUG_READ_MEM_VERBOSE(lin, &value, sizeof(value)))
147        value = 0;
148     return value;
149 }
150
151
152 /***********************************************************************
153  *           DEBUG_WriteMemory
154  *
155  * Store a value in memory.
156  */
157 void DEBUG_WriteMemory( const DBG_ADDR *address, int value )
158 {
159     DBG_ADDR    addr = *address;
160     void*       lin;
161
162     DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
163     lin = (void*)DEBUG_ToLinear( &addr );
164     DEBUG_WRITE_MEM_VERBOSE(lin, &value, sizeof(value));
165 }
166
167
168 /***********************************************************************
169  *           DEBUG_ExamineMemory
170  *
171  * Implementation of the 'x' command.
172  */
173 void DEBUG_ExamineMemory( const DBG_VALUE *_value, int count, char format )
174 {
175     DBG_VALUE             value = *_value;
176     int                   i;
177     unsigned char       * pnt;
178     struct datatype     * testtype;
179
180     assert(_value->cookie == DV_TARGET || _value->cookie == DV_HOST);
181
182     DEBUG_FixAddress( &value.addr, 
183                       (format == 'i') ?
184                       DEBUG_context.SegCs : 
185                       DEBUG_context.SegDs );
186
187     /*
188      * Dereference pointer to get actual memory address we need to be
189      * reading.  We will use the same segment as what we have already,
190      * and hope that this is a sensible thing to do.
191      */
192     if( value.type != NULL )
193       {
194         if( value.type == DEBUG_TypeIntConst )
195           {
196             /*
197              * We know that we have the actual offset stored somewhere
198              * else in 32-bit space.  Grab it, and we
199              * should be all set.
200              */
201             unsigned int  seg2 = value.addr.seg;
202             value.addr.seg = 0;
203             value.addr.off = DEBUG_GetExprValue(&value, NULL);
204             value.addr.seg = seg2;
205           }
206         else
207           {
208             if (DEBUG_TypeDerefPointer(&value, &testtype) == 0)
209               return;
210             if( testtype != NULL || value.type == DEBUG_TypeIntConst )
211               {
212                 value.addr.off = DEBUG_GetExprValue(&value, NULL);
213               }
214           }
215       }
216     else if (!value.addr.seg && !value.addr.off)
217     {
218         fprintf(stderr,"Invalid expression\n");
219         return;
220     }
221
222     if (format != 'i' && count > 1)
223     {
224         DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
225         fprintf(stderr,": ");
226     }
227
228     pnt = (void*)DEBUG_ToLinear( &value.addr );
229
230     switch(format)
231     {
232         case 'u': {
233                 WCHAR wch;
234                 if (count == 1) count = 256;
235                 while (count--)
236                 {
237                     if (!DEBUG_READ_MEM_VERBOSE(pnt, &wch, sizeof(wch)))
238                        break;
239                     pnt += sizeof(wch);
240                     fputc( (char)wch, stderr );
241                 }
242                 fprintf(stderr,"\n");
243                 return;
244             }
245           case 's': {
246                 char ch;
247
248                 if (count == 1) count = 256;
249                 while (count--)
250                 {
251                     if (!DEBUG_READ_MEM_VERBOSE(pnt, &ch, sizeof(ch)))
252                        break;
253                     pnt++;
254                     fputc( ch, stderr );
255                 }
256                 fprintf(stderr,"\n");
257                 return;
258           }
259         case 'i':
260                 while (count--)
261                 {
262                     DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, TRUE );
263                     fprintf(stderr,": ");
264                     DEBUG_Disasm( &value.addr, TRUE );
265                     fprintf(stderr,"\n");
266                 }
267                 return;
268 #define DO_DUMP2(_t,_l,_f,_vv) { \
269                 _t _v; \
270                 for(i=0; i<count; i++) { \
271                     if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
272                     fprintf(stderr,_f,(_vv)); \
273                     pnt += sizeof(_t); value.addr.off += sizeof(_t); \
274                     if ((i % (_l)) == (_l)-1) { \
275                         fprintf(stderr,"\n"); \
276                         DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
277                         fprintf(stderr,": ");\
278                     } \
279                 } \
280                 fprintf(stderr,"\n"); \
281         } \
282         return
283 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v) 
284
285         case 'x': DO_DUMP(int, 4, " %8.8x");
286         case 'd': DO_DUMP(unsigned int, 4, " %10d");    
287         case 'w': DO_DUMP(unsigned short, 8, " %04x");
288         case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
289         case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);
290         }
291 }