Use futimes() instead of utime() to implement SetFileTime, so that it
[wine.git] / programs / winedbg / memory.c
blob88b51b6cb81a3075ba7c27d11a956dda4e7de4fe
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(msg);
39 exit(1);
42 void* DBG_alloc(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* DBG_realloc(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* DBG_strdup(const char *str)
61 char *res = strdup(str);
62 if (!res)
63 DEBUG_Die("Memory exhausted.\n");
64 return res;
67 void DBG_free(void *ptr)
69 free(ptr);
72 enum dbg_mode DEBUG_GetSelectorType( WORD sel )
74 #ifdef __i386__
75 LDT_ENTRY le;
77 if (IS_VM86_MODE()) return MODE_VM86;
78 if (sel == 0) return MODE_32;
79 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, sel, &le))
80 return le.HighWord.Bits.Default_Big ? MODE_32 : MODE_16;
81 /* selector doesn't exist */
82 return MODE_INVALID;
83 #else
84 return MODE_32;
85 #endif
87 #ifdef __i386__
88 void DEBUG_FixAddress( DBG_ADDR *addr, DWORD def)
90 if (addr->seg == 0xffffffff) addr->seg = def;
91 if (DEBUG_IsSelectorSystem(addr->seg)) addr->seg = 0;
94 /* Determine if sel is a system selector (i.e. not managed by Wine) */
95 BOOL DEBUG_IsSelectorSystem(WORD sel)
97 if (IS_VM86_MODE()) return FALSE; /* no system selectors in vm86 mode */
98 return !(sel & 4) || ((sel >> 3) < 17);
100 #endif /* __i386__ */
102 DWORD DEBUG_ToLinear( const DBG_ADDR *addr )
104 #ifdef __i386__
105 LDT_ENTRY le;
107 if (IS_VM86_MODE()) return (DWORD)(LOWORD(addr->seg) << 4) + addr->off;
109 if (DEBUG_IsSelectorSystem(addr->seg))
110 return addr->off;
112 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, addr->seg, &le)) {
113 return (le.HighWord.Bits.BaseHi << 24) + (le.HighWord.Bits.BaseMid << 16) + le.BaseLow + addr->off;
115 return 0;
116 #else
117 return addr->off;
118 #endif
121 void DEBUG_GetCurrentAddress( DBG_ADDR *addr )
123 #ifdef __i386__
124 addr->seg = DEBUG_context.SegCs;
126 if (DEBUG_IsSelectorSystem(addr->seg))
127 addr->seg = 0;
128 addr->off = DEBUG_context.Eip;
129 #elif defined(__sparc__)
130 addr->seg = 0;
131 addr->off = DEBUG_context.pc;
132 #elif defined(__powerpc__)
133 addr->seg = 0;
134 addr->off = DEBUG_context.Iar;
135 #else
136 # error You must define GET_IP for this CPU
137 #endif
140 void DEBUG_InvalAddr( const DBG_ADDR* addr )
142 DEBUG_Printf("*** Invalid address ");
143 DEBUG_PrintAddress(addr, DEBUG_CurrThread ? DEBUG_CurrThread->dbg_mode : MODE_32, FALSE);
144 DEBUG_Printf("\n");
145 if (DBG_IVAR(ExtDbgOnInvalidAddress)) DEBUG_ExternalDebugger();
148 void DEBUG_InvalLinAddr( void* addr )
150 DBG_ADDR address;
152 address.seg = 0;
153 address.off = (unsigned long)addr;
154 DEBUG_InvalAddr( &address );
157 /***********************************************************************
158 * DEBUG_ReadMemory
160 * Read a memory value.
162 /* FIXME: this function is now getting closer and closer to
163 * DEBUG_ExprGetValue. They should be merged...
165 int DEBUG_ReadMemory( const DBG_VALUE* val )
167 int value = 0; /* to clear any unused byte */
168 int os = DEBUG_GetObjectSize(val->type);
170 assert(sizeof(value) >= os);
172 /* FIXME: only works on little endian systems */
174 if (val->cookie == DV_TARGET) {
175 DBG_ADDR addr = val->addr;
176 void* lin;
178 #ifdef __i386__
179 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
180 #endif
181 lin = (void*)DEBUG_ToLinear( &addr );
183 DEBUG_READ_MEM_VERBOSE(lin, &value, os);
184 } else {
185 if (val->addr.off)
186 memcpy(&value, (void*)val->addr.off, os);
188 return value;
192 /***********************************************************************
193 * DEBUG_WriteMemory
195 * Store a value in memory.
197 void DEBUG_WriteMemory( const DBG_VALUE* val, int value )
199 int os = DEBUG_GetObjectSize(val->type);
201 assert(sizeof(value) >= os);
203 /* FIXME: only works on little endian systems */
205 if (val->cookie == DV_TARGET) {
206 DBG_ADDR addr = val->addr;
207 void* lin;
209 #ifdef __i386__
210 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
211 #endif
212 lin = (void*)DEBUG_ToLinear( &addr );
213 DEBUG_WRITE_MEM_VERBOSE(lin, &value, os);
214 } else {
215 memcpy((void*)val->addr.off, &value, os);
219 /***********************************************************************
220 * DEBUG_GrabAddress
222 * Get the address from a value
224 BOOL DEBUG_GrabAddress( DBG_VALUE* value, BOOL fromCode )
226 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
228 #ifdef __i386__
229 DEBUG_FixAddress( &value->addr,
230 (fromCode) ? DEBUG_context.SegCs : DEBUG_context.SegDs);
231 #endif
234 * Dereference pointer to get actual memory address we need to be
235 * reading. We will use the same segment as what we have already,
236 * and hope that this is a sensible thing to do.
238 if (value->type != NULL) {
239 if (value->type == DEBUG_GetBasicType(DT_BASIC_CONST_INT)) {
241 * We know that we have the actual offset stored somewhere
242 * else in 32-bit space. Grab it, and we
243 * should be all set.
245 unsigned int seg2 = value->addr.seg;
246 value->addr.seg = 0;
247 value->addr.off = DEBUG_GetExprValue(value, NULL);
248 value->addr.seg = seg2;
249 } else {
250 struct datatype * testtype;
252 if (DEBUG_TypeDerefPointer(value, &testtype) == 0)
253 return FALSE;
254 if (testtype != NULL || value->type == DEBUG_GetBasicType(DT_BASIC_CONST_INT))
255 value->addr.off = DEBUG_GetExprValue(value, NULL);
257 } else if (!value->addr.seg && !value->addr.off) {
258 DEBUG_Printf("Invalid expression\n");
259 return FALSE;
261 return TRUE;
264 /***********************************************************************
265 * DEBUG_ExamineMemory
267 * Implementation of the 'x' command.
269 void DEBUG_ExamineMemory( const DBG_VALUE *_value, int count, char format )
271 DBG_VALUE value = *_value;
272 int i;
273 unsigned char * pnt;
275 if (!DEBUG_GrabAddress(&value, (format == 'i'))) return;
277 if (format != 'i' && count > 1)
279 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
280 DEBUG_Printf(": ");
283 pnt = (void*)DEBUG_ToLinear( &value.addr );
285 switch(format)
287 case 'u':
288 if (count == 1) count = 256;
289 DEBUG_nchar += DEBUG_PrintStringW(&value.addr, count);
290 DEBUG_Printf("\n");
291 return;
292 case 's':
293 if (count == 1) count = 256;
294 DEBUG_nchar += DEBUG_PrintStringA(&value.addr, count);
295 DEBUG_Printf("\n");
296 return;
297 case 'i':
298 while (count-- && DEBUG_DisassembleInstruction( &value.addr ));
299 return;
300 case 'g':
301 while (count--)
303 GUID guid;
304 if (!DEBUG_READ_MEM_VERBOSE(pnt, &guid, sizeof(guid))) break;
305 DEBUG_Printf("{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
306 guid.Data1, guid.Data2, guid.Data3,
307 guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
308 guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7] );
309 pnt += sizeof(guid);
310 value.addr.off += sizeof(guid);
311 if (count)
313 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
314 DEBUG_Printf(": ");
317 return;
319 #define DO_DUMP2(_t,_l,_f,_vv) { \
320 _t _v; \
321 for(i=0; i<count; i++) { \
322 if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
323 DEBUG_Printf(_f,(_vv)); \
324 pnt += sizeof(_t); value.addr.off += sizeof(_t); \
325 if ((i % (_l)) == (_l)-1) { \
326 DEBUG_Printf("\n"); \
327 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
328 DEBUG_Printf(": ");\
331 DEBUG_Printf("\n"); \
333 return
334 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
336 case 'x': DO_DUMP(int, 4, " %8.8x");
337 case 'd': DO_DUMP(unsigned int, 4, " %10d");
338 case 'w': DO_DUMP(unsigned short, 8, " %04x");
339 case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
340 case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);
344 #define CHARBUFSIZE 16
346 /******************************************************************
347 * DEBUG_PrintStringA
349 * Prints on channel chnl, the string starting at address in target
350 * address space. The string stops when either len chars (if <> -1)
351 * have been printed, or the '\0' char is printed
353 int DEBUG_PrintStringA(const DBG_ADDR* address, int len)
355 char* lin = (void*)DEBUG_ToLinear(address);
356 char ch[CHARBUFSIZE+1];
357 int written = 0;
359 if (len == -1) len = 32767; /* should be big enough */
361 while (written < len)
363 int to_write = min(CHARBUFSIZE, len - written );
364 if (!DEBUG_READ_MEM_VERBOSE(lin, ch, to_write)) break;
365 ch[to_write] = '\0'; /* protect from displaying junk */
366 to_write = lstrlenA(ch);
367 DEBUG_OutputA(ch, to_write);
368 lin += to_write;
369 written += to_write;
370 if (to_write < CHARBUFSIZE) break;
372 return written; /* number of actually written chars */
375 int DEBUG_PrintStringW(const DBG_ADDR* address, int len)
377 WCHAR* lin = (void*)DEBUG_ToLinear(address);
378 WCHAR ch[CHARBUFSIZE+1];
379 int written = 0;
381 if (len == -1) len = 32767; /* should be big enough */
383 while (written < len)
385 int to_write = min(CHARBUFSIZE, len - written );
386 if (!DEBUG_READ_MEM_VERBOSE(lin, ch, to_write * sizeof(WCHAR))) break;
387 ch[to_write] = 0; /* protect from displaying junk */
388 to_write = lstrlenW(ch);
389 DEBUG_OutputW(ch, to_write);
390 lin += to_write;
391 written += to_write;
392 if (to_write < CHARBUFSIZE) break;
394 return written; /* number of actually written chars */