blob: fc2f9901ab7305095c238294b117dc239d52708c [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 Julliard0799c1a2002-03-09 23:29:33 +00007 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Alexandre Julliard808cb041995-08-17 17:11:36 +000021 */
22
Marcus Meissnerb0d52b01999-02-28 19:59:00 +000023#include "config.h"
Alexandre Julliard5769d1d2002-04-26 19:05:15 +000024#include "wine/port.h"
25
Alexandre Julliard808cb041995-08-17 17:11:36 +000026#include <stdlib.h>
Eric Pouech527eea92000-03-08 16:44:54 +000027#include <string.h>
Patrik Stridvall3b233622000-03-24 21:19:02 +000028
Alexandre Julliard808cb041995-08-17 17:11:36 +000029#include "debugger.h"
Eric Pouech527eea92000-03-08 16:44:54 +000030#include "winbase.h"
Alexandre Julliard808cb041995-08-17 17:11:36 +000031
Ulrich Weigandb3ec4b91999-11-13 20:58:45 +000032#ifdef __i386__
Alexandre Julliard954a4132000-09-24 03:15:50 +000033#define IS_VM86_MODE() (DEBUG_context.EFlags & V86_FLAG)
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +000034#endif
Alexandre Julliardaf0bae51995-10-03 17:06:08 +000035
Eric Pouech527eea92000-03-08 16:44:54 +000036static void DEBUG_Die(const char* msg)
Alexandre Julliardaf0bae51995-10-03 17:06:08 +000037{
Eric Poueche5efa0c2000-04-13 19:31:58 +000038 DEBUG_Printf(DBG_CHN_MESG, msg);
Eric Pouech527eea92000-03-08 16:44:54 +000039 exit(1);
Alexandre Julliardaf0bae51995-10-03 17:06:08 +000040}
41
Eric Pouech527eea92000-03-08 16:44:54 +000042void* DEBUG_XMalloc(size_t size)
43{
44 void *res = malloc(size ? size : 1);
45 if (res == NULL)
46 DEBUG_Die("Memory exhausted.\n");
47 memset(res, 0, size);
48 return res;
49}
50
51void* DEBUG_XReAlloc(void *ptr, size_t size)
52{
53 void* res = realloc(ptr, size);
54 if ((res == NULL) && size)
55 DEBUG_Die("Memory exhausted.\n");
56 return res;
57}
58
59char* DEBUG_XStrDup(const char *str)
60{
61 char *res = strdup(str);
62 if (!res)
63 DEBUG_Die("Memory exhausted.\n");
64 return res;
65}
66
Alexandre Julliard954a4132000-09-24 03:15:50 +000067enum dbg_mode DEBUG_GetSelectorType( WORD sel )
68{
69#ifdef __i386__
70 LDT_ENTRY le;
71
72 if (IS_VM86_MODE()) return MODE_VM86;
73 if (sel == 0) return MODE_32;
74 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, sel, &le))
75 return le.HighWord.Bits.Default_Big ? MODE_32 : MODE_16;
76 /* selector doesn't exist */
77 return MODE_INVALID;
78#else
79 return MODE_32;
80#endif
81}
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +000082#ifdef __i386__
Eric Pouech527eea92000-03-08 16:44:54 +000083void DEBUG_FixAddress( DBG_ADDR *addr, DWORD def)
84{
85 if (addr->seg == 0xffffffff) addr->seg = def;
Alexandre Julliard954a4132000-09-24 03:15:50 +000086 if (DEBUG_IsSelectorSystem(addr->seg)) addr->seg = 0;
Eric Pouech527eea92000-03-08 16:44:54 +000087}
88
89/* Determine if sel is a system selector (i.e. not managed by Wine) */
90BOOL DEBUG_IsSelectorSystem(WORD sel)
91{
Alexandre Julliard954a4132000-09-24 03:15:50 +000092 if (IS_VM86_MODE()) return FALSE; /* no system selectors in vm86 mode */
93 return !(sel & 4) || ((sel >> 3) < 17);
Eric Pouech527eea92000-03-08 16:44:54 +000094}
95#endif /* __i386__ */
96
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +000097DWORD DEBUG_ToLinear( const DBG_ADDR *addr )
98{
99#ifdef __i386__
100 LDT_ENTRY le;
101
Alexandre Julliard954a4132000-09-24 03:15:50 +0000102 if (IS_VM86_MODE()) return (DWORD)(LOWORD(addr->seg) << 4) + addr->off;
103
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +0000104 if (DEBUG_IsSelectorSystem(addr->seg))
105 return addr->off;
106
107 if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, addr->seg, &le)) {
108 return (le.HighWord.Bits.BaseHi << 24) + (le.HighWord.Bits.BaseMid << 16) + le.BaseLow + addr->off;
109 }
110 return 0;
111#else
112 return addr->off;
113#endif
114}
115
Eric Pouech527eea92000-03-08 16:44:54 +0000116void DEBUG_GetCurrentAddress( DBG_ADDR *addr )
117{
Eric Pouech527eea92000-03-08 16:44:54 +0000118#ifdef __i386__
119 addr->seg = DEBUG_context.SegCs;
120
Alexandre Julliard954a4132000-09-24 03:15:50 +0000121 if (DEBUG_IsSelectorSystem(addr->seg))
Eric Pouech527eea92000-03-08 16:44:54 +0000122 addr->seg = 0;
123 addr->off = DEBUG_context.Eip;
Eric Pouech911436b2000-06-18 19:30:24 +0000124#elif defined(__sparc__)
125 addr->seg = 0;
126 addr->off = DEBUG_context.pc;
Eric Pouech527eea92000-03-08 16:44:54 +0000127#else
Eric Pouech911436b2000-06-18 19:30:24 +0000128# error You must define GET_IP for this CPU
Eric Pouech527eea92000-03-08 16:44:54 +0000129#endif
130}
131
Eric Poueche5efa0c2000-04-13 19:31:58 +0000132void DEBUG_InvalAddr( const DBG_ADDR* addr )
133{
134 DEBUG_Printf(DBG_CHN_MESG,"*** Invalid address ");
135 DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
136 DEBUG_Printf(DBG_CHN_MESG,"\n");
137 if (DBG_IVAR(ExtDbgOnInvalidAddress)) DEBUG_ExternalDebugger();
138}
139
Eric Pouech527eea92000-03-08 16:44:54 +0000140void DEBUG_InvalLinAddr( void* addr )
141{
142 DBG_ADDR address;
143
Eric Pouech527eea92000-03-08 16:44:54 +0000144 address.seg = 0;
145 address.off = (unsigned long)addr;
Eric Poueche5efa0c2000-04-13 19:31:58 +0000146 DEBUG_InvalAddr( &address );
Eric Pouech527eea92000-03-08 16:44:54 +0000147}
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000148
149/***********************************************************************
Alexandre Julliard808cb041995-08-17 17:11:36 +0000150 * DEBUG_ReadMemory
151 *
152 * Read a memory value.
153 */
Eric Pouech04c16b82000-04-30 12:21:15 +0000154/* FIXME: this function is now getting closer and closer to
155 * DEBUG_ExprGetValue. They should be merged...
156 */
157int DEBUG_ReadMemory( const DBG_VALUE* val )
Alexandre Julliard808cb041995-08-17 17:11:36 +0000158{
Eric Pouech04c16b82000-04-30 12:21:15 +0000159 int value = 0; /* to clear any unused byte */
160 int os = DEBUG_GetObjectSize(val->type);
161
162 assert(sizeof(value) >= os);
163
164 /* FIXME: only works on little endian systems */
165
166 if (val->cookie == DV_TARGET) {
167 DBG_ADDR addr = val->addr;
168 void* lin;
169
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +0000170#ifdef __i386__
Eric Pouech04c16b82000-04-30 12:21:15 +0000171 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +0000172#endif
Eric Pouech04c16b82000-04-30 12:21:15 +0000173 lin = (void*)DEBUG_ToLinear( &addr );
174
175 DEBUG_READ_MEM_VERBOSE(lin, &value, os);
176 } else {
177 if (val->addr.off)
178 memcpy(&value, (void*)val->addr.off, os);
179 }
Eric Pouech527eea92000-03-08 16:44:54 +0000180 return value;
Alexandre Julliard808cb041995-08-17 17:11:36 +0000181}
182
183
184/***********************************************************************
185 * DEBUG_WriteMemory
186 *
187 * Store a value in memory.
188 */
Eric Pouech04c16b82000-04-30 12:21:15 +0000189void DEBUG_WriteMemory( const DBG_VALUE* val, int value )
Alexandre Julliard808cb041995-08-17 17:11:36 +0000190{
Eric Pouech04c16b82000-04-30 12:21:15 +0000191 int os = DEBUG_GetObjectSize(val->type);
Alexandre Julliard808cb041995-08-17 17:11:36 +0000192
Eric Pouech04c16b82000-04-30 12:21:15 +0000193 assert(sizeof(value) >= os);
194
195 /* FIXME: only works on little endian systems */
196
197 if (val->cookie == DV_TARGET) {
198 DBG_ADDR addr = val->addr;
199 void* lin;
200
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +0000201#ifdef __i386__
Eric Pouech04c16b82000-04-30 12:21:15 +0000202 DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
Ulrich Weigand1cbf27a2000-06-04 01:33:21 +0000203#endif
Eric Pouech04c16b82000-04-30 12:21:15 +0000204 lin = (void*)DEBUG_ToLinear( &addr );
205 DEBUG_WRITE_MEM_VERBOSE(lin, &value, os);
206 } else {
207 memcpy((void*)val->addr.off, &value, os);
208 }
Alexandre Julliard808cb041995-08-17 17:11:36 +0000209}
210
Eric Pouech71189b52000-07-25 12:51:56 +0000211/***********************************************************************
212 * DEBUG_GrabAddress
213 *
214 * Get the address from a value
215 */
216BOOL DEBUG_GrabAddress( DBG_VALUE* value, BOOL fromCode )
217{
218 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
219
220#ifdef __i386__
221 DEBUG_FixAddress( &value->addr,
222 (fromCode) ? DEBUG_context.SegCs : DEBUG_context.SegDs);
223#endif
224
225 /*
226 * Dereference pointer to get actual memory address we need to be
227 * reading. We will use the same segment as what we have already,
228 * and hope that this is a sensible thing to do.
229 */
230 if (value->type != NULL) {
Eric Pouech02ecb682001-12-21 20:29:58 +0000231 if (value->type == DEBUG_GetBasicType(DT_BASIC_CONST_INT)) {
Eric Pouech71189b52000-07-25 12:51:56 +0000232 /*
233 * We know that we have the actual offset stored somewhere
234 * else in 32-bit space. Grab it, and we
235 * should be all set.
236 */
237 unsigned int seg2 = value->addr.seg;
238 value->addr.seg = 0;
239 value->addr.off = DEBUG_GetExprValue(value, NULL);
240 value->addr.seg = seg2;
241 } else {
242 struct datatype * testtype;
243
244 if (DEBUG_TypeDerefPointer(value, &testtype) == 0)
245 return FALSE;
Eric Pouech02ecb682001-12-21 20:29:58 +0000246 if (testtype != NULL || value->type == DEBUG_GetBasicType(DT_BASIC_CONST_INT))
Eric Pouech71189b52000-07-25 12:51:56 +0000247 value->addr.off = DEBUG_GetExprValue(value, NULL);
248 }
249 } else if (!value->addr.seg && !value->addr.off) {
250 DEBUG_Printf(DBG_CHN_MESG,"Invalid expression\n");
251 return FALSE;
252 }
253 return TRUE;
254}
Alexandre Julliard808cb041995-08-17 17:11:36 +0000255
256/***********************************************************************
257 * DEBUG_ExamineMemory
258 *
259 * Implementation of the 'x' command.
260 */
Eric Pouechd33bcb62000-03-15 19:57:20 +0000261void DEBUG_ExamineMemory( const DBG_VALUE *_value, int count, char format )
Alexandre Julliard808cb041995-08-17 17:11:36 +0000262{
Eric Pouechd33bcb62000-03-15 19:57:20 +0000263 DBG_VALUE value = *_value;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000264 int i;
265 unsigned char * pnt;
Alexandre Julliard808cb041995-08-17 17:11:36 +0000266
Eric Pouech71189b52000-07-25 12:51:56 +0000267 if (!DEBUG_GrabAddress(&value, (format == 'i'))) return;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000268
Alexandre Julliard808cb041995-08-17 17:11:36 +0000269 if (format != 'i' && count > 1)
270 {
Eric Pouechd33bcb62000-03-15 19:57:20 +0000271 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
Eric Poueche5efa0c2000-04-13 19:31:58 +0000272 DEBUG_Printf(DBG_CHN_MESG,": ");
Alexandre Julliard808cb041995-08-17 17:11:36 +0000273 }
274
Eric Pouechd33bcb62000-03-15 19:57:20 +0000275 pnt = (void*)DEBUG_ToLinear( &value.addr );
Alexandre Julliard808cb041995-08-17 17:11:36 +0000276
277 switch(format)
278 {
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000279 case 'u': {
Eric Pouech527eea92000-03-08 16:44:54 +0000280 WCHAR wch;
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000281 if (count == 1) count = 256;
282 while (count--)
283 {
Eric Pouech4a07ed82000-05-09 22:32:01 +0000284 if (!DEBUG_READ_MEM_VERBOSE(pnt, &wch, sizeof(wch)) || !wch)
Eric Pouech527eea92000-03-08 16:44:54 +0000285 break;
286 pnt += sizeof(wch);
Eric Pouech04c16b82000-04-30 12:21:15 +0000287 DEBUG_Printf(DBG_CHN_MESG, "%c", (char)wch);
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000288 }
Eric Poueche5efa0c2000-04-13 19:31:58 +0000289 DEBUG_Printf(DBG_CHN_MESG,"\n");
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000290 return;
291 }
Eric Pouech527eea92000-03-08 16:44:54 +0000292 case 's': {
293 char ch;
294
Alexandre Julliard808cb041995-08-17 17:11:36 +0000295 if (count == 1) count = 256;
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000296 while (count--)
297 {
Eric Pouech4a07ed82000-05-09 22:32:01 +0000298 if (!DEBUG_READ_MEM_VERBOSE(pnt, &ch, sizeof(ch)) || !ch)
Eric Pouech527eea92000-03-08 16:44:54 +0000299 break;
300 pnt++;
Eric Pouech04c16b82000-04-30 12:21:15 +0000301 DEBUG_Output(DBG_CHN_MESG, &ch, 1);
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000302 }
Eric Poueche5efa0c2000-04-13 19:31:58 +0000303 DEBUG_Printf(DBG_CHN_MESG,"\n");
Alexandre Julliard808cb041995-08-17 17:11:36 +0000304 return;
Eric Pouech527eea92000-03-08 16:44:54 +0000305 }
Alexandre Julliard808cb041995-08-17 17:11:36 +0000306 case 'i':
Eric Pouech71189b52000-07-25 12:51:56 +0000307 while (count-- && DEBUG_DisassembleInstruction( &value.addr ));
Alexandre Julliard808cb041995-08-17 17:11:36 +0000308 return;
Eric Pouech527eea92000-03-08 16:44:54 +0000309#define DO_DUMP2(_t,_l,_f,_vv) { \
310 _t _v; \
311 for(i=0; i<count; i++) { \
312 if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
Eric Poueche5efa0c2000-04-13 19:31:58 +0000313 DEBUG_Printf(DBG_CHN_MESG,_f,(_vv)); \
Eric Pouechd33bcb62000-03-15 19:57:20 +0000314 pnt += sizeof(_t); value.addr.off += sizeof(_t); \
Eric Pouech527eea92000-03-08 16:44:54 +0000315 if ((i % (_l)) == (_l)-1) { \
Eric Poueche5efa0c2000-04-13 19:31:58 +0000316 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
Eric Pouechd33bcb62000-03-15 19:57:20 +0000317 DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
Eric Poueche5efa0c2000-04-13 19:31:58 +0000318 DEBUG_Printf(DBG_CHN_MESG,": ");\
Eric Pouech527eea92000-03-08 16:44:54 +0000319 } \
320 } \
Eric Poueche5efa0c2000-04-13 19:31:58 +0000321 DEBUG_Printf(DBG_CHN_MESG,"\n"); \
Eric Pouech527eea92000-03-08 16:44:54 +0000322 } \
323 return
324#define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
325
326 case 'x': DO_DUMP(int, 4, " %8.8x");
327 case 'd': DO_DUMP(unsigned int, 4, " %10d");
328 case 'w': DO_DUMP(unsigned short, 8, " %04x");
329 case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
330 case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);
Alexandre Julliard808cb041995-08-17 17:11:36 +0000331 }
332}