wined3d: Return the map pitch in wined3d_device_context_map().
[wine.git] / programs / winedbg / memory.c
blob8cc039a6899416027748c62d9c73622f7b230a41
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"
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 BOOL be_cpu_build_addr(HANDLE hThread, const dbg_ctx_t *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;
48 return TRUE;
51 void* memory_to_linear_addr(const ADDRESS64* addr)
53 return dbg_curr_process->be_cpu->linearize(dbg_curr_thread->handle, addr);
56 BOOL memory_get_current_pc(ADDRESS64* addr)
58 assert(dbg_curr_process->be_cpu->get_addr);
59 return dbg_curr_process->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(dbg_curr_process->be_cpu->get_addr);
66 return dbg_curr_process->be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
67 be_cpu_addr_stack, addr);
70 static void memory_report_invalid_addr(const void* addr)
72 ADDRESS64 address;
74 address.Mode = AddrModeFlat;
75 address.Segment = 0;
76 address.Offset = (ULONG_PTR)addr;
77 dbg_printf("*** Invalid address ");
78 print_address(&address, FALSE);
79 dbg_printf(" ***\n");
82 /***********************************************************************
83 * memory_read_value
85 * Read a memory value.
87 BOOL memory_read_value(const struct dbg_lvalue* lvalue, DWORD size, void* result)
89 BOOL ret = FALSE;
91 if (lvalue->cookie == DLV_TARGET)
93 void* linear = memory_to_linear_addr(&lvalue->addr);
94 if (!(ret = dbg_read_memory(linear, result, size)))
95 memory_report_invalid_addr(linear);
97 else
99 if (lvalue->addr.Offset)
101 memcpy(result, (void*)(DWORD_PTR)lvalue->addr.Offset, size);
102 ret = TRUE;
105 return ret;
108 /***********************************************************************
109 * memory_write_value
111 * Store a value in memory.
113 BOOL memory_write_value(const struct dbg_lvalue* lvalue, DWORD size, void* value)
115 BOOL ret = TRUE;
116 DWORD64 os;
118 if (!types_get_info(&lvalue->type, TI_GET_LENGTH, &os)) return FALSE;
119 if (size != os)
121 dbg_printf("Size mismatch in memory_write_value, got %u from type while expecting %u\n",
122 (DWORD)os, size);
123 return FALSE;
126 /* FIXME: only works on little endian systems */
127 if (lvalue->cookie == DLV_TARGET)
129 void* linear = memory_to_linear_addr(&lvalue->addr);
130 if (!(ret = dbg_write_memory(linear, value, size)))
131 memory_report_invalid_addr(linear);
133 else
135 memcpy((void*)(DWORD_PTR)lvalue->addr.Offset, value, size);
137 return ret;
140 /***********************************************************************
141 * memory_examine
143 * Implementation of the 'x' command.
145 void memory_examine(const struct dbg_lvalue *lvalue, int count, char format)
147 int i;
148 char buffer[256];
149 ADDRESS64 addr;
150 void *linear;
152 types_extract_as_address(lvalue, &addr);
153 linear = memory_to_linear_addr(&addr);
155 if (format != 'i' && count > 1)
157 print_address(&addr, FALSE);
158 dbg_printf(": ");
161 switch (format)
163 case 'u':
164 if (count == 1) count = 256;
165 memory_get_string(dbg_curr_process, linear,
166 TRUE, TRUE, buffer, min(count, sizeof(buffer)));
167 dbg_printf("%s\n", buffer);
168 return;
169 case 's':
170 if (count == 1) count = 256;
171 memory_get_string(dbg_curr_process, linear,
172 TRUE, FALSE, buffer, min(count, sizeof(buffer)));
173 dbg_printf("%s\n", buffer);
174 return;
175 case 'i':
176 while (count-- && memory_disasm_one_insn(&addr));
177 return;
178 case 'g':
179 while (count--)
181 GUID guid;
182 if (!dbg_read_memory(linear, &guid, sizeof(guid)))
184 memory_report_invalid_addr(linear);
185 break;
187 dbg_printf("{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
188 guid.Data1, guid.Data2, guid.Data3,
189 guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
190 guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
191 linear = (char*)linear + sizeof(guid);
192 addr.Offset += sizeof(guid);
193 if (count)
195 print_address(&addr, FALSE);
196 dbg_printf(": ");
199 return;
201 #define DO_DUMP2(_t,_l,_f,_vv) { \
202 _t _v; \
203 for (i = 0; i < count; i++) { \
204 if (!dbg_read_memory(linear, &_v, sizeof(_t))) \
205 { memory_report_invalid_addr(linear); break; } \
206 dbg_printf(_f, (_vv)); \
207 addr.Offset += sizeof(_t); \
208 linear = (char*)linear + sizeof(_t); \
209 if ((i % (_l)) == (_l) - 1 && i != count - 1) \
211 dbg_printf("\n"); \
212 print_address(&addr, FALSE); \
213 dbg_printf(": "); \
216 dbg_printf("\n"); \
218 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
220 case 'x': DO_DUMP(int, 4, " %8.8x"); break;
221 case 'd': DO_DUMP(unsigned int, 4, " %4.4d"); break;
222 case 'w': DO_DUMP(unsigned short, 8, " %04x"); break;
223 case 'a':
224 if (sizeof(DWORD_PTR) == 4)
226 DO_DUMP(DWORD_PTR, 4, " %8.8lx");
228 else
230 DO_DUMP(DWORD_PTR, 2, " %16.16lx");
232 break;
233 case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v); break;
234 case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff); break;
238 BOOL memory_get_string(struct dbg_process* pcs, void* addr, BOOL in_debuggee,
239 BOOL unicode, char* buffer, int size)
241 SIZE_T sz;
242 WCHAR* buffW;
244 buffer[0] = 0;
245 if (!addr) return FALSE;
246 if (in_debuggee)
248 BOOL ret;
250 if (!unicode) ret = pcs->process_io->read(pcs->handle, addr, buffer, size, &sz);
251 else
253 buffW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
254 ret = pcs->process_io->read(pcs->handle, addr, buffW, size * sizeof(WCHAR), &sz);
255 WideCharToMultiByte(CP_ACP, 0, buffW, sz / sizeof(WCHAR), buffer, size,
256 NULL, NULL);
257 HeapFree(GetProcessHeap(), 0, buffW);
259 if (size) buffer[size-1] = 0;
260 return ret;
262 else
264 lstrcpynA(buffer, addr, size);
266 return TRUE;
269 BOOL memory_get_string_indirect(struct dbg_process* pcs, void* addr, BOOL unicode, WCHAR* buffer, int size)
271 void* ad = 0;
272 SIZE_T sz;
274 buffer[0] = 0;
275 if (addr &&
276 pcs->process_io->read(pcs->handle, addr, &ad, pcs->be_cpu->pointer_size, &sz) && sz == pcs->be_cpu->pointer_size && ad)
278 LPSTR buff;
279 BOOL ret;
281 if (unicode)
282 ret = pcs->process_io->read(pcs->handle, ad, buffer, size * sizeof(WCHAR), &sz) && sz != 0;
283 else
285 if ((buff = HeapAlloc(GetProcessHeap(), 0, size)))
287 ret = pcs->process_io->read(pcs->handle, ad, buff, size, &sz) && sz != 0;
288 MultiByteToWideChar(CP_ACP, 0, buff, sz, buffer, size);
289 HeapFree(GetProcessHeap(), 0, buff);
291 else ret = FALSE;
293 if (size) buffer[size-1] = 0;
294 return ret;
296 return FALSE;
300 * Convert an address offset to hex string. If mode == 32, treat offset as
301 * 32 bits (discard upper 32 bits), if mode == 64 use all 64 bits, if mode == 0
302 * treat as either 32 or 64 bits, depending on whether we're running as
303 * Wine32 or Wine64.
305 char* memory_offset_to_string(char *str, DWORD64 offset, unsigned mode)
307 if (mode != 32 && mode != 64)
309 #ifdef _WIN64
310 mode = 64;
311 #else
312 mode = 32;
313 #endif
316 if (mode == 32)
317 sprintf(str, "0x%08x", (unsigned int) offset);
318 else
319 sprintf(str, "0x%08x%08x", (unsigned int)(offset >> 32),
320 (unsigned int)offset);
322 return str;
325 static void dbg_print_longlong(LONGLONG sv, BOOL is_signed)
327 char tmp[24], *ptr = tmp + sizeof(tmp) - 1;
328 ULONGLONG uv, div;
329 *ptr = '\0';
330 if (is_signed && sv < 0) uv = -sv;
331 else { uv = sv; is_signed = FALSE; }
332 for (div = 10; uv; div *= 10, uv /= 10)
333 *--ptr = '0' + (uv % 10);
334 if (ptr == tmp + sizeof(tmp) - 1) *--ptr = '0';
335 if (is_signed) *--ptr = '-';
336 dbg_printf("%s", ptr);
339 static void dbg_print_hex(DWORD size, ULONGLONG sv)
341 if (!sv)
342 dbg_printf("0");
343 else if (size > 4 && (sv >> 32))
344 dbg_printf("0x%x%08x", (DWORD)(sv >> 32), (DWORD)sv);
345 else
346 dbg_printf("0x%x", (DWORD)sv);
349 static void print_typed_basic(const struct dbg_lvalue* lvalue)
351 LONGLONG val_int;
352 void* val_ptr;
353 long double val_real;
354 DWORD64 size64;
355 DWORD tag, size, count, bt;
356 struct dbg_type type = lvalue->type;
357 struct dbg_type sub_type;
358 struct dbg_lvalue sub_lvalue;
360 if (!types_get_real_type(&type, &tag)) return;
362 switch (tag)
364 case SymTagBaseType:
365 if (!types_get_info(&type, TI_GET_LENGTH, &size64) ||
366 !types_get_info(&type, TI_GET_BASETYPE, &bt))
368 WINE_ERR("Couldn't get information\n");
369 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
370 return;
372 size = (DWORD)size64;
373 switch (bt)
375 case btInt:
376 case btLong:
377 if (!dbg_curr_process->be_cpu->fetch_integer(lvalue, size, TRUE, &val_int)) return;
378 if (size == 1) goto print_char;
379 dbg_print_hex(size, val_int);
380 break;
381 case btUInt:
382 case btULong:
383 if (!dbg_curr_process->be_cpu->fetch_integer(lvalue, size, FALSE, &val_int)) return;
384 dbg_print_hex(size, val_int);
385 break;
386 case btFloat:
387 if (!dbg_curr_process->be_cpu->fetch_float(lvalue, size, &val_real)) return;
388 dbg_printf("%Lf", val_real);
389 break;
390 case btChar:
391 case btWChar:
392 /* sometimes WCHAR is defined as btChar with size = 2, so discrimate
393 * Ansi/Unicode based on size, not on basetype
395 if (!dbg_curr_process->be_cpu->fetch_integer(lvalue, size, TRUE, &val_int)) return;
396 print_char:
397 if ((size == 1 && isprint((char)val_int)) ||
398 (size == 2 && val_int < 127 && isprint((char)val_int)))
399 dbg_printf("'%c'", (char)val_int);
400 else
401 dbg_printf("%d", (int)val_int);
402 break;
403 case btBool:
404 if (!dbg_curr_process->be_cpu->fetch_integer(lvalue, size, TRUE, &val_int)) return;
405 dbg_printf("%s", val_int ? "true" : "false");
406 break;
407 default:
408 WINE_FIXME("Unsupported basetype %u\n", bt);
409 break;
411 break;
412 case SymTagPointerType:
413 if (!types_array_index(lvalue, 0, &sub_lvalue))
415 dbg_printf("Internal symbol error: unable to access memory location %p",
416 memory_to_linear_addr(&lvalue->addr));
417 break;
419 val_ptr = memory_to_linear_addr(&sub_lvalue.addr);
420 if (types_get_real_type(&sub_lvalue.type, &tag) && tag == SymTagBaseType &&
421 types_get_info(&sub_lvalue.type, TI_GET_BASETYPE, &bt) &&
422 types_get_info(&sub_lvalue.type, TI_GET_LENGTH, &size64))
424 char buffer[1024];
426 if (!val_ptr) dbg_printf("0x0");
427 else if (((bt == btChar || bt == btInt) && size64 == 1) || (bt == btUInt && size64 == 2))
429 if (memory_get_string(dbg_curr_process, val_ptr, sub_lvalue.cookie == DLV_TARGET,
430 size64 == 2, buffer, sizeof(buffer)))
431 dbg_printf("\"%s\"", buffer);
432 else
433 dbg_printf("*** invalid address %p ***", val_ptr);
434 break;
437 dbg_printf("%p", val_ptr);
438 break;
439 case SymTagArrayType:
440 case SymTagUDT:
441 if (!memory_read_value(lvalue, sizeof(val_ptr), &val_ptr)) return;
442 dbg_printf("%p", val_ptr);
443 break;
444 case SymTagEnum:
446 BOOL ok = FALSE;
448 /* FIXME: it depends on underlying type for enums
449 * (not supported yet in dbghelp)
450 * Assuming 4 as for an int
452 if (!dbg_curr_process->be_cpu->fetch_integer(lvalue, 4, TRUE, &val_int)) return;
454 if (types_get_info(&type, TI_GET_CHILDRENCOUNT, &count))
456 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
457 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
458 WCHAR* ptr;
459 char tmp[256];
460 VARIANT variant;
461 int i;
463 fcp->Start = 0;
464 while (count)
466 fcp->Count = min(count, 256);
467 if (types_get_info(&type, TI_FINDCHILDREN, fcp))
469 sub_type.module = type.module;
470 for (i = 0; i < min(fcp->Count, count); i++)
472 sub_type.id = fcp->ChildId[i];
473 if (!types_get_info(&sub_type, TI_GET_VALUE, &variant))
474 continue;
475 switch (variant.n1.n2.vt)
477 case VT_I4: ok = (val_int == variant.n1.n2.n3.lVal); break;
478 default: WINE_FIXME("Unsupported variant type (%u)\n", variant.n1.n2.vt);
480 if (ok)
482 ptr = NULL;
483 types_get_info(&sub_type, TI_GET_SYMNAME, &ptr);
484 if (!ptr) continue;
485 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
486 HeapFree(GetProcessHeap(), 0, ptr);
487 dbg_printf("%s", tmp);
488 count = 0; /* so that we'll get away from outer loop */
489 break;
493 count -= min(count, 256);
494 fcp->Start += 256;
497 if (!ok) dbg_print_longlong(val_int, TRUE);
499 break;
500 default:
501 WINE_FIXME("Unsupported tag %u\n", tag);
502 break;
506 /***********************************************************************
507 * print_basic
509 * Implementation of the 'print' command.
511 void print_basic(const struct dbg_lvalue* lvalue, char format)
513 if (lvalue->type.id == dbg_itype_none)
515 dbg_printf("Unable to evaluate expression\n");
516 return;
519 if (format != 0)
521 unsigned size;
522 LONGLONG res = types_extract_as_longlong(lvalue, &size, NULL);
523 WCHAR wch;
525 switch (format)
527 case 'x':
528 dbg_print_hex(size, (ULONGLONG)res);
529 return;
531 case 'd':
532 dbg_print_longlong(res, TRUE);
533 return;
535 case 'c':
536 dbg_printf("%d = '%c'", (char)(res & 0xff), (char)(res & 0xff));
537 return;
539 case 'u':
540 wch = (WCHAR)(res & 0xFFFF);
541 dbg_printf("%d = '", wch);
542 dbg_outputW(&wch, 1);
543 dbg_printf("'");
544 return;
546 case 'i':
547 case 's':
548 case 'w':
549 case 'b':
550 dbg_printf("Format specifier '%c' is meaningless in 'print' command\n", format);
553 if (lvalue->type.id == dbg_itype_segptr)
555 dbg_print_longlong(types_extract_as_longlong(lvalue, NULL, NULL), TRUE);
557 else print_typed_basic(lvalue);
560 void print_bare_address(const ADDRESS64* addr)
562 char hexbuf[MAX_OFFSET_TO_STR_LEN];
564 switch (addr->Mode)
566 case AddrModeFlat:
567 dbg_printf("%s", memory_offset_to_string(hexbuf, addr->Offset, 0));
568 break;
569 case AddrModeReal:
570 case AddrMode1616:
571 dbg_printf("0x%04x:0x%04x", addr->Segment, (unsigned) addr->Offset);
572 break;
573 case AddrMode1632:
574 dbg_printf("0x%04x:%s", addr->Segment,
575 memory_offset_to_string(hexbuf, addr->Offset, 32));
576 break;
577 default:
578 dbg_printf("Unknown mode %x", addr->Mode);
579 break;
583 /***********************************************************************
584 * print_address
586 * Print an 16- or 32-bit address, with the nearest symbol if any.
588 void print_address(const ADDRESS64* addr, BOOLEAN with_line)
590 char buffer[sizeof(SYMBOL_INFO) + 256];
591 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
592 void* lin = memory_to_linear_addr(addr);
593 DWORD64 disp64;
594 DWORD disp;
596 print_bare_address(addr);
598 si->SizeOfStruct = sizeof(*si);
599 si->MaxNameLen = 256;
600 if (!SymFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp64, si)) return;
601 dbg_printf(" %s", si->Name);
602 if (disp64) dbg_printf("+0x%lx", (DWORD_PTR)disp64);
603 if (with_line)
605 IMAGEHLP_LINE64 il;
606 IMAGEHLP_MODULE im;
608 il.SizeOfStruct = sizeof(il);
609 if (SymGetLineFromAddr64(dbg_curr_process->handle, (DWORD_PTR)lin, &disp, &il))
610 dbg_printf(" [%s:%u]", il.FileName, il.LineNumber);
611 im.SizeOfStruct = sizeof(im);
612 if (SymGetModuleInfo(dbg_curr_process->handle, (DWORD_PTR)lin, &im))
613 dbg_printf(" in %s", im.ModuleName);
617 BOOL memory_disasm_one_insn(ADDRESS64* addr)
619 char ch;
621 print_address(addr, TRUE);
622 dbg_printf(": ");
623 if (!dbg_read_memory(memory_to_linear_addr(addr), &ch, sizeof(ch)))
625 dbg_printf("-- no code accessible --\n");
626 return FALSE;
628 dbg_curr_process->be_cpu->disasm_one_insn(addr, TRUE);
629 dbg_printf("\n");
630 return TRUE;
633 void memory_disassemble(const struct dbg_lvalue* xstart,
634 const struct dbg_lvalue* xend, int instruction_count)
636 static ADDRESS64 last = {0,0,0};
637 int stop = 0;
638 int i;
640 if (!xstart && !xend)
642 if (!last.Segment && !last.Offset) memory_get_current_pc(&last);
644 else
646 if (xstart)
647 types_extract_as_address(xstart, &last);
648 if (xend)
649 stop = types_extract_as_integer(xend);
651 for (i = 0; (instruction_count == 0 || i < instruction_count) &&
652 (stop == 0 || last.Offset <= stop); i++)
653 memory_disasm_one_insn(&last);
656 BOOL memory_get_register(DWORD regno, DWORD_PTR** value, char* buffer, int len)
658 const struct dbg_internal_var* div;
660 /* negative register values are wine's dbghelp hacks
661 * see dlls/dbghelp/dbghelp_private.h for the details
663 switch (regno)
665 case -1:
666 if (buffer) snprintf(buffer, len, "<internal error>");
667 return FALSE;
668 case -2:
669 if (buffer) snprintf(buffer, len, "<couldn't compute location>");
670 return FALSE;
671 case -3:
672 if (buffer) snprintf(buffer, len, "<is not available>");
673 return FALSE;
674 case -4:
675 if (buffer) snprintf(buffer, len, "<couldn't read memory>");
676 return FALSE;
677 case -5:
678 if (buffer) snprintf(buffer, len, "<has been optimized away by compiler>");
679 return FALSE;
682 for (div = dbg_curr_process->be_cpu->context_vars; div->name; div++)
684 if (div->val == regno)
686 if (!stack_get_register_frame(div, value))
688 if (buffer) snprintf(buffer, len, "<register %s not accessible in this frame>", div->name);
689 return FALSE;
692 if (buffer) lstrcpynA(buffer, div->name, len);
693 return TRUE;
696 if (buffer) snprintf(buffer, len, "<unknown register %u>", regno);
697 return FALSE;