Added LGPL standard comment, and copyright notices where necessary.
[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  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "debugger.h"
28 #include "winbase.h"
29
30 #ifdef __i386__
31 #define IS_VM86_MODE() (DEBUG_context.EFlags & V86_FLAG)
32 #endif
33
34 static  void    DEBUG_Die(const char* msg)
35 {
36    DEBUG_Printf(DBG_CHN_MESG, msg);
37    exit(1);
38 }
39
40 void*   DEBUG_XMalloc(size_t size)
41 {
42    void *res = malloc(size ? size : 1);
43    if (res == NULL)
44       DEBUG_Die("Memory exhausted.\n");
45    memset(res, 0, size);
46    return res;
47 }
48
49 void* DEBUG_XReAlloc(void *ptr, size_t size)
50 {
51    void* res = realloc(ptr, size);
52    if ((res == NULL) && size)
53       DEBUG_Die("Memory exhausted.\n");
54    return res;
55 }
56
57 char*   DEBUG_XStrDup(const char *str)
58 {
59    char *res = strdup(str);
60    if (!res)
61       DEBUG_Die("Memory exhausted.\n");
62    return res;
63 }
64
65 enum dbg_mode DEBUG_GetSelectorType( WORD sel )
66 {
67 #ifdef __i386__
68     LDT_ENTRY   le;
69
70     if (IS_VM86_MODE()) return MODE_VM86;
71     if (sel == 0) return MODE_32;
72     if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, sel, &le)) 
73         return le.HighWord.Bits.Default_Big ? MODE_32 : MODE_16;
74     /* selector doesn't exist */
75     return MODE_INVALID;
76 #else
77     return MODE_32;
78 #endif
79 }
80 #ifdef __i386__
81 void DEBUG_FixAddress( DBG_ADDR *addr, DWORD def) 
82 {
83    if (addr->seg == 0xffffffff) addr->seg = def;
84    if (DEBUG_IsSelectorSystem(addr->seg)) addr->seg = 0;
85 }
86
87 /* Determine if sel is a system selector (i.e. not managed by Wine) */
88 BOOL    DEBUG_IsSelectorSystem(WORD sel)
89 {
90     if (IS_VM86_MODE()) return FALSE;  /* no system selectors in vm86 mode */
91     return !(sel & 4) || ((sel >> 3) < 17);
92 }
93 #endif /* __i386__ */
94
95 DWORD DEBUG_ToLinear( const DBG_ADDR *addr )
96 {
97 #ifdef __i386__
98    LDT_ENTRY    le;
99    
100    if (IS_VM86_MODE()) return (DWORD)(LOWORD(addr->seg) << 4) + addr->off;
101
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_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  *           DEBUG_GrabAddress
211  *
212  * Get the address from a value
213  */
214 BOOL DEBUG_GrabAddress( DBG_VALUE* value, BOOL fromCode )
215 {
216     assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
217
218 #ifdef __i386__
219     DEBUG_FixAddress( &value->addr, 
220                       (fromCode) ? DEBUG_context.SegCs : DEBUG_context.SegDs);
221 #endif
222
223     /*
224      * Dereference pointer to get actual memory address we need to be
225      * reading.  We will use the same segment as what we have already,
226      * and hope that this is a sensible thing to do.
227      */
228     if (value->type != NULL) {
229         if (value->type == DEBUG_GetBasicType(DT_BASIC_CONST_INT)) {
230             /*
231              * We know that we have the actual offset stored somewhere
232              * else in 32-bit space.  Grab it, and we
233              * should be all set.
234              */
235             unsigned int  seg2 = value->addr.seg;
236             value->addr.seg = 0;
237             value->addr.off = DEBUG_GetExprValue(value, NULL);
238             value->addr.seg = seg2;
239         } else {
240             struct datatype     * testtype;
241
242             if (DEBUG_TypeDerefPointer(value, &testtype) == 0)
243                 return FALSE;
244             if (testtype != NULL || value->type == DEBUG_GetBasicType(DT_BASIC_CONST_INT))
245                 value->addr.off = DEBUG_GetExprValue(value, NULL);
246         }
247     } else if (!value->addr.seg && !value->addr.off) {
248         DEBUG_Printf(DBG_CHN_MESG,"Invalid expression\n");
249         return FALSE;
250     }
251     return TRUE;
252 }
253
254 /***********************************************************************
255  *           DEBUG_ExamineMemory
256  *
257  * Implementation of the 'x' command.
258  */
259 void DEBUG_ExamineMemory( const DBG_VALUE *_value, int count, char format )
260 {
261     DBG_VALUE             value = *_value;
262     int                   i;
263     unsigned char       * pnt;
264
265     if (!DEBUG_GrabAddress(&value, (format == 'i'))) return;
266
267     if (format != 'i' && count > 1)
268     {
269         DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
270         DEBUG_Printf(DBG_CHN_MESG,": ");
271     }
272
273     pnt = (void*)DEBUG_ToLinear( &value.addr );
274
275     switch(format)
276     {
277         case 'u': {
278                 WCHAR wch;
279                 if (count == 1) count = 256;
280                 while (count--)
281                 {
282                     if (!DEBUG_READ_MEM_VERBOSE(pnt, &wch, sizeof(wch)) || !wch)
283                        break;
284                     pnt += sizeof(wch);
285                     DEBUG_Printf(DBG_CHN_MESG, "%c", (char)wch);
286                 }
287                 DEBUG_Printf(DBG_CHN_MESG,"\n");
288                 return;
289             }
290           case 's': {
291                 char ch;
292
293                 if (count == 1) count = 256;
294                 while (count--)
295                 {
296                     if (!DEBUG_READ_MEM_VERBOSE(pnt, &ch, sizeof(ch)) || !ch)
297                        break;
298                     pnt++;
299                     DEBUG_Output(DBG_CHN_MESG, &ch, 1);
300                 }
301                 DEBUG_Printf(DBG_CHN_MESG,"\n");
302                 return;
303           }
304         case 'i':
305                 while (count-- && DEBUG_DisassembleInstruction( &value.addr ));
306                 return;
307 #define DO_DUMP2(_t,_l,_f,_vv) { \
308                 _t _v; \
309                 for(i=0; i<count; i++) { \
310                     if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
311                     DEBUG_Printf(DBG_CHN_MESG,_f,(_vv)); \
312                     pnt += sizeof(_t); value.addr.off += sizeof(_t); \
313                     if ((i % (_l)) == (_l)-1) { \
314                         DEBUG_Printf(DBG_CHN_MESG,"\n"); \
315                         DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
316                         DEBUG_Printf(DBG_CHN_MESG,": ");\
317                     } \
318                 } \
319                 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
320         } \
321         return
322 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v) 
323
324         case 'x': DO_DUMP(int, 4, " %8.8x");
325         case 'd': DO_DUMP(unsigned int, 4, " %10d");    
326         case 'w': DO_DUMP(unsigned short, 8, " %04x");
327         case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
328         case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);
329         }
330 }