Fix the ITS moniker implementation.
[wine.git] / programs / winedbg / memory.c
blob8d1d16c0c6ecbaa7dc3529715cccf92af14fa1c6
1 /*
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
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 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;
47 return TRUE;
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 void memory_report_invalid_addr(const void* addr)
78 ADDRESS 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 if (lvalue->cookie == DLV_TARGET)
97 if (!dbg_read_memory_verbose(memory_to_linear_addr(&lvalue->addr), result, size))
98 return FALSE;
100 else
102 if (!lvalue->addr.Offset) return FALSE;
103 memcpy(result, (void*)lvalue->addr.Offset, size);
105 return TRUE;
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 DWORD os;
117 DWORD linear = (DWORD)memory_to_linear_addr(&lvalue->addr);
119 os = ~size;
120 types_get_info(&lvalue->type, TI_GET_LENGTH, &os);
121 assert(size == os);
123 /* FIXME: only works on little endian systems */
124 if (lvalue->cookie == DLV_TARGET)
126 ret = dbg_write_memory_verbose((void*)linear, value, size);
128 else
130 memcpy((void*)lvalue->addr.Offset, value, size);
132 return ret;
135 /***********************************************************************
136 * memory_examine
138 * Implementation of the 'x' command.
140 void memory_examine(void* linear, int count, char format)
142 int i;
143 char buffer[256];
144 ADDRESS addr;
146 addr.Mode = AddrModeFlat;
147 addr.Offset = (unsigned long)linear;
149 if (format != 'i' && count > 1)
151 print_address(&addr, FALSE);
152 dbg_printf(": ");
155 switch (format)
157 case 'u':
158 if (count == 1) count = 256;
159 memory_get_string(dbg_curr_process->handle, linear,
160 TRUE, TRUE, buffer, min(count, sizeof(buffer)));
161 dbg_printf("%s\n", buffer);
162 return;
163 case 's':
164 if (count == 1) count = 256;
165 memory_get_string(dbg_curr_process->handle, linear,
166 TRUE, FALSE, buffer, min(count, sizeof(buffer)));
167 dbg_printf("%s\n", buffer);
168 return;
169 case 'i':
170 while (count-- && memory_disasm_one_insn(&addr));
171 return;
172 case 'g':
173 while (count--)
175 GUID guid;
176 if (!dbg_read_memory_verbose(linear, &guid, sizeof(guid))) break;
177 dbg_printf("{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
178 guid.Data1, guid.Data2, guid.Data3,
179 guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
180 guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
181 linear = (char*)linear + sizeof(guid);
182 addr.Offset += sizeof(guid);
183 if (count)
185 print_address(&addr, FALSE);
186 dbg_printf(": ");
189 return;
191 #define DO_DUMP2(_t,_l,_f,_vv) { \
192 _t _v; \
193 for (i = 0; i < count; i++) { \
194 if (!dbg_read_memory_verbose(linear, &_v, \
195 sizeof(_t))) break; \
196 dbg_printf(_f, (_vv)); \
197 addr.Offset += sizeof(_t); \
198 linear = (char*)linear + sizeof(_t); \
199 if ((i % (_l)) == (_l) - 1 && i != count - 1) \
201 dbg_printf("\n"); \
202 print_address(&addr, FALSE); \
203 dbg_printf(": "); \
206 dbg_printf("\n"); \
208 return
209 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
211 case 'x': DO_DUMP(int, 4, " %8.8x");
212 case 'd': DO_DUMP(unsigned int, 4, " %10d");
213 case 'w': DO_DUMP(unsigned short, 8, " %04x");
214 case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v);
215 case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff);
219 BOOL memory_get_string(HANDLE hp, void* addr, BOOL in_debuggee, BOOL unicode,
220 char* buffer, int size)
222 DWORD sz;
223 WCHAR* buffW;
225 buffer[0] = 0;
226 if (!addr) return FALSE;
227 if (in_debuggee)
229 if (!unicode) return ReadProcessMemory(hp, addr, buffer, size, &sz);
231 buffW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
232 ReadProcessMemory(hp, addr, buffW, size * sizeof(WCHAR), &sz);
233 WideCharToMultiByte(CP_ACP, 0, buffW, sz / sizeof(WCHAR), buffer, size,
234 NULL, NULL);
235 HeapFree(GetProcessHeap(), 0, buffW);
237 else
239 strncpy(buffer, addr, size);
240 buffer[size - 1] = 0;
242 return TRUE;
245 BOOL memory_get_string_indirect(HANDLE hp, void* addr, BOOL unicode, char* buffer, int size)
247 void* ad;
248 DWORD sz;
250 buffer[0] = 0;
251 if (addr &&
252 ReadProcessMemory(hp, addr, &ad, sizeof(ad), &sz) && sz == sizeof(ad) && ad)
254 return memory_get_string(hp, ad, TRUE, unicode, buffer, size);
256 return FALSE;
259 static void print_typed_basic(const struct dbg_lvalue* lvalue)
261 long long int val_int;
262 void* val_ptr;
263 long double val_real;
264 DWORD tag, size, count, bt;
265 struct dbg_type rtype;
266 DWORD linear = (DWORD)memory_to_linear_addr(&lvalue->addr);
268 if (lvalue->type.id == dbg_itype_none ||
269 !types_get_info(&lvalue->type, TI_GET_SYMTAG, &tag))
270 return;
272 switch (tag)
274 case SymTagBaseType:
275 if (!types_get_info(&lvalue->type, TI_GET_LENGTH, &size) ||
276 !types_get_info(&lvalue->type, TI_GET_BASETYPE, &bt))
278 WINE_ERR("Couldn't get information\n");
279 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
282 switch (bt)
284 case btInt:
285 if (!be_cpu->fetch_integer(lvalue, size, TRUE, &val_int)) return;
286 dbg_printf("%lld", val_int);
287 break;
288 case btUInt:
289 if (!be_cpu->fetch_integer(lvalue, size, FALSE, &val_int)) return;
290 dbg_printf("%llu", val_int);
291 break;
292 case btFloat:
293 if (!be_cpu->fetch_float(lvalue, size, &val_real)) return;
294 dbg_printf("%Lf", val_real);
295 break;
296 case btChar:
297 if (!be_cpu->fetch_integer(lvalue, size, TRUE, &val_int)) return;
298 /* FIXME: should do the same for a Unicode character (size == 2) */
299 if (size == 1 && (val_int < 0x20 || val_int > 0x80))
300 dbg_printf("%d", (int)val_int);
301 else
302 dbg_printf("'%c'", (char)val_int);
303 break;
304 default:
305 WINE_FIXME("Unsupported basetype %lu\n", bt);
306 break;
308 break;
309 case SymTagPointerType:
310 if (!memory_read_value(lvalue, sizeof(void*), &val_ptr)) return;
312 if (!types_get_info(&lvalue->type, TI_GET_TYPE, &rtype.id) ||
313 rtype.id == dbg_itype_none)
315 dbg_printf("Internal symbol error: unable to access memory location %p", val_ptr);
316 break;
318 rtype.module = lvalue->type.module;
319 if (types_get_info(&rtype, TI_GET_SYMTAG, &tag) && tag == SymTagBaseType &&
320 types_get_info(&rtype, TI_GET_BASETYPE, &bt) && bt == btChar &&
321 types_get_info(&rtype, TI_GET_LENGTH, &size))
323 char buffer[1024];
325 memory_get_string(dbg_curr_process->handle, val_ptr,
326 lvalue->cookie == DLV_TARGET,
327 size == 2, buffer, sizeof(buffer));
328 dbg_printf("\"%s\"", buffer);
330 else dbg_printf("%p", val_ptr);
331 break;
332 case SymTagArrayType:
333 case SymTagUDT:
334 assert(lvalue->cookie == DLV_TARGET);
335 if (!memory_read_value(lvalue, sizeof(val_ptr), &val_ptr)) return;
336 dbg_printf("%p", val_ptr);
337 break;
338 case SymTagEnum:
340 BOOL ok = FALSE;
342 assert(lvalue->cookie == DLV_TARGET);
343 /* FIXME: it depends on underlying type for enums
344 * (not supported yet in dbghelp)
345 * Assuming 4 as for an int
347 if (!be_cpu->fetch_integer(lvalue, 4, TRUE, &val_int)) return;
349 if (types_get_info(&lvalue->type, TI_GET_CHILDRENCOUNT, &count))
351 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
352 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
353 WCHAR* ptr;
354 char tmp[256];
355 VARIANT variant;
356 int i;
357 struct dbg_type type;
359 fcp->Start = 0;
360 while (count)
362 fcp->Count = min(count, 256);
363 if (types_get_info(&lvalue->type, TI_FINDCHILDREN, fcp))
365 type.module = linear;
366 for (i = 0; i < min(fcp->Count, count); i++)
368 type.id = fcp->ChildId[i];
369 if (!types_get_info(&type, TI_GET_VALUE, &variant))
370 continue;
371 switch (variant.n1.n2.vt)
373 case VT_I4: ok = (val_int == variant.n1.n2.n3.lVal); break;
374 default: WINE_FIXME("Unsupported variant type (%u)\n", variant.n1.n2.vt);
376 if (ok)
378 ptr = NULL;
379 types_get_info(&type, TI_GET_SYMNAME, &ptr);
380 if (!ptr) continue;
381 WideCharToMultiByte(CP_ACP, 0, ptr, -1, tmp, sizeof(tmp), NULL, NULL);
382 HeapFree(GetProcessHeap(), 0, ptr);
383 dbg_printf("%s", tmp);
384 count = 0; /* so that we'll get away from outter loop */
385 break;
390 count -= min(count, 256);
391 fcp->Start += 256;
393 if (!ok) dbg_printf("%lld", val_int);
395 break;
396 default:
397 WINE_FIXME("Unsupported tag %lu\n", tag);
398 break;
402 /***********************************************************************
403 * print_basic
405 * Implementation of the 'print' command.
407 void print_basic(const struct dbg_lvalue* lvalue, int count, char format)
409 long int res;
411 if (lvalue->type.id == dbg_itype_none)
413 dbg_printf("Unable to evaluate expression\n");
414 return;
417 res = types_extract_as_integer(lvalue);
419 /* FIXME: this implies i386 byte ordering */
420 switch (format)
422 case 'x':
423 if (lvalue->addr.Mode != AddrModeFlat)
424 dbg_printf("0x%04lx", res);
425 else
426 dbg_printf("0x%08lx", res);
427 break;
429 case 'd':
430 dbg_printf("%ld\n", res);
431 break;
433 case 'c':
434 dbg_printf("%d = '%c'", (char)(res & 0xff), (char)(res & 0xff));
435 break;
437 case 'u':
439 WCHAR wch = (WCHAR)(res & 0xFFFF);
440 dbg_printf("%d = '", wch);
441 dbg_outputW(&wch, 1);
442 dbg_printf("'");
444 break;
446 case 'i':
447 case 's':
448 case 'w':
449 case 'b':
450 dbg_printf("Format specifier '%c' is meaningless in 'print' command\n", format);
451 case 0:
452 print_typed_basic(lvalue);
453 break;
457 void print_bare_address(const ADDRESS* addr)
459 switch (addr->Mode)
461 case AddrModeFlat:
462 dbg_printf("0x%08lx", addr->Offset);
463 break;
464 case AddrModeReal:
465 case AddrMode1616:
466 dbg_printf("0x%04x:0x%04lx", addr->Segment, addr->Offset);
467 break;
468 case AddrMode1632:
469 dbg_printf("0x%04x:0x%08lx", addr->Segment, addr->Offset);
470 break;
471 default:
472 dbg_printf("Unknown mode %x\n", addr->Mode);
473 break;
477 /***********************************************************************
478 * print_address
480 * Print an 16- or 32-bit address, with the nearest symbol if any.
482 void print_address(const ADDRESS* addr, BOOLEAN with_line)
484 char buffer[sizeof(SYMBOL_INFO) + 256];
485 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
486 void* lin = memory_to_linear_addr(addr);
488 print_bare_address(addr);
490 si->SizeOfStruct = sizeof(*si);
491 si->MaxNameLen = 256;
492 if (!SymFromAddr(dbg_curr_process->handle, (unsigned long)lin, NULL, si)) return;
493 dbg_printf(" %s", si->Name);
494 if (with_line)
496 IMAGEHLP_LINE il;
497 IMAGEHLP_MODULE im;
499 il.SizeOfStruct = sizeof(il);
500 if (SymGetLineFromAddr(dbg_curr_process->handle, (unsigned long)lin, NULL, &il))
501 dbg_printf(" [%s:%lu]", il.FileName, il.LineNumber);
502 im.SizeOfStruct = sizeof(im);
503 if (SymGetModuleInfo(dbg_curr_process->handle, (unsigned long)lin, &im))
504 dbg_printf(" in %s", im.ModuleName);
508 struct sym_enum
510 char* tmp;
511 DWORD frame;
514 static BOOL WINAPI sym_enum_cb(SYMBOL_INFO* sym_info, ULONG size, void* user)
516 struct sym_enum* se = (struct sym_enum*)user;
517 DWORD addr;
518 unsigned val;
519 long offset;
521 if ((sym_info->Flags & (SYMFLAG_PARAMETER|SYMFLAG_FRAMEREL)) == (SYMFLAG_PARAMETER|SYMFLAG_FRAMEREL))
523 struct dbg_type type;
525 if (se->tmp[0]) strcat(se->tmp, ", ");
526 addr = se->frame;
527 type.module = sym_info->ModBase;
528 type.id = sym_info->TypeIndex;
529 types_get_info(&type, TI_GET_OFFSET, &offset);
530 addr += offset;
531 dbg_read_memory_verbose((char*)addr, &val, sizeof(val));
532 sprintf(se->tmp + strlen(se->tmp), "%s=0x%x", sym_info->Name, val);
534 return TRUE;
537 void print_addr_and_args(const ADDRESS* pc, const ADDRESS* frame)
539 char buffer[sizeof(SYMBOL_INFO) + 256];
540 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
541 IMAGEHLP_STACK_FRAME isf;
542 IMAGEHLP_LINE il;
543 IMAGEHLP_MODULE im;
544 struct sym_enum se;
545 char tmp[1024];
546 DWORD disp;
548 if (pc->Mode != AddrModeFlat)
549 dbg_printf("0x%04x:0x%04lx", pc->Segment, pc->Offset);
550 else
551 dbg_printf("0x%08lx", pc->Offset);
553 isf.InstructionOffset = (unsigned long)memory_to_linear_addr(pc);
554 isf.FrameOffset = (unsigned long)memory_to_linear_addr(frame);
556 si->SizeOfStruct = sizeof(*si);
557 si->MaxNameLen = 256;
558 if (!SymFromAddr(dbg_curr_process->handle, isf.InstructionOffset, &disp, si))
559 return;
561 dbg_printf(" %s", si->Name);
562 if (disp) dbg_printf("+0x%lx", disp);
564 SymSetContext(dbg_curr_process->handle, &isf, NULL);
565 se.tmp = tmp;
566 se.frame = isf.FrameOffset;
567 tmp[0] = '\0';
568 SymEnumSymbols(dbg_curr_process->handle, 0, NULL, sym_enum_cb, &se);
569 if (tmp[0]) dbg_printf("(%s)", tmp);
571 il.SizeOfStruct = sizeof(il);
572 if (SymGetLineFromAddr(dbg_curr_process->handle, isf.InstructionOffset,
573 NULL, &il))
574 dbg_printf(" [%s:%lu]", il.FileName, il.LineNumber);
575 im.SizeOfStruct = sizeof(im);
576 if (SymGetModuleInfo(dbg_curr_process->handle, isf.InstructionOffset, &im))
577 dbg_printf(" in %s", im.ModuleName);
580 BOOL memory_disasm_one_insn(ADDRESS* addr)
582 char ch;
584 print_address(addr, TRUE);
585 dbg_printf(": ");
586 if (!dbg_read_memory(memory_to_linear_addr(addr), &ch, sizeof(ch)))
588 dbg_printf("-- no code accessible --\n");
589 return FALSE;
591 be_cpu->disasm_one_insn(addr, TRUE);
592 dbg_printf("\n");
593 return TRUE;
596 void memory_disassemble(const struct dbg_lvalue* xstart,
597 const struct dbg_lvalue* xend, int offset)
599 static ADDRESS last = {0,0,0};
601 if (!xstart && !xend)
603 if (!last.Segment && !last.Offset) memory_get_current_pc(&last);
605 else
607 if (xstart)
609 last.Mode = AddrModeFlat;
610 last.Offset = types_extract_as_integer(xstart);
612 if (xend) offset = types_extract_as_integer(xend) - last.Offset + 1;
614 while (offset-- > 0 && memory_disasm_one_insn(&last));