shlwapi/tests: Fix a test failure on NT4.
[wine/hacks.git] / programs / winedbg / memory.c
blob9554ea42861992844dcee4ab87d5c8db34309b66
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 ADDRESS64* addr)
37 assert(addr->Mode == AddrModeFlat);
38 return (void*)(DWORD_PTR)addr->Offset;
41 unsigned be_cpu_build_addr(HANDLE hThread, const CONTEXT* ctx, ADDRESS64* 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 ADDRESS64* addr)
52 return be_cpu->linearize(dbg_curr_thread->handle, addr);
55 BOOL memory_get_current_pc(ADDRESS64* 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(ADDRESS64* 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(ADDRESS64* 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 ADDRESS64 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*)(DWORD_PTR)lvalue->addr.Offset, size);
108 ret = TRUE;
111 return ret;
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*)(DWORD_PTR)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 ADDRESS64 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("{%08x-%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 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
222 case 'x': DO_DUMP(int, 4, " %8.8x"); break;
223 case 'd': DO_DUMP(unsigned int, 4, " %4.4d"); break;
224 case 'w': DO_DUMP(unsigned short, 8, " %04x"); break;
225 case 'a':
226 if (sizeof(DWORD_PTR) == 4)
228 DO_DUMP(DWORD_PTR, 4, " %8.8lx");
230 else
232 DO_DUMP(DWORD_PTR, 2, " %16.16lx");
234 break;
235 case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v); break;
236 case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff); break;
240 BOOL memory_get_string(struct dbg_process* pcs, void* addr, BOOL in_debuggee,
241 BOOL unicode, char* buffer, int size)
243 SIZE_T sz;
244 WCHAR* buffW;
246 buffer[0] = 0;
247 if (!addr) return FALSE;
248 if (in_debuggee)
250 BOOL ret;
252 if (!unicode) ret = pcs->process_io->read(pcs->handle, addr, buffer, size, &sz);
253 else
255 buffW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
256 ret = pcs->process_io->read(pcs->handle, addr, buffW, size * sizeof(WCHAR), &sz);
257 WideCharToMultiByte(CP_ACP, 0, buffW, sz / sizeof(WCHAR), buffer, size,
258 NULL, NULL);
259 HeapFree(GetProcessHeap(), 0, buffW);
261 if (size) buffer[size-1] = 0;
262 return ret;
264 else
266 lstrcpynA(buffer, addr, size);
268 return TRUE;
271 BOOL memory_get_string_indirect(struct dbg_process* pcs, void* addr, BOOL unicode, WCHAR* buffer, int size)
273 void* ad;
274 SIZE_T sz;
276 buffer[0] = 0;
277 if (addr &&
278 pcs->process_io->read(pcs->handle, addr, &ad, sizeof(ad), &sz) && sz == sizeof(ad) && ad)
280 LPSTR buff;
281 BOOL ret;
283 if (unicode)
284 ret = pcs->process_io->read(pcs->handle, ad, buffer, size * sizeof(WCHAR), &sz) && sz != 0;
285 else
287 if ((buff = HeapAlloc(GetProcessHeap(), 0, size)))
289 ret = pcs->process_io->read(pcs->handle, ad, buff, size, &sz) && sz != 0;
290 MultiByteToWideChar(CP_ACP, 0, buff, sz, buffer, size);
291 HeapFree(GetProcessHeap(), 0, buff);
293 else ret = FALSE;
295 if (size) buffer[size-1] = 0;
296 return ret;
298 return FALSE;
302 * Convert an address offset to hex string. If mode == 32, treat offset as
303 * 32 bits (discard upper 32 bits), if mode == 64 use all 64 bits, if mode == 0
304 * treat as either 32 or 64 bits, depending on whether we're running as
305 * Wine32 or Wine64.
307 char* memory_offset_to_string(char *str, DWORD64 offset, unsigned mode)
309 if (mode != 32 && mode != 64)
311 #ifdef _WIN64
312 mode = 64;
313 #else
314 mode = 32;
315 #endif
318 if (mode == 32)
319 sprintf(str, "0x%08x", (unsigned int) offset);
320 else
321 sprintf(str, "0x%08x%08x", (unsigned int)(offset >> 32),
322 (unsigned int)offset);
324 return str;
327 static void dbg_print_longlong(LONGLONG sv, BOOL is_signed)
329 char tmp[24], *ptr = tmp + sizeof(tmp) - 1;
330 ULONGLONG uv, div;
331 *ptr = '\0';
332 if (is_signed && sv < 0) uv = -sv;
333 else { uv = sv; is_signed = FALSE; }
334 for (div = 10; uv; div *= 10, uv /= 10)
335 *--ptr = '0' + (uv % 10);
336 if (ptr == tmp + sizeof(tmp) - 1) *--ptr = '0';
337 if (is_signed) *--ptr = '-';
338 dbg_printf("%s", ptr);
341 static void dbg_print_hex(ULONGLONG sv)
343 if (!sv)
345 dbg_printf("0");
346 return;
349 if (sv >> 32)
350 dbg_printf("0x%lx%08lx", (unsigned long)(sv >> 32), (unsigned long)sv);
351 else
352 dbg_printf("0x%04lx", (unsigned long)sv);
355 static void print_typed_basic(const struct dbg_lvalue* lvalue)
357 LONGLONG val_int;
358 void* val_ptr;
359 long double val_real;
360 DWORD64 size64;
361 DWORD tag, size, count, bt;
362 struct dbg_type type = lvalue->type;
363 struct dbg_type sub_type;
364 struct dbg_lvalue sub_lvalue;
366 if (!types_get_real_type(&type, &tag)) return;
368 switch (tag)
370 case SymTagBaseType:
371 if (!types_get_info(&type, TI_GET_LENGTH, &size64) ||
372 !types_get_info(&type, TI_GET_BASETYPE, &bt))
374 WINE_ERR("Couldn't get information\n");
375 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
377 size = (DWORD)size64;
378 switch (bt)
380 case btInt:
381 case btLong:
382 if (!be_cpu->fetch_integer(lvalue, size, TRUE, &val_int)) return;
383 if (size == 1) goto print_char;
384 dbg_print_hex(val_int);
385 break;
386 case btUInt:
387 case btULong:
388 if (!be_cpu->fetch_integer(lvalue, size, FALSE, &val_int)) return;
389 dbg_print_hex(val_int);
390 break;
391 case btFloat:
392 if (!be_cpu->fetch_float(lvalue, size, &val_real)) return;
393 dbg_printf("%Lf", val_real);
394 break;
395 case btChar:
396 if (!be_cpu->fetch_integer(lvalue, size, TRUE, &val_int)) return;
397 /* FIXME: should do the same for a Unicode character (size == 2) */
398 print_char:
399 if (size == 1 && (val_int < 0x20 || val_int > 0x80))
400 dbg_printf("%d", (int)val_int);
401 else if (size == 2)
403 WCHAR wch = (WCHAR)val_int;
404 dbg_outputW(&wch, 1);
406 else
407 dbg_printf("'%c'", (char)val_int);
408 break;
409 case btBool:
410 if (!be_cpu->fetch_integer(lvalue, size, TRUE, &val_int)) return;
411 dbg_printf("%s", val_int ? "true" : "false");
412 break;
413 default:
414 WINE_FIXME("Unsupported basetype %u\n", bt);
415 break;
417 break;
418 case SymTagPointerType:
419 if (!types_deref(lvalue, &sub_lvalue))
421 dbg_printf("Internal symbol error: unable to access memory location %p",
422 memory_to_linear_addr(&lvalue->addr));
423 break;
425 val_ptr = memory_to_linear_addr(&sub_lvalue.addr);
426 if (types_get_real_type(&sub_lvalue.type, &tag) && tag == SymTagBaseType &&
427 types_get_info(&sub_lvalue.type, TI_GET_BASETYPE, &bt) &&
428 types_get_info(&sub_lvalue.type, TI_GET_LENGTH, &size64))
430 char buffer[1024];
432 if (!val_ptr) dbg_printf("0x0");
433 else if (((bt == btChar || bt == btInt) && size64 == 1) || (bt == btUInt && size64 == 2))
435 if (memory_get_string(dbg_curr_process, val_ptr, sub_lvalue.cookie == DLV_TARGET,
436 size64 == 2, buffer, sizeof(buffer)))
437 dbg_printf("\"%s\"", buffer);
438 else
439 dbg_printf("*** invalid address %p ***", val_ptr);
440 break;
443 dbg_printf("%p", val_ptr);
444 break;
445 case SymTagArrayType:
446 case SymTagUDT:
447 if (!memory_read_value(lvalue, sizeof(val_ptr), &val_ptr)) return;
448 dbg_printf("%p", val_ptr);
449 break;
450 case SymTagEnum:
452 BOOL ok = FALSE;
454 /* FIXME: it depends on underlying type for enums
455 * (not supported yet in dbghelp)
456 * Assuming 4 as for an int
458 if (!be_cpu->fetch_integer(lvalue, 4, TRUE, &val_int)) return;
460 if (types_get_info(&type, TI_GET_CHILDRENCOUNT, &count))
462 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
463 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
464 WCHAR* ptr;
465 char tmp[256];
466 VARIANT variant;
467 int i;
469 fcp->Start = 0;
470 while (count)
472 fcp->Count = min(count, 256);
473 if (types_get_info(&type, TI_FINDCHILDREN, fcp))
475 sub_type.module = type.module;
476 for (i = 0; i < min(fcp->Count, count); i++)
478 sub_type.id = fcp->ChildId[i];
479 if (!types_get_info(&sub_type, TI_GET_VALUE, &variant))
480 continue;
481 switch (variant.n1.n2.vt)
483 case VT_I4: ok = (val_int == variant.n1.n2.n3.lVal); break;
484 default: WINE_FIXME("Unsupported variant type (%u)\n", variant.n1.n2.vt);
486 if (ok)
488 ptr = NULL;
489 types_get_info(&sub_type, TI_GET_SYMNAME, &ptr);
490 if (!ptr) continue;
491 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
492 HeapFree(GetProcessHeap(), 0, ptr);
493 dbg_printf("%s", tmp);
494 count = 0; /* so that we'll get away from outter loop */
495 break;
499 count -= min(count, 256);
500 fcp->Start += 256;
503 if (!ok) dbg_print_longlong(val_int, TRUE);
505 break;
506 default:
507 WINE_FIXME("Unsupported tag %u\n", tag);
508 break;
512 /***********************************************************************
513 * print_basic
515 * Implementation of the 'print' command.
517 void print_basic(const struct dbg_lvalue* lvalue, char format)
519 if (lvalue->type.id == dbg_itype_none)
521 dbg_printf("Unable to evaluate expression\n");
522 return;
525 if (format != 0)
527 unsigned size;
528 LONGLONG res = types_extract_as_longlong(lvalue, &size);
529 DWORD hi;
530 WCHAR wch;
532 /* FIXME: this implies i386 byte ordering */
533 switch (format)
535 case 'x':
536 hi = (ULONG64)res >> 32;
537 if (size == 8 && hi)
538 dbg_printf("0x%x%08x", hi, (DWORD)res);
539 else
540 dbg_printf("0x%x", (DWORD)res);
541 return;
543 case 'd':
544 dbg_print_longlong(res, TRUE);
545 dbg_printf("\n");
546 return;
548 case 'c':
549 dbg_printf("%d = '%c'", (char)(res & 0xff), (char)(res & 0xff));
550 return;
552 case 'u':
553 wch = (WCHAR)(res & 0xFFFF);
554 dbg_printf("%d = '", wch);
555 dbg_outputW(&wch, 1);
556 dbg_printf("'");
557 return;
559 case 'i':
560 case 's':
561 case 'w':
562 case 'b':
563 dbg_printf("Format specifier '%c' is meaningless in 'print' command\n", format);
566 if (lvalue->type.id == dbg_itype_segptr)
568 dbg_print_longlong(types_extract_as_longlong(lvalue, NULL), TRUE);
569 dbg_printf("\n");
571 else print_typed_basic(lvalue);
574 void print_bare_address(const ADDRESS64* addr)
576 char hexbuf[MAX_OFFSET_TO_STR_LEN];
578 switch (addr->Mode)
580 case AddrModeFlat:
581 dbg_printf("%s", memory_offset_to_string(hexbuf, addr->Offset, 0));
582 break;
583 case AddrModeReal:
584 case AddrMode1616:
585 dbg_printf("0x%04x:0x%04x", addr->Segment, (unsigned) addr->Offset);
586 break;
587 case AddrMode1632:
588 dbg_printf("0x%04x:%s", addr->Segment,
589 memory_offset_to_string(hexbuf, addr->Offset, 32));
590 break;
591 default:
592 dbg_printf("Unknown mode %x\n", addr->Mode);
593 break;
597 /***********************************************************************
598 * print_address
600 * Print an 16- or 32-bit address, with the nearest symbol if any.
602 void print_address(const ADDRESS64* addr, BOOLEAN with_line)
604 char buffer[sizeof(SYMBOL_INFO) + 256];
605 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
606 void* lin = memory_to_linear_addr(addr);
607 DWORD64 disp64;
608 DWORD disp;
610 print_bare_address(addr);
612 si->SizeOfStruct = sizeof(*si);
613 si->MaxNameLen = 256;
614 if (!SymFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp64, si)) return;
615 dbg_printf(" %s", si->Name);
616 if (disp64) dbg_printf("+0x%lx", (DWORD_PTR)disp64);
617 if (with_line)
619 IMAGEHLP_LINE64 il;
620 IMAGEHLP_MODULE im;
622 il.SizeOfStruct = sizeof(il);
623 if (SymGetLineFromAddr64(dbg_curr_process->handle, (DWORD_PTR)lin, &disp, &il))
624 dbg_printf(" [%s:%u]", il.FileName, il.LineNumber);
625 im.SizeOfStruct = sizeof(im);
626 if (SymGetModuleInfo(dbg_curr_process->handle, (DWORD_PTR)lin, &im))
627 dbg_printf(" in %s", im.ModuleName);
631 BOOL memory_disasm_one_insn(ADDRESS64* addr)
633 char ch;
635 print_address(addr, TRUE);
636 dbg_printf(": ");
637 if (!dbg_read_memory(memory_to_linear_addr(addr), &ch, sizeof(ch)))
639 dbg_printf("-- no code accessible --\n");
640 return FALSE;
642 be_cpu->disasm_one_insn(addr, TRUE);
643 dbg_printf("\n");
644 return TRUE;
647 void memory_disassemble(const struct dbg_lvalue* xstart,
648 const struct dbg_lvalue* xend, int instruction_count)
650 static ADDRESS64 last = {0,0,0};
651 int stop = 0;
652 int i;
654 if (!xstart && !xend)
656 if (!last.Segment && !last.Offset) memory_get_current_pc(&last);
658 else
660 if (xstart)
661 types_extract_as_address(xstart, &last);
662 if (xend)
663 stop = types_extract_as_integer(xend);
665 for (i = 0; (instruction_count == 0 || i < instruction_count) &&
666 (stop == 0 || last.Offset <= stop); i++)
667 memory_disasm_one_insn(&last);
670 BOOL memory_get_register(DWORD regno, DWORD_PTR** value, char* buffer, int len)
672 const struct dbg_internal_var* div;
674 /* negative register values are wine's dbghelp hacks
675 * see dlls/dbghelp/dbghelp_internal.h for the details
677 switch (regno)
679 case -1:
680 if (buffer) snprintf(buffer, len, "<internal error>");
681 return FALSE;
682 case -2:
683 if (buffer) snprintf(buffer, len, "<couldn't compute location>");
684 return FALSE;
685 case -3:
686 if (buffer) snprintf(buffer, len, "<is not available>");
687 return FALSE;
688 case -4:
689 if (buffer) snprintf(buffer, len, "<couldn't read memory>");
690 return FALSE;
693 for (div = dbg_context_vars; div->name; div++)
695 if (div->val == regno)
697 if (dbg_curr_thread->curr_frame != 0)
699 if (!stack_get_register_current_frame(regno, value))
701 if (buffer) snprintf(buffer, len, "<register %s not in topmost frame>", div->name);
702 return FALSE;
705 else
706 *value = div->pval;
708 if (buffer) lstrcpynA(buffer, div->name, len);
709 return TRUE;
712 if (buffer) snprintf(buffer, len, "<unknown register %u>", regno);
713 return FALSE;