2 * Generate hash tables for Wine debugger symbols
4 * Copyright (C) 1993, Eric Youngdale.
5 * 2004-2005, 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 if (!typeid) return FALSE
; /* native dbghelp not always fills the info field */
44 type
.module
= mod_base
;
47 if (!types_get_info(&type
, TI_GET_CHILDRENCOUNT
, &count
)) return FALSE
;
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);
73 /* FIXME: NUMDBGV should be made variable */
74 struct dbg_lvalue lvalue
;
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
;
92 unsigned cookie
= DLV_TARGET
, insp
;
94 if (sym
->Flags
& SYMFLAG_REGISTER
)
98 if (!memory_get_register(sym
->Register
, &val
, tmp
, sizeof(tmp
)))
100 dbg_printf(" %s (register): %s\n", sym
->Name
, tmp
);
103 addr
= (ULONG64
)(DWORD_PTR
)val
;
106 else if (sym
->Flags
& SYMFLAG_LOCAL
) /* covers both local & parameters */
108 addr
= sgv
->frame_offset
+ sym
->Address
;
110 else if (sym
->Flags
& SYMFLAG_THUNK
)
112 if (!sgv
->do_thunks
) return TRUE
;
119 il
.SizeOfStruct
= sizeof(il
);
120 SymGetLineFromAddr(dbg_curr_process
->handle
, sym
->Address
, &disp
, &il
);
121 if (sgv
->filename
&& strcmp(sgv
->filename
, il
.FileName
))
123 WINE_FIXME("File name mismatch (%s / %s)\n", sgv
->filename
, il
.FileName
);
127 if (sgv
->lineno
== -1)
130 !symbol_get_debug_start(sym
->ModBase
, sym
->info
, &addr
))
138 if (sgv
->lineno
== il
.LineNumber
)
143 } while (SymGetLineNext(dbg_curr_process
->handle
, &il
));
146 WINE_FIXME("No line (%d) found for %s (setting to symbol)\n",
147 sgv
->lineno
, sgv
->name
);
153 if (sgv
->num
>= NUMDBGV
)
155 dbg_printf("Too many addresses for symbol '%s', limiting the first %d\n",
159 WINE_TRACE("==> %s %s%s%s%s%s%s%s\n",
161 (sym
->Flags
& SYMFLAG_FUNCTION
) ? "func " : "",
162 (sym
->Flags
& SYMFLAG_FRAMEREL
) ? "framerel " : "",
163 (sym
->Flags
& SYMFLAG_REGISTER
) ? "register " : "",
164 (sym
->Flags
& SYMFLAG_REGREL
) ? "regrel " : "",
165 (sym
->Flags
& SYMFLAG_PARAMETER
) ? "param " : "",
166 (sym
->Flags
& SYMFLAG_LOCAL
) ? "local " : "",
167 (sym
->Flags
& SYMFLAG_THUNK
) ? "thunk " : "");
169 /* always keep the thunks at end of the array */
171 if (sgv
->num_thunks
&& !(sym
->Flags
& SYMFLAG_THUNK
))
173 insp
-= sgv
->num_thunks
;
174 memmove(&sgv
->syms
[insp
+ 1], &sgv
->syms
[insp
],
175 sizeof(sgv
->syms
[0]) * sgv
->num_thunks
);
177 sgv
->syms
[insp
].lvalue
.cookie
= cookie
;
178 sgv
->syms
[insp
].lvalue
.addr
.Mode
= AddrModeFlat
;
179 sgv
->syms
[insp
].lvalue
.addr
.Offset
= addr
;
180 sgv
->syms
[insp
].lvalue
.type
.module
= sym
->ModBase
;
181 sgv
->syms
[insp
].lvalue
.type
.id
= sym
->TypeIndex
;
182 sgv
->syms
[insp
].flags
= sym
->Flags
;
188 /***********************************************************************
191 * Get the address of a named symbol.
193 * sglv_found: if the symbol is found
194 * sglv_unknown: if the symbol isn't found
195 * sglv_aborted: some error occurred (likely, many symbols of same name exist,
196 * and user didn't pick one of them)
198 enum sym_get_lval
symbol_get_lvalue(const char* name
, const int lineno
,
199 struct dbg_lvalue
* rtn
, BOOL bp_disp
)
205 IMAGEHLP_STACK_FRAME ihsf
;
207 if (strlen(name
) + 4 > sizeof(buffer
))
209 WINE_WARN("Too long symbol (%s)\n", name
);
215 sgv
.name
= &buffer
[2];
218 sgv
.bp_disp
= bp_disp
? TRUE
: FALSE
;
219 sgv
.do_thunks
= DBG_IVAR(AlwaysShowThunks
);
221 if (strchr(name
, '!'))
223 strcpy(buffer
, name
);
229 strcpy(&buffer
[2], name
);
232 /* this is a wine specific options to return also ELF modules in the
235 SymSetOptions((opt
= SymGetOptions()) | 0x40000000);
236 SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, sgv_cb
, (void*)&sgv
);
238 if (!sgv
.num
&& (name
[0] != '_'))
240 char* ptr
= strchr(name
, '!');
244 memmove(ptr
+ 1, ptr
, strlen(ptr
));
252 strcpy(&buffer
[3], name
);
254 SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, sgv_cb
, (void*)&sgv
);
258 /* now grab local symbols */
259 if (stack_get_current_frame(&ihsf
) && sgv
.num
< NUMDBGV
)
261 sgv
.frame_offset
= ihsf
.FrameOffset
;
262 SymEnumSymbols(dbg_curr_process
->handle
, 0, name
, sgv_cb
, (void*)&sgv
);
267 dbg_printf("No symbols found for %s\n", name
);
271 if (dbg_interactiveP
)
273 if (sgv
.num
- sgv
.num_thunks
> 1 || /* many symbols non thunks (and showing only non thunks) */
274 (sgv
.num
> 1 && DBG_IVAR(AlwaysShowThunks
)) || /* many symbols (showing symbols & thunks) */
275 (sgv
.num
== sgv
.num_thunks
&& sgv
.num_thunks
> 1))
277 dbg_printf("Many symbols with name '%s', "
278 "choose the one you want (<cr> to abort):\n", name
);
279 for (i
= 0; i
< sgv
.num
; i
++)
281 if (sgv
.num
- sgv
.num_thunks
> 1 && (sgv
.syms
[i
].flags
& SYMFLAG_THUNK
) && !DBG_IVAR(AlwaysShowThunks
))
283 dbg_printf("[%d]: ", i
+ 1);
284 if (sgv
.syms
[i
].flags
& SYMFLAG_LOCAL
)
286 dbg_printf("%s %sof %s\n",
287 sgv
.syms
[i
].flags
& SYMFLAG_PARAMETER
? "Parameter" : "Local variable",
288 sgv
.syms
[i
].flags
& (SYMFLAG_REGISTER
|SYMFLAG_REGREL
) ? "(in a register) " : "",
291 else if (sgv
.syms
[i
].flags
& SYMFLAG_THUNK
)
293 print_address(&sgv
.syms
[i
].lvalue
.addr
, TRUE
);
294 /* FIXME: should display where the thunks points to */
295 dbg_printf(" thunk %s\n", name
);
299 print_address(&sgv
.syms
[i
].lvalue
.addr
, TRUE
);
306 if (input_read_line("=> ", buffer
, sizeof(buffer
)))
308 if (buffer
[0] == '\0') return sglv_aborted
;
310 if (i
< 1 || i
> sgv
.num
)
311 dbg_printf("Invalid choice %d\n", i
);
313 } while (i
< 1 || i
> sgv
.num
);
315 /* The array is 0-based, but the choices are 1..n,
316 * so we have to subtract one before returning.
323 /* FIXME: could display the list of non-picked up symbols */
325 dbg_printf("More than one symbol named %s, picking the first one\n", name
);
327 *rtn
= sgv
.syms
[i
].lvalue
;
331 BOOL
symbol_is_local(const char* name
)
334 IMAGEHLP_STACK_FRAME ihsf
;
342 sgv
.do_thunks
= FALSE
;
344 if (stack_get_current_frame(&ihsf
))
346 sgv
.frame_offset
= ihsf
.FrameOffset
;
347 SymEnumSymbols(dbg_curr_process
->handle
, 0, name
, sgv_cb
, (void*)&sgv
);
352 /***********************************************************************
353 * symbol_read_symtable
355 * Read a symbol file into the hash table.
357 void symbol_read_symtable(const char* filename
, unsigned long offset
)
359 dbg_printf("No longer supported\n");
362 /* FIXME: have to implement SymAddSymbol in dbghelp, but likely we'll need to link
363 * this with an already loaded module !!
372 if (!(symbolfile
= fopen(filename
, "r")))
374 WINE_WARN("Unable to open symbol table %s\n", filename
);
378 dbg_printf("Reading symbols from file %s\n", filename
);
382 fgets(buffer
, sizeof(buffer
), symbolfile
);
383 if (feof(symbolfile
)) break;
385 /* Strip any text after a # sign (i.e. comments) */
386 cpnt
= strchr(buffer
, '#');
387 if (cpnt
) *cpnt
= '\0';
389 /* Quietly ignore any lines that have just whitespace */
390 for (cpnt
= buffer
; *cpnt
; cpnt
++)
392 if (*cpnt
!= ' ' && *cpnt
!= '\t') break;
394 if (!*cpnt
|| *cpnt
== '\n') continue;
396 if (sscanf(buffer
, "%lx %c %s", &addr
, &type
, name
) == 3)
398 if (value
.addr
.off
+ offset
< value
.addr
.off
)
399 WINE_WARN("Address wrap around\n");
400 value
.addr
.off
+= offset
;
401 SymAddSymbol(current_process
->handle
, BaseOfDll
,
409 /***********************************************************************
410 * symbol_get_function_line_status
412 * Find the symbol nearest to a given address.
414 enum dbg_line_status
symbol_get_function_line_status(const ADDRESS64
* addr
)
418 ULONG64 disp64
, start
;
419 DWORD lin
= (DWORD
)memory_to_linear_addr(addr
);
420 char buffer
[sizeof(SYMBOL_INFO
) + 256];
421 SYMBOL_INFO
* sym
= (SYMBOL_INFO
*)buffer
;
423 il
.SizeOfStruct
= sizeof(il
);
424 sym
->SizeOfStruct
= sizeof(SYMBOL_INFO
);
425 sym
->MaxNameLen
= sizeof(buffer
) - sizeof(SYMBOL_INFO
);
427 /* do we have some info for lin address ? */
428 if (!SymFromAddr(dbg_curr_process
->handle
, lin
, &disp64
, sym
))
429 return dbg_no_line_info
;
434 /* FIXME: so far dbghelp doesn't return the 16 <=> 32 thunks
435 * and furthermore, we no longer take care of them !!!
437 return dbg_in_a_thunk
;
439 case SymTagPublicSymbol
: break;
441 WINE_FIXME("Unexpected sym-tag 0x%08lx\n", sym
->Tag
);
443 return dbg_no_line_info
;
445 /* we should have a function now */
446 if (!SymGetLineFromAddr(dbg_curr_process
->handle
, lin
, &disp
, &il
))
447 return dbg_no_line_info
;
449 if (symbol_get_debug_start(sym
->ModBase
, sym
->info
, &start
) && lin
< start
)
450 return dbg_not_on_a_line_number
;
451 if (!sym
->Size
) sym
->Size
= 0x100000;
452 if (il
.FileName
&& il
.FileName
[0] && disp
< sym
->Size
)
453 return (disp
== 0) ? dbg_on_a_line_number
: dbg_not_on_a_line_number
;
455 return dbg_no_line_info
;
458 /***********************************************************************
461 * Find the symbol nearest to a given address.
462 * Returns sourcefile name and line number in a format that the listing
463 * handler can deal with.
465 BOOL
symbol_get_line(const char* filename
, const char* name
, IMAGEHLP_LINE
* line
)
473 sgv
.name
= &buffer
[2];
474 sgv
.filename
= filename
;
477 sgv
.do_thunks
= FALSE
;
481 strcpy(&buffer
[2], name
);
483 /* this is a wine specific options to return also ELF modules in the
486 SymSetOptions((opt
= SymGetOptions()) | 0x40000000);
487 if (!SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, sgv_cb
, (void*)&sgv
))
493 if (!sgv
.num
&& (name
[0] != '_'))
496 strcpy(&buffer
[3], name
);
497 if (!SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, sgv_cb
, (void*)&sgv
))
508 if (filename
) dbg_printf("No such function %s in %s\n", name
, filename
);
509 else dbg_printf("No such function %s\n", name
);
512 WINE_FIXME("Several found, returning first (may not be what you want)...\n");
514 return SymGetLineFromAddr(dbg_curr_process
->handle
,
515 (DWORD
)memory_to_linear_addr(&sgv
.syms
[0].lvalue
.addr
),
521 static BOOL CALLBACK
info_locals_cb(SYMBOL_INFO
* sym
, ULONG size
, void* ctx
)
525 struct dbg_type type
;
528 type
.module
= sym
->ModBase
;
529 type
.id
= sym
->TypeIndex
;
530 types_print_type(&type
, FALSE
);
534 if (sym
->Flags
& SYMFLAG_REGISTER
)
538 if (!memory_get_register(sym
->Register
, &pval
, tmp
, sizeof(tmp
)))
540 dbg_printf(" %s (register): %s\n", sym
->Name
, tmp
);
543 sprintf(buf
, " in register %s", tmp
);
546 else if (sym
->Flags
& SYMFLAG_LOCAL
)
548 type
.id
= sym
->TypeIndex
;
549 v
= (ULONG
)ctx
+ sym
->Address
;
551 if (!dbg_read_memory((void*)v
, &val
, sizeof(val
)))
553 dbg_printf(" %s (%s) *** cannot read at 0x%08lx\n",
554 sym
->Name
, (sym
->Flags
& SYMFLAG_PARAMETER
) ? "parameter" : "local",
559 dbg_printf(" %s = 0x%8.8lx (%s%s)\n", sym
->Name
, val
,
560 (sym
->Flags
& SYMFLAG_PARAMETER
) ? "parameter" : "local", buf
);
565 int symbol_info_locals(void)
567 IMAGEHLP_STACK_FRAME ihsf
;
570 stack_get_current_frame(&ihsf
);
571 addr
.Mode
= AddrModeFlat
;
572 addr
.Offset
= ihsf
.InstructionOffset
;
573 print_address(&addr
, FALSE
);
574 dbg_printf(": (%08lx)\n", (DWORD_PTR
)ihsf
.FrameOffset
);
575 SymEnumSymbols(dbg_curr_process
->handle
, 0, NULL
, info_locals_cb
, (void*)(DWORD_PTR
)ihsf
.FrameOffset
);
581 static BOOL CALLBACK
symbols_info_cb(SYMBOL_INFO
* sym
, ULONG size
, void* ctx
)
583 struct dbg_type type
;
586 mi
.SizeOfStruct
= sizeof(mi
);
588 if (!SymGetModuleInfo(dbg_curr_process
->handle
, sym
->ModBase
, &mi
))
589 mi
.ModuleName
[0] = '\0';
592 size_t len
= strlen(mi
.ModuleName
);
593 if (len
> 5 && !strcmp(mi
.ModuleName
+ len
- 5, "<elf>"))
594 mi
.ModuleName
[len
- 5] = '\0';
597 dbg_printf("%08lx: %s!%s", (ULONG_PTR
)sym
->Address
, mi
.ModuleName
, sym
->Name
);
598 type
.id
= sym
->TypeIndex
;
599 type
.module
= sym
->ModBase
;
601 if (sym
->TypeIndex
!= dbg_itype_none
&& sym
->TypeIndex
!= 0)
604 types_print_type(&type
, FALSE
);
610 void symbol_info(const char* str
)
615 if (strlen(str
) + 3 >= sizeof(buffer
))
617 dbg_printf("Symbol too long (%s)\n", str
);
622 strcpy(&buffer
[2], str
);
623 /* this is a wine specific options to return also ELF modules in the
626 SymSetOptions((opt
= SymGetOptions()) | 0x40000000);
627 SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, symbols_info_cb
, NULL
);