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
24 #include "wine/port.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
36 void* be_cpu_linearize(HANDLE hThread, const ADDRESS64* addr)
38 assert(addr->Mode == AddrModeFlat);
39 return (void*)(DWORD_PTR)addr->Offset;
42 unsigned be_cpu_build_addr(HANDLE hThread, const CONTEXT* ctx, ADDRESS64* addr,
43 unsigned seg, unsigned long offset)
45 addr->Mode = AddrModeFlat;
46 addr->Segment = 0; /* don't need segment */
47 addr->Offset = offset;
51 void* memory_to_linear_addr(const ADDRESS64* addr)
53 return be_cpu->linearize(dbg_curr_thread->handle, addr);
56 BOOL memory_get_current_pc(ADDRESS64* addr)
58 assert(be_cpu->get_addr);
59 return be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
60 be_cpu_addr_pc, addr);
63 BOOL memory_get_current_stack(ADDRESS64* addr)
65 assert(be_cpu->get_addr);
66 return be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
67 be_cpu_addr_stack, addr);
70 BOOL memory_get_current_frame(ADDRESS64* addr)
72 assert(be_cpu->get_addr);
73 return be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
74 be_cpu_addr_frame, addr);
77 static void memory_report_invalid_addr(const void* addr)
81 address.Mode = AddrModeFlat;
83 address.Offset = (unsigned long)addr;
84 dbg_printf("*** Invalid address ");
85 print_address(&address, FALSE);
89 /***********************************************************************
92 * Read a memory value.
94 BOOL memory_read_value(const struct dbg_lvalue* lvalue, DWORD size, void* result)
98 if (lvalue->cookie == DLV_TARGET)
100 void* linear = memory_to_linear_addr(&lvalue->addr);
101 if (!(ret = dbg_read_memory(linear, result, size)))
102 memory_report_invalid_addr(linear);
106 if (lvalue->addr.Offset)
108 memcpy(result, (void*)(DWORD_PTR)lvalue->addr.Offset, size);
115 /***********************************************************************
118 * Store a value in memory.
120 BOOL memory_write_value(const struct dbg_lvalue* lvalue, DWORD size, void* value)
125 if (!types_get_info(&lvalue->type, TI_GET_LENGTH, &os)) return FALSE;
128 dbg_printf("Size mismatch in memory_write_value, got %u from type while expecting %u\n",
133 /* FIXME: only works on little endian systems */
134 if (lvalue->cookie == DLV_TARGET)
136 void* linear = memory_to_linear_addr(&lvalue->addr);
137 if (!(ret = dbg_write_memory(linear, value, size)))
138 memory_report_invalid_addr(linear);
142 memcpy((void*)(DWORD_PTR)lvalue->addr.Offset, value, size);
147 /***********************************************************************
150 * Implementation of the 'x' command.
152 void memory_examine(const struct dbg_lvalue *lvalue, int count, char format)
159 types_extract_as_address(lvalue, &addr);
160 linear = memory_to_linear_addr(&addr);
162 if (format != 'i' && count > 1)
164 print_address(&addr, FALSE);
171 if (count == 1) count = 256;
172 memory_get_string(dbg_curr_process, linear,
173 TRUE, TRUE, buffer, min(count, sizeof(buffer)));
174 dbg_printf("%s\n", buffer);
177 if (count == 1) count = 256;
178 memory_get_string(dbg_curr_process, linear,
179 TRUE, FALSE, buffer, min(count, sizeof(buffer)));
180 dbg_printf("%s\n", buffer);
183 while (count-- && memory_disasm_one_insn(&addr));
189 if (!dbg_read_memory(linear, &guid, sizeof(guid)))
191 memory_report_invalid_addr(linear);
194 dbg_printf("{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
195 guid.Data1, guid.Data2, guid.Data3,
196 guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
197 guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
198 linear = (char*)linear + sizeof(guid);
199 addr.Offset += sizeof(guid);
202 print_address(&addr, FALSE);
208 #define DO_DUMP2(_t,_l,_f,_vv) { \
210 for (i = 0; i < count; i++) { \
211 if (!dbg_read_memory(linear, &_v, sizeof(_t))) \
212 { memory_report_invalid_addr(linear); break; } \
213 dbg_printf(_f, (_vv)); \
214 addr.Offset += sizeof(_t); \
215 linear = (char*)linear + sizeof(_t); \
216 if ((i % (_l)) == (_l) - 1 && i != count - 1) \
219 print_address(&addr, FALSE); \
225 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
227 case 'x': DO_DUMP(int, 4, " %8.8x"); break;
228 case 'd': DO_DUMP(unsigned int, 4, " %4.4d"); break;
229 case 'w': DO_DUMP(unsigned short, 8, " %04x"); break;
231 if (sizeof(DWORD_PTR) == 4)
233 DO_DUMP(DWORD_PTR, 4, " %8.8lx");
237 DO_DUMP(DWORD_PTR, 2, " %16.16lx");
240 case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v); break;
241 case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff); break;
245 BOOL memory_get_string(struct dbg_process* pcs, void* addr, BOOL in_debuggee,
246 BOOL unicode, char* buffer, int size)
252 if (!addr) return FALSE;
257 if (!unicode) ret = pcs->process_io->read(pcs->handle, addr, buffer, size, &sz);
260 buffW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
261 ret = pcs->process_io->read(pcs->handle, addr, buffW, size * sizeof(WCHAR), &sz);
262 WideCharToMultiByte(CP_ACP, 0, buffW, sz / sizeof(WCHAR), buffer, size,
264 HeapFree(GetProcessHeap(), 0, buffW);
266 if (size) buffer[size-1] = 0;
271 lstrcpynA(buffer, addr, size);
276 BOOL memory_get_string_indirect(struct dbg_process* pcs, void* addr, BOOL unicode, WCHAR* buffer, int size)
283 pcs->process_io->read(pcs->handle, addr, &ad, sizeof(ad), &sz) && sz == sizeof(ad) && ad)
289 ret = pcs->process_io->read(pcs->handle, ad, buffer, size * sizeof(WCHAR), &sz) && sz != 0;
292 if ((buff = HeapAlloc(GetProcessHeap(), 0, size)))
294 ret = pcs->process_io->read(pcs->handle, ad, buff, size, &sz) && sz != 0;
295 MultiByteToWideChar(CP_ACP, 0, buff, sz, buffer, size);
296 HeapFree(GetProcessHeap(), 0, buff);
300 if (size) buffer[size-1] = 0;
307 * Convert an address offset to hex string. If mode == 32, treat offset as
308 * 32 bits (discard upper 32 bits), if mode == 64 use all 64 bits, if mode == 0
309 * treat as either 32 or 64 bits, depending on whether we're running as
312 char* memory_offset_to_string(char *str, DWORD64 offset, unsigned mode)
314 if (mode != 32 && mode != 64)
324 sprintf(str, "0x%08x", (unsigned int) offset);
326 sprintf(str, "0x%08x%08x", (unsigned int)(offset >> 32),
327 (unsigned int)offset);
332 static void dbg_print_longlong(LONGLONG sv, BOOL is_signed)
334 char tmp[24], *ptr = tmp + sizeof(tmp) - 1;
337 if (is_signed && sv < 0) uv = -sv;
338 else { uv = sv; is_signed = FALSE; }
339 for (div = 10; uv; div *= 10, uv /= 10)
340 *--ptr = '0' + (uv % 10);
341 if (ptr == tmp + sizeof(tmp) - 1) *--ptr = '0';
342 if (is_signed) *--ptr = '-';
343 dbg_printf("%s", ptr);
346 static void dbg_print_hex(DWORD size, ULONGLONG sv)
350 else if (size > 4 && (sv >> 32))
351 dbg_printf("0x%x%08x", (DWORD)(sv >> 32), (DWORD)sv);
353 dbg_printf("0x%x", (DWORD)sv);
356 static void print_typed_basic(const struct dbg_lvalue* lvalue)
360 long double val_real;
362 DWORD tag, size, count, bt;
363 struct dbg_type type = lvalue->type;
364 struct dbg_type sub_type;
365 struct dbg_lvalue sub_lvalue;
367 if (!types_get_real_type(&type, &tag)) return;
372 if (!types_get_info(&type, TI_GET_LENGTH, &size64) ||
373 !types_get_info(&type, TI_GET_BASETYPE, &bt))
375 WINE_ERR("Couldn't get information\n");
376 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
378 size = (DWORD)size64;
383 if (!be_cpu->fetch_integer(lvalue, size, TRUE, &val_int)) return;
384 if (size == 1) goto print_char;
385 dbg_print_hex(size, val_int);
389 if (!be_cpu->fetch_integer(lvalue, size, FALSE, &val_int)) return;
390 dbg_print_hex(size, val_int);
393 if (!be_cpu->fetch_float(lvalue, size, &val_real)) return;
394 dbg_printf("%Lf", val_real);
398 /* sometimes WCHAR is defined as btChar with size = 2, so discrimate
399 * Ansi/Unicode based on size, not on basetype
401 if (!be_cpu->fetch_integer(lvalue, size, TRUE, &val_int)) return;
403 if (size == 1 && isprint((char)val_int))
404 dbg_printf("'%c'", (char)val_int);
405 else if (size == 2 && isprintW((WCHAR)val_int))
407 WCHAR wch = (WCHAR)val_int;
409 dbg_outputW(&wch, 1);
413 dbg_printf("%d", (int)val_int);
416 if (!be_cpu->fetch_integer(lvalue, size, TRUE, &val_int)) return;
417 dbg_printf("%s", val_int ? "true" : "false");
420 WINE_FIXME("Unsupported basetype %u\n", bt);
424 case SymTagPointerType:
425 if (!types_array_index(lvalue, 0, &sub_lvalue))
427 dbg_printf("Internal symbol error: unable to access memory location %p",
428 memory_to_linear_addr(&lvalue->addr));
431 val_ptr = memory_to_linear_addr(&sub_lvalue.addr);
432 if (types_get_real_type(&sub_lvalue.type, &tag) && tag == SymTagBaseType &&
433 types_get_info(&sub_lvalue.type, TI_GET_BASETYPE, &bt) &&
434 types_get_info(&sub_lvalue.type, TI_GET_LENGTH, &size64))
438 if (!val_ptr) dbg_printf("0x0");
439 else if (((bt == btChar || bt == btInt) && size64 == 1) || (bt == btUInt && size64 == 2))
441 if (memory_get_string(dbg_curr_process, val_ptr, sub_lvalue.cookie == DLV_TARGET,
442 size64 == 2, buffer, sizeof(buffer)))
443 dbg_printf("\"%s\"", buffer);
445 dbg_printf("*** invalid address %p ***", val_ptr);
449 dbg_printf("%p", val_ptr);
451 case SymTagArrayType:
453 if (!memory_read_value(lvalue, sizeof(val_ptr), &val_ptr)) return;
454 dbg_printf("%p", val_ptr);
460 /* FIXME: it depends on underlying type for enums
461 * (not supported yet in dbghelp)
462 * Assuming 4 as for an int
464 if (!be_cpu->fetch_integer(lvalue, 4, TRUE, &val_int)) return;
466 if (types_get_info(&type, TI_GET_CHILDRENCOUNT, &count))
468 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
469 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
478 fcp->Count = min(count, 256);
479 if (types_get_info(&type, TI_FINDCHILDREN, fcp))
481 sub_type.module = type.module;
482 for (i = 0; i < min(fcp->Count, count); i++)
484 sub_type.id = fcp->ChildId[i];
485 if (!types_get_info(&sub_type, TI_GET_VALUE, &variant))
487 switch (variant.n1.n2.vt)
489 case VT_I4: ok = (val_int == variant.n1.n2.n3.lVal); break;
490 default: WINE_FIXME("Unsupported variant type (%u)\n", variant.n1.n2.vt);
495 types_get_info(&sub_type, TI_GET_SYMNAME, &ptr);
497 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
498 HeapFree(GetProcessHeap(), 0, ptr);
499 dbg_printf("%s", tmp);
500 count = 0; /* so that we'll get away from outter loop */
505 count -= min(count, 256);
509 if (!ok) dbg_print_longlong(val_int, TRUE);
513 WINE_FIXME("Unsupported tag %u\n", tag);
518 /***********************************************************************
521 * Implementation of the 'print' command.
523 void print_basic(const struct dbg_lvalue* lvalue, char format)
525 if (lvalue->type.id == dbg_itype_none)
527 dbg_printf("Unable to evaluate expression\n");
534 LONGLONG res = types_extract_as_longlong(lvalue, &size);
540 dbg_print_hex(size, (ULONGLONG)res);
544 dbg_print_longlong(res, TRUE);
548 dbg_printf("%d = '%c'", (char)(res & 0xff), (char)(res & 0xff));
552 wch = (WCHAR)(res & 0xFFFF);
553 dbg_printf("%d = '", wch);
554 dbg_outputW(&wch, 1);
562 dbg_printf("Format specifier '%c' is meaningless in 'print' command\n", format);
565 if (lvalue->type.id == dbg_itype_segptr)
567 dbg_print_longlong(types_extract_as_longlong(lvalue, NULL), TRUE);
569 else print_typed_basic(lvalue);
572 void print_bare_address(const ADDRESS64* addr)
574 char hexbuf[MAX_OFFSET_TO_STR_LEN];
579 dbg_printf("%s", memory_offset_to_string(hexbuf, addr->Offset, 0));
583 dbg_printf("0x%04x:0x%04x", addr->Segment, (unsigned) addr->Offset);
586 dbg_printf("0x%04x:%s", addr->Segment,
587 memory_offset_to_string(hexbuf, addr->Offset, 32));
590 dbg_printf("Unknown mode %x\n", addr->Mode);
595 /***********************************************************************
598 * Print an 16- or 32-bit address, with the nearest symbol if any.
600 void print_address(const ADDRESS64* addr, BOOLEAN with_line)
602 char buffer[sizeof(SYMBOL_INFO) + 256];
603 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
604 void* lin = memory_to_linear_addr(addr);
608 print_bare_address(addr);
610 si->SizeOfStruct = sizeof(*si);
611 si->MaxNameLen = 256;
612 if (!SymFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp64, si)) return;
613 dbg_printf(" %s", si->Name);
614 if (disp64) dbg_printf("+0x%lx", (DWORD_PTR)disp64);
620 il.SizeOfStruct = sizeof(il);
621 if (SymGetLineFromAddr64(dbg_curr_process->handle, (DWORD_PTR)lin, &disp, &il))
622 dbg_printf(" [%s:%u]", il.FileName, il.LineNumber);
623 im.SizeOfStruct = sizeof(im);
624 if (SymGetModuleInfo(dbg_curr_process->handle, (DWORD_PTR)lin, &im))
625 dbg_printf(" in %s", im.ModuleName);
629 BOOL memory_disasm_one_insn(ADDRESS64* addr)
633 print_address(addr, TRUE);
635 if (!dbg_read_memory(memory_to_linear_addr(addr), &ch, sizeof(ch)))
637 dbg_printf("-- no code accessible --\n");
640 be_cpu->disasm_one_insn(addr, TRUE);
645 void memory_disassemble(const struct dbg_lvalue* xstart,
646 const struct dbg_lvalue* xend, int instruction_count)
648 static ADDRESS64 last = {0,0,0};
652 if (!xstart && !xend)
654 if (!last.Segment && !last.Offset) memory_get_current_pc(&last);
659 types_extract_as_address(xstart, &last);
661 stop = types_extract_as_integer(xend);
663 for (i = 0; (instruction_count == 0 || i < instruction_count) &&
664 (stop == 0 || last.Offset <= stop); i++)
665 memory_disasm_one_insn(&last);
668 BOOL memory_get_register(DWORD regno, DWORD_PTR** value, char* buffer, int len)
670 const struct dbg_internal_var* div;
672 /* negative register values are wine's dbghelp hacks
673 * see dlls/dbghelp/dbghelp_private.h for the details
678 if (buffer) snprintf(buffer, len, "<internal error>");
681 if (buffer) snprintf(buffer, len, "<couldn't compute location>");
684 if (buffer) snprintf(buffer, len, "<is not available>");
687 if (buffer) snprintf(buffer, len, "<couldn't read memory>");
690 if (buffer) snprintf(buffer, len, "<has been optimized away by compiler>");
694 for (div = be_cpu->context_vars; div->name; div++)
696 if (div->val == regno)
698 if (!stack_get_register_frame(div, value))
700 if (buffer) snprintf(buffer, len, "<register %s not accessible in this frame>", div->name);
704 if (buffer) lstrcpynA(buffer, div->name, len);
708 if (buffer) snprintf(buffer, len, "<unknown register %u>", regno);