mf/session: Implement support for sinks that provide sample allocators.
[wine.git] / dlls / dbghelp / source.c
blob1844eec783871132b9a5cc1a478aa9a979d53432
1 /*
2 * File source.c - source files management
4 * Copyright (C) 2004, Eric Pouech.
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.
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.
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
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <assert.h>
26 #include "dbghelp_private.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
31 static struct module* rb_module;
32 struct source_rb
34 struct wine_rb_entry entry;
35 unsigned source;
38 int source_rb_compare(const void *key, const struct wine_rb_entry *entry)
40 const struct source_rb *t = WINE_RB_ENTRY_VALUE(entry, const struct source_rb, entry);
42 return strcmp((const char*)key, rb_module->sources + t->source);
45 /******************************************************************
46 * source_find
48 * check whether a source file has already been stored
50 static unsigned source_find(const char* name)
52 struct wine_rb_entry* e;
54 e = wine_rb_get(&rb_module->sources_offsets_tree, name);
55 if (!e) return -1;
56 return WINE_RB_ENTRY_VALUE(e, struct source_rb, entry)->source;
59 /******************************************************************
60 * source_new
62 * checks if source exists. if not, add it
64 unsigned source_new(struct module* module, const char* base, const char* name)
66 unsigned ret = -1;
67 const char* full;
68 char* tmp = NULL;
70 if (!name) return ret;
71 if (!base || *name == '/')
72 full = name;
73 else
75 unsigned bsz = strlen(base);
77 tmp = HeapAlloc(GetProcessHeap(), 0, bsz + 1 + strlen(name) + 1);
78 if (!tmp) return ret;
79 full = tmp;
80 strcpy(tmp, base);
81 if (tmp[bsz - 1] != '/') tmp[bsz++] = '/';
82 strcpy(&tmp[bsz], name);
84 rb_module = module;
85 if (!module->sources || (ret = source_find(full)) == (unsigned)-1)
87 char* new;
88 int len = strlen(full) + 1;
89 struct source_rb* rb;
91 if (module->sources_used + len + 1 > module->sources_alloc)
93 if (!module->sources)
95 module->sources_alloc = (module->sources_used + len + 1 + 255) & ~255;
96 new = HeapAlloc(GetProcessHeap(), 0, module->sources_alloc);
98 else
100 module->sources_alloc = max( module->sources_alloc * 2,
101 (module->sources_used + len + 1 + 255) & ~255 );
102 new = HeapReAlloc(GetProcessHeap(), 0, module->sources,
103 module->sources_alloc);
105 if (!new) goto done;
106 module->sources = new;
108 ret = module->sources_used;
109 memcpy(module->sources + module->sources_used, full, len);
110 module->sources_used += len;
111 module->sources[module->sources_used] = '\0';
112 if ((rb = pool_alloc(&module->pool, sizeof(*rb))))
114 rb->source = ret;
115 wine_rb_put(&module->sources_offsets_tree, full, &rb->entry);
118 done:
119 HeapFree(GetProcessHeap(), 0, tmp);
120 return ret;
123 /******************************************************************
124 * source_get
126 * returns a stored source file name
128 const char* source_get(const struct module* module, unsigned idx)
130 if (idx == -1) return "";
131 assert(module->sources);
132 return module->sources + idx;
135 /******************************************************************
136 * SymEnumSourceFilesW (DBGHELP.@)
139 BOOL WINAPI SymEnumSourceFilesW(HANDLE hProcess, ULONG64 ModBase, PCWSTR Mask,
140 PSYM_ENUMSOURCEFILES_CALLBACKW cbSrcFiles,
141 PVOID UserContext)
143 struct module_pair pair;
144 SOURCEFILEW sf;
145 char* ptr;
146 WCHAR* conversion_buffer = NULL;
147 DWORD conversion_buffer_len = 0;
149 if (!cbSrcFiles) return FALSE;
150 pair.pcs = process_find_by_handle(hProcess);
151 if (!pair.pcs) return FALSE;
153 if (ModBase)
155 pair.requested = module_find_by_addr(pair.pcs, ModBase, DMT_UNKNOWN);
156 if (!module_get_debug(&pair)) return FALSE;
158 else
160 if (Mask[0] == '!')
162 pair.requested = module_find_by_nameW(pair.pcs, Mask + 1);
163 if (!module_get_debug(&pair)) return FALSE;
165 else
167 FIXME("Unsupported yet (should get info from current context)\n");
168 return FALSE;
171 if (!pair.effective->sources) return FALSE;
172 for (ptr = pair.effective->sources; *ptr; ptr += strlen(ptr) + 1)
174 DWORD len = MultiByteToWideChar(CP_ACP, 0, ptr, -1, NULL, 0);
176 if (len > conversion_buffer_len)
178 HeapFree(GetProcessHeap(), 0, conversion_buffer);
179 conversion_buffer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
180 if (!conversion_buffer) return FALSE;
181 conversion_buffer_len = len;
184 MultiByteToWideChar(CP_ACP, 0, ptr, -1, conversion_buffer, len);
186 /* FIXME: not using Mask */
187 sf.ModBase = ModBase;
188 sf.FileName = conversion_buffer;
189 if (!cbSrcFiles(&sf, UserContext)) break;
192 HeapFree(GetProcessHeap(), 0, conversion_buffer);
193 return TRUE;
196 struct enum_sources_files_context
198 PSYM_ENUMSOURCEFILES_CALLBACK callbackA;
199 PVOID caller_context;
200 char *conversion_buffer;
201 DWORD conversion_buffer_len;
202 DWORD callback_error;
205 static BOOL CALLBACK enum_source_files_W_to_A(PSOURCEFILEW source_file, PVOID context)
207 struct enum_sources_files_context *ctx = context;
208 SOURCEFILE source_fileA;
209 DWORD len;
211 len = WideCharToMultiByte(CP_ACP, 0, source_file->FileName, -1, NULL, 0, NULL, NULL);
212 if (len > ctx->conversion_buffer_len)
214 char *ptr = ctx->conversion_buffer ? HeapReAlloc(GetProcessHeap(), 0, ctx->conversion_buffer, len) :
215 HeapAlloc(GetProcessHeap(), 0, len);
217 if (!ptr)
219 ctx->callback_error = ERROR_OUTOFMEMORY;
220 return FALSE;
223 ctx->conversion_buffer = ptr;
224 ctx->conversion_buffer_len = len;
227 WideCharToMultiByte(CP_ACP, 0, source_file->FileName, -1, ctx->conversion_buffer, len, NULL, NULL);
229 source_fileA.ModBase = source_file->ModBase;
230 source_fileA.FileName = ctx->conversion_buffer;
231 return ctx->callbackA(&source_fileA, ctx->caller_context);
234 /******************************************************************
235 * SymEnumSourceFiles (DBGHELP.@)
238 BOOL WINAPI SymEnumSourceFiles(HANDLE hProcess, ULONG64 ModBase, PCSTR Mask,
239 PSYM_ENUMSOURCEFILES_CALLBACK cbSrcFiles,
240 PVOID UserContext)
242 WCHAR *maskW = NULL;
243 PSYM_ENUMSOURCEFILES_CALLBACKW callbackW;
244 PVOID context;
245 struct enum_sources_files_context callback_context = {cbSrcFiles, UserContext};
246 BOOL ret;
248 if (Mask)
250 DWORD len = MultiByteToWideChar(CP_ACP, 0, Mask, -1, NULL, 0);
252 maskW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
253 if (!maskW)
255 SetLastError(ERROR_OUTOFMEMORY);
256 return FALSE;
259 MultiByteToWideChar(CP_ACP, 0, Mask, -1, maskW, len);
262 if (cbSrcFiles)
264 callbackW = enum_source_files_W_to_A;
265 context = &callback_context;
267 else
269 callbackW = NULL;
270 context = UserContext;
273 ret = SymEnumSourceFilesW(hProcess, ModBase, maskW, callbackW, context);
275 if (callback_context.callback_error)
277 SetLastError(callback_context.callback_error);
278 ret = FALSE;
281 HeapFree(GetProcessHeap(), 0, callback_context.conversion_buffer);
282 HeapFree(GetProcessHeap(), 0, maskW);
284 return ret;
287 /******************************************************************
288 * SymEnumSourceLines (DBGHELP.@)
291 BOOL WINAPI SymEnumSourceLines(HANDLE hProcess, ULONG64 base, PCSTR obj,
292 PCSTR file, DWORD line, DWORD flags,
293 PSYM_ENUMLINES_CALLBACK EnumLinesCallback,
294 PVOID UserContext)
296 FIXME("%p %s %s %s %u %u %p %p: stub!\n",
297 hProcess, wine_dbgstr_longlong(base), debugstr_a(obj), debugstr_a(file),
298 line, flags, EnumLinesCallback, UserContext);
299 SetLastError(ERROR_NOT_SUPPORTED);
300 return FALSE;
303 /******************************************************************
304 * SymEnumSourceLinesW(DBGHELP.@)
307 BOOL WINAPI SymEnumSourceLinesW(HANDLE hProcess, ULONG64 base, PCWSTR obj,
308 PCWSTR file, DWORD line, DWORD flags,
309 PSYM_ENUMLINES_CALLBACKW EnumLinesCallback,
310 PVOID UserContext)
312 FIXME("%p %s %s %s %u %u %p %p: stub!\n",
313 hProcess, wine_dbgstr_longlong(base), debugstr_w(obj), debugstr_w(file),
314 line, flags, EnumLinesCallback, UserContext);
315 SetLastError(ERROR_NOT_SUPPORTED);
316 return FALSE;
319 /******************************************************************
320 * SymGetSourceFileToken (DBGHELP.@)
323 BOOL WINAPI SymGetSourceFileToken(HANDLE hProcess, ULONG64 base,
324 PCSTR src, PVOID* token, DWORD* size)
326 FIXME("%p %s %s %p %p: stub!\n",
327 hProcess, wine_dbgstr_longlong(base), debugstr_a(src), token, size);
328 SetLastError(ERROR_NOT_SUPPORTED);
329 return FALSE;
332 /******************************************************************
333 * SymGetSourceFileTokenW (DBGHELP.@)
336 BOOL WINAPI SymGetSourceFileTokenW(HANDLE hProcess, ULONG64 base,
337 PCWSTR src, PVOID* token, DWORD* size)
339 FIXME("%p %s %s %p %p: stub!\n",
340 hProcess, wine_dbgstr_longlong(base), debugstr_w(src), token, size);
341 SetLastError(ERROR_NOT_SUPPORTED);
342 return FALSE;