2 * Generate hash tables for Wine debugger symbols
4 * Copyright (C) 1993, Eric Youngdale.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
35 static BOOL symbol_get_debug_start(DWORD mod_base, DWORD typeid, ULONG64* start)
38 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
39 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
43 type.module = mod_base;
46 if (!types_get_info(&type, TI_GET_CHILDRENCOUNT, &count)) return FALSE;
50 fcp->Count = min(count, 256);
51 if (types_get_info(&type, TI_FINDCHILDREN, fcp))
53 for (i = 0; i < min(fcp->Count, count); i++)
55 type.id = fcp->ChildId[i];
56 types_get_info(&type, TI_GET_SYMTAG, &tag);
57 if (tag != SymTagFuncDebugStart) continue;
58 return types_get_info(&type, TI_GET_ADDRESS, start);
60 count -= min(count, 256);
72 /* FIXME: NUMDBGV should be made variable */
73 struct dbg_lvalue lvalue;
75 } syms[NUMDBGV]; /* out : will be filled in with various found symbols */
76 int num; /* out : number of found symbols */
77 int num_thunks; /* out : number of thunks found */
78 const char* name; /* in : name of symbol to look up */
79 const char* filename; /* in (opt): filename where to look up symbol */
80 int lineno; /* in (opt): line number in filename where to look up symbol */
81 unsigned bp_disp : 1, /* in : whether if we take into account func address or func first displayable insn */
82 do_thunks : 1; /* in : whether we return thunks tags */
83 ULONG64 frame_offset; /* in : frame for local & parameter variables look up */
86 static BOOL CALLBACK sgv_cb(SYMBOL_INFO* sym, ULONG size, void* ctx)
88 struct sgv_data* sgv = (struct sgv_data*)ctx;
91 unsigned cookie = DLV_TARGET, insp;
93 if (sym->Flags & SYMFLAG_REGISTER)
95 const struct dbg_internal_var* div;
97 if (dbg_curr_frame != 0)
99 dbg_printf(" %s (register): << cannot display, not in correct frame\n",
103 for (div = dbg_context_vars; div->name && div->val != sym->Register; div++);
106 dbg_printf(" %s (register): couldn't find register %lu\n",
107 sym->Name, sym->Register);
110 addr = (ULONG64)(DWORD_PTR)div->pval;
113 else if (sym->Flags & SYMFLAG_LOCAL) /* covers both local & parameters */
115 addr = sgv->frame_offset + sym->Address;
117 else if (sym->Flags & SYMFLAG_THUNK)
119 if (!sgv->do_thunks) return TRUE;
126 il.SizeOfStruct = sizeof(il);
127 SymGetLineFromAddr(dbg_curr_process->handle, sym->Address, &disp, &il);
128 if (sgv->filename && strcmp(sgv->filename, il.FileName))
130 WINE_FIXME("File name mismatch (%s / %s)\n", sgv->filename, il.FileName);
134 if (sgv->lineno == -1)
137 !symbol_get_debug_start(sym->ModBase, sym->TypeIndex, &addr))
145 if (sgv->lineno == il.LineNumber)
150 } while (SymGetLineNext(dbg_curr_process->handle, &il));
153 WINE_FIXME("No line (%d) found for %s (setting to symbol)\n",
154 sgv->lineno, sgv->name);
160 if (sgv->num >= NUMDBGV)
162 dbg_printf("Too many addresses for symbol '%s', limiting the first %d\n",
166 WINE_TRACE("==> %s %s%s%s%s%s%s%s\n",
168 (sym->Flags & SYMFLAG_FUNCTION) ? "func " : "",
169 (sym->Flags & SYMFLAG_FRAMEREL) ? "framerel " : "",
170 (sym->Flags & SYMFLAG_REGISTER) ? "register " : "",
171 (sym->Flags & SYMFLAG_REGREL) ? "regrel " : "",
172 (sym->Flags & SYMFLAG_PARAMETER) ? "param " : "",
173 (sym->Flags & SYMFLAG_LOCAL) ? "local " : "",
174 (sym->Flags & SYMFLAG_THUNK) ? "thunk " : "");
176 /* always keep the thunks at end of the array */
178 if (sgv->num_thunks && !(sym->Flags & SYMFLAG_THUNK))
180 insp -= sgv->num_thunks;
181 memmove(&sgv->syms[insp + 1], &sgv->syms[insp],
182 sizeof(sgv->syms[0]) * sgv->num_thunks);
184 sgv->syms[insp].lvalue.cookie = cookie;
185 sgv->syms[insp].lvalue.addr.Mode = AddrModeFlat;
186 sgv->syms[insp].lvalue.addr.Offset = addr;
187 sgv->syms[insp].lvalue.type.module = sym->ModBase;
188 sgv->syms[insp].lvalue.type.id = sym->TypeIndex;
189 types_get_info(&sgv->syms[insp].lvalue.type, TI_GET_TYPE,
190 &sgv->syms[insp].lvalue.type.id);
191 sgv->syms[insp].flags = sym->Flags;
197 /***********************************************************************
200 * Get the address of a named symbol.
202 * sglv_found: if the symbol is found
203 * sglv_unknown: if the symbol isn't found
204 * sglv_aborted: some error occurred (likely, many symbols of same name exist,
205 * and user didn't pick one of them)
207 enum sym_get_lval symbol_get_lvalue(const char* name, const int lineno,
208 struct dbg_lvalue* rtn, BOOL bp_disp)
212 char tmp[sizeof(SYMBOL_INFO) + 256];
213 SYMBOL_INFO* si = (SYMBOL_INFO*)tmp;
216 IMAGEHLP_STACK_FRAME ihsf;
218 if (strlen(name) + 4 > sizeof(buffer))
220 WINE_WARN("Too long symbol (%s)\n", name);
226 sgv.name = &buffer[2];
229 sgv.bp_disp = bp_disp ? TRUE : FALSE;
230 sgv.do_thunks = DBG_IVAR(AlwaysShowThunks);
232 if (strchr(name, '!'))
234 strcpy(buffer, name);
240 strcpy(&buffer[2], name);
243 /* this is a wine specific options to return also ELF modules in the
246 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
247 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
253 if (!sgv.num && (name[0] != '_'))
255 char* ptr = strchr(name, '!');
259 memmove(ptr + 1, ptr, strlen(ptr));
267 strcpy(&buffer[3], name);
269 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
277 /* now grab local symbols */
278 si->SizeOfStruct = sizeof(*si);
279 si->MaxNameLen = 256;
280 if (stack_get_frame(si, &ihsf) && sgv.num < NUMDBGV)
282 sgv.frame_offset = ihsf.FrameOffset;
283 if (SymSetContext(dbg_curr_process->handle, &ihsf, NULL))
284 SymEnumSymbols(dbg_curr_process->handle, 0, name, sgv_cb, (void*)&sgv);
289 dbg_printf("No symbols found for %s\n", name);
293 if (dbg_interactiveP)
295 if (sgv.num - sgv.num_thunks > 1 || /* many symbols non thunks (and showing only non thunks) */
296 (sgv.num > 1 && DBG_IVAR(AlwaysShowThunks)) || /* many symbols (showing symbols & thunks) */
297 (sgv.num == sgv.num_thunks && sgv.num_thunks > 1))
299 dbg_printf("Many symbols with name '%s', "
300 "choose the one you want (<cr> to abort):\n", name);
301 for (i = 0; i < sgv.num; i++)
303 if (sgv.num - sgv.num_thunks > 1 && (sgv.syms[i].flags & SYMFLAG_THUNK) && !DBG_IVAR(AlwaysShowThunks))
305 dbg_printf("[%d]: ", i + 1);
306 if (sgv.syms[i].flags & SYMFLAG_LOCAL)
308 dbg_printf("%s %sof %s\n",
309 sgv.syms[i].flags & SYMFLAG_PARAMETER ? "Parameter" : "Local variable",
310 sgv.syms[i].flags & (SYMFLAG_REGISTER|SYMFLAG_REGREL) ? "(in a register) " : "",
313 else if (sgv.syms[i].flags & SYMFLAG_THUNK)
315 print_address(&sgv.syms[i].lvalue.addr, TRUE);
316 /* FIXME: should display where the thunks points to */
317 dbg_printf(" thunk %s\n", name);
321 print_address(&sgv.syms[i].lvalue.addr, TRUE);
328 if (input_read_line("=> ", buffer, sizeof(buffer)))
330 if (buffer[0] == '\0') return sglv_aborted;
332 if (i < 1 || i > sgv.num)
333 dbg_printf("Invalid choice %d\n", i);
335 } while (i < 1 || i > sgv.num);
337 /* The array is 0-based, but the choices are 1..n,
338 * so we have to subtract one before returning.
345 /* FIXME: could display the list of non-picked up symbols */
347 dbg_printf("More than one symbol named %s, picking the first one\n", name);
349 *rtn = sgv.syms[i].lvalue;
353 BOOL symbol_is_local(const char* name)
356 char tmp[sizeof(SYMBOL_INFO) + 256];
357 SYMBOL_INFO* si = (SYMBOL_INFO*)tmp;
358 IMAGEHLP_STACK_FRAME ihsf;
366 sgv.do_thunks = FALSE;
368 si->SizeOfStruct = sizeof(*si);
369 si->MaxNameLen = 256;
370 if (stack_get_frame(si, &ihsf) &&
371 SymSetContext(dbg_curr_process->handle, &ihsf, NULL))
373 sgv.frame_offset = ihsf.FrameOffset;
374 SymEnumSymbols(dbg_curr_process->handle, 0, name, sgv_cb, (void*)&sgv);
379 /***********************************************************************
380 * symbol_read_symtable
382 * Read a symbol file into the hash table.
384 void symbol_read_symtable(const char* filename, unsigned long offset)
386 dbg_printf("No longer supported\n");
389 /* FIXME: have to implement SymAddSymbol in dbghelp, but likely we'll need to link
390 * this with an already loaded module !!
399 if (!(symbolfile = fopen(filename, "r")))
401 WINE_WARN("Unable to open symbol table %s\n", filename);
405 dbg_printf("Reading symbols from file %s\n", filename);
409 fgets(buffer, sizeof(buffer), symbolfile);
410 if (feof(symbolfile)) break;
412 /* Strip any text after a # sign (i.e. comments) */
413 cpnt = strchr(buffer, '#');
414 if (cpnt) *cpnt = '\0';
416 /* Quietly ignore any lines that have just whitespace */
417 for (cpnt = buffer; *cpnt; cpnt++)
419 if (*cpnt != ' ' && *cpnt != '\t') break;
421 if (!*cpnt || *cpnt == '\n') continue;
423 if (sscanf(buffer, "%lx %c %s", &addr, &type, name) == 3)
425 if (value.addr.off + offset < value.addr.off)
426 WINE_WARN("Address wrap around\n");
427 value.addr.off += offset;
428 SymAddSymbol(current_process->handle, BaseOfDll,
436 /***********************************************************************
437 * symbol_get_function_line_status
439 * Find the symbol nearest to a given address.
441 enum dbg_line_status symbol_get_function_line_status(const ADDRESS* addr)
445 ULONG64 disp64, start, size;
446 DWORD lin = (DWORD)memory_to_linear_addr(addr);
447 char buffer[sizeof(SYMBOL_INFO) + 256];
448 SYMBOL_INFO* sym = (SYMBOL_INFO*)buffer;
449 struct dbg_type type;
451 il.SizeOfStruct = sizeof(il);
452 sym->SizeOfStruct = sizeof(SYMBOL_INFO);
453 sym->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
455 /* do we have some info for lin address ? */
456 if (!SymFromAddr(dbg_curr_process->handle, lin, &disp64, sym))
457 return dbg_no_line_info;
462 /* FIXME: so far dbghelp doesn't return the 16 <=> 32 thunks
463 * and furthermore, we no longer take care of them !!!
465 return dbg_in_a_thunk;
467 case SymTagPublicSymbol: break;
469 WINE_FIXME("Unexpected sym-tag 0x%08lx\n", sym->Tag);
471 return dbg_no_line_info;
473 /* we should have a function now */
474 if (!SymGetLineFromAddr(dbg_curr_process->handle, lin, &disp, &il))
475 return dbg_no_line_info;
477 if (symbol_get_debug_start(sym->ModBase, sym->TypeIndex, &start) && lin < start)
478 return dbg_not_on_a_line_number;
479 type.module = sym->ModBase;
480 type.id = sym->TypeIndex;
481 if (!types_get_info(&type, TI_GET_LENGTH, &size) || size == 0)
483 if (il.FileName && il.FileName[0] && disp < size)
484 return (disp == 0) ? dbg_on_a_line_number : dbg_not_on_a_line_number;
486 return dbg_no_line_info;
489 /***********************************************************************
492 * Find the symbol nearest to a given address.
493 * Returns sourcefile name and line number in a format that the listing
494 * handler can deal with.
496 BOOL symbol_get_line(const char* filename, const char* name, IMAGEHLP_LINE* line)
504 sgv.name = &buffer[2];
505 sgv.filename = filename;
508 sgv.do_thunks = FALSE;
512 strcpy(&buffer[2], name);
514 /* this is a wine specific options to return also ELF modules in the
517 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
518 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
524 if (!sgv.num && (name[0] != '_'))
527 strcpy(&buffer[3], name);
528 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
539 if (filename) dbg_printf("No such function %s in %s\n", name, filename);
540 else dbg_printf("No such function %s\n", name);
543 WINE_FIXME("Several found, returning first (may not be what you want)...\n");
545 return SymGetLineFromAddr(dbg_curr_process->handle,
546 (DWORD)memory_to_linear_addr(&sgv.syms[0].lvalue.addr),
552 static BOOL CALLBACK info_locals_cb(SYMBOL_INFO* sym, ULONG size, void* ctx)
555 const char* explain = NULL;
557 struct dbg_type type;
560 type.module = sym->ModBase;
561 type.id = sym->TypeIndex;
562 types_get_info(&type, TI_GET_TYPE, &type.id);
563 types_print_type(&type, FALSE);
565 if (sym->Flags & SYMFLAG_PARAMETER) explain = "parameter";
566 else if (sym->Flags & SYMFLAG_LOCAL) explain = "local";
567 else if (sym->Flags & SYMFLAG_REGISTER) explain = buf;
569 if (sym->Flags & SYMFLAG_REGISTER)
571 const struct dbg_internal_var* div;
573 if (dbg_curr_frame != 0)
575 dbg_printf(" %s (register): << cannot display, not in correct frame\n",
579 for (div = dbg_context_vars; div->name; div++)
581 if (div->val == sym->Register)
584 sprintf(buf, "local in register %s", div->name);
589 else if (sym->Flags & SYMFLAG_LOCAL)
591 type.id = sym->TypeIndex;
592 v = ((IMAGEHLP_STACK_FRAME*)ctx)->FrameOffset + sym->Address;
594 if (!dbg_read_memory((void*)v, &val, sizeof(val)))
596 dbg_printf(" %s (%s) *** cannot read value at 0x%08lx\n", sym->Name, explain, v);
600 dbg_printf(" %s = 0x%8.8lx (%s)\n", sym->Name, val, explain);
605 int symbol_info_locals(void)
607 IMAGEHLP_STACK_FRAME ihsf;
608 char buffer[sizeof(SYMBOL_INFO) + 256];
609 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
611 si->SizeOfStruct = sizeof(*si);
612 si->MaxNameLen = 256;
613 if (stack_get_frame(si, &ihsf))
615 dbg_printf("%s:\n", si->Name);
616 if (SymSetContext(dbg_curr_process->handle, &ihsf, NULL))
617 SymEnumSymbols(dbg_curr_process->handle, 0, NULL, info_locals_cb, &ihsf);
622 static BOOL CALLBACK symbols_info_cb(SYMBOL_INFO* sym, ULONG size, void* ctx)
624 struct dbg_type type;
627 mi.SizeOfStruct = sizeof(mi);
629 if (!SymGetModuleInfo(dbg_curr_process->handle, sym->ModBase, &mi))
630 mi.ModuleName[0] = '\0';
633 size_t len = strlen(mi.ModuleName);
634 if (len > 5 && !strcmp(mi.ModuleName + len - 5, "<elf>"))
635 mi.ModuleName[len - 5] = '\0';
638 dbg_printf("%08lx: %s!%s", (ULONG_PTR)sym->Address, mi.ModuleName, sym->Name);
639 type.id = sym->TypeIndex;
640 type.module = sym->ModBase;
642 if (sym->TypeIndex != dbg_itype_none && sym->TypeIndex != 0 &&
643 types_get_info(&type, TI_GET_TYPE, &type.id))
646 types_print_type(&type, FALSE);
652 void symbol_info(const char* str)
657 if (strlen(str) + 3 >= sizeof(buffer))
659 dbg_printf("Symbol too long (%s)\n", str);
664 strcpy(&buffer[2], str);
665 /* this is a wine specific options to return also ELF modules in the
668 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
669 SymEnumSymbols(dbg_curr_process->handle, 0, buffer, symbols_info_cb, NULL);