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 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
32 static struct module
* rb_module
;
35 struct wine_rb_entry entry
;
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 /******************************************************************
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
);
57 return WINE_RB_ENTRY_VALUE(e
, struct source_rb
, entry
)->source
;
60 /******************************************************************
63 * checks if source exists. if not, add it
65 unsigned source_new(struct module
* module
, const char* base
, const char* name
)
71 if (!name
) return ret
;
72 if (!base
|| *name
== '/')
76 unsigned bsz
= strlen(base
);
78 tmp
= HeapAlloc(GetProcessHeap(), 0, bsz
+ 1 + strlen(name
) + 1);
82 if (tmp
[bsz
- 1] != '/') tmp
[bsz
++] = '/';
83 strcpy(&tmp
[bsz
], name
);
86 if (!module
->sources
|| (ret
= source_find(full
)) == (unsigned)-1)
89 int len
= strlen(full
) + 1;
92 if (module
->sources_used
+ len
+ 1 > module
->sources_alloc
)
96 module
->sources_alloc
= (module
->sources_used
+ len
+ 1 + 255) & ~255;
97 new = HeapAlloc(GetProcessHeap(), 0, module
->sources_alloc
);
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
);
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
))))
116 wine_rb_put(&module
->sources_offsets_tree
, full
, &rb
->entry
);
120 HeapFree(GetProcessHeap(), 0, tmp
);
124 /******************************************************************
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
,
144 struct module_pair pair
;
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
;
156 pair
.requested
= module_find_by_addr(pair
.pcs
, ModBase
, DMT_UNKNOWN
);
157 if (!module_get_debug(&pair
)) return FALSE
;
163 pair
.requested
= module_find_by_nameW(pair
.pcs
, Mask
+ 1);
164 if (!module_get_debug(&pair
)) return FALSE
;
168 FIXME("Unsupported yet (should get info from current context)\n");
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
);
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
;
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
);
220 ctx
->callback_error
= ERROR_OUTOFMEMORY
;
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
,
244 PSYM_ENUMSOURCEFILES_CALLBACKW callbackW
;
246 struct enum_sources_files_context callback_context
= {cbSrcFiles
, UserContext
};
251 DWORD len
= MultiByteToWideChar(CP_ACP
, 0, Mask
, -1, NULL
, 0);
253 maskW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
256 SetLastError(ERROR_OUTOFMEMORY
);
260 MultiByteToWideChar(CP_ACP
, 0, Mask
, -1, maskW
, len
);
265 callbackW
= enum_source_files_W_to_A
;
266 context
= &callback_context
;
271 context
= UserContext
;
274 ret
= SymEnumSourceFilesW(hProcess
, ModBase
, maskW
, callbackW
, context
);
276 if (callback_context
.callback_error
)
278 SetLastError(callback_context
.callback_error
);
282 HeapFree(GetProcessHeap(), 0, callback_context
.conversion_buffer
);
283 HeapFree(GetProcessHeap(), 0, maskW
);
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
,
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
);
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
,
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
);
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
);
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
);