Well, SetParent doesn't work properly at all, but at least it must preserve
[wine.git] / debugger / memory.c
blobd1feb935ba1022db31335bcf07ff68b3fa28d34e
1 /*
2 * Debugger memory handling
4 * Copyright 1993 Eric Youngdale
5 * Copyright 1995 Alexandre Julliard
6 * Copyright 2000 Eric Pouech
7 */
9 #include "config.h"
10 #include <stdlib.h>
11 #include <string.h>
13 #include "debugger.h"
14 #include "miscemu.h"
15 #include "winbase.h"
17 #ifdef __i386__
18 #include "wine/winbase16.h"
20 #define DBG_V86_MODULE(seg) ((seg)>>16)
21 #define IS_SELECTOR_V86(seg) DBG_V86_MODULE(seg)
22 #endif
24 static void DEBUG_Die(const char* msg)
26 DEBUG_Printf(DBG_CHN_MESG, msg);
27 exit(1);
30 void* DEBUG_XMalloc(size_t size)
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;
39 void* DEBUG_XReAlloc(void *ptr, size_t size)
41 void* res = realloc(ptr, size);
42 if ((res == NULL) && size)
43 DEBUG_Die("Memory exhausted.\n");
44 return res;
47 char* DEBUG_XStrDup(const char *str)
49 char *res = strdup(str);
50 if (!res)
51 DEBUG_Die("Memory exhausted.\n");
52 return res;
55 #ifdef __i386__
56 void DEBUG_FixAddress( DBG_ADDR *addr, DWORD def)
58 if (addr->seg == 0xffffffff) addr->seg = def;
59 if (!IS_SELECTOR_V86(addr->seg) && DEBUG_IsSelectorSystem(addr->seg)) addr->seg = 0;
62 BOOL DEBUG_FixSegment( DBG_ADDR* addr )
64 /* V86 mode ? */
65 if (DEBUG_context.EFlags & V86_FLAG) {
66 addr->seg |= (DWORD)(GetExePtr(GetCurrentTask())) << 16;
67 return TRUE;
69 return FALSE;
72 int DEBUG_GetSelectorType( WORD sel )
74 LDT_ENTRY le;
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;
86 /* Determine if sel is a system selector (i.e. not managed by Wine) */
87 BOOL DEBUG_IsSelectorSystem(WORD sel)
89 return !(sel & 4) || (((sel & 0xFFFF) >> 3) < 17);
91 #endif /* __i386__ */
93 DWORD DEBUG_ToLinear( const DBG_ADDR *addr )
95 #ifdef __i386__
96 LDT_ENTRY le;
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;
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;
106 return 0;
107 #else
108 return addr->off;
109 #endif
112 void DEBUG_GetCurrentAddress( DBG_ADDR *addr )
114 #ifdef __i386__
115 addr->seg = DEBUG_context.SegCs;
117 if (!DEBUG_FixSegment( addr ) && DEBUG_IsSelectorSystem(addr->seg))
118 addr->seg = 0;
119 addr->off = DEBUG_context.Eip;
120 #else
121 addr->seg = 0;
122 addr->off = GET_IP( &DEBUG_context );
123 #endif
126 void DEBUG_InvalAddr( const DBG_ADDR* addr )
128 DEBUG_Printf(DBG_CHN_MESG,"*** Invalid address ");
129 DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
130 DEBUG_Printf(DBG_CHN_MESG,"\n");
131 if (DBG_IVAR(ExtDbgOnInvalidAddress)) DEBUG_ExternalDebugger();
134 void DEBUG_InvalLinAddr( void* addr )
136 DBG_ADDR address;
138 address.seg = 0;
139 address.off = (unsigned long)addr;
140 DEBUG_InvalAddr( &address );
143 /***********************************************************************
144 * DEBUG_ReadMemory
146 * Read a memory value.
148 /* FIXME: this function is now getting closer and closer to
149 * DEBUG_ExprGetValue. They should be merged...
151 int DEBUG_ReadMemory( const DBG_VALUE* val )
153 int value = 0; /* to clear any unused byte */
154 int os = DEBUG_GetObjectSize(val->type);
156 assert(sizeof(value) >= os);
158 /* FIXME: only works on little endian systems */
160 if (val->cookie == DV_TARGET) {
161 DBG_ADDR addr = val->addr;
162 void* lin;
164 #ifdef __i386__
165 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
166 #endif
167 lin = (void*)DEBUG_ToLinear( &addr );
169 DEBUG_READ_MEM_VERBOSE(lin, &value, os);
170 } else {
171 if (val->addr.off)
172 memcpy(&value, (void*)val->addr.off, os);
174 return value;
178 /***********************************************************************
179 * DEBUG_WriteMemory
181 * Store a value in memory.
183 void DEBUG_WriteMemory( const DBG_VALUE* val, int value )
185 int os = DEBUG_GetObjectSize(val->type);
187 assert(sizeof(value) >= os);
189 /* FIXME: only works on little endian systems */
191 if (val->cookie == DV_TARGET) {
192 DBG_ADDR addr = val->addr;
193 void* lin;
195 #ifdef __i386__
196 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
197 #endif
198 lin = (void*)DEBUG_ToLinear( &addr );
199 DEBUG_WRITE_MEM_VERBOSE(lin, &value, os);
200 } else {
201 memcpy((void*)val->addr.off, &value, os);
206 /***********************************************************************
207 * DEBUG_ExamineMemory
209 * Implementation of the 'x' command.
211 void DEBUG_ExamineMemory( const DBG_VALUE *_value, int count, char format )
213 DBG_VALUE value = *_value;
214 int i;
215 unsigned char * pnt;
216 struct datatype * testtype;
218 assert(_value->cookie == DV_TARGET || _value->cookie == DV_HOST);
220 #ifdef __i386__
221 DEBUG_FixAddress( &value.addr,
222 (format == 'i') ?
223 DEBUG_context.SegCs :
224 DEBUG_context.SegDs );
225 #endif
228 * Dereference pointer to get actual memory address we need to be
229 * reading. We will use the same segment as what we have already,
230 * and hope that this is a sensible thing to do.
232 if( value.type != NULL )
234 if( value.type == DEBUG_TypeIntConst )
237 * We know that we have the actual offset stored somewhere
238 * else in 32-bit space. Grab it, and we
239 * should be all set.
241 unsigned int seg2 = value.addr.seg;
242 value.addr.seg = 0;
243 value.addr.off = DEBUG_GetExprValue(&value, NULL);
244 value.addr.seg = seg2;
246 else
248 if (DEBUG_TypeDerefPointer(&value, &testtype) == 0)
249 return;
250 if( testtype != NULL || value.type == DEBUG_TypeIntConst )
252 value.addr.off = DEBUG_GetExprValue(&value, NULL);
256 else if (!value.addr.seg && !value.addr.off)
258 DEBUG_Printf(DBG_CHN_MESG,"Invalid expression\n");
259 return;
262 if (format != 'i' && count > 1)
264 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
265 DEBUG_Printf(DBG_CHN_MESG,": ");
268 pnt = (void*)DEBUG_ToLinear( &value.addr );
270 switch(format)
272 case 'u': {
273 WCHAR wch;
274 if (count == 1) count = 256;
275 while (count--)
277 if (!DEBUG_READ_MEM_VERBOSE(pnt, &wch, sizeof(wch)) || !wch)
278 break;
279 pnt += sizeof(wch);
280 DEBUG_Printf(DBG_CHN_MESG, "%c", (char)wch);
282 DEBUG_Printf(DBG_CHN_MESG,"\n");
283 return;
285 case 's': {
286 char ch;
288 if (count == 1) count = 256;
289 while (count--)
291 if (!DEBUG_READ_MEM_VERBOSE(pnt, &ch, sizeof(ch)) || !ch)
292 break;
293 pnt++;
294 DEBUG_Output(DBG_CHN_MESG, &ch, 1);
296 DEBUG_Printf(DBG_CHN_MESG,"\n");
297 return;
299 case 'i':
300 while (count--)
302 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, TRUE );
303 DEBUG_Printf(DBG_CHN_MESG,": ");
304 DEBUG_Disasm( &value.addr, TRUE );
305 DEBUG_Printf(DBG_CHN_MESG,"\n");
307 return;
308 #define DO_DUMP2(_t,_l,_f,_vv) { \
309 _t _v; \
310 for(i=0; i<count; i++) { \
311 if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
312 DEBUG_Printf(DBG_CHN_MESG,_f,(_vv)); \
313 pnt += sizeof(_t); value.addr.off += sizeof(_t); \
314 if ((i % (_l)) == (_l)-1) { \
315 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
316 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
317 DEBUG_Printf(DBG_CHN_MESG,": ");\
320 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
322 return
323 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
325 case 'x': DO_DUMP(int, 4, " %8.8x");
326 case 'd': DO_DUMP(unsigned int, 4, " %10d");
327 case 'w': DO_DUMP(unsigned short, 8, " %04x");
328 case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
329 case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);