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 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
)
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",
104 for (div
= dbg_context_vars
; div
->name
&& div
->val
!= sym
->Register
; div
++);
107 dbg_printf(" %s (register): couldn't find register %lu\n",
108 sym
->Name
, sym
->Register
);
111 addr
= (ULONG64
)(DWORD_PTR
)div
->pval
;
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
;
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
);
135 if (sgv
->lineno
== -1)
138 !symbol_get_debug_start(sym
->ModBase
, sym
->info
, &addr
))
146 if (sgv
->lineno
== il
.LineNumber
)
151 } while (SymGetLineNext(dbg_curr_process
->handle
, &il
));
154 WINE_FIXME("No line (%d) found for %s (setting to symbol)\n",
155 sgv
->lineno
, sgv
->name
);
161 if (sgv
->num
>= NUMDBGV
)
163 dbg_printf("Too many addresses for symbol '%s', limiting the first %d\n",
167 WINE_TRACE("==> %s %s%s%s%s%s%s%s\n",
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 */
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
;
196 /***********************************************************************
199 * Get the address of a named symbol.
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
)
213 IMAGEHLP_STACK_FRAME ihsf
;
215 if (strlen(name
) + 4 > sizeof(buffer
))
217 WINE_WARN("Too long symbol (%s)\n", name
);
223 sgv
.name
= &buffer
[2];
226 sgv
.bp_disp
= bp_disp
? TRUE
: FALSE
;
227 sgv
.do_thunks
= DBG_IVAR(AlwaysShowThunks
);
229 if (strchr(name
, '!'))
231 strcpy(buffer
, name
);
237 strcpy(&buffer
[2], name
);
240 /* this is a wine specific options to return also ELF modules in the
243 SymSetOptions((opt
= SymGetOptions()) | 0x40000000);
244 if (!SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, sgv_cb
, (void*)&sgv
))
250 if (!sgv
.num
&& (name
[0] != '_'))
252 char* ptr
= strchr(name
, '!');
256 memmove(ptr
+ 1, ptr
, strlen(ptr
));
264 strcpy(&buffer
[3], name
);
266 if (!SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, sgv_cb
, (void*)&sgv
))
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
);
283 dbg_printf("No symbols found for %s\n", name
);
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
))
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) " : "",
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
);
315 print_address(&sgv
.syms
[i
].lvalue
.addr
, TRUE
);
322 if (input_read_line("=> ", buffer
, sizeof(buffer
)))
324 if (buffer
[0] == '\0') return sglv_aborted
;
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.
339 /* FIXME: could display the list of non-picked up symbols */
341 dbg_printf("More than one symbol named %s, picking the first one\n", name
);
343 *rtn
= sgv
.syms
[i
].lvalue
;
347 BOOL
symbol_is_local(const char* name
)
350 IMAGEHLP_STACK_FRAME ihsf
;
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
);
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");
378 /* FIXME: have to implement SymAddSymbol in dbghelp, but likely we'll need to link
379 * this with an already loaded module !!
388 if (!(symbolfile
= fopen(filename
, "r")))
390 WINE_WARN("Unable to open symbol table %s\n", filename
);
394 dbg_printf("Reading symbols from file %s\n", filename
);
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
,
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
)
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
;
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
;
455 case SymTagPublicSymbol
: break;
457 WINE_FIXME("Unexpected sym-tag 0x%08lx\n", sym
->Tag
);
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 /***********************************************************************
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
)
489 sgv
.name
= &buffer
[2];
490 sgv
.filename
= filename
;
493 sgv
.do_thunks
= FALSE
;
497 strcpy(&buffer
[2], name
);
499 /* this is a wine specific options to return also ELF modules in the
502 SymSetOptions((opt
= SymGetOptions()) | 0x40000000);
503 if (!SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, sgv_cb
, (void*)&sgv
))
509 if (!sgv
.num
&& (name
[0] != '_'))
512 strcpy(&buffer
[3], name
);
513 if (!SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, sgv_cb
, (void*)&sgv
))
524 if (filename
) dbg_printf("No such function %s in %s\n", name
, filename
);
525 else dbg_printf("No such function %s\n", name
);
528 WINE_FIXME("Several found, returning first (may not be what you want)...\n");
530 return SymGetLineFromAddr(dbg_curr_process
->handle
,
531 (DWORD
)memory_to_linear_addr(&sgv
.syms
[0].lvalue
.addr
),
537 static BOOL CALLBACK
info_locals_cb(SYMBOL_INFO
* sym
, ULONG size
, void* ctx
)
540 const char* explain
= NULL
;
542 struct dbg_type type
;
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",
563 for (div
= dbg_context_vars
; div
->name
; div
++)
565 if (div
->val
== sym
->Register
)
568 sprintf(buf
, "local in register %s", div
->name
);
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
);
584 dbg_printf(" %s = 0x%8.8lx (%s)\n", sym
->Name
, val
, explain
);
589 int symbol_info_locals(void)
591 IMAGEHLP_STACK_FRAME ihsf
;
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
);
605 static BOOL CALLBACK
symbols_info_cb(SYMBOL_INFO
* sym
, ULONG size
, void* ctx
)
607 struct dbg_type type
;
610 mi
.SizeOfStruct
= sizeof(mi
);
612 if (!SymGetModuleInfo(dbg_curr_process
->handle
, sym
->ModBase
, &mi
))
613 mi
.ModuleName
[0] = '\0';
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)
628 types_print_type(&type
, FALSE
);
634 void symbol_info(const char* str
)
639 if (strlen(str
) + 3 >= sizeof(buffer
))
641 dbg_printf("Symbol too long (%s)\n", str
);
646 strcpy(&buffer
[2], str
);
647 /* this is a wine specific options to return also ELF modules in the
650 SymSetOptions((opt
= SymGetOptions()) | 0x40000000);
651 SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, symbols_info_cb
, NULL
);