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