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