Release 4.0.4.
[wine.git] / dlls / dbghelp / source.c
blob60784f1f0debebde33084213ba6ebdc82b4157e6
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 int source_rb_compare(const void *key, const struct wine_rb_entry *entry)
41 const struct source_rb *t = WINE_RB_ENTRY_VALUE(entry, const struct source_rb, entry);
43 return strcmp((const char*)key, rb_module->sources + t->source);
46 /******************************************************************
47 * source_find
49 * check whether a source file has already been stored
51 static unsigned source_find(const char* name)
53 struct wine_rb_entry* e;
55 e = wine_rb_get(&rb_module->sources_offsets_tree, name);
56 if (!e) return -1;
57 return WINE_RB_ENTRY_VALUE(e, struct source_rb, entry)->source;
60 /******************************************************************
61 * source_new
63 * checks if source exists. if not, add it
65 unsigned source_new(struct module* module, const char* base, const char* name)
67 unsigned ret = -1;
68 const char* full;
69 char* tmp = NULL;
71 if (!name) return ret;
72 if (!base || *name == '/')
73 full = name;
74 else
76 unsigned bsz = strlen(base);
78 tmp = HeapAlloc(GetProcessHeap(), 0, bsz + 1 + strlen(name) + 1);
79 if (!tmp) return ret;
80 full = tmp;
81 strcpy(tmp, base);
82 if (tmp[bsz - 1] != '/') tmp[bsz++] = '/';
83 strcpy(&tmp[bsz], name);
85 rb_module = module;
86 if (!module->sources || (ret = source_find(full)) == (unsigned)-1)
88 char* new;
89 int len = strlen(full) + 1;
90 struct source_rb* rb;
92 if (module->sources_used + len + 1 > module->sources_alloc)
94 if (!module->sources)
96 module->sources_alloc = (module->sources_used + len + 1 + 255) & ~255;
97 new = HeapAlloc(GetProcessHeap(), 0, module->sources_alloc);
99 else
101 module->sources_alloc = max( module->sources_alloc * 2,
102 (module->sources_used + len + 1 + 255) & ~255 );
103 new = HeapReAlloc(GetProcessHeap(), 0, module->sources,
104 module->sources_alloc);
106 if (!new) goto done;
107 module->sources = new;
109 ret = module->sources_used;
110 memcpy(module->sources + module->sources_used, full, len);
111 module->sources_used += len;
112 module->sources[module->sources_used] = '\0';
113 if ((rb = pool_alloc(&module->pool, sizeof(*rb))))
115 rb->source = ret;
116 wine_rb_put(&module->sources_offsets_tree, full, &rb->entry);
119 done:
120 HeapFree(GetProcessHeap(), 0, tmp);
121 return ret;
124 /******************************************************************
125 * source_get
127 * returns a stored source file name
129 const char* source_get(const struct module* module, unsigned idx)
131 if (idx == -1) return "";
132 assert(module->sources);
133 return module->sources + idx;
136 /******************************************************************
137 * SymEnumSourceFilesW (DBGHELP.@)
140 BOOL WINAPI SymEnumSourceFilesW(HANDLE hProcess, ULONG64 ModBase, PCWSTR Mask,
141 PSYM_ENUMSOURCEFILES_CALLBACKW cbSrcFiles,
142 PVOID UserContext)
144 struct module_pair pair;
145 SOURCEFILEW sf;
146 char* ptr;
147 WCHAR* conversion_buffer = NULL;
148 DWORD conversion_buffer_len = 0;
150 if (!cbSrcFiles) return FALSE;
151 pair.pcs = process_find_by_handle(hProcess);
152 if (!pair.pcs) return FALSE;
154 if (ModBase)
156 pair.requested = module_find_by_addr(pair.pcs, ModBase, DMT_UNKNOWN);
157 if (!module_get_debug(&pair)) return FALSE;
159 else
161 if (Mask[0] == '!')
163 pair.requested = module_find_by_nameW(pair.pcs, Mask + 1);
164 if (!module_get_debug(&pair)) return FALSE;
166 else
168 FIXME("Unsupported yet (should get info from current context)\n");
169 return FALSE;
172 if (!pair.effective->sources) return FALSE;
173 for (ptr = pair.effective->sources; *ptr; ptr += strlen(ptr) + 1)
175 DWORD len = MultiByteToWideChar(CP_ACP, 0, ptr, -1, NULL, 0);
177 if (len > conversion_buffer_len)
179 HeapFree(GetProcessHeap(), 0, conversion_buffer);
180 conversion_buffer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
181 if (!conversion_buffer) return FALSE;
182 conversion_buffer_len = len;
185 MultiByteToWideChar(CP_ACP, 0, ptr, -1, conversion_buffer, len);
187 /* FIXME: not using Mask */
188 sf.ModBase = ModBase;
189 sf.FileName = conversion_buffer;
190 if (!cbSrcFiles(&sf, UserContext)) break;
193 HeapFree(GetProcessHeap(), 0, conversion_buffer);
194 return TRUE;
197 struct enum_sources_files_context
199 PSYM_ENUMSOURCEFILES_CALLBACK callbackA;
200 PVOID caller_context;
201 char *conversion_buffer;
202 DWORD conversion_buffer_len;
203 DWORD callback_error;
206 static BOOL CALLBACK enum_source_files_W_to_A(PSOURCEFILEW source_file, PVOID context)
208 struct enum_sources_files_context *ctx = context;
209 SOURCEFILE source_fileA;
210 DWORD len;
212 len = WideCharToMultiByte(CP_ACP, 0, source_file->FileName, -1, NULL, 0, NULL, NULL);
213 if (len > ctx->conversion_buffer_len)
215 char *ptr = ctx->conversion_buffer ? HeapReAlloc(GetProcessHeap(), 0, ctx->conversion_buffer, len) :
216 HeapAlloc(GetProcessHeap(), 0, len);
218 if (!ptr)
220 ctx->callback_error = ERROR_OUTOFMEMORY;
221 return FALSE;
224 ctx->conversion_buffer = ptr;
225 ctx->conversion_buffer_len = len;
228 WideCharToMultiByte(CP_ACP, 0, source_file->FileName, -1, ctx->conversion_buffer, len, NULL, NULL);
230 source_fileA.ModBase = source_file->ModBase;
231 source_fileA.FileName = ctx->conversion_buffer;
232 return ctx->callbackA(&source_fileA, ctx->caller_context);
235 /******************************************************************
236 * SymEnumSourceFiles (DBGHELP.@)
239 BOOL WINAPI SymEnumSourceFiles(HANDLE hProcess, ULONG64 ModBase, PCSTR Mask,
240 PSYM_ENUMSOURCEFILES_CALLBACK cbSrcFiles,
241 PVOID UserContext)
243 WCHAR *maskW = NULL;
244 PSYM_ENUMSOURCEFILES_CALLBACKW callbackW;
245 PVOID context;
246 struct enum_sources_files_context callback_context = {cbSrcFiles, UserContext};
247 BOOL ret;
249 if (Mask)
251 DWORD len = MultiByteToWideChar(CP_ACP, 0, Mask, -1, NULL, 0);
253 maskW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
254 if (!maskW)
256 SetLastError(ERROR_OUTOFMEMORY);
257 return FALSE;
260 MultiByteToWideChar(CP_ACP, 0, Mask, -1, maskW, len);
263 if (cbSrcFiles)
265 callbackW = enum_source_files_W_to_A;
266 context = &callback_context;
268 else
270 callbackW = NULL;
271 context = UserContext;
274 ret = SymEnumSourceFilesW(hProcess, ModBase, maskW, callbackW, context);
276 if (callback_context.callback_error)
278 SetLastError(callback_context.callback_error);
279 ret = FALSE;
282 HeapFree(GetProcessHeap(), 0, callback_context.conversion_buffer);
283 HeapFree(GetProcessHeap(), 0, maskW);
285 return ret;
288 /******************************************************************
289 * SymEnumSourceLines (DBGHELP.@)
292 BOOL WINAPI SymEnumSourceLines(HANDLE hProcess, ULONG64 base, PCSTR obj,
293 PCSTR file, DWORD line, DWORD flags,
294 PSYM_ENUMLINES_CALLBACK EnumLinesCallback,
295 PVOID UserContext)
297 FIXME("%p %s %s %s %u %u %p %p: stub!\n",
298 hProcess, wine_dbgstr_longlong(base), debugstr_a(obj), debugstr_a(file),
299 line, flags, EnumLinesCallback, UserContext);
300 SetLastError(ERROR_NOT_SUPPORTED);
301 return FALSE;
304 /******************************************************************
305 * SymEnumSourceLinesW(DBGHELP.@)
308 BOOL WINAPI SymEnumSourceLinesW(HANDLE hProcess, ULONG64 base, PCWSTR obj,
309 PCWSTR file, DWORD line, DWORD flags,
310 PSYM_ENUMLINES_CALLBACKW EnumLinesCallback,
311 PVOID UserContext)
313 FIXME("%p %s %s %s %u %u %p %p: stub!\n",
314 hProcess, wine_dbgstr_longlong(base), debugstr_w(obj), debugstr_w(file),
315 line, flags, EnumLinesCallback, UserContext);
316 SetLastError(ERROR_NOT_SUPPORTED);
317 return FALSE;
320 /******************************************************************
321 * SymGetSourceFileToken (DBGHELP.@)
324 BOOL WINAPI SymGetSourceFileToken(HANDLE hProcess, ULONG64 base,
325 PCSTR src, PVOID* token, DWORD* size)
327 FIXME("%p %s %s %p %p: stub!\n",
328 hProcess, wine_dbgstr_longlong(base), debugstr_a(src), token, size);
329 SetLastError(ERROR_NOT_SUPPORTED);
330 return FALSE;
333 /******************************************************************
334 * SymGetSourceFileTokenW (DBGHELP.@)
337 BOOL WINAPI SymGetSourceFileTokenW(HANDLE hProcess, ULONG64 base,
338 PCWSTR src, PVOID* token, DWORD* size)
340 FIXME("%p %s %s %p %p: stub!\n",
341 hProcess, wine_dbgstr_longlong(base), debugstr_w(src), token, size);
342 SetLastError(ERROR_NOT_SUPPORTED);
343 return FALSE;