- get rid of winedbg internal channels for output
[wine] / programs / winedbg / memory.c
1 /*
2  * Debugger memory handling
3  *
4  * Copyright 1993 Eric Youngdale
5  * Copyright 1995 Alexandre Julliard
6  * Copyright 2000 Eric Pouech
7  *
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
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "debugger.h"
30 #include "winbase.h"
31
32 #ifdef __i386__
33 #define IS_VM86_MODE() (DEBUG_context.EFlags & V86_FLAG)
34 #endif
35
36 static  void    DEBUG_Die(const char* msg)
37 {
38    DEBUG_Printf(msg);
39    exit(1);
40 }
41
42 void* DBG_alloc(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
51 void* DBG_realloc(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
59 char* DBG_strdup(const char *str)
60 {
61    char *res = strdup(str);
62    if (!res)
63       DEBUG_Die("Memory exhausted.\n");
64    return res;
65 }
66
67 void DBG_free(void *ptr)
68 {
69     free(ptr);
70 }
71
72 enum dbg_mode DEBUG_GetSelectorType( WORD sel )
73 {
74 #ifdef __i386__
75     LDT_ENTRY   le;
76
77     if (IS_VM86_MODE()) return MODE_VM86;
78     if (sel == 0) return MODE_32;
79     if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, sel, &le))
80         return le.HighWord.Bits.Default_Big ? MODE_32 : MODE_16;
81     /* selector doesn't exist */
82     return MODE_INVALID;
83 #else
84     return MODE_32;
85 #endif
86 }
87 #ifdef __i386__
88 void DEBUG_FixAddress( DBG_ADDR *addr, DWORD def)
89 {
90    if (addr->seg == 0xffffffff) addr->seg = def;
91    if (DEBUG_IsSelectorSystem(addr->seg)) addr->seg = 0;
92 }
93
94 /* Determine if sel is a system selector (i.e. not managed by Wine) */
95 BOOL    DEBUG_IsSelectorSystem(WORD sel)
96 {
97     if (IS_VM86_MODE()) return FALSE;  /* no system selectors in vm86 mode */
98     return !(sel & 4) || ((sel >> 3) < 17);
99 }
100 #endif /* __i386__ */
101
102 DWORD DEBUG_ToLinear( const DBG_ADDR *addr )
103 {
104 #ifdef __i386__
105    LDT_ENTRY    le;
106
107    if (IS_VM86_MODE()) return (DWORD)(LOWORD(addr->seg) << 4) + addr->off;
108
109    if (DEBUG_IsSelectorSystem(addr->seg))
110       return addr->off;
111
112    if (GetThreadSelectorEntry( DEBUG_CurrThread->handle, addr->seg, &le)) {
113       return (le.HighWord.Bits.BaseHi << 24) + (le.HighWord.Bits.BaseMid << 16) + le.BaseLow + addr->off;
114    }
115    return 0;
116 #else
117    return addr->off;
118 #endif
119 }
120
121 void DEBUG_GetCurrentAddress( DBG_ADDR *addr )
122 {
123 #ifdef __i386__
124     addr->seg  = DEBUG_context.SegCs;
125
126     if (DEBUG_IsSelectorSystem(addr->seg))
127        addr->seg = 0;
128     addr->off  = DEBUG_context.Eip;
129 #elif defined(__sparc__)
130     addr->seg = 0;
131     addr->off = DEBUG_context.pc;
132 #elif defined(__powerpc__)
133     addr->seg = 0;
134     addr->off = DEBUG_context.Iar;
135 #else
136 #       error You must define GET_IP for this CPU
137 #endif
138 }
139
140 void    DEBUG_InvalAddr( const DBG_ADDR* addr )
141 {
142    DEBUG_Printf("*** Invalid address ");
143    DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, FALSE);
144    DEBUG_Printf("\n");
145    if (DBG_IVAR(ExtDbgOnInvalidAddress)) DEBUG_ExternalDebugger();
146 }
147
148 void    DEBUG_InvalLinAddr( void* addr )
149 {
150    DBG_ADDR address;
151
152    address.seg = 0;
153    address.off = (unsigned long)addr;
154    DEBUG_InvalAddr( &address );
155 }
156
157 /***********************************************************************
158  *           DEBUG_ReadMemory
159  *
160  * Read a memory value.
161  */
162 /* FIXME: this function is now getting closer and closer to
163  * DEBUG_ExprGetValue. They should be merged...
164  */
165 int DEBUG_ReadMemory( const DBG_VALUE* val )
166 {
167     int         value = 0; /* to clear any unused byte */
168     int         os = DEBUG_GetObjectSize(val->type);
169
170     assert(sizeof(value) >= os);
171
172     /* FIXME: only works on little endian systems */
173
174     if (val->cookie == DV_TARGET) {
175        DBG_ADDR addr = val->addr;
176        void*    lin;
177
178 #ifdef __i386__
179        DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
180 #endif
181        lin = (void*)DEBUG_ToLinear( &addr );
182
183        DEBUG_READ_MEM_VERBOSE(lin, &value, os);
184     } else {
185        if (val->addr.off)
186           memcpy(&value, (void*)val->addr.off, os);
187     }
188     return value;
189 }
190
191
192 /***********************************************************************
193  *           DEBUG_WriteMemory
194  *
195  * Store a value in memory.
196  */
197 void DEBUG_WriteMemory( const DBG_VALUE* val, int value )
198 {
199     int         os = DEBUG_GetObjectSize(val->type);
200
201     assert(sizeof(value) >= os);
202
203     /* FIXME: only works on little endian systems */
204
205     if (val->cookie == DV_TARGET) {
206        DBG_ADDR addr = val->addr;
207        void*    lin;
208
209 #ifdef __i386__
210        DEBUG_FixAddress( &addr, DEBUG_context.SegDs );
211 #endif
212        lin = (void*)DEBUG_ToLinear( &addr );
213        DEBUG_WRITE_MEM_VERBOSE(lin, &value, os);
214     } else {
215        memcpy((void*)val->addr.off, &value, os);
216     }
217 }
218
219 /***********************************************************************
220  *           DEBUG_GrabAddress
221  *
222  * Get the address from a value
223  */
224 BOOL DEBUG_GrabAddress( DBG_VALUE* value, BOOL fromCode )
225 {
226     assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
227
228 #ifdef __i386__
229     DEBUG_FixAddress( &value->addr,
230                       (fromCode) ? DEBUG_context.SegCs : DEBUG_context.SegDs);
231 #endif
232
233     /*
234      * Dereference pointer to get actual memory address we need to be
235      * reading.  We will use the same segment as what we have already,
236      * and hope that this is a sensible thing to do.
237      */
238     if (value->type != NULL) {
239         if (value->type == DEBUG_GetBasicType(DT_BASIC_CONST_INT)) {
240             /*
241              * We know that we have the actual offset stored somewhere
242              * else in 32-bit space.  Grab it, and we
243              * should be all set.
244              */
245             unsigned int  seg2 = value->addr.seg;
246             value->addr.seg = 0;
247             value->addr.off = DEBUG_GetExprValue(value, NULL);
248             value->addr.seg = seg2;
249         } else {
250             struct datatype     * testtype;
251
252             if (DEBUG_TypeDerefPointer(value, &testtype) == 0)
253                 return FALSE;
254             if (testtype != NULL || value->type == DEBUG_GetBasicType(DT_BASIC_CONST_INT))
255                 value->addr.off = DEBUG_GetExprValue(value, NULL);
256         }
257     } else if (!value->addr.seg && !value->addr.off) {
258         DEBUG_Printf("Invalid expression\n");
259         return FALSE;
260     }
261     return TRUE;
262 }
263
264 /***********************************************************************
265  *           DEBUG_ExamineMemory
266  *
267  * Implementation of the 'x' command.
268  */
269 void DEBUG_ExamineMemory( const DBG_VALUE *_value, int count, char format )
270 {
271     DBG_VALUE             value = *_value;
272     int                   i;
273     unsigned char       * pnt;
274
275     if (!DEBUG_GrabAddress(&value, (format == 'i'))) return;
276
277     if (format != 'i' && count > 1)
278     {
279         DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
280         DEBUG_Printf(": ");
281     }
282
283     pnt = (void*)DEBUG_ToLinear( &value.addr );
284
285     switch(format)
286     {
287          case 'u':
288                 if (count == 1) count = 256;
289                 DEBUG_nchar += DEBUG_PrintStringW(&value.addr, count);
290                 DEBUG_Printf("\n");
291                 return;
292         case 's':
293                 if (count == 1) count = 256;
294                 DEBUG_nchar += DEBUG_PrintStringA(&value.addr, count);
295                 DEBUG_Printf("\n");
296                 return;
297         case 'i':
298                 while (count-- && DEBUG_DisassembleInstruction( &value.addr ));
299                 return;
300         case 'g':
301                 while (count--)
302                 {
303                     GUID guid;
304                     if (!DEBUG_READ_MEM_VERBOSE(pnt, &guid, sizeof(guid))) break;
305                     DEBUG_Printf("{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
306                                  guid.Data1, guid.Data2, guid.Data3,
307                                  guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
308                                  guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7] );
309                     pnt += sizeof(guid);
310                     value.addr.off += sizeof(guid);
311                     if (count)
312                     {
313                         DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );
314                         DEBUG_Printf(": ");
315                     }
316                 }
317                 return;
318
319 #define DO_DUMP2(_t,_l,_f,_vv) { \
320                 _t _v; \
321                 for(i=0; i<count; i++) { \
322                     if (!DEBUG_READ_MEM_VERBOSE(pnt, &_v, sizeof(_t))) break; \
323                     DEBUG_Printf(_f,(_vv)); \
324                     pnt += sizeof(_t); value.addr.off += sizeof(_t); \
325                     if ((i % (_l)) == (_l)-1) { \
326                         DEBUG_Printf("\n"); \
327                         DEBUG_PrintAddress( &value.addr, DEBUG_CurrThread->dbg_mode, FALSE );\
328                         DEBUG_Printf(": ");\
329                     } \
330                 } \
331                 DEBUG_Printf("\n"); \
332         } \
333         return
334 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
335
336         case 'x': DO_DUMP(int, 4, " %8.8x");
337         case 'd': DO_DUMP(unsigned int, 4, " %10d");
338         case 'w': DO_DUMP(unsigned short, 8, " %04x");
339         case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
340         case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);
341         }
342 }
343
344 #define CHARBUFSIZE 16
345
346 /******************************************************************
347  *              DEBUG_PrintStringA
348  *
349  * Prints on channel chnl, the string starting at address in target
350  * address space. The string stops when either len chars (if <> -1)
351  * have been printed, or the '\0' char is printed
352  */
353 int  DEBUG_PrintStringA(const DBG_ADDR* address, int len)
354 {
355     char*       lin = (void*)DEBUG_ToLinear(address);
356     char        ch[CHARBUFSIZE+1];
357     int         written = 0;
358
359     if (len == -1) len = 32767; /* should be big enough */
360
361     while (written < len)
362     {
363         int to_write = min(CHARBUFSIZE, len - written );
364         if (!DEBUG_READ_MEM_VERBOSE(lin, ch, to_write)) break;
365         ch[to_write] = '\0';  /* protect from displaying junk */
366         to_write = lstrlenA(ch);
367         DEBUG_OutputA(ch, to_write);
368         lin += to_write;
369         written += to_write;
370         if (to_write < CHARBUFSIZE) break;
371     }
372     return written; /* number of actually written chars */
373 }
374
375 int  DEBUG_PrintStringW(const DBG_ADDR* address, int len)
376 {
377     char*       lin = (void*)DEBUG_ToLinear(address);
378     WCHAR       ch[CHARBUFSIZE+1];
379     int         written = 0;
380
381     if (len == -1) len = 32767; /* should be big enough */
382
383     while  (written < len)
384     {
385         int to_write = min(CHARBUFSIZE, len - written );
386         if (!DEBUG_READ_MEM_VERBOSE(lin, ch, to_write * sizeof(WCHAR))) break;
387         ch[to_write] = 0;  /* protect from displaying junk */
388         to_write = lstrlenW(ch);
389         DEBUG_OutputW(ch, to_write);
390         lin += to_write;
391         written += to_write;
392         if (to_write < CHARBUFSIZE) break;
393     }
394     return written; /* number of actually written chars */
395 }