Upgraded configure script to use autoconf 2.50 features, and renamed
[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 <stdlib.h>
11 #include <string.h>
12
13 #include "debugger.h"
14 #include "winbase.h"
15
16 #ifdef __i386__
17 #define IS_VM86_MODE() (DEBUG_context.EFlags & V86_FLAG)
18 #endif
19
20 static  void    DEBUG_Die(const char* msg)
21 {
22    DEBUG_Printf(DBG_CHN_MESG, msg);
23    exit(1);
24 }
25
26 void*   DEBUG_XMalloc(size_t size)
27 {
28    void *res = malloc(size ? size : 1);
29    if (res == NULL)
30       DEBUG_Die("Memory exhausted.\n");
31    memset(res, 0, size);
32    return res;
33 }
34
35 void* DEBUG_XReAlloc(void *ptr, size_t size)
36 {
37    void* res = realloc(ptr, size);
38    if ((res == NULL) && size)
39       DEBUG_Die("Memory exhausted.\n");
40    return res;
41 }
42
43 char*   DEBUG_XStrDup(const char *str)
44 {
45    char *res = strdup(str);
46    if (!res)
47       DEBUG_Die("Memory exhausted.\n");
48    return res;
49 }
50
51 enum dbg_mode DEBUG_GetSelectorType( WORD sel )
52 {
53 #ifdef __i386__
54     LDT_ENTRY   le;
55
56     if (IS_VM86_MODE()) return MODE_VM86;
57     if (sel == 0) return MODE_32;
58     if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, sel, &le)) 
59         return le.HighWord.Bits.Default_Big ? MODE_32 : MODE_16;
60     /* selector doesn't exist */
61     return MODE_INVALID;
62 #else
63     return MODE_32;
64 #endif
65 }
66 #ifdef __i386__
67 void DEBUG_FixAddress( DBG_ADDR *addr, DWORD def) 
68 {
69    if (addr->seg == 0xffffffff) addr->seg = def;
70    if (DEBUG_IsSelectorSystem(addr->seg)) addr->seg = 0;
71 }
72
73 /* Determine if sel is a system selector (i.e. not managed by Wine) */
74 BOOL    DEBUG_IsSelectorSystem(WORD sel)
75 {
76     if (IS_VM86_MODE()) return FALSE;  /* no system selectors in vm86 mode */
77     return !(sel & 4) || ((sel >> 3) < 17);
78 }
79 #endif /* __i386__ */
80
81 DWORD DEBUG_ToLinear( const DBG_ADDR *addr )
82 {
83 #ifdef __i386__
84    LDT_ENTRY    le;
85    
86    if (IS_VM86_MODE()) return (DWORD)(LOWORD(addr->seg) << 4) + addr->off;
87
88    if (DEBUG_IsSelectorSystem(addr->seg))
89       return addr->off;
90    
91    if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, addr->seg, &le)) {
92       return (le.HighWord.Bits.BaseHi << 24) + (le.HighWord.Bits.BaseMid << 16) + le.BaseLow + addr->off;
93    }
94    return 0;
95 #else
96    return addr->off;
97 #endif
98 }
99
100 void DEBUG_GetCurrentAddress( DBG_ADDR *addr )
101 {
102 #ifdef __i386__
103     addr->seg  = DEBUG_context.SegCs;
104
105     if (DEBUG_IsSelectorSystem(addr->seg))
106        addr->seg = 0;
107     addr->off  = DEBUG_context.Eip;
108 #elif defined(__sparc__)
109          addr->seg = 0;
110     addr->off = DEBUG_context.pc;
111 #else
112 #       error You must define GET_IP for this CPU
113 #endif
114 }
115
116 void    DEBUG_InvalAddr( const DBG_ADDR* addr )
117 {
118    DEBUG_Printf(DBG_CHN_MESG,"*** Invalid address ");
119    DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
120    DEBUG_Printf(DBG_CHN_MESG,"\n");
121    if (DBG_IVAR(ExtDbgOnInvalidAddress)) DEBUG_ExternalDebugger();
122 }
123
124 void    DEBUG_InvalLinAddr( void* addr )
125 {
126    DBG_ADDR address;
127
128    address.seg = 0;
129    address.off = (unsigned long)addr;
130    DEBUG_InvalAddr( &address );
131 }
132
133 /***********************************************************************
134  *           DEBUG_ReadMemory
135  *
136  * Read a memory value.
137  */
138 /* FIXME: this function is now getting closer and closer to 
139  * DEBUG_ExprGetValue. They should be merged...
140  */
141 int DEBUG_ReadMemory( const DBG_VALUE* val )
142 {
143     int         value = 0; /* to clear any unused byte */
144     int         os = DEBUG_GetObjectSize(val->type);
145
146     assert(sizeof(value) >= os);
147
148     /* FIXME: only works on little endian systems */
149
150     if (val->cookie == DV_TARGET) {
151        DBG_ADDR addr = val->addr;
152        void*    lin;
153
154 #ifdef __i386__
155        DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
156 #endif
157        lin = (void*)DEBUG_ToLinear( &addr );
158        
159        DEBUG_READ_MEM_VERBOSE(lin, &value, os);
160     } else {
161        if (val->addr.off)
162           memcpy(&value, (void*)val->addr.off, os);
163     }
164     return value;
165 }
166
167
168 /***********************************************************************
169  *           DEBUG_WriteMemory
170  *
171  * Store a value in memory.
172  */
173 void DEBUG_WriteMemory( const DBG_VALUE* val, int value )
174 {
175     int         os = DEBUG_GetObjectSize(val->type);
176
177     assert(sizeof(value) >= os);
178
179     /* FIXME: only works on little endian systems */
180
181     if (val->cookie == DV_TARGET) {
182        DBG_ADDR addr = val->addr;
183        void*    lin;
184
185 #ifdef __i386__
186        DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
187 #endif
188        lin = (void*)DEBUG_ToLinear( &addr );
189        DEBUG_WRITE_MEM_VERBOSE(lin, &value, os);
190     } else {
191        memcpy((void*)val->addr.off, &value, os);
192     }
193 }
194
195 /***********************************************************************
196  *           DEBUG_GrabAddress
197  *
198  * Get the address from a value
199  */
200 BOOL DEBUG_GrabAddress( DBG_VALUE* value, BOOL fromCode )
201 {
202     assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
203
204 #ifdef __i386__
205     DEBUG_FixAddress( &value->addr, 
206                       (fromCode) ? DEBUG_context.SegCs : DEBUG_context.SegDs);
207 #endif
208
209     /*
210      * Dereference pointer to get actual memory address we need to be
211      * reading.  We will use the same segment as what we have already,
212      * and hope that this is a sensible thing to do.
213      */
214     if (value->type != NULL) {
215         if (value->type == DEBUG_GetBasicType(DT_BASIC_CONST_INT)) {
216             /*
217              * We know that we have the actual offset stored somewhere
218              * else in 32-bit space.  Grab it, and we
219              * should be all set.
220              */
221             unsigned int  seg2 = value->addr.seg;
222             value->addr.seg = 0;
223             value->addr.off = DEBUG_GetExprValue(value, NULL);
224             value->addr.seg = seg2;
225         } else {
226             struct datatype     * testtype;
227
228             if (DEBUG_TypeDerefPointer(value, &testtype) == 0)
229                 return FALSE;
230             if (testtype != NULL || value->type == DEBUG_GetBasicType(DT_BASIC_CONST_INT))
231                 value->addr.off = DEBUG_GetExprValue(value, NULL);
232         }
233     } else if (!value->addr.seg && !value->addr.off) {
234         DEBUG_Printf(DBG_CHN_MESG,"Invalid expression\n");
235         return FALSE;
236     }
237     return TRUE;
238 }
239
240 /***********************************************************************
241  *           DEBUG_ExamineMemory
242  *
243  * Implementation of the 'x' command.
244  */
245 void DEBUG_ExamineMemory( const DBG_VALUE *_value, int count, char format )
246 {
247     DBG_VALUE             value = *_value;
248     int                   i;
249     unsigned char       * pnt;
250
251     if (!DEBUG_GrabAddress(&value, (format == 'i'))) return;
252
253     if (format != 'i' && count > 1)
254     {
255         DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
256         DEBUG_Printf(DBG_CHN_MESG,": ");
257     }
258
259     pnt = (void*)DEBUG_ToLinear( &value.addr );
260
261     switch(format)
262     {
263         case 'u': {
264                 WCHAR wch;
265                 if (count == 1) count = 256;
266                 while (count--)
267                 {
268                     if (!DEBUG_READ_MEM_VERBOSE(pnt, &wch, sizeof(wch)) || !wch)
269                        break;
270                     pnt += sizeof(wch);
271                     DEBUG_Printf(DBG_CHN_MESG, "%c", (char)wch);
272                 }
273                 DEBUG_Printf(DBG_CHN_MESG,"\n");
274                 return;
275             }
276           case 's': {
277                 char ch;
278
279                 if (count == 1) count = 256;
280                 while (count--)
281                 {
282                     if (!DEBUG_READ_MEM_VERBOSE(pnt, &ch, sizeof(ch)) || !ch)
283                        break;
284                     pnt++;
285                     DEBUG_Output(DBG_CHN_MESG, &ch, 1);
286                 }
287                 DEBUG_Printf(DBG_CHN_MESG,"\n");
288                 return;
289           }
290         case 'i':
291                 while (count-- && DEBUG_DisassembleInstruction( &value.addr ));
292                 return;
293 #define DO_DUMP2(_t,_l,_f,_vv) { \
294                 _t _v; \
295                 for(i=0; i<count; i++) { \
296                     if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
297                     DEBUG_Printf(DBG_CHN_MESG,_f,(_vv)); \
298                     pnt += sizeof(_t); value.addr.off += sizeof(_t); \
299                     if ((i % (_l)) == (_l)-1) { \
300                         DEBUG_Printf(DBG_CHN_MESG,"\n"); \
301                         DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
302                         DEBUG_Printf(DBG_CHN_MESG,": ");\
303                     } \
304                 } \
305                 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
306         } \
307         return
308 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v) 
309
310         case 'x': DO_DUMP(int, 4, " %8.8x");
311         case 'd': DO_DUMP(unsigned int, 4, " %10d");    
312         case 'w': DO_DUMP(unsigned short, 8, " %04x");
313         case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
314         case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);
315         }
316 }