Synchronize with trunk revision 59636 (just before Alex's CreateProcess revamp).
[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
29 #ifndef DBGHELP_STATIC_LIB
30 #include <wine/debug.h>
31 #endif
32
33 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
34
35 static struct module* rb_module;
36 struct source_rb
37 {
38 struct wine_rb_entry entry;
39 unsigned source;
40 };
41
42 static void *source_rb_alloc(size_t size)
43 {
44 return HeapAlloc(GetProcessHeap(), 0, size);
45 }
46
47 static void *source_rb_realloc(void *ptr, size_t size)
48 {
49 return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
50 }
51
52 static void source_rb_free(void *ptr)
53 {
54 HeapFree(GetProcessHeap(), 0, ptr);
55 }
56
57 static int source_rb_compare(const void *key, const struct wine_rb_entry *entry)
58 {
59 const struct source_rb *t = WINE_RB_ENTRY_VALUE(entry, const struct source_rb, entry);
60
61 return strcmp((const char*)key, rb_module->sources + t->source);
62 }
63
64 const struct wine_rb_functions source_rb_functions =
65 {
66 source_rb_alloc,
67 source_rb_realloc,
68 source_rb_free,
69 source_rb_compare,
70 };
71
72 /******************************************************************
73 * source_find
74 *
75 * check whether a source file has already been stored
76 */
77 static unsigned source_find(const char* name)
78 {
79 struct wine_rb_entry* e;
80
81 e = wine_rb_get(&rb_module->sources_offsets_tree, name);
82 if (!e) return -1;
83 return WINE_RB_ENTRY_VALUE(e, struct source_rb, entry)->source;
84 }
85
86 /******************************************************************
87 * source_new
88 *
89 * checks if source exists. if not, add it
90 */
91 unsigned source_new(struct module* module, const char* base, const char* name)
92 {
93 unsigned ret = -1;
94 const char* full;
95 char* tmp = NULL;
96
97 if (!name) return ret;
98 if (!base || *name == '/')
99 full = name;
100 else
101 {
102 unsigned bsz = strlen(base);
103
104 tmp = HeapAlloc(GetProcessHeap(), 0, bsz + 1 + strlen(name) + 1);
105 if (!tmp) return ret;
106 full = tmp;
107 strcpy(tmp, base);
108 if (tmp[bsz - 1] != '/') tmp[bsz++] = '/';
109 strcpy(&tmp[bsz], name);
110 }
111 rb_module = module;
112 if (!module->sources || (ret = source_find(full)) == (unsigned)-1)
113 {
114 char* new;
115 int len = strlen(full) + 1;
116 struct source_rb* rb;
117
118 if (module->sources_used + len + 1 > module->sources_alloc)
119 {
120 if (!module->sources)
121 {
122 module->sources_alloc = (module->sources_used + len + 1 + 255) & ~255;
123 new = HeapAlloc(GetProcessHeap(), 0, module->sources_alloc);
124 }
125 else
126 {
127 module->sources_alloc = max( module->sources_alloc * 2,
128 (module->sources_used + len + 1 + 255) & ~255 );
129 new = HeapReAlloc(GetProcessHeap(), 0, module->sources,
130 module->sources_alloc);
131 }
132 if (!new) goto done;
133 module->sources = new;
134 }
135 ret = module->sources_used;
136 memcpy(module->sources + module->sources_used, full, len);
137 module->sources_used += len;
138 module->sources[module->sources_used] = '\0';
139 if ((rb = pool_alloc(&module->pool, sizeof(*rb))))
140 {
141 rb->source = ret;
142 wine_rb_put(&module->sources_offsets_tree, full, &rb->entry);
143 }
144 }
145 done:
146 HeapFree(GetProcessHeap(), 0, tmp);
147 return ret;
148 }
149
150 /******************************************************************
151 * source_get
152 *
153 * returns a stored source file name
154 */
155 const char* source_get(const struct module* module, unsigned idx)
156 {
157 if (idx == -1) return "";
158 assert(module->sources);
159 return module->sources + idx;
160 }
161
162 /******************************************************************
163 * SymEnumSourceFilesW (DBGHELP.@)
164 *
165 */
166 BOOL WINAPI SymEnumSourceFilesW(HANDLE hProcess, ULONG64 ModBase, PCWSTR Mask,
167 PSYM_ENUMSOURCEFILES_CALLBACKW cbSrcFiles,
168 PVOID UserContext)
169 {
170 struct module_pair pair;
171 SOURCEFILEW sf;
172 char* ptr;
173 WCHAR* conversion_buffer = NULL;
174 DWORD conversion_buffer_len = 0;
175
176 if (!cbSrcFiles) return FALSE;
177 pair.pcs = process_find_by_handle(hProcess);
178 if (!pair.pcs) return FALSE;
179
180 if (ModBase)
181 {
182 pair.requested = module_find_by_addr(pair.pcs, ModBase, DMT_UNKNOWN);
183 if (!module_get_debug(&pair)) return FALSE;
184 }
185 else
186 {
187 if (Mask[0] == '!')
188 {
189 pair.requested = module_find_by_nameW(pair.pcs, Mask + 1);
190 if (!module_get_debug(&pair)) return FALSE;
191 }
192 else
193 {
194 FIXME("Unsupported yet (should get info from current context)\n");
195 return FALSE;
196 }
197 }
198 if (!pair.effective->sources) return FALSE;
199 for (ptr = pair.effective->sources; *ptr; ptr += strlen(ptr) + 1)
200 {
201 DWORD len = MultiByteToWideChar(CP_ACP, 0, ptr, -1, NULL, 0);
202
203 if (len > conversion_buffer_len)
204 {
205 HeapFree(GetProcessHeap(), 0, conversion_buffer);
206 conversion_buffer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
207 if (!conversion_buffer) return FALSE;
208 conversion_buffer_len = len;
209 }
210
211 MultiByteToWideChar(CP_ACP, 0, ptr, -1, conversion_buffer, len);
212
213 /* FIXME: not using Mask */
214 sf.ModBase = ModBase;
215 sf.FileName = conversion_buffer;
216 if (!cbSrcFiles(&sf, UserContext)) break;
217 }
218
219 HeapFree(GetProcessHeap(), 0, conversion_buffer);
220 return TRUE;
221 }
222
223 struct enum_sources_files_context
224 {
225 PSYM_ENUMSOURCEFILES_CALLBACK callbackA;
226 PVOID caller_context;
227 char *conversion_buffer;
228 DWORD conversion_buffer_len;
229 DWORD callback_error;
230 };
231
232 static BOOL CALLBACK enum_source_files_W_to_A(PSOURCEFILEW source_file, PVOID context)
233 {
234 struct enum_sources_files_context *ctx = context;
235 SOURCEFILE source_fileA;
236 DWORD len;
237
238 len = WideCharToMultiByte(CP_ACP, 0, source_file->FileName, -1, NULL, 0, NULL, NULL);
239 if (len > ctx->conversion_buffer_len)
240 {
241 char *ptr = ctx->conversion_buffer ? HeapReAlloc(GetProcessHeap(), 0, ctx->conversion_buffer, len) :
242 HeapAlloc(GetProcessHeap(), 0, len);
243
244 if (!ptr)
245 {
246 ctx->callback_error = ERROR_OUTOFMEMORY;
247 return FALSE;
248 }
249
250 ctx->conversion_buffer = ptr;
251 ctx->conversion_buffer_len = len;
252 }
253
254 WideCharToMultiByte(CP_ACP, 0, source_file->FileName, -1, ctx->conversion_buffer, len, NULL, NULL);
255
256 source_fileA.ModBase = source_file->ModBase;
257 source_fileA.FileName = ctx->conversion_buffer;
258 return ctx->callbackA(&source_fileA, ctx->caller_context);
259 }
260
261 /******************************************************************
262 * SymEnumSourceFiles (DBGHELP.@)
263 *
264 */
265 BOOL WINAPI SymEnumSourceFiles(HANDLE hProcess, ULONG64 ModBase, PCSTR Mask,
266 PSYM_ENUMSOURCEFILES_CALLBACK cbSrcFiles,
267 PVOID UserContext)
268 {
269 WCHAR *maskW = NULL;
270 PSYM_ENUMSOURCEFILES_CALLBACKW callbackW;
271 PVOID context;
272 struct enum_sources_files_context callback_context = {cbSrcFiles, UserContext};
273 BOOL ret;
274
275 if (Mask)
276 {
277 DWORD len = MultiByteToWideChar(CP_ACP, 0, Mask, -1, NULL, 0);
278
279 maskW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
280 if (!maskW)
281 {
282 SetLastError(ERROR_OUTOFMEMORY);
283 return FALSE;
284 }
285
286 MultiByteToWideChar(CP_ACP, 0, Mask, -1, maskW, len);
287 }
288
289 if (cbSrcFiles)
290 {
291 callbackW = enum_source_files_W_to_A;
292 context = &callback_context;
293 }
294 else
295 {
296 callbackW = NULL;
297 context = UserContext;
298 }
299
300 ret = SymEnumSourceFilesW(hProcess, ModBase, maskW, callbackW, context);
301
302 if (callback_context.callback_error)
303 {
304 SetLastError(callback_context.callback_error);
305 ret = FALSE;
306 }
307
308 HeapFree(GetProcessHeap(), 0, callback_context.conversion_buffer);
309 HeapFree(GetProcessHeap(), 0, maskW);
310
311 return ret;
312 }
313
314 /******************************************************************
315 * SymEnumSourceLines (DBGHELP.@)
316 *
317 */
318 BOOL WINAPI SymEnumSourceLines(HANDLE hProcess, ULONG64 base, PCSTR obj,
319 PCSTR file, DWORD line, DWORD flags,
320 PSYM_ENUMLINES_CALLBACK EnumLinesCallback,
321 PVOID UserContext)
322 {
323 FIXME("%p %s %s %s %u %u %p %p: stub!\n",
324 hProcess, wine_dbgstr_longlong(base), debugstr_a(obj), debugstr_a(file),
325 line, flags, EnumLinesCallback, UserContext);
326 SetLastError(ERROR_NOT_SUPPORTED);
327 return FALSE;
328 }
329
330 /******************************************************************
331 * SymEnumSourceLinesW(DBGHELP.@)
332 *
333 */
334 BOOL WINAPI SymEnumSourceLinesW(HANDLE hProcess, ULONG64 base, PCWSTR obj,
335 PCWSTR file, DWORD line, DWORD flags,
336 PSYM_ENUMLINES_CALLBACKW EnumLinesCallback,
337 PVOID UserContext)
338 {
339 FIXME("%p %s %s %s %u %u %p %p: stub!\n",
340 hProcess, wine_dbgstr_longlong(base), debugstr_w(obj), debugstr_w(file),
341 line, flags, EnumLinesCallback, UserContext);
342 SetLastError(ERROR_NOT_SUPPORTED);
343 return FALSE;
344 }
345
346 /******************************************************************
347 * SymGetSourceFileToken (DBGHELP.@)
348 *
349 */
350 BOOL WINAPI SymGetSourceFileToken(HANDLE hProcess, ULONG64 base,
351 PCSTR src, PVOID* token, DWORD* size)
352 {
353 FIXME("%p %s %s %p %p: stub!\n",
354 hProcess, wine_dbgstr_longlong(base), debugstr_a(src), token, size);
355 SetLastError(ERROR_NOT_SUPPORTED);
356 return FALSE;
357 }
358
359 /******************************************************************
360 * SymGetSourceFileTokenW (DBGHELP.@)
361 *
362 */
363 BOOL WINAPI SymGetSourceFileTokenW(HANDLE hProcess, ULONG64 base,
364 PCWSTR src, PVOID* token, DWORD* size)
365 {
366 FIXME("%p %s %s %p %p: stub!\n",
367 hProcess, wine_dbgstr_longlong(base), debugstr_w(src), token, size);
368 SetLastError(ERROR_NOT_SUPPORTED);
369 return FALSE;
370 }