2 * Debugger memory handling
4 * Copyright 1993 Eric Youngdale
5 * Copyright 1995 Alexandre Julliard
6 * Copyright 2000 Eric Pouech
18 #define IS_VM86_MODE() (DEBUG_context.EFlags & V86_FLAG)
21 static void DEBUG_Die(const char* msg
)
23 DEBUG_Printf(DBG_CHN_MESG
, msg
);
27 void* DEBUG_XMalloc(size_t size
)
29 void *res
= malloc(size
? size
: 1);
31 DEBUG_Die("Memory exhausted.\n");
36 void* DEBUG_XReAlloc(void *ptr
, size_t size
)
38 void* res
= realloc(ptr
, size
);
39 if ((res
== NULL
) && size
)
40 DEBUG_Die("Memory exhausted.\n");
44 char* DEBUG_XStrDup(const char *str
)
46 char *res
= strdup(str
);
48 DEBUG_Die("Memory exhausted.\n");
52 enum dbg_mode
DEBUG_GetSelectorType( WORD sel
)
57 if (IS_VM86_MODE()) return MODE_VM86
;
58 if (sel
== 0) return MODE_32
;
59 if (GetThreadSelectorEntry( DEBUG_CurrThread
->handle
, sel
, &le
))
60 return le
.HighWord
.Bits
.Default_Big
? MODE_32
: MODE_16
;
61 /* selector doesn't exist */
68 void DEBUG_FixAddress( DBG_ADDR
*addr
, DWORD def
)
70 if (addr
->seg
== 0xffffffff) addr
->seg
= def
;
71 if (DEBUG_IsSelectorSystem(addr
->seg
)) addr
->seg
= 0;
74 /* Determine if sel is a system selector (i.e. not managed by Wine) */
75 BOOL
DEBUG_IsSelectorSystem(WORD sel
)
77 if (IS_VM86_MODE()) return FALSE
; /* no system selectors in vm86 mode */
78 return !(sel
& 4) || ((sel
>> 3) < 17);
82 DWORD
DEBUG_ToLinear( const DBG_ADDR
*addr
)
87 if (IS_VM86_MODE()) return (DWORD
)(LOWORD(addr
->seg
) << 4) + addr
->off
;
89 if (DEBUG_IsSelectorSystem(addr
->seg
))
92 if (GetThreadSelectorEntry( DEBUG_CurrThread
->handle
, addr
->seg
, &le
)) {
93 return (le
.HighWord
.Bits
.BaseHi
<< 24) + (le
.HighWord
.Bits
.BaseMid
<< 16) + le
.BaseLow
+ addr
->off
;
101 void DEBUG_GetCurrentAddress( DBG_ADDR
*addr
)
104 addr
->seg
= DEBUG_context
.SegCs
;
106 if (DEBUG_IsSelectorSystem(addr
->seg
))
108 addr
->off
= DEBUG_context
.Eip
;
109 #elif defined(__sparc__)
111 addr
->off
= DEBUG_context
.pc
;
113 # error You must define GET_IP for this CPU
117 void DEBUG_InvalAddr( const DBG_ADDR
* addr
)
119 DEBUG_Printf(DBG_CHN_MESG
,"*** Invalid address ");
120 DEBUG_PrintAddress(addr
, DEBUG_CurrThread
->dbg_mode
, FALSE
);
121 DEBUG_Printf(DBG_CHN_MESG
,"\n");
122 if (DBG_IVAR(ExtDbgOnInvalidAddress
)) DEBUG_ExternalDebugger();
125 void DEBUG_InvalLinAddr( void* addr
)
130 address
.off
= (unsigned long)addr
;
131 DEBUG_InvalAddr( &address
);
134 /***********************************************************************
137 * Read a memory value.
139 /* FIXME: this function is now getting closer and closer to
140 * DEBUG_ExprGetValue. They should be merged...
142 int DEBUG_ReadMemory( const DBG_VALUE
* val
)
144 int value
= 0; /* to clear any unused byte */
145 int os
= DEBUG_GetObjectSize(val
->type
);
147 assert(sizeof(value
) >= os
);
149 /* FIXME: only works on little endian systems */
151 if (val
->cookie
== DV_TARGET
) {
152 DBG_ADDR addr
= val
->addr
;
156 DEBUG_FixAddress( &addr
, DEBUG_context
.SegDs
);
158 lin
= (void*)DEBUG_ToLinear( &addr
);
160 DEBUG_READ_MEM_VERBOSE(lin
, &value
, os
);
163 memcpy(&value
, (void*)val
->addr
.off
, os
);
169 /***********************************************************************
172 * Store a value in memory.
174 void DEBUG_WriteMemory( const DBG_VALUE
* val
, int value
)
176 int os
= DEBUG_GetObjectSize(val
->type
);
178 assert(sizeof(value
) >= os
);
180 /* FIXME: only works on little endian systems */
182 if (val
->cookie
== DV_TARGET
) {
183 DBG_ADDR addr
= val
->addr
;
187 DEBUG_FixAddress( &addr
, DEBUG_context
.SegDs
);
189 lin
= (void*)DEBUG_ToLinear( &addr
);
190 DEBUG_WRITE_MEM_VERBOSE(lin
, &value
, os
);
192 memcpy((void*)val
->addr
.off
, &value
, os
);
196 /***********************************************************************
199 * Get the address from a value
201 BOOL
DEBUG_GrabAddress( DBG_VALUE
* value
, BOOL fromCode
)
203 assert(value
->cookie
== DV_TARGET
|| value
->cookie
== DV_HOST
);
206 DEBUG_FixAddress( &value
->addr
,
207 (fromCode
) ? DEBUG_context
.SegCs
: DEBUG_context
.SegDs
);
211 * Dereference pointer to get actual memory address we need to be
212 * reading. We will use the same segment as what we have already,
213 * and hope that this is a sensible thing to do.
215 if (value
->type
!= NULL
) {
216 if (value
->type
== DEBUG_TypeIntConst
) {
218 * We know that we have the actual offset stored somewhere
219 * else in 32-bit space. Grab it, and we
222 unsigned int seg2
= value
->addr
.seg
;
224 value
->addr
.off
= DEBUG_GetExprValue(value
, NULL
);
225 value
->addr
.seg
= seg2
;
227 struct datatype
* testtype
;
229 if (DEBUG_TypeDerefPointer(value
, &testtype
) == 0)
231 if (testtype
!= NULL
|| value
->type
== DEBUG_TypeIntConst
)
232 value
->addr
.off
= DEBUG_GetExprValue(value
, NULL
);
234 } else if (!value
->addr
.seg
&& !value
->addr
.off
) {
235 DEBUG_Printf(DBG_CHN_MESG
,"Invalid expression\n");
241 /***********************************************************************
242 * DEBUG_ExamineMemory
244 * Implementation of the 'x' command.
246 void DEBUG_ExamineMemory( const DBG_VALUE
*_value
, int count
, char format
)
248 DBG_VALUE value
= *_value
;
252 if (!DEBUG_GrabAddress(&value
, (format
== 'i'))) return;
254 if (format
!= 'i' && count
> 1)
256 DEBUG_PrintAddress( &value
.addr
, DEBUG_CurrThread
->dbg_mode
, FALSE
);
257 DEBUG_Printf(DBG_CHN_MESG
,": ");
260 pnt
= (void*)DEBUG_ToLinear( &value
.addr
);
266 if (count
== 1) count
= 256;
269 if (!DEBUG_READ_MEM_VERBOSE(pnt
, &wch
, sizeof(wch
)) || !wch
)
272 DEBUG_Printf(DBG_CHN_MESG
, "%c", (char)wch
);
274 DEBUG_Printf(DBG_CHN_MESG
,"\n");
280 if (count
== 1) count
= 256;
283 if (!DEBUG_READ_MEM_VERBOSE(pnt
, &ch
, sizeof(ch
)) || !ch
)
286 DEBUG_Output(DBG_CHN_MESG
, &ch
, 1);
288 DEBUG_Printf(DBG_CHN_MESG
,"\n");
292 while (count
-- && DEBUG_DisassembleInstruction( &value
.addr
));
294 #define DO_DUMP2(_t,_l,_f,_vv) { \
296 for(i=0; i<count; i++) { \
297 if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
298 DEBUG_Printf(DBG_CHN_MESG,_f,(_vv)); \
299 pnt += sizeof(_t); value.addr.off += sizeof(_t); \
300 if ((i % (_l)) == (_l)-1) { \
301 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
302 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
303 DEBUG_Printf(DBG_CHN_MESG,": ");\
306 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
309 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
311 case 'x': DO_DUMP(int, 4, " %8.8x");
312 case 'd': DO_DUMP(unsigned int, 4, " %10d");
313 case 'w': DO_DUMP(unsigned short, 8, " %04x");
314 case 'c': DO_DUMP2(char, 32, " %c", (_v
< 0x20) ? ' ' : _v
);
315 case 'b': DO_DUMP2(char, 16, " %02x", (_v
) & 0xff);