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