riched20: Fix one more memory leak.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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* base, const char* name)
58 {
59     int         len;
60     unsigned    ret;
61     const char* full;
62     char*       tmp = NULL;
63
64     if (!name) return (unsigned)-1;
65     if (!base || *name == '/')
66         full = name;
67     else
68     {
69         unsigned bsz = strlen(base);
70
71         tmp = HeapAlloc(GetProcessHeap(), 0, bsz + 1 + strlen(name) + 1);
72         if (!tmp) return (unsigned)-1;
73         full = tmp;
74         strcpy(tmp, base);
75         if (tmp[bsz - 1] != '/') tmp[bsz++] = '/';
76         strcpy(&tmp[bsz], name);
77     }
78     if (module->sources && (ret = source_find(module, full)) != (unsigned)-1)
79         return ret;
80
81     len = strlen(full) + 1;
82     if (module->sources_used + len + 1 > module->sources_alloc)
83     {
84         /* Alloc by block of 256 bytes */
85         module->sources_alloc = (module->sources_used + len + 1 + 255) & ~255;
86         if (!module->sources)
87             module->sources = HeapAlloc(GetProcessHeap(), 0, module->sources_alloc);
88         else
89             module->sources = HeapReAlloc(GetProcessHeap(), 0, module->sources,
90                                           module->sources_alloc);
91     }
92     ret = module->sources_used;
93     strcpy(module->sources + module->sources_used, full);
94     module->sources_used += len;
95     module->sources[module->sources_used] = '\0';
96     HeapFree(GetProcessHeap(), 0, tmp);
97     return ret;
98 }
99
100 /******************************************************************
101  *              source_get
102  *
103  * returns a stored source file name
104  */
105 const char* source_get(const struct module* module, unsigned idx)
106 {
107     if (idx == -1) return "";
108     assert(module->sources);
109     return module->sources + idx;
110 }
111
112 /******************************************************************
113  *              SymEnumSourceFiles (DBGHELP.@)
114  *
115  */
116 BOOL WINAPI SymEnumSourceFiles(HANDLE hProcess, ULONG64 ModBase, PCSTR Mask,
117                                PSYM_ENUMSOURCEFILES_CALLBACK cbSrcFiles,
118                                PVOID UserContext)
119 {
120     struct process*     pcs;
121     struct module_pair  pair;
122     SOURCEFILE          sf;
123     char*               ptr;
124     
125     if (!cbSrcFiles) return FALSE;
126     pcs = process_find_by_handle(hProcess);
127     if (!pcs) return FALSE;
128          
129     if (ModBase)
130     {
131         pair.requested = module_find_by_addr(pcs, ModBase, DMT_UNKNOWN);
132         if (!module_get_debug(pcs, &pair)) return FALSE;
133     }
134     else
135     {
136         if (Mask[0] == '!')
137         {
138             pair.requested = module_find_by_name(pcs, Mask + 1, DMT_UNKNOWN);
139             if (!module_get_debug(pcs, &pair)) return FALSE;
140         }
141         else
142         {
143             FIXME("Unsupported yet (should get info from current context)\n");
144             return FALSE;
145         }
146     }
147     if (!pair.effective->sources) return FALSE;
148     for (ptr = pair.effective->sources; *ptr; ptr += strlen(ptr) + 1)
149     {
150         /* FIXME: not using Mask */
151         sf.ModBase = ModBase;
152         sf.FileName = ptr;
153         if (!cbSrcFiles(&sf, UserContext)) break;
154     }
155
156     return TRUE;
157 }
158
159 /******************************************************************
160  *              SymEnumLines (DBGHELP.@)
161  *
162  */
163 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
164                          PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
165 {
166     struct process*             pcs;
167     struct module_pair          pair;
168     struct hash_table_iter      hti;
169     struct symt_ht*             sym;
170     regex_t                     re;
171     struct line_info*           dli;
172     void*                       ptr;
173     SRCCODEINFO                 sci;
174     const char*                 file;
175
176     if (!cb) return FALSE;
177     pcs = process_find_by_handle(hProcess);
178     if (!pcs) return FALSE;
179     if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
180     if (regcomp(&re, srcfile, REG_NOSUB))
181     {
182         FIXME("Couldn't compile %s\n", srcfile);
183         SetLastError(ERROR_INVALID_PARAMETER);
184         return FALSE;
185     }
186     if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
187     pair.requested = module_find_by_addr(pcs, base, DMT_UNKNOWN);
188     if (!module_get_debug(pcs, &pair)) return FALSE;
189
190     sci.SizeOfStruct = sizeof(sci);
191     sci.ModBase      = base;
192
193     hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
194     while ((ptr = hash_table_iter_up(&hti)))
195     {
196         sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
197         if (sym->symt.tag != SymTagFunction) continue;
198
199         dli = NULL;
200         sci.FileName[0] = '\0';
201         while ((dli = vector_iter_up(&((struct symt_function*)sym)->vlines, dli)))
202         {
203             if (dli->is_source_file)
204             {
205                 file = source_get(pair.effective, dli->u.source_file);
206                 if (regexec(&re, file, 0, NULL, 0) != 0) file = "";
207                 strcpy(sci.FileName, file);
208             }
209             else if (sci.FileName[0])
210             {
211                 sci.Key = dli;
212                 sci.Obj[0] = '\0'; /* FIXME */
213                 sci.LineNumber = dli->line_number;
214                 sci.Address = dli->u.pc_offset;
215                 if (!cb(&sci, user)) break;
216             }
217         }
218     }
219     return TRUE;
220 }
221
222 /******************************************************************
223  *              SymGetSourceFileToken (DBGHELP.@)
224  *
225  */
226 BOOL WINAPI SymGetSourceFileToken(HANDLE hProcess, ULONG64 base,
227                                   PCSTR src, PVOID* token, DWORD* size)
228 {
229     FIXME("%p %s %s %p %p: stub!\n",
230           hProcess, wine_dbgstr_longlong(base), debugstr_a(src), token, size);
231     SetLastError(ERROR_NOT_SUPPORTED);
232     return FALSE;
233 }
234
235 /******************************************************************
236  *              SymGetSourceFileTokenW (DBGHELP.@)
237  *
238  */
239 BOOL WINAPI SymGetSourceFileTokenW(HANDLE hProcess, ULONG64 base,
240                                    PCWSTR src, PVOID* token, DWORD* size)
241 {
242     FIXME("%p %s %s %p %p: stub!\n",
243           hProcess, wine_dbgstr_longlong(base), debugstr_w(src), token, size);
244     SetLastError(ERROR_NOT_SUPPORTED);
245     return FALSE;
246 }