urlmon: Added http protocol tests (currently failing in Wine).
[wine/wine64.git] / programs / winedbg / symbol.c
blobf7dcfce539cc2df4fd9a85e65ef8262ba5d71e6e
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 if (!typeid) return FALSE; /* native dbghelp not always fills the info field */
44 type.module = mod_base;
45 type.id = typeid;
47 if (!types_get_info(&type, TI_GET_CHILDRENCOUNT, &count)) return FALSE;
48 fcp->Start = 0;
49 while (count)
51 fcp->Count = min(count, 256);
52 if (types_get_info(&type, TI_FINDCHILDREN, fcp))
54 for (i = 0; i < min(fcp->Count, count); i++)
56 type.id = fcp->ChildId[i];
57 types_get_info(&type, TI_GET_SYMTAG, &tag);
58 if (tag != SymTagFuncDebugStart) continue;
59 return types_get_info(&type, TI_GET_ADDRESS, start);
61 count -= min(count, 256);
62 fcp->Start += 256;
65 return FALSE;
68 struct sgv_data
70 #define NUMDBGV 100
71 struct
73 /* FIXME: NUMDBGV should be made variable */
74 struct dbg_lvalue lvalue;
75 DWORD flags;
76 } syms[NUMDBGV]; /* out : will be filled in with various found symbols */
77 int num; /* out : number of found symbols */
78 int num_thunks; /* out : number of thunks found */
79 const char* name; /* in : name of symbol to look up */
80 const char* filename; /* in (opt): filename where to look up symbol */
81 int lineno; /* in (opt): line number in filename where to look up symbol */
82 unsigned bp_disp : 1, /* in : whether if we take into account func address or func first displayable insn */
83 do_thunks : 1; /* in : whether we return thunks tags */
84 ULONG64 frame_offset; /* in : frame for local & parameter variables look up */
87 static BOOL CALLBACK sgv_cb(SYMBOL_INFO* sym, ULONG size, void* ctx)
89 struct sgv_data* sgv = (struct sgv_data*)ctx;
90 ULONG64 addr;
91 IMAGEHLP_LINE il;
92 unsigned cookie = DLV_TARGET, insp;
94 if (sym->Flags & SYMFLAG_REGISTER)
96 const struct dbg_internal_var* div;
98 if (dbg_curr_thread->curr_frame != 0)
100 dbg_printf(" %s (register): << cannot display, not in correct frame\n",
101 sym->Name);
102 return TRUE;
104 for (div = dbg_context_vars; div->name && div->val != sym->Register; div++);
105 if (!div->name)
107 dbg_printf(" %s (register): couldn't find register %lu\n",
108 sym->Name, sym->Register);
109 return TRUE;
111 addr = (ULONG64)(DWORD_PTR)div->pval;
112 cookie = DLV_HOST;
114 else if (sym->Flags & SYMFLAG_LOCAL) /* covers both local & parameters */
116 addr = sgv->frame_offset + sym->Address;
118 else if (sym->Flags & SYMFLAG_THUNK)
120 if (!sgv->do_thunks) return TRUE;
121 sgv->num_thunks++;
122 addr = sym->Address;
124 else
126 DWORD disp;
127 il.SizeOfStruct = sizeof(il);
128 SymGetLineFromAddr(dbg_curr_process->handle, sym->Address, &disp, &il);
129 if (sgv->filename && strcmp(sgv->filename, il.FileName))
131 WINE_FIXME("File name mismatch (%s / %s)\n", sgv->filename, il.FileName);
132 return TRUE;
135 if (sgv->lineno == -1)
137 if (!sgv->bp_disp ||
138 !symbol_get_debug_start(sym->ModBase, sym->info, &addr))
139 addr = sym->Address;
141 else
143 addr = 0;
146 if (sgv->lineno == il.LineNumber)
148 addr = il.Address;
149 break;
151 } while (SymGetLineNext(dbg_curr_process->handle, &il));
152 if (!addr)
154 WINE_FIXME("No line (%d) found for %s (setting to symbol)\n",
155 sgv->lineno, sgv->name);
156 addr = sym->Address;
161 if (sgv->num >= NUMDBGV)
163 dbg_printf("Too many addresses for symbol '%s', limiting the first %d\n",
164 sgv->name, NUMDBGV);
165 return FALSE;
167 WINE_TRACE("==> %s %s%s%s%s%s%s%s\n",
168 sym->Name,
169 (sym->Flags & SYMFLAG_FUNCTION) ? "func " : "",
170 (sym->Flags & SYMFLAG_FRAMEREL) ? "framerel " : "",
171 (sym->Flags & SYMFLAG_REGISTER) ? "register " : "",
172 (sym->Flags & SYMFLAG_REGREL) ? "regrel " : "",
173 (sym->Flags & SYMFLAG_PARAMETER) ? "param " : "",
174 (sym->Flags & SYMFLAG_LOCAL) ? "local " : "",
175 (sym->Flags & SYMFLAG_THUNK) ? "thunk " : "");
177 /* always keep the thunks at end of the array */
178 insp = sgv->num;
179 if (sgv->num_thunks && !(sym->Flags & SYMFLAG_THUNK))
181 insp -= sgv->num_thunks;
182 memmove(&sgv->syms[insp + 1], &sgv->syms[insp],
183 sizeof(sgv->syms[0]) * sgv->num_thunks);
185 sgv->syms[insp].lvalue.cookie = cookie;
186 sgv->syms[insp].lvalue.addr.Mode = AddrModeFlat;
187 sgv->syms[insp].lvalue.addr.Offset = addr;
188 sgv->syms[insp].lvalue.type.module = sym->ModBase;
189 sgv->syms[insp].lvalue.type.id = sym->TypeIndex;
190 sgv->syms[insp].flags = sym->Flags;
191 sgv->num++;
193 return TRUE;
196 /***********************************************************************
197 * symbol_get_lvalue
199 * Get the address of a named symbol.
200 * Return values:
201 * sglv_found: if the symbol is found
202 * sglv_unknown: if the symbol isn't found
203 * sglv_aborted: some error occurred (likely, many symbols of same name exist,
204 * and user didn't pick one of them)
206 enum sym_get_lval symbol_get_lvalue(const char* name, const int lineno,
207 struct dbg_lvalue* rtn, BOOL bp_disp)
209 struct sgv_data sgv;
210 int i = 0;
211 char buffer[512];
212 DWORD opt;
213 IMAGEHLP_STACK_FRAME ihsf;
215 if (strlen(name) + 4 > sizeof(buffer))
217 WINE_WARN("Too long symbol (%s)\n", name);
218 return sglv_unknown;
221 sgv.num = 0;
222 sgv.num_thunks = 0;
223 sgv.name = &buffer[2];
224 sgv.filename = NULL;
225 sgv.lineno = lineno;
226 sgv.bp_disp = bp_disp ? TRUE : FALSE;
227 sgv.do_thunks = DBG_IVAR(AlwaysShowThunks);
229 if (strchr(name, '!'))
231 strcpy(buffer, name);
233 else
235 buffer[0] = '*';
236 buffer[1] = '!';
237 strcpy(&buffer[2], name);
240 /* this is a wine specific options to return also ELF modules in the
241 * enumeration
243 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
244 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
246 SymSetOptions(opt);
247 return sglv_unknown;
250 if (!sgv.num && (name[0] != '_'))
252 char* ptr = strchr(name, '!');
254 if (ptr++)
256 memmove(ptr + 1, ptr, strlen(ptr));
257 *ptr = '_';
259 else
261 buffer[0] = '*';
262 buffer[1] = '!';
263 buffer[2] = '_';
264 strcpy(&buffer[3], name);
266 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
268 SymSetOptions(opt);
269 return sglv_unknown;
272 SymSetOptions(opt);
274 /* now grab local symbols */
275 if (stack_get_current_frame(&ihsf) && sgv.num < NUMDBGV)
277 sgv.frame_offset = ihsf.FrameOffset;
278 SymEnumSymbols(dbg_curr_process->handle, 0, name, sgv_cb, (void*)&sgv);
281 if (!sgv.num)
283 dbg_printf("No symbols found for %s\n", name);
284 return sglv_unknown;
287 if (dbg_interactiveP)
289 if (sgv.num - sgv.num_thunks > 1 || /* many symbols non thunks (and showing only non thunks) */
290 (sgv.num > 1 && DBG_IVAR(AlwaysShowThunks)) || /* many symbols (showing symbols & thunks) */
291 (sgv.num == sgv.num_thunks && sgv.num_thunks > 1))
293 dbg_printf("Many symbols with name '%s', "
294 "choose the one you want (<cr> to abort):\n", name);
295 for (i = 0; i < sgv.num; i++)
297 if (sgv.num - sgv.num_thunks > 1 && (sgv.syms[i].flags & SYMFLAG_THUNK) && !DBG_IVAR(AlwaysShowThunks))
298 continue;
299 dbg_printf("[%d]: ", i + 1);
300 if (sgv.syms[i].flags & SYMFLAG_LOCAL)
302 dbg_printf("%s %sof %s\n",
303 sgv.syms[i].flags & SYMFLAG_PARAMETER ? "Parameter" : "Local variable",
304 sgv.syms[i].flags & (SYMFLAG_REGISTER|SYMFLAG_REGREL) ? "(in a register) " : "",
305 name);
307 else if (sgv.syms[i].flags & SYMFLAG_THUNK)
309 print_address(&sgv.syms[i].lvalue.addr, TRUE);
310 /* FIXME: should display where the thunks points to */
311 dbg_printf(" thunk %s\n", name);
313 else
315 print_address(&sgv.syms[i].lvalue.addr, TRUE);
316 dbg_printf("\n");
321 i = 0;
322 if (input_read_line("=> ", buffer, sizeof(buffer)))
324 if (buffer[0] == '\0') return sglv_aborted;
325 i = atoi(buffer);
326 if (i < 1 || i > sgv.num)
327 dbg_printf("Invalid choice %d\n", i);
329 } while (i < 1 || i > sgv.num);
331 /* The array is 0-based, but the choices are 1..n,
332 * so we have to subtract one before returning.
334 i--;
337 else
339 /* FIXME: could display the list of non-picked up symbols */
340 if (sgv.num > 1)
341 dbg_printf("More than one symbol named %s, picking the first one\n", name);
343 *rtn = sgv.syms[i].lvalue;
344 return sglv_found;
347 BOOL symbol_is_local(const char* name)
349 struct sgv_data sgv;
350 IMAGEHLP_STACK_FRAME ihsf;
352 sgv.num = 0;
353 sgv.num_thunks = 0;
354 sgv.name = name;
355 sgv.filename = NULL;
356 sgv.lineno = 0;
357 sgv.bp_disp = FALSE;
358 sgv.do_thunks = FALSE;
360 if (stack_get_current_frame(&ihsf))
362 sgv.frame_offset = ihsf.FrameOffset;
363 SymEnumSymbols(dbg_curr_process->handle, 0, name, sgv_cb, (void*)&sgv);
365 return sgv.num > 0;
368 /***********************************************************************
369 * symbol_read_symtable
371 * Read a symbol file into the hash table.
373 void symbol_read_symtable(const char* filename, unsigned long offset)
375 dbg_printf("No longer supported\n");
377 #if 0
378 /* FIXME: have to implement SymAddSymbol in dbghelp, but likely we'll need to link
379 * this with an already loaded module !!
381 FILE* symbolfile;
382 unsigned addr;
383 char type;
384 char* cpnt;
385 char buffer[256];
386 char name[256];
388 if (!(symbolfile = fopen(filename, "r")))
390 WINE_WARN("Unable to open symbol table %s\n", filename);
391 return;
394 dbg_printf("Reading symbols from file %s\n", filename);
396 while (1)
398 fgets(buffer, sizeof(buffer), symbolfile);
399 if (feof(symbolfile)) break;
401 /* Strip any text after a # sign (i.e. comments) */
402 cpnt = strchr(buffer, '#');
403 if (cpnt) *cpnt = '\0';
405 /* Quietly ignore any lines that have just whitespace */
406 for (cpnt = buffer; *cpnt; cpnt++)
408 if (*cpnt != ' ' && *cpnt != '\t') break;
410 if (!*cpnt || *cpnt == '\n') continue;
412 if (sscanf(buffer, "%lx %c %s", &addr, &type, name) == 3)
414 if (value.addr.off + offset < value.addr.off)
415 WINE_WARN("Address wrap around\n");
416 value.addr.off += offset;
417 SymAddSymbol(current_process->handle, BaseOfDll,
418 name, addr, 0, 0);
421 fclose(symbolfile);
422 #endif
425 /***********************************************************************
426 * symbol_get_function_line_status
428 * Find the symbol nearest to a given address.
430 enum dbg_line_status symbol_get_function_line_status(const ADDRESS* addr)
432 IMAGEHLP_LINE il;
433 DWORD disp;
434 ULONG64 disp64, start;
435 DWORD lin = (DWORD)memory_to_linear_addr(addr);
436 char buffer[sizeof(SYMBOL_INFO) + 256];
437 SYMBOL_INFO* sym = (SYMBOL_INFO*)buffer;
439 il.SizeOfStruct = sizeof(il);
440 sym->SizeOfStruct = sizeof(SYMBOL_INFO);
441 sym->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
443 /* do we have some info for lin address ? */
444 if (!SymFromAddr(dbg_curr_process->handle, lin, &disp64, sym))
445 return dbg_no_line_info;
447 switch (sym->Tag)
449 case SymTagThunk:
450 /* FIXME: so far dbghelp doesn't return the 16 <=> 32 thunks
451 * and furthermore, we no longer take care of them !!!
453 return dbg_in_a_thunk;
454 case SymTagFunction:
455 case SymTagPublicSymbol: break;
456 default:
457 WINE_FIXME("Unexpected sym-tag 0x%08lx\n", sym->Tag);
458 case SymTagData:
459 return dbg_no_line_info;
461 /* we should have a function now */
462 if (!SymGetLineFromAddr(dbg_curr_process->handle, lin, &disp, &il))
463 return dbg_no_line_info;
465 if (symbol_get_debug_start(sym->ModBase, sym->info, &start) && lin < start)
466 return dbg_not_on_a_line_number;
467 if (!sym->Size) sym->Size = 0x100000;
468 if (il.FileName && il.FileName[0] && disp < sym->Size)
469 return (disp == 0) ? dbg_on_a_line_number : dbg_not_on_a_line_number;
471 return dbg_no_line_info;
474 /***********************************************************************
475 * symbol_get_line
477 * Find the symbol nearest to a given address.
478 * Returns sourcefile name and line number in a format that the listing
479 * handler can deal with.
481 BOOL symbol_get_line(const char* filename, const char* name, IMAGEHLP_LINE* line)
483 struct sgv_data sgv;
484 char buffer[512];
485 DWORD opt, disp;
487 sgv.num = 0;
488 sgv.num_thunks = 0;
489 sgv.name = &buffer[2];
490 sgv.filename = filename;
491 sgv.lineno = -1;
492 sgv.bp_disp = FALSE;
493 sgv.do_thunks = FALSE;
495 buffer[0] = '*';
496 buffer[1] = '!';
497 strcpy(&buffer[2], name);
499 /* this is a wine specific options to return also ELF modules in the
500 * enumeration
502 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
503 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
505 SymSetOptions(opt);
506 return sglv_unknown;
509 if (!sgv.num && (name[0] != '_'))
511 buffer[2] = '_';
512 strcpy(&buffer[3], name);
513 if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
515 SymSetOptions(opt);
516 return sglv_unknown;
519 SymSetOptions(opt);
521 switch (sgv.num)
523 case 0:
524 if (filename) dbg_printf("No such function %s in %s\n", name, filename);
525 else dbg_printf("No such function %s\n", name);
526 return FALSE;
527 default:
528 WINE_FIXME("Several found, returning first (may not be what you want)...\n");
529 case 1:
530 return SymGetLineFromAddr(dbg_curr_process->handle,
531 (DWORD)memory_to_linear_addr(&sgv.syms[0].lvalue.addr),
532 &disp, line);
534 return TRUE;
537 static BOOL CALLBACK info_locals_cb(SYMBOL_INFO* sym, ULONG size, void* ctx)
539 ULONG v, val;
540 const char* explain = NULL;
541 char buf[128];
542 struct dbg_type type;
544 dbg_printf("\t");
545 type.module = sym->ModBase;
546 type.id = sym->TypeIndex;
547 types_print_type(&type, FALSE);
549 if (sym->Flags & SYMFLAG_PARAMETER) explain = "parameter";
550 else if (sym->Flags & SYMFLAG_LOCAL) explain = "local";
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_thread->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_LOCAL)
575 type.id = sym->TypeIndex;
576 v = (ULONG)ctx + sym->Address;
578 if (!dbg_read_memory((void*)v, &val, sizeof(val)))
580 dbg_printf(" %s (%s) *** cannot read value at 0x%08lx\n", sym->Name, explain, v);
581 return TRUE;
584 dbg_printf(" %s = 0x%8.8lx (%s)\n", sym->Name, val, explain);
586 return TRUE;
589 int symbol_info_locals(void)
591 IMAGEHLP_STACK_FRAME ihsf;
592 ADDRESS addr;
594 stack_get_current_frame(&ihsf);
595 addr.Mode = AddrModeFlat;
596 addr.Offset = ihsf.InstructionOffset;
597 print_address(&addr, FALSE);
598 dbg_printf(": (%08lx)\n", (DWORD_PTR)ihsf.FrameOffset);
599 SymEnumSymbols(dbg_curr_process->handle, 0, NULL, info_locals_cb, (void*)(DWORD_PTR)ihsf.FrameOffset);
601 return TRUE;
605 static BOOL CALLBACK symbols_info_cb(SYMBOL_INFO* sym, ULONG size, void* ctx)
607 struct dbg_type type;
608 IMAGEHLP_MODULE mi;
610 mi.SizeOfStruct = sizeof(mi);
612 if (!SymGetModuleInfo(dbg_curr_process->handle, sym->ModBase, &mi))
613 mi.ModuleName[0] = '\0';
614 else
616 size_t len = strlen(mi.ModuleName);
617 if (len > 5 && !strcmp(mi.ModuleName + len - 5, "<elf>"))
618 mi.ModuleName[len - 5] = '\0';
621 dbg_printf("%08lx: %s!%s", (ULONG_PTR)sym->Address, mi.ModuleName, sym->Name);
622 type.id = sym->TypeIndex;
623 type.module = sym->ModBase;
625 if (sym->TypeIndex != dbg_itype_none && sym->TypeIndex != 0)
627 dbg_printf(" ");
628 types_print_type(&type, FALSE);
630 dbg_printf("\n");
631 return TRUE;
634 void symbol_info(const char* str)
636 char buffer[512];
637 DWORD opt;
639 if (strlen(str) + 3 >= sizeof(buffer))
641 dbg_printf("Symbol too long (%s)\n", str);
642 return;
644 buffer[0] = '*';
645 buffer[1] = '!';
646 strcpy(&buffer[2], str);
647 /* this is a wine specific options to return also ELF modules in the
648 * enumeration
650 SymSetOptions((opt = SymGetOptions()) | 0x40000000);
651 SymEnumSymbols(dbg_curr_process->handle, 0, buffer, symbols_info_cb, NULL);
652 SymSetOptions(opt);