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
26 #include "dbghelp_private.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
31 static struct module
* rb_module
;
34 struct wine_rb_entry entry
;
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 /******************************************************************
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
);
56 return WINE_RB_ENTRY_VALUE(e
, struct source_rb
, entry
)->source
;
59 /******************************************************************
62 * checks if source exists. if not, add it
64 unsigned source_new(struct module
* module
, const char* base
, const char* name
)
70 if (!name
) return ret
;
71 if (!base
|| *name
== '/')
75 unsigned bsz
= strlen(base
);
77 tmp
= HeapAlloc(GetProcessHeap(), 0, bsz
+ 1 + strlen(name
) + 1);
81 if (bsz
&& tmp
[bsz
- 1] != '/') tmp
[bsz
++] = '/';
82 strcpy(&tmp
[bsz
], name
);
85 if (!module
->sources
|| (ret
= source_find(full
)) == (unsigned)-1)
88 int len
= strlen(full
) + 1;
91 if (module
->sources_used
+ len
+ 1 > module
->sources_alloc
)
95 module
->sources_alloc
= (module
->sources_used
+ len
+ 1 + 255) & ~255;
96 new = HeapAlloc(GetProcessHeap(), 0, module
->sources_alloc
);
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
);
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
))))
115 wine_rb_put(&module
->sources_offsets_tree
, full
, &rb
->entry
);
119 HeapFree(GetProcessHeap(), 0, tmp
);
123 /******************************************************************
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
,
143 struct module_pair pair
;
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
;
155 pair
.requested
= module_find_by_addr(pair
.pcs
, ModBase
, DMT_UNKNOWN
);
156 if (!module_get_debug(&pair
)) return FALSE
;
162 pair
.requested
= module_find_by_nameW(pair
.pcs
, Mask
+ 1);
163 if (!module_get_debug(&pair
)) return FALSE
;
167 FIXME("Unsupported yet (should get info from current context)\n");
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
);
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
;
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
);
219 ctx
->callback_error
= ERROR_OUTOFMEMORY
;
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
,
243 PSYM_ENUMSOURCEFILES_CALLBACKW callbackW
;
245 struct enum_sources_files_context callback_context
= {cbSrcFiles
, UserContext
};
250 DWORD len
= MultiByteToWideChar(CP_ACP
, 0, Mask
, -1, NULL
, 0);
252 maskW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
255 SetLastError(ERROR_OUTOFMEMORY
);
259 MultiByteToWideChar(CP_ACP
, 0, Mask
, -1, maskW
, len
);
264 callbackW
= enum_source_files_W_to_A
;
265 context
= &callback_context
;
270 context
= UserContext
;
273 ret
= SymEnumSourceFilesW(hProcess
, ModBase
, maskW
, callbackW
, context
);
275 if (callback_context
.callback_error
)
277 SetLastError(callback_context
.callback_error
);
281 HeapFree(GetProcessHeap(), 0, callback_context
.conversion_buffer
);
282 HeapFree(GetProcessHeap(), 0, maskW
);
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
,
296 FIXME("%p %I64x %s %s %lu %lu %p %p: stub!\n",
297 hProcess
, base
, debugstr_a(obj
), debugstr_a(file
),
298 line
, flags
, EnumLinesCallback
, UserContext
);
299 SetLastError(ERROR_NOT_SUPPORTED
);
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
,
312 FIXME("%p %I64x %s %s %lu %lu %p %p: stub!\n",
313 hProcess
, base
, debugstr_w(obj
), debugstr_w(file
),
314 line
, flags
, EnumLinesCallback
, UserContext
);
315 SetLastError(ERROR_NOT_SUPPORTED
);
319 /******************************************************************
320 * SymGetSourceFileToken (DBGHELP.@)
323 BOOL WINAPI
SymGetSourceFileToken(HANDLE hProcess
, ULONG64 base
,
324 PCSTR src
, PVOID
* token
, DWORD
* size
)
326 FIXME("%p %I64x %s %p %p: stub!\n",
327 hProcess
, base
, debugstr_a(src
), token
, size
);
328 SetLastError(ERROR_NOT_SUPPORTED
);
332 /******************************************************************
333 * SymGetSourceFileTokenW (DBGHELP.@)
336 BOOL WINAPI
SymGetSourceFileTokenW(HANDLE hProcess
, ULONG64 base
,
337 PCWSTR src
, PVOID
* token
, DWORD
* size
)
339 FIXME("%p %I64x %s %p %p: stub!\n",
340 hProcess
, base
, debugstr_w(src
), token
, size
);
341 SetLastError(ERROR_NOT_SUPPORTED
);