wineoss: Fix missing break statement.
[wine.git] / programs / winedbg / memory.c
blobaa5c32b42f26d8b5e5796e64738dc37e7ce83607
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 <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
27 #include "debugger.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
32 void* be_cpu_linearize(HANDLE hThread, const ADDRESS64* addr)
34 assert(addr->Mode == AddrModeFlat);
35 return (void*)(DWORD_PTR)addr->Offset;
38 BOOL be_cpu_build_addr(HANDLE hThread, const dbg_ctx_t *ctx, ADDRESS64* addr,
39 unsigned seg, DWORD64 offset)
41 addr->Mode = AddrModeFlat;
42 addr->Segment = 0; /* don't need segment */
43 addr->Offset = offset;
44 return TRUE;
47 void* memory_to_linear_addr(const ADDRESS64* addr)
49 return dbg_curr_process->be_cpu->linearize(dbg_curr_thread->handle, addr);
52 BOOL memory_get_current_pc(ADDRESS64* addr)
54 assert(dbg_curr_process->be_cpu->get_addr);
55 return dbg_curr_process->be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
56 be_cpu_addr_pc, addr);
59 BOOL memory_get_current_stack(ADDRESS64* addr)
61 assert(dbg_curr_process->be_cpu->get_addr);
62 return dbg_curr_process->be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
63 be_cpu_addr_stack, addr);
66 static void memory_report_invalid_addr(const void* addr)
68 ADDRESS64 address;
70 address.Mode = AddrModeFlat;
71 address.Segment = 0;
72 address.Offset = (ULONG_PTR)addr;
73 dbg_printf("*** Invalid address ");
74 print_address(&address, FALSE);
75 dbg_printf(" ***\n");
78 /***********************************************************************
79 * memory_read_value
81 * Read a memory value.
83 BOOL memory_read_value(const struct dbg_lvalue* lvalue, DWORD size, void* result)
85 BOOL ret = FALSE;
87 if (lvalue->in_debuggee)
89 void* linear = memory_to_linear_addr(&lvalue->addr);
90 if (!(ret = dbg_read_memory(linear, result, size)))
91 memory_report_invalid_addr(linear);
93 else
95 if (lvalue->addr.Offset)
97 memcpy(result, (void*)(DWORD_PTR)lvalue->addr.Offset, size);
98 ret = TRUE;
101 return ret;
104 /***********************************************************************
105 * memory_write_value
107 * Store a value in memory.
109 BOOL memory_write_value(const struct dbg_lvalue* lvalue, DWORD size, void* value)
111 BOOL ret = TRUE;
112 DWORD64 os;
114 if (!types_get_info(&lvalue->type, TI_GET_LENGTH, &os)) return FALSE;
115 if (size != os)
117 dbg_printf("Size mismatch in memory_write_value, got %I64u from type while expecting %lu\n",
118 os, size);
119 return FALSE;
122 /* FIXME: only works on little endian systems */
123 if (lvalue->in_debuggee)
125 void* linear = memory_to_linear_addr(&lvalue->addr);
126 if (!(ret = dbg_write_memory(linear, value, size)))
127 memory_report_invalid_addr(linear);
129 else
131 memcpy((void*)(DWORD_PTR)lvalue->addr.Offset, value, size);
133 return ret;
136 /* transfer a block of memory
137 * the two lvalue:s are expected to be of same size
139 BOOL memory_transfer_value(const struct dbg_lvalue* to, const struct dbg_lvalue* from)
141 DWORD64 size_to, size_from;
142 BYTE tmp[256];
143 BYTE* ptr = tmp;
144 BOOL ret;
146 if (to->bitlen || from->bitlen) return FALSE;
147 if (!types_get_info(&to->type, TI_GET_LENGTH, &size_to) ||
148 !types_get_info(&from->type, TI_GET_LENGTH, &size_from) ||
149 size_from != size_to) return FALSE;
150 /* optimize debugger to debugger transfer */
151 if (!to->in_debuggee && !from->in_debuggee)
153 memcpy(memory_to_linear_addr(&to->addr), memory_to_linear_addr(&from->addr), size_from);
154 return TRUE;
156 if (size_to > sizeof(tmp))
158 ptr = malloc(size_from);
159 if (!ptr) return FALSE;
161 ret = memory_read_value(from, size_from, ptr) &&
162 memory_write_value(to, size_from, ptr);
163 if (size_to > sizeof(tmp)) free(ptr);
164 return ret;
167 /***********************************************************************
168 * memory_examine
170 * Implementation of the 'x' command.
172 void memory_examine(const struct dbg_lvalue *lvalue, int count, char format)
174 int i;
175 char buffer[256];
176 ADDRESS64 addr;
177 void *linear;
179 types_extract_as_address(lvalue, &addr);
180 linear = memory_to_linear_addr(&addr);
182 if (format != 'i' && count > 1)
184 print_address(&addr, FALSE);
185 dbg_printf(": ");
188 switch (format)
190 case 'u':
191 if (count == 1) count = 256;
192 memory_get_string(dbg_curr_process, linear,
193 TRUE, TRUE, buffer, min(count, sizeof(buffer)));
194 dbg_printf("%s\n", buffer);
195 return;
196 case 's':
197 if (count == 1) count = 256;
198 memory_get_string(dbg_curr_process, linear,
199 TRUE, FALSE, buffer, min(count, sizeof(buffer)));
200 dbg_printf("%s\n", buffer);
201 return;
202 case 'i':
203 while (count-- && memory_disasm_one_insn(&addr));
204 return;
205 case 'g':
206 while (count--)
208 GUID guid;
209 if (!dbg_read_memory(linear, &guid, sizeof(guid)))
211 memory_report_invalid_addr(linear);
212 break;
214 dbg_printf("{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
215 guid.Data1, guid.Data2, guid.Data3,
216 guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
217 guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
218 linear = (char*)linear + sizeof(guid);
219 addr.Offset += sizeof(guid);
220 if (count)
222 print_address(&addr, FALSE);
223 dbg_printf(": ");
226 return;
228 #define DO_DUMP2(_t,_l,_f,_vv) { \
229 _t _v; \
230 for (i = 0; i < count; i++) { \
231 if (!dbg_read_memory(linear, &_v, sizeof(_t))) \
232 { memory_report_invalid_addr(linear); break; } \
233 dbg_printf(_f, (_vv)); \
234 addr.Offset += sizeof(_t); \
235 linear = (char*)linear + sizeof(_t); \
236 if ((i % (_l)) == (_l) - 1 && i != count - 1) \
238 dbg_printf("\n"); \
239 print_address(&addr, FALSE); \
240 dbg_printf(": "); \
243 dbg_printf("\n"); \
245 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
247 case 'x': DO_DUMP(int, 4, " %8.8x"); break;
248 case 'd': DO_DUMP(unsigned int, 4, " %4.4d"); break;
249 case 'w': DO_DUMP(unsigned short, 8, " %04x"); break;
250 case 'a':
251 if (sizeof(DWORD_PTR) == 4)
253 DO_DUMP(DWORD_PTR, 4, " %8.8Ix");
255 else
257 DO_DUMP(DWORD_PTR, 2, " %16.16Ix");
259 break;
260 case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v); break;
261 case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff); break;
265 BOOL memory_fetch_integer(const struct dbg_lvalue* lvalue, unsigned size,
266 BOOL is_signed, dbg_lgint_t* ret)
268 /* size must fit in ret and be a power of two */
269 if (size > sizeof(*ret) || (size & (size - 1))) return FALSE;
271 if (lvalue->bitlen)
273 struct dbg_lvalue alt_lvalue = *lvalue;
274 dbg_lguint_t mask;
275 DWORD bt;
276 /* FIXME: this test isn't sufficient, depending on start of bitfield
277 * (ie a 64 bit field can spread across 9 bytes)
279 if (lvalue->bitlen > 8 * sizeof(dbg_lgint_t)) return FALSE;
280 alt_lvalue.addr.Offset += lvalue->bitstart >> 3;
282 * Bitfield operation. We have to extract the field.
284 if (!memory_read_value(&alt_lvalue, sizeof(*ret), ret)) return FALSE;
285 mask = ~(dbg_lguint_t)0 << lvalue->bitlen;
286 *ret >>= lvalue->bitstart & 7;
287 *ret &= ~mask;
290 * OK, now we have the correct part of the number.
291 * Check to see whether the basic type is signed or not, and if so,
292 * we need to sign extend the number.
294 if (types_get_info(&lvalue->type, TI_GET_BASETYPE, &bt) &&
295 (bt == btInt || bt == btLong) && (*ret & (1 << (lvalue->bitlen - 1))))
297 *ret |= mask;
300 else
302 /* we are on little endian CPU */
303 memset(ret, 0, sizeof(*ret)); /* clear unread bytes */
304 if (!memory_read_value(lvalue, size, ret)) return FALSE;
306 /* propagate sign information */
307 if (is_signed && size < 8 && (*ret >> (size * 8 - 1)) != 0)
309 dbg_lguint_t neg = -1;
310 *ret |= neg << (size * 8);
313 return TRUE;
316 BOOL memory_store_integer(const struct dbg_lvalue* lvalue, dbg_lgint_t val)
318 DWORD64 size;
319 if (!types_get_info(&lvalue->type, TI_GET_LENGTH, &size)) return FALSE;
320 if (lvalue->bitlen)
322 struct dbg_lvalue alt_lvalue = *lvalue;
323 dbg_lguint_t mask, dst;
325 /* FIXME: this test isn't sufficient, depending on start of bitfield
326 * (ie a 64 bit field can spread across 9 bytes)
328 if (lvalue->bitlen > 8 * sizeof(dbg_lgint_t)) return FALSE;
329 /* mask is 1 where bitfield is present, 0 outside */
330 mask = (~(dbg_lguint_t)0 >> (sizeof(val) * 8 - lvalue->bitlen)) << (lvalue->bitstart & 7);
331 alt_lvalue.addr.Offset += lvalue->bitstart >> 3;
332 val <<= lvalue->bitstart & 7;
333 if (!memory_read_value(&alt_lvalue, (unsigned)size, &dst)) return FALSE;
334 val = (dst & ~mask) | (val & mask);
335 return memory_write_value(&alt_lvalue, (unsigned)size, &val);
337 /* this is simple if we're on a little endian CPU */
338 return memory_write_value(lvalue, (unsigned)size, &val);
341 BOOL memory_fetch_float(const struct dbg_lvalue* lvalue, double *ret)
343 DWORD64 size;
344 if (!types_get_info(&lvalue->type, TI_GET_LENGTH, &size)) return FALSE;
345 /* FIXME: this assumes that debuggee and debugger use the same
346 * representation for reals
348 if (size > sizeof(*ret)) return FALSE;
349 if (!memory_read_value(lvalue, size, ret)) return FALSE;
351 if (size == sizeof(float)) *ret = *(float*)ret;
352 else if (size != sizeof(double)) return FALSE;
354 return TRUE;
357 BOOL memory_store_float(const struct dbg_lvalue* lvalue, double *ret)
359 DWORD64 size;
360 if (!types_get_info(&lvalue->type, TI_GET_LENGTH, &size)) return FALSE;
361 /* FIXME: this assumes that debuggee and debugger use the same
362 * representation for reals
364 if (size > sizeof(*ret)) return FALSE;
365 if (size == sizeof(float))
367 float f = *ret;
368 return memory_write_value(lvalue, size, &f);
370 if (size != sizeof(double)) return FALSE;
371 return memory_write_value(lvalue, size, ret);
374 BOOL memory_get_string(struct dbg_process* pcs, void* addr, BOOL in_debuggee,
375 BOOL unicode, char* buffer, int size)
377 SIZE_T sz;
378 WCHAR* buffW;
380 buffer[0] = 0;
381 if (!addr) return FALSE;
382 if (in_debuggee)
384 BOOL ret;
386 if (!unicode) ret = pcs->process_io->read(pcs->handle, addr, buffer, size, &sz);
387 else
389 buffW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
390 ret = pcs->process_io->read(pcs->handle, addr, buffW, size * sizeof(WCHAR), &sz);
391 WideCharToMultiByte(CP_ACP, 0, buffW, sz / sizeof(WCHAR), buffer, size,
392 NULL, NULL);
393 HeapFree(GetProcessHeap(), 0, buffW);
395 if (size) buffer[size-1] = 0;
396 return ret;
398 else
400 lstrcpynA(buffer, addr, size);
402 return TRUE;
405 BOOL memory_get_string_indirect(struct dbg_process* pcs, void* addr, BOOL unicode, WCHAR* buffer, int size)
407 void* ad = 0;
408 SIZE_T sz;
410 buffer[0] = 0;
411 if (addr &&
412 pcs->process_io->read(pcs->handle, addr, &ad, pcs->be_cpu->pointer_size, &sz) && sz == pcs->be_cpu->pointer_size && ad)
414 LPSTR buff;
415 BOOL ret;
417 if (unicode)
418 ret = pcs->process_io->read(pcs->handle, ad, buffer, size * sizeof(WCHAR), &sz) && sz != 0;
419 else
421 if ((buff = HeapAlloc(GetProcessHeap(), 0, size)))
423 ret = pcs->process_io->read(pcs->handle, ad, buff, size, &sz) && sz != 0;
424 MultiByteToWideChar(CP_ACP, 0, buff, sz, buffer, size);
425 HeapFree(GetProcessHeap(), 0, buff);
427 else ret = FALSE;
429 if (size) buffer[size-1] = 0;
430 return ret;
432 return FALSE;
436 * Convert an address offset to hex string. If mode == 32, treat offset as
437 * 32 bits (discard upper 32 bits), if mode == 64 use all 64 bits, if mode == 0
438 * treat as either 32 or 64 bits, depending on whether we're running as
439 * Wine32 or Wine64.
441 char* memory_offset_to_string(char *str, DWORD64 offset, unsigned mode)
443 if (mode != 32 && mode != 64)
445 #ifdef _WIN64
446 mode = 64;
447 #else
448 mode = 32;
449 #endif
452 if (mode == 32)
453 sprintf(str, "0x%08x", (unsigned int) offset);
454 else
455 sprintf(str, "%#016I64x", offset);
457 return str;
460 static void dbg_print_sdecimal(dbg_lgint_t sv)
462 dbg_printf("%I64d", sv);
465 static void dbg_print_hex(DWORD size, dbg_lgint_t sv)
467 if (!sv)
468 dbg_printf("0");
469 else
470 /* clear unneeded high bits, esp. sign extension */
471 dbg_printf("%#I64x", sv & (~(dbg_lguint_t)0 >> (8 * (sizeof(dbg_lgint_t) - size))));
474 static void print_typed_basic(const struct dbg_lvalue* lvalue)
476 dbg_lgint_t val_int;
477 void* val_ptr;
478 double val_real;
479 DWORD64 size64;
480 DWORD tag, size, count, bt;
481 struct dbg_type type = lvalue->type;
482 struct dbg_type sub_type;
483 struct dbg_lvalue sub_lvalue;
485 if (!types_get_real_type(&type, &tag)) return;
487 switch (tag)
489 case SymTagBaseType:
490 if (!types_get_info(&type, TI_GET_LENGTH, &size64) ||
491 !types_get_info(&type, TI_GET_BASETYPE, &bt))
493 WINE_ERR("Couldn't get information\n");
494 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
495 return;
497 size = (DWORD)size64;
498 switch (bt)
500 case btInt:
501 case btLong:
502 if (!memory_fetch_integer(lvalue, size, TRUE, &val_int)) return;
503 dbg_print_hex(size, val_int);
504 break;
505 case btUInt:
506 case btULong:
507 if (!memory_fetch_integer(lvalue, size, FALSE, &val_int)) return;
508 dbg_print_hex(size, val_int);
509 break;
510 case btFloat:
511 if (!memory_fetch_float(lvalue, &val_real)) return;
512 dbg_printf("%f", val_real);
513 break;
514 case btChar:
515 if (!memory_fetch_integer(lvalue, size, TRUE, &val_int)) return;
516 if (size == 1 && isprint((char)val_int))
517 dbg_printf("'%c'", (char)val_int);
518 else
519 dbg_print_hex(size, val_int);
520 break;
521 case btWChar:
522 if (!memory_fetch_integer(lvalue, size, TRUE, &val_int)) return;
523 if (size == 2 && iswprint((WCHAR)val_int))
524 dbg_printf("L'%lc'", (WCHAR)val_int);
525 else
526 dbg_print_hex(size, val_int);
527 break;
528 case btBool:
529 if (!memory_fetch_integer(lvalue, size, TRUE, &val_int)) return;
530 dbg_printf("%s", val_int ? "true" : "false");
531 break;
532 default:
533 WINE_FIXME("Unsupported basetype %lu\n", bt);
534 break;
536 break;
537 case SymTagPointerType:
538 if (!types_array_index(lvalue, 0, &sub_lvalue))
540 dbg_printf("Internal symbol error: unable to access memory location %p",
541 memory_to_linear_addr(&lvalue->addr));
542 break;
544 val_ptr = memory_to_linear_addr(&sub_lvalue.addr);
545 if (types_get_real_type(&sub_lvalue.type, &tag) && tag == SymTagBaseType &&
546 types_get_info(&sub_lvalue.type, TI_GET_BASETYPE, &bt) &&
547 types_get_info(&sub_lvalue.type, TI_GET_LENGTH, &size64))
549 char buffer[1024];
551 if (!val_ptr) dbg_printf("0x0");
552 else if ((bt == btChar && size64 == 1) || (bt == btWChar && size64 == 2))
554 if (memory_get_string(dbg_curr_process, val_ptr, sub_lvalue.in_debuggee,
555 bt == btWChar, buffer, sizeof(buffer)))
556 dbg_printf("%s\"%s\"", bt == btWChar ? "L" : "", buffer);
557 else
558 dbg_printf("*** invalid address %p ***", val_ptr);
559 break;
562 dbg_printf("%p", val_ptr);
563 break;
564 case SymTagArrayType:
565 case SymTagUDT:
566 if (!memory_read_value(lvalue, sizeof(val_ptr), &val_ptr)) return;
567 dbg_printf("%p", val_ptr);
568 break;
569 case SymTagEnum:
571 BOOL ok = FALSE;
573 if (!types_get_info(&type, TI_GET_LENGTH, &size64) ||
574 !memory_fetch_integer(lvalue, size64, TRUE, &val_int)) return;
576 if (types_get_info(&type, TI_GET_CHILDRENCOUNT, &count))
578 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
579 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
580 WCHAR* ptr;
581 VARIANT variant;
582 int i;
584 fcp->Start = 0;
585 while (count)
587 fcp->Count = min(count, 256);
588 if (types_get_info(&type, TI_FINDCHILDREN, fcp))
590 sub_type.module = type.module;
591 for (i = 0; i < min(fcp->Count, count); i++)
593 sub_type.id = fcp->ChildId[i];
594 if (!types_get_info(&sub_type, TI_GET_VALUE, &variant))
595 continue;
596 switch (V_VT(&variant))
598 case VT_I1: ok = (val_int == V_I1(&variant)); break;
599 case VT_I2: ok = (val_int == V_I2(&variant)); break;
600 case VT_I4: ok = (val_int == V_I4(&variant)); break;
601 case VT_I8: ok = (val_int == V_I8(&variant)); break;
602 default: WINE_FIXME("Unsupported variant type (%u)\n", V_VT(&variant));
604 if (ok && types_get_info(&sub_type, TI_GET_SYMNAME, &ptr) && ptr)
606 dbg_printf("%ls", ptr);
607 HeapFree(GetProcessHeap(), 0, ptr);
608 count = 0; /* so that we'll get away from outer loop */
609 break;
613 count -= min(count, 256);
614 fcp->Start += 256;
617 if (!ok) dbg_print_sdecimal(val_int);
619 break;
620 default:
621 WINE_FIXME("Unsupported tag %lu\n", tag);
622 break;
626 /***********************************************************************
627 * print_basic
629 * Implementation of the 'print' command.
631 void print_basic(const struct dbg_lvalue* lvalue, char format)
633 if (lvalue->type.id == dbg_itype_none)
635 dbg_printf("Unable to evaluate expression\n");
636 return;
639 if (format != 0)
641 unsigned size;
642 dbg_lgint_t res = types_extract_as_lgint(lvalue, &size, NULL);
644 switch (format)
646 case 'x':
647 dbg_print_hex(size, res);
648 return;
650 case 'd':
651 dbg_print_sdecimal(res);
652 return;
654 case 'c':
655 dbg_printf("%d = '%c'", (char)(res & 0xff), (char)(res & 0xff));
656 return;
658 case 'u':
659 dbg_printf("%d = '%lc'", (WCHAR)(res & 0xFFFF), (WCHAR)(res & 0xFFFF));
660 return;
662 case 'i':
663 case 's':
664 case 'w':
665 case 'b':
666 dbg_printf("Format specifier '%c' is meaningless in 'print' command\n", format);
669 if (lvalue->type.id == dbg_itype_segptr)
671 dbg_print_sdecimal(types_extract_as_lgint(lvalue, NULL, NULL));
673 else print_typed_basic(lvalue);
676 void print_bare_address(const ADDRESS64* addr)
678 char hexbuf[MAX_OFFSET_TO_STR_LEN];
680 switch (addr->Mode)
682 case AddrModeFlat:
683 dbg_printf("%s", memory_offset_to_string(hexbuf, addr->Offset, 0));
684 break;
685 case AddrModeReal:
686 case AddrMode1616:
687 dbg_printf("0x%04x:0x%04x", addr->Segment, (unsigned) addr->Offset);
688 break;
689 case AddrMode1632:
690 dbg_printf("0x%04x:%s", addr->Segment,
691 memory_offset_to_string(hexbuf, addr->Offset, 32));
692 break;
693 default:
694 dbg_printf("Unknown mode %x", addr->Mode);
695 break;
699 /***********************************************************************
700 * print_address
702 * Print an 16- or 32-bit address, with the nearest symbol if any.
704 void print_address(const ADDRESS64* addr, BOOLEAN with_line)
706 char buffer[sizeof(SYMBOL_INFO) + 256];
707 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
708 DWORD_PTR lin = (DWORD_PTR)memory_to_linear_addr(addr);
709 DWORD64 disp64;
710 DWORD disp;
711 IMAGEHLP_MODULE im;
713 print_bare_address(addr);
715 si->SizeOfStruct = sizeof(*si);
716 si->MaxNameLen = 256;
717 im.SizeOfStruct = 0;
718 if (SymFromAddr(dbg_curr_process->handle, lin, &disp64, si) && disp64 < si->Size)
720 dbg_printf(" %s", si->Name);
721 if (disp64) dbg_printf("+0x%I64x", disp64);
723 else
725 im.SizeOfStruct = sizeof(im);
726 if (!SymGetModuleInfo(dbg_curr_process->handle, lin, &im)) return;
727 dbg_printf(" %s", im.ModuleName);
728 if (lin > im.BaseOfImage)
729 dbg_printf("+0x%Ix", lin - im.BaseOfImage);
731 if (with_line)
733 IMAGEHLP_LINE64 il;
735 il.SizeOfStruct = sizeof(il);
736 if (SymGetLineFromAddr64(dbg_curr_process->handle, lin, &disp, &il))
737 dbg_printf(" [%s:%lu]", il.FileName, il.LineNumber);
738 if (im.SizeOfStruct == 0) /* don't display again module if address is in module+disp form */
740 im.SizeOfStruct = sizeof(im);
741 if (SymGetModuleInfo(dbg_curr_process->handle, lin, &im))
742 dbg_printf(" in %s", im.ModuleName);
747 BOOL memory_disasm_one_insn(ADDRESS64* addr)
749 char ch;
751 print_address(addr, TRUE);
752 dbg_printf(": ");
753 if (!dbg_read_memory(memory_to_linear_addr(addr), &ch, sizeof(ch)))
755 dbg_printf("-- no code accessible --\n");
756 return FALSE;
758 dbg_curr_process->be_cpu->disasm_one_insn(addr, TRUE);
759 dbg_printf("\n");
760 return TRUE;
763 void memory_disassemble(const struct dbg_lvalue* xstart,
764 const struct dbg_lvalue* xend, int instruction_count)
766 static ADDRESS64 last = {0,0,0};
767 dbg_lgint_t stop = 0;
768 int i;
770 if (!xstart && !xend)
772 if (!last.Segment && !last.Offset) memory_get_current_pc(&last);
774 else
776 if (xstart)
777 types_extract_as_address(xstart, &last);
778 if (xend)
779 stop = types_extract_as_integer(xend);
781 for (i = 0; (instruction_count == 0 || i < instruction_count) &&
782 (stop == 0 || last.Offset <= stop); i++)
783 memory_disasm_one_insn(&last);
786 BOOL memory_get_register(DWORD regno, struct dbg_lvalue* lvalue, char* buffer, int len)
788 const struct dbg_internal_var* div;
790 /* negative register values are wine's dbghelp hacks
791 * see dlls/dbghelp/dbghelp_private.h for the details
793 switch (regno)
795 case -1:
796 if (buffer) snprintf(buffer, len, "<internal error>");
797 return FALSE;
798 case -2:
799 if (buffer) snprintf(buffer, len, "<couldn't compute location>");
800 return FALSE;
801 case -3:
802 if (buffer) snprintf(buffer, len, "<is not available>");
803 return FALSE;
804 case -4:
805 if (buffer) snprintf(buffer, len, "<couldn't read memory>");
806 return FALSE;
807 case -5:
808 if (buffer) snprintf(buffer, len, "<has been optimized away by compiler>");
809 return FALSE;
812 for (div = dbg_curr_process->be_cpu->context_vars; div->name; div++)
814 if (div->val == regno)
816 if (!stack_get_register_frame(div, lvalue))
818 if (buffer) snprintf(buffer, len, "<register %s not accessible in this frame>", div->name);
819 return FALSE;
822 if (buffer) lstrcpynA(buffer, div->name, len);
823 return TRUE;
826 if (buffer) snprintf(buffer, len, "<unknown register %lu>", regno);
827 return FALSE;