2 * Debugger memory handling
4 * Copyright 1993 Eric Youngdale
5 * Copyright 1995 Alexandre Julliard
6 * Copyright 2000 Eric Pouech
17 #define IS_VM86_MODE() (DEBUG_context.EFlags & V86_FLAG)
20 static void DEBUG_Die(const char* msg
)
22 DEBUG_Printf(DBG_CHN_MESG
, msg
);
26 void* DEBUG_XMalloc(size_t size
)
28 void *res
= malloc(size
? size
: 1);
30 DEBUG_Die("Memory exhausted.\n");
35 void* DEBUG_XReAlloc(void *ptr
, size_t size
)
37 void* res
= realloc(ptr
, size
);
38 if ((res
== NULL
) && size
)
39 DEBUG_Die("Memory exhausted.\n");
43 char* DEBUG_XStrDup(const char *str
)
45 char *res
= strdup(str
);
47 DEBUG_Die("Memory exhausted.\n");
51 enum dbg_mode
DEBUG_GetSelectorType( WORD sel
)
56 if (IS_VM86_MODE()) return MODE_VM86
;
57 if (sel
== 0) return MODE_32
;
58 if (GetThreadSelectorEntry( DEBUG_CurrThread
->handle
, sel
, &le
))
59 return le
.HighWord
.Bits
.Default_Big
? MODE_32
: MODE_16
;
60 /* selector doesn't exist */
67 void DEBUG_FixAddress( DBG_ADDR
*addr
, DWORD def
)
69 if (addr
->seg
== 0xffffffff) addr
->seg
= def
;
70 if (DEBUG_IsSelectorSystem(addr
->seg
)) addr
->seg
= 0;
73 /* Determine if sel is a system selector (i.e. not managed by Wine) */
74 BOOL
DEBUG_IsSelectorSystem(WORD sel
)
76 if (IS_VM86_MODE()) return FALSE
; /* no system selectors in vm86 mode */
77 return !(sel
& 4) || ((sel
>> 3) < 17);
81 DWORD
DEBUG_ToLinear( const DBG_ADDR
*addr
)
86 if (IS_VM86_MODE()) return (DWORD
)(LOWORD(addr
->seg
) << 4) + addr
->off
;
88 if (DEBUG_IsSelectorSystem(addr
->seg
))
91 if (GetThreadSelectorEntry( DEBUG_CurrThread
->handle
, addr
->seg
, &le
)) {
92 return (le
.HighWord
.Bits
.BaseHi
<< 24) + (le
.HighWord
.Bits
.BaseMid
<< 16) + le
.BaseLow
+ addr
->off
;
100 void DEBUG_GetCurrentAddress( DBG_ADDR
*addr
)
103 addr
->seg
= DEBUG_context
.SegCs
;
105 if (DEBUG_IsSelectorSystem(addr
->seg
))
107 addr
->off
= DEBUG_context
.Eip
;
108 #elif defined(__sparc__)
110 addr
->off
= DEBUG_context
.pc
;
112 # error You must define GET_IP for this CPU
116 void DEBUG_InvalAddr( const DBG_ADDR
* addr
)
118 DEBUG_Printf(DBG_CHN_MESG
,"*** Invalid address ");
119 DEBUG_PrintAddress(addr
, DEBUG_CurrThread
->dbg_mode
, FALSE
);
120 DEBUG_Printf(DBG_CHN_MESG
,"\n");
121 if (DBG_IVAR(ExtDbgOnInvalidAddress
)) DEBUG_ExternalDebugger();
124 void DEBUG_InvalLinAddr( void* addr
)
129 address
.off
= (unsigned long)addr
;
130 DEBUG_InvalAddr( &address
);
133 /***********************************************************************
136 * Read a memory value.
138 /* FIXME: this function is now getting closer and closer to
139 * DEBUG_ExprGetValue. They should be merged...
141 int DEBUG_ReadMemory( const DBG_VALUE
* val
)
143 int value
= 0; /* to clear any unused byte */
144 int os
= DEBUG_GetObjectSize(val
->type
);
146 assert(sizeof(value
) >= os
);
148 /* FIXME: only works on little endian systems */
150 if (val
->cookie
== DV_TARGET
) {
151 DBG_ADDR addr
= val
->addr
;
155 DEBUG_FixAddress( &addr
, DEBUG_context
.SegDs
);
157 lin
= (void*)DEBUG_ToLinear( &addr
);
159 DEBUG_READ_MEM_VERBOSE(lin
, &value
, os
);
162 memcpy(&value
, (void*)val
->addr
.off
, os
);
168 /***********************************************************************
171 * Store a value in memory.
173 void DEBUG_WriteMemory( const DBG_VALUE
* val
, int value
)
175 int os
= DEBUG_GetObjectSize(val
->type
);
177 assert(sizeof(value
) >= os
);
179 /* FIXME: only works on little endian systems */
181 if (val
->cookie
== DV_TARGET
) {
182 DBG_ADDR addr
= val
->addr
;
186 DEBUG_FixAddress( &addr
, DEBUG_context
.SegDs
);
188 lin
= (void*)DEBUG_ToLinear( &addr
);
189 DEBUG_WRITE_MEM_VERBOSE(lin
, &value
, os
);
191 memcpy((void*)val
->addr
.off
, &value
, os
);
195 /***********************************************************************
198 * Get the address from a value
200 BOOL
DEBUG_GrabAddress( DBG_VALUE
* value
, BOOL fromCode
)
202 assert(value
->cookie
== DV_TARGET
|| value
->cookie
== DV_HOST
);
205 DEBUG_FixAddress( &value
->addr
,
206 (fromCode
) ? DEBUG_context
.SegCs
: DEBUG_context
.SegDs
);
210 * Dereference pointer to get actual memory address we need to be
211 * reading. We will use the same segment as what we have already,
212 * and hope that this is a sensible thing to do.
214 if (value
->type
!= NULL
) {
215 if (value
->type
== DEBUG_TypeIntConst
) {
217 * We know that we have the actual offset stored somewhere
218 * else in 32-bit space. Grab it, and we
221 unsigned int seg2
= value
->addr
.seg
;
223 value
->addr
.off
= DEBUG_GetExprValue(value
, NULL
);
224 value
->addr
.seg
= seg2
;
226 struct datatype
* testtype
;
228 if (DEBUG_TypeDerefPointer(value
, &testtype
) == 0)
230 if (testtype
!= NULL
|| value
->type
== DEBUG_TypeIntConst
)
231 value
->addr
.off
= DEBUG_GetExprValue(value
, NULL
);
233 } else if (!value
->addr
.seg
&& !value
->addr
.off
) {
234 DEBUG_Printf(DBG_CHN_MESG
,"Invalid expression\n");
240 /***********************************************************************
241 * DEBUG_ExamineMemory
243 * Implementation of the 'x' command.
245 void DEBUG_ExamineMemory( const DBG_VALUE
*_value
, int count
, char format
)
247 DBG_VALUE value
= *_value
;
251 if (!DEBUG_GrabAddress(&value
, (format
== 'i'))) return;
253 if (format
!= 'i' && count
> 1)
255 DEBUG_PrintAddress( &value
.addr
, DEBUG_CurrThread
->dbg_mode
, FALSE
);
256 DEBUG_Printf(DBG_CHN_MESG
,": ");
259 pnt
= (void*)DEBUG_ToLinear( &value
.addr
);
265 if (count
== 1) count
= 256;
268 if (!DEBUG_READ_MEM_VERBOSE(pnt
, &wch
, sizeof(wch
)) || !wch
)
271 DEBUG_Printf(DBG_CHN_MESG
, "%c", (char)wch
);
273 DEBUG_Printf(DBG_CHN_MESG
,"\n");
279 if (count
== 1) count
= 256;
282 if (!DEBUG_READ_MEM_VERBOSE(pnt
, &ch
, sizeof(ch
)) || !ch
)
285 DEBUG_Output(DBG_CHN_MESG
, &ch
, 1);
287 DEBUG_Printf(DBG_CHN_MESG
,"\n");
291 while (count
-- && DEBUG_DisassembleInstruction( &value
.addr
));
293 #define DO_DUMP2(_t,_l,_f,_vv) { \
295 for(i=0; i<count; i++) { \
296 if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
297 DEBUG_Printf(DBG_CHN_MESG,_f,(_vv)); \
298 pnt += sizeof(_t); value.addr.off += sizeof(_t); \
299 if ((i % (_l)) == (_l)-1) { \
300 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
301 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
302 DEBUG_Printf(DBG_CHN_MESG,": ");\
305 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
308 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
310 case 'x': DO_DUMP(int, 4, " %8.8x");
311 case 'd': DO_DUMP(unsigned int, 4, " %10d");
312 case 'w': DO_DUMP(unsigned short, 8, " %04x");
313 case 'c': DO_DUMP2(char, 32, " %c", (_v
< 0x20) ? ' ' : _v
);
314 case 'b': DO_DUMP2(char, 16, " %02x", (_v
) & 0xff);