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, DWORD
* start
)
38 char buffer
[sizeof(TI_FINDCHILDREN_PARAMS
) + 256 * sizeof(DWORD
)];
39 TI_FINDCHILDREN_PARAMS
* fcp
= (TI_FINDCHILDREN_PARAMS
*)buffer
;
42 if (!types_get_info(mod_base
, typeid, TI_GET_CHILDRENCOUNT
, &count
)) return FALSE
;
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);
66 /* FIXME: NUMDBGV should be made variable */
67 struct dbg_lvalue lvalue
;
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
;
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",
97 for (div
= dbg_context_vars
; div
->name
&& div
->val
!= sym
->Register
; div
++);
100 dbg_printf(" %s (register): couldn't find register %lu\n",
101 sym
->Name
, sym
->Register
);
104 addr
= (DWORD
)div
->pval
;
107 else if (sym
->Flags
& SYMFLAG_FRAMEREL
)
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
;
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
);
129 if (sgv
->lineno
== -1)
132 !symbol_get_debug_start(sym
->ModBase
, sym
->TypeIndex
, &addr
))
140 if (sgv
->lineno
== il
.LineNumber
)
145 } while (SymGetLineNext(dbg_curr_process
->handle
, &il
));
148 WINE_FIXME("No line (%d) found for %s (setting to symbol)\n",
149 sgv
->lineno
, sgv
->name
);
155 if (sgv
->num
>= NUMDBGV
)
157 dbg_printf("Too many addresses for symbol '%s', limiting the first %d\n",
161 WINE_TRACE("==> %s %s%s%s%s%s%s\n",
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 */
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
;
189 /***********************************************************************
192 * Get the address of a named symbol.
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
)
204 char tmp
[sizeof(SYMBOL_INFO
) + 256];
205 SYMBOL_INFO
* si
= (SYMBOL_INFO
*)tmp
;
208 if (strlen(name
) + 4 > sizeof(buffer
))
210 WINE_WARN("Too long symbol (%s)\n", name
);
216 sgv
.name
= &buffer
[2];
219 sgv
.bp_disp
= bp_disp
? TRUE
: FALSE
;
220 sgv
.do_thunks
= DBG_IVAR(AlwaysShowThunks
);
224 strcpy(&buffer
[2], name
);
226 if (!SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, sgv_cb
, (void*)&sgv
))
229 if (!sgv
.num
&& (name
[0] != '_'))
232 strcpy(&buffer
[3], name
);
233 if (!SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, sgv_cb
, (void*)&sgv
))
237 /* now grab local symbols */
238 si
->SizeOfStruct
= sizeof(*si
);
239 si
->MaxNameLen
= 256;
240 if (stack_get_frame(si
, &sgv
.ihsf
) && sgv
.num
< NUMDBGV
)
242 if (SymSetContext(dbg_curr_process
->handle
, &sgv
.ihsf
, NULL
))
243 SymEnumSymbols(dbg_curr_process
->handle
, 0, name
, sgv_cb
, (void*)&sgv
);
248 dbg_printf("No symbols found for %s\n", name
);
252 if (dbg_interactiveP
)
254 if (sgv
.num
- sgv
.num_thunks
> 1 || /* many symbols non thunks (and showing only non thunks) */
255 (sgv
.num
> 1 && DBG_IVAR(AlwaysShowThunks
)) || /* many symbols (showing symbols & thunks) */
256 (sgv
.num
== sgv
.num_thunks
&& sgv
.num_thunks
> 1))
258 dbg_printf("Many symbols with name '%s', "
259 "choose the one you want (<cr> to abort):\n", name
);
260 for (i
= 0; i
< sgv
.num
; i
++)
262 if (sgv
.num
- sgv
.num_thunks
> 1 && (sgv
.syms
[i
].flags
& SYMFLAG_THUNK
) && !DBG_IVAR(AlwaysShowThunks
))
264 dbg_printf("[%d]: ", i
+ 1);
265 if (sgv
.syms
[i
].flags
& SYMFLAG_LOCAL
)
267 dbg_printf("local variable of %s\n", si
->Name
);
269 else if (sgv
.syms
[i
].flags
& SYMFLAG_PARAMETER
)
271 dbg_printf("parameter of %s\n", si
->Name
);
273 else if (sgv
.syms
[i
].flags
& SYMFLAG_THUNK
)
275 print_address(&sgv
.syms
[i
].lvalue
.addr
, TRUE
);
276 /* FIXME: should display where the thunks points to */
277 dbg_printf(" thunk %s\n", name
);
281 print_address(&sgv
.syms
[i
].lvalue
.addr
, TRUE
);
288 if (input_read_line("=> ", buffer
, sizeof(buffer
)))
290 if (buffer
[0] == '\0') return sglv_aborted
;
292 if (i
< 1 || i
> sgv
.num
)
293 dbg_printf("Invalid choice %d\n", i
);
295 } while (i
< 1 || i
> sgv
.num
);
297 /* The array is 0-based, but the choices are 1..n,
298 * so we have to subtract one before returning.
305 dbg_printf("More than one symbol named %s, picking the first one\n", name
);
308 *rtn
= sgv
.syms
[i
].lvalue
;
312 /***********************************************************************
313 * symbol_read_symtable
315 * Read a symbol file into the hash table.
317 void symbol_read_symtable(const char* filename
, unsigned long offset
)
319 dbg_printf("No longer supported\n");
322 /* FIXME: have to implement SymAddSymbol in dbghelp, but likely to link this with
332 if (!(symbolfile
= fopen(filename
, "r")))
334 WINE_WARN("Unable to open symbol table %s\n", filename
);
338 dbg_printf("Reading symbols from file %s\n", filename
);
342 fgets(buffer
, sizeof(buffer
), symbolfile
);
343 if (feof(symbolfile
)) break;
345 /* Strip any text after a # sign (i.e. comments) */
346 cpnt
= strchr(buffer
, '#');
347 if (cpnt
) *cpnt
= '\0';
349 /* Quietly ignore any lines that have just whitespace */
350 for (cpnt
= buffer
; *cpnt
; cpnt
++)
352 if (*cpnt
!= ' ' && *cpnt
!= '\t') break;
354 if (!*cpnt
|| *cpnt
== '\n') continue;
356 if (sscanf(buffer
, "%lx %c %s", &addr
, &type
, name
) == 3)
358 if (value
.addr
.off
+ offset
< value
.addr
.off
)
359 WINE_WARN("Address wrap around\n");
360 value
.addr
.off
+= offset
;
361 SymAddSymbol(current_process
->handle
, BaseOfDll
,
369 /***********************************************************************
370 * symbol_get_function_line_status
372 * Find the symbol nearest to a given address.
374 enum dbg_line_status
symbol_get_function_line_status(const ADDRESS
* addr
)
377 DWORD disp
, start
, size
;
378 DWORD lin
= (DWORD
)memory_to_linear_addr(addr
);
379 char buffer
[sizeof(SYMBOL_INFO
) + 256];
380 SYMBOL_INFO
* sym
= (SYMBOL_INFO
*)buffer
;
382 il
.SizeOfStruct
= sizeof(il
);
383 sym
->SizeOfStruct
= sizeof(SYMBOL_INFO
);
384 sym
->MaxNameLen
= sizeof(buffer
) - sizeof(SYMBOL_INFO
);
386 /* do we have some info for lin address ? */
387 if (!SymFromAddr(dbg_curr_process
->handle
, lin
, NULL
, sym
))
388 return dbg_no_line_info
;
393 /* FIXME: so far dbghelp doesn't return the 16 <=> 32 thunks
394 * and furthermore, we no longer take care of them !!!
396 return dbg_in_a_thunk
;
398 case SymTagPublicSymbol
: break;
400 WINE_FIXME("Unexpected sym-tag 0x%08lx\n", sym
->Tag
);
402 return dbg_no_line_info
;
404 /* we should have a function now */
405 if (!SymGetLineFromAddr(dbg_curr_process
->handle
, lin
, &disp
, &il
))
406 return dbg_no_line_info
;
408 if (symbol_get_debug_start(sym
->ModBase
, sym
->TypeIndex
, &start
) && lin
< start
)
409 return dbg_not_on_a_line_number
;
410 if (!types_get_info(sym
->ModBase
, sym
->TypeIndex
, TI_GET_LENGTH
, &size
) || size
== 0)
412 if (il
.FileName
&& il
.FileName
[0] && disp
< size
)
413 return (disp
== 0) ? dbg_on_a_line_number
: dbg_not_on_a_line_number
;
415 return dbg_no_line_info
;
418 /***********************************************************************
421 * Find the symbol nearest to a given address.
422 * Returns sourcefile name and line number in a format that the listing
423 * handler can deal with.
425 BOOL
symbol_get_line(const char* filename
, const char* name
, IMAGEHLP_LINE
* line
)
432 sgv
.name
= &buffer
[2];
433 sgv
.filename
= filename
;
436 sgv
.do_thunks
= FALSE
;
440 strcpy(&buffer
[2], name
);
442 if (!SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, sgv_cb
, (void*)&sgv
))
445 if (!sgv
.num
&& (name
[0] != '_'))
448 strcpy(&buffer
[3], name
);
449 if (!SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, sgv_cb
, (void*)&sgv
))
456 if (filename
) dbg_printf("No such function %s in %s\n", name
, filename
);
457 else dbg_printf("No such function %s\n", name
);
460 WINE_FIXME("Several found, returning first (may not be what you want)...\n");
462 return SymGetLineFromAddr(dbg_curr_process
->handle
,
463 (DWORD
)memory_to_linear_addr(&sgv
.syms
[0].lvalue
.addr
),
469 static BOOL CALLBACK
info_locals_cb(SYMBOL_INFO
* sym
, ULONG size
, void* ctx
)
473 const char* explain
= NULL
;
477 types_get_info(sym
->ModBase
, sym
->TypeIndex
, TI_GET_TYPE
, &tid
);
478 types_print_type(sym
->ModBase
, tid
, FALSE
);
480 if (sym
->Flags
& SYMFLAG_LOCAL
) explain
= "local";
481 else if (sym
->Flags
& SYMFLAG_PARAMETER
) explain
= "parameter";
482 else if (sym
->Flags
& SYMFLAG_REGISTER
) explain
= buf
;
484 if (sym
->Flags
& SYMFLAG_REGISTER
)
486 const struct dbg_internal_var
* div
;
488 if (dbg_curr_frame
!= 0)
490 dbg_printf(" %s (register): << cannot display, not in correct frame\n",
494 for (div
= dbg_context_vars
; div
->name
; div
++)
496 if (div
->val
== sym
->Register
)
499 sprintf(buf
, "local in register %s", div
->name
);
504 else if (sym
->Flags
& SYMFLAG_FRAMEREL
)
506 types_get_info(sym
->ModBase
, sym
->TypeIndex
, TI_GET_OFFSET
, &v
);
507 v
+= ((IMAGEHLP_STACK_FRAME
*)ctx
)->FrameOffset
;
509 dbg_read_memory_verbose((void*)v
, &val
, sizeof(val
));
511 dbg_printf(" %s = 0x%8.8lx (%s)\n", sym
->Name
, val
, explain
);
516 int symbol_info_locals(void)
518 IMAGEHLP_STACK_FRAME ihsf
;
519 char buffer
[sizeof(SYMBOL_INFO
) + 256];
520 SYMBOL_INFO
* si
= (SYMBOL_INFO
*)buffer
;
522 si
->SizeOfStruct
= sizeof(*si
);
523 si
->MaxNameLen
= 256;
524 if (stack_get_frame(si
, &ihsf
))
526 dbg_printf("%s:\n", si
->Name
);
527 if (SymSetContext(dbg_curr_process
->handle
, &ihsf
, NULL
))
528 SymEnumSymbols(dbg_curr_process
->handle
, 0, NULL
, info_locals_cb
, &ihsf
);
533 static BOOL CALLBACK
symbols_info_cb(SYMBOL_INFO
* sym
, ULONG size
, void* ctx
)
537 dbg_printf("%08lx: %s (", sym
->Address
, sym
->Name
);
538 if (sym
->TypeIndex
!= dbg_itype_none
&& sym
->TypeIndex
!= 0 &&
539 types_get_info(sym
->ModBase
, sym
->TypeIndex
, TI_GET_TYPE
, &type
))
541 types_print_type(sym
->ModBase
, type
, FALSE
);
547 void symbol_info(const char* str
)
551 if (strlen(str
) + 3 >= sizeof(buffer
))
553 dbg_printf("Symbol too long (%s)\n", str
);
558 strcpy(&buffer
[2], str
);
559 SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, symbols_info_cb
, NULL
);