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