Fix handling of unix absolute paths in DOSFS_GetFullName and
[wine/multimedia.git] / debugger / memory.c
blobfc2f9901ab7305095c238294b117dc239d52708c
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 #else
128 # error You must define GET_IP for this CPU
129 #endif
132 void DEBUG_InvalAddr( const DBG_ADDR* addr )
134 DEBUG_Printf(DBG_CHN_MESG,"*** Invalid address ");
135 DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
136 DEBUG_Printf(DBG_CHN_MESG,"\n");
137 if (DBG_IVAR(ExtDbgOnInvalidAddress)) DEBUG_ExternalDebugger();
140 void DEBUG_InvalLinAddr( void* addr )
142 DBG_ADDR address;
144 address.seg = 0;
145 address.off = (unsigned long)addr;
146 DEBUG_InvalAddr( &address );
149 /***********************************************************************
150 * DEBUG_ReadMemory
152 * Read a memory value.
154 /* FIXME: this function is now getting closer and closer to
155 * DEBUG_ExprGetValue. They should be merged...
157 int DEBUG_ReadMemory( const DBG_VALUE* val )
159 int value = 0; /* to clear any unused byte */
160 int os = DEBUG_GetObjectSize(val->type);
162 assert(sizeof(value) >= os);
164 /* FIXME: only works on little endian systems */
166 if (val->cookie == DV_TARGET) {
167 DBG_ADDR addr = val->addr;
168 void* lin;
170 #ifdef __i386__
171 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
172 #endif
173 lin = (void*)DEBUG_ToLinear( &addr );
175 DEBUG_READ_MEM_VERBOSE(lin, &value, os);
176 } else {
177 if (val->addr.off)
178 memcpy(&value, (void*)val->addr.off, os);
180 return value;
184 /***********************************************************************
185 * DEBUG_WriteMemory
187 * Store a value in memory.
189 void DEBUG_WriteMemory( const DBG_VALUE* val, int value )
191 int os = DEBUG_GetObjectSize(val->type);
193 assert(sizeof(value) >= os);
195 /* FIXME: only works on little endian systems */
197 if (val->cookie == DV_TARGET) {
198 DBG_ADDR addr = val->addr;
199 void* lin;
201 #ifdef __i386__
202 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
203 #endif
204 lin = (void*)DEBUG_ToLinear( &addr );
205 DEBUG_WRITE_MEM_VERBOSE(lin, &value, os);
206 } else {
207 memcpy((void*)val->addr.off, &value, os);
211 /***********************************************************************
212 * DEBUG_GrabAddress
214 * Get the address from a value
216 BOOL DEBUG_GrabAddress( DBG_VALUE* value, BOOL fromCode )
218 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
220 #ifdef __i386__
221 DEBUG_FixAddress( &value->addr,
222 (fromCode) ? DEBUG_context.SegCs : DEBUG_context.SegDs);
223 #endif
226 * Dereference pointer to get actual memory address we need to be
227 * reading. We will use the same segment as what we have already,
228 * and hope that this is a sensible thing to do.
230 if (value->type != NULL) {
231 if (value->type == DEBUG_GetBasicType(DT_BASIC_CONST_INT)) {
233 * We know that we have the actual offset stored somewhere
234 * else in 32-bit space. Grab it, and we
235 * should be all set.
237 unsigned int seg2 = value->addr.seg;
238 value->addr.seg = 0;
239 value->addr.off = DEBUG_GetExprValue(value, NULL);
240 value->addr.seg = seg2;
241 } else {
242 struct datatype * testtype;
244 if (DEBUG_TypeDerefPointer(value, &testtype) == 0)
245 return FALSE;
246 if (testtype != NULL || value->type == DEBUG_GetBasicType(DT_BASIC_CONST_INT))
247 value->addr.off = DEBUG_GetExprValue(value, NULL);
249 } else if (!value->addr.seg && !value->addr.off) {
250 DEBUG_Printf(DBG_CHN_MESG,"Invalid expression\n");
251 return FALSE;
253 return TRUE;
256 /***********************************************************************
257 * DEBUG_ExamineMemory
259 * Implementation of the 'x' command.
261 void DEBUG_ExamineMemory( const DBG_VALUE *_value, int count, char format )
263 DBG_VALUE value = *_value;
264 int i;
265 unsigned char * pnt;
267 if (!DEBUG_GrabAddress(&value, (format == 'i'))) return;
269 if (format != 'i' && count > 1)
271 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
272 DEBUG_Printf(DBG_CHN_MESG,": ");
275 pnt = (void*)DEBUG_ToLinear( &value.addr );
277 switch(format)
279 case 'u': {
280 WCHAR wch;
281 if (count == 1) count = 256;
282 while (count--)
284 if (!DEBUG_READ_MEM_VERBOSE(pnt, &wch, sizeof(wch)) || !wch)
285 break;
286 pnt += sizeof(wch);
287 DEBUG_Printf(DBG_CHN_MESG, "%c", (char)wch);
289 DEBUG_Printf(DBG_CHN_MESG,"\n");
290 return;
292 case 's': {
293 char ch;
295 if (count == 1) count = 256;
296 while (count--)
298 if (!DEBUG_READ_MEM_VERBOSE(pnt, &ch, sizeof(ch)) || !ch)
299 break;
300 pnt++;
301 DEBUG_Output(DBG_CHN_MESG, &ch, 1);
303 DEBUG_Printf(DBG_CHN_MESG,"\n");
304 return;
306 case 'i':
307 while (count-- && DEBUG_DisassembleInstruction( &value.addr ));
308 return;
309 #define DO_DUMP2(_t,_l,_f,_vv) { \
310 _t _v; \
311 for(i=0; i<count; i++) { \
312 if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
313 DEBUG_Printf(DBG_CHN_MESG,_f,(_vv)); \
314 pnt += sizeof(_t); value.addr.off += sizeof(_t); \
315 if ((i % (_l)) == (_l)-1) { \
316 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
317 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
318 DEBUG_Printf(DBG_CHN_MESG,": ");\
321 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
323 return
324 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
326 case 'x': DO_DUMP(int, 4, " %8.8x");
327 case 'd': DO_DUMP(unsigned int, 4, " %10d");
328 case 'w': DO_DUMP(unsigned short, 8, " %04x");
329 case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
330 case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);