Added some TRACEs to the ddraw code.
[wine/gsoc_dplay.git] / programs / winedbg / memory.c
blob49454f5eb84aa5bad1176f999da87a5168cc388a
1 /*
2 * Debugger memory handling
4 * Copyright 1993 Eric Youngdale
5 * Copyright 1995 Alexandre Julliard
6 * Copyright 2000 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdlib.h>
27 #include <string.h>
29 #include "debugger.h"
30 #include "winbase.h"
32 #ifdef __i386__
33 #define IS_VM86_MODE() (DEBUG_context.EFlags & V86_FLAG)
34 #endif
36 static void DEBUG_Die(const char* msg)
38 DEBUG_Printf(DBG_CHN_MESG, msg);
39 exit(1);
42 void* DEBUG_XMalloc(size_t size)
44 void *res = malloc(size ? size : 1);
45 if (res == NULL)
46 DEBUG_Die("Memory exhausted.\n");
47 memset(res, 0, size);
48 return res;
51 void* DEBUG_XReAlloc(void *ptr, size_t size)
53 void* res = realloc(ptr, size);
54 if ((res == NULL) && size)
55 DEBUG_Die("Memory exhausted.\n");
56 return res;
59 char* DEBUG_XStrDup(const char *str)
61 char *res = strdup(str);
62 if (!res)
63 DEBUG_Die("Memory exhausted.\n");
64 return res;
67 enum dbg_mode DEBUG_GetSelectorType( WORD sel )
69 #ifdef __i386__
70 LDT_ENTRY le;
72 if (IS_VM86_MODE()) return MODE_VM86;
73 if (sel == 0) return MODE_32;
74 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, sel, &le))
75 return le.HighWord.Bits.Default_Big ? MODE_32 : MODE_16;
76 /* selector doesn't exist */
77 return MODE_INVALID;
78 #else
79 return MODE_32;
80 #endif
82 #ifdef __i386__
83 void DEBUG_FixAddress( DBG_ADDR *addr, DWORD def)
85 if (addr->seg == 0xffffffff) addr->seg = def;
86 if (DEBUG_IsSelectorSystem(addr->seg)) addr->seg = 0;
89 /* Determine if sel is a system selector (i.e. not managed by Wine) */
90 BOOL DEBUG_IsSelectorSystem(WORD sel)
92 if (IS_VM86_MODE()) return FALSE; /* no system selectors in vm86 mode */
93 return !(sel & 4) || ((sel >> 3) < 17);
95 #endif /* __i386__ */
97 DWORD DEBUG_ToLinear( const DBG_ADDR *addr )
99 #ifdef __i386__
100 LDT_ENTRY le;
102 if (IS_VM86_MODE()) return (DWORD)(LOWORD(addr->seg) << 4) + addr->off;
104 if (DEBUG_IsSelectorSystem(addr->seg))
105 return addr->off;
107 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, addr->seg, &le)) {
108 return (le.HighWord.Bits.BaseHi << 24) + (le.HighWord.Bits.BaseMid << 16) + le.BaseLow + addr->off;
110 return 0;
111 #else
112 return addr->off;
113 #endif
116 void DEBUG_GetCurrentAddress( DBG_ADDR *addr )
118 #ifdef __i386__
119 addr->seg = DEBUG_context.SegCs;
121 if (DEBUG_IsSelectorSystem(addr->seg))
122 addr->seg = 0;
123 addr->off = DEBUG_context.Eip;
124 #elif defined(__sparc__)
125 addr->seg = 0;
126 addr->off = DEBUG_context.pc;
127 #elif defined(__powerpc__)
128 addr->seg = 0;
129 addr->off = DEBUG_context.Iar;
130 #else
131 # error You must define GET_IP for this CPU
132 #endif
135 void DEBUG_InvalAddr( const DBG_ADDR* addr )
137 DEBUG_Printf(DBG_CHN_MESG,"*** Invalid address ");
138 DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
139 DEBUG_Printf(DBG_CHN_MESG,"\n");
140 if (DBG_IVAR(ExtDbgOnInvalidAddress)) DEBUG_ExternalDebugger();
143 void DEBUG_InvalLinAddr( void* addr )
145 DBG_ADDR address;
147 address.seg = 0;
148 address.off = (unsigned long)addr;
149 DEBUG_InvalAddr( &address );
152 /***********************************************************************
153 * DEBUG_ReadMemory
155 * Read a memory value.
157 /* FIXME: this function is now getting closer and closer to
158 * DEBUG_ExprGetValue. They should be merged...
160 int DEBUG_ReadMemory( const DBG_VALUE* val )
162 int value = 0; /* to clear any unused byte */
163 int os = DEBUG_GetObjectSize(val->type);
165 assert(sizeof(value) >= os);
167 /* FIXME: only works on little endian systems */
169 if (val->cookie == DV_TARGET) {
170 DBG_ADDR addr = val->addr;
171 void* lin;
173 #ifdef __i386__
174 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
175 #endif
176 lin = (void*)DEBUG_ToLinear( &addr );
178 DEBUG_READ_MEM_VERBOSE(lin, &value, os);
179 } else {
180 if (val->addr.off)
181 memcpy(&value, (void*)val->addr.off, os);
183 return value;
187 /***********************************************************************
188 * DEBUG_WriteMemory
190 * Store a value in memory.
192 void DEBUG_WriteMemory( const DBG_VALUE* val, int value )
194 int os = DEBUG_GetObjectSize(val->type);
196 assert(sizeof(value) >= os);
198 /* FIXME: only works on little endian systems */
200 if (val->cookie == DV_TARGET) {
201 DBG_ADDR addr = val->addr;
202 void* lin;
204 #ifdef __i386__
205 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
206 #endif
207 lin = (void*)DEBUG_ToLinear( &addr );
208 DEBUG_WRITE_MEM_VERBOSE(lin, &value, os);
209 } else {
210 memcpy((void*)val->addr.off, &value, os);
214 /***********************************************************************
215 * DEBUG_GrabAddress
217 * Get the address from a value
219 BOOL DEBUG_GrabAddress( DBG_VALUE* value, BOOL fromCode )
221 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
223 #ifdef __i386__
224 DEBUG_FixAddress( &value->addr,
225 (fromCode) ? DEBUG_context.SegCs : DEBUG_context.SegDs);
226 #endif
229 * Dereference pointer to get actual memory address we need to be
230 * reading. We will use the same segment as what we have already,
231 * and hope that this is a sensible thing to do.
233 if (value->type != NULL) {
234 if (value->type == DEBUG_GetBasicType(DT_BASIC_CONST_INT)) {
236 * We know that we have the actual offset stored somewhere
237 * else in 32-bit space. Grab it, and we
238 * should be all set.
240 unsigned int seg2 = value->addr.seg;
241 value->addr.seg = 0;
242 value->addr.off = DEBUG_GetExprValue(value, NULL);
243 value->addr.seg = seg2;
244 } else {
245 struct datatype * testtype;
247 if (DEBUG_TypeDerefPointer(value, &testtype) == 0)
248 return FALSE;
249 if (testtype != NULL || value->type == DEBUG_GetBasicType(DT_BASIC_CONST_INT))
250 value->addr.off = DEBUG_GetExprValue(value, NULL);
252 } else if (!value->addr.seg && !value->addr.off) {
253 DEBUG_Printf(DBG_CHN_MESG,"Invalid expression\n");
254 return FALSE;
256 return TRUE;
259 /***********************************************************************
260 * DEBUG_ExamineMemory
262 * Implementation of the 'x' command.
264 void DEBUG_ExamineMemory( const DBG_VALUE *_value, int count, char format )
266 DBG_VALUE value = *_value;
267 int i;
268 unsigned char * pnt;
270 if (!DEBUG_GrabAddress(&value, (format == 'i'))) return;
272 if (format != 'i' && count > 1)
274 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
275 DEBUG_Printf(DBG_CHN_MESG,": ");
278 pnt = (void*)DEBUG_ToLinear( &value.addr );
280 switch(format)
282 case 'u':
283 if (count == 1) count = 256;
284 DEBUG_nchar += DEBUG_PrintStringW(DBG_CHN_MESG, &value.addr, count);
285 DEBUG_Printf(DBG_CHN_MESG, "\n");
286 return;
287 case 's':
288 DEBUG_nchar += DEBUG_PrintStringA(DBG_CHN_MESG, &value.addr, count);
289 DEBUG_Printf(DBG_CHN_MESG, "\n");
290 return;
291 case 'i':
292 while (count-- && DEBUG_DisassembleInstruction( &value.addr ));
293 return;
294 case 'g':
295 while (count--)
297 GUID guid;
298 if (!DEBUG_READ_MEM_VERBOSE(pnt, &guid, sizeof(guid))) break;
299 DEBUG_Printf(DBG_CHN_MESG,"{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
300 guid.Data1, guid.Data2, guid.Data3,
301 guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
302 guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7] );
303 pnt += sizeof(guid);
304 value.addr.off += sizeof(guid);
305 if (count)
307 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
308 DEBUG_Printf(DBG_CHN_MESG,": ");
311 return;
313 #define DO_DUMP2(_t,_l,_f,_vv) { \
314 _t _v; \
315 for(i=0; i<count; i++) { \
316 if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
317 DEBUG_Printf(DBG_CHN_MESG,_f,(_vv)); \
318 pnt += sizeof(_t); value.addr.off += sizeof(_t); \
319 if ((i % (_l)) == (_l)-1) { \
320 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
321 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
322 DEBUG_Printf(DBG_CHN_MESG,": ");\
325 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
327 return
328 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
330 case 'x': DO_DUMP(int, 4, " %8.8x");
331 case 'd': DO_DUMP(unsigned int, 4, " %10d");
332 case 'w': DO_DUMP(unsigned short, 8, " %04x");
333 case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
334 case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);
338 /******************************************************************
339 * DEBUG_PrintStringA
341 * Prints on channel chnl, the string starting at address in target
342 * address space. The string stops when either len chars (if <> -1)
343 * have been printed, or the '\0' char is printed
345 int DEBUG_PrintStringA(int chnl, const DBG_ADDR* address, int len)
347 char* lin = (void*)DEBUG_ToLinear(address);
348 char ach[16+1];
349 int i, l;
351 if (len == -1) len = 32767; /* should be big enough */
353 /* so that the ach is always terminated */
354 ach[sizeof(ach) - 1] = '\0';
355 for (i = len; i >= 0; i -= sizeof(ach) - 1)
357 l = min(sizeof(ach) - 1, i);
358 DEBUG_READ_MEM_VERBOSE(lin, ach, l);
359 l = strlen(ach);
360 DEBUG_OutputA(chnl, ach, l);
361 lin += l;
362 if (l < sizeof(ach) - 1) break;
364 return len - i; /* number of actually written chars */
367 int DEBUG_PrintStringW(int chnl, const DBG_ADDR* address, int len)
369 char* lin = (void*)DEBUG_ToLinear(address);
370 WCHAR wch;
371 int ret = 0;
373 if (len == -1) len = 32767; /* should be big enough */
374 while (len--)
376 if (!DEBUG_READ_MEM_VERBOSE(lin, &wch, sizeof(wch)) || !wch)
377 break;
378 lin += sizeof(wch);
379 DEBUG_OutputW(chnl, &wch, 1);
380 ret++;
382 return ret;