ieframe: Store container IOleCommandTarget in DocHost.
[wine.git] / dlls / dbghelp / source.c
blob693ba78c1bd8e85c85263a8f123f2fed3cd4e465
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 "config.h"
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <assert.h>
27 #include "dbghelp_private.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
32 static struct module* rb_module;
33 struct source_rb
35 struct wine_rb_entry entry;
36 unsigned source;
39 static void *source_rb_alloc(size_t size)
41 return HeapAlloc(GetProcessHeap(), 0, size);
44 static void *source_rb_realloc(void *ptr, size_t size)
46 return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
49 static void source_rb_free(void *ptr)
51 HeapFree(GetProcessHeap(), 0, ptr);
54 static int source_rb_compare(const void *key, const struct wine_rb_entry *entry)
56 const struct source_rb *t = WINE_RB_ENTRY_VALUE(entry, const struct source_rb, entry);
58 return strcmp((const char*)key, rb_module->sources + t->source);
61 const struct wine_rb_functions source_rb_functions =
63 source_rb_alloc,
64 source_rb_realloc,
65 source_rb_free,
66 source_rb_compare,
69 /******************************************************************
70 * source_find
72 * check whether a source file has already been stored
74 static unsigned source_find(const char* name)
76 struct wine_rb_entry* e;
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;
83 /******************************************************************
84 * source_new
86 * checks if source exists. if not, add it
88 unsigned source_new(struct module* module, const char* base, const char* name)
90 unsigned ret = -1;
91 const char* full;
92 char* tmp = NULL;
94 if (!name) return ret;
95 if (!base || *name == '/')
96 full = name;
97 else
99 unsigned bsz = strlen(base);
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);
108 rb_module = module;
109 if (!module->sources || (ret = source_find(full)) == (unsigned)-1)
111 char* new;
112 int len = strlen(full) + 1;
113 struct source_rb* rb;
115 if (module->sources_used + len + 1 > module->sources_alloc)
117 if (!module->sources)
119 module->sources_alloc = (module->sources_used + len + 1 + 255) & ~255;
120 new = HeapAlloc(GetProcessHeap(), 0, module->sources_alloc);
122 else
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);
129 if (!new) goto done;
130 module->sources = new;
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))))
138 rb->source = ret;
139 wine_rb_put(&module->sources_offsets_tree, full, &rb->entry);
142 done:
143 HeapFree(GetProcessHeap(), 0, tmp);
144 return ret;
147 /******************************************************************
148 * source_get
150 * returns a stored source file name
152 const char* source_get(const struct module* module, unsigned idx)
154 if (idx == -1) return "";
155 assert(module->sources);
156 return module->sources + idx;
159 /******************************************************************
160 * SymEnumSourceFilesW (DBGHELP.@)
163 BOOL WINAPI SymEnumSourceFilesW(HANDLE hProcess, ULONG64 ModBase, PCWSTR Mask,
164 PSYM_ENUMSOURCEFILES_CALLBACKW cbSrcFiles,
165 PVOID UserContext)
167 struct module_pair pair;
168 SOURCEFILEW sf;
169 char* ptr;
170 WCHAR* conversion_buffer = NULL;
171 DWORD conversion_buffer_len = 0;
173 if (!cbSrcFiles) return FALSE;
174 pair.pcs = process_find_by_handle(hProcess);
175 if (!pair.pcs) return FALSE;
177 if (ModBase)
179 pair.requested = module_find_by_addr(pair.pcs, ModBase, DMT_UNKNOWN);
180 if (!module_get_debug(&pair)) return FALSE;
182 else
184 if (Mask[0] == '!')
186 pair.requested = module_find_by_nameW(pair.pcs, Mask + 1);
187 if (!module_get_debug(&pair)) return FALSE;
189 else
191 FIXME("Unsupported yet (should get info from current context)\n");
192 return FALSE;
195 if (!pair.effective->sources) return FALSE;
196 for (ptr = pair.effective->sources; *ptr; ptr += strlen(ptr) + 1)
198 DWORD len = MultiByteToWideChar(CP_ACP, 0, ptr, -1, NULL, 0);
200 if (len > conversion_buffer_len)
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;
208 MultiByteToWideChar(CP_ACP, 0, ptr, -1, conversion_buffer, len);
210 /* FIXME: not using Mask */
211 sf.ModBase = ModBase;
212 sf.FileName = conversion_buffer;
213 if (!cbSrcFiles(&sf, UserContext)) break;
216 HeapFree(GetProcessHeap(), 0, conversion_buffer);
217 return TRUE;
220 struct enum_sources_files_context
222 PSYM_ENUMSOURCEFILES_CALLBACK callbackA;
223 PVOID caller_context;
224 char *conversion_buffer;
225 DWORD conversion_buffer_len;
226 DWORD callback_error;
229 static BOOL CALLBACK enum_source_files_W_to_A(PSOURCEFILEW source_file, PVOID context)
231 struct enum_sources_files_context *ctx = context;
232 SOURCEFILE source_fileA;
233 DWORD len;
235 len = WideCharToMultiByte(CP_ACP, 0, source_file->FileName, -1, NULL, 0, NULL, NULL);
236 if (len > ctx->conversion_buffer_len)
238 char *ptr = ctx->conversion_buffer ? HeapReAlloc(GetProcessHeap(), 0, ctx->conversion_buffer, len) :
239 HeapAlloc(GetProcessHeap(), 0, len);
241 if (!ptr)
243 ctx->callback_error = ERROR_OUTOFMEMORY;
244 return FALSE;
247 ctx->conversion_buffer = ptr;
248 ctx->conversion_buffer_len = len;
251 WideCharToMultiByte(CP_ACP, 0, source_file->FileName, -1, ctx->conversion_buffer, len, NULL, NULL);
253 source_fileA.ModBase = source_file->ModBase;
254 source_fileA.FileName = ctx->conversion_buffer;
255 return ctx->callbackA(&source_fileA, ctx->caller_context);
258 /******************************************************************
259 * SymEnumSourceFiles (DBGHELP.@)
262 BOOL WINAPI SymEnumSourceFiles(HANDLE hProcess, ULONG64 ModBase, PCSTR Mask,
263 PSYM_ENUMSOURCEFILES_CALLBACK cbSrcFiles,
264 PVOID UserContext)
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;
272 if (Mask)
274 DWORD len = MultiByteToWideChar(CP_ACP, 0, Mask, -1, NULL, 0);
276 maskW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
277 if (!maskW)
279 SetLastError(ERROR_OUTOFMEMORY);
280 return FALSE;
283 MultiByteToWideChar(CP_ACP, 0, Mask, -1, maskW, len);
286 if (cbSrcFiles)
288 callbackW = enum_source_files_W_to_A;
289 context = &callback_context;
291 else
293 callbackW = NULL;
294 context = UserContext;
297 ret = SymEnumSourceFilesW(hProcess, ModBase, maskW, callbackW, context);
299 if (callback_context.callback_error)
301 SetLastError(callback_context.callback_error);
302 ret = FALSE;
305 HeapFree(GetProcessHeap(), 0, callback_context.conversion_buffer);
306 HeapFree(GetProcessHeap(), 0, maskW);
308 return ret;
311 /******************************************************************
312 * SymEnumSourceLines (DBGHELP.@)
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)
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;
327 /******************************************************************
328 * SymEnumSourceLinesW(DBGHELP.@)
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)
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;
343 /******************************************************************
344 * SymGetSourceFileToken (DBGHELP.@)
347 BOOL WINAPI SymGetSourceFileToken(HANDLE hProcess, ULONG64 base,
348 PCSTR src, PVOID* token, DWORD* size)
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;
356 /******************************************************************
357 * SymGetSourceFileTokenW (DBGHELP.@)
360 BOOL WINAPI SymGetSourceFileTokenW(HANDLE hProcess, ULONG64 base,
361 PCWSTR src, PVOID* token, DWORD* size)
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;