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
24 #include "wine/port.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
;
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
)
80 address
.Mode
= AddrModeFlat
;
82 address
.Offset
= (unsigned long)addr
;
83 dbg_printf("*** Invalid address ");
84 print_address(&address
, FALSE
);
88 /***********************************************************************
91 * Read a memory value.
93 BOOL
memory_read_value(const struct dbg_lvalue
* lvalue
, DWORD size
, void* result
)
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
);
105 if (lvalue
->addr
.Offset
)
107 memcpy(result
, (void*)(DWORD_PTR
)lvalue
->addr
.Offset
, size
);
114 /***********************************************************************
117 * Store a value in memory.
119 BOOL
memory_write_value(const struct dbg_lvalue
* lvalue
, DWORD size
, void* value
)
125 types_get_info(&lvalue
->type
, TI_GET_LENGTH
, &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
);
137 memcpy((void*)(DWORD_PTR
)lvalue
->addr
.Offset
, value
, size
);
142 /***********************************************************************
145 * Implementation of the 'x' command.
147 void memory_examine(const struct dbg_lvalue
*lvalue
, int count
, char format
)
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
);
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
);
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
);
178 while (count
-- && memory_disasm_one_insn(&addr
));
184 if (!dbg_read_memory(linear
, &guid
, sizeof(guid
)))
186 memory_report_invalid_addr(linear
);
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
);
197 print_address(&addr
, FALSE
);
203 #define DO_DUMP2(_t,_l,_f,_vv) { \
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) \
214 print_address(&addr, FALSE); \
221 #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
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);
231 BOOL
memory_get_string(struct dbg_process
* pcs
, void* addr
, BOOL in_debuggee
,
232 BOOL unicode
, char* buffer
, int size
)
238 if (!addr
) return FALSE
;
243 if (!unicode
) ret
= pcs
->process_io
->read(pcs
->handle
, addr
, buffer
, size
, &sz
);
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
,
250 HeapFree(GetProcessHeap(), 0, buffW
);
252 if (size
) buffer
[size
-1] = 0;
257 lstrcpynA(buffer
, addr
, size
);
262 BOOL
memory_get_string_indirect(struct dbg_process
* pcs
, void* addr
, BOOL unicode
, char* buffer
, int size
)
269 pcs
->process_io
->read(pcs
->handle
, addr
, &ad
, sizeof(ad
), &sz
) && sz
== sizeof(ad
) && ad
)
271 return memory_get_string(pcs
, ad
, TRUE
, unicode
, buffer
, size
);
277 * Convert an address offset to hex string. If mode == 32, treat offset as
278 * 32 bits (discard upper 32 bits), if mode == 64 use all 64 bits, if mode == 0
279 * treat as either 32 or 64 bits, depending on whether we're running as
282 char* memory_offset_to_string(char *str
, DWORD64 offset
, unsigned mode
)
284 if (mode
!= 32 && mode
!= 64)
294 sprintf(str
, "0x%08x", (unsigned int) offset
);
296 sprintf(str
, "0x%08x%08x", (unsigned int)(offset
>> 32),
297 (unsigned int)offset
);
302 static void dbg_print_longlong(LONGLONG sv
, BOOL is_signed
)
304 char tmp
[24], *ptr
= tmp
+ sizeof(tmp
) - 1;
307 if (is_signed
&& sv
< 0) uv
= -sv
;
308 else { uv
= sv
; is_signed
= FALSE
; }
309 for (div
= 10; uv
; div
*= 10, uv
/= 10)
310 *--ptr
= '0' + (uv
% 10);
311 if (ptr
== tmp
+ sizeof(tmp
) - 1) *--ptr
= '0';
312 if (is_signed
) *--ptr
= '-';
313 dbg_printf("%s", ptr
);
316 static void print_typed_basic(const struct dbg_lvalue
* lvalue
)
320 long double val_real
;
322 DWORD tag
, size
, count
, bt
;
323 struct dbg_type type
= lvalue
->type
;
324 struct dbg_type sub_type
;
326 if (!types_get_real_type(&type
, &tag
)) return;
331 if (!types_get_info(&type
, TI_GET_LENGTH
, &size64
) ||
332 !types_get_info(&type
, TI_GET_BASETYPE
, &bt
))
334 WINE_ERR("Couldn't get information\n");
335 RaiseException(DEBUG_STATUS_INTERNAL_ERROR
, 0, 0, NULL
);
337 size
= (DWORD
)size64
;
342 if (!be_cpu
->fetch_integer(lvalue
, size
, TRUE
, &val_int
)) return;
343 dbg_print_longlong(val_int
, TRUE
);
347 if (!be_cpu
->fetch_integer(lvalue
, size
, FALSE
, &val_int
)) return;
348 dbg_print_longlong(val_int
, FALSE
);
351 if (!be_cpu
->fetch_float(lvalue
, size
, &val_real
)) return;
352 dbg_printf("%Lf", val_real
);
355 if (!be_cpu
->fetch_integer(lvalue
, size
, TRUE
, &val_int
)) return;
356 /* FIXME: should do the same for a Unicode character (size == 2) */
357 if (size
== 1 && (val_int
< 0x20 || val_int
> 0x80))
358 dbg_printf("%d", (int)val_int
);
360 dbg_printf("'%c'", (char)val_int
);
363 WINE_FIXME("Unsupported basetype %u\n", bt
);
367 case SymTagPointerType
:
368 if (!memory_read_value(lvalue
, sizeof(void*), &val_ptr
)) return;
370 sub_type
.module
= lvalue
->type
.module
;
371 if (!types_get_info(&type
, TI_GET_TYPE
, &sub_type
.id
) ||
372 sub_type
.id
== dbg_itype_none
)
374 dbg_printf("Internal symbol error: unable to access memory location %p", val_ptr
);
377 if (!types_get_real_type(&sub_type
, &tag
)) return;
379 if (types_get_info(&sub_type
, TI_GET_SYMTAG
, &tag
) && tag
== SymTagBaseType
&&
380 types_get_info(&sub_type
, TI_GET_BASETYPE
, &bt
) && bt
== btChar
&&
381 types_get_info(&sub_type
, TI_GET_LENGTH
, &size64
))
385 if (!val_ptr
) dbg_printf("0x0");
386 else if (memory_get_string(dbg_curr_process
, val_ptr
,
387 lvalue
->cookie
== DLV_TARGET
,
388 size64
== 2, buffer
, sizeof(buffer
)))
389 dbg_printf("\"%s\"", buffer
);
391 dbg_printf("*** invalid address %p ***", val_ptr
);
393 else dbg_printf("%p", val_ptr
);
395 case SymTagArrayType
:
397 if (!memory_read_value(lvalue
, sizeof(val_ptr
), &val_ptr
)) return;
398 dbg_printf("%p", val_ptr
);
404 /* FIXME: it depends on underlying type for enums
405 * (not supported yet in dbghelp)
406 * Assuming 4 as for an int
408 if (!be_cpu
->fetch_integer(lvalue
, 4, TRUE
, &val_int
)) return;
410 if (types_get_info(&type
, TI_GET_CHILDRENCOUNT
, &count
))
412 char buffer
[sizeof(TI_FINDCHILDREN_PARAMS
) + 256 * sizeof(DWORD
)];
413 TI_FINDCHILDREN_PARAMS
* fcp
= (TI_FINDCHILDREN_PARAMS
*)buffer
;
422 fcp
->Count
= min(count
, 256);
423 if (types_get_info(&type
, TI_FINDCHILDREN
, fcp
))
425 sub_type
.module
= type
.module
;
426 for (i
= 0; i
< min(fcp
->Count
, count
); i
++)
428 sub_type
.id
= fcp
->ChildId
[i
];
429 if (!types_get_info(&sub_type
, TI_GET_VALUE
, &variant
))
431 switch (variant
.n1
.n2
.vt
)
433 case VT_I4
: ok
= (val_int
== variant
.n1
.n2
.n3
.lVal
); break;
434 default: WINE_FIXME("Unsupported variant type (%u)\n", variant
.n1
.n2
.vt
);
439 types_get_info(&sub_type
, TI_GET_SYMNAME
, &ptr
);
441 WideCharToMultiByte(CP_ACP
, 0, ptr
, -1, tmp
, sizeof(tmp
), NULL
, NULL
);
442 HeapFree(GetProcessHeap(), 0, ptr
);
443 dbg_printf("%s", tmp
);
444 count
= 0; /* so that we'll get away from outter loop */
450 count
-= min(count
, 256);
453 if (!ok
) dbg_print_longlong(val_int
, TRUE
);
457 WINE_FIXME("Unsupported tag %u\n", tag
);
462 /***********************************************************************
465 * Implementation of the 'print' command.
467 void print_basic(const struct dbg_lvalue
* lvalue
, int count
, char format
)
471 if (lvalue
->type
.id
== dbg_itype_none
)
473 dbg_printf("Unable to evaluate expression\n");
477 res
= types_extract_as_longlong(lvalue
);
479 /* FIXME: this implies i386 byte ordering */
483 dbg_printf("0x%x", (DWORD
)(ULONG64
)res
);
487 dbg_print_longlong(res
, TRUE
);
492 dbg_printf("%d = '%c'", (char)(res
& 0xff), (char)(res
& 0xff));
497 WCHAR wch
= (WCHAR
)(res
& 0xFFFF);
498 dbg_printf("%d = '", wch
);
499 dbg_outputW(&wch
, 1);
508 dbg_printf("Format specifier '%c' is meaningless in 'print' command\n", format
);
510 if (lvalue
->type
.id
== dbg_itype_segptr
)
512 dbg_print_longlong(res
, TRUE
);
516 print_typed_basic(lvalue
);
521 void print_bare_address(const ADDRESS64
* addr
)
523 char hexbuf
[MAX_OFFSET_TO_STR_LEN
];
528 dbg_printf("%s", memory_offset_to_string(hexbuf
, addr
->Offset
, 0));
532 dbg_printf("0x%04x:0x%04x", addr
->Segment
, (unsigned) addr
->Offset
);
535 dbg_printf("0x%04x:%s", addr
->Segment
,
536 memory_offset_to_string(hexbuf
, addr
->Offset
, 32));
539 dbg_printf("Unknown mode %x\n", addr
->Mode
);
544 /***********************************************************************
547 * Print an 16- or 32-bit address, with the nearest symbol if any.
549 void print_address(const ADDRESS64
* addr
, BOOLEAN with_line
)
551 char buffer
[sizeof(SYMBOL_INFO
) + 256];
552 SYMBOL_INFO
* si
= (SYMBOL_INFO
*)buffer
;
553 void* lin
= memory_to_linear_addr(addr
);
557 print_bare_address(addr
);
559 si
->SizeOfStruct
= sizeof(*si
);
560 si
->MaxNameLen
= 256;
561 if (!SymFromAddr(dbg_curr_process
->handle
, (DWORD_PTR
)lin
, &disp64
, si
)) return;
562 dbg_printf(" %s", si
->Name
);
563 if (disp64
) dbg_printf("+0x%lx", (DWORD_PTR
)disp64
);
569 il
.SizeOfStruct
= sizeof(il
);
570 if (SymGetLineFromAddr(dbg_curr_process
->handle
, (DWORD_PTR
)lin
, &disp
, &il
))
571 dbg_printf(" [%s:%u]", il
.FileName
, il
.LineNumber
);
572 im
.SizeOfStruct
= sizeof(im
);
573 if (SymGetModuleInfo(dbg_curr_process
->handle
, (DWORD_PTR
)lin
, &im
))
574 dbg_printf(" in %s", im
.ModuleName
);
578 BOOL
memory_disasm_one_insn(ADDRESS64
* addr
)
582 print_address(addr
, TRUE
);
584 if (!dbg_read_memory(memory_to_linear_addr(addr
), &ch
, sizeof(ch
)))
586 dbg_printf("-- no code accessible --\n");
589 be_cpu
->disasm_one_insn(addr
, TRUE
);
594 void memory_disassemble(const struct dbg_lvalue
* xstart
,
595 const struct dbg_lvalue
* xend
, int instruction_count
)
597 static ADDRESS64 last
= {0,0,0};
601 if (!xstart
&& !xend
)
603 if (!last
.Segment
&& !last
.Offset
) memory_get_current_pc(&last
);
608 types_extract_as_address(xstart
, &last
);
610 stop
= types_extract_as_integer(xend
);
612 for (i
= 0; (instruction_count
== 0 || i
< instruction_count
) &&
613 (stop
== 0 || last
.Offset
<= stop
); i
++)
614 memory_disasm_one_insn(&last
);
617 BOOL
memory_get_register(DWORD regno
, DWORD
** value
, char* buffer
, int len
)
619 const struct dbg_internal_var
* div
;
621 /* negative register values are wine's dbghelp hacks
622 * see dlls/dbghelp/dbghelp_internal.h for the details
627 if (buffer
) snprintf(buffer
, len
, "<internal error>");
630 if (buffer
) snprintf(buffer
, len
, "<couldn't compute location>");
633 if (buffer
) snprintf(buffer
, len
, "<is not available>");
636 if (buffer
) snprintf(buffer
, len
, "<couldn't read memory>");
640 for (div
= dbg_context_vars
; div
->name
; div
++)
642 if (div
->val
== regno
)
644 if (dbg_curr_thread
->curr_frame
!= 0)
646 if (!stack_get_register_current_frame(regno
, value
))
648 if (buffer
) snprintf(buffer
, len
, "<register %s not in topmost frame>", div
->name
);
655 snprintf(buffer
, len
, div
->name
);
659 if (buffer
) snprintf(buffer
, len
, "<unknown register %u>", regno
);