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
27 #include "dbghelp_private.h"
29 #ifndef DBGHELP_STATIC_LIB
30 #include <wine/debug.h>
33 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
35 static struct module
* rb_module
;
38 struct wine_rb_entry entry
;
42 static void *source_rb_alloc(size_t size
)
44 return HeapAlloc(GetProcessHeap(), 0, size
);
47 static void *source_rb_realloc(void *ptr
, size_t size
)
49 return HeapReAlloc(GetProcessHeap(), 0, ptr
, size
);
52 static void source_rb_free(void *ptr
)
54 HeapFree(GetProcessHeap(), 0, ptr
);
57 static int source_rb_compare(const void *key
, const struct wine_rb_entry
*entry
)
59 const struct source_rb
*t
= WINE_RB_ENTRY_VALUE(entry
, const struct source_rb
, entry
);
61 return strcmp((const char*)key
, rb_module
->sources
+ t
->source
);
64 const struct wine_rb_functions source_rb_functions
=
72 /******************************************************************
75 * check whether a source file has already been stored
77 static unsigned source_find(const char* name
)
79 struct wine_rb_entry
* e
;
81 e
= wine_rb_get(&rb_module
->sources_offsets_tree
, name
);
83 return WINE_RB_ENTRY_VALUE(e
, struct source_rb
, entry
)->source
;
86 /******************************************************************
89 * checks if source exists. if not, add it
91 unsigned source_new(struct module
* module
, const char* base
, const char* name
)
97 if (!name
) return ret
;
98 if (!base
|| *name
== '/')
102 unsigned bsz
= strlen(base
);
104 tmp
= HeapAlloc(GetProcessHeap(), 0, bsz
+ 1 + strlen(name
) + 1);
105 if (!tmp
) return ret
;
108 if (tmp
[bsz
- 1] != '/') tmp
[bsz
++] = '/';
109 strcpy(&tmp
[bsz
], name
);
112 if (!module
->sources
|| (ret
= source_find(full
)) == (unsigned)-1)
115 int len
= strlen(full
) + 1;
116 struct source_rb
* rb
;
118 if (module
->sources_used
+ len
+ 1 > module
->sources_alloc
)
120 if (!module
->sources
)
122 module
->sources_alloc
= (module
->sources_used
+ len
+ 1 + 255) & ~255;
123 new = HeapAlloc(GetProcessHeap(), 0, module
->sources_alloc
);
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
);
133 module
->sources
= new;
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
))))
142 wine_rb_put(&module
->sources_offsets_tree
, full
, &rb
->entry
);
146 HeapFree(GetProcessHeap(), 0, tmp
);
150 /******************************************************************
153 * returns a stored source file name
155 const char* source_get(const struct module
* module
, unsigned idx
)
157 if (idx
== -1) return "";
158 assert(module
->sources
);
159 return module
->sources
+ idx
;
162 /******************************************************************
163 * SymEnumSourceFilesW (DBGHELP.@)
166 BOOL WINAPI
SymEnumSourceFilesW(HANDLE hProcess
, ULONG64 ModBase
, PCWSTR Mask
,
167 PSYM_ENUMSOURCEFILES_CALLBACKW cbSrcFiles
,
170 struct module_pair pair
;
173 WCHAR
* conversion_buffer
= NULL
;
174 DWORD conversion_buffer_len
= 0;
176 if (!cbSrcFiles
) return FALSE
;
177 pair
.pcs
= process_find_by_handle(hProcess
);
178 if (!pair
.pcs
) return FALSE
;
182 pair
.requested
= module_find_by_addr(pair
.pcs
, ModBase
, DMT_UNKNOWN
);
183 if (!module_get_debug(&pair
)) return FALSE
;
189 pair
.requested
= module_find_by_nameW(pair
.pcs
, Mask
+ 1);
190 if (!module_get_debug(&pair
)) return FALSE
;
194 FIXME("Unsupported yet (should get info from current context)\n");
198 if (!pair
.effective
->sources
) return FALSE
;
199 for (ptr
= pair
.effective
->sources
; *ptr
; ptr
+= strlen(ptr
) + 1)
201 DWORD len
= MultiByteToWideChar(CP_ACP
, 0, ptr
, -1, NULL
, 0);
203 if (len
> conversion_buffer_len
)
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
;
211 MultiByteToWideChar(CP_ACP
, 0, ptr
, -1, conversion_buffer
, len
);
213 /* FIXME: not using Mask */
214 sf
.ModBase
= ModBase
;
215 sf
.FileName
= conversion_buffer
;
216 if (!cbSrcFiles(&sf
, UserContext
)) break;
219 HeapFree(GetProcessHeap(), 0, conversion_buffer
);
223 struct enum_sources_files_context
225 PSYM_ENUMSOURCEFILES_CALLBACK callbackA
;
226 PVOID caller_context
;
227 char *conversion_buffer
;
228 DWORD conversion_buffer_len
;
229 DWORD callback_error
;
232 static BOOL CALLBACK
enum_source_files_W_to_A(PSOURCEFILEW source_file
, PVOID context
)
234 struct enum_sources_files_context
*ctx
= context
;
235 SOURCEFILE source_fileA
;
238 len
= WideCharToMultiByte(CP_ACP
, 0, source_file
->FileName
, -1, NULL
, 0, NULL
, NULL
);
239 if (len
> ctx
->conversion_buffer_len
)
241 char *ptr
= ctx
->conversion_buffer
? HeapReAlloc(GetProcessHeap(), 0, ctx
->conversion_buffer
, len
) :
242 HeapAlloc(GetProcessHeap(), 0, len
);
246 ctx
->callback_error
= ERROR_OUTOFMEMORY
;
250 ctx
->conversion_buffer
= ptr
;
251 ctx
->conversion_buffer_len
= len
;
254 WideCharToMultiByte(CP_ACP
, 0, source_file
->FileName
, -1, ctx
->conversion_buffer
, len
, NULL
, NULL
);
256 source_fileA
.ModBase
= source_file
->ModBase
;
257 source_fileA
.FileName
= ctx
->conversion_buffer
;
258 return ctx
->callbackA(&source_fileA
, ctx
->caller_context
);
261 /******************************************************************
262 * SymEnumSourceFiles (DBGHELP.@)
265 BOOL WINAPI
SymEnumSourceFiles(HANDLE hProcess
, ULONG64 ModBase
, PCSTR Mask
,
266 PSYM_ENUMSOURCEFILES_CALLBACK cbSrcFiles
,
270 PSYM_ENUMSOURCEFILES_CALLBACKW callbackW
;
272 struct enum_sources_files_context callback_context
= {cbSrcFiles
, UserContext
};
277 DWORD len
= MultiByteToWideChar(CP_ACP
, 0, Mask
, -1, NULL
, 0);
279 maskW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
282 SetLastError(ERROR_OUTOFMEMORY
);
286 MultiByteToWideChar(CP_ACP
, 0, Mask
, -1, maskW
, len
);
291 callbackW
= enum_source_files_W_to_A
;
292 context
= &callback_context
;
297 context
= UserContext
;
300 ret
= SymEnumSourceFilesW(hProcess
, ModBase
, maskW
, callbackW
, context
);
302 if (callback_context
.callback_error
)
304 SetLastError(callback_context
.callback_error
);
308 HeapFree(GetProcessHeap(), 0, callback_context
.conversion_buffer
);
309 HeapFree(GetProcessHeap(), 0, maskW
);
314 /******************************************************************
315 * SymEnumSourceLines (DBGHELP.@)
318 BOOL WINAPI
SymEnumSourceLines(HANDLE hProcess
, ULONG64 base
, PCSTR obj
,
319 PCSTR file
, DWORD line
, DWORD flags
,
320 PSYM_ENUMLINES_CALLBACK EnumLinesCallback
,
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
);
330 /******************************************************************
331 * SymEnumSourceLinesW(DBGHELP.@)
334 BOOL WINAPI
SymEnumSourceLinesW(HANDLE hProcess
, ULONG64 base
, PCWSTR obj
,
335 PCWSTR file
, DWORD line
, DWORD flags
,
336 PSYM_ENUMLINES_CALLBACKW EnumLinesCallback
,
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
);
346 /******************************************************************
347 * SymGetSourceFileToken (DBGHELP.@)
350 BOOL WINAPI
SymGetSourceFileToken(HANDLE hProcess
, ULONG64 base
,
351 PCSTR src
, PVOID
* token
, DWORD
* size
)
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
);
359 /******************************************************************
360 * SymGetSourceFileTokenW (DBGHELP.@)
363 BOOL WINAPI
SymGetSourceFileTokenW(HANDLE hProcess
, ULONG64 base
,
364 PCWSTR src
, PVOID
* token
, DWORD
* size
)
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
);