- enhanced 'info share' output
[wine/gsoc_dplay.git] / programs / winedbg / symbol.c
blob6addcb865263b92249d3cb09b37d144c1a4124c7
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, DWORD* 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;
42 if (!types_get_info(mod_base, typeid, TI_GET_CHILDRENCOUNT, &count)) return FALSE;
43 fcp->Start = 0;
44 while (count)
46 fcp->Count = min(count, 256);
47 if (types_get_info(mod_base, typeid, TI_FINDCHILDREN, fcp))
49 for (i = 0; i < min(fcp->Count, count); i++)
51 types_get_info(mod_base, fcp->ChildId[i], TI_GET_SYMTAG, &tag);
52 if (tag != SymTagFuncDebugStart) continue;
53 return types_get_info(mod_base, fcp->ChildId[i], TI_GET_ADDRESS, start);
55 count -= min(count, 256);
56 fcp->Start += 256;
59 return FALSE;
62 struct sgv_data
64 #define NUMDBGV 100
65 struct {
66 /* FIXME: NUMDBGV should be made variable */
67 struct dbg_lvalue lvalue;
68 DWORD flags;
69 } syms[NUMDBGV]; /* out : will be filled in with various found symbols */
70 int num; /* out : number of found symbols */
71 int num_thunks; /* out : number of thunks found */
72 const char* name; /* in : name of symbol to look up */
73 const char* filename; /* in (opt): filename where to look up symbol */
74 int lineno; /* in (opt): line number in filename where to look up symbol */
75 unsigned bp_disp : 1, /* in : whether if we take into account func address or func first displayable insn */
76 do_thunks : 1; /* in : whether we return thunks tags */
77 IMAGEHLP_STACK_FRAME ihsf; /* in : frame for local & parameter variables look up */
80 static BOOL CALLBACK sgv_cb(SYMBOL_INFO* sym, ULONG size, void* ctx)
82 struct sgv_data* sgv = (struct sgv_data*)ctx;
83 DWORD addr;
84 IMAGEHLP_LINE il;
85 unsigned cookie = DLV_TARGET, insp;
87 if (sym->Flags & SYMFLAG_REGISTER)
89 const struct dbg_internal_var* div;
91 if (dbg_curr_frame != 0)
93 dbg_printf(" %s (register): << cannot display, not in correct frame\n",
94 sym->Name);
95 return TRUE;
97 for (div = dbg_context_vars; div->name && div->val != sym->Register; div++);
98 if (!div->name)
100 dbg_printf(" %s (register): couldn't find register %lu\n",
101 sym->Name, sym->Register);
102 return TRUE;
104 addr = (DWORD)div->pval;
105 cookie = DLV_HOST;
107 else if (sym->Flags & SYMFLAG_FRAMEREL)
109 ULONG offset;
110 types_get_info(sym->ModBase, sym->TypeIndex, TI_GET_OFFSET, &offset);
111 addr = sgv->ihsf.FrameOffset + offset;
113 else if (sym->Flags & SYMFLAG_THUNK)
115 if (!sgv->do_thunks) return TRUE;
116 sgv->num_thunks++;
117 addr = sym->Address;
119 else
121 il.SizeOfStruct = sizeof(il);
122 SymGetLineFromAddr(dbg_curr_process->handle, sym->Address, NULL, &il);
123 if (sgv->filename && strcmp(sgv->filename, il.FileName))
125 WINE_FIXME("File name mismatch (%s / %s)\n", sgv->filename, il.FileName);
126 return TRUE;
129 if (sgv->lineno == -1)
131 if (!sgv->bp_disp ||
132 !symbol_get_debug_start(sym->ModBase, sym->TypeIndex, &addr))
133 addr = sym->Address;
135 else
137 addr = 0;
140 if (sgv->lineno == il.LineNumber)
142 addr = il.Address;
143 break;
145 } while (SymGetLineNext(dbg_curr_process->handle, &il));
146 if (!addr)
148 WINE_FIXME("No line (%d) found for %s (setting to symbol)\n",
149 sgv->lineno, sgv->name);
150 addr = sym->Address;
155 if (sgv->num >= NUMDBGV)
157 dbg_printf("Too many addresses for symbol '%s', limiting the first %d\n",
158 sgv->name, NUMDBGV);
159 return FALSE;
161 WINE_TRACE("==> %s %s%s%s%s%s%s\n",
162 sym->Name,
163 (sym->Flags & SYMFLAG_FUNCTION) ? "func " : "",
164 (sym->Flags & SYMFLAG_FRAMEREL) ? "framerel " : "",
165 (sym->Flags & SYMFLAG_REGISTER) ? "register " : "",
166 (sym->Flags & SYMFLAG_REGREL) ? "regrel " : "",
167 (sym->Flags & SYMFLAG_PARAMETER) ? "param " : "",
168 (sym->Flags & SYMFLAG_THUNK) ? "thunk " : "");
170 /* always keep the thunks at end of the array */
171 insp = sgv->num;
172 if (sgv->num_thunks && !(sym->Flags & SYMFLAG_THUNK))
174 insp -= sgv->num_thunks;
175 memmove(&sgv->syms[insp + 1], &sgv->syms[insp],
176 sizeof(sgv->syms[0]) * sgv->num_thunks);
178 sgv->syms[insp].lvalue.addr.Mode = AddrModeFlat;
179 sgv->syms[insp].lvalue.addr.Offset = addr;
180 types_get_info(sym->ModBase, sym->TypeIndex, TI_GET_TYPE,
181 &sgv->syms[insp].lvalue.typeid);
182 sgv->syms[insp].lvalue.cookie = cookie;
183 sgv->syms[insp].flags = sym->Flags;
184 sgv->num++;
186 return TRUE;
189 /***********************************************************************
190 * symbol_get_lvalue
192 * Get the address of a named symbol.
193 * Return values:
194 * sglv_found: if the symbol is found
195 * sglv_unknown: if the symbol isn't found
196 * sglv_aborted: some error occurred (likely, many symbols of same name exist,
197 * and user didn't pick one of them)
199 enum sym_get_lval symbol_get_lvalue(const char* name, const int lineno,
200 struct dbg_lvalue* rtn, BOOL bp_disp)
202 struct sgv_data sgv;
203 int i = 0;
204 char tmp[sizeof(SYMBOL_INFO) + 256];
205 SYMBOL_INFO* si = (SYMBOL_INFO*)tmp;
206 char buffer[512];
207 DWORD opt;
209 if (strlen(name) + 4 > sizeof(buffer))
211 WINE_WARN("Too long symbol (%s)\n", name);
212 return sglv_unknown;
215 sgv.num = 0;
216 sgv.num_thunks = 0;
217 sgv.name = &buffer[2];
218 sgv.filename = NULL;
219 sgv.lineno = lineno;
220 sgv.bp_disp = bp_disp ? TRUE : FALSE;
221 sgv.do_thunks = DBG_IVAR(AlwaysShowThunks);
223 buffer[0] = '*';
224 buffer[1] = '!';
225 strcpy(&buffer[2], name);
227 /* this is a wine specific options to return also ELF modules in the
228 * enumeration
230 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
231 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
233 SymSetOptions(opt);
234 return sglv_unknown;
237 if (!sgv.num && (name[0] != '_'))
239 buffer[2] = '_';
240 strcpy(&buffer[3], name);
241 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
243 SymSetOptions(opt);
244 return sglv_unknown;
247 SymSetOptions(opt);
249 /* now grab local symbols */
250 si->SizeOfStruct = sizeof(*si);
251 si->MaxNameLen = 256;
252 if (stack_get_frame(si, &sgv.ihsf) && sgv.num < NUMDBGV)
254 if (SymSetContext(dbg_curr_process->handle, &sgv.ihsf, NULL))
255 SymEnumSymbols(dbg_curr_process->handle, 0, name, sgv_cb, (void*)&sgv);
258 if (!sgv.num)
260 dbg_printf("No symbols found for %s\n", name);
261 return sglv_unknown;
264 if (dbg_interactiveP)
266 if (sgv.num - sgv.num_thunks > 1 || /* many symbols non thunks (and showing only non thunks) */
267 (sgv.num > 1 && DBG_IVAR(AlwaysShowThunks)) || /* many symbols (showing symbols & thunks) */
268 (sgv.num == sgv.num_thunks && sgv.num_thunks > 1))
270 dbg_printf("Many symbols with name '%s', "
271 "choose the one you want (<cr> to abort):\n", name);
272 for (i = 0; i < sgv.num; i++)
274 if (sgv.num - sgv.num_thunks > 1 && (sgv.syms[i].flags & SYMFLAG_THUNK) && !DBG_IVAR(AlwaysShowThunks))
275 continue;
276 dbg_printf("[%d]: ", i + 1);
277 if (sgv.syms[i].flags & SYMFLAG_LOCAL)
279 dbg_printf("local variable of %s\n", si->Name);
281 else if (sgv.syms[i].flags & SYMFLAG_PARAMETER)
283 dbg_printf("parameter of %s\n", si->Name);
285 else if (sgv.syms[i].flags & SYMFLAG_THUNK)
287 print_address(&sgv.syms[i].lvalue.addr, TRUE);
288 /* FIXME: should display where the thunks points to */
289 dbg_printf(" thunk %s\n", name);
291 else
293 print_address(&sgv.syms[i].lvalue.addr, TRUE);
294 dbg_printf("\n");
299 i = 0;
300 if (input_read_line("=> ", buffer, sizeof(buffer)))
302 if (buffer[0] == '\0') return sglv_aborted;
303 i = atoi(buffer);
304 if (i < 1 || i > sgv.num)
305 dbg_printf("Invalid choice %d\n", i);
307 } while (i < 1 || i > sgv.num);
309 /* The array is 0-based, but the choices are 1..n,
310 * so we have to subtract one before returning.
312 i--;
315 else
317 dbg_printf("More than one symbol named %s, picking the first one\n", name);
318 i = 0;
320 *rtn = sgv.syms[i].lvalue;
321 return sglv_found;
324 /***********************************************************************
325 * symbol_read_symtable
327 * Read a symbol file into the hash table.
329 void symbol_read_symtable(const char* filename, unsigned long offset)
331 dbg_printf("No longer supported\n");
333 #if 0
334 /* FIXME: have to implement SymAddSymbol in dbghelp, but likely to link this with
335 * a loaded module !!
337 FILE* symbolfile;
338 unsigned addr;
339 char type;
340 char* cpnt;
341 char buffer[256];
342 char name[256];
344 if (!(symbolfile = fopen(filename, "r")))
346 WINE_WARN("Unable to open symbol table %s\n", filename);
347 return;
350 dbg_printf("Reading symbols from file %s\n", filename);
352 while (1)
354 fgets(buffer, sizeof(buffer), symbolfile);
355 if (feof(symbolfile)) break;
357 /* Strip any text after a # sign (i.e. comments) */
358 cpnt = strchr(buffer, '#');
359 if (cpnt) *cpnt = '\0';
361 /* Quietly ignore any lines that have just whitespace */
362 for (cpnt = buffer; *cpnt; cpnt++)
364 if (*cpnt != ' ' && *cpnt != '\t') break;
366 if (!*cpnt || *cpnt == '\n') continue;
368 if (sscanf(buffer, "%lx %c %s", &addr, &type, name) == 3)
370 if (value.addr.off + offset < value.addr.off)
371 WINE_WARN("Address wrap around\n");
372 value.addr.off += offset;
373 SymAddSymbol(current_process->handle, BaseOfDll,
374 name, addr, 0, 0);
377 fclose(symbolfile);
378 #endif
381 /***********************************************************************
382 * symbol_get_function_line_status
384 * Find the symbol nearest to a given address.
386 enum dbg_line_status symbol_get_function_line_status(const ADDRESS* addr)
388 IMAGEHLP_LINE il;
389 DWORD disp, start, size;
390 DWORD lin = (DWORD)memory_to_linear_addr(addr);
391 char buffer[sizeof(SYMBOL_INFO) + 256];
392 SYMBOL_INFO* sym = (SYMBOL_INFO*)buffer;
394 il.SizeOfStruct = sizeof(il);
395 sym->SizeOfStruct = sizeof(SYMBOL_INFO);
396 sym->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
398 /* do we have some info for lin address ? */
399 if (!SymFromAddr(dbg_curr_process->handle, lin, NULL, sym))
400 return dbg_no_line_info;
402 switch (sym->Tag)
404 case SymTagThunk:
405 /* FIXME: so far dbghelp doesn't return the 16 <=> 32 thunks
406 * and furthermore, we no longer take care of them !!!
408 return dbg_in_a_thunk;
409 case SymTagFunction:
410 case SymTagPublicSymbol: break;
411 default:
412 WINE_FIXME("Unexpected sym-tag 0x%08lx\n", sym->Tag);
413 case SymTagData:
414 return dbg_no_line_info;
416 /* we should have a function now */
417 if (!SymGetLineFromAddr(dbg_curr_process->handle, lin, &disp, &il))
418 return dbg_no_line_info;
420 if (symbol_get_debug_start(sym->ModBase, sym->TypeIndex, &start) && lin < start)
421 return dbg_not_on_a_line_number;
422 if (!types_get_info(sym->ModBase, sym->TypeIndex, TI_GET_LENGTH, &size) || size == 0)
423 size = 0x100000;
424 if (il.FileName && il.FileName[0] && disp < size)
425 return (disp == 0) ? dbg_on_a_line_number : dbg_not_on_a_line_number;
427 return dbg_no_line_info;
430 /***********************************************************************
431 * symbol_get_line
433 * Find the symbol nearest to a given address.
434 * Returns sourcefile name and line number in a format that the listing
435 * handler can deal with.
437 BOOL symbol_get_line(const char* filename, const char* name, IMAGEHLP_LINE* line)
439 struct sgv_data sgv;
440 char buffer[512];
441 DWORD opt;
443 sgv.num = 0;
444 sgv.num_thunks = 0;
445 sgv.name = &buffer[2];
446 sgv.filename = filename;
447 sgv.lineno = -1;
448 sgv.bp_disp = FALSE;
449 sgv.do_thunks = FALSE;
451 buffer[0] = '*';
452 buffer[1] = '!';
453 strcpy(&buffer[2], name);
455 /* this is a wine specific options to return also ELF modules in the
456 * enumeration
458 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
459 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
461 SymSetOptions(opt);
462 return sglv_unknown;
465 if (!sgv.num && (name[0] != '_'))
467 buffer[2] = '_';
468 strcpy(&buffer[3], name);
469 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
471 SymSetOptions(opt);
472 return sglv_unknown;
475 SymSetOptions(opt);
477 switch (sgv.num)
479 case 0:
480 if (filename) dbg_printf("No such function %s in %s\n", name, filename);
481 else dbg_printf("No such function %s\n", name);
482 return FALSE;
483 default:
484 WINE_FIXME("Several found, returning first (may not be what you want)...\n");
485 case 1:
486 return SymGetLineFromAddr(dbg_curr_process->handle,
487 (DWORD)memory_to_linear_addr(&sgv.syms[0].lvalue.addr),
488 NULL, line);
490 return TRUE;
493 static BOOL CALLBACK info_locals_cb(SYMBOL_INFO* sym, ULONG size, void* ctx)
495 DWORD tid;
496 ULONG v, val;
497 const char* explain = NULL;
498 char buf[128];
500 dbg_printf("\t");
501 types_get_info(sym->ModBase, sym->TypeIndex, TI_GET_TYPE, &tid);
502 types_print_type(sym->ModBase, tid, FALSE);
504 if (sym->Flags & SYMFLAG_LOCAL) explain = "local";
505 else if (sym->Flags & SYMFLAG_PARAMETER) explain = "parameter";
506 else if (sym->Flags & SYMFLAG_REGISTER) explain = buf;
508 if (sym->Flags & SYMFLAG_REGISTER)
510 const struct dbg_internal_var* div;
512 if (dbg_curr_frame != 0)
514 dbg_printf(" %s (register): << cannot display, not in correct frame\n",
515 sym->Name);
516 return TRUE;
518 for (div = dbg_context_vars; div->name; div++)
520 if (div->val == sym->Register)
522 val = *div->pval;
523 sprintf(buf, "local in register %s", div->name);
524 break;
528 else if (sym->Flags & SYMFLAG_FRAMEREL)
530 types_get_info(sym->ModBase, sym->TypeIndex, TI_GET_OFFSET, &v);
531 v += ((IMAGEHLP_STACK_FRAME*)ctx)->FrameOffset;
533 dbg_read_memory_verbose((void*)v, &val, sizeof(val));
535 dbg_printf(" %s = 0x%8.8lx (%s)\n", sym->Name, val, explain);
537 return TRUE;
540 int symbol_info_locals(void)
542 IMAGEHLP_STACK_FRAME ihsf;
543 char buffer[sizeof(SYMBOL_INFO) + 256];
544 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
546 si->SizeOfStruct = sizeof(*si);
547 si->MaxNameLen = 256;
548 if (stack_get_frame(si, &ihsf))
550 dbg_printf("%s:\n", si->Name);
551 if (SymSetContext(dbg_curr_process->handle, &ihsf, NULL))
552 SymEnumSymbols(dbg_curr_process->handle, 0, NULL, info_locals_cb, &ihsf);
554 return TRUE;
557 static BOOL CALLBACK symbols_info_cb(SYMBOL_INFO* sym, ULONG size, void* ctx)
559 DWORD type;
561 dbg_printf("%08lx: %s (", sym->Address, sym->Name);
562 if (sym->TypeIndex != dbg_itype_none && sym->TypeIndex != 0 &&
563 types_get_info(sym->ModBase, sym->TypeIndex, TI_GET_TYPE, &type))
565 types_print_type(sym->ModBase, type, FALSE);
567 dbg_printf(")\n");
568 return TRUE;
571 void symbol_info(const char* str)
573 char buffer[512];
574 DWORD opt;
576 if (strlen(str) + 3 >= sizeof(buffer))
578 dbg_printf("Symbol too long (%s)\n", str);
579 return;
581 buffer[0] = '*';
582 buffer[1] = '!';
583 strcpy(&buffer[2], str);
584 /* this is a wine specific options to return also ELF modules in the
585 * enumeration
587 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
588 SymEnumSymbols(dbg_curr_process->handle, 0, buffer, symbols_info_cb, NULL);
589 SymSetOptions(opt);