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
24 #include "wine/port.h"
33 #define IS_VM86_MODE() (DEBUG_context.EFlags & V86_FLAG)
36 static void DEBUG_Die(const char* msg
)
42 void* DBG_alloc(size_t size
)
44 void *res
= malloc(size
? size
: 1);
46 DEBUG_Die("Memory exhausted.\n");
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");
59 char* DBG_strdup(const char *str
)
61 char *res
= strdup(str
);
63 DEBUG_Die("Memory exhausted.\n");
67 void DBG_free(void *ptr
)
72 enum dbg_mode
DEBUG_GetSelectorType( WORD sel
)
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 */
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
)
107 if (IS_VM86_MODE()) return (DWORD
)(LOWORD(addr
->seg
) << 4) + addr
->off
;
109 if (DEBUG_IsSelectorSystem(addr
->seg
))
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
;
121 void DEBUG_GetCurrentAddress( DBG_ADDR
*addr
)
124 addr
->seg
= DEBUG_context
.SegCs
;
126 if (DEBUG_IsSelectorSystem(addr
->seg
))
128 addr
->off
= DEBUG_context
.Eip
;
129 #elif defined(__sparc__)
131 addr
->off
= DEBUG_context
.pc
;
132 #elif defined(__powerpc__)
134 addr
->off
= DEBUG_context
.Iar
;
136 # error You must define GET_IP for this CPU
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
);
145 if (DBG_IVAR(ExtDbgOnInvalidAddress
)) DEBUG_ExternalDebugger();
148 void DEBUG_InvalLinAddr( void* addr
)
153 address
.off
= (unsigned long)addr
;
154 DEBUG_InvalAddr( &address
);
157 /***********************************************************************
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
;
179 DEBUG_FixAddress( &addr
, DEBUG_context
.SegDs
);
181 lin
= (void*)DEBUG_ToLinear( &addr
);
183 DEBUG_READ_MEM_VERBOSE(lin
, &value
, os
);
186 memcpy(&value
, (void*)val
->addr
.off
, os
);
192 /***********************************************************************
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
;
210 DEBUG_FixAddress( &addr
, DEBUG_context
.SegDs
);
212 lin
= (void*)DEBUG_ToLinear( &addr
);
213 DEBUG_WRITE_MEM_VERBOSE(lin
, &value
, os
);
215 memcpy((void*)val
->addr
.off
, &value
, os
);
219 /***********************************************************************
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
);
229 DEBUG_FixAddress( &value
->addr
,
230 (fromCode
) ? DEBUG_context
.SegCs
: DEBUG_context
.SegDs
);
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
245 unsigned int seg2
= value
->addr
.seg
;
247 value
->addr
.off
= DEBUG_GetExprValue(value
, NULL
);
248 value
->addr
.seg
= seg2
;
250 struct datatype
* testtype
;
252 if (DEBUG_TypeDerefPointer(value
, &testtype
) == 0)
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");
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
;
275 if (!DEBUG_GrabAddress(&value
, (format
== 'i'))) return;
277 if (format
!= 'i' && count
> 1)
279 DEBUG_PrintAddress( &value
.addr
, DEBUG_CurrThread
->dbg_mode
, FALSE
);
283 pnt
= (void*)DEBUG_ToLinear( &value
.addr
);
288 if (count
== 1) count
= 256;
289 DEBUG_nchar
+= DEBUG_PrintStringW(&value
.addr
, count
);
293 if (count
== 1) count
= 256;
294 DEBUG_nchar
+= DEBUG_PrintStringA(&value
.addr
, count
);
298 while (count
-- && DEBUG_DisassembleInstruction( &value
.addr
));
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] );
310 value
.addr
.off
+= sizeof(guid
);
313 DEBUG_PrintAddress( &value
.addr
, DEBUG_CurrThread
->dbg_mode
, FALSE
);
319 #define DO_DUMP2(_t,_l,_f,_vv) { \
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 );\
331 DEBUG_Printf("\n"); \
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 /******************************************************************
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];
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
);
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];
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
);
392 if (to_write
< CHARBUFSIZE
) break;
394 return written
; /* number of actually written chars */