blob: 597138266764d2a5fe7a83b718d627fc49f29885 [file] [log] [blame]
Alexandre Julliard808cb041995-08-17 17:11:36 +00001/*
2 * Debugger memory handling
3 *
4 * Copyright 1993 Eric Youngdale
5 * Copyright 1995 Alexandre Julliard
Eric Pouechd33bcb62000-03-15 19:57:20 +00006 * Copyright 2000 Eric Pouech
Alexandre Julliard808cb041995-08-17 17:11:36 +00007 */
8
Marcus Meissnerb0d52b01999-02-28 19:59:00 +00009#include "config.h"
Alexandre Julliard808cb041995-08-17 17:11:36 +000010#include <stdlib.h>
Eric Pouech527eea92000-03-08 16:44:54 +000011#include <string.h>
Patrik Stridvall3b233622000-03-24 21:19:02 +000012
Alexandre Julliard808cb041995-08-17 17:11:36 +000013#include "debugger.h"
Eric Pouech527eea92000-03-08 16:44:54 +000014#include "winbase.h"
Alexandre Julliard808cb041995-08-17 17:11:36 +000015
Ulrich Weigandb3ec4b91999-11-13 20:58:45 +000016#ifdef __i386__
Alexandre Julliard954a4132000-09-24 03:15:50 +000017#define IS_VM86_MODE() (DEBUG_context.EFlags & V86_FLAG)
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +000018#endif
Alexandre Julliardaf0bae51995-10-03 17:06:08 +000019
Eric Pouech527eea92000-03-08 16:44:54 +000020static void DEBUG_Die(const char* msg)
Alexandre Julliardaf0bae51995-10-03 17:06:08 +000021{
Eric Poueche5efa0c2000-04-13 19:31:58 +000022 DEBUG_Printf(DBG_CHN_MESG, msg);
Eric Pouech527eea92000-03-08 16:44:54 +000023 exit(1);
Alexandre Julliardaf0bae51995-10-03 17:06:08 +000024}
25
Eric Pouech527eea92000-03-08 16:44:54 +000026void* DEBUG_XMalloc(size_t size)
27{
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;
33}
34
35void* DEBUG_XReAlloc(void *ptr, size_t size)
36{
37 void* res = realloc(ptr, size);
38 if ((res == NULL) && size)
39 DEBUG_Die("Memory exhausted.\n");
40 return res;
41}
42
43char* DEBUG_XStrDup(const char *str)
44{
45 char *res = strdup(str);
46 if (!res)
47 DEBUG_Die("Memory exhausted.\n");
48 return res;
49}
50
Alexandre Julliard954a4132000-09-24 03:15:50 +000051enum dbg_mode DEBUG_GetSelectorType( WORD sel )
52{
53#ifdef __i386__
54 LDT_ENTRY le;
55
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
65}
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +000066#ifdef __i386__
Eric Pouech527eea92000-03-08 16:44:54 +000067void DEBUG_FixAddress( DBG_ADDR *addr, DWORD def)
68{
69 if (addr->seg == 0xffffffff) addr->seg = def;
Alexandre Julliard954a4132000-09-24 03:15:50 +000070 if (DEBUG_IsSelectorSystem(addr->seg)) addr->seg = 0;
Eric Pouech527eea92000-03-08 16:44:54 +000071}
72
73/* Determine if sel is a system selector (i.e. not managed by Wine) */
74BOOL DEBUG_IsSelectorSystem(WORD sel)
75{
Alexandre Julliard954a4132000-09-24 03:15:50 +000076 if (IS_VM86_MODE()) return FALSE; /* no system selectors in vm86 mode */
77 return !(sel & 4) || ((sel >> 3) < 17);
Eric Pouech527eea92000-03-08 16:44:54 +000078}
79#endif /* __i386__ */
80
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +000081DWORD DEBUG_ToLinear( const DBG_ADDR *addr )
82{
83#ifdef __i386__
84 LDT_ENTRY le;
85
Alexandre Julliard954a4132000-09-24 03:15:50 +000086 if (IS_VM86_MODE()) return (DWORD)(LOWORD(addr->seg) << 4) + addr->off;
87
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +000088 if (DEBUG_IsSelectorSystem(addr->seg))
89 return addr->off;
90
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;
93 }
94 return 0;
95#else
96 return addr->off;
97#endif
98}
99
Eric Pouech527eea92000-03-08 16:44:54 +0000100void DEBUG_GetCurrentAddress( DBG_ADDR *addr )
101{
Eric Pouech527eea92000-03-08 16:44:54 +0000102#ifdef __i386__
103 addr->seg = DEBUG_context.SegCs;
104
Alexandre Julliard954a4132000-09-24 03:15:50 +0000105 if (DEBUG_IsSelectorSystem(addr->seg))
Eric Pouech527eea92000-03-08 16:44:54 +0000106 addr->seg = 0;
107 addr->off = DEBUG_context.Eip;
Eric Pouech911436b2000-06-18 19:30:24 +0000108#elif defined(__sparc__)
109 addr->seg = 0;
110 addr->off = DEBUG_context.pc;
Eric Pouech527eea92000-03-08 16:44:54 +0000111#else
Eric Pouech911436b2000-06-18 19:30:24 +0000112# error You must define GET_IP for this CPU
Eric Pouech527eea92000-03-08 16:44:54 +0000113#endif
114}
115
Eric Poueche5efa0c2000-04-13 19:31:58 +0000116void DEBUG_InvalAddr( const DBG_ADDR* addr )
117{
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();
122}
123
Eric Pouech527eea92000-03-08 16:44:54 +0000124void DEBUG_InvalLinAddr( void* addr )
125{
126 DBG_ADDR address;
127
Eric Pouech527eea92000-03-08 16:44:54 +0000128 address.seg = 0;
129 address.off = (unsigned long)addr;
Eric Poueche5efa0c2000-04-13 19:31:58 +0000130 DEBUG_InvalAddr( &address );
Eric Pouech527eea92000-03-08 16:44:54 +0000131}
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000132
133/***********************************************************************
Alexandre Julliard808cb041995-08-17 17:11:36 +0000134 * DEBUG_ReadMemory
135 *
136 * Read a memory value.
137 */
Eric Pouech04c16b82000-04-30 12:21:15 +0000138/* FIXME: this function is now getting closer and closer to
139 * DEBUG_ExprGetValue. They should be merged...
140 */
141int DEBUG_ReadMemory( const DBG_VALUE* val )
Alexandre Julliard808cb041995-08-17 17:11:36 +0000142{
Eric Pouech04c16b82000-04-30 12:21:15 +0000143 int value = 0; /* to clear any unused byte */
144 int os = DEBUG_GetObjectSize(val->type);
145
146 assert(sizeof(value) >= os);
147
148 /* FIXME: only works on little endian systems */
149
150 if (val->cookie == DV_TARGET) {
151 DBG_ADDR addr = val->addr;
152 void* lin;
153
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +0000154#ifdef __i386__
Eric Pouech04c16b82000-04-30 12:21:15 +0000155 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +0000156#endif
Eric Pouech04c16b82000-04-30 12:21:15 +0000157 lin = (void*)DEBUG_ToLinear( &addr );
158
159 DEBUG_READ_MEM_VERBOSE(lin, &value, os);
160 } else {
161 if (val->addr.off)
162 memcpy(&value, (void*)val->addr.off, os);
163 }
Eric Pouech527eea92000-03-08 16:44:54 +0000164 return value;
Alexandre Julliard808cb041995-08-17 17:11:36 +0000165}
166
167
168/***********************************************************************
169 * DEBUG_WriteMemory
170 *
171 * Store a value in memory.
172 */
Eric Pouech04c16b82000-04-30 12:21:15 +0000173void DEBUG_WriteMemory( const DBG_VALUE* val, int value )
Alexandre Julliard808cb041995-08-17 17:11:36 +0000174{
Eric Pouech04c16b82000-04-30 12:21:15 +0000175 int os = DEBUG_GetObjectSize(val->type);
Alexandre Julliard808cb041995-08-17 17:11:36 +0000176
Eric Pouech04c16b82000-04-30 12:21:15 +0000177 assert(sizeof(value) >= os);
178
179 /* FIXME: only works on little endian systems */
180
181 if (val->cookie == DV_TARGET) {
182 DBG_ADDR addr = val->addr;
183 void* lin;
184
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +0000185#ifdef __i386__
Eric Pouech04c16b82000-04-30 12:21:15 +0000186 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +0000187#endif
Eric Pouech04c16b82000-04-30 12:21:15 +0000188 lin = (void*)DEBUG_ToLinear( &addr );
189 DEBUG_WRITE_MEM_VERBOSE(lin, &value, os);
190 } else {
191 memcpy((void*)val->addr.off, &value, os);
192 }
Alexandre Julliard808cb041995-08-17 17:11:36 +0000193}
194
Eric Pouech71189b52000-07-25 12:51:56 +0000195/***********************************************************************
196 * DEBUG_GrabAddress
197 *
198 * Get the address from a value
199 */
200BOOL DEBUG_GrabAddress( DBG_VALUE* value, BOOL fromCode )
201{
202 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
203
204#ifdef __i386__
205 DEBUG_FixAddress( &value->addr,
206 (fromCode) ? DEBUG_context.SegCs : DEBUG_context.SegDs);
207#endif
208
209 /*
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.
213 */
214 if (value->type != NULL) {
215 if (value->type == DEBUG_TypeIntConst) {
216 /*
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.
220 */
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;
227
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);
232 }
233 } else if (!value->addr.seg && !value->addr.off) {
234 DEBUG_Printf(DBG_CHN_MESG,"Invalid expression\n");
235 return FALSE;
236 }
237 return TRUE;
238}
Alexandre Julliard808cb041995-08-17 17:11:36 +0000239
240/***********************************************************************
241 * DEBUG_ExamineMemory
242 *
243 * Implementation of the 'x' command.
244 */
Eric Pouechd33bcb62000-03-15 19:57:20 +0000245void DEBUG_ExamineMemory( const DBG_VALUE *_value, int count, char format )
Alexandre Julliard808cb041995-08-17 17:11:36 +0000246{
Eric Pouechd33bcb62000-03-15 19:57:20 +0000247 DBG_VALUE value = *_value;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000248 int i;
249 unsigned char * pnt;
Alexandre Julliard808cb041995-08-17 17:11:36 +0000250
Eric Pouech71189b52000-07-25 12:51:56 +0000251 if (!DEBUG_GrabAddress(&value, (format == 'i'))) return;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000252
Alexandre Julliard808cb041995-08-17 17:11:36 +0000253 if (format != 'i' && count > 1)
254 {
Eric Pouechd33bcb62000-03-15 19:57:20 +0000255 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
Eric Poueche5efa0c2000-04-13 19:31:58 +0000256 DEBUG_Printf(DBG_CHN_MESG,": ");
Alexandre Julliard808cb041995-08-17 17:11:36 +0000257 }
258
Eric Pouechd33bcb62000-03-15 19:57:20 +0000259 pnt = (void*)DEBUG_ToLinear( &value.addr );
Alexandre Julliard808cb041995-08-17 17:11:36 +0000260
261 switch(format)
262 {
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000263 case 'u': {
Eric Pouech527eea92000-03-08 16:44:54 +0000264 WCHAR wch;
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000265 if (count == 1) count = 256;
266 while (count--)
267 {
Eric Pouech4a07ed82000-05-09 22:32:01 +0000268 if (!DEBUG_READ_MEM_VERBOSE(pnt, &wch, sizeof(wch)) || !wch)
Eric Pouech527eea92000-03-08 16:44:54 +0000269 break;
270 pnt += sizeof(wch);
Eric Pouech04c16b82000-04-30 12:21:15 +0000271 DEBUG_Printf(DBG_CHN_MESG, "%c", (char)wch);
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000272 }
Eric Poueche5efa0c2000-04-13 19:31:58 +0000273 DEBUG_Printf(DBG_CHN_MESG,"\n");
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000274 return;
275 }
Eric Pouech527eea92000-03-08 16:44:54 +0000276 case 's': {
277 char ch;
278
Alexandre Julliard808cb041995-08-17 17:11:36 +0000279 if (count == 1) count = 256;
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000280 while (count--)
281 {
Eric Pouech4a07ed82000-05-09 22:32:01 +0000282 if (!DEBUG_READ_MEM_VERBOSE(pnt, &ch, sizeof(ch)) || !ch)
Eric Pouech527eea92000-03-08 16:44:54 +0000283 break;
284 pnt++;
Eric Pouech04c16b82000-04-30 12:21:15 +0000285 DEBUG_Output(DBG_CHN_MESG, &ch, 1);
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000286 }
Eric Poueche5efa0c2000-04-13 19:31:58 +0000287 DEBUG_Printf(DBG_CHN_MESG,"\n");
Alexandre Julliard808cb041995-08-17 17:11:36 +0000288 return;
Eric Pouech527eea92000-03-08 16:44:54 +0000289 }
Alexandre Julliard808cb041995-08-17 17:11:36 +0000290 case 'i':
Eric Pouech71189b52000-07-25 12:51:56 +0000291 while (count-- && DEBUG_DisassembleInstruction( &value.addr ));
Alexandre Julliard808cb041995-08-17 17:11:36 +0000292 return;
Eric Pouech527eea92000-03-08 16:44:54 +0000293#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; \
Eric Poueche5efa0c2000-04-13 19:31:58 +0000297 DEBUG_Printf(DBG_CHN_MESG,_f,(_vv)); \
Eric Pouechd33bcb62000-03-15 19:57:20 +0000298 pnt += sizeof(_t); value.addr.off += sizeof(_t); \
Eric Pouech527eea92000-03-08 16:44:54 +0000299 if ((i % (_l)) == (_l)-1) { \
Eric Poueche5efa0c2000-04-13 19:31:58 +0000300 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
Eric Pouechd33bcb62000-03-15 19:57:20 +0000301 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
Eric Poueche5efa0c2000-04-13 19:31:58 +0000302 DEBUG_Printf(DBG_CHN_MESG,": ");\
Eric Pouech527eea92000-03-08 16:44:54 +0000303 } \
304 } \
Eric Poueche5efa0c2000-04-13 19:31:58 +0000305 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
Eric Pouech527eea92000-03-08 16:44:54 +0000306 } \
307 return
308#define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
309
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);
Alexandre Julliard808cb041995-08-17 17:11:36 +0000315 }
316}