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