/****************************************************************** * stack_fetch_frames * * Do a backtrace on the current thread */ unsigned stack_fetch_frames(const CONTEXT* _ctx) { STACKFRAME64 sf; unsigned nf = 0; /* as native stackwalk can modify the context passed to it, simply copy * it to avoid any damage */ CONTEXT ctx = *_ctx, prevctx = ctx; HeapFree(GetProcessHeap(), 0, dbg_curr_thread->frames); dbg_curr_thread->frames = NULL; memset(&sf, 0, sizeof(sf)); be_cpu->get_addr(dbg_curr_thread->handle, &ctx, be_cpu_addr_frame, &sf.AddrFrame); be_cpu->get_addr(dbg_curr_thread->handle, &ctx, be_cpu_addr_pc, &sf.AddrPC); be_cpu->get_addr(dbg_curr_thread->handle, &ctx, be_cpu_addr_stack, &sf.AddrStack); /* don't confuse StackWalk by passing in inconsistent addresses */ if ((sf.AddrPC.Mode == AddrModeFlat) && (sf.AddrFrame.Mode != AddrModeFlat)) { sf.AddrFrame.Offset = (ULONG_PTR)memory_to_linear_addr(&sf.AddrFrame); sf.AddrFrame.Mode = AddrModeFlat; } while (StackWalk64(be_cpu->machine, dbg_curr_process->handle, dbg_curr_thread->handle, &sf, &ctx, stack_read_mem, SymFunctionTableAccess64, SymGetModuleBase64, NULL)) { dbg_curr_thread->frames = dbg_heap_realloc(dbg_curr_thread->frames, (nf + 1) * sizeof(dbg_curr_thread->frames[0])); dbg_curr_thread->frames[nf].addr_pc = sf.AddrPC; dbg_curr_thread->frames[nf].linear_pc = (DWORD_PTR)memory_to_linear_addr(&sf.AddrPC); dbg_curr_thread->frames[nf].addr_frame = sf.AddrFrame; dbg_curr_thread->frames[nf].linear_frame = (DWORD_PTR)memory_to_linear_addr(&sf.AddrFrame); dbg_curr_thread->frames[nf].addr_stack = sf.AddrStack; dbg_curr_thread->frames[nf].linear_stack = (DWORD_PTR)memory_to_linear_addr(&sf.AddrStack); dbg_curr_thread->frames[nf].context = prevctx; /* FIXME: can this heuristic be improved: we declare first context always valid, and next ones * if it has been modified by the call to StackWalk... */ dbg_curr_thread->frames[nf].is_ctx_valid = (nf == 0 || (dbg_curr_thread->frames[nf - 1].is_ctx_valid && memcmp(&dbg_curr_thread->frames[nf - 1].context, &ctx, sizeof(ctx)))); prevctx = ctx; nf++; /* we've probably gotten ourselves into an infinite loop so bail */ if (nf > 200) break; } dbg_curr_thread->curr_frame = -1; dbg_curr_thread->num_frames = nf; stack_set_frame_internal(0); return nf; }
/*********************************************************************** * find_xpoint * * Find the breakpoint for a given address. Return the breakpoint * number or -1 if none. */ static int find_xpoint(const ADDRESS64* addr, enum be_xpoint_type type) { int i; void* lin = memory_to_linear_addr(addr); struct dbg_breakpoint* bp = dbg_curr_process->bp; for (i = 0; i < dbg_curr_process->next_bp; i++) { if (bp[i].refcount && bp[i].enabled && bp[i].xpoint_type == type && memory_to_linear_addr(&bp[i].addr) == lin) return i; } return -1; }
/****************************************************************** * break_delete_xpoints_from_module * * Remove all Xpoints from module which base is 'base' */ void break_delete_xpoints_from_module(DWORD64 base) { IMAGEHLP_MODULE64 im, im_elf; int i; DWORD_PTR linear; struct dbg_breakpoint* bp = dbg_curr_process->bp; /* FIXME: should do it also on the ELF sibling if any */ im.SizeOfStruct = sizeof(im); im_elf.SizeOfStruct = sizeof(im_elf); if (!SymGetModuleInfo64(dbg_curr_process->handle, base, &im)) return; /* try to get in fact the underlying ELF module (if any) */ if (SymGetModuleInfo64(dbg_curr_process->handle, im.BaseOfImage - 1, &im_elf) && im_elf.BaseOfImage <= im.BaseOfImage && im_elf.BaseOfImage + im_elf.ImageSize >= im.BaseOfImage + im.ImageSize) im = im_elf; for (i = 0; i < dbg_curr_process->next_bp; i++) { if (bp[i].refcount && bp[i].enabled) { linear = (DWORD_PTR)memory_to_linear_addr(&bp[i].addr); if (im.BaseOfImage <= linear && linear < im.BaseOfImage + im.ImageSize) { break_delete_xpoint(i); } } } }
/*********************************************************************** * print_address * * Print an 16- or 32-bit address, with the nearest symbol if any. */ void print_address(const ADDRESS64* addr, BOOLEAN with_line) { char buffer[sizeof(SYMBOL_INFO) + 256]; SYMBOL_INFO* si = (SYMBOL_INFO*)buffer; void* lin = memory_to_linear_addr(addr); DWORD64 disp64; DWORD disp; print_bare_address(addr); si->SizeOfStruct = sizeof(*si); si->MaxNameLen = 256; if (!SymFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp64, si)) return; dbg_printf(" %s", si->Name); if (disp64) dbg_printf("+0x%lx", (DWORD_PTR)disp64); if (with_line) { IMAGEHLP_LINE il; IMAGEHLP_MODULE im; il.SizeOfStruct = sizeof(il); if (SymGetLineFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp, &il)) dbg_printf(" [%s:%lu]", il.FileName, il.LineNumber); im.SizeOfStruct = sizeof(im); if (SymGetModuleInfo(dbg_curr_process->handle, (DWORD_PTR)lin, &im)) dbg_printf(" in %s", im.ModuleName); } }
BOOL stack_set_frame(int newframe) { ADDRESS64 addr; if (!stack_set_frame_internal(newframe)) return FALSE; addr.Mode = AddrModeFlat; addr.Offset = (DWORD_PTR)memory_to_linear_addr(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_pc); source_list_from_addr(&addr, 0); return TRUE; }
/*********************************************************************** * symbol_get_function_line_status * * Find the symbol nearest to a given address. */ enum dbg_line_status symbol_get_function_line_status(const ADDRESS64* addr) { IMAGEHLP_LINE64 il; DWORD disp; ULONG64 disp64, start; DWORD_PTR lin = (DWORD_PTR)memory_to_linear_addr(addr); char buffer[sizeof(SYMBOL_INFO) + 256]; SYMBOL_INFO* sym = (SYMBOL_INFO*)buffer; struct dbg_type func; il.SizeOfStruct = sizeof(il); sym->SizeOfStruct = sizeof(SYMBOL_INFO); sym->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO); /* do we have some info for lin address ? */ if (!SymFromAddr(dbg_curr_process->handle, lin, &disp64, sym)) { ADDRESS64 jumpee; /* some compilers insert thunks in their code without debug info associated * take care of this situation */ if (be_cpu->is_jump((void*)lin, &jumpee)) return symbol_get_function_line_status(&jumpee); return dbg_no_line_info; } switch (sym->Tag) { case SymTagThunk: /* FIXME: so far dbghelp doesn't return the 16 <=> 32 thunks * and furthermore, we no longer take care of them !!! */ return dbg_in_a_thunk; case SymTagFunction: case SymTagPublicSymbol: break; default: WINE_FIXME("Unexpected sym-tag 0x%08x\n", sym->Tag); case SymTagData: return dbg_no_line_info; } /* we should have a function now */ if (!SymGetLineFromAddr64(dbg_curr_process->handle, lin, &disp, &il)) return dbg_no_line_info; func.module = sym->ModBase; func.id = sym->info; if (symbol_get_debug_start(&func, &start) && lin < start) return dbg_not_on_a_line_number; if (!sym->Size) sym->Size = 0x100000; if (il.FileName && il.FileName[0] && disp < sym->Size) return (disp == 0) ? dbg_on_a_line_number : dbg_not_on_a_line_number; return dbg_no_line_info; }
BOOL memory_disasm_one_insn(ADDRESS64* addr) { char ch; print_address(addr, TRUE); dbg_printf(": "); if (!dbg_read_memory(memory_to_linear_addr(addr), &ch, sizeof(ch))) { dbg_printf("-- no code accessible --\n"); return FALSE; } be_cpu->disasm_one_insn(addr, TRUE); dbg_printf("\n"); return TRUE; }
/*********************************************************************** * get_watched_value * * Returns the value watched by watch point 'num'. */ static BOOL get_watched_value(int num, DWORD64* val) { DWORD64 buf[1]; if (!dbg_read_memory(memory_to_linear_addr(&dbg_curr_process->bp[num].addr), buf, dbg_curr_process->bp[num].w.len + 1)) return FALSE; switch (dbg_curr_process->bp[num].w.len + 1) { case 8: *val = *(DWORD64*)buf; break; case 4: *val = *(DWORD*)buf; break; case 2: *val = *(WORD*)buf; break; case 1: *val = *(BYTE*)buf; break; default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL); } return TRUE; }
void source_list_from_addr(const ADDRESS64* addr, int nlines) { IMAGEHLP_LINE il; ADDRESS64 la; DWORD disp; if (!addr) { memory_get_current_pc(&la); addr = &la; } il.SizeOfStruct = sizeof(il); if (SymGetLineFromAddr(dbg_curr_process->handle, (unsigned long)memory_to_linear_addr(addr), &disp, &il)) source_list(&il, NULL, nlines); }
/*********************************************************************** * memory_read_value * * Read a memory value. */ BOOL memory_read_value(const struct dbg_lvalue* lvalue, DWORD size, void* result) { BOOL ret = FALSE; if (lvalue->cookie == DLV_TARGET) { void* linear = memory_to_linear_addr(&lvalue->addr); if (!(ret = dbg_read_memory(linear, result, size))) memory_report_invalid_addr(linear); } else { if (lvalue->addr.Offset) { memcpy(result, (void*)(DWORD_PTR)lvalue->addr.Offset, size); ret = TRUE; } } return TRUE; }
/*********************************************************************** * break_set_xpoints * * Set or remove all the breakpoints & watchpoints */ void break_set_xpoints(BOOL set) { static BOOL last; /* = 0 = FALSE */ unsigned int i, ret, size; void* addr; struct dbg_breakpoint* bp = dbg_curr_process->bp; if (set == last) return; last = set; for (i = 0; i < dbg_curr_process->next_bp; i++) { if (!bp[i].refcount || !bp[i].enabled) continue; if (is_xpoint_break(i)) size = 0; else size = bp[i].w.len + 1; addr = memory_to_linear_addr(&bp[i].addr); if (set) ret = be_cpu->insert_Xpoint(dbg_curr_process->handle, dbg_curr_process->process_io, &dbg_context, bp[i].xpoint_type, addr, &bp[i].info, size); else ret = be_cpu->remove_Xpoint(dbg_curr_process->handle, dbg_curr_process->process_io, &dbg_context, bp[i].xpoint_type, addr, bp[i].info, size); if (!ret) { dbg_printf("Invalid address ("); print_address(&bp[i].addr, FALSE); dbg_printf(") for breakpoint %d, disabling it\n", i); bp[i].enabled = FALSE; } } }
/*********************************************************************** * memory_write_value * * Store a value in memory. */ BOOL memory_write_value(const struct dbg_lvalue* lvalue, DWORD size, void* value) { BOOL ret = TRUE; DWORD64 os; os = ~(DWORD64)size; types_get_info(&lvalue->type, TI_GET_LENGTH, &os); assert(size == os); /* FIXME: only works on little endian systems */ if (lvalue->cookie == DLV_TARGET) { void* linear = memory_to_linear_addr(&lvalue->addr); if (!(ret = dbg_write_memory(linear, value, size))) memory_report_invalid_addr(linear); } else { memcpy((void*)(DWORD_PTR)lvalue->addr.Offset, value, size); } return ret; }
/*********************************************************************** * break_add_break * * Add a breakpoint. */ BOOL break_add_break(const ADDRESS64* addr, BOOL verbose, BOOL swbp) { int num; BYTE ch; struct dbg_breakpoint* bp = dbg_curr_process->bp; int type = swbp ? be_xpoint_break : be_xpoint_watch_exec; if ((num = find_xpoint(addr, type)) >= 1) { bp[num].refcount++; dbg_printf("Breakpoint %d at ", num); print_address(&bp[num].addr, TRUE); dbg_printf(" (refcount=%d)\n", bp[num].refcount); return TRUE; } if (!dbg_read_memory(memory_to_linear_addr(addr), &ch, sizeof(ch))) { if (verbose) { dbg_printf("Invalid address "); print_bare_address(addr); dbg_printf(", can't set breakpoint\n"); } return FALSE; } if ((num = init_xpoint(type, addr)) == -1) return FALSE; dbg_printf("Breakpoint %d at ", num); print_address(&bp[num].addr, TRUE); dbg_printf("\n"); return TRUE; }
/*********************************************************************** * break_add_break_from_lineno * * Add a breakpoint from a line number in current file */ void break_add_break_from_lineno(const char *filename, int lineno, BOOL swbp) { struct cb_break_lineno bkln; bkln.addr.Offset = 0; bkln.lineno = lineno; if (!filename) { DWORD disp; ADDRESS64 curr; IMAGEHLP_LINE64 il; DWORD_PTR linear; memory_get_current_pc(&curr); linear = (DWORD_PTR)memory_to_linear_addr(&curr); il.SizeOfStruct = sizeof(il); if (!SymGetLineFromAddr64(dbg_curr_process->handle, linear, &disp, &il)) { dbg_printf("Unable to add breakpoint (unknown address %lx)\n", linear); return; } filename = il.FileName; SymEnumLines(dbg_curr_process->handle, linear, NULL, filename, line_cb, &bkln); } else { /* we have to enumerate across modules */ bkln.filename = filename; SymEnumerateModulesW64(dbg_curr_process->handle, mcb, &bkln); } if (bkln.addr.Offset) break_add_break(&bkln.addr, TRUE, swbp); else dbg_printf("Unknown line number\n" "(either out of file, or no code at given line number)\n"); }
/****************************************************************** * types_extract_as_integer * * Given a lvalue, try to get an integral (or pointer/address) value * out of it */ long int types_extract_as_integer(const struct dbg_lvalue* lvalue) { long int rtn; LONGLONG val; DWORD tag, bt; DWORD64 size; struct dbg_type type = lvalue->type; if (!types_get_real_type(&type, &tag)) RaiseException(DEBUG_STATUS_NOT_AN_INTEGER, 0, 0, NULL); if (type.id == dbg_itype_segptr) { return (long int)memory_to_linear_addr(&lvalue->addr); } switch (tag) { case SymTagBaseType: if (!types_get_info(&type, TI_GET_LENGTH, &size) || !types_get_info(&type, TI_GET_BASETYPE, &bt)) { WINE_ERR("Couldn't get information\n"); RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL); } if (size > sizeof(rtn)) { WINE_ERR("Size too large (%s)\n", wine_dbgstr_longlong(size)); RaiseException(DEBUG_STATUS_NOT_AN_INTEGER, 0, 0, NULL); } switch (bt) { case btChar: case btInt: if (!be_cpu->fetch_integer(lvalue, (unsigned)size, TRUE, &val)) RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL); rtn = (long)val; break; case btUInt: if (!be_cpu->fetch_integer(lvalue, (unsigned)size, FALSE, &val)) RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL); rtn = (DWORD)(DWORD64)val; break; case btFloat: RaiseException(DEBUG_STATUS_NOT_AN_INTEGER, 0, 0, NULL); } break; case SymTagPointerType: if (!memory_read_value(lvalue, sizeof(void*), &rtn)) RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL); break; case SymTagArrayType: case SymTagUDT: assert(lvalue->cookie == DLV_TARGET); if (!memory_read_value(lvalue, sizeof(rtn), &rtn)) RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL); break; case SymTagEnum: assert(lvalue->cookie == DLV_TARGET); if (!memory_read_value(lvalue, sizeof(rtn), &rtn)) RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL); break; case SymTagFunctionType: rtn = (unsigned)memory_to_linear_addr(&lvalue->addr); break; default: WINE_FIXME("Unsupported tag %lu\n", tag); RaiseException(DEBUG_STATUS_NOT_AN_INTEGER, 0, 0, NULL); break; } return rtn; }
/****************************************************************** * types_extract_as_longlong * * Given a lvalue, try to get an integral (or pointer/address) value * out of it */ LONGLONG types_extract_as_longlong(const struct dbg_lvalue* lvalue, unsigned* psize, BOOL *issigned) { LONGLONG rtn; DWORD tag, bt; DWORD64 size; struct dbg_type type = lvalue->type; BOOL s = FALSE; if (!types_get_real_type(&type, &tag)) RaiseException(DEBUG_STATUS_NOT_AN_INTEGER, 0, 0, NULL); if (type.id == dbg_itype_segptr) { return (LONG_PTR)memory_to_linear_addr(&lvalue->addr); } if (psize) *psize = 0; if (issigned) *issigned = FALSE; switch (tag) { case SymTagBaseType: if (!types_get_info(&type, TI_GET_LENGTH, &size) || !types_get_info(&type, TI_GET_BASETYPE, &bt)) { WINE_ERR("Couldn't get information\n"); RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL); } if (size > sizeof(rtn)) { WINE_ERR("Size too large (%s)\n", wine_dbgstr_longlong(size)); RaiseException(DEBUG_STATUS_NOT_AN_INTEGER, 0, 0, NULL); } switch (bt) { case btChar: case btInt: if (!be_cpu->fetch_integer(lvalue, (unsigned)size, s = TRUE, &rtn)) RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL); break; case btUInt: if (!be_cpu->fetch_integer(lvalue, (unsigned)size, s = FALSE, &rtn)) RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL); break; case btFloat: RaiseException(DEBUG_STATUS_NOT_AN_INTEGER, 0, 0, NULL); } if (psize) *psize = (unsigned)size; if (issigned) *issigned = s; break; case SymTagPointerType: if (!be_cpu->fetch_integer(lvalue, sizeof(void*), s = FALSE, &rtn)) RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL); break; case SymTagArrayType: case SymTagUDT: if (!be_cpu->fetch_integer(lvalue, sizeof(unsigned), s = FALSE, &rtn)) RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL); break; case SymTagEnum: /* FIXME: we don't handle enum size */ if (!be_cpu->fetch_integer(lvalue, sizeof(unsigned), s = FALSE, &rtn)) RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL); break; case SymTagFunctionType: rtn = (ULONG_PTR)memory_to_linear_addr(&lvalue->addr); break; default: WINE_FIXME("Unsupported tag %u\n", tag); RaiseException(DEBUG_STATUS_NOT_AN_INTEGER, 0, 0, NULL); break; } return rtn; }
/*********************************************************************** * dbg_exception_prolog * * Examine exception and decide if interactive mode is entered(return TRUE) * or exception is silently continued(return FALSE) * is_debug means the exception is a breakpoint or single step exception */ static unsigned dbg_exception_prolog(BOOL is_debug, BOOL first_chance, const EXCEPTION_RECORD* rec) { ADDRESS64 addr; BOOL is_break; char hexbuf[MAX_OFFSET_TO_STR_LEN]; memory_get_current_pc(&addr); break_suspend_execution(); dbg_curr_thread->excpt_record = *rec; dbg_curr_thread->in_exception = TRUE; if (!is_debug) { switch (addr.Mode) { case AddrModeFlat: dbg_printf(" in 32-bit code (%s)", memory_offset_to_string(hexbuf, addr.Offset, 0)); break; case AddrModeReal: dbg_printf(" in vm86 code (%04x:%04x)", addr.Segment, (unsigned) addr.Offset); break; case AddrMode1616: dbg_printf(" in 16-bit code (%04x:%04x)", addr.Segment, (unsigned) addr.Offset); break; case AddrMode1632: dbg_printf(" in 32-bit code (%04x:%08lx)", addr.Segment, (unsigned long) addr.Offset); break; default: dbg_printf(" bad address"); } dbg_printf(".\n"); } /* this will resynchronize builtin dbghelp's internal ELF module list */ SymLoadModule(dbg_curr_process->handle, 0, 0, 0, 0, 0); if (is_debug) break_adjust_pc(&addr, rec->ExceptionCode, first_chance, &is_break); /* * Do a quiet backtrace so that we have an idea of what the situation * is WRT the source files. */ stack_fetch_frames(); if (is_debug && !is_break && break_should_continue(&addr, rec->ExceptionCode)) return FALSE; if (addr.Mode != dbg_curr_thread->addr_mode) { const char* name = NULL; switch (addr.Mode) { case AddrMode1616: name = "16 bit"; break; case AddrMode1632: name = "32 bit"; break; case AddrModeReal: name = "vm86"; break; case AddrModeFlat: name = "32 bit"; break; } dbg_printf("In %s mode.\n", name); dbg_curr_thread->addr_mode = addr.Mode; } display_print(); if (!is_debug) { /* This is a real crash, dump some info */ be_cpu->print_context(dbg_curr_thread->handle, &dbg_context, 0); stack_info(); be_cpu->print_segment_info(dbg_curr_thread->handle, &dbg_context); stack_backtrace(dbg_curr_tid); } else { static char* last_name; static char* last_file; char buffer[sizeof(SYMBOL_INFO) + 256]; SYMBOL_INFO* si = (SYMBOL_INFO*)buffer; void* lin = memory_to_linear_addr(&addr); DWORD64 disp64; IMAGEHLP_LINE il; DWORD disp; si->SizeOfStruct = sizeof(*si); si->MaxNameLen = 256; il.SizeOfStruct = sizeof(il); if (SymFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp64, si) && SymGetLineFromAddr(dbg_curr_process->handle, (DWORD_PTR)lin, &disp, &il)) { if ((!last_name || strcmp(last_name, si->Name)) || (!last_file || strcmp(last_file, il.FileName))) { HeapFree(GetProcessHeap(), 0, last_name); HeapFree(GetProcessHeap(), 0, last_file); last_name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(si->Name) + 1), si->Name); last_file = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(il.FileName) + 1), il.FileName); dbg_printf("%s () at %s:%u\n", last_name, last_file, il.LineNumber); } } } if (!is_debug || is_break || dbg_curr_thread->exec_mode == dbg_exec_step_over_insn || dbg_curr_thread->exec_mode == dbg_exec_step_into_insn) { ADDRESS64 tmp = addr; /* Show where we crashed */ memory_disasm_one_insn(&tmp); } source_list_from_addr(&addr, 0); return TRUE; }
/*********************************************************************** * memory_examine * * Implementation of the 'x' command. */ void memory_examine(const struct dbg_lvalue *lvalue, int count, char format) { int i; char buffer[256]; ADDRESS64 addr; void *linear; types_extract_as_address(lvalue, &addr); linear = memory_to_linear_addr(&addr); if (format != 'i' && count > 1) { print_address(&addr, FALSE); dbg_printf(": "); } switch (format) { case 'u': if (count == 1) count = 256; memory_get_string(dbg_curr_process, linear, TRUE, TRUE, buffer, min(count, sizeof(buffer))); dbg_printf("%s\n", buffer); return; case 's': if (count == 1) count = 256; memory_get_string(dbg_curr_process, linear, TRUE, FALSE, buffer, min(count, sizeof(buffer))); dbg_printf("%s\n", buffer); return; case 'i': while (count-- && memory_disasm_one_insn(&addr)); return; case 'g': while (count--) { GUID guid; if (!dbg_read_memory(linear, &guid, sizeof(guid))) { memory_report_invalid_addr(linear); break; } dbg_printf("{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n", guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]); linear = (char*)linear + sizeof(guid); addr.Offset += sizeof(guid); if (count) { print_address(&addr, FALSE); dbg_printf(": "); } } return; #define DO_DUMP2(_t,_l,_f,_vv) { \ _t _v; \ for (i = 0; i < count; i++) { \ if (!dbg_read_memory(linear, &_v, sizeof(_t))) \ { memory_report_invalid_addr(linear); break; } \ dbg_printf(_f, (_vv)); \ addr.Offset += sizeof(_t); \ linear = (char*)linear + sizeof(_t); \ if ((i % (_l)) == (_l) - 1 && i != count - 1) \ { \ dbg_printf("\n"); \ print_address(&addr, FALSE); \ dbg_printf(": "); \ } \ } \ dbg_printf("\n"); \ } \ return #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v) case 'x': DO_DUMP(int, 4, " %8.8x"); case 'd': DO_DUMP(unsigned int, 4, " %10d"); case 'w': DO_DUMP(unsigned short, 8, " %04x"); case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v); case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff); } }
/*********************************************************************** * symbol_get_line * * Find the symbol nearest to a given address. * Returns sourcefile name and line number in a format that the listing * handler can deal with. */ BOOL symbol_get_line(const char* filename, const char* name, IMAGEHLP_LINE* line) { struct sgv_data sgv; char buffer[512]; DWORD opt, disp, linear; unsigned i, found = FALSE; IMAGEHLP_LINE il; sgv.num = 0; sgv.num_thunks = 0; sgv.name = &buffer[2]; sgv.do_thunks = FALSE; buffer[0] = '*'; buffer[1] = '!'; strcpy(&buffer[2], name); /* this is a wine specific options to return also ELF modules in the * enumeration */ SymSetOptions((opt = SymGetOptions()) | 0x40000000); if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv)) { SymSetOptions(opt); return FALSE; } if (!sgv.num && (name[0] != '_')) { buffer[2] = '_'; strcpy(&buffer[3], name); if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv)) { SymSetOptions(opt); return FALSE; } } SymSetOptions(opt); for (i = 0; i < sgv.num; i++) { linear = (DWORD)memory_to_linear_addr(&sgv.syms[i].lvalue.addr); il.SizeOfStruct = sizeof(il); if (!SymGetLineFromAddr(dbg_curr_process->handle, linear, &disp, &il)) continue; if (filename && strcmp(line->FileName, filename)) continue; if (found) { WINE_FIXME("Several found, returning first (may not be what you want)...\n"); break; } found = TRUE; *line = il; } if (!found) { if (filename) dbg_printf("No such function %s in %s\n", name, filename); else dbg_printf("No such function %s\n", name); return FALSE; } return TRUE; }
/*********************************************************************** * symbol_get_lvalue * * Get the address of a named symbol. * Return values: * sglv_found: if the symbol is found * sglv_unknown: if the symbol isn't found * sglv_aborted: some error occurred (likely, many symbols of same name exist, * and user didn't pick one of them) */ enum sym_get_lval symbol_get_lvalue(const char* name, const int lineno, struct dbg_lvalue* rtn, BOOL bp_disp) { struct sgv_data sgv; int i; char buffer[512]; DWORD opt; IMAGEHLP_STACK_FRAME ihsf; if (strlen(name) + 4 > sizeof(buffer)) { WINE_WARN("Too long symbol (%s)\n", name); return sglv_unknown; } sgv.num = 0; sgv.num_thunks = 0; sgv.name = &buffer[2]; sgv.do_thunks = DBG_IVAR(AlwaysShowThunks); if (strchr(name, '!')) { strcpy(buffer, name); } else { buffer[0] = '*'; buffer[1] = '!'; strcpy(&buffer[2], name); } /* this is a wine specific options to return also ELF modules in the * enumeration */ SymSetOptions((opt = SymGetOptions()) | 0x40000000); SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv); if (!sgv.num) { const char* ptr = strchr(name, '!'); if ((ptr && ptr[1] != '_') || (!ptr && *name != '_')) { if (ptr) { int offset = ptr - name; memcpy(buffer, name, offset + 1); buffer[offset + 1] = '_'; strcpy(&buffer[offset + 2], ptr + 1); } else { buffer[0] = '*'; buffer[1] = '!'; buffer[2] = '_'; strcpy(&buffer[3], name); } SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv); } } SymSetOptions(opt); /* now grab local symbols */ if (stack_get_current_frame(&ihsf) && sgv.num < NUMDBGV) { sgv.frame_offset = ihsf.FrameOffset; SymEnumSymbols(dbg_curr_process->handle, 0, name, sgv_cb, (void*)&sgv); } if (!sgv.num) { dbg_printf("No symbols found for %s\n", name); return sglv_unknown; } /* recompute potential offsets for functions (linenumber, skip prolog) */ for (i = 0; i < sgv.num; i++) { if (sgv.syms[i].flags & (SYMFLAG_REGISTER|SYMFLAG_REGREL|SYMFLAG_LOCAL|SYMFLAG_THUNK)) continue; if (lineno == -1) { struct dbg_type type; ULONG64 addr; type.module = sgv.syms[i].lvalue.type.module; type.id = sgv.syms[i].sym_info; if (bp_disp && symbol_get_debug_start(&type, &addr)) sgv.syms[i].lvalue.addr.Offset = addr; } else { DWORD disp; IMAGEHLP_LINE il; BOOL found = FALSE; il.SizeOfStruct = sizeof(il); SymGetLineFromAddr(dbg_curr_process->handle, (DWORD)memory_to_linear_addr(&sgv.syms[i].lvalue.addr), &disp, &il); do { if (lineno == il.LineNumber) { sgv.syms[i].lvalue.addr.Offset = il.Address; found = TRUE; break; } } while (SymGetLineNext(dbg_curr_process->handle, &il)); if (!found) WINE_FIXME("No line (%d) found for %s (setting to symbol start)\n", lineno, name); } } i = 0; if (dbg_interactiveP) { if (sgv.num - sgv.num_thunks > 1 || /* many symbols non thunks (and showing only non thunks) */ (sgv.num > 1 && DBG_IVAR(AlwaysShowThunks)) || /* many symbols (showing symbols & thunks) */ (sgv.num == sgv.num_thunks && sgv.num_thunks > 1)) { dbg_printf("Many symbols with name '%s', " "choose the one you want (<cr> to abort):\n", name); for (i = 0; i < sgv.num; i++) { if (sgv.num - sgv.num_thunks > 1 && (sgv.syms[i].flags & SYMFLAG_THUNK) && !DBG_IVAR(AlwaysShowThunks)) continue; dbg_printf("[%d]: ", i + 1); if (sgv.syms[i].flags & SYMFLAG_LOCAL) { dbg_printf("%s %sof %s\n", sgv.syms[i].flags & SYMFLAG_PARAMETER ? "Parameter" : "Local variable", sgv.syms[i].flags & (SYMFLAG_REGISTER|SYMFLAG_REGREL) ? "(in a register) " : "", name); } else if (sgv.syms[i].flags & SYMFLAG_THUNK) { print_address(&sgv.syms[i].lvalue.addr, TRUE); /* FIXME: should display where the thunks points to */ dbg_printf(" thunk %s\n", name); } else { print_address(&sgv.syms[i].lvalue.addr, TRUE); dbg_printf("\n"); } } do { i = 0; if (input_read_line("=> ", buffer, sizeof(buffer))) { if (buffer[0] == '\0') return sglv_aborted; i = atoi(buffer); if (i < 1 || i > sgv.num) dbg_printf("Invalid choice %d\n", i); } else return sglv_aborted; } while (i < 1 || i > sgv.num); /* The array is 0-based, but the choices are 1..n, * so we have to subtract one before returning. */ i--; } } else { /* FIXME: could display the list of non-picked up symbols */ if (sgv.num > 1) dbg_printf("More than one symbol named %s, picking the first one\n", name); } *rtn = sgv.syms[i].lvalue; return sglv_found; }
/*********************************************************************** * symbol_get_lvalue * * Get the address of a named symbol. * Return values: * sglv_found: if the symbol is found * sglv_unknown: if the symbol isn't found * sglv_aborted: some error occurred (likely, many symbols of same name exist, * and user didn't pick one of them) */ enum sym_get_lval symbol_get_lvalue(const char* name, const int lineno, struct dbg_lvalue* rtn, BOOL bp_disp) { struct sgv_data sgv; int i; char buffer[512]; DWORD opt; IMAGEHLP_STACK_FRAME ihsf; if (strlen(name) + 4 > sizeof(buffer)) { WINE_WARN("Too long symbol (%s)\n", name); return sglv_unknown; } sgv.num = 0; sgv.num_thunks = 0; sgv.name = &buffer[2]; sgv.do_thunks = DBG_IVAR(AlwaysShowThunks); if (strchr(name, '!')) { strcpy(buffer, name); } else { buffer[0] = '*'; buffer[1] = '!'; strcpy(&buffer[2], name); } /* this is a wine specific options to return also ELF modules in the * enumeration */ SymSetOptions((opt = SymGetOptions()) | 0x40000000); SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv); if (!sgv.num) { const char* ptr = strchr(name, '!'); if ((ptr && ptr[1] != '_') || (!ptr && *name != '_')) { if (ptr) { int offset = ptr - name; memcpy(buffer, name, offset + 1); buffer[offset + 1] = '_'; strcpy(&buffer[offset + 2], ptr + 1); } else { buffer[0] = '*'; buffer[1] = '!'; buffer[2] = '_'; strcpy(&buffer[3], name); } SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv); } } SymSetOptions(opt); /* now grab local symbols */ if (stack_get_current_frame(&ihsf) && sgv.num < NUMDBGV) { sgv.frame_offset = ihsf.FrameOffset; SymEnumSymbols(dbg_curr_process->handle, 0, name, sgv_cb, (void*)&sgv); } if (!sgv.num) { dbg_printf("No symbols found for %s\n", name); return sglv_unknown; } /* recompute potential offsets for functions (linenumber, skip prolog) */ for (i = 0; i < sgv.num; i++) { if (sgv.syms[i].flags & (SYMFLAG_REGISTER|SYMFLAG_REGREL|SYMFLAG_LOCAL|SYMFLAG_THUNK)) continue; if (lineno == -1) { struct dbg_type type; ULONG64 addr; type.module = sgv.syms[i].lvalue.type.module; type.id = sgv.syms[i].sym_info; if (bp_disp && symbol_get_debug_start(&type, &addr)) sgv.syms[i].lvalue.addr.Offset = addr; } else { DWORD disp; IMAGEHLP_LINE64 il; BOOL found = FALSE; il.SizeOfStruct = sizeof(il); SymGetLineFromAddr64(dbg_curr_process->handle, (DWORD_PTR)memory_to_linear_addr(&sgv.syms[i].lvalue.addr), &disp, &il); do { if (lineno == il.LineNumber) { sgv.syms[i].lvalue.addr.Offset = il.Address; found = TRUE; break; } } while (SymGetLineNext64(dbg_curr_process->handle, &il)); if (!found) WINE_FIXME("No line (%d) found for %s (setting to symbol start)\n", lineno, name); } } if (sgv.num - sgv.num_thunks > 1 || /* many symbols non thunks (and showing only non thunks) */ (sgv.num > 1 && DBG_IVAR(AlwaysShowThunks)) || /* many symbols (showing symbols & thunks) */ (sgv.num == sgv.num_thunks && sgv.num_thunks > 1)) { return symbol_current_picker(name, &sgv, rtn); } /* first symbol is the one we want: * - only one symbol found, * - or many symbols but only one non thunk when AlwaysShowThunks is FALSE */ *rtn = sgv.syms[0].lvalue; return sglv_found; }
/*********************************************************************** * break_restart_execution * * Set the bp to the correct state to restart execution * in the given mode. */ void break_restart_execution(int count) { ADDRESS64 addr; enum dbg_line_status status; enum dbg_exec_mode mode, ret_mode; ADDRESS64 callee; void* linear; memory_get_current_pc(&addr); linear = memory_to_linear_addr(&addr); /* * This is the mode we will be running in after we finish. We would like * to be able to modify this in certain cases. */ ret_mode = mode = dbg_curr_thread->exec_mode; /* we've stopped on a xpoint (other than step over) */ if (dbg_curr_thread->stopped_xpoint > 0) { /* * If we have set a new value, then save it in the BP number. */ if (count != 0 && mode == dbg_exec_cont) { dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].skipcount = count; } /* If we've stopped on a breakpoint, single step over it (, then run) */ if (is_xpoint_break(dbg_curr_thread->stopped_xpoint)) mode = dbg_exec_step_into_insn; } else if (mode == dbg_exec_cont && count > 1) { dbg_printf("Not stopped at any breakpoint; argument ignored.\n"); } if (mode == dbg_exec_finish && be_cpu->is_function_return(linear)) { mode = ret_mode = dbg_exec_step_into_insn; } /* * See if the function we are stepping into has debug info * and line numbers. If not, then we step over it instead. * FIXME - we need to check for things like thunks or trampolines, * as the actual function may in fact have debug info. */ if (be_cpu->is_function_call(linear, &callee)) { status = symbol_get_function_line_status(&callee); #if 0 /* FIXME: we need to get the thunk type */ /* * Anytime we have a trampoline, step over it. */ if ((mode == EXEC_STEP_OVER || mode == EXEC_STEPI_OVER) && status == dbg_in_a_thunk) { WINE_WARN("Not stepping into trampoline at %p (no lines)\n", memory_to_linear_addr(&callee)); mode = EXEC_STEP_OVER_TRAMPOLINE; } #endif if (mode == dbg_exec_step_into_line && status == dbg_no_line_info) { WINE_WARN("Not stepping into function at %p (no lines)\n", memory_to_linear_addr(&callee)); mode = dbg_exec_step_over_line; } } if (mode == dbg_exec_step_into_line && symbol_get_function_line_status(&addr) == dbg_no_line_info) { dbg_printf("Single stepping until exit from function,\n" "which has no line number information.\n"); ret_mode = mode = dbg_exec_finish; } switch (mode) { case dbg_exec_cont: /* Continuous execution */ be_cpu->single_step(&dbg_context, FALSE); break_set_xpoints(TRUE); break; #if 0 case EXEC_STEP_OVER_TRAMPOLINE: /* * This is the means by which we step over our conversion stubs * in callfrom*.s and callto*.s. We dig the appropriate address * off the stack, and we set the breakpoint there instead of the * address just after the call. */ be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context, be_cpu_addr_stack, &addr); /* FIXME: we assume stack grows as on an i386 */ addr.Offset += 2 * sizeof(unsigned int); dbg_read_memory(memory_to_linear_addr(&addr), &addr.Offset, sizeof(addr.Offset)); dbg_curr_process->bp[0].addr = addr; dbg_curr_process->bp[0].enabled = TRUE; dbg_curr_process->bp[0].refcount = 1; dbg_curr_process->bp[0].skipcount = 0; dbg_curr_process->bp[0].xpoint_type = be_xpoint_break; dbg_curr_process->bp[0].condition = NULL; be_cpu->single_step(&dbg_context, FALSE); break_set_xpoints(TRUE); break; #endif case dbg_exec_finish: case dbg_exec_step_over_insn: /* Stepping over a call */ case dbg_exec_step_over_line: /* Stepping over a call */ if (be_cpu->is_step_over_insn(linear)) { be_cpu->disasm_one_insn(&addr, FALSE); dbg_curr_process->bp[0].addr = addr; dbg_curr_process->bp[0].enabled = TRUE; dbg_curr_process->bp[0].refcount = 1; dbg_curr_process->bp[0].skipcount = 0; dbg_curr_process->bp[0].xpoint_type = be_xpoint_break; dbg_curr_process->bp[0].condition = NULL; be_cpu->single_step(&dbg_context, FALSE); break_set_xpoints(TRUE); break; } /* else fall through to single-stepping */ case dbg_exec_step_into_line: /* Single-stepping a line */ case dbg_exec_step_into_insn: /* Single-stepping an instruction */ be_cpu->single_step(&dbg_context, TRUE); break; default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL); } dbg_curr_thread->step_over_bp = dbg_curr_process->bp[0]; dbg_curr_thread->exec_mode = ret_mode; }