2 * Debugger memory handling
4 * Copyright 1993 Eric Youngdale
5 * Copyright 1995 Alexandre Julliard
6 * Copyright 2000 Eric Pouech
18 #include "wine/winbase16.h"
20 #define DBG_V86_MODULE(seg) ((seg)>>16)
21 #define IS_SELECTOR_V86(seg) DBG_V86_MODULE(seg)
23 static void DEBUG_Die(const char* msg
)
25 DEBUG_Printf(DBG_CHN_MESG
, msg
);
29 void* DEBUG_XMalloc(size_t size
)
31 void *res
= malloc(size
? size
: 1);
33 DEBUG_Die("Memory exhausted.\n");
38 void* DEBUG_XReAlloc(void *ptr
, size_t size
)
40 void* res
= realloc(ptr
, size
);
41 if ((res
== NULL
) && size
)
42 DEBUG_Die("Memory exhausted.\n");
46 char* DEBUG_XStrDup(const char *str
)
48 char *res
= strdup(str
);
50 DEBUG_Die("Memory exhausted.\n");
54 void DEBUG_FixAddress( DBG_ADDR
*addr
, DWORD def
)
56 if (addr
->seg
== 0xffffffff) addr
->seg
= def
;
57 if (!IS_SELECTOR_V86(addr
->seg
) && DEBUG_IsSelectorSystem(addr
->seg
)) addr
->seg
= 0;
60 BOOL
DEBUG_FixSegment( DBG_ADDR
* addr
)
63 if (DEBUG_context
.EFlags
& V86_FLAG
) {
64 addr
->seg
|= (DWORD
)(GetExePtr(GetCurrentTask())) << 16;
70 DWORD
DEBUG_ToLinear( const DBG_ADDR
*addr
)
74 if (IS_SELECTOR_V86(addr
->seg
))
75 return (DWORD
) DOSMEM_MemoryBase(DBG_V86_MODULE(addr
->seg
)) + (((addr
->seg
)&0xFFFF)<<4) + addr
->off
;
76 if (DEBUG_IsSelectorSystem(addr
->seg
))
79 if (GetThreadSelectorEntry( DEBUG_CurrThread
->handle
, addr
->seg
, &le
)) {
80 return (le
.HighWord
.Bits
.BaseHi
<< 24) + (le
.HighWord
.Bits
.BaseMid
<< 16) + le
.BaseLow
+ addr
->off
;
85 int DEBUG_GetSelectorType( WORD sel
)
91 if (IS_SELECTOR_V86(sel
))
93 if (GetThreadSelectorEntry( DEBUG_CurrThread
->handle
, sel
, &le
))
94 return le
.HighWord
.Bits
.Default_Big
? 32 : 16;
95 /* selector doesn't exist */
99 /* Determine if sel is a system selector (i.e. not managed by Wine) */
100 BOOL
DEBUG_IsSelectorSystem(WORD sel
)
102 return !(sel
& 4) || (((sel
& 0xFFFF) >> 3) < 17);
104 #endif /* __i386__ */
106 void DEBUG_GetCurrentAddress( DBG_ADDR
*addr
)
109 addr
->seg
= DEBUG_context
.SegCs
;
111 if (!DEBUG_FixSegment( addr
) && DEBUG_IsSelectorSystem(addr
->seg
))
113 addr
->off
= DEBUG_context
.Eip
;
120 void DEBUG_InvalAddr( const DBG_ADDR
* addr
)
122 DEBUG_Printf(DBG_CHN_MESG
,"*** Invalid address ");
123 DEBUG_PrintAddress(addr
, DEBUG_CurrThread
->dbg_mode
, FALSE
);
124 DEBUG_Printf(DBG_CHN_MESG
,"\n");
125 if (DBG_IVAR(ExtDbgOnInvalidAddress
)) DEBUG_ExternalDebugger();
128 void DEBUG_InvalLinAddr( void* addr
)
133 address
.off
= (unsigned long)addr
;
134 DEBUG_InvalAddr( &address
);
137 /***********************************************************************
140 * Read a memory value.
142 /* FIXME: this function is now getting closer and closer to
143 * DEBUG_ExprGetValue. They should be merged...
145 int DEBUG_ReadMemory( const DBG_VALUE
* val
)
147 int value
= 0; /* to clear any unused byte */
148 int os
= DEBUG_GetObjectSize(val
->type
);
150 assert(sizeof(value
) >= os
);
152 /* FIXME: only works on little endian systems */
154 if (val
->cookie
== DV_TARGET
) {
155 DBG_ADDR addr
= val
->addr
;
158 DEBUG_FixAddress( &addr
, DEBUG_context
.SegDs
);
159 lin
= (void*)DEBUG_ToLinear( &addr
);
161 DEBUG_READ_MEM_VERBOSE(lin
, &value
, os
);
164 memcpy(&value
, (void*)val
->addr
.off
, os
);
170 /***********************************************************************
173 * Store a value in memory.
175 void DEBUG_WriteMemory( const DBG_VALUE
* val
, int value
)
177 int os
= DEBUG_GetObjectSize(val
->type
);
179 assert(sizeof(value
) >= os
);
181 /* FIXME: only works on little endian systems */
183 if (val
->cookie
== DV_TARGET
) {
184 DBG_ADDR addr
= val
->addr
;
187 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
);
196 /***********************************************************************
197 * DEBUG_ExamineMemory
199 * Implementation of the 'x' command.
201 void DEBUG_ExamineMemory( const DBG_VALUE
*_value
, int count
, char format
)
203 DBG_VALUE value
= *_value
;
206 struct datatype
* testtype
;
208 assert(_value
->cookie
== DV_TARGET
|| _value
->cookie
== DV_HOST
);
210 DEBUG_FixAddress( &value
.addr
,
212 DEBUG_context
.SegCs
:
213 DEBUG_context
.SegDs
);
216 * Dereference pointer to get actual memory address we need to be
217 * reading. We will use the same segment as what we have already,
218 * and hope that this is a sensible thing to do.
220 if( value
.type
!= NULL
)
222 if( value
.type
== DEBUG_TypeIntConst
)
225 * We know that we have the actual offset stored somewhere
226 * else in 32-bit space. Grab it, and we
229 unsigned int seg2
= value
.addr
.seg
;
231 value
.addr
.off
= DEBUG_GetExprValue(&value
, NULL
);
232 value
.addr
.seg
= seg2
;
236 if (DEBUG_TypeDerefPointer(&value
, &testtype
) == 0)
238 if( testtype
!= NULL
|| value
.type
== DEBUG_TypeIntConst
)
240 value
.addr
.off
= DEBUG_GetExprValue(&value
, NULL
);
244 else if (!value
.addr
.seg
&& !value
.addr
.off
)
246 DEBUG_Printf(DBG_CHN_MESG
,"Invalid expression\n");
250 if (format
!= 'i' && count
> 1)
252 DEBUG_PrintAddress( &value
.addr
, DEBUG_CurrThread
->dbg_mode
, FALSE
);
253 DEBUG_Printf(DBG_CHN_MESG
,": ");
256 pnt
= (void*)DEBUG_ToLinear( &value
.addr
);
262 if (count
== 1) count
= 256;
265 if (!DEBUG_READ_MEM_VERBOSE(pnt
, &wch
, sizeof(wch
)) || !wch
)
268 DEBUG_Printf(DBG_CHN_MESG
, "%c", (char)wch
);
270 DEBUG_Printf(DBG_CHN_MESG
,"\n");
276 if (count
== 1) count
= 256;
279 if (!DEBUG_READ_MEM_VERBOSE(pnt
, &ch
, sizeof(ch
)) || !ch
)
282 DEBUG_Output(DBG_CHN_MESG
, &ch
, 1);
284 DEBUG_Printf(DBG_CHN_MESG
,"\n");
290 DEBUG_PrintAddress( &value
.addr
, DEBUG_CurrThread
->dbg_mode
, TRUE
);
291 DEBUG_Printf(DBG_CHN_MESG
,": ");
292 DEBUG_Disasm( &value
.addr
, TRUE
);
293 DEBUG_Printf(DBG_CHN_MESG
,"\n");
296 #define DO_DUMP2(_t,_l,_f,_vv) { \
298 for(i=0; i<count; i++) { \
299 if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
300 DEBUG_Printf(DBG_CHN_MESG,_f,(_vv)); \
301 pnt += sizeof(_t); value.addr.off += sizeof(_t); \
302 if ((i % (_l)) == (_l)-1) { \
303 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
304 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
305 DEBUG_Printf(DBG_CHN_MESG,": ");\
308 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
311 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
313 case 'x': DO_DUMP(int, 4, " %8.8x");
314 case 'd': DO_DUMP(unsigned int, 4, " %10d");
315 case 'w': DO_DUMP(unsigned short, 8, " %04x");
316 case 'c': DO_DUMP2(char, 32, " %c", (_v
< 0x20) ? ' ' : _v
);
317 case 'b': DO_DUMP2(char, 16, " %02x", (_v
) & 0xff);