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
;
209 if (strlen(name
) + 4 > sizeof(buffer
))
211 WINE_WARN("Too long symbol (%s)\n", name
);
217 sgv
.name
= &buffer
[2];
220 sgv
.bp_disp
= bp_disp
? TRUE
: FALSE
;
221 sgv
.do_thunks
= DBG_IVAR(AlwaysShowThunks
);
225 strcpy(&buffer
[2], name
);
227 /* this is a wine specific options to return also ELF modules in the
230 SymSetOptions((opt
= SymGetOptions()) | 0x40000000);
231 if (!SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, sgv_cb
, (void*)&sgv
))
237 if (!sgv
.num
&& (name
[0] != '_'))
240 strcpy(&buffer
[3], name
);
241 if (!SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, sgv_cb
, (void*)&sgv
))
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
);
260 dbg_printf("No symbols found for %s\n", name
);
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
))
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
);
293 print_address(&sgv
.syms
[i
].lvalue
.addr
, TRUE
);
300 if (input_read_line("=> ", buffer
, sizeof(buffer
)))
302 if (buffer
[0] == '\0') return sglv_aborted
;
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.
317 dbg_printf("More than one symbol named %s, picking the first one\n", name
);
320 *rtn
= sgv
.syms
[i
].lvalue
;
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");
334 /* FIXME: have to implement SymAddSymbol in dbghelp, but likely to link this with
344 if (!(symbolfile
= fopen(filename
, "r")))
346 WINE_WARN("Unable to open symbol table %s\n", filename
);
350 dbg_printf("Reading symbols from file %s\n", filename
);
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
,
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
)
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
;
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
;
410 case SymTagPublicSymbol
: break;
412 WINE_FIXME("Unexpected sym-tag 0x%08lx\n", sym
->Tag
);
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)
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 /***********************************************************************
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
)
445 sgv
.name
= &buffer
[2];
446 sgv
.filename
= filename
;
449 sgv
.do_thunks
= FALSE
;
453 strcpy(&buffer
[2], name
);
455 /* this is a wine specific options to return also ELF modules in the
458 SymSetOptions((opt
= SymGetOptions()) | 0x40000000);
459 if (!SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, sgv_cb
, (void*)&sgv
))
465 if (!sgv
.num
&& (name
[0] != '_'))
468 strcpy(&buffer
[3], name
);
469 if (!SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, sgv_cb
, (void*)&sgv
))
480 if (filename
) dbg_printf("No such function %s in %s\n", name
, filename
);
481 else dbg_printf("No such function %s\n", name
);
484 WINE_FIXME("Several found, returning first (may not be what you want)...\n");
486 return SymGetLineFromAddr(dbg_curr_process
->handle
,
487 (DWORD
)memory_to_linear_addr(&sgv
.syms
[0].lvalue
.addr
),
493 static BOOL CALLBACK
info_locals_cb(SYMBOL_INFO
* sym
, ULONG size
, void* ctx
)
497 const char* explain
= NULL
;
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",
518 for (div
= dbg_context_vars
; div
->name
; div
++)
520 if (div
->val
== sym
->Register
)
523 sprintf(buf
, "local in register %s", div
->name
);
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
);
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
);
557 static BOOL CALLBACK
symbols_info_cb(SYMBOL_INFO
* sym
, ULONG size
, void* ctx
)
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
);
571 void symbol_info(const char* str
)
576 if (strlen(str
) + 3 >= sizeof(buffer
))
578 dbg_printf("Symbol too long (%s)\n", str
);
583 strcpy(&buffer
[2], str
);
584 /* this is a wine specific options to return also ELF modules in the
587 SymSetOptions((opt
= SymGetOptions()) | 0x40000000);
588 SymEnumSymbols(dbg_curr_process
->handle
, 0, buffer
, symbols_info_cb
, NULL
);