Added WSC*InstallProvider stubs.
[wine/wine-kai.git] / debugger / memory.c
blob597138266764d2a5fe7a83b718d627fc49f29885
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 "winbase.h"
16 #ifdef __i386__
17 #define IS_VM86_MODE() (DEBUG_context.EFlags & V86_FLAG)
18 #endif
20 static void DEBUG_Die(const char* msg)
22 DEBUG_Printf(DBG_CHN_MESG, msg);
23 exit(1);
26 void* DEBUG_XMalloc(size_t size)
28 void *res = malloc(size ? size : 1);
29 if (res == NULL)
30 DEBUG_Die("Memory exhausted.\n");
31 memset(res, 0, size);
32 return res;
35 void* DEBUG_XReAlloc(void *ptr, size_t size)
37 void* res = realloc(ptr, size);
38 if ((res == NULL) && size)
39 DEBUG_Die("Memory exhausted.\n");
40 return res;
43 char* DEBUG_XStrDup(const char *str)
45 char *res = strdup(str);
46 if (!res)
47 DEBUG_Die("Memory exhausted.\n");
48 return res;
51 enum dbg_mode DEBUG_GetSelectorType( WORD sel )
53 #ifdef __i386__
54 LDT_ENTRY le;
56 if (IS_VM86_MODE()) return MODE_VM86;
57 if (sel == 0) return MODE_32;
58 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, sel, &le))
59 return le.HighWord.Bits.Default_Big ? MODE_32 : MODE_16;
60 /* selector doesn't exist */
61 return MODE_INVALID;
62 #else
63 return MODE_32;
64 #endif
66 #ifdef __i386__
67 void DEBUG_FixAddress( DBG_ADDR *addr, DWORD def)
69 if (addr->seg == 0xffffffff) addr->seg = def;
70 if (DEBUG_IsSelectorSystem(addr->seg)) addr->seg = 0;
73 /* Determine if sel is a system selector (i.e. not managed by Wine) */
74 BOOL DEBUG_IsSelectorSystem(WORD sel)
76 if (IS_VM86_MODE()) return FALSE; /* no system selectors in vm86 mode */
77 return !(sel & 4) || ((sel >> 3) < 17);
79 #endif /* __i386__ */
81 DWORD DEBUG_ToLinear( const DBG_ADDR *addr )
83 #ifdef __i386__
84 LDT_ENTRY le;
86 if (IS_VM86_MODE()) return (DWORD)(LOWORD(addr->seg) << 4) + addr->off;
88 if (DEBUG_IsSelectorSystem(addr->seg))
89 return addr->off;
91 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, addr->seg, &le)) {
92 return (le.HighWord.Bits.BaseHi << 24) + (le.HighWord.Bits.BaseMid << 16) + le.BaseLow + addr->off;
94 return 0;
95 #else
96 return addr->off;
97 #endif
100 void DEBUG_GetCurrentAddress( DBG_ADDR *addr )
102 #ifdef __i386__
103 addr->seg = DEBUG_context.SegCs;
105 if (DEBUG_IsSelectorSystem(addr->seg))
106 addr->seg = 0;
107 addr->off = DEBUG_context.Eip;
108 #elif defined(__sparc__)
109 addr->seg = 0;
110 addr->off = DEBUG_context.pc;
111 #else
112 # error You must define GET_IP for this CPU
113 #endif
116 void DEBUG_InvalAddr( const DBG_ADDR* addr )
118 DEBUG_Printf(DBG_CHN_MESG,"*** Invalid address ");
119 DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
120 DEBUG_Printf(DBG_CHN_MESG,"\n");
121 if (DBG_IVAR(ExtDbgOnInvalidAddress)) DEBUG_ExternalDebugger();
124 void DEBUG_InvalLinAddr( void* addr )
126 DBG_ADDR address;
128 address.seg = 0;
129 address.off = (unsigned long)addr;
130 DEBUG_InvalAddr( &address );
133 /***********************************************************************
134 * DEBUG_ReadMemory
136 * Read a memory value.
138 /* FIXME: this function is now getting closer and closer to
139 * DEBUG_ExprGetValue. They should be merged...
141 int DEBUG_ReadMemory( const DBG_VALUE* val )
143 int value = 0; /* to clear any unused byte */
144 int os = DEBUG_GetObjectSize(val->type);
146 assert(sizeof(value) >= os);
148 /* FIXME: only works on little endian systems */
150 if (val->cookie == DV_TARGET) {
151 DBG_ADDR addr = val->addr;
152 void* lin;
154 #ifdef __i386__
155 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
156 #endif
157 lin = (void*)DEBUG_ToLinear( &addr );
159 DEBUG_READ_MEM_VERBOSE(lin, &value, os);
160 } else {
161 if (val->addr.off)
162 memcpy(&value, (void*)val->addr.off, os);
164 return value;
168 /***********************************************************************
169 * DEBUG_WriteMemory
171 * Store a value in memory.
173 void DEBUG_WriteMemory( const DBG_VALUE* val, int value )
175 int os = DEBUG_GetObjectSize(val->type);
177 assert(sizeof(value) >= os);
179 /* FIXME: only works on little endian systems */
181 if (val->cookie == DV_TARGET) {
182 DBG_ADDR addr = val->addr;
183 void* lin;
185 #ifdef __i386__
186 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
187 #endif
188 lin = (void*)DEBUG_ToLinear( &addr );
189 DEBUG_WRITE_MEM_VERBOSE(lin, &value, os);
190 } else {
191 memcpy((void*)val->addr.off, &value, os);
195 /***********************************************************************
196 * DEBUG_GrabAddress
198 * Get the address from a value
200 BOOL DEBUG_GrabAddress( DBG_VALUE* value, BOOL fromCode )
202 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
204 #ifdef __i386__
205 DEBUG_FixAddress( &value->addr,
206 (fromCode) ? DEBUG_context.SegCs : DEBUG_context.SegDs);
207 #endif
210 * Dereference pointer to get actual memory address we need to be
211 * reading. We will use the same segment as what we have already,
212 * and hope that this is a sensible thing to do.
214 if (value->type != NULL) {
215 if (value->type == DEBUG_TypeIntConst) {
217 * We know that we have the actual offset stored somewhere
218 * else in 32-bit space. Grab it, and we
219 * should be all set.
221 unsigned int seg2 = value->addr.seg;
222 value->addr.seg = 0;
223 value->addr.off = DEBUG_GetExprValue(value, NULL);
224 value->addr.seg = seg2;
225 } else {
226 struct datatype * testtype;
228 if (DEBUG_TypeDerefPointer(value, &testtype) == 0)
229 return FALSE;
230 if (testtype != NULL || value->type == DEBUG_TypeIntConst)
231 value->addr.off = DEBUG_GetExprValue(value, NULL);
233 } else if (!value->addr.seg && !value->addr.off) {
234 DEBUG_Printf(DBG_CHN_MESG,"Invalid expression\n");
235 return FALSE;
237 return TRUE;
240 /***********************************************************************
241 * DEBUG_ExamineMemory
243 * Implementation of the 'x' command.
245 void DEBUG_ExamineMemory( const DBG_VALUE *_value, int count, char format )
247 DBG_VALUE value = *_value;
248 int i;
249 unsigned char * pnt;
251 if (!DEBUG_GrabAddress(&value, (format == 'i'))) return;
253 if (format != 'i' && count > 1)
255 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
256 DEBUG_Printf(DBG_CHN_MESG,": ");
259 pnt = (void*)DEBUG_ToLinear( &value.addr );
261 switch(format)
263 case 'u': {
264 WCHAR wch;
265 if (count == 1) count = 256;
266 while (count--)
268 if (!DEBUG_READ_MEM_VERBOSE(pnt, &wch, sizeof(wch)) || !wch)
269 break;
270 pnt += sizeof(wch);
271 DEBUG_Printf(DBG_CHN_MESG, "%c", (char)wch);
273 DEBUG_Printf(DBG_CHN_MESG,"\n");
274 return;
276 case 's': {
277 char ch;
279 if (count == 1) count = 256;
280 while (count--)
282 if (!DEBUG_READ_MEM_VERBOSE(pnt, &ch, sizeof(ch)) || !ch)
283 break;
284 pnt++;
285 DEBUG_Output(DBG_CHN_MESG, &ch, 1);
287 DEBUG_Printf(DBG_CHN_MESG,"\n");
288 return;
290 case 'i':
291 while (count-- && DEBUG_DisassembleInstruction( &value.addr ));
292 return;
293 #define DO_DUMP2(_t,_l,_f,_vv) { \
294 _t _v; \
295 for(i=0; i<count; i++) { \
296 if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
297 DEBUG_Printf(DBG_CHN_MESG,_f,(_vv)); \
298 pnt += sizeof(_t); value.addr.off += sizeof(_t); \
299 if ((i % (_l)) == (_l)-1) { \
300 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
301 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
302 DEBUG_Printf(DBG_CHN_MESG,": ");\
305 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
307 return
308 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
310 case 'x': DO_DUMP(int, 4, " %8.8x");
311 case 'd': DO_DUMP(unsigned int, 4, " %10d");
312 case 'w': DO_DUMP(unsigned short, 8, " %04x");
313 case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
314 case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);