1 /*
2 * Debugger memory handling
3 *
4 * Copyright 1993 Eric Youngdale
5 * Copyright 1995 Alexandre Julliard
6 * Copyright 2000-2005 Eric Pouech
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29
30 #include "debugger.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
34
35 void* be_cpu_linearize(HANDLE hThread, const ADDRESS64* addr)
36 {
37 assert(addr->Mode == AddrModeFlat);
38 return (void*)(DWORD_PTR)addr->Offset;
39 }
40
41 unsigned be_cpu_build_addr(HANDLE hThread, const CONTEXT* ctx, ADDRESS64* addr,
42 unsigned seg, unsigned long offset)
43 {
44 addr->Mode = AddrModeFlat;
45 addr->Segment = 0; /* don't need segment */
46 addr->Offset = offset;
47 return TRUE;
48 }
49
50 void* memory_to_linear_addr(const ADDRESS64* addr)
51 {
52 return be_cpu->linearize(dbg_curr_thread->handle, addr);
53 }
54
55 BOOL memory_get_current_pc(ADDRESS64* addr)
56 {
57 assert(be_cpu->get_addr);
58 return be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
59 be_cpu_addr_pc, addr);
60 }
61
62 BOOL memory_get_current_stack(ADDRESS64* addr)
63 {
64 assert(be_cpu->get_addr);
65 return be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
66 be_cpu_addr_stack, addr);
67 }
68
69 BOOL memory_get_current_frame(ADDRESS64* addr)
70 {
71 assert(be_cpu->get_addr);
72 return be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
73 be_cpu_addr_frame, addr);
74 }
75
76 static void memory_report_invalid_addr(const void* addr)
77 {
78 ADDRESS64 address;
79
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");
86 }
87
88 /***********************************************************************
89 * memory_read_value
90 *
91 * Read a memory value.
92 */
93 BOOL memory_read_value(const struct dbg_lvalue* lvalue, DWORD size, void* result)
94 {
95 BOOL ret = FALSE;
96
97 if (lvalue->cookie == DLV_TARGET)
98 {
99 void* linear = memory_to_linear_addr(&lvalue->addr);
100 if (!(ret = dbg_read_memory(linear, result, size)))
101 memory_report_invalid_addr(linear);
102 }
103 else
104 {
105 if (lvalue->addr.Offset)
106 {
107 memcpy(result, (void*)(DWORD_PTR)lvalue->addr.Offset, size);
108 ret = TRUE;
109 }
110 }
111 return ret;
112 }
113
114 /***********************************************************************
115 * memory_write_value
116 *
117 * Store a value in memory.
118 */
119 BOOL memory_write_value(const struct dbg_lvalue* lvalue, DWORD size, void* value)
120 {
121 BOOL ret = TRUE;
122 DWORD64 os;
123
124 os = ~(DWORD64)size;
125 types_get_info(&lvalue->type, TI_GET_LENGTH, &os);
126 assert(size == os);
127
128 /* FIXME: only works on little endian systems */
129 if (lvalue->cookie == DLV_TARGET)
130 {
131 void* linear = memory_to_linear_addr(&lvalue->addr);
132 if (!(ret = dbg_write_memory(linear, value, size)))
133 memory_report_invalid_addr(linear);
134 }
135 else
136 {
137 memcpy((void*)(DWORD_PTR)lvalue->addr.Offset, value, size);
138 }
139 return ret;
140 }
141
142 /***********************************************************************
143 * memory_examine
144 *
145 * Implementation of the 'x' command.
146 */
147 void memory_examine(const struct dbg_lvalue *lvalue, int count, char format)
148 {
149 int i;
150 char buffer[256];
151 ADDRESS64 addr;
152 void *linear;
153
154 types_extract_as_address(lvalue, &addr);
155 linear = memory_to_linear_addr(&addr);
156
157 if (format != 'i' && count > 1)
158 {
159 print_address(&addr, FALSE);
160 dbg_printf(": ");
161 }
162
163 switch (format)
164 {
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--)
182 {
183 GUID guid;
184 if (!dbg_read_memory(linear, &guid, sizeof(guid)))
185 {
186 memory_report_invalid_addr(linear);
187 break;
188 }
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)
196 {
197 print_address(&addr, FALSE);
198 dbg_printf(": ");
199 }
200 }
201 return;
202
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) \
212 { \
213 dbg_printf("\n"); \
214 print_address(&addr, FALSE); \
215 dbg_printf(": "); \
216 } \
217 } \
218 dbg_printf("\n"); \
219 } \
220 return
221 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
222
223 case 'x': DO_DUMP(int, 4, " %8.8x");
224 case 'd': DO_DUMP(unsigned int, 4, " %4.4d");
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);
228 }
229 }
230
231 BOOL memory_get_string(struct dbg_process* pcs, void* addr, BOOL in_debuggee,
232 BOOL unicode, char* buffer, int size)
233 {
234 SIZE_T sz;
235 WCHAR* buffW;
236
237 buffer[0] = 0;
238 if (!addr) return FALSE;
239 if (in_debuggee)
240 {
241 BOOL ret;
242
243 if (!unicode) ret = pcs->process_io->read(pcs->handle, addr, buffer, size, &sz);
244 else
245 {
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);
251 }
252 if (size) buffer[size-1] = 0;
253 return ret;
254 }
255 else
256 {
257 lstrcpynA(buffer, addr, size);
258 }
259 return TRUE;
260 }
261
262 BOOL memory_get_string_indirect(struct dbg_process* pcs, void* addr, BOOL unicode, WCHAR* buffer, int size)
263 {
264 void* ad;
265 SIZE_T sz;
266
267 buffer[0] = 0;
268 if (addr &&
269 pcs->process_io->read(pcs->handle, addr, &ad, sizeof(ad), &sz) && sz == sizeof(ad) && ad)
270 {
271 LPSTR buff;
272 BOOL ret;
273
274 if (unicode)
275 ret = pcs->process_io->read(pcs->handle, ad, buffer, size * sizeof(WCHAR), &sz) && sz != 0;
276 else
277 {
278 if ((buff = HeapAlloc(GetProcessHeap(), 0, size)))
279 {
280 ret = pcs->process_io->read(pcs->handle, ad, buff, size, &sz) && sz != 0;
281 MultiByteToWideChar(CP_ACP, 0, buff, sz, buffer, size);
282 HeapFree(GetProcessHeap(), 0, buff);
283 }
284 else ret = FALSE;
285 }
286 if (size) buffer[size-1] = 0;
287 return ret;
288 }
289 return FALSE;
290 }
291
292 /*
293 * Convert an address offset to hex string. If mode == 32, treat offset as
294 * 32 bits (discard upper 32 bits), if mode == 64 use all 64 bits, if mode == 0
295 * treat as either 32 or 64 bits, depending on whether we're running as
296 * Wine32 or Wine64.
297 */
298 char* memory_offset_to_string(char *str, DWORD64 offset, unsigned mode)
299 {
300 if (mode != 32 && mode != 64)
301 {
302 #ifdef _WIN64
303 mode = 64;
304 #else
305 mode = 32;
306 #endif
307 }
308
309 if (mode == 32)
310 sprintf(str, "0x%08x", (unsigned int) offset);
311 else
312 sprintf(str, "0x%08x%08x", (unsigned int)(offset >> 32),
313 (unsigned int)offset);
314
315 return str;
316 }
317
318 static void dbg_print_longlong(LONGLONG sv, BOOL is_signed)
319 {
320 char tmp[24], *ptr = tmp + sizeof(tmp) - 1;
321 ULONGLONG uv, div;
322 *ptr = '\0';
323 if (is_signed && sv < 0) uv = -sv;
324 else { uv = sv; is_signed = FALSE; }
325 for (div = 10; uv; div *= 10, uv /= 10)
326 *--ptr = '' + (uv % 10);
327 if (ptr == tmp + sizeof(tmp) - 1) *--ptr = '';
328 if (is_signed) *--ptr = '-';
329 dbg_printf("%s", ptr);
330 }
331
332 static void print_typed_basic(const struct dbg_lvalue* lvalue)
333 {
334 LONGLONG val_int;
335 void* val_ptr;
336 long double val_real;
337 DWORD64 size64;
338 DWORD tag, size, count, bt;
339 struct dbg_type type = lvalue->type;
340 struct dbg_type sub_type;
341
342 if (!types_get_real_type(&type, &tag)) return;
343
344 switch (tag)
345 {
346 case SymTagBaseType:
347 if (!types_get_info(&type, TI_GET_LENGTH, &size64) ||
348 !types_get_info(&type, TI_GET_BASETYPE, &bt))
349 {
350 WINE_ERR("Couldn't get information\n");
351 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
352 }
353 size = (DWORD)size64;
354 switch (bt)
355 {
356 case btInt:
357 case btLong:
358 if (!be_cpu->fetch_integer(lvalue, size, TRUE, &val_int)) return;
359 if (size == 1) goto print_char;
360 dbg_print_longlong(val_int, TRUE);
361 break;
362 case btUInt:
363 case btULong:
364 if (!be_cpu->fetch_integer(lvalue, size, FALSE, &val_int)) return;
365 dbg_print_longlong(val_int, FALSE);
366 break;
367 case btFloat:
368 if (!be_cpu->fetch_float(lvalue, size, &val_real)) return;
369 dbg_printf("%Lf", val_real);
370 break;
371 case btChar:
372 if (!be_cpu->fetch_integer(lvalue, size, TRUE, &val_int)) return;
373 /* FIXME: should do the same for a Unicode character (size == 2) */
374 print_char:
375 if (size == 1 && (val_int < 0x20 || val_int > 0x80))
376 dbg_printf("%d", (int)val_int);
377 else
378 dbg_printf("'%c'", (char)val_int);
379 break;
380 case btBool:
381 if (!be_cpu->fetch_integer(lvalue, size, TRUE, &val_int)) return;
382 dbg_printf("%s", val_int ? "true" : "false");
383 break;
384 default:
385 WINE_FIXME("Unsupported basetype %u\n", bt);
386 break;
387 }
388 break;
389 case SymTagPointerType:
390 if (!memory_read_value(lvalue, sizeof(void*), &val_ptr)) return;
391
392 sub_type.module = lvalue->type.module;
393 if (!types_get_info(&type, TI_GET_TYPE, &sub_type.id) ||
394 sub_type.id == dbg_itype_none)
395 {
396 dbg_printf("Internal symbol error: unable to access memory location %p", val_ptr);
397 break;
398 }
399 if (!types_get_real_type(&sub_type, &tag)) return;
400
401 if (types_get_info(&sub_type, TI_GET_SYMTAG, &tag) && tag == SymTagBaseType &&
402 types_get_info(&sub_type, TI_GET_BASETYPE, &bt) && (bt == btChar || bt == btInt) &&
403 types_get_info(&sub_type, TI_GET_LENGTH, &size64) && size64 == 1)
404 {
405 char buffer[1024];
406
407 if (!val_ptr) dbg_printf("0x0");
408 else if (memory_get_string(dbg_curr_process, val_ptr,
409 lvalue->cookie == DLV_TARGET,
410 size64 == 2, buffer, sizeof(buffer)))
411 dbg_printf("\"%s\"", buffer);
412 else
413 dbg_printf("*** invalid address %p ***", val_ptr);
414 }
415 else dbg_printf("%p", val_ptr);
416 break;
417 case SymTagArrayType:
418 case SymTagUDT:
419 if (!memory_read_value(lvalue, sizeof(val_ptr), &val_ptr)) return;
420 dbg_printf("%p", val_ptr);
421 break;
422 case SymTagEnum:
423 {
424 BOOL ok = FALSE;
425
426 /* FIXME: it depends on underlying type for enums
427 * (not supported yet in dbghelp)
428 * Assuming 4 as for an int
429 */
430 if (!be_cpu->fetch_integer(lvalue, 4, TRUE, &val_int)) return;
431
432 if (types_get_info(&type, TI_GET_CHILDRENCOUNT, &count))
433 {
434 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
435 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
436 WCHAR* ptr;
437 char tmp[256];
438 VARIANT variant;
439 int i;
440
441 fcp->Start = 0;
442 while (count)
443 {
444 fcp->Count = min(count, 256);
445 if (types_get_info(&type, TI_FINDCHILDREN, fcp))
446 {
447 sub_type.module = type.module;
448 for (i = 0; i < min(fcp->Count, count); i++)
449 {
450 sub_type.id = fcp->ChildId[i];
451 if (!types_get_info(&sub_type, TI_GET_VALUE, &variant))
452 continue;
453 switch (variant.n1.n2.vt)
454 {
455 case VT_I4: ok = (val_int == variant.n1.n2.n3.lVal); break;
456 default: WINE_FIXME("Unsupported variant type (%u)\n", variant.n1.n2.vt);
457 }
458 if (ok)
459 {
460 ptr = NULL;
461 types_get_info(&sub_type, TI_GET_SYMNAME, &ptr);
462 if (!ptr) continue;
463 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
464 HeapFree(GetProcessHeap(), 0, ptr);
465 dbg_printf("%s", tmp);
466 count = 0; /* so that we'll get away from outter loop */
467 break;
468 }
469 }
470 }
471 count -= min(count, 256);
472 fcp->Start += 256;
473 }
474 }
475 if (!ok) dbg_print_longlong(val_int, TRUE);
476 }
477 break;
478 default:
479 WINE_FIXME("Unsupported tag %u\n", tag);
480 break;
481 }
482 }
483
484 /***********************************************************************
485 * print_basic
486 *
487 * Implementation of the 'print' command.
488 */
489 void print_basic(const struct dbg_lvalue* lvalue, char format)
490 {
491 if (lvalue->type.id == dbg_itype_none)
492 {
493 dbg_printf("Unable to evaluate expression\n");
494 return;
495 }
496
497 if (format != 0)
498 {
499 unsigned size;
500 LONGLONG res = types_extract_as_longlong(lvalue, &size);
501 DWORD hi;
502 WCHAR wch;
503
504 /* FIXME: this implies i386 byte ordering */
505 switch (format)
506 {
507 case 'x':
508 hi = (ULONG64)res >> 32;
509 if (size == 8 && hi)
510 dbg_printf("0x%x%08x", hi, (DWORD)res);
511 else
512 dbg_printf("0x%x", (DWORD)res);
513 return;
514
515 case 'd':
516 dbg_print_longlong(res, TRUE);
517 dbg_printf("\n");
518 return;
519
520 case 'c':
521 dbg_printf("%d = '%c'", (char)(res & 0xff), (char)(res & 0xff));
522 return;
523
524 case 'u':
525 wch = (WCHAR)(res & 0xFFFF);
526 dbg_printf("%d = '", wch);
527 dbg_outputW(&wch, 1);
528 dbg_printf("'");
529 return;
530
531 case 'i':
532 case 's':
533 case 'w':
534 case 'b':
535 dbg_printf("Format specifier '%c' is meaningless in 'print' command\n", format);
536 }
537 }
538 if (lvalue->type.id == dbg_itype_segptr)
539 {
540 dbg_print_longlong(types_extract_as_longlong(lvalue, NULL), TRUE);
541 dbg_printf("\n");
542 }
543 else print_typed_basic(lvalue);
544 }
545
546 void print_bare_address(const ADDRESS64* addr)
547 {
548 char hexbuf[MAX_OFFSET_TO_STR_LEN];
549
550 switch (addr->Mode)
551 {
552 case AddrModeFlat:
553 dbg_printf("%s", memory_offset_to_string(hexbuf, addr->Offset, 0));
554 break;
555 case AddrModeReal:
556 case AddrMode1616:
557 dbg_printf("0x%04x:0x%04x", addr->Segment, (unsigned) addr->Offset);
558 break;
559 case AddrMode1632:
560 dbg_printf("0x%04x:%s", addr->Segment,
561 memory_offset_to_string(hexbuf, addr->Offset, 32));
562 break;
563 default:
564 dbg_printf("Unknown mode %x\n", addr->Mode);
565 break;
566 }
567 }
568
569 /***********************************************************************
570 * print_address
571 *
572 * Print an 16- or 32-bit address, with the nearest symbol if any.
573 */
574 void print_address(const ADDRESS64* addr, BOOLEAN with_line)
575 {
576 char buffer[sizeof(SYMBOL_INFO) + 256];
577 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
578 void* lin = memory_to_linear_addr(addr);
579 DWORD64 disp64;
580 DWORD disp;
581
582 print_bare_address(addr);
583
584 si->SizeOfStruct = sizeof(*si);
585 si->MaxNameLen = 256;
586 if (!SymFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp64, si)) return;
587 dbg_printf(" %s", si->Name);
588 if (disp64) dbg_printf("+0x%lx", (DWORD_PTR)disp64);
589 if (with_line)
590 {
591 IMAGEHLP_LINE il;
592 IMAGEHLP_MODULE im;
593
594 il.SizeOfStruct = sizeof(il);
595 if (SymGetLineFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp, &il))
596 dbg_printf(" [%s:%u]", il.FileName, il.LineNumber);
597 im.SizeOfStruct = sizeof(im);
598 if (SymGetModuleInfo(dbg_curr_process->handle, (DWORD_PTR)lin, &im))
599 dbg_printf(" in %s", im.ModuleName);
600 }
601 }
602
603 BOOL memory_disasm_one_insn(ADDRESS64* addr)
604 {
605 char ch;
606
607 print_address(addr, TRUE);
608 dbg_printf(": ");
609 if (!dbg_read_memory(memory_to_linear_addr(addr), &ch, sizeof(ch)))
610 {
611 dbg_printf("-- no code accessible --\n");
612 return FALSE;
613 }
614 be_cpu->disasm_one_insn(addr, TRUE);
615 dbg_printf("\n");
616 return TRUE;
617 }
618
619 void memory_disassemble(const struct dbg_lvalue* xstart,
620 const struct dbg_lvalue* xend, int instruction_count)
621 {
622 static ADDRESS64 last = {0,0,0};
623 int stop = 0;
624 int i;
625
626 if (!xstart && !xend)
627 {
628 if (!last.Segment && !last.Offset) memory_get_current_pc(&last);
629 }
630 else
631 {
632 if (xstart)
633 types_extract_as_address(xstart, &last);
634 if (xend)
635 stop = types_extract_as_integer(xend);
636 }
637 for (i = 0; (instruction_count == 0 || i < instruction_count) &&
638 (stop == 0 || last.Offset <= stop); i++)
639 memory_disasm_one_insn(&last);
640 }
641
642 BOOL memory_get_register(DWORD regno, DWORD** value, char* buffer, int len)
643 {
644 const struct dbg_internal_var* div;
645
646 /* negative register values are wine's dbghelp hacks
647 * see dlls/dbghelp/dbghelp_internal.h for the details
648 */
649 switch (regno)
650 {
651 case -1:
652 if (buffer) snprintf(buffer, len, "<internal error>");
653 return FALSE;
654 case -2:
655 if (buffer) snprintf(buffer, len, "<couldn't compute location>");
656 return FALSE;
657 case -3:
658 if (buffer) snprintf(buffer, len, "<is not available>");
659 return FALSE;
660 case -4:
661 if (buffer) snprintf(buffer, len, "<couldn't read memory>");
662 return FALSE;
663 }
664
665 for (div = dbg_context_vars; div->name; div++)
666 {
667 if (div->val == regno)
668 {
669 if (dbg_curr_thread->curr_frame != 0)
670 {
671 if (!stack_get_register_current_frame(regno, value))
672 {
673 if (buffer) snprintf(buffer, len, "<register %s not in topmost frame>", div->name);
674 return FALSE;
675 }
676 }
677 else
678 *value = div->pval;
679
680 if (buffer) lstrcpynA(buffer, div->name, len);
681 return TRUE;
682 }
683 }
684 if (buffer) snprintf(buffer, len, "<unknown register %u>", regno);
685 return FALSE;
686 }
687
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.