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