Fixed ttydrv compile when using curses. Cleaned up a few #ifdefs.
[wine/multimedia.git] / debugger / memory.c
blob19466e5df7ada14e202d309c0072bc504a02510a
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 fprintf(stderr, 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_InvalLinAddr( void* addr )
123 DBG_ADDR address;
125 address.seg = 0;
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 /***********************************************************************
134 * DEBUG_ReadMemory
136 * Read a memory value.
138 int DEBUG_ReadMemory( const DBG_ADDR *address )
140 DBG_ADDR addr = *address;
141 void* lin;
142 int value;
144 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
145 lin = (void*)DEBUG_ToLinear( &addr );
146 if (!DEBUG_READ_MEM_VERBOSE(lin, &value, sizeof(value)))
147 value = 0;
148 return value;
152 /***********************************************************************
153 * DEBUG_WriteMemory
155 * Store a value in memory.
157 void DEBUG_WriteMemory( const DBG_ADDR *address, int value )
159 DBG_ADDR addr = *address;
160 void* lin;
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;
176 int i;
177 unsigned char * pnt;
178 struct datatype * testtype;
180 assert(_value->cookie == DV_TARGET || _value->cookie == DV_HOST);
182 DEBUG_FixAddress( &value.addr,
183 (format == 'i') ?
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
199 * should be all set.
201 unsigned int seg2 = value.addr.seg;
202 value.addr.seg = 0;
203 value.addr.off = DEBUG_GetExprValue(&value, NULL);
204 value.addr.seg = seg2;
206 else
208 if (DEBUG_TypeDerefPointer(&value, &testtype) == 0)
209 return;
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");
219 return;
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 );
230 switch(format)
232 case 'u': {
233 WCHAR wch;
234 if (count == 1) count = 256;
235 while (count--)
237 if (!DEBUG_READ_MEM_VERBOSE(pnt, &wch, sizeof(wch)))
238 break;
239 pnt += sizeof(wch);
240 fputc( (char)wch, stderr );
242 fprintf(stderr,"\n");
243 return;
245 case 's': {
246 char ch;
248 if (count == 1) count = 256;
249 while (count--)
251 if (!DEBUG_READ_MEM_VERBOSE(pnt, &ch, sizeof(ch)))
252 break;
253 pnt++;
254 fputc( ch, stderr );
256 fprintf(stderr,"\n");
257 return;
259 case 'i':
260 while (count--)
262 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, TRUE );
263 fprintf(stderr,": ");
264 DEBUG_Disasm( &value.addr, TRUE );
265 fprintf(stderr,"\n");
267 return;
268 #define DO_DUMP2(_t,_l,_f,_vv) { \
269 _t _v; \
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"); \
282 return
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);