Fixed GetProcessHeap() for the STRICT mode.
[wine/testsucceed.git] / debugger / memory.c
blob4889ce7b26474f96b961b5e43a3b509a0ae50849
1 /*
2 * Debugger memory handling
4 * Copyright 1993 Eric Youngdale
5 * Copyright 1995 Alexandre Julliard
6 * Copyright 2000 Eric Pouech
7 */
9 #include "config.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
14 #include "debugger.h"
15 #include "miscemu.h"
16 #include "winbase.h"
18 #ifdef __i386__
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)
26 DEBUG_Printf(DBG_CHN_MESG, msg);
27 exit(1);
30 void* DEBUG_XMalloc(size_t size)
32 void *res = malloc(size ? size : 1);
33 if (res == NULL)
34 DEBUG_Die("Memory exhausted.\n");
35 memset(res, 0, size);
36 return res;
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");
44 return res;
47 char* DEBUG_XStrDup(const char *str)
49 char *res = strdup(str);
50 if (!res)
51 DEBUG_Die("Memory exhausted.\n");
52 return res;
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 )
63 /* V86 mode ? */
64 if (DEBUG_context.EFlags & V86_FLAG) {
65 addr->seg |= (DWORD)(GetExePtr(GetCurrentTask())) << 16;
66 return TRUE;
68 return FALSE;
71 DWORD DEBUG_ToLinear( const DBG_ADDR *addr )
73 LDT_ENTRY le;
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))
78 return addr->off;
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;
83 return 0;
86 int DEBUG_GetSelectorType( WORD sel )
88 LDT_ENTRY le;
90 if (sel == 0)
91 return 32;
92 if (IS_SELECTOR_V86(sel))
93 return 16;
94 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, sel, &le))
95 return le.HighWord.Bits.Default_Big ? 32 : 16;
96 /* selector doesn't exist */
97 return 0;
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 )
109 #ifdef __i386__
110 addr->seg = DEBUG_context.SegCs;
112 if (!DEBUG_FixSegment( addr ) && DEBUG_IsSelectorSystem(addr->seg))
113 addr->seg = 0;
114 addr->off = DEBUG_context.Eip;
115 #else
116 addr->seg = 0;
117 addr->off = 0;
118 #endif
121 void DEBUG_InvalAddr( const DBG_ADDR* addr )
123 DEBUG_Printf(DBG_CHN_MESG,"*** Invalid address ");
124 DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
125 DEBUG_Printf(DBG_CHN_MESG,"\n");
126 if (DBG_IVAR(ExtDbgOnInvalidAddress)) DEBUG_ExternalDebugger();
129 void DEBUG_InvalLinAddr( void* addr )
131 DBG_ADDR address;
133 address.seg = 0;
134 address.off = (unsigned long)addr;
135 DEBUG_InvalAddr( &address );
138 /***********************************************************************
139 * DEBUG_ReadMemory
141 * Read a memory value.
143 int DEBUG_ReadMemory( const DBG_ADDR *address )
145 DBG_ADDR addr = *address;
146 void* lin;
147 int value;
149 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
150 lin = (void*)DEBUG_ToLinear( &addr );
151 if (!DEBUG_READ_MEM_VERBOSE(lin, &value, sizeof(value)))
152 value = 0;
153 return value;
157 /***********************************************************************
158 * DEBUG_WriteMemory
160 * Store a value in memory.
162 void DEBUG_WriteMemory( const DBG_ADDR *address, int value )
164 DBG_ADDR addr = *address;
165 void* lin;
167 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
168 lin = (void*)DEBUG_ToLinear( &addr );
169 DEBUG_WRITE_MEM_VERBOSE(lin, &value, sizeof(value));
173 /***********************************************************************
174 * DEBUG_ExamineMemory
176 * Implementation of the 'x' command.
178 void DEBUG_ExamineMemory( const DBG_VALUE *_value, int count, char format )
180 DBG_VALUE value = *_value;
181 int i;
182 unsigned char * pnt;
183 struct datatype * testtype;
185 assert(_value->cookie == DV_TARGET || _value->cookie == DV_HOST);
187 DEBUG_FixAddress( &value.addr,
188 (format == 'i') ?
189 DEBUG_context.SegCs :
190 DEBUG_context.SegDs );
193 * Dereference pointer to get actual memory address we need to be
194 * reading. We will use the same segment as what we have already,
195 * and hope that this is a sensible thing to do.
197 if( value.type != NULL )
199 if( value.type == DEBUG_TypeIntConst )
202 * We know that we have the actual offset stored somewhere
203 * else in 32-bit space. Grab it, and we
204 * should be all set.
206 unsigned int seg2 = value.addr.seg;
207 value.addr.seg = 0;
208 value.addr.off = DEBUG_GetExprValue(&value, NULL);
209 value.addr.seg = seg2;
211 else
213 if (DEBUG_TypeDerefPointer(&value, &testtype) == 0)
214 return;
215 if( testtype != NULL || value.type == DEBUG_TypeIntConst )
217 value.addr.off = DEBUG_GetExprValue(&value, NULL);
221 else if (!value.addr.seg && !value.addr.off)
223 DEBUG_Printf(DBG_CHN_MESG,"Invalid expression\n");
224 return;
227 if (format != 'i' && count > 1)
229 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
230 DEBUG_Printf(DBG_CHN_MESG,": ");
233 pnt = (void*)DEBUG_ToLinear( &value.addr );
235 switch(format)
237 case 'u': {
238 WCHAR wch;
239 if (count == 1) count = 256;
240 while (count--)
242 if (!DEBUG_READ_MEM_VERBOSE(pnt, &wch, sizeof(wch)))
243 break;
244 pnt += sizeof(wch);
245 fputc( (char)wch, stderr );
247 DEBUG_Printf(DBG_CHN_MESG,"\n");
248 return;
250 case 's': {
251 char ch;
253 if (count == 1) count = 256;
254 while (count--)
256 if (!DEBUG_READ_MEM_VERBOSE(pnt, &ch, sizeof(ch)))
257 break;
258 pnt++;
259 fputc( ch, stderr );
261 DEBUG_Printf(DBG_CHN_MESG,"\n");
262 return;
264 case 'i':
265 while (count--)
267 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, TRUE );
268 DEBUG_Printf(DBG_CHN_MESG,": ");
269 DEBUG_Disasm( &value.addr, TRUE );
270 DEBUG_Printf(DBG_CHN_MESG,"\n");
272 return;
273 #define DO_DUMP2(_t,_l,_f,_vv) { \
274 _t _v; \
275 for(i=0; i<count; i++) { \
276 if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
277 DEBUG_Printf(DBG_CHN_MESG,_f,(_vv)); \
278 pnt += sizeof(_t); value.addr.off += sizeof(_t); \
279 if ((i % (_l)) == (_l)-1) { \
280 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
281 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
282 DEBUG_Printf(DBG_CHN_MESG,": ");\
285 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
287 return
288 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
290 case 'x': DO_DUMP(int, 4, " %8.8x");
291 case 'd': DO_DUMP(unsigned int, 4, " %10d");
292 case 'w': DO_DUMP(unsigned short, 8, " %04x");
293 case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
294 case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);