ole32: Fix some leaks (coverity).
[wine] / programs / winedbg / symbol.c
1 /*
2  * Generate hash tables for Wine debugger symbols
3  *
4  * Copyright (C) 1993, Eric Youngdale.
5  *               2004-2005, Eric Pouech.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
24
25 #include "config.h"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "debugger.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
34
35 static BOOL symbol_get_debug_start(const struct dbg_type* func, ULONG64* start)
36 {
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             child;
42
43     if (!func->id) return FALSE; /* native dbghelp not always fills the info field */
44
45     if (!types_get_info(func, TI_GET_CHILDRENCOUNT, &count)) return FALSE;
46     fcp->Start = 0;
47     while (count)
48     {
49         fcp->Count = min(count, 256);
50         if (types_get_info(func, TI_FINDCHILDREN, fcp))
51         {
52             for (i = 0; i < min(fcp->Count, count); i++)
53             {
54                 child.module = func->module;
55                 child.id = fcp->ChildId[i];
56                 types_get_info(&child, TI_GET_SYMTAG, &tag);
57                 if (tag != SymTagFuncDebugStart) continue;
58                 return types_get_info(&child, TI_GET_ADDRESS, start);
59             }
60             count -= min(count, 256);
61             fcp->Start += 256;
62             fcp->Start += 256;
63         }
64     }
65     return FALSE;
66 }
67
68 static BOOL fill_sym_lvalue(const SYMBOL_INFO* sym, ULONG_PTR base,
69                             struct dbg_lvalue* lvalue, char* buffer, size_t sz)
70 {
71     if (buffer) buffer[0] = '\0';
72     if (sym->Flags & SYMFLAG_REGISTER)
73     {
74         DWORD_PTR* pval;
75
76         if (!memory_get_register(sym->Register, &pval, buffer, sz))
77             return FALSE;
78         lvalue->cookie = DLV_HOST;
79         lvalue->addr.Offset = (DWORD_PTR)pval;
80     }
81     else if (sym->Flags & SYMFLAG_REGREL)
82     {
83         DWORD_PTR* pval;
84         size_t  l;
85
86         *buffer++ = '['; sz--;
87         if (!memory_get_register(sym->Register, &pval, buffer, sz))
88             return FALSE;
89         l = strlen(buffer);
90         sz -= l;
91         buffer += l;
92         lvalue->cookie = DLV_TARGET;
93         lvalue->addr.Offset = (ULONG64)*pval + sym->Address;
94         if ((LONG_PTR)sym->Address >= 0)
95             snprintf(buffer, sz, "+%ld]", (ULONG_PTR)sym->Address);
96         else
97             snprintf(buffer, sz, "-%ld]", -(LONG_PTR)sym->Address);
98     }
99     else if (sym->Flags & SYMFLAG_VALUEPRESENT)
100     {
101         struct dbg_type type;
102         VARIANT         v;
103
104         type.module = sym->ModBase;
105         type.id = sym->info;
106
107         if (!types_get_info(&type, TI_GET_VALUE, &v))
108         {
109             if (buffer) snprintf(buffer, sz, "Couldn't get full value information for %s", sym->Name);
110             return FALSE;
111         }
112         else if (v.n1.n2.vt & VT_BYREF)
113         {
114             /* FIXME: this won't work for pointers or arrays, as we don't always
115              * know, if the value to be dereferenced lies in debuggee or
116              * debugger address space.
117              */
118             if (sym->Tag == SymTagPointerType || sym->Tag == SymTagArrayType)
119             {
120                 if (buffer) snprintf(buffer, sz, "Couldn't dereference pointer for const value for %s", sym->Name);
121                 return FALSE;
122             }
123             /* this is likely Wine's dbghelp which passes const values by reference
124              * (object is managed by dbghelp, hence in debugger address space)
125              */
126             lvalue->cookie = DLV_HOST;
127             lvalue->addr.Offset = (DWORD_PTR)sym->Value;
128         }
129         else
130         {
131             DWORD* pdw = (DWORD*)lexeme_alloc_size(sizeof(*pdw));
132             lvalue->cookie = DLV_HOST;
133             lvalue->addr.Offset = (DWORD_PTR)pdw;
134             *pdw = sym->Value;
135         }
136     }
137     else if (sym->Flags & SYMFLAG_LOCAL)
138     {
139         lvalue->cookie = DLV_TARGET;
140         lvalue->addr.Offset = base + sym->Address;
141     }
142     else if (sym->Flags & SYMFLAG_TLSREL)
143     {
144         PROCESS_BASIC_INFORMATION pbi;
145         THREAD_BASIC_INFORMATION  tbi;
146         DWORD_PTR                 addr;
147         PEB                       peb;
148         PEB_LDR_DATA              ldr_data;
149         PLIST_ENTRY               head, current;
150         LDR_MODULE                ldr_module;
151         unsigned                  tlsindex = -1;
152
153         if (NtQueryInformationProcess(dbg_curr_process->handle, ProcessBasicInformation,
154                                       &pbi, sizeof(pbi), NULL) ||
155             NtQueryInformationThread(dbg_curr_thread->handle, ThreadBasicInformation,
156                                      &tbi, sizeof(tbi), NULL))
157         {
158         tls_error:
159             if (buffer) snprintf(buffer, sz, "Cannot read TLS address\n");
160             return FALSE;
161         }
162         addr = (DWORD_PTR)&(((TEB*)tbi.TebBaseAddress)->ThreadLocalStoragePointer);
163         if (!dbg_read_memory((void*)addr, &addr, sizeof(addr)) ||
164             !dbg_read_memory(pbi.PebBaseAddress, &peb, sizeof(peb)) ||
165             !dbg_read_memory(peb.LdrData, &ldr_data, sizeof(ldr_data)))
166             goto tls_error;
167         current = ldr_data.InLoadOrderModuleList.Flink;
168         head = &((PEB_LDR_DATA*)peb.LdrData)->InLoadOrderModuleList;
169         do
170         {
171             if (!dbg_read_memory(CONTAINING_RECORD(current, LDR_MODULE, InLoadOrderModuleList),
172                                  &ldr_module, sizeof(ldr_module))) goto tls_error;
173             if ((DWORD_PTR)ldr_module.BaseAddress == sym->ModBase)
174             {
175                 tlsindex = ldr_module.TlsIndex;
176                 break;
177             }
178             current = ldr_module.InLoadOrderModuleList.Flink;
179         } while (current != head);
180
181         addr += tlsindex * sizeof(DWORD_PTR);
182         if (!dbg_read_memory((void*)addr, &addr, sizeof(addr))) goto tls_error;
183         lvalue->cookie = DLV_TARGET;
184         lvalue->addr.Offset = addr + sym->Address;
185     }
186     else
187     {
188         lvalue->cookie = DLV_TARGET;
189         lvalue->addr.Offset = sym->Address;
190     }
191     lvalue->addr.Mode = AddrModeFlat;
192     lvalue->type.module = sym->ModBase;
193     lvalue->type.id = sym->TypeIndex;
194
195     return TRUE;
196 }
197
198 struct sgv_data
199 {
200 #define NUMDBGV                 100
201     struct
202     {
203         /* FIXME: NUMDBGV should be made variable */
204         struct dbg_lvalue               lvalue;
205         DWORD                           flags;
206         DWORD                           sym_info;
207     }                           syms[NUMDBGV];  /* out     : will be filled in with various found symbols */
208     int                         num;            /* out     : number of found symbols */
209     int                         num_thunks;     /* out     : number of thunks found */
210     const char*                 name;           /* in      : name of symbol to look up */
211     unsigned                    do_thunks : 1;  /* in      : whether we return thunks tags */
212     ULONG64                     frame_offset;   /* in      : frame for local & parameter variables look up */
213 };
214
215 static BOOL CALLBACK sgv_cb(PSYMBOL_INFO sym, ULONG size, PVOID ctx)
216 {
217     struct sgv_data*    sgv = ctx;
218     unsigned            insp;
219     char                tmp[64];
220
221     if (sym->Flags & SYMFLAG_THUNK)
222     {
223         if (!sgv->do_thunks) return TRUE;
224         sgv->num_thunks++;
225     }
226
227     if (sgv->num >= NUMDBGV)
228     {
229         dbg_printf("Too many addresses for symbol '%s', limiting the first %d\n",
230                    sgv->name, NUMDBGV);
231         return FALSE;
232     }
233     WINE_TRACE("==> %s %s%s%s%s%s%s%s%s\n",
234                sym->Name,
235                (sym->Flags & SYMFLAG_FUNCTION) ? "func " : "",
236                (sym->Flags & SYMFLAG_FRAMEREL) ? "framerel " : "",
237                (sym->Flags & SYMFLAG_TLSREL) ? "tlsrel " : "",
238                (sym->Flags & SYMFLAG_REGISTER) ? "register " : "",
239                (sym->Flags & SYMFLAG_REGREL) ? "regrel " : "",
240                (sym->Flags & SYMFLAG_PARAMETER) ? "param " : "",
241                (sym->Flags & SYMFLAG_LOCAL) ? "local " : "",
242                (sym->Flags & SYMFLAG_THUNK) ? "thunk " : "");
243
244     /* always keep the thunks at end of the array */
245     insp = sgv->num;
246     if (sgv->num_thunks && !(sym->Flags & SYMFLAG_THUNK))
247     {
248         insp -= sgv->num_thunks;
249         memmove(&sgv->syms[insp + 1], &sgv->syms[insp],
250                 sizeof(sgv->syms[0]) * sgv->num_thunks);
251     }
252     if (!fill_sym_lvalue(sym, sgv->frame_offset, &sgv->syms[insp].lvalue, tmp, sizeof(tmp)))
253     {
254         dbg_printf("%s: %s\n", sym->Name, tmp);
255         return TRUE;
256     }
257     sgv->syms[insp].flags              = sym->Flags;
258     sgv->syms[insp].sym_info           = sym->info;
259     sgv->num++;
260
261     return TRUE;
262 }
263
264 enum sym_get_lval symbol_picker_interactive(const char* name, const struct sgv_data* sgv,
265                                             struct dbg_lvalue* rtn)
266 {
267     char        buffer[512];
268     unsigned    i;
269
270     if (!dbg_interactiveP)
271     {
272         dbg_printf("More than one symbol named %s, picking the first one\n", name);
273         *rtn = sgv->syms[0].lvalue;
274         return sglv_found;
275     }
276
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++)
280     {
281         if (sgv->num - sgv->num_thunks > 1 && (sgv->syms[i].flags & SYMFLAG_THUNK) && !DBG_IVAR(AlwaysShowThunks))
282             continue;
283         dbg_printf("[%d]: ", i + 1);
284         if (sgv->syms[i].flags & (SYMFLAG_LOCAL | SYMFLAG_PARAMETER))
285         {
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) " : "",
289                        name);
290         }
291         else if (sgv->syms[i].flags & SYMFLAG_THUNK)
292         {
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);
296         }
297         else
298         {
299             print_address(&sgv->syms[i].lvalue.addr, TRUE);
300             dbg_printf("\n");
301         }
302     }
303     do
304     {
305         if (input_read_line("=> ", buffer, sizeof(buffer)))
306         {
307             if (buffer[0] == '\0') return sglv_aborted;
308             i = atoi(buffer);
309             if (i < 1 || i > sgv->num)
310                 dbg_printf("Invalid choice %d\n", i);
311         }
312         else return sglv_aborted;
313     } while (i < 1 || i > sgv->num);
314
315     /* The array is 0-based, but the choices are 1..n,
316      * so we have to subtract one before returning.
317      */
318     *rtn = sgv->syms[i - 1].lvalue;
319     return sglv_found;
320 }
321
322 enum sym_get_lval symbol_picker_scoped(const char* name, const struct sgv_data* sgv,
323                                        struct dbg_lvalue* rtn)
324 {
325     unsigned i;
326     int local = -1;
327
328     for (i = 0; i < sgv->num; i++)
329     {
330         if (sgv->num - sgv->num_thunks > 1 && (sgv->syms[i].flags & SYMFLAG_THUNK) && !DBG_IVAR(AlwaysShowThunks))
331             continue;
332         if (sgv->syms[i].flags & (SYMFLAG_LOCAL | SYMFLAG_PARAMETER))
333         {
334             if (local == -1)
335                 local = i;
336             else
337             {
338                 /* FIXME: several locals with same name... which one to pick ?? */
339                 dbg_printf("Several local variables/parameters for %s, aborting\n", name);
340                 return sglv_aborted;
341             }
342         }
343     }
344     if (local != -1)
345     {
346         *rtn = sgv->syms[local].lvalue;
347         return sglv_found;
348     }
349     /* no locals found, multiple globals... abort for now */
350     dbg_printf("Several global variables for %s, aborting\n", name);
351     return sglv_aborted;
352 }
353
354 symbol_picker_t symbol_current_picker = symbol_picker_interactive;
355
356 /***********************************************************************
357  *           symbol_get_lvalue
358  *
359  * Get the address of a named symbol.
360  * Return values:
361  *      sglv_found:   if the symbol is found
362  *      sglv_unknown: if the symbol isn't found
363  *      sglv_aborted: some error occurred (likely, many symbols of same name exist,
364  *          and user didn't pick one of them)
365  */
366 enum sym_get_lval symbol_get_lvalue(const char* name, const int lineno,
367                                     struct dbg_lvalue* rtn, BOOL bp_disp)
368 {
369     struct sgv_data             sgv;
370     int                         i;
371     char                        buffer[512];
372     DWORD                       opt;
373     IMAGEHLP_STACK_FRAME        ihsf;
374
375     if (strlen(name) + 4 > sizeof(buffer))
376     {
377         WINE_WARN("Too long symbol (%s)\n", name);
378         return sglv_unknown;
379     }
380
381     sgv.num        = 0;
382     sgv.num_thunks = 0;
383     sgv.name       = &buffer[2];
384     sgv.do_thunks  = DBG_IVAR(AlwaysShowThunks);
385
386     if (strchr(name, '!'))
387     {
388         strcpy(buffer, name);
389     }
390     else
391     {
392         buffer[0] = '*';
393         buffer[1] = '!';
394         strcpy(&buffer[2], name);
395     }
396
397     /* this is a wine specific options to return also ELF modules in the
398      * enumeration
399      */
400     SymSetOptions((opt = SymGetOptions()) | 0x40000000);
401     SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv);
402
403     if (!sgv.num)
404     {
405         const char*   ptr = strchr(name, '!');
406         if ((ptr && ptr[1] != '_') || (!ptr && *name != '_'))
407         {
408             if (ptr)
409             {
410                 int offset = ptr - name;
411                 memcpy(buffer, name, offset + 1);
412                 buffer[offset + 1] = '_';
413                 strcpy(&buffer[offset + 2], ptr + 1);
414             }
415             else
416             {
417                 buffer[0] = '*';
418                 buffer[1] = '!';
419                 buffer[2] = '_';
420                 strcpy(&buffer[3], name);
421             }
422             SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv);
423         }
424     }
425     SymSetOptions(opt);
426
427     /* now grab local symbols */
428     if (stack_get_current_frame(&ihsf) && sgv.num < NUMDBGV)
429     {
430         sgv.frame_offset = ihsf.FrameOffset;
431         SymEnumSymbols(dbg_curr_process->handle, 0, name, sgv_cb, (void*)&sgv);
432     }
433
434     if (!sgv.num)
435     {
436         dbg_printf("No symbols found for %s\n", name);
437         return sglv_unknown;
438     }
439
440     /* recompute potential offsets for functions (linenumber, skip prolog) */
441     for (i = 0; i < sgv.num; i++)
442     {
443         if (sgv.syms[i].flags & (SYMFLAG_REGISTER|SYMFLAG_REGREL|SYMFLAG_LOCAL|SYMFLAG_THUNK))
444             continue;
445
446         if (lineno == -1)
447         {
448             struct dbg_type     type;
449             ULONG64             addr;
450
451             type.module = sgv.syms[i].lvalue.type.module;
452             type.id     = sgv.syms[i].sym_info;
453             if (bp_disp && symbol_get_debug_start(&type, &addr))
454                 sgv.syms[i].lvalue.addr.Offset = addr;
455         }
456         else
457         {
458             DWORD               disp;
459             IMAGEHLP_LINE64     il;
460             BOOL                found = FALSE;
461
462             il.SizeOfStruct = sizeof(il);
463             SymGetLineFromAddr64(dbg_curr_process->handle,
464                                  (DWORD_PTR)memory_to_linear_addr(&sgv.syms[i].lvalue.addr),
465                                  &disp, &il);
466             do
467             {
468                 if (lineno == il.LineNumber)
469                 {
470                     sgv.syms[i].lvalue.addr.Offset = il.Address;
471                     found = TRUE;
472                     break;
473                 }
474             } while (SymGetLineNext64(dbg_curr_process->handle, &il));
475             if (!found)
476                 WINE_FIXME("No line (%d) found for %s (setting to symbol start)\n",
477                            lineno, name);
478         }
479     }
480
481     if (sgv.num - sgv.num_thunks > 1 || /* many symbols non thunks (and showing only non thunks) */
482         (sgv.num > 1 && DBG_IVAR(AlwaysShowThunks)) || /* many symbols (showing symbols & thunks) */
483         (sgv.num == sgv.num_thunks && sgv.num_thunks > 1))
484     {
485         return symbol_current_picker(name, &sgv, rtn);
486     }
487     /* first symbol is the one we want:
488      * - only one symbol found,
489      * - or many symbols but only one non thunk when AlwaysShowThunks is FALSE
490      */
491     *rtn = sgv.syms[0].lvalue;
492     return sglv_found;
493 }
494
495 BOOL symbol_is_local(const char* name)
496 {
497     struct sgv_data             sgv;
498     IMAGEHLP_STACK_FRAME        ihsf;
499
500     sgv.num        = 0;
501     sgv.num_thunks = 0;
502     sgv.name       = name;
503     sgv.do_thunks  = FALSE;
504
505     if (stack_get_current_frame(&ihsf))
506     {
507         sgv.frame_offset = ihsf.FrameOffset;
508         SymEnumSymbols(dbg_curr_process->handle, 0, name, sgv_cb, (void*)&sgv);
509     }
510     return sgv.num > 0;
511 }
512
513 /***********************************************************************
514  *           symbol_read_symtable
515  *
516  * Read a symbol file into the hash table.
517  */
518 void symbol_read_symtable(const char* filename, unsigned long offset)
519 {
520     dbg_printf("No longer supported\n");
521
522 #if 0
523 /* FIXME: have to implement SymAddSymbol in dbghelp, but likely we'll need to link
524  * this with an already loaded module !! 
525  */
526     FILE*       symbolfile;
527     unsigned    addr;
528     char        type;
529     char*       cpnt;
530     char        buffer[256];
531     char        name[256];
532
533     if (!(symbolfile = fopen(filename, "r")))
534     {
535         WINE_WARN("Unable to open symbol table %s\n", filename);
536         return;
537     }
538
539     dbg_printf("Reading symbols from file %s\n", filename);
540
541     while (1)
542     {
543         fgets(buffer, sizeof(buffer), symbolfile);
544         if (feof(symbolfile)) break;
545
546         /* Strip any text after a # sign (i.e. comments) */
547         cpnt = strchr(buffer, '#');
548         if (cpnt) *cpnt = '\0';
549
550         /* Quietly ignore any lines that have just whitespace */
551         for (cpnt = buffer; *cpnt; cpnt++)
552         {
553             if (*cpnt != ' ' && *cpnt != '\t') break;
554         }
555         if (!*cpnt || *cpnt == '\n') continue;
556
557         if (sscanf(buffer, "%lx %c %s", &addr, &type, name) == 3)
558         {
559             if (value.addr.off + offset < value.addr.off)
560                 WINE_WARN("Address wrap around\n");
561             value.addr.off += offset;
562             SymAddSymbol(current_process->handle, BaseOfDll,
563                          name, addr, 0, 0);
564         }
565     }
566     fclose(symbolfile);
567 #endif
568 }
569
570 /***********************************************************************
571  *           symbol_get_function_line_status
572  *
573  * Find the symbol nearest to a given address.
574  */
575 enum dbg_line_status symbol_get_function_line_status(const ADDRESS64* addr)
576 {
577     IMAGEHLP_LINE64     il;
578     DWORD               disp;
579     ULONG64             disp64, start;
580     DWORD_PTR           lin = (DWORD_PTR)memory_to_linear_addr(addr);
581     char                buffer[sizeof(SYMBOL_INFO) + 256];
582     SYMBOL_INFO*        sym = (SYMBOL_INFO*)buffer;
583     struct dbg_type     func;
584
585     il.SizeOfStruct = sizeof(il);
586     sym->SizeOfStruct = sizeof(SYMBOL_INFO);
587     sym->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
588
589     /* do we have some info for lin address ? */
590     if (!SymFromAddr(dbg_curr_process->handle, lin, &disp64, sym))
591     {
592         ADDRESS64   jumpee;
593         /* some compilers insert thunks in their code without debug info associated
594          * take care of this situation
595          */
596         if (be_cpu->is_jump((void*)lin, &jumpee))
597             return symbol_get_function_line_status(&jumpee);
598         return dbg_no_line_info;
599     }
600
601     switch (sym->Tag)
602     {
603     case SymTagThunk:
604         /* FIXME: so far dbghelp doesn't return the 16 <=> 32 thunks
605          * and furthermore, we no longer take care of them !!!
606          */
607         return dbg_in_a_thunk;
608     case SymTagFunction:
609     case SymTagPublicSymbol: break;
610     default:
611         WINE_FIXME("Unexpected sym-tag 0x%08x\n", sym->Tag);
612     case SymTagData:
613         return dbg_no_line_info;
614     }
615     /* we should have a function now */
616     if (!SymGetLineFromAddr64(dbg_curr_process->handle, lin, &disp, &il))
617         return dbg_no_line_info;
618
619     func.module = sym->ModBase;
620     func.id     = sym->info;
621
622     if (symbol_get_debug_start(&func, &start) && lin < start)
623         return dbg_not_on_a_line_number;
624
625     if (!sym->Size) sym->Size = 0x100000;
626     if (il.FileName && il.FileName[0] && disp < sym->Size)
627         return (disp == 0) ? dbg_on_a_line_number : dbg_not_on_a_line_number;
628
629     return dbg_no_line_info;
630 }
631
632 /***********************************************************************
633  *           symbol_get_line
634  *
635  * Find the symbol nearest to a given address.
636  * Returns sourcefile name and line number in a format that the listing
637  * handler can deal with.
638  */
639 BOOL symbol_get_line(const char* filename, const char* name,
640                      IMAGEHLP_LINE64* line)
641 {
642     struct sgv_data     sgv;
643     char                buffer[512];
644     DWORD               opt, disp;
645     unsigned            i, found = FALSE;
646     IMAGEHLP_LINE64     il;
647
648     sgv.num        = 0;
649     sgv.num_thunks = 0;
650     sgv.name       = &buffer[2];
651     sgv.do_thunks  = FALSE;
652
653     buffer[0] = '*';
654     buffer[1] = '!';
655     strcpy(&buffer[2], name);
656
657     /* this is a wine specific options to return also ELF modules in the
658      * enumeration
659      */
660     SymSetOptions((opt = SymGetOptions()) | 0x40000000);
661     if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
662     {
663         SymSetOptions(opt);
664         return FALSE;
665     }
666
667     if (!sgv.num && (name[0] != '_'))
668     {
669         buffer[2] = '_';
670         strcpy(&buffer[3], name);
671         if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
672         {
673             SymSetOptions(opt);
674             return FALSE;
675         }
676     }
677     SymSetOptions(opt);
678
679     for (i = 0; i < sgv.num; i++)
680     {
681         DWORD_PTR linear = (DWORD_PTR)memory_to_linear_addr(&sgv.syms[i].lvalue.addr);
682
683         il.SizeOfStruct = sizeof(il);
684         if (!SymGetLineFromAddr64(dbg_curr_process->handle, linear, &disp, &il))
685             continue;
686         if (filename && strcmp(il.FileName, filename)) continue;
687         if (found)
688         {
689             WINE_FIXME("Several found, returning first (may not be what you want)...\n");
690             break;
691         }
692         found = TRUE;
693         *line = il;
694     }
695     if (!found)
696     {
697         if (filename)   dbg_printf("No such function %s in %s\n", name, filename);
698         else            dbg_printf("No such function %s\n", name);
699         return FALSE;
700     }
701     return TRUE;
702 }
703
704 /******************************************************************
705  *              symbol_print_local
706  *
707  * Overall format is:
708  * <name>=<value>                       in non detailed form
709  * <name>=<value> (local|pmt <where>)   in detailed form
710  * Note <value> can be an error message in case of error
711  */
712 void symbol_print_local(const SYMBOL_INFO* sym, DWORD_PTR base, BOOL detailed)
713 {
714     struct dbg_lvalue   lvalue;
715     char                buffer[64];
716
717     dbg_printf("%s=", sym->Name);
718
719     if (fill_sym_lvalue(sym, base, &lvalue, buffer, sizeof(buffer)))
720     {
721         print_value(&lvalue, 0, 1);
722         if (detailed)
723             dbg_printf(" (%s %s)",
724                        (sym->Flags & SYMFLAG_PARAMETER) ? "parameter" : "local",
725                        buffer);
726     }
727     else
728     {
729         dbg_printf("%s", buffer);
730         if (detailed)
731             dbg_printf(" (%s)",
732                        (sym->Flags & SYMFLAG_PARAMETER) ? "parameter" : "local");
733     }
734 }
735
736 static BOOL CALLBACK info_locals_cb(PSYMBOL_INFO sym, ULONG size, PVOID ctx)
737 {
738     struct dbg_type     type;
739
740     dbg_printf("\t");
741     type.module = sym->ModBase;
742     type.id = sym->TypeIndex;
743     types_print_type(&type, FALSE);
744
745     dbg_printf(" ");
746     symbol_print_local(sym, (DWORD_PTR)ctx, TRUE);
747     dbg_printf("\n");
748
749     return TRUE;
750 }
751
752 int symbol_info_locals(void)
753 {
754     IMAGEHLP_STACK_FRAME        ihsf;
755     ADDRESS64                   addr;
756
757     stack_get_current_frame(&ihsf);
758     addr.Mode = AddrModeFlat;
759     addr.Offset = ihsf.InstructionOffset;
760     print_address(&addr, FALSE);
761     dbg_printf(": (%08lx)\n", (DWORD_PTR)ihsf.FrameOffset);
762     SymEnumSymbols(dbg_curr_process->handle, 0, NULL, info_locals_cb, (void*)(DWORD_PTR)ihsf.FrameOffset);
763
764     return TRUE;
765
766 }
767
768 static BOOL CALLBACK symbols_info_cb(PSYMBOL_INFO sym, ULONG size, PVOID ctx)
769 {
770     struct dbg_type     type;
771     IMAGEHLP_MODULE     mi;
772
773     mi.SizeOfStruct = sizeof(mi);
774
775     if (!SymGetModuleInfo(dbg_curr_process->handle, sym->ModBase, &mi))
776         mi.ModuleName[0] = '\0';
777     else
778     {
779         size_t  len = strlen(mi.ModuleName);
780         if (len > 5 && !strcmp(mi.ModuleName + len - 5, "<elf>"))
781             mi.ModuleName[len - 5] = '\0';
782     }
783
784     dbg_printf("%08lx: %s!%s", (ULONG_PTR)sym->Address, mi.ModuleName, sym->Name);
785     type.id = sym->TypeIndex;
786     type.module = sym->ModBase;
787
788     if (sym->TypeIndex != dbg_itype_none && sym->TypeIndex != 0)
789     {
790         dbg_printf(" ");
791         types_print_type(&type, FALSE);
792     }
793     dbg_printf("\n");
794     return TRUE;
795 }
796
797 void symbol_info(const char* str)
798 {
799     char        buffer[512];
800     DWORD       opt;
801
802     if (strlen(str) + 3 >= sizeof(buffer))
803     {
804         dbg_printf("Symbol too long (%s)\n", str);
805         return;
806     }
807     buffer[0] = '*';
808     buffer[1] = '!';
809     strcpy(&buffer[2], str);
810     /* this is a wine specific options to return also ELF modules in the
811      * enumeration
812      */
813     SymSetOptions((opt = SymGetOptions()) | 0x40000000);
814     SymEnumSymbols(dbg_curr_process->handle, 0, buffer, symbols_info_cb, NULL);
815     SymSetOptions(opt);
816 }