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