Removed obsolete SHMDATA parameter in GLOBAL_CreateBlock.
[wine/hacks.git] / debugger / memory.c
blob6f7083ca2884cadf6c8b80564dcb20cfc9aa6726
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 <stdlib.h>
11 #include <string.h>
13 #include "debugger.h"
14 #include "miscemu.h"
15 #include "winbase.h"
17 #ifdef __i386__
18 #include "wine/winbase16.h"
20 #define DBG_V86_MODULE(seg) ((seg)>>16)
21 #define IS_SELECTOR_V86(seg) DBG_V86_MODULE(seg)
22 #endif
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 #ifdef __i386__
56 void DEBUG_FixAddress( DBG_ADDR *addr, DWORD def)
58 if (addr->seg == 0xffffffff) addr->seg = def;
59 if (!IS_SELECTOR_V86(addr->seg) && DEBUG_IsSelectorSystem(addr->seg)) addr->seg = 0;
62 BOOL DEBUG_FixSegment( DBG_ADDR* addr )
64 /* V86 mode ? */
65 if (DEBUG_context.EFlags & V86_FLAG) {
66 addr->seg |= (DWORD)(GetExePtr(GetCurrentTask())) << 16;
67 return TRUE;
69 return FALSE;
72 int DEBUG_GetSelectorType( WORD sel )
74 LDT_ENTRY le;
76 if (sel == 0)
77 return 32;
78 if (IS_SELECTOR_V86(sel))
79 return 16;
80 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, sel, &le))
81 return le.HighWord.Bits.Default_Big ? 32 : 16;
82 /* selector doesn't exist */
83 return 0;
86 /* Determine if sel is a system selector (i.e. not managed by Wine) */
87 BOOL DEBUG_IsSelectorSystem(WORD sel)
89 return !(sel & 4) || (((sel & 0xFFFF) >> 3) < 17);
91 #endif /* __i386__ */
93 DWORD DEBUG_ToLinear( const DBG_ADDR *addr )
95 #ifdef __i386__
96 LDT_ENTRY le;
98 #if 0
99 if (IS_SELECTOR_V86(addr->seg))
100 return (DWORD) DOSMEM_MemoryBase(DBG_V86_MODULE(addr->seg)) + (((addr->seg)&0xFFFF)<<4) + addr->off;
101 #endif
102 if (DEBUG_IsSelectorSystem(addr->seg))
103 return addr->off;
105 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, addr->seg, &le)) {
106 return (le.HighWord.Bits.BaseHi << 24) + (le.HighWord.Bits.BaseMid << 16) + le.BaseLow + addr->off;
108 return 0;
109 #else
110 return addr->off;
111 #endif
114 void DEBUG_GetCurrentAddress( DBG_ADDR *addr )
116 #ifdef __i386__
117 addr->seg = DEBUG_context.SegCs;
119 if (!DEBUG_FixSegment( addr ) && DEBUG_IsSelectorSystem(addr->seg))
120 addr->seg = 0;
121 addr->off = DEBUG_context.Eip;
122 #elif defined(__sparc__)
123 addr->seg = 0;
124 addr->off = DEBUG_context.pc;
125 #else
126 # error You must define GET_IP for this CPU
127 #endif
130 void DEBUG_InvalAddr( const DBG_ADDR* addr )
132 DEBUG_Printf(DBG_CHN_MESG,"*** Invalid address ");
133 DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
134 DEBUG_Printf(DBG_CHN_MESG,"\n");
135 if (DBG_IVAR(ExtDbgOnInvalidAddress)) DEBUG_ExternalDebugger();
138 void DEBUG_InvalLinAddr( void* addr )
140 DBG_ADDR address;
142 address.seg = 0;
143 address.off = (unsigned long)addr;
144 DEBUG_InvalAddr( &address );
147 /***********************************************************************
148 * DEBUG_ReadMemory
150 * Read a memory value.
152 /* FIXME: this function is now getting closer and closer to
153 * DEBUG_ExprGetValue. They should be merged...
155 int DEBUG_ReadMemory( const DBG_VALUE* val )
157 int value = 0; /* to clear any unused byte */
158 int os = DEBUG_GetObjectSize(val->type);
160 assert(sizeof(value) >= os);
162 /* FIXME: only works on little endian systems */
164 if (val->cookie == DV_TARGET) {
165 DBG_ADDR addr = val->addr;
166 void* lin;
168 #ifdef __i386__
169 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
170 #endif
171 lin = (void*)DEBUG_ToLinear( &addr );
173 DEBUG_READ_MEM_VERBOSE(lin, &value, os);
174 } else {
175 if (val->addr.off)
176 memcpy(&value, (void*)val->addr.off, os);
178 return value;
182 /***********************************************************************
183 * DEBUG_WriteMemory
185 * Store a value in memory.
187 void DEBUG_WriteMemory( const DBG_VALUE* val, int value )
189 int os = DEBUG_GetObjectSize(val->type);
191 assert(sizeof(value) >= os);
193 /* FIXME: only works on little endian systems */
195 if (val->cookie == DV_TARGET) {
196 DBG_ADDR addr = val->addr;
197 void* lin;
199 #ifdef __i386__
200 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
201 #endif
202 lin = (void*)DEBUG_ToLinear( &addr );
203 DEBUG_WRITE_MEM_VERBOSE(lin, &value, os);
204 } else {
205 memcpy((void*)val->addr.off, &value, os);
209 /***********************************************************************
210 * DEBUG_GrabAddress
212 * Get the address from a value
214 BOOL DEBUG_GrabAddress( DBG_VALUE* value, BOOL fromCode )
216 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
218 #ifdef __i386__
219 DEBUG_FixAddress( &value->addr,
220 (fromCode) ? DEBUG_context.SegCs : DEBUG_context.SegDs);
221 #endif
224 * Dereference pointer to get actual memory address we need to be
225 * reading. We will use the same segment as what we have already,
226 * and hope that this is a sensible thing to do.
228 if (value->type != NULL) {
229 if (value->type == DEBUG_TypeIntConst) {
231 * We know that we have the actual offset stored somewhere
232 * else in 32-bit space. Grab it, and we
233 * should be all set.
235 unsigned int seg2 = value->addr.seg;
236 value->addr.seg = 0;
237 value->addr.off = DEBUG_GetExprValue(value, NULL);
238 value->addr.seg = seg2;
239 } else {
240 struct datatype * testtype;
242 if (DEBUG_TypeDerefPointer(value, &testtype) == 0)
243 return FALSE;
244 if (testtype != NULL || value->type == DEBUG_TypeIntConst)
245 value->addr.off = DEBUG_GetExprValue(value, NULL);
247 } else if (!value->addr.seg && !value->addr.off) {
248 DEBUG_Printf(DBG_CHN_MESG,"Invalid expression\n");
249 return FALSE;
251 return TRUE;
254 /***********************************************************************
255 * DEBUG_ExamineMemory
257 * Implementation of the 'x' command.
259 void DEBUG_ExamineMemory( const DBG_VALUE *_value, int count, char format )
261 DBG_VALUE value = *_value;
262 int i;
263 unsigned char * pnt;
265 if (!DEBUG_GrabAddress(&value, (format == 'i'))) return;
267 if (format != 'i' && count > 1)
269 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
270 DEBUG_Printf(DBG_CHN_MESG,": ");
273 pnt = (void*)DEBUG_ToLinear( &value.addr );
275 switch(format)
277 case 'u': {
278 WCHAR wch;
279 if (count == 1) count = 256;
280 while (count--)
282 if (!DEBUG_READ_MEM_VERBOSE(pnt, &wch, sizeof(wch)) || !wch)
283 break;
284 pnt += sizeof(wch);
285 DEBUG_Printf(DBG_CHN_MESG, "%c", (char)wch);
287 DEBUG_Printf(DBG_CHN_MESG,"\n");
288 return;
290 case 's': {
291 char ch;
293 if (count == 1) count = 256;
294 while (count--)
296 if (!DEBUG_READ_MEM_VERBOSE(pnt, &ch, sizeof(ch)) || !ch)
297 break;
298 pnt++;
299 DEBUG_Output(DBG_CHN_MESG, &ch, 1);
301 DEBUG_Printf(DBG_CHN_MESG,"\n");
302 return;
304 case 'i':
305 while (count-- && DEBUG_DisassembleInstruction( &value.addr ));
306 return;
307 #define DO_DUMP2(_t,_l,_f,_vv) { \
308 _t _v; \
309 for(i=0; i<count; i++) { \
310 if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
311 DEBUG_Printf(DBG_CHN_MESG,_f,(_vv)); \
312 pnt += sizeof(_t); value.addr.off += sizeof(_t); \
313 if ((i % (_l)) == (_l)-1) { \
314 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
315 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
316 DEBUG_Printf(DBG_CHN_MESG,": ");\
319 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
321 return
322 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
324 case 'x': DO_DUMP(int, 4, " %8.8x");
325 case 'd': DO_DUMP(unsigned int, 4, " %10d");
326 case 'w': DO_DUMP(unsigned short, 8, " %04x");
327 case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
328 case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);