Added sample config file in the new format.
[wine/multimedia.git] / debugger / memory.c
blob416788d0a90083157c221629a790bc2fa7996f27
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 #define IS_VM86_MODE() (DEBUG_context.EFlags & V86_FLAG)
19 #endif
21 static void DEBUG_Die(const char* msg)
23 DEBUG_Printf(DBG_CHN_MESG, msg);
24 exit(1);
27 void* DEBUG_XMalloc(size_t size)
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;
36 void* DEBUG_XReAlloc(void *ptr, size_t size)
38 void* res = realloc(ptr, size);
39 if ((res == NULL) && size)
40 DEBUG_Die("Memory exhausted.\n");
41 return res;
44 char* DEBUG_XStrDup(const char *str)
46 char *res = strdup(str);
47 if (!res)
48 DEBUG_Die("Memory exhausted.\n");
49 return res;
52 enum dbg_mode DEBUG_GetSelectorType( WORD sel )
54 #ifdef __i386__
55 LDT_ENTRY le;
57 if (IS_VM86_MODE()) return MODE_VM86;
58 if (sel == 0) return MODE_32;
59 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, sel, &le))
60 return le.HighWord.Bits.Default_Big ? MODE_32 : MODE_16;
61 /* selector doesn't exist */
62 return MODE_INVALID;
63 #else
64 return MODE_32;
65 #endif
67 #ifdef __i386__
68 void DEBUG_FixAddress( DBG_ADDR *addr, DWORD def)
70 if (addr->seg == 0xffffffff) addr->seg = def;
71 if (DEBUG_IsSelectorSystem(addr->seg)) addr->seg = 0;
74 /* Determine if sel is a system selector (i.e. not managed by Wine) */
75 BOOL DEBUG_IsSelectorSystem(WORD sel)
77 if (IS_VM86_MODE()) return FALSE; /* no system selectors in vm86 mode */
78 return !(sel & 4) || ((sel >> 3) < 17);
80 #endif /* __i386__ */
82 DWORD DEBUG_ToLinear( const DBG_ADDR *addr )
84 #ifdef __i386__
85 LDT_ENTRY le;
87 if (IS_VM86_MODE()) return (DWORD)(LOWORD(addr->seg) << 4) + addr->off;
89 if (DEBUG_IsSelectorSystem(addr->seg))
90 return addr->off;
92 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, addr->seg, &le)) {
93 return (le.HighWord.Bits.BaseHi << 24) + (le.HighWord.Bits.BaseMid << 16) + le.BaseLow + addr->off;
95 return 0;
96 #else
97 return addr->off;
98 #endif
101 void DEBUG_GetCurrentAddress( DBG_ADDR *addr )
103 #ifdef __i386__
104 addr->seg = DEBUG_context.SegCs;
106 if (DEBUG_IsSelectorSystem(addr->seg))
107 addr->seg = 0;
108 addr->off = DEBUG_context.Eip;
109 #elif defined(__sparc__)
110 addr->seg = 0;
111 addr->off = DEBUG_context.pc;
112 #else
113 # error You must define GET_IP for this CPU
114 #endif
117 void DEBUG_InvalAddr( const DBG_ADDR* addr )
119 DEBUG_Printf(DBG_CHN_MESG,"*** Invalid address ");
120 DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
121 DEBUG_Printf(DBG_CHN_MESG,"\n");
122 if (DBG_IVAR(ExtDbgOnInvalidAddress)) DEBUG_ExternalDebugger();
125 void DEBUG_InvalLinAddr( void* addr )
127 DBG_ADDR address;
129 address.seg = 0;
130 address.off = (unsigned long)addr;
131 DEBUG_InvalAddr( &address );
134 /***********************************************************************
135 * DEBUG_ReadMemory
137 * Read a memory value.
139 /* FIXME: this function is now getting closer and closer to
140 * DEBUG_ExprGetValue. They should be merged...
142 int DEBUG_ReadMemory( const DBG_VALUE* val )
144 int value = 0; /* to clear any unused byte */
145 int os = DEBUG_GetObjectSize(val->type);
147 assert(sizeof(value) >= os);
149 /* FIXME: only works on little endian systems */
151 if (val->cookie == DV_TARGET) {
152 DBG_ADDR addr = val->addr;
153 void* lin;
155 #ifdef __i386__
156 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
157 #endif
158 lin = (void*)DEBUG_ToLinear( &addr );
160 DEBUG_READ_MEM_VERBOSE(lin, &value, os);
161 } else {
162 if (val->addr.off)
163 memcpy(&value, (void*)val->addr.off, os);
165 return value;
169 /***********************************************************************
170 * DEBUG_WriteMemory
172 * Store a value in memory.
174 void DEBUG_WriteMemory( const DBG_VALUE* val, int value )
176 int os = DEBUG_GetObjectSize(val->type);
178 assert(sizeof(value) >= os);
180 /* FIXME: only works on little endian systems */
182 if (val->cookie == DV_TARGET) {
183 DBG_ADDR addr = val->addr;
184 void* lin;
186 #ifdef __i386__
187 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
188 #endif
189 lin = (void*)DEBUG_ToLinear( &addr );
190 DEBUG_WRITE_MEM_VERBOSE(lin, &value, os);
191 } else {
192 memcpy((void*)val->addr.off, &value, os);
196 /***********************************************************************
197 * DEBUG_GrabAddress
199 * Get the address from a value
201 BOOL DEBUG_GrabAddress( DBG_VALUE* value, BOOL fromCode )
203 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
205 #ifdef __i386__
206 DEBUG_FixAddress( &value->addr,
207 (fromCode) ? DEBUG_context.SegCs : DEBUG_context.SegDs);
208 #endif
211 * Dereference pointer to get actual memory address we need to be
212 * reading. We will use the same segment as what we have already,
213 * and hope that this is a sensible thing to do.
215 if (value->type != NULL) {
216 if (value->type == DEBUG_TypeIntConst) {
218 * We know that we have the actual offset stored somewhere
219 * else in 32-bit space. Grab it, and we
220 * should be all set.
222 unsigned int seg2 = value->addr.seg;
223 value->addr.seg = 0;
224 value->addr.off = DEBUG_GetExprValue(value, NULL);
225 value->addr.seg = seg2;
226 } else {
227 struct datatype * testtype;
229 if (DEBUG_TypeDerefPointer(value, &testtype) == 0)
230 return FALSE;
231 if (testtype != NULL || value->type == DEBUG_TypeIntConst)
232 value->addr.off = DEBUG_GetExprValue(value, NULL);
234 } else if (!value->addr.seg && !value->addr.off) {
235 DEBUG_Printf(DBG_CHN_MESG,"Invalid expression\n");
236 return FALSE;
238 return TRUE;
241 /***********************************************************************
242 * DEBUG_ExamineMemory
244 * Implementation of the 'x' command.
246 void DEBUG_ExamineMemory( const DBG_VALUE *_value, int count, char format )
248 DBG_VALUE value = *_value;
249 int i;
250 unsigned char * pnt;
252 if (!DEBUG_GrabAddress(&value, (format == 'i'))) return;
254 if (format != 'i' && count > 1)
256 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
257 DEBUG_Printf(DBG_CHN_MESG,": ");
260 pnt = (void*)DEBUG_ToLinear( &value.addr );
262 switch(format)
264 case 'u': {
265 WCHAR wch;
266 if (count == 1) count = 256;
267 while (count--)
269 if (!DEBUG_READ_MEM_VERBOSE(pnt, &wch, sizeof(wch)) || !wch)
270 break;
271 pnt += sizeof(wch);
272 DEBUG_Printf(DBG_CHN_MESG, "%c", (char)wch);
274 DEBUG_Printf(DBG_CHN_MESG,"\n");
275 return;
277 case 's': {
278 char ch;
280 if (count == 1) count = 256;
281 while (count--)
283 if (!DEBUG_READ_MEM_VERBOSE(pnt, &ch, sizeof(ch)) || !ch)
284 break;
285 pnt++;
286 DEBUG_Output(DBG_CHN_MESG, &ch, 1);
288 DEBUG_Printf(DBG_CHN_MESG,"\n");
289 return;
291 case 'i':
292 while (count-- && DEBUG_DisassembleInstruction( &value.addr ));
293 return;
294 #define DO_DUMP2(_t,_l,_f,_vv) { \
295 _t _v; \
296 for(i=0; i<count; i++) { \
297 if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
298 DEBUG_Printf(DBG_CHN_MESG,_f,(_vv)); \
299 pnt += sizeof(_t); value.addr.off += sizeof(_t); \
300 if ((i % (_l)) == (_l)-1) { \
301 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
302 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
303 DEBUG_Printf(DBG_CHN_MESG,": ");\
306 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
308 return
309 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
311 case 'x': DO_DUMP(int, 4, " %8.8x");
312 case 'd': DO_DUMP(unsigned int, 4, " %10d");
313 case 'w': DO_DUMP(unsigned short, 8, " %04x");
314 case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
315 case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);