memory.c 8.84 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4 5
/*
 * Debugger memory handling
 *
 * Copyright 1993 Eric Youngdale
 * Copyright 1995 Alexandre Julliard
6
 * Copyright 2000 Eric Pouech
7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Alexandre Julliard's avatar
Alexandre Julliard committed
21 22
 */

23
#include "config.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
24
#include <stdlib.h>
25
#include <string.h>
Patrik Stridvall's avatar
Patrik Stridvall committed
26

Alexandre Julliard's avatar
Alexandre Julliard committed
27
#include "debugger.h"
28
#include "winbase.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
29

30
#ifdef __i386__
31
#define IS_VM86_MODE() (DEBUG_context.EFlags & V86_FLAG)
32
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
33

34
static	void	DEBUG_Die(const char* msg)
Alexandre Julliard's avatar
Alexandre Julliard committed
35
{
36
   DEBUG_Printf(DBG_CHN_MESG, msg);
37 38
   exit(1);
}
39

40 41 42 43 44 45 46 47
void*	DEBUG_XMalloc(size_t size)
{
   void *res = malloc(size ? size : 1);
   if (res == NULL)
      DEBUG_Die("Memory exhausted.\n");
   memset(res, 0, size);
   return res;
}
48

49 50 51 52 53 54 55
void* DEBUG_XReAlloc(void *ptr, size_t size)
{
   void* res = realloc(ptr, size);
   if ((res == NULL) && size)
      DEBUG_Die("Memory exhausted.\n");
   return res;
}
56

57 58 59 60 61 62 63
char*	DEBUG_XStrDup(const char *str)
{
   char *res = strdup(str);
   if (!res)
      DEBUG_Die("Memory exhausted.\n");
   return res;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
64

65
enum dbg_mode DEBUG_GetSelectorType( WORD sel )
Alexandre Julliard's avatar
Alexandre Julliard committed
66
{
67
#ifdef __i386__
68 69
    LDT_ENTRY	le;

70 71
    if (IS_VM86_MODE()) return MODE_VM86;
    if (sel == 0) return MODE_32;
72
    if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, sel, &le)) 
73 74 75 76 77 78 79 80 81 82 83 84
        return le.HighWord.Bits.Default_Big ? MODE_32 : MODE_16;
    /* selector doesn't exist */
    return MODE_INVALID;
#else
    return MODE_32;
#endif
}
#ifdef __i386__
void DEBUG_FixAddress( DBG_ADDR *addr, DWORD def) 
{
   if (addr->seg == 0xffffffff) addr->seg = def;
   if (DEBUG_IsSelectorSystem(addr->seg)) addr->seg = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
85 86
}

87 88 89
/* Determine if sel is a system selector (i.e. not managed by Wine) */
BOOL	DEBUG_IsSelectorSystem(WORD sel)
{
90 91
    if (IS_VM86_MODE()) return FALSE;  /* no system selectors in vm86 mode */
    return !(sel & 4) || ((sel >> 3) < 17);
92 93
}
#endif /* __i386__ */
Alexandre Julliard's avatar
Alexandre Julliard committed
94

95 96 97 98 99
DWORD DEBUG_ToLinear( const DBG_ADDR *addr )
{
#ifdef __i386__
   LDT_ENTRY	le;
   
100 101
   if (IS_VM86_MODE()) return (DWORD)(LOWORD(addr->seg) << 4) + addr->off;

102 103 104 105 106 107 108 109 110 111 112 113
   if (DEBUG_IsSelectorSystem(addr->seg))
      return addr->off;
   
   if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, addr->seg, &le)) {
      return (le.HighWord.Bits.BaseHi << 24) + (le.HighWord.Bits.BaseMid << 16) + le.BaseLow + addr->off;
   }
   return 0;
#else
   return addr->off;
#endif
}

114
void DEBUG_GetCurrentAddress( DBG_ADDR *addr )
Alexandre Julliard's avatar
Alexandre Julliard committed
115
{
116
#ifdef __i386__
117 118
    addr->seg  = DEBUG_context.SegCs;

119
    if (DEBUG_IsSelectorSystem(addr->seg))
120 121
       addr->seg = 0;
    addr->off  = DEBUG_context.Eip;
122 123 124
#elif defined(__sparc__)
	 addr->seg = 0;
    addr->off = DEBUG_context.pc;
125
#else
126
#	error You must define GET_IP for this CPU
127
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
128 129
}

130 131 132 133 134 135 136 137
void	DEBUG_InvalAddr( const DBG_ADDR* addr )
{
   DEBUG_Printf(DBG_CHN_MESG,"*** Invalid address ");
   DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
   DEBUG_Printf(DBG_CHN_MESG,"\n");
   if (DBG_IVAR(ExtDbgOnInvalidAddress)) DEBUG_ExternalDebugger();
}

138 139 140 141 142 143
void	DEBUG_InvalLinAddr( void* addr )
{
   DBG_ADDR address;

   address.seg = 0;
   address.off = (unsigned long)addr;
144
   DEBUG_InvalAddr( &address );
145
}
Alexandre Julliard's avatar
Alexandre Julliard committed
146

Alexandre Julliard's avatar
Alexandre Julliard committed
147 148 149 150 151
/***********************************************************************
 *           DEBUG_ReadMemory
 *
 * Read a memory value.
 */
152 153 154 155
/* FIXME: this function is now getting closer and closer to 
 * DEBUG_ExprGetValue. They should be merged...
 */
int DEBUG_ReadMemory( const DBG_VALUE* val )
Alexandre Julliard's avatar
Alexandre Julliard committed
156
{
157 158 159 160 161 162 163 164 165 166 167
    int		value = 0; /* to clear any unused byte */
    int		os = DEBUG_GetObjectSize(val->type);

    assert(sizeof(value) >= os);

    /* FIXME: only works on little endian systems */

    if (val->cookie == DV_TARGET) {
       DBG_ADDR	addr = val->addr;
       void*	lin;

168
#ifdef __i386__
169
       DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
170
#endif
171 172 173 174 175 176 177
       lin = (void*)DEBUG_ToLinear( &addr );
       
       DEBUG_READ_MEM_VERBOSE(lin, &value, os);
    } else {
       if (val->addr.off)
	  memcpy(&value, (void*)val->addr.off, os);
    }
178
    return value;
Alexandre Julliard's avatar
Alexandre Julliard committed
179 180 181 182 183 184 185 186
}


/***********************************************************************
 *           DEBUG_WriteMemory
 *
 * Store a value in memory.
 */
187
void DEBUG_WriteMemory( const DBG_VALUE* val, int value )
Alexandre Julliard's avatar
Alexandre Julliard committed
188
{
189 190 191 192 193
    int		os = DEBUG_GetObjectSize(val->type);

    assert(sizeof(value) >= os);

    /* FIXME: only works on little endian systems */
Alexandre Julliard's avatar
Alexandre Julliard committed
194

195 196 197 198
    if (val->cookie == DV_TARGET) {
       DBG_ADDR addr = val->addr;
       void*	lin;

199
#ifdef __i386__
200
       DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
201
#endif
202 203 204 205 206
       lin = (void*)DEBUG_ToLinear( &addr );
       DEBUG_WRITE_MEM_VERBOSE(lin, &value, os);
    } else {
       memcpy((void*)val->addr.off, &value, os);
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
207 208 209
}

/***********************************************************************
210
 *           DEBUG_GrabAddress
Alexandre Julliard's avatar
Alexandre Julliard committed
211
 *
212
 * Get the address from a value
Alexandre Julliard's avatar
Alexandre Julliard committed
213
 */
214
BOOL DEBUG_GrabAddress( DBG_VALUE* value, BOOL fromCode )
Alexandre Julliard's avatar
Alexandre Julliard committed
215
{
216
    assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
217

218
#ifdef __i386__
219 220
    DEBUG_FixAddress( &value->addr, 
		      (fromCode) ? DEBUG_context.SegCs : DEBUG_context.SegDs);
221
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
222

Alexandre Julliard's avatar
Alexandre Julliard committed
223 224 225 226 227
    /*
     * Dereference pointer to get actual memory address we need to be
     * reading.  We will use the same segment as what we have already,
     * and hope that this is a sensible thing to do.
     */
228
    if (value->type != NULL) {
229
        if (value->type == DEBUG_GetBasicType(DT_BASIC_CONST_INT)) {
Alexandre Julliard's avatar
Alexandre Julliard committed
230 231 232 233 234
	    /*
	     * We know that we have the actual offset stored somewhere
	     * else in 32-bit space.  Grab it, and we
	     * should be all set.
	     */
235 236 237 238 239 240 241 242 243
	    unsigned int  seg2 = value->addr.seg;
	    value->addr.seg = 0;
	    value->addr.off = DEBUG_GetExprValue(value, NULL);
	    value->addr.seg = seg2;
	} else {
	    struct datatype	* testtype;

	    if (DEBUG_TypeDerefPointer(value, &testtype) == 0)
	        return FALSE;
244
	    if (testtype != NULL || value->type == DEBUG_GetBasicType(DT_BASIC_CONST_INT))
245 246 247 248 249
	        value->addr.off = DEBUG_GetExprValue(value, NULL);
	}
    } else if (!value->addr.seg && !value->addr.off) {
        DEBUG_Printf(DBG_CHN_MESG,"Invalid expression\n");
	return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
250
    }
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    return TRUE;
}

/***********************************************************************
 *           DEBUG_ExamineMemory
 *
 * Implementation of the 'x' command.
 */
void DEBUG_ExamineMemory( const DBG_VALUE *_value, int count, char format )
{
    DBG_VALUE		  value = *_value;
    int			  i;
    unsigned char	* pnt;

    if (!DEBUG_GrabAddress(&value, (format == 'i'))) return;
Alexandre Julliard's avatar
Alexandre Julliard committed
266

Alexandre Julliard's avatar
Alexandre Julliard committed
267 268
    if (format != 'i' && count > 1)
    {
269
        DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
270
        DEBUG_Printf(DBG_CHN_MESG,": ");
Alexandre Julliard's avatar
Alexandre Julliard committed
271 272
    }

273
    pnt = (void*)DEBUG_ToLinear( &value.addr );
Alexandre Julliard's avatar
Alexandre Julliard committed
274 275 276

    switch(format)
    {
277
	case 'u': {
278
		WCHAR wch;
279 280 281
		if (count == 1) count = 256;
                while (count--)
                {
Eric Pouech's avatar
Eric Pouech committed
282
		    if (!DEBUG_READ_MEM_VERBOSE(pnt, &wch, sizeof(wch)) || !wch)
283 284
		       break;
                    pnt += sizeof(wch);
285
                    DEBUG_Printf(DBG_CHN_MESG, "%c", (char)wch);
286
                }
287
		DEBUG_Printf(DBG_CHN_MESG,"\n");
288 289
		return;
	    }
290 291 292
          case 's': {
	        char ch;

Alexandre Julliard's avatar
Alexandre Julliard committed
293
		if (count == 1) count = 256;
Alexandre Julliard's avatar
Alexandre Julliard committed
294 295
                while (count--)
                {
Eric Pouech's avatar
Eric Pouech committed
296
                    if (!DEBUG_READ_MEM_VERBOSE(pnt, &ch, sizeof(ch)) || !ch)
297 298
		       break;
                    pnt++;
299
                    DEBUG_Output(DBG_CHN_MESG, &ch, 1);
Alexandre Julliard's avatar
Alexandre Julliard committed
300
                }
301
		DEBUG_Printf(DBG_CHN_MESG,"\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
302
		return;
303
	  }
Alexandre Julliard's avatar
Alexandre Julliard committed
304
	case 'i':
305
		while (count-- && DEBUG_DisassembleInstruction( &value.addr ));
Alexandre Julliard's avatar
Alexandre Julliard committed
306
		return;
307 308 309 310
#define DO_DUMP2(_t,_l,_f,_vv) { \
	        _t _v; \
		for(i=0; i<count; i++) { \
                    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,": ");\
317 318
                    } \
		} \
319
		DEBUG_Printf(DBG_CHN_MESG,"\n"); \
320 321 322 323 324 325 326 327 328
        } \
	return
#define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v) 

        case 'x': DO_DUMP(int, 4, " %8.8x");
	case 'd': DO_DUMP(unsigned int, 4, " %10d");	
	case 'w': DO_DUMP(unsigned short, 8, " %04x");
        case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
	case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);
Alexandre Julliard's avatar
Alexandre Julliard committed
329 330
	}
}