winedbg: Proper handling of typedefs.
[wine/dcerpc.git] / programs / winedbg / memory.c
blobfca6e3d8da0475a437a90e584935e945b22e9583
1 /*
2 * Debugger memory handling
4 * Copyright 1993 Eric Youngdale
5 * Copyright 1995 Alexandre Julliard
6 * Copyright 2000-2005 Eric Pouech
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
30 #include "debugger.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
35 void* be_cpu_linearize(HANDLE hThread, const ADDRESS* addr)
37 assert(addr->Mode == AddrModeFlat);
38 return (void*)addr->Offset;
41 unsigned be_cpu_build_addr(HANDLE hThread, const CONTEXT* ctx, ADDRESS* addr,
42 unsigned seg, unsigned long offset)
44 addr->Mode = AddrModeFlat;
45 addr->Segment = 0; /* don't need segment */
46 addr->Offset = offset;
47 return TRUE;
50 void* memory_to_linear_addr(const ADDRESS* addr)
52 return be_cpu->linearize(dbg_curr_thread->handle, addr);
55 BOOL memory_get_current_pc(ADDRESS* addr)
57 assert(be_cpu->get_addr);
58 return be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
59 be_cpu_addr_pc, addr);
62 BOOL memory_get_current_stack(ADDRESS* addr)
64 assert(be_cpu->get_addr);
65 return be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
66 be_cpu_addr_stack, addr);
69 BOOL memory_get_current_frame(ADDRESS* addr)
71 assert(be_cpu->get_addr);
72 return be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
73 be_cpu_addr_frame, addr);
76 static void memory_report_invalid_addr(const void* addr)
78 ADDRESS address;
80 address.Mode = AddrModeFlat;
81 address.Segment = 0;
82 address.Offset = (unsigned long)addr;
83 dbg_printf("*** Invalid address ");
84 print_address(&address, FALSE);
85 dbg_printf("\n");
88 /***********************************************************************
89 * memory_read_value
91 * Read a memory value.
93 BOOL memory_read_value(const struct dbg_lvalue* lvalue, DWORD size, void* result)
95 BOOL ret = FALSE;
97 if (lvalue->cookie == DLV_TARGET)
99 void* linear = memory_to_linear_addr(&lvalue->addr);
100 if (!(ret = dbg_read_memory(linear, result, size)))
101 memory_report_invalid_addr(linear);
103 else
105 if (lvalue->addr.Offset)
107 memcpy(result, (void*)lvalue->addr.Offset, size);
108 ret = TRUE;
111 return TRUE;
114 /***********************************************************************
115 * memory_write_value
117 * Store a value in memory.
119 BOOL memory_write_value(const struct dbg_lvalue* lvalue, DWORD size, void* value)
121 BOOL ret = TRUE;
122 DWORD64 os;
124 os = ~(DWORD64)size;
125 types_get_info(&lvalue->type, TI_GET_LENGTH, &os);
126 assert(size == os);
128 /* FIXME: only works on little endian systems */
129 if (lvalue->cookie == DLV_TARGET)
131 void* linear = memory_to_linear_addr(&lvalue->addr);
132 if (!(ret = dbg_write_memory(linear, value, size)))
133 memory_report_invalid_addr(linear);
135 else
137 memcpy((void*)lvalue->addr.Offset, value, size);
139 return ret;
142 /***********************************************************************
143 * memory_examine
145 * Implementation of the 'x' command.
147 void memory_examine(const struct dbg_lvalue *lvalue, int count, char format)
149 int i;
150 char buffer[256];
151 ADDRESS addr;
152 void *linear;
154 types_extract_as_address(lvalue, &addr);
155 linear = memory_to_linear_addr(&addr);
157 if (format != 'i' && count > 1)
159 print_address(&addr, FALSE);
160 dbg_printf(": ");
163 switch (format)
165 case 'u':
166 if (count == 1) count = 256;
167 memory_get_string(dbg_curr_process, linear,
168 TRUE, TRUE, buffer, min(count, sizeof(buffer)));
169 dbg_printf("%s\n", buffer);
170 return;
171 case 's':
172 if (count == 1) count = 256;
173 memory_get_string(dbg_curr_process, linear,
174 TRUE, FALSE, buffer, min(count, sizeof(buffer)));
175 dbg_printf("%s\n", buffer);
176 return;
177 case 'i':
178 while (count-- && memory_disasm_one_insn(&addr));
179 return;
180 case 'g':
181 while (count--)
183 GUID guid;
184 if (!dbg_read_memory(linear, &guid, sizeof(guid)))
186 memory_report_invalid_addr(linear);
187 break;
189 dbg_printf("{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
190 guid.Data1, guid.Data2, guid.Data3,
191 guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
192 guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
193 linear = (char*)linear + sizeof(guid);
194 addr.Offset += sizeof(guid);
195 if (count)
197 print_address(&addr, FALSE);
198 dbg_printf(": ");
201 return;
203 #define DO_DUMP2(_t,_l,_f,_vv) { \
204 _t _v; \
205 for (i = 0; i < count; i++) { \
206 if (!dbg_read_memory(linear, &_v, sizeof(_t))) \
207 { memory_report_invalid_addr(linear); break; } \
208 dbg_printf(_f, (_vv)); \
209 addr.Offset += sizeof(_t); \
210 linear = (char*)linear + sizeof(_t); \
211 if ((i % (_l)) == (_l) - 1 && i != count - 1) \
213 dbg_printf("\n"); \
214 print_address(&addr, FALSE); \
215 dbg_printf(": "); \
218 dbg_printf("\n"); \
220 return
221 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
223 case 'x': DO_DUMP(int, 4, " %8.8x");
224 case 'd': DO_DUMP(unsigned int, 4, " %10d");
225 case 'w': DO_DUMP(unsigned short, 8, " %04x");
226 case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
227 case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);
231 BOOL memory_get_string(struct dbg_process* pcs, void* addr, BOOL in_debuggee,
232 BOOL unicode, char* buffer, int size)
234 DWORD sz;
235 WCHAR* buffW;
237 buffer[0] = 0;
238 if (!addr) return FALSE;
239 if (in_debuggee)
241 BOOL ret;
243 if (!unicode) ret = pcs->process_io->read(pcs->handle, addr, buffer, size, &sz);
244 else
246 buffW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
247 ret = pcs->process_io->read(pcs->handle, addr, buffW, size * sizeof(WCHAR), &sz);
248 WideCharToMultiByte(CP_ACP, 0, buffW, sz / sizeof(WCHAR), buffer, size,
249 NULL, NULL);
250 HeapFree(GetProcessHeap(), 0, buffW);
252 if (size) buffer[size-1] = 0;
253 return ret;
255 else
257 lstrcpynA(buffer, addr, size);
259 return TRUE;
262 BOOL memory_get_string_indirect(struct dbg_process* pcs, void* addr, BOOL unicode, char* buffer, int size)
264 void* ad;
265 DWORD sz;
267 buffer[0] = 0;
268 if (addr &&
269 pcs->process_io->read(pcs->handle, addr, &ad, sizeof(ad), &sz) && sz == sizeof(ad) && ad)
271 return memory_get_string(pcs, ad, TRUE, unicode, buffer, size);
273 return FALSE;
276 static void print_typed_basic(const struct dbg_lvalue* lvalue)
278 LONGLONG val_int;
279 void* val_ptr;
280 long double val_real;
281 DWORD64 size64;
282 DWORD tag, size, count, bt;
283 struct dbg_type type = lvalue->type;
284 struct dbg_type sub_type;
286 if (!types_get_real_type(&type, &tag)) return;
288 switch (tag)
290 case SymTagBaseType:
291 if (!types_get_info(&type, TI_GET_LENGTH, &size64) ||
292 !types_get_info(&type, TI_GET_BASETYPE, &bt))
294 WINE_ERR("Couldn't get information\n");
295 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
297 size = (DWORD)size64;
298 switch (bt)
300 case btInt:
301 if (!be_cpu->fetch_integer(lvalue, size, TRUE, &val_int)) return;
302 dbg_printf("%lld", val_int);
303 break;
304 case btUInt:
305 if (!be_cpu->fetch_integer(lvalue, size, FALSE, &val_int)) return;
306 dbg_printf("%llu", val_int);
307 break;
308 case btFloat:
309 if (!be_cpu->fetch_float(lvalue, size, &val_real)) return;
310 dbg_printf("%Lf", val_real);
311 break;
312 case btChar:
313 if (!be_cpu->fetch_integer(lvalue, size, TRUE, &val_int)) return;
314 /* FIXME: should do the same for a Unicode character (size == 2) */
315 if (size == 1 && (val_int < 0x20 || val_int > 0x80))
316 dbg_printf("%d", (int)val_int);
317 else
318 dbg_printf("'%c'", (char)val_int);
319 break;
320 default:
321 WINE_FIXME("Unsupported basetype %lu\n", bt);
322 break;
324 break;
325 case SymTagPointerType:
326 if (!memory_read_value(lvalue, sizeof(void*), &val_ptr)) return;
328 sub_type.module = lvalue->type.module;
329 if (!types_get_info(&type, TI_GET_TYPE, &sub_type.id) ||
330 sub_type.id == dbg_itype_none)
332 dbg_printf("Internal symbol error: unable to access memory location %p", val_ptr);
333 break;
335 if (!types_get_real_type(&sub_type, &tag)) return;
337 if (types_get_info(&sub_type, TI_GET_SYMTAG, &tag) && tag == SymTagBaseType &&
338 types_get_info(&sub_type, TI_GET_BASETYPE, &bt) && bt == btChar &&
339 types_get_info(&sub_type, TI_GET_LENGTH, &size64))
341 char buffer[1024];
343 if (!val_ptr) dbg_printf("0x0");
344 else if (memory_get_string(dbg_curr_process, val_ptr,
345 lvalue->cookie == DLV_TARGET,
346 size64 == 2, buffer, sizeof(buffer)))
347 dbg_printf("\"%s\"", buffer);
348 else
349 dbg_printf("*** invalid address %p ***", val_ptr);
351 else dbg_printf("%p", val_ptr);
352 break;
353 case SymTagArrayType:
354 case SymTagUDT:
355 assert(lvalue->cookie == DLV_TARGET);
356 if (!memory_read_value(lvalue, sizeof(val_ptr), &val_ptr)) return;
357 dbg_printf("%p", val_ptr);
358 break;
359 case SymTagEnum:
361 BOOL ok = FALSE;
363 assert(lvalue->cookie == DLV_TARGET);
364 /* FIXME: it depends on underlying type for enums
365 * (not supported yet in dbghelp)
366 * Assuming 4 as for an int
368 if (!be_cpu->fetch_integer(lvalue, 4, TRUE, &val_int)) return;
370 if (types_get_info(&type, TI_GET_CHILDRENCOUNT, &count))
372 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
373 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
374 WCHAR* ptr;
375 char tmp[256];
376 VARIANT variant;
377 int i;
379 fcp->Start = 0;
380 while (count)
382 fcp->Count = min(count, 256);
383 if (types_get_info(&type, TI_FINDCHILDREN, fcp))
385 sub_type.module = type.module;
386 for (i = 0; i < min(fcp->Count, count); i++)
388 sub_type.id = fcp->ChildId[i];
389 if (!types_get_info(&sub_type, TI_GET_VALUE, &variant))
390 continue;
391 switch (variant.n1.n2.vt)
393 case VT_I4: ok = (val_int == variant.n1.n2.n3.lVal); break;
394 default: WINE_FIXME("Unsupported variant type (%u)\n", variant.n1.n2.vt);
396 if (ok)
398 ptr = NULL;
399 types_get_info(&sub_type, TI_GET_SYMNAME, &ptr);
400 if (!ptr) continue;
401 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
402 HeapFree(GetProcessHeap(), 0, ptr);
403 dbg_printf("%s", tmp);
404 count = 0; /* so that we'll get away from outter loop */
405 break;
410 count -= min(count, 256);
411 fcp->Start += 256;
413 if (!ok) dbg_printf("%lld", val_int);
415 break;
416 default:
417 WINE_FIXME("Unsupported tag %lu\n", tag);
418 break;
422 /***********************************************************************
423 * print_basic
425 * Implementation of the 'print' command.
427 void print_basic(const struct dbg_lvalue* lvalue, int count, char format)
429 long int res;
431 if (lvalue->type.id == dbg_itype_none)
433 dbg_printf("Unable to evaluate expression\n");
434 return;
437 res = types_extract_as_integer(lvalue);
439 /* FIXME: this implies i386 byte ordering */
440 switch (format)
442 case 'x':
443 if (lvalue->addr.Mode == AddrMode1616 ||
444 lvalue->addr.Mode == AddrModeReal)
445 dbg_printf("0x%04lx", res);
446 else
447 dbg_printf("0x%08lx", res);
448 break;
450 case 'd':
451 dbg_printf("%ld\n", res);
452 break;
454 case 'c':
455 dbg_printf("%d = '%c'", (char)(res & 0xff), (char)(res & 0xff));
456 break;
458 case 'u':
460 WCHAR wch = (WCHAR)(res & 0xFFFF);
461 dbg_printf("%d = '", wch);
462 dbg_outputW(&wch, 1);
463 dbg_printf("'");
465 break;
467 case 'i':
468 case 's':
469 case 'w':
470 case 'b':
471 dbg_printf("Format specifier '%c' is meaningless in 'print' command\n", format);
472 case 0:
473 if (lvalue->type.id == dbg_itype_segptr)
474 dbg_printf("%ld", res);
475 else
476 print_typed_basic(lvalue);
477 break;
481 void print_bare_address(const ADDRESS* addr)
483 switch (addr->Mode)
485 case AddrModeFlat:
486 dbg_printf("0x%08lx", addr->Offset);
487 break;
488 case AddrModeReal:
489 case AddrMode1616:
490 dbg_printf("0x%04x:0x%04lx", addr->Segment, addr->Offset);
491 break;
492 case AddrMode1632:
493 dbg_printf("0x%04x:0x%08lx", addr->Segment, addr->Offset);
494 break;
495 default:
496 dbg_printf("Unknown mode %x\n", addr->Mode);
497 break;
501 /***********************************************************************
502 * print_address
504 * Print an 16- or 32-bit address, with the nearest symbol if any.
506 void print_address(const ADDRESS* addr, BOOLEAN with_line)
508 char buffer[sizeof(SYMBOL_INFO) + 256];
509 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
510 void* lin = memory_to_linear_addr(addr);
511 DWORD64 disp64;
512 DWORD disp;
514 print_bare_address(addr);
516 si->SizeOfStruct = sizeof(*si);
517 si->MaxNameLen = 256;
518 if (!SymFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp64, si)) return;
519 dbg_printf(" %s", si->Name);
520 if (disp64) dbg_printf("+0x%lx", (DWORD_PTR)disp64);
521 if (with_line)
523 IMAGEHLP_LINE il;
524 IMAGEHLP_MODULE im;
526 il.SizeOfStruct = sizeof(il);
527 if (SymGetLineFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp, &il))
528 dbg_printf(" [%s:%lu]", il.FileName, il.LineNumber);
529 im.SizeOfStruct = sizeof(im);
530 if (SymGetModuleInfo(dbg_curr_process->handle, (DWORD_PTR)lin, &im))
531 dbg_printf(" in %s", im.ModuleName);
535 BOOL memory_disasm_one_insn(ADDRESS* addr)
537 char ch;
539 print_address(addr, TRUE);
540 dbg_printf(": ");
541 if (!dbg_read_memory(memory_to_linear_addr(addr), &ch, sizeof(ch)))
543 dbg_printf("-- no code accessible --\n");
544 return FALSE;
546 be_cpu->disasm_one_insn(addr, TRUE);
547 dbg_printf("\n");
548 return TRUE;
551 void memory_disassemble(const struct dbg_lvalue* xstart,
552 const struct dbg_lvalue* xend, int instruction_count)
554 static ADDRESS last = {0,0,0};
555 int stop = 0;
556 int i;
558 if (!xstart && !xend)
560 if (!last.Segment && !last.Offset) memory_get_current_pc(&last);
562 else
564 if (xstart)
565 types_extract_as_address(xstart, &last);
566 if (xend)
567 stop = types_extract_as_integer(xend);
569 for (i = 0; (instruction_count == 0 || i < instruction_count) &&
570 (stop == 0 || last.Offset <= stop); i++)
571 memory_disasm_one_insn(&last);
574 BOOL memory_get_register(DWORD regno, DWORD** value, char* buffer, int len)
576 const struct dbg_internal_var* div;
578 if (dbg_curr_thread->curr_frame != 0)
580 if (buffer) snprintf(buffer, len, "<register not in topmost frame>");
581 return FALSE;
583 for (div = dbg_context_vars; div->name; div++)
585 if (div->val == regno)
587 *value = div->pval;
588 snprintf(buffer, len, div->name);
589 return TRUE;
592 if (buffer) snprintf(buffer, len, "<unknown register %lu>", regno);
593 return FALSE;