2 * File dbghelp.c - generic routines (process) for dbghelp DLL
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "dbghelp_private.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
31 * - support for symbols' types is still partly missing
33 * + funcargtype:s are (partly) wrong: they should be a specific struct (like
34 * typedef) pointing to the actual type (and not a direct access)
35 * + we should store the underlying type for an enum in the symt_enum struct
36 * - most options (dbghelp_options) are not used (loading lines, decoration,
37 * deferring reading of module symbols, public symbols...)
38 * - in symbol lookup by name, we don't use RE everywhere we should. Moreover, when
39 * we're supposed to use RE, it doesn't make use of our hash tables. Therefore,
40 * we could use hash if name isn't a RE, and fall back to a full search when we
42 * - (un)decoration is not handled (should make winedump's code a (.a) library
43 * and link it to winedump, and potentially to msvcrt and dbghelp (check best
44 * way not to duplicate code in msvcrt & dbghelp)
46 * + we should add parameters' types to the function's signature
47 * while processing a function's parameters
48 * + get rid of MSC reading FIXME:s (lots of types are not defined)
49 * + support the PUBLICS_ONLY, NO_PUBLICS and AUTO_PUBLICS options
53 * - implement the callback notification mechanism
56 unsigned dbghelp_options = SYMOPT_UNDNAME;
58 /***********************************************************************
59 * DllMain (DEBUGHLP.@)
61 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
65 case DLL_PROCESS_ATTACH: break;
66 case DLL_PROCESS_DETACH: break;
67 case DLL_THREAD_ATTACH: break;
68 case DLL_THREAD_DETACH: break;
74 static struct process* process_first /* = NULL */;
76 /******************************************************************
77 * process_find_by_handle
80 struct process* process_find_by_handle(HANDLE hProcess)
84 for (p = process_first; p && p->handle != hProcess; p = p->next);
85 if (!p) SetLastError(ERROR_INVALID_HANDLE);
89 /******************************************************************
90 * SymSetSearchPath (DBGHELP.@)
93 BOOL WINAPI SymSetSearchPath(HANDLE hProcess, PSTR searchPath)
95 struct process* pcs = process_find_by_handle(hProcess);
97 if (!pcs) return FALSE;
98 if (!searchPath) return FALSE;
100 HeapFree(GetProcessHeap(), 0, pcs->search_path);
101 pcs->search_path = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(searchPath) + 1),
106 /***********************************************************************
107 * SymGetSearchPath (DBGHELP.@)
109 BOOL WINAPI SymGetSearchPath(HANDLE hProcess, LPSTR szSearchPath,
110 DWORD SearchPathLength)
112 struct process* pcs = process_find_by_handle(hProcess);
113 if (!pcs) return FALSE;
115 strncpy(szSearchPath, pcs->search_path, SearchPathLength);
116 szSearchPath[SearchPathLength - 1] = '\0';
120 /******************************************************************
123 * SymInitialize helper: loads in dbghelp all known (and loaded modules)
124 * this assumes that hProcess is a handle on a valid process
126 static BOOL process_invade(HANDLE hProcess)
133 if (!EnumProcessModules(hProcess, hMods, sizeof(hMods), &sz))
134 return FALSE; /* FIXME should grow hMods */
136 for (i = 0; i < sz / sizeof(HMODULE); i++)
138 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
139 !GetModuleFileNameExA(hProcess, hMods[i], img, sizeof(img)) ||
140 !SymLoadModule(hProcess, 0, img, NULL, (DWORD)mi.lpBaseOfDll, mi.SizeOfImage))
147 /******************************************************************
148 * SymInitialize (DBGHELP.@)
150 * The initialisation of a dbghelp's context.
151 * Note that hProcess doesn't need to be a valid process handle (except
152 * when fInvadeProcess is TRUE).
153 * Since, we're also allow to load ELF (pure) libraries and Wine ELF libraries
154 * containing PE (and NE) module(s), here's how we handle it:
155 * - we load every module (ELF, NE, PE) passed in SymLoadModule
156 * - in fInvadeProcess (in SymInitialize) is TRUE, we set up what is called ELF
157 * synchronization: hProcess should be a valid process handle, and we hook
158 * ourselves on hProcess's loaded ELF-modules, and keep this list in sync with
159 * our internal ELF modules representation (loading / unloading). This way,
160 * we'll pair every loaded builtin PE module with its ELF counterpart (and
161 * access its debug information).
162 * - if fInvadeProcess (in SymInitialize) is FALSE, we won't be able to
163 * make the peering between a builtin PE module and its ELF counterpart, hence
164 * we won't be able to provide the requested debug information. We'll
165 * however be able to load native PE modules (and their debug information)
166 * without any trouble.
167 * Note also that this scheme can be intertwined with the deferred loading
168 * mechanism (ie only load the debug information when we actually need it).
170 BOOL WINAPI SymInitialize(HANDLE hProcess, PSTR UserSearchPath, BOOL fInvadeProcess)
174 TRACE("(%p %s %u)\n", hProcess, debugstr_a(UserSearchPath), fInvadeProcess);
176 if (process_find_by_handle(hProcess))
177 FIXME("what to do ??\n");
179 pcs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pcs));
180 if (!pcs) return FALSE;
182 pcs->handle = hProcess;
186 pcs->search_path = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(UserSearchPath) + 1),
194 pcs->search_path = HeapAlloc(GetProcessHeap(), 0, len = MAX_PATH);
195 while ((size = GetCurrentDirectoryA(len, pcs->search_path)) >= len)
196 pcs->search_path = HeapReAlloc(GetProcessHeap(), 0, pcs->search_path, len *= 2);
197 pcs->search_path = HeapReAlloc(GetProcessHeap(), 0, pcs->search_path, size + 1);
199 len = GetEnvironmentVariableA("_NT_SYMBOL_PATH", NULL, 0);
202 pcs->search_path = HeapReAlloc(GetProcessHeap(), 0, pcs->search_path, size + 1 + len + 1);
203 pcs->search_path[size] = ';';
204 GetEnvironmentVariableA("_NT_SYMBOL_PATH", pcs->search_path + size + 1, len);
207 len = GetEnvironmentVariableA("_NT_ALTERNATE_SYMBOL_PATH", NULL, 0);
210 pcs->search_path = HeapReAlloc(GetProcessHeap(), 0, pcs->search_path, size + 1 + len + 1);
211 pcs->search_path[size] = ';';
212 GetEnvironmentVariableA("_NT_ALTERNATE_SYMBOL_PATH", pcs->search_path + size + 1, len);
217 pcs->lmodules = NULL;
218 pcs->dbg_hdr_addr = 0;
219 pcs->next = process_first;
224 if (!elf_read_wine_loader_dbg_info(pcs))
226 SymCleanup(hProcess);
229 process_invade(hProcess);
230 elf_synchronize_module_list(pcs);
235 /******************************************************************
236 * SymCleanup (DBGHELP.@)
239 BOOL WINAPI SymCleanup(HANDLE hProcess)
241 struct process** ppcs;
242 struct process* next;
244 for (ppcs = &process_first; *ppcs; ppcs = &(*ppcs)->next)
246 if ((*ppcs)->handle == hProcess)
248 while ((*ppcs)->lmodules) module_remove(*ppcs, (*ppcs)->lmodules);
250 HeapFree(GetProcessHeap(), 0, (*ppcs)->search_path);
251 next = (*ppcs)->next;
252 HeapFree(GetProcessHeap(), 0, *ppcs);
260 /******************************************************************
261 * SymSetOptions (DBGHELP.@)
264 DWORD WINAPI SymSetOptions(DWORD opts)
266 return dbghelp_options = opts;
269 /******************************************************************
270 * SymGetOptions (DBGHELP.@)
273 DWORD WINAPI SymGetOptions(void)
275 return dbghelp_options;
278 /******************************************************************
279 * SymSetContext (DBGHELP.@)
282 BOOL WINAPI SymSetContext(HANDLE hProcess, PIMAGEHLP_STACK_FRAME StackFrame,
283 PIMAGEHLP_CONTEXT Context)
285 struct process* pcs = process_find_by_handle(hProcess);
286 if (!pcs) return FALSE;
288 pcs->ctx_frame = *StackFrame;
289 /* MSDN states that Context is not (no longer?) used */
293 /***********************************************************************
294 * SymRegisterCallback (DBGHELP.@)
296 BOOL WINAPI SymRegisterCallback(HANDLE hProcess,
297 PSYMBOL_REGISTERED_CALLBACK CallbackFunction,
300 FIXME("(%p, %p, %p): stub\n", hProcess, CallbackFunction, UserContext);
301 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
305 /* This is imagehlp version not dbghelp !! */
306 static API_VERSION api_version = { 4, 0, 2, 0 };
308 /***********************************************************************
309 * ImagehlpApiVersion (DBGHELP.@)
311 LPAPI_VERSION WINAPI ImagehlpApiVersion(VOID)
316 /***********************************************************************
317 * ImagehlpApiVersionEx (DBGHELP.@)
319 LPAPI_VERSION WINAPI ImagehlpApiVersionEx(LPAPI_VERSION AppVersion)
321 if (!AppVersion) return NULL;
323 AppVersion->MajorVersion = api_version.MajorVersion;
324 AppVersion->MinorVersion = api_version.MinorVersion;
325 AppVersion->Revision = api_version.Revision;
326 AppVersion->Reserved = api_version.Reserved;