2 * Debugger memory handling
4 * Copyright 1993 Eric Youngdale
5 * Copyright 1995 Alexandre Julliard
16 #include "wine/winbase16.h"
18 #define DBG_V86_MODULE(seg) ((seg)>>16)
19 #define IS_SELECTOR_V86(seg) DBG_V86_MODULE(seg)
21 static void DEBUG_Die(const char* 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 void DEBUG_FixAddress( DBG_ADDR *addr, DWORD def)
54 if (addr->seg == 0xffffffff) addr->seg = def;
55 if (!IS_SELECTOR_V86(addr->seg) && DEBUG_IsSelectorSystem(addr->seg)) addr->seg = 0;
58 BOOL DEBUG_FixSegment( DBG_ADDR* addr )
61 if (DEBUG_context.EFlags & V86_FLAG) {
62 addr->seg |= (DWORD)(GetExePtr(GetCurrentTask())) << 16;
68 DWORD DEBUG_ToLinear( const DBG_ADDR *addr )
72 if (IS_SELECTOR_V86(addr->seg))
73 return DOSMEM_MemoryBase(DBG_V86_MODULE(addr->seg)) + (((addr->seg)&0xFFFF)<<4) + addr->off;
74 if (DEBUG_IsSelectorSystem(addr->seg))
77 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, addr->seg, &le)) {
78 return (le.HighWord.Bits.BaseHi << 24) + (le.HighWord.Bits.BaseMid << 16) + le.BaseLow + addr->off;
83 int DEBUG_GetSelectorType( WORD sel )
89 if (IS_SELECTOR_V86(sel))
91 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, sel, &le))
92 return le.HighWord.Bits.Default_Big ? 32 : 16;
93 /* selector doesn't exist */
97 /* Determine if sel is a system selector (i.e. not managed by Wine) */
98 BOOL DEBUG_IsSelectorSystem(WORD sel)
100 return !(sel & 4) || (((sel & 0xFFFF) >> 3) < 17);
102 #endif /* __i386__ */
104 void DEBUG_GetCurrentAddress( DBG_ADDR *addr )
108 addr->seg = DEBUG_context.SegCs;
110 if (!DEBUG_FixSegment( addr ) && DEBUG_IsSelectorSystem(addr->seg))
112 addr->off = DEBUG_context.Eip;
119 void DEBUG_InvalLinAddr( void* addr )
125 address.off = (unsigned long)addr;
127 fprintf(stderr,"*** Invalid address ");
128 DEBUG_PrintAddress(&address, DEBUG_CurrThread->dbg_mode, FALSE);
129 fprintf(stderr,"\n");
132 /***********************************************************************
135 * Read a memory value.
137 int DEBUG_ReadMemory( const DBG_ADDR *address )
139 DBG_ADDR addr = *address;
143 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
144 lin = (void*)DEBUG_ToLinear( &addr );
145 if (!DEBUG_READ_MEM_VERBOSE(lin, &value, sizeof(value)))
151 /***********************************************************************
154 * Store a value in memory.
156 void DEBUG_WriteMemory( const DBG_ADDR *address, int value )
158 DBG_ADDR addr = *address;
161 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
162 lin = (void*)DEBUG_ToLinear( &addr );
163 DEBUG_WRITE_MEM_VERBOSE(lin, &value, sizeof(value));
167 /***********************************************************************
168 * DEBUG_ExamineMemory
170 * Implementation of the 'x' command.
172 void DEBUG_ExamineMemory( const DBG_ADDR *address, int count, char format )
174 DBG_ADDR addr = * address;
177 struct datatype * testtype;
179 DEBUG_FixAddress( &addr,
181 DEBUG_context.SegCs :
182 DEBUG_context.SegDs );
185 * Dereference pointer to get actual memory address we need to be
186 * reading. We will use the same segment as what we have already,
187 * and hope that this is a sensible thing to do.
189 if( addr.type != NULL )
191 if( addr.type == DEBUG_TypeIntConst )
194 * We know that we have the actual offset stored somewhere
195 * else in 32-bit space. Grab it, and we
198 unsigned int seg2 = addr.seg;
200 addr.off = DEBUG_GetExprValue(&addr, NULL);
205 if (DEBUG_TypeDerefPointer(&addr, &testtype) == 0)
207 if( testtype != NULL || addr.type == DEBUG_TypeIntConst )
209 addr.off = DEBUG_GetExprValue(&addr, NULL);
213 else if (!addr.seg && !addr.off)
215 fprintf(stderr,"Invalid expression\n");
219 if (format != 'i' && count > 1)
221 DEBUG_PrintAddress( &addr, DEBUG_CurrThread->dbg_mode, FALSE );
222 fprintf(stderr,": ");
225 pnt = (void*)DEBUG_ToLinear( &addr );
231 if (count == 1) count = 256;
234 if (!DEBUG_READ_MEM_VERBOSE(pnt, &wch, sizeof(wch)))
237 fputc( (char)wch, stderr );
239 fprintf(stderr,"\n");
245 if (count == 1) count = 256;
248 if (!DEBUG_READ_MEM_VERBOSE(pnt, &ch, sizeof(ch)))
253 fprintf(stderr,"\n");
259 DEBUG_PrintAddress( &addr, DEBUG_CurrThread->dbg_mode, TRUE );
260 fprintf(stderr,": ");
261 DEBUG_Disasm( &addr, TRUE );
262 fprintf(stderr,"\n");
265 #define DO_DUMP2(_t,_l,_f,_vv) { \
267 for(i=0; i<count; i++) { \
268 if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
269 fprintf(stderr,_f,(_vv)); \
270 pnt += sizeof(_t); addr.off += sizeof(_t); \
271 if ((i % (_l)) == (_l)-1) { \
272 fprintf(stderr,"\n"); \
273 DEBUG_PrintAddress( &addr, DEBUG_CurrThread->dbg_mode, FALSE );\
274 fprintf(stderr,": ");\
277 fprintf(stderr,"\n"); \
280 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
282 case 'x': DO_DUMP(int, 4, " %8.8x");
283 case 'd': DO_DUMP(unsigned int, 4, " %10d");
284 case 'w': DO_DUMP(unsigned short, 8, " %04x");
285 case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
286 case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);