2 * Debugger memory handling
4 * Copyright 1993 Eric Youngdale
5 * Copyright 1995 Alexandre Julliard
6 * Copyright 2000 Eric Pouech
19 #include "wine/winbase16.h"
21 #define DBG_V86_MODULE(seg) ((seg)>>16)
22 #define IS_SELECTOR_V86(seg) DBG_V86_MODULE(seg)
24 static void DEBUG_Die(const char* msg
)
30 void* DEBUG_XMalloc(size_t size
)
32 void *res
= malloc(size
? size
: 1);
34 DEBUG_Die("Memory exhausted.\n");
39 void* DEBUG_XReAlloc(void *ptr
, size_t size
)
41 void* res
= realloc(ptr
, size
);
42 if ((res
== NULL
) && size
)
43 DEBUG_Die("Memory exhausted.\n");
47 char* DEBUG_XStrDup(const char *str
)
49 char *res
= strdup(str
);
51 DEBUG_Die("Memory exhausted.\n");
55 void DEBUG_FixAddress( DBG_ADDR
*addr
, DWORD def
)
57 if (addr
->seg
== 0xffffffff) addr
->seg
= def
;
58 if (!IS_SELECTOR_V86(addr
->seg
) && DEBUG_IsSelectorSystem(addr
->seg
)) addr
->seg
= 0;
61 BOOL
DEBUG_FixSegment( DBG_ADDR
* addr
)
64 if (DEBUG_context
.EFlags
& V86_FLAG
) {
65 addr
->seg
|= (DWORD
)(GetExePtr(GetCurrentTask())) << 16;
71 DWORD
DEBUG_ToLinear( const DBG_ADDR
*addr
)
75 if (IS_SELECTOR_V86(addr
->seg
))
76 return (DWORD
) DOSMEM_MemoryBase(DBG_V86_MODULE(addr
->seg
)) + (((addr
->seg
)&0xFFFF)<<4) + addr
->off
;
77 if (DEBUG_IsSelectorSystem(addr
->seg
))
80 if (GetThreadSelectorEntry( DEBUG_CurrThread
->handle
, addr
->seg
, &le
)) {
81 return (le
.HighWord
.Bits
.BaseHi
<< 24) + (le
.HighWord
.Bits
.BaseMid
<< 16) + le
.BaseLow
+ addr
->off
;
86 int DEBUG_GetSelectorType( WORD sel
)
92 if (IS_SELECTOR_V86(sel
))
94 if (GetThreadSelectorEntry( DEBUG_CurrThread
->handle
, sel
, &le
))
95 return le
.HighWord
.Bits
.Default_Big
? 32 : 16;
96 /* selector doesn't exist */
100 /* Determine if sel is a system selector (i.e. not managed by Wine) */
101 BOOL
DEBUG_IsSelectorSystem(WORD sel
)
103 return !(sel
& 4) || (((sel
& 0xFFFF) >> 3) < 17);
105 #endif /* __i386__ */
107 void DEBUG_GetCurrentAddress( DBG_ADDR
*addr
)
110 addr
->seg
= DEBUG_context
.SegCs
;
112 if (!DEBUG_FixSegment( addr
) && DEBUG_IsSelectorSystem(addr
->seg
))
114 addr
->off
= DEBUG_context
.Eip
;
121 void DEBUG_InvalLinAddr( void* addr
)
126 address
.off
= (unsigned long)addr
;
128 fprintf(stderr
,"*** Invalid address ");
129 DEBUG_PrintAddress(&address
, DEBUG_CurrThread
->dbg_mode
, FALSE
);
130 fprintf(stderr
,"\n");
133 /***********************************************************************
136 * Read a memory value.
138 int DEBUG_ReadMemory( const DBG_ADDR
*address
)
140 DBG_ADDR addr
= *address
;
144 DEBUG_FixAddress( &addr
, DEBUG_context
.SegDs
);
145 lin
= (void*)DEBUG_ToLinear( &addr
);
146 if (!DEBUG_READ_MEM_VERBOSE(lin
, &value
, sizeof(value
)))
152 /***********************************************************************
155 * Store a value in memory.
157 void DEBUG_WriteMemory( const DBG_ADDR
*address
, int value
)
159 DBG_ADDR addr
= *address
;
162 DEBUG_FixAddress( &addr
, DEBUG_context
.SegDs
);
163 lin
= (void*)DEBUG_ToLinear( &addr
);
164 DEBUG_WRITE_MEM_VERBOSE(lin
, &value
, sizeof(value
));
168 /***********************************************************************
169 * DEBUG_ExamineMemory
171 * Implementation of the 'x' command.
173 void DEBUG_ExamineMemory( const DBG_VALUE
*_value
, int count
, char format
)
175 DBG_VALUE value
= *_value
;
178 struct datatype
* testtype
;
180 assert(_value
->cookie
== DV_TARGET
|| _value
->cookie
== DV_HOST
);
182 DEBUG_FixAddress( &value
.addr
,
184 DEBUG_context
.SegCs
:
185 DEBUG_context
.SegDs
);
188 * Dereference pointer to get actual memory address we need to be
189 * reading. We will use the same segment as what we have already,
190 * and hope that this is a sensible thing to do.
192 if( value
.type
!= NULL
)
194 if( value
.type
== DEBUG_TypeIntConst
)
197 * We know that we have the actual offset stored somewhere
198 * else in 32-bit space. Grab it, and we
201 unsigned int seg2
= value
.addr
.seg
;
203 value
.addr
.off
= DEBUG_GetExprValue(&value
, NULL
);
204 value
.addr
.seg
= seg2
;
208 if (DEBUG_TypeDerefPointer(&value
, &testtype
) == 0)
210 if( testtype
!= NULL
|| value
.type
== DEBUG_TypeIntConst
)
212 value
.addr
.off
= DEBUG_GetExprValue(&value
, NULL
);
216 else if (!value
.addr
.seg
&& !value
.addr
.off
)
218 fprintf(stderr
,"Invalid expression\n");
222 if (format
!= 'i' && count
> 1)
224 DEBUG_PrintAddress( &value
.addr
, DEBUG_CurrThread
->dbg_mode
, FALSE
);
225 fprintf(stderr
,": ");
228 pnt
= (void*)DEBUG_ToLinear( &value
.addr
);
234 if (count
== 1) count
= 256;
237 if (!DEBUG_READ_MEM_VERBOSE(pnt
, &wch
, sizeof(wch
)))
240 fputc( (char)wch
, stderr
);
242 fprintf(stderr
,"\n");
248 if (count
== 1) count
= 256;
251 if (!DEBUG_READ_MEM_VERBOSE(pnt
, &ch
, sizeof(ch
)))
256 fprintf(stderr
,"\n");
262 DEBUG_PrintAddress( &value
.addr
, DEBUG_CurrThread
->dbg_mode
, TRUE
);
263 fprintf(stderr
,": ");
264 DEBUG_Disasm( &value
.addr
, TRUE
);
265 fprintf(stderr
,"\n");
268 #define DO_DUMP2(_t,_l,_f,_vv) { \
270 for(i=0; i<count; i++) { \
271 if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
272 fprintf(stderr,_f,(_vv)); \
273 pnt += sizeof(_t); value.addr.off += sizeof(_t); \
274 if ((i % (_l)) == (_l)-1) { \
275 fprintf(stderr,"\n"); \
276 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
277 fprintf(stderr,": ");\
280 fprintf(stderr,"\n"); \
283 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
285 case 'x': DO_DUMP(int, 4, " %8.8x");
286 case 'd': DO_DUMP(unsigned int, 4, " %10d");
287 case 'w': DO_DUMP(unsigned short, 8, " %04x");
288 case 'c': DO_DUMP2(char, 32, " %c", (_v
< 0x20) ? ' ' : _v
);
289 case 'b': DO_DUMP2(char, 16, " %02x", (_v
) & 0xff);