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