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"
28 #ifndef DBGHELP_STATIC_LIB
29 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
34 static struct module
* rb_module
;
37 struct wine_rb_entry entry
;
41 int source_rb_compare(const void *key
, const struct wine_rb_entry
*entry
)
43 const struct source_rb
*t
= WINE_RB_ENTRY_VALUE(entry
, const struct source_rb
, entry
);
45 return strcmp((const char*)key
, rb_module
->sources
+ t
->source
);
48 /******************************************************************
51 * check whether a source file has already been stored
53 static unsigned source_find(const char* name
)
55 struct wine_rb_entry
* e
;
57 e
= wine_rb_get(&rb_module
->sources_offsets_tree
, name
);
59 return WINE_RB_ENTRY_VALUE(e
, struct source_rb
, entry
)->source
;
62 /******************************************************************
65 * checks if source exists. if not, add it
67 unsigned source_new(struct module
* module
, const char* base
, const char* name
)
73 if (!name
) return ret
;
74 if (!base
|| *name
== '/')
78 unsigned bsz
= strlen(base
);
80 tmp
= HeapAlloc(GetProcessHeap(), 0, bsz
+ 1 + strlen(name
) + 1);
84 if (tmp
[bsz
- 1] != '/') tmp
[bsz
++] = '/';
85 strcpy(&tmp
[bsz
], name
);
88 if (!module
->sources
|| (ret
= source_find(full
)) == (unsigned)-1)
91 int len
= strlen(full
) + 1;
94 if (module
->sources_used
+ len
+ 1 > module
->sources_alloc
)
98 module
->sources_alloc
= (module
->sources_used
+ len
+ 1 + 255) & ~255;
99 new = HeapAlloc(GetProcessHeap(), 0, module
->sources_alloc
);
103 module
->sources_alloc
= max( module
->sources_alloc
* 2,
104 (module
->sources_used
+ len
+ 1 + 255) & ~255 );
105 new = HeapReAlloc(GetProcessHeap(), 0, module
->sources
,
106 module
->sources_alloc
);
109 module
->sources
= new;
111 ret
= module
->sources_used
;
112 memcpy(module
->sources
+ module
->sources_used
, full
, len
);
113 module
->sources_used
+= len
;
114 module
->sources
[module
->sources_used
] = '\0';
115 if ((rb
= pool_alloc(&module
->pool
, sizeof(*rb
))))
118 wine_rb_put(&module
->sources_offsets_tree
, full
, &rb
->entry
);
122 HeapFree(GetProcessHeap(), 0, tmp
);
126 /******************************************************************
129 * returns a stored source file name
131 const char* source_get(const struct module
* module
, unsigned idx
)
133 if (idx
== -1) return "";
134 assert(module
->sources
);
135 return module
->sources
+ idx
;
138 /******************************************************************
139 * SymEnumSourceFilesW (DBGHELP.@)
142 BOOL WINAPI
SymEnumSourceFilesW(HANDLE hProcess
, ULONG64 ModBase
, PCWSTR Mask
,
143 PSYM_ENUMSOURCEFILES_CALLBACKW cbSrcFiles
,
146 struct module_pair pair
;
149 WCHAR
* conversion_buffer
= NULL
;
150 DWORD conversion_buffer_len
= 0;
152 if (!cbSrcFiles
) return FALSE
;
153 pair
.pcs
= process_find_by_handle(hProcess
);
154 if (!pair
.pcs
) return FALSE
;
158 pair
.requested
= module_find_by_addr(pair
.pcs
, ModBase
, DMT_UNKNOWN
);
159 if (!module_get_debug(&pair
)) return FALSE
;
165 pair
.requested
= module_find_by_nameW(pair
.pcs
, Mask
+ 1);
166 if (!module_get_debug(&pair
)) return FALSE
;
170 FIXME("Unsupported yet (should get info from current context)\n");
174 if (!pair
.effective
->sources
) return FALSE
;
175 for (ptr
= pair
.effective
->sources
; *ptr
; ptr
+= strlen(ptr
) + 1)
177 DWORD len
= MultiByteToWideChar(CP_ACP
, 0, ptr
, -1, NULL
, 0);
179 if (len
> conversion_buffer_len
)
181 HeapFree(GetProcessHeap(), 0, conversion_buffer
);
182 conversion_buffer
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
183 if (!conversion_buffer
) return FALSE
;
184 conversion_buffer_len
= len
;
187 MultiByteToWideChar(CP_ACP
, 0, ptr
, -1, conversion_buffer
, len
);
189 /* FIXME: not using Mask */
190 sf
.ModBase
= ModBase
;
191 sf
.FileName
= conversion_buffer
;
192 if (!cbSrcFiles(&sf
, UserContext
)) break;
195 HeapFree(GetProcessHeap(), 0, conversion_buffer
);
199 struct enum_sources_files_context
201 PSYM_ENUMSOURCEFILES_CALLBACK callbackA
;
202 PVOID caller_context
;
203 char *conversion_buffer
;
204 DWORD conversion_buffer_len
;
205 DWORD callback_error
;
208 static BOOL CALLBACK
enum_source_files_W_to_A(PSOURCEFILEW source_file
, PVOID context
)
210 struct enum_sources_files_context
*ctx
= context
;
211 SOURCEFILE source_fileA
;
214 len
= WideCharToMultiByte(CP_ACP
, 0, source_file
->FileName
, -1, NULL
, 0, NULL
, NULL
);
215 if (len
> ctx
->conversion_buffer_len
)
217 char *ptr
= ctx
->conversion_buffer
? HeapReAlloc(GetProcessHeap(), 0, ctx
->conversion_buffer
, len
) :
218 HeapAlloc(GetProcessHeap(), 0, len
);
222 ctx
->callback_error
= ERROR_OUTOFMEMORY
;
226 ctx
->conversion_buffer
= ptr
;
227 ctx
->conversion_buffer_len
= len
;
230 WideCharToMultiByte(CP_ACP
, 0, source_file
->FileName
, -1, ctx
->conversion_buffer
, len
, NULL
, NULL
);
232 source_fileA
.ModBase
= source_file
->ModBase
;
233 source_fileA
.FileName
= ctx
->conversion_buffer
;
234 return ctx
->callbackA(&source_fileA
, ctx
->caller_context
);
237 /******************************************************************
238 * SymEnumSourceFiles (DBGHELP.@)
241 BOOL WINAPI
SymEnumSourceFiles(HANDLE hProcess
, ULONG64 ModBase
, PCSTR Mask
,
242 PSYM_ENUMSOURCEFILES_CALLBACK cbSrcFiles
,
246 PSYM_ENUMSOURCEFILES_CALLBACKW callbackW
;
248 struct enum_sources_files_context callback_context
= {cbSrcFiles
, UserContext
};
253 DWORD len
= MultiByteToWideChar(CP_ACP
, 0, Mask
, -1, NULL
, 0);
255 maskW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
258 SetLastError(ERROR_OUTOFMEMORY
);
262 MultiByteToWideChar(CP_ACP
, 0, Mask
, -1, maskW
, len
);
267 callbackW
= enum_source_files_W_to_A
;
268 context
= &callback_context
;
273 context
= UserContext
;
276 ret
= SymEnumSourceFilesW(hProcess
, ModBase
, maskW
, callbackW
, context
);
278 if (callback_context
.callback_error
)
280 SetLastError(callback_context
.callback_error
);
284 HeapFree(GetProcessHeap(), 0, callback_context
.conversion_buffer
);
285 HeapFree(GetProcessHeap(), 0, maskW
);
290 /******************************************************************
291 * SymEnumSourceLines (DBGHELP.@)
294 BOOL WINAPI
SymEnumSourceLines(HANDLE hProcess
, ULONG64 base
, PCSTR obj
,
295 PCSTR file
, DWORD line
, DWORD flags
,
296 PSYM_ENUMLINES_CALLBACK EnumLinesCallback
,
299 FIXME("%p %s %s %s %u %u %p %p: stub!\n",
300 hProcess
, wine_dbgstr_longlong(base
), debugstr_a(obj
), debugstr_a(file
),
301 line
, flags
, EnumLinesCallback
, UserContext
);
302 SetLastError(ERROR_NOT_SUPPORTED
);
306 /******************************************************************
307 * SymEnumSourceLinesW(DBGHELP.@)
310 BOOL WINAPI
SymEnumSourceLinesW(HANDLE hProcess
, ULONG64 base
, PCWSTR obj
,
311 PCWSTR file
, DWORD line
, DWORD flags
,
312 PSYM_ENUMLINES_CALLBACKW EnumLinesCallback
,
315 FIXME("%p %s %s %s %u %u %p %p: stub!\n",
316 hProcess
, wine_dbgstr_longlong(base
), debugstr_w(obj
), debugstr_w(file
),
317 line
, flags
, EnumLinesCallback
, UserContext
);
318 SetLastError(ERROR_NOT_SUPPORTED
);
322 /******************************************************************
323 * SymGetSourceFileToken (DBGHELP.@)
326 BOOL WINAPI
SymGetSourceFileToken(HANDLE hProcess
, ULONG64 base
,
327 PCSTR src
, PVOID
* token
, DWORD
* size
)
329 FIXME("%p %s %s %p %p: stub!\n",
330 hProcess
, wine_dbgstr_longlong(base
), debugstr_a(src
), token
, size
);
331 SetLastError(ERROR_NOT_SUPPORTED
);
335 /******************************************************************
336 * SymGetSourceFileTokenW (DBGHELP.@)
339 BOOL WINAPI
SymGetSourceFileTokenW(HANDLE hProcess
, ULONG64 base
,
340 PCWSTR src
, PVOID
* token
, DWORD
* size
)
342 FIXME("%p %s %s %p %p: stub!\n",
343 hProcess
, wine_dbgstr_longlong(base
), debugstr_w(src
), token
, size
);
344 SetLastError(ERROR_NOT_SUPPORTED
);