dbghelp: Always set the size to public symbols to 1 when we don't know the size
[wine] / dlls / dbghelp / source.c
1 /*
2  * File source.c - source files management
3  *
4  * Copyright (C) 2004,      Eric Pouech.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21 #include "config.h"
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <assert.h>
26
27 #include "dbghelp_private.h"
28 #include "wine/debug.h"
29 #ifdef HAVE_REGEX_H
30 # include <regex.h>
31 #endif
32
33 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
34
35 /******************************************************************
36  *              source_find
37  *
38  * check whether a source file has already been stored
39  */
40 static unsigned source_find(const struct module* module, const char* name)
41 {
42     char*       ptr = module->sources;
43
44     while (*ptr)
45     {
46         if (strcmp(ptr, name) == 0) return ptr - module->sources;
47         ptr += strlen(ptr) + 1;
48     }
49     return (unsigned)-1;
50 }
51
52 /******************************************************************
53  *              source_new
54  *
55  * checks if source exists. if not, add it
56  */
57 unsigned source_new(struct module* module, const char* name)
58 {
59     int         len;
60     unsigned    ret;
61
62     if (!name) return (unsigned)-1;
63     if (module->sources && (ret = source_find(module, name)) != (unsigned)-1)
64         return ret;
65
66     len = strlen(name) + 1;
67     if (module->sources_used + len + 1 > module->sources_alloc)
68     {
69         /* Alloc by block of 256 bytes */
70         module->sources_alloc = (module->sources_used + len + 1 + 255) & ~255;
71         if (!module->sources)
72             module->sources = HeapAlloc(GetProcessHeap(), 0, module->sources_alloc);
73         else
74             module->sources = HeapReAlloc(GetProcessHeap(), 0, module->sources,
75                                           module->sources_alloc);
76     }
77     ret = module->sources_used;
78     strcpy(module->sources + module->sources_used, name);
79     module->sources_used += len;
80     module->sources[module->sources_used] = '\0';
81     return ret;
82 }
83
84 /******************************************************************
85  *              source_get
86  *
87  * returns a stored source file name
88  */
89 const char* source_get(const struct module* module, unsigned idx)
90 {
91     if (idx == -1) return "";
92     assert(module->sources);
93     return module->sources + idx;
94 }
95
96 /******************************************************************
97  *              SymEnumSourceFiles (DBGHELP.@)
98  *
99  */
100 BOOL WINAPI SymEnumSourceFiles(HANDLE hProcess, ULONG64 ModBase, PCSTR Mask,
101                                PSYM_ENUMSOURCEFILES_CALLBACK cbSrcFiles,
102                                PVOID UserContext)
103 {
104     struct process*     pcs;
105     struct module*      module;
106     SOURCEFILE          sf;
107     char*               ptr;
108     
109     if (!cbSrcFiles) return FALSE;
110     pcs = process_find_by_handle(hProcess);
111     if (!pcs) return FALSE;
112          
113     if (ModBase)
114     {
115         module = module_find_by_addr(pcs, ModBase, DMT_UNKNOWN);
116         if (!(module = module_get_debug(pcs, module))) return FALSE;
117     }
118     else
119     {
120         if (Mask[0] == '!')
121         {
122             module = module_find_by_name(pcs, Mask + 1, DMT_UNKNOWN);
123             if (!(module = module_get_debug(pcs, module))) return FALSE;
124         }
125         else
126         {
127             FIXME("Unsupported yet (should get info from current context)\n");
128             return FALSE;
129         }
130     }
131     if (!module->sources) return FALSE;
132     for (ptr = module->sources; *ptr; ptr += strlen(ptr) + 1)
133     {
134         /* FIXME: not using Mask */
135         sf.ModBase = ModBase;
136         sf.FileName = ptr;
137         if (!cbSrcFiles(&sf, UserContext)) break;
138     }
139
140     return TRUE;
141 }
142
143 /******************************************************************
144  *              SymEnumLines (DBGHELP.@)
145  *
146  */
147 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
148                          PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
149 {
150     struct process*             pcs;
151     struct module*              module;
152     struct hash_table_iter      hti;
153     struct symt_ht*             sym;
154     regex_t                     re;
155     struct line_info*           dli;
156     void*                       ptr;
157     SRCCODEINFO                 sci;
158     char*                       file;
159
160     if (!cb) return FALSE;
161     pcs = process_find_by_handle(hProcess);
162     if (!pcs) return FALSE;
163     if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
164     if (regcomp(&re, srcfile, REG_NOSUB))
165     {
166         FIXME("Couldn't compile %s\n", srcfile);
167         SetLastError(ERROR_INVALID_PARAMETER);
168         return FALSE;
169     }
170     if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
171     module = module_find_by_addr(pcs, base, DMT_UNKNOWN);
172     if (!(module = module_get_debug(pcs, module))) return FALSE;
173
174     sci.SizeOfStruct = sizeof(sci);
175     sci.ModBase      = base;
176
177     hash_table_iter_init(&module->ht_symbols, &hti, NULL);
178     while ((ptr = hash_table_iter_up(&hti)))
179     {
180         sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
181         if (sym->symt.tag != SymTagFunction) continue;
182
183         dli = NULL;
184         sci.FileName[0] = '\0';
185         while ((dli = vector_iter_up(&((struct symt_function*)sym)->vlines, dli)))
186         {
187             if (dli->is_source_file)
188             {
189                 file = (char*)source_get(module, dli->u.source_file);
190                 if (regexec(&re, file, 0, NULL, 0) != 0) file = "";
191                 strcpy(sci.FileName, file);
192             }
193             else if (sci.FileName[0])
194             {
195                 sci.Key = dli;
196                 sci.Obj[0] = '\0'; /* FIXME */
197                 sci.LineNumber = dli->line_number;
198                 sci.Address = dli->u.pc_offset;
199                 if (!cb(&sci, user)) break;
200             }
201         }
202     }
203     return TRUE;
204 }