Move a fixed size buffer to the heap.
[wine/multimedia.git] / programs / winedbg / symbol.c
blob837f118cf01b76ed9a2989634eef4d1ecd6731de
1 /*
2 * Generate hash tables for Wine debugger symbols
4 * Copyright (C) 1993, Eric Youngdale.
5 * 2004, Eric Pouech.
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
25 #include "config.h"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include "debugger.h"
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)
37 DWORD count, tag;
38 char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
39 TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
40 int i;
41 struct dbg_type type;
43 type.module = mod_base;
44 type.id = typeid;
46 if (!types_get_info(&type, TI_GET_CHILDRENCOUNT, &count)) return FALSE;
47 fcp->Start = 0;
48 while (count)
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);
61 fcp->Start += 256;
64 return FALSE;
67 struct sgv_data
69 #define NUMDBGV 100
70 struct {
71 /* FIXME: NUMDBGV should be made variable */
72 struct dbg_lvalue lvalue;
73 DWORD flags;
74 } syms[NUMDBGV]; /* out : will be filled in with various found symbols */
75 int num; /* out : number of found symbols */
76 int num_thunks; /* out : number of thunks found */
77 const char* name; /* in : name of symbol to look up */
78 const char* filename; /* in (opt): filename where to look up symbol */
79 int lineno; /* in (opt): line number in filename where to look up symbol */
80 unsigned bp_disp : 1, /* in : whether if we take into account func address or func first displayable insn */
81 do_thunks : 1; /* in : whether we return thunks tags */
82 IMAGEHLP_STACK_FRAME ihsf; /* in : frame for local & parameter variables look up */
85 static BOOL CALLBACK sgv_cb(SYMBOL_INFO* sym, ULONG size, void* ctx)
87 struct sgv_data* sgv = (struct sgv_data*)ctx;
88 ULONG64 addr;
89 IMAGEHLP_LINE il;
90 unsigned cookie = DLV_TARGET, insp;
92 if (sym->Flags & SYMFLAG_REGISTER)
94 const struct dbg_internal_var* div;
96 if (dbg_curr_frame != 0)
98 dbg_printf(" %s (register): << cannot display, not in correct frame\n",
99 sym->Name);
100 return TRUE;
102 for (div = dbg_context_vars; div->name && div->val != sym->Register; div++);
103 if (!div->name)
105 dbg_printf(" %s (register): couldn't find register %lu\n",
106 sym->Name, sym->Register);
107 return TRUE;
109 addr = (ULONG64)(DWORD_PTR)div->pval;
110 cookie = DLV_HOST;
112 else if (sym->Flags & SYMFLAG_FRAMEREL)
114 ULONG offset;
115 struct dbg_type type;
117 type.module = sym->ModBase;
118 type.id = sym->TypeIndex;
119 types_get_info(&type, TI_GET_OFFSET, &offset);
120 addr = sgv->ihsf.FrameOffset + offset;
122 else if (sym->Flags & SYMFLAG_THUNK)
124 if (!sgv->do_thunks) return TRUE;
125 sgv->num_thunks++;
126 addr = sym->Address;
128 else
130 DWORD disp;
131 il.SizeOfStruct = sizeof(il);
132 SymGetLineFromAddr(dbg_curr_process->handle, sym->Address, &disp, &il);
133 if (sgv->filename && strcmp(sgv->filename, il.FileName))
135 WINE_FIXME("File name mismatch (%s / %s)\n", sgv->filename, il.FileName);
136 return TRUE;
139 if (sgv->lineno == -1)
141 if (!sgv->bp_disp ||
142 !symbol_get_debug_start(sym->ModBase, sym->TypeIndex, &addr))
143 addr = sym->Address;
145 else
147 addr = 0;
150 if (sgv->lineno == il.LineNumber)
152 addr = il.Address;
153 break;
155 } while (SymGetLineNext(dbg_curr_process->handle, &il));
156 if (!addr)
158 WINE_FIXME("No line (%d) found for %s (setting to symbol)\n",
159 sgv->lineno, sgv->name);
160 addr = sym->Address;
165 if (sgv->num >= NUMDBGV)
167 dbg_printf("Too many addresses for symbol '%s', limiting the first %d\n",
168 sgv->name, NUMDBGV);
169 return FALSE;
171 WINE_TRACE("==> %s %s%s%s%s%s%s\n",
172 sym->Name,
173 (sym->Flags & SYMFLAG_FUNCTION) ? "func " : "",
174 (sym->Flags & SYMFLAG_FRAMEREL) ? "framerel " : "",
175 (sym->Flags & SYMFLAG_REGISTER) ? "register " : "",
176 (sym->Flags & SYMFLAG_REGREL) ? "regrel " : "",
177 (sym->Flags & SYMFLAG_PARAMETER) ? "param " : "",
178 (sym->Flags & SYMFLAG_THUNK) ? "thunk " : "");
180 /* always keep the thunks at end of the array */
181 insp = sgv->num;
182 if (sgv->num_thunks && !(sym->Flags & SYMFLAG_THUNK))
184 insp -= sgv->num_thunks;
185 memmove(&sgv->syms[insp + 1], &sgv->syms[insp],
186 sizeof(sgv->syms[0]) * sgv->num_thunks);
188 sgv->syms[insp].lvalue.cookie = cookie;
189 sgv->syms[insp].lvalue.addr.Mode = AddrModeFlat;
190 sgv->syms[insp].lvalue.addr.Offset = addr;
191 sgv->syms[insp].lvalue.type.module = sym->ModBase;
192 sgv->syms[insp].lvalue.type.id = sym->TypeIndex;
193 types_get_info(&sgv->syms[insp].lvalue.type, TI_GET_TYPE,
194 &sgv->syms[insp].lvalue.type.id);
195 sgv->syms[insp].flags = sym->Flags;
196 sgv->num++;
198 return TRUE;
201 /***********************************************************************
202 * symbol_get_lvalue
204 * Get the address of a named symbol.
205 * Return values:
206 * sglv_found: if the symbol is found
207 * sglv_unknown: if the symbol isn't found
208 * sglv_aborted: some error occurred (likely, many symbols of same name exist,
209 * and user didn't pick one of them)
211 enum sym_get_lval symbol_get_lvalue(const char* name, const int lineno,
212 struct dbg_lvalue* rtn, BOOL bp_disp)
214 struct sgv_data sgv;
215 int i = 0;
216 char tmp[sizeof(SYMBOL_INFO) + 256];
217 SYMBOL_INFO* si = (SYMBOL_INFO*)tmp;
218 char buffer[512];
219 DWORD opt;
221 if (strlen(name) + 4 > sizeof(buffer))
223 WINE_WARN("Too long symbol (%s)\n", name);
224 return sglv_unknown;
227 sgv.num = 0;
228 sgv.num_thunks = 0;
229 sgv.name = &buffer[2];
230 sgv.filename = NULL;
231 sgv.lineno = lineno;
232 sgv.bp_disp = bp_disp ? TRUE : FALSE;
233 sgv.do_thunks = DBG_IVAR(AlwaysShowThunks);
235 buffer[0] = '*';
236 buffer[1] = '!';
237 strcpy(&buffer[2], name);
239 /* this is a wine specific options to return also ELF modules in the
240 * enumeration
242 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
243 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
245 SymSetOptions(opt);
246 return sglv_unknown;
249 if (!sgv.num && (name[0] != '_'))
251 buffer[2] = '_';
252 strcpy(&buffer[3], name);
253 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
255 SymSetOptions(opt);
256 return sglv_unknown;
259 SymSetOptions(opt);
261 /* now grab local symbols */
262 si->SizeOfStruct = sizeof(*si);
263 si->MaxNameLen = 256;
264 if (stack_get_frame(si, &sgv.ihsf) && sgv.num < NUMDBGV)
266 if (SymSetContext(dbg_curr_process->handle, &sgv.ihsf, NULL))
267 SymEnumSymbols(dbg_curr_process->handle, 0, name, sgv_cb, (void*)&sgv);
270 if (!sgv.num)
272 dbg_printf("No symbols found for %s\n", name);
273 return sglv_unknown;
276 if (dbg_interactiveP)
278 if (sgv.num - sgv.num_thunks > 1 || /* many symbols non thunks (and showing only non thunks) */
279 (sgv.num > 1 && DBG_IVAR(AlwaysShowThunks)) || /* many symbols (showing symbols & thunks) */
280 (sgv.num == sgv.num_thunks && sgv.num_thunks > 1))
282 dbg_printf("Many symbols with name '%s', "
283 "choose the one you want (<cr> to abort):\n", name);
284 for (i = 0; i < sgv.num; i++)
286 if (sgv.num - sgv.num_thunks > 1 && (sgv.syms[i].flags & SYMFLAG_THUNK) && !DBG_IVAR(AlwaysShowThunks))
287 continue;
288 dbg_printf("[%d]: ", i + 1);
289 if (sgv.syms[i].flags & SYMFLAG_LOCAL)
291 dbg_printf("local variable %sof %s\n",
292 sgv.syms[i].flags & SYMFLAG_REGISTER ? "(in a register) " : "",
293 si->Name);
295 else if (sgv.syms[i].flags & SYMFLAG_PARAMETER)
297 dbg_printf("parameter %sof %s\n",
298 sgv.syms[i].flags & SYMFLAG_REGISTER ? "(in a register) " : "",
299 si->Name);
301 else if (sgv.syms[i].flags & SYMFLAG_THUNK)
303 print_address(&sgv.syms[i].lvalue.addr, TRUE);
304 /* FIXME: should display where the thunks points to */
305 dbg_printf(" thunk %s\n", name);
307 else
309 print_address(&sgv.syms[i].lvalue.addr, TRUE);
310 dbg_printf("\n");
315 i = 0;
316 if (input_read_line("=> ", buffer, sizeof(buffer)))
318 if (buffer[0] == '\0') return sglv_aborted;
319 i = atoi(buffer);
320 if (i < 1 || i > sgv.num)
321 dbg_printf("Invalid choice %d\n", i);
323 } while (i < 1 || i > sgv.num);
325 /* The array is 0-based, but the choices are 1..n,
326 * so we have to subtract one before returning.
328 i--;
331 else
333 /* FIXME: could display the list of non-picked up symbols */
334 if (sgv.num > 1)
335 dbg_printf("More than one symbol named %s, picking the first one\n", name);
337 *rtn = sgv.syms[i].lvalue;
338 return sglv_found;
341 BOOL symbol_is_local(const char* name)
343 struct sgv_data sgv;
344 char tmp[sizeof(SYMBOL_INFO) + 256];
345 SYMBOL_INFO* si = (SYMBOL_INFO*)tmp;
347 sgv.num = 0;
348 sgv.num_thunks = 0;
349 sgv.name = name;
350 sgv.filename = NULL;
351 sgv.lineno = 0;
352 sgv.bp_disp = FALSE;
353 sgv.do_thunks = FALSE;
355 si->SizeOfStruct = sizeof(*si);
356 si->MaxNameLen = 256;
357 if (stack_get_frame(si, &sgv.ihsf) &&
358 SymSetContext(dbg_curr_process->handle, &sgv.ihsf, NULL))
359 SymEnumSymbols(dbg_curr_process->handle, 0, name, sgv_cb, (void*)&sgv);
360 return sgv.num > 0;
363 /***********************************************************************
364 * symbol_read_symtable
366 * Read a symbol file into the hash table.
368 void symbol_read_symtable(const char* filename, unsigned long offset)
370 dbg_printf("No longer supported\n");
372 #if 0
373 /* FIXME: have to implement SymAddSymbol in dbghelp, but likely we'll need to link
374 * this with an already loaded module !!
376 FILE* symbolfile;
377 unsigned addr;
378 char type;
379 char* cpnt;
380 char buffer[256];
381 char name[256];
383 if (!(symbolfile = fopen(filename, "r")))
385 WINE_WARN("Unable to open symbol table %s\n", filename);
386 return;
389 dbg_printf("Reading symbols from file %s\n", filename);
391 while (1)
393 fgets(buffer, sizeof(buffer), symbolfile);
394 if (feof(symbolfile)) break;
396 /* Strip any text after a # sign (i.e. comments) */
397 cpnt = strchr(buffer, '#');
398 if (cpnt) *cpnt = '\0';
400 /* Quietly ignore any lines that have just whitespace */
401 for (cpnt = buffer; *cpnt; cpnt++)
403 if (*cpnt != ' ' && *cpnt != '\t') break;
405 if (!*cpnt || *cpnt == '\n') continue;
407 if (sscanf(buffer, "%lx %c %s", &addr, &type, name) == 3)
409 if (value.addr.off + offset < value.addr.off)
410 WINE_WARN("Address wrap around\n");
411 value.addr.off += offset;
412 SymAddSymbol(current_process->handle, BaseOfDll,
413 name, addr, 0, 0);
416 fclose(symbolfile);
417 #endif
420 /***********************************************************************
421 * symbol_get_function_line_status
423 * Find the symbol nearest to a given address.
425 enum dbg_line_status symbol_get_function_line_status(const ADDRESS* addr)
427 IMAGEHLP_LINE il;
428 DWORD disp, size;
429 ULONG64 disp64, start;
430 DWORD lin = (DWORD)memory_to_linear_addr(addr);
431 char buffer[sizeof(SYMBOL_INFO) + 256];
432 SYMBOL_INFO* sym = (SYMBOL_INFO*)buffer;
433 struct dbg_type type;
435 il.SizeOfStruct = sizeof(il);
436 sym->SizeOfStruct = sizeof(SYMBOL_INFO);
437 sym->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
439 /* do we have some info for lin address ? */
440 if (!SymFromAddr(dbg_curr_process->handle, lin, &disp64, sym))
441 return dbg_no_line_info;
443 switch (sym->Tag)
445 case SymTagThunk:
446 /* FIXME: so far dbghelp doesn't return the 16 <=> 32 thunks
447 * and furthermore, we no longer take care of them !!!
449 return dbg_in_a_thunk;
450 case SymTagFunction:
451 case SymTagPublicSymbol: break;
452 default:
453 WINE_FIXME("Unexpected sym-tag 0x%08lx\n", sym->Tag);
454 case SymTagData:
455 return dbg_no_line_info;
457 /* we should have a function now */
458 if (!SymGetLineFromAddr(dbg_curr_process->handle, lin, &disp, &il))
459 return dbg_no_line_info;
461 if (symbol_get_debug_start(sym->ModBase, sym->TypeIndex, &start) && lin < start)
462 return dbg_not_on_a_line_number;
463 type.module = sym->ModBase;
464 type.id = sym->TypeIndex;
465 if (!types_get_info(&type, TI_GET_LENGTH, &size) || size == 0)
466 size = 0x100000;
467 if (il.FileName && il.FileName[0] && disp < size)
468 return (disp == 0) ? dbg_on_a_line_number : dbg_not_on_a_line_number;
470 return dbg_no_line_info;
473 /***********************************************************************
474 * symbol_get_line
476 * Find the symbol nearest to a given address.
477 * Returns sourcefile name and line number in a format that the listing
478 * handler can deal with.
480 BOOL symbol_get_line(const char* filename, const char* name, IMAGEHLP_LINE* line)
482 struct sgv_data sgv;
483 char buffer[512];
484 DWORD opt, disp;
486 sgv.num = 0;
487 sgv.num_thunks = 0;
488 sgv.name = &buffer[2];
489 sgv.filename = filename;
490 sgv.lineno = -1;
491 sgv.bp_disp = FALSE;
492 sgv.do_thunks = FALSE;
494 buffer[0] = '*';
495 buffer[1] = '!';
496 strcpy(&buffer[2], name);
498 /* this is a wine specific options to return also ELF modules in the
499 * enumeration
501 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
502 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
504 SymSetOptions(opt);
505 return sglv_unknown;
508 if (!sgv.num && (name[0] != '_'))
510 buffer[2] = '_';
511 strcpy(&buffer[3], name);
512 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
514 SymSetOptions(opt);
515 return sglv_unknown;
518 SymSetOptions(opt);
520 switch (sgv.num)
522 case 0:
523 if (filename) dbg_printf("No such function %s in %s\n", name, filename);
524 else dbg_printf("No such function %s\n", name);
525 return FALSE;
526 default:
527 WINE_FIXME("Several found, returning first (may not be what you want)...\n");
528 case 1:
529 return SymGetLineFromAddr(dbg_curr_process->handle,
530 (DWORD)memory_to_linear_addr(&sgv.syms[0].lvalue.addr),
531 &disp, line);
533 return TRUE;
536 static BOOL CALLBACK info_locals_cb(SYMBOL_INFO* sym, ULONG size, void* ctx)
538 ULONG v, val;
539 const char* explain = NULL;
540 char buf[128];
541 struct dbg_type type;
543 dbg_printf("\t");
544 type.module = sym->ModBase;
545 type.id = sym->TypeIndex;
546 types_get_info(&type, TI_GET_TYPE, &type.id);
547 types_print_type(&type, FALSE);
549 if (sym->Flags & SYMFLAG_LOCAL) explain = "local";
550 else if (sym->Flags & SYMFLAG_PARAMETER) explain = "parameter";
551 else if (sym->Flags & SYMFLAG_REGISTER) explain = buf;
553 if (sym->Flags & SYMFLAG_REGISTER)
555 const struct dbg_internal_var* div;
557 if (dbg_curr_frame != 0)
559 dbg_printf(" %s (register): << cannot display, not in correct frame\n",
560 sym->Name);
561 return TRUE;
563 for (div = dbg_context_vars; div->name; div++)
565 if (div->val == sym->Register)
567 val = *div->pval;
568 sprintf(buf, "local in register %s", div->name);
569 break;
573 else if (sym->Flags & SYMFLAG_FRAMEREL)
575 type.id = sym->TypeIndex;
576 types_get_info(&type, TI_GET_OFFSET, &v);
577 v += ((IMAGEHLP_STACK_FRAME*)ctx)->FrameOffset;
579 if (!dbg_read_memory((void*)v, &val, sizeof(val)))
581 dbg_printf(" %s (%s) *** cannot read value at 0x%08lx\n", sym->Name, explain, v);
582 return TRUE;
585 dbg_printf(" %s = 0x%8.8lx (%s)\n", sym->Name, val, explain);
587 return TRUE;
590 int symbol_info_locals(void)
592 IMAGEHLP_STACK_FRAME ihsf;
593 char buffer[sizeof(SYMBOL_INFO) + 256];
594 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
596 si->SizeOfStruct = sizeof(*si);
597 si->MaxNameLen = 256;
598 if (stack_get_frame(si, &ihsf))
600 dbg_printf("%s:\n", si->Name);
601 if (SymSetContext(dbg_curr_process->handle, &ihsf, NULL))
602 SymEnumSymbols(dbg_curr_process->handle, 0, NULL, info_locals_cb, &ihsf);
604 return TRUE;
607 static BOOL CALLBACK symbols_info_cb(SYMBOL_INFO* sym, ULONG size, void* ctx)
609 struct dbg_type type;
610 IMAGEHLP_MODULE mi;
612 mi.SizeOfStruct = sizeof(mi);
614 if (!SymGetModuleInfo(dbg_curr_process->handle, sym->ModBase, &mi))
615 mi.ModuleName[0] = '\0';
616 else
618 size_t len = strlen(mi.ModuleName);
619 if (len > 5 && !strcmp(mi.ModuleName + len - 5, "<elf>"))
620 mi.ModuleName[len - 5] = '\0';
623 dbg_printf("%08lx: %s!%s", (ULONG_PTR)sym->Address, mi.ModuleName, sym->Name);
624 type.id = sym->TypeIndex;
625 type.module = sym->ModBase;
627 if (sym->TypeIndex != dbg_itype_none && sym->TypeIndex != 0 &&
628 types_get_info(&type, TI_GET_TYPE, &type.id))
630 dbg_printf(" ");
631 types_print_type(&type, FALSE);
633 dbg_printf("\n");
634 return TRUE;
637 void symbol_info(const char* str)
639 char buffer[512];
640 DWORD opt;
642 if (strlen(str) + 3 >= sizeof(buffer))
644 dbg_printf("Symbol too long (%s)\n", str);
645 return;
647 buffer[0] = '*';
648 buffer[1] = '!';
649 strcpy(&buffer[2], str);
650 /* this is a wine specific options to return also ELF modules in the
651 * enumeration
653 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
654 SymEnumSymbols(dbg_curr_process->handle, 0, buffer, symbols_info_cb, NULL);
655 SymSetOptions(opt);