Serge Ivanov
[wine/multimedia.git] / debugger / memory.c
blob453e3e0218a6693fc931753466bbc877b4d9fc44
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)
23 static void DEBUG_Die(const char* msg)
25 DEBUG_Printf(DBG_CHN_MESG, msg);
26 exit(1);
29 void* DEBUG_XMalloc(size_t size)
31 void *res = malloc(size ? size : 1);
32 if (res == NULL)
33 DEBUG_Die("Memory exhausted.\n");
34 memset(res, 0, size);
35 return res;
38 void* DEBUG_XReAlloc(void *ptr, size_t size)
40 void* res = realloc(ptr, size);
41 if ((res == NULL) && size)
42 DEBUG_Die("Memory exhausted.\n");
43 return res;
46 char* DEBUG_XStrDup(const char *str)
48 char *res = strdup(str);
49 if (!res)
50 DEBUG_Die("Memory exhausted.\n");
51 return res;
54 void DEBUG_FixAddress( DBG_ADDR *addr, DWORD def)
56 if (addr->seg == 0xffffffff) addr->seg = def;
57 if (!IS_SELECTOR_V86(addr->seg) && DEBUG_IsSelectorSystem(addr->seg)) addr->seg = 0;
60 BOOL DEBUG_FixSegment( DBG_ADDR* addr )
62 /* V86 mode ? */
63 if (DEBUG_context.EFlags & V86_FLAG) {
64 addr->seg |= (DWORD)(GetExePtr(GetCurrentTask())) << 16;
65 return TRUE;
67 return FALSE;
70 DWORD DEBUG_ToLinear( const DBG_ADDR *addr )
72 LDT_ENTRY le;
74 if (IS_SELECTOR_V86(addr->seg))
75 return (DWORD) DOSMEM_MemoryBase(DBG_V86_MODULE(addr->seg)) + (((addr->seg)&0xFFFF)<<4) + addr->off;
76 if (DEBUG_IsSelectorSystem(addr->seg))
77 return addr->off;
79 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, addr->seg, &le)) {
80 return (le.HighWord.Bits.BaseHi << 24) + (le.HighWord.Bits.BaseMid << 16) + le.BaseLow + addr->off;
82 return 0;
85 int DEBUG_GetSelectorType( WORD sel )
87 LDT_ENTRY le;
89 if (sel == 0)
90 return 32;
91 if (IS_SELECTOR_V86(sel))
92 return 16;
93 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, sel, &le))
94 return le.HighWord.Bits.Default_Big ? 32 : 16;
95 /* selector doesn't exist */
96 return 0;
99 /* Determine if sel is a system selector (i.e. not managed by Wine) */
100 BOOL DEBUG_IsSelectorSystem(WORD sel)
102 return !(sel & 4) || (((sel & 0xFFFF) >> 3) < 17);
104 #endif /* __i386__ */
106 void DEBUG_GetCurrentAddress( DBG_ADDR *addr )
108 #ifdef __i386__
109 addr->seg = DEBUG_context.SegCs;
111 if (!DEBUG_FixSegment( addr ) && DEBUG_IsSelectorSystem(addr->seg))
112 addr->seg = 0;
113 addr->off = DEBUG_context.Eip;
114 #else
115 addr->seg = 0;
116 addr->off = 0;
117 #endif
120 void DEBUG_InvalAddr( const DBG_ADDR* addr )
122 DEBUG_Printf(DBG_CHN_MESG,"*** Invalid address ");
123 DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
124 DEBUG_Printf(DBG_CHN_MESG,"\n");
125 if (DBG_IVAR(ExtDbgOnInvalidAddress)) DEBUG_ExternalDebugger();
128 void DEBUG_InvalLinAddr( void* addr )
130 DBG_ADDR address;
132 address.seg = 0;
133 address.off = (unsigned long)addr;
134 DEBUG_InvalAddr( &address );
137 /***********************************************************************
138 * DEBUG_ReadMemory
140 * Read a memory value.
142 /* FIXME: this function is now getting closer and closer to
143 * DEBUG_ExprGetValue. They should be merged...
145 int DEBUG_ReadMemory( const DBG_VALUE* val )
147 int value = 0; /* to clear any unused byte */
148 int os = DEBUG_GetObjectSize(val->type);
150 assert(sizeof(value) >= os);
152 /* FIXME: only works on little endian systems */
154 if (val->cookie == DV_TARGET) {
155 DBG_ADDR addr = val->addr;
156 void* lin;
158 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
159 lin = (void*)DEBUG_ToLinear( &addr );
161 DEBUG_READ_MEM_VERBOSE(lin, &value, os);
162 } else {
163 if (val->addr.off)
164 memcpy(&value, (void*)val->addr.off, os);
166 return value;
170 /***********************************************************************
171 * DEBUG_WriteMemory
173 * Store a value in memory.
175 void DEBUG_WriteMemory( const DBG_VALUE* val, int value )
177 int os = DEBUG_GetObjectSize(val->type);
179 assert(sizeof(value) >= os);
181 /* FIXME: only works on little endian systems */
183 if (val->cookie == DV_TARGET) {
184 DBG_ADDR addr = val->addr;
185 void* lin;
187 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
188 lin = (void*)DEBUG_ToLinear( &addr );
189 DEBUG_WRITE_MEM_VERBOSE(lin, &value, os);
190 } else {
191 memcpy((void*)val->addr.off, &value, os);
196 /***********************************************************************
197 * DEBUG_ExamineMemory
199 * Implementation of the 'x' command.
201 void DEBUG_ExamineMemory( const DBG_VALUE *_value, int count, char format )
203 DBG_VALUE value = *_value;
204 int i;
205 unsigned char * pnt;
206 struct datatype * testtype;
208 assert(_value->cookie == DV_TARGET || _value->cookie == DV_HOST);
210 DEBUG_FixAddress( &value.addr,
211 (format == 'i') ?
212 DEBUG_context.SegCs :
213 DEBUG_context.SegDs );
216 * Dereference pointer to get actual memory address we need to be
217 * reading. We will use the same segment as what we have already,
218 * and hope that this is a sensible thing to do.
220 if( value.type != NULL )
222 if( value.type == DEBUG_TypeIntConst )
225 * We know that we have the actual offset stored somewhere
226 * else in 32-bit space. Grab it, and we
227 * should be all set.
229 unsigned int seg2 = value.addr.seg;
230 value.addr.seg = 0;
231 value.addr.off = DEBUG_GetExprValue(&value, NULL);
232 value.addr.seg = seg2;
234 else
236 if (DEBUG_TypeDerefPointer(&value, &testtype) == 0)
237 return;
238 if( testtype != NULL || value.type == DEBUG_TypeIntConst )
240 value.addr.off = DEBUG_GetExprValue(&value, NULL);
244 else if (!value.addr.seg && !value.addr.off)
246 DEBUG_Printf(DBG_CHN_MESG,"Invalid expression\n");
247 return;
250 if (format != 'i' && count > 1)
252 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
253 DEBUG_Printf(DBG_CHN_MESG,": ");
256 pnt = (void*)DEBUG_ToLinear( &value.addr );
258 switch(format)
260 case 'u': {
261 WCHAR wch;
262 if (count == 1) count = 256;
263 while (count--)
265 if (!DEBUG_READ_MEM_VERBOSE(pnt, &wch, sizeof(wch)) || !wch)
266 break;
267 pnt += sizeof(wch);
268 DEBUG_Printf(DBG_CHN_MESG, "%c", (char)wch);
270 DEBUG_Printf(DBG_CHN_MESG,"\n");
271 return;
273 case 's': {
274 char ch;
276 if (count == 1) count = 256;
277 while (count--)
279 if (!DEBUG_READ_MEM_VERBOSE(pnt, &ch, sizeof(ch)) || !ch)
280 break;
281 pnt++;
282 DEBUG_Output(DBG_CHN_MESG, &ch, 1);
284 DEBUG_Printf(DBG_CHN_MESG,"\n");
285 return;
287 case 'i':
288 while (count--)
290 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, TRUE );
291 DEBUG_Printf(DBG_CHN_MESG,": ");
292 DEBUG_Disasm( &value.addr, TRUE );
293 DEBUG_Printf(DBG_CHN_MESG,"\n");
295 return;
296 #define DO_DUMP2(_t,_l,_f,_vv) { \
297 _t _v; \
298 for(i=0; i<count; i++) { \
299 if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
300 DEBUG_Printf(DBG_CHN_MESG,_f,(_vv)); \
301 pnt += sizeof(_t); value.addr.off += sizeof(_t); \
302 if ((i % (_l)) == (_l)-1) { \
303 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
304 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
305 DEBUG_Printf(DBG_CHN_MESG,": ");\
308 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
310 return
311 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
313 case 'x': DO_DUMP(int, 4, " %8.8x");
314 case 'd': DO_DUMP(unsigned int, 4, " %10d");
315 case 'w': DO_DUMP(unsigned short, 8, " %04x");
316 case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
317 case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);