2 * Debugger memory handling
4 * Copyright 1993 Eric Youngdale
5 * Copyright 1995 Alexandre Julliard
6 * Copyright 2000-2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.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;
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)
80 address.Mode = AddrModeFlat;
82 address.Offset = (unsigned long)addr;
83 dbg_printf("*** Invalid address ");
84 print_address(&address, FALSE);
88 /***********************************************************************
91 * Read a memory value.
93 BOOL memory_read_value(const struct dbg_lvalue* lvalue, DWORD size, void* result)
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);
105 if (lvalue->addr.Offset)
107 memcpy(result, (void*)lvalue->addr.Offset, size);
114 /***********************************************************************
117 * Store a value in memory.
119 BOOL memory_write_value(const struct dbg_lvalue* lvalue, DWORD size, void* value)
125 types_get_info(&lvalue->type, TI_GET_LENGTH, &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);
137 memcpy((void*)lvalue->addr.Offset, value, size);
142 /***********************************************************************
145 * Implementation of the 'x' command.
147 void memory_examine(const struct dbg_lvalue *lvalue, int count, char format)
154 if (lvalue->type.id == dbg_itype_none) addr = lvalue->addr;
157 addr.Mode = AddrModeFlat;
158 addr.Offset = types_extract_as_integer( lvalue );
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("{%08lx-%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); \
226 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
228 case 'x': DO_DUMP(int, 4, " %8.8x");
229 case 'd': DO_DUMP(unsigned int, 4, " %10d");
230 case 'w': DO_DUMP(unsigned short, 8, " %04x");
231 case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
232 case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);
236 BOOL memory_get_string(struct dbg_process* pcs, void* addr, BOOL in_debuggee,
237 BOOL unicode, char* buffer, int size)
243 if (!addr) return FALSE;
248 if (!unicode) ret = pcs->process_io->read(pcs->handle, addr, buffer, size, &sz);
251 buffW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
252 ret = pcs->process_io->read(pcs->handle, addr, buffW, size * sizeof(WCHAR), &sz);
253 WideCharToMultiByte(CP_ACP, 0, buffW, sz / sizeof(WCHAR), buffer, size,
255 HeapFree(GetProcessHeap(), 0, buffW);
257 if (size) buffer[size-1] = 0;
262 lstrcpynA(buffer, addr, size);
267 BOOL memory_get_string_indirect(struct dbg_process* pcs, void* addr, BOOL unicode, char* buffer, int size)
274 pcs->process_io->read(pcs->handle, addr, &ad, sizeof(ad), &sz) && sz == sizeof(ad) && ad)
276 return memory_get_string(pcs, ad, TRUE, unicode, buffer, size);
281 static void print_typed_basic(const struct dbg_lvalue* lvalue)
283 long long int val_int;
285 long double val_real;
286 DWORD tag, size, count, bt;
287 struct dbg_type rtype;
289 if (lvalue->type.id == dbg_itype_none ||
290 !types_get_info(&lvalue->type, TI_GET_SYMTAG, &tag))
296 if (!types_get_info(&lvalue->type, TI_GET_LENGTH, &size) ||
297 !types_get_info(&lvalue->type, TI_GET_BASETYPE, &bt))
299 WINE_ERR("Couldn't get information\n");
300 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
306 if (!be_cpu->fetch_integer(lvalue, size, TRUE, &val_int)) return;
307 dbg_printf("%lld", val_int);
310 if (!be_cpu->fetch_integer(lvalue, size, FALSE, &val_int)) return;
311 dbg_printf("%llu", val_int);
314 if (!be_cpu->fetch_float(lvalue, size, &val_real)) return;
315 dbg_printf("%Lf", val_real);
318 if (!be_cpu->fetch_integer(lvalue, size, TRUE, &val_int)) return;
319 /* FIXME: should do the same for a Unicode character (size == 2) */
320 if (size == 1 && (val_int < 0x20 || val_int > 0x80))
321 dbg_printf("%d", (int)val_int);
323 dbg_printf("'%c'", (char)val_int);
326 WINE_FIXME("Unsupported basetype %lu\n", bt);
330 case SymTagPointerType:
331 if (!memory_read_value(lvalue, sizeof(void*), &val_ptr)) return;
333 if (!types_get_info(&lvalue->type, TI_GET_TYPE, &rtype.id) ||
334 rtype.id == dbg_itype_none)
336 dbg_printf("Internal symbol error: unable to access memory location %p", val_ptr);
339 rtype.module = lvalue->type.module;
340 if (types_get_info(&rtype, TI_GET_SYMTAG, &tag) && tag == SymTagBaseType &&
341 types_get_info(&rtype, TI_GET_BASETYPE, &bt) && bt == btChar &&
342 types_get_info(&rtype, TI_GET_LENGTH, &size))
346 memory_get_string(dbg_curr_process, val_ptr,
347 lvalue->cookie == DLV_TARGET,
348 size == 2, buffer, sizeof(buffer));
349 dbg_printf("\"%s\"", buffer);
351 else dbg_printf("%p", val_ptr);
353 case SymTagArrayType:
355 assert(lvalue->cookie == DLV_TARGET);
356 if (!memory_read_value(lvalue, sizeof(val_ptr), &val_ptr)) return;
357 dbg_printf("%p", val_ptr);
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(&lvalue->type, TI_GET_CHILDRENCOUNT, &count))
372 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
373 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
378 struct dbg_type type;
383 fcp->Count = min(count, 256);
384 if (types_get_info(&lvalue->type, TI_FINDCHILDREN, fcp))
386 type.module = lvalue->type.module;
387 for (i = 0; i < min(fcp->Count, count); i++)
389 type.id = fcp->ChildId[i];
390 if (!types_get_info(&type, TI_GET_VALUE, &variant))
392 switch (variant.n1.n2.vt)
394 case VT_I4: ok = (val_int == variant.n1.n2.n3.lVal); break;
395 default: WINE_FIXME("Unsupported variant type (%u)\n", variant.n1.n2.vt);
400 types_get_info(&type, TI_GET_SYMNAME, &ptr);
402 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
403 HeapFree(GetProcessHeap(), 0, ptr);
404 dbg_printf("%s", tmp);
405 count = 0; /* so that we'll get away from outter loop */
411 count -= min(count, 256);
414 if (!ok) dbg_printf("%lld", val_int);
418 WINE_FIXME("Unsupported tag %lu\n", tag);
423 /***********************************************************************
426 * Implementation of the 'print' command.
428 void print_basic(const struct dbg_lvalue* lvalue, int count, char format)
432 if (lvalue->type.id == dbg_itype_none)
434 dbg_printf("Unable to evaluate expression\n");
438 res = types_extract_as_integer(lvalue);
440 /* FIXME: this implies i386 byte ordering */
444 if (lvalue->addr.Mode != AddrModeFlat)
445 dbg_printf("0x%04lx", res);
447 dbg_printf("0x%08lx", res);
451 dbg_printf("%ld\n", res);
455 dbg_printf("%d = '%c'", (char)(res & 0xff), (char)(res & 0xff));
460 WCHAR wch = (WCHAR)(res & 0xFFFF);
461 dbg_printf("%d = '", wch);
462 dbg_outputW(&wch, 1);
471 dbg_printf("Format specifier '%c' is meaningless in 'print' command\n", format);
473 print_typed_basic(lvalue);
478 void print_bare_address(const ADDRESS* addr)
483 dbg_printf("0x%08lx", addr->Offset);
487 dbg_printf("0x%04x:0x%04lx", addr->Segment, addr->Offset);
490 dbg_printf("0x%04x:0x%08lx", addr->Segment, addr->Offset);
493 dbg_printf("Unknown mode %x\n", addr->Mode);
498 /***********************************************************************
501 * Print an 16- or 32-bit address, with the nearest symbol if any.
503 void print_address(const ADDRESS* addr, BOOLEAN with_line)
505 char buffer[sizeof(SYMBOL_INFO) + 256];
506 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
507 void* lin = memory_to_linear_addr(addr);
511 print_bare_address(addr);
513 si->SizeOfStruct = sizeof(*si);
514 si->MaxNameLen = 256;
515 if (!SymFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp64, si)) return;
516 dbg_printf(" %s", si->Name);
517 if (disp64) dbg_printf("+0x%lx", (DWORD_PTR)disp64);
523 il.SizeOfStruct = sizeof(il);
524 if (SymGetLineFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp, &il))
525 dbg_printf(" [%s:%lu]", il.FileName, il.LineNumber);
526 im.SizeOfStruct = sizeof(im);
527 if (SymGetModuleInfo(dbg_curr_process->handle, (DWORD_PTR)lin, &im))
528 dbg_printf(" in %s", im.ModuleName);
538 static BOOL WINAPI sym_enum_cb(SYMBOL_INFO* sym_info, ULONG size, void* user)
540 struct sym_enum* se = (struct sym_enum*)user;
545 if ((sym_info->Flags & (SYMFLAG_PARAMETER|SYMFLAG_FRAMEREL)) == (SYMFLAG_PARAMETER|SYMFLAG_FRAMEREL))
547 struct dbg_type type;
549 if (se->tmp[0]) strcat(se->tmp, ", ");
551 type.module = sym_info->ModBase;
552 type.id = sym_info->TypeIndex;
553 types_get_info(&type, TI_GET_OFFSET, &offset);
555 if (dbg_read_memory((char*)addr, &val, sizeof(val)))
556 sprintf(se->tmp + strlen(se->tmp), "%s=0x%x", sym_info->Name, val);
558 sprintf(se->tmp + strlen(se->tmp), "%s=<\?\?\?>", sym_info->Name);
563 void print_addr_and_args(const ADDRESS* pc, const ADDRESS* frame)
565 char buffer[sizeof(SYMBOL_INFO) + 256];
566 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
567 IMAGEHLP_STACK_FRAME isf;
572 print_bare_address(pc);
574 isf.InstructionOffset = (DWORD_PTR)memory_to_linear_addr(pc);
575 isf.FrameOffset = (DWORD_PTR)memory_to_linear_addr(frame);
577 /* grab module where symbol is. If we don't have a module, we cannot print more */
578 im.SizeOfStruct = sizeof(im);
579 if (!SymGetModuleInfo(dbg_curr_process->handle, isf.InstructionOffset, &im))
582 si->SizeOfStruct = sizeof(*si);
583 si->MaxNameLen = 256;
584 if (SymFromAddr(dbg_curr_process->handle, isf.InstructionOffset, &disp64, si))
590 dbg_printf(" %s", si->Name);
591 if (disp) dbg_printf("+0x%lx", (DWORD_PTR)disp64);
593 SymSetContext(dbg_curr_process->handle, &isf, NULL);
595 se.frame = isf.FrameOffset;
597 SymEnumSymbols(dbg_curr_process->handle, 0, NULL, sym_enum_cb, &se);
598 if (tmp[0]) dbg_printf("(%s)", tmp);
600 il.SizeOfStruct = sizeof(il);
601 if (SymGetLineFromAddr(dbg_curr_process->handle, isf.InstructionOffset,
603 dbg_printf(" [%s:%lu]", il.FileName, il.LineNumber);
604 dbg_printf(" in %s", im.ModuleName);
606 else dbg_printf(" in %s (+0x%lx)",
607 im.ModuleName, (DWORD_PTR)(isf.InstructionOffset - im.BaseOfImage));
610 BOOL memory_disasm_one_insn(ADDRESS* addr)
614 print_address(addr, TRUE);
616 if (!dbg_read_memory(memory_to_linear_addr(addr), &ch, sizeof(ch)))
618 dbg_printf("-- no code accessible --\n");
621 be_cpu->disasm_one_insn(addr, TRUE);
626 void memory_disassemble(const struct dbg_lvalue* xstart,
627 const struct dbg_lvalue* xend, int instruction_count)
629 static ADDRESS last = {0,0,0};
633 if (!xstart && !xend)
635 if (!last.Segment && !last.Offset) memory_get_current_pc(&last);
641 last.Mode = AddrModeFlat;
642 last.Offset = types_extract_as_integer(xstart);
645 stop = types_extract_as_integer(xend);
647 for (i = 0; (instruction_count == 0 || i < instruction_count) &&
648 (stop == 0 || last.Offset <= stop); i++)
649 memory_disasm_one_insn(&last);