VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/base/nsStackFrameWin.cpp@ 6542

Last change on this file since 6542 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.6 KB
Line 
1/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is nsStackFrameWin.h code, released
17 * December 20, 2003.
18 *
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 2003
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 * Michael Judge, 20-December-2000
26 *
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
38 *
39 * ***** END LICENSE BLOCK ***** */
40
41#include "nscore.h"
42#include "windows.h"
43#include "imagehlp.h"
44#include "stdio.h"
45#include "nsStackFrameWin.h"
46
47// Define these as static pointers so that we can load the DLL on the
48// fly (and not introduce a link-time dependency on it). Tip o' the
49// hat to Matt Pietrick for this idea. See:
50//
51// http://msdn.microsoft.com/library/periodic/period97/F1/D3/S245C6.htm
52//
53
54
55PR_BEGIN_EXTERN_C
56
57SYMSETOPTIONSPROC _SymSetOptions;
58
59SYMINITIALIZEPROC _SymInitialize;
60
61SYMCLEANUPPROC _SymCleanup;
62
63STACKWALKPROC _StackWalk;
64
65SYMFUNCTIONTABLEACCESSPROC _SymFunctionTableAccess;
66
67SYMGETMODULEBASEPROC _SymGetModuleBase;
68
69SYMGETSYMFROMADDRPROC _SymGetSymFromAddr;
70
71SYMLOADMODULE _SymLoadModule;
72
73SYMUNDNAME _SymUnDName;
74
75SYMGETMODULEINFO _SymGetModuleInfo;
76
77ENUMLOADEDMODULES _EnumerateLoadedModules;
78
79SYMGETLINEFROMADDRPROC _SymGetLineFromAddr;
80
81PR_END_EXTERN_C
82
83
84
85
86PRBool
87EnsureImageHlpInitialized()
88{
89 static PRBool gInitialized = PR_FALSE;
90
91 if (! gInitialized) {
92 HMODULE module = ::LoadLibrary("IMAGEHLP.DLL");
93 if (!module) return PR_FALSE;
94
95 _SymSetOptions = (SYMSETOPTIONSPROC) ::GetProcAddress(module, "SymSetOptions");
96 if (!_SymSetOptions) return PR_FALSE;
97
98 _SymInitialize = (SYMINITIALIZEPROC) ::GetProcAddress(module, "SymInitialize");
99 if (!_SymInitialize) return PR_FALSE;
100
101 _SymCleanup = (SYMCLEANUPPROC)GetProcAddress(module, "SymCleanup");
102 if (!_SymCleanup) return PR_FALSE;
103
104 _StackWalk = (STACKWALKPROC)GetProcAddress(module, "StackWalk");
105 if (!_StackWalk) return PR_FALSE;
106
107 _SymFunctionTableAccess = (SYMFUNCTIONTABLEACCESSPROC) GetProcAddress(module, "SymFunctionTableAccess");
108 if (!_SymFunctionTableAccess) return PR_FALSE;
109
110 _SymGetModuleBase = (SYMGETMODULEBASEPROC)GetProcAddress(module, "SymGetModuleBase");
111 if (!_SymGetModuleBase) return PR_FALSE;
112
113 _SymGetSymFromAddr = (SYMGETSYMFROMADDRPROC)GetProcAddress(module, "SymGetSymFromAddr");
114 if (!_SymGetSymFromAddr) return PR_FALSE;
115
116 _SymLoadModule = (SYMLOADMODULE)GetProcAddress(module, "SymLoadModule");
117 if (!_SymLoadModule) return PR_FALSE;
118
119 _SymUnDName = (SYMUNDNAME)GetProcAddress(module, "SymUnDName");
120 if (!_SymUnDName) return PR_FALSE;
121
122 _SymGetModuleInfo = (SYMGETMODULEINFO)GetProcAddress(module, "SymGetModuleInfo");
123 if (!_SymGetModuleInfo) return PR_FALSE;
124
125 _EnumerateLoadedModules = (ENUMLOADEDMODULES)GetProcAddress(module, "EnumerateLoadedModules");
126 if (!_EnumerateLoadedModules) return PR_FALSE;
127
128 _SymGetLineFromAddr = (SYMGETLINEFROMADDRPROC)GetProcAddress(module, "SymGetLineFromAddr");
129 if (!_SymGetLineFromAddr) return PR_FALSE;
130
131 gInitialized = PR_TRUE;
132 }
133
134 return gInitialized;
135}
136
137/*
138 * Callback used by SymGetModuleInfoEspecial
139 */
140static BOOL CALLBACK callbackEspecial(LPSTR aModuleName, ULONG aModuleBase, ULONG aModuleSize, PVOID aUserContext)
141{
142 BOOL retval = TRUE;
143 DWORD addr = (DWORD)aUserContext;
144
145 /*
146 * You'll want to control this if we are running on an
147 * architecture where the addresses go the other direction.
148 * Not sure this is even a realistic consideration.
149 */
150 const BOOL addressIncreases = TRUE;
151
152 /*
153 * If it falls in side the known range, load the symbols.
154 */
155 if(addressIncreases
156 ? (addr >= aModuleBase && addr <= (aModuleBase + aModuleSize))
157 : (addr <= aModuleBase && addr >= (aModuleBase - aModuleSize))
158 )
159 {
160 BOOL loadRes = FALSE;
161 HANDLE process = GetCurrentProcess();
162
163 loadRes = _SymLoadModule(process, NULL, aModuleName, NULL, aModuleBase, aModuleSize);
164 PR_ASSERT(FALSE != loadRes);
165 }
166
167 return retval;
168}
169
170/*
171 * SymGetModuleInfoEspecial
172 *
173 * Attempt to determine the module information.
174 * Bug 112196 says this DLL may not have been loaded at the time
175 * SymInitialize was called, and thus the module information
176 * and symbol information is not available.
177 * This code rectifies that problem.
178 */
179BOOL SymGetModuleInfoEspecial(HANDLE aProcess, DWORD aAddr, PIMAGEHLP_MODULE aModuleInfo, PIMAGEHLP_LINE aLineInfo)
180{
181 BOOL retval = FALSE;
182
183 /*
184 * Init the vars if we have em.
185 */
186 aModuleInfo->SizeOfStruct = sizeof(IMAGEHLP_MODULE);
187 if (nsnull != aLineInfo) {
188 aLineInfo->SizeOfStruct = sizeof(IMAGEHLP_LINE);
189 }
190
191 /*
192 * Give it a go.
193 * It may already be loaded.
194 */
195 retval = _SymGetModuleInfo(aProcess, aAddr, aModuleInfo);
196
197 if (FALSE == retval) {
198 BOOL enumRes = FALSE;
199
200 /*
201 * Not loaded, here's the magic.
202 * Go through all the modules.
203 */
204 enumRes = _EnumerateLoadedModules(aProcess, callbackEspecial, (PVOID)aAddr);
205 if(FALSE != enumRes)
206 {
207 /*
208 * One final go.
209 * If it fails, then well, we have other problems.
210 */
211 retval = _SymGetModuleInfo(aProcess, aAddr, aModuleInfo);
212 }
213 }
214
215 /*
216 * If we got module info, we may attempt line info as well.
217 * We will not report failure if this does not work.
218 */
219 if (FALSE != retval && nsnull != aLineInfo && nsnull != _SymGetLineFromAddr) {
220 DWORD displacement = 0;
221 BOOL lineRes = FALSE;
222
223 lineRes = _SymGetLineFromAddr(aProcess, aAddr, &displacement, aLineInfo);
224 }
225
226 return retval;
227}
228
229PRBool
230EnsureSymInitialized()
231{
232 static PRBool gInitialized = PR_FALSE;
233
234 if (! gInitialized) {
235 if (! EnsureImageHlpInitialized())
236 return PR_FALSE;
237 _SymSetOptions(SYMOPT_LOAD_LINES | SYMOPT_UNDNAME);
238 gInitialized = _SymInitialize(GetCurrentProcess(), 0, TRUE);
239 }
240 return gInitialized;
241}
242
243
244/**
245 * Walk the stack, translating PC's found into strings and recording the
246 * chain in aBuffer. For this to work properly, the dll's must be rebased
247 * so that the address in the file agrees with the address in memory.
248 * Otherwise StackWalk will return FALSE when it hits a frame in a dll's
249 * whose in memory address doesn't match it's in-file address.
250 *
251 * Fortunately, there is a handy dandy routine in IMAGEHLP.DLL that does
252 * the rebasing and accordingly I've made a tool to use it to rebase the
253 * DLL's in one fell swoop (see xpcom/tools/windows/rebasedlls.cpp).
254 */
255
256
257void
258DumpStackToFile(FILE* aStream)
259{
260 HANDLE myProcess = ::GetCurrentProcess();
261 HANDLE myThread = ::GetCurrentThread();
262 BOOL ok;
263
264 ok = EnsureSymInitialized();
265 if (! ok)
266 return;
267
268 // Get the context information for this thread. That way we will
269 // know where our sp, fp, pc, etc. are and can fill in the
270 // STACKFRAME with the initial values.
271 CONTEXT context;
272 context.ContextFlags = CONTEXT_FULL;
273 ok = GetThreadContext(myThread, &context);
274 if (! ok)
275 return;
276
277 // Setup initial stack frame to walk from
278 STACKFRAME frame;
279 memset(&frame, 0, sizeof(frame));
280 frame.AddrPC.Offset = context.Eip;
281 frame.AddrPC.Mode = AddrModeFlat;
282 frame.AddrStack.Offset = context.Esp;
283 frame.AddrStack.Mode = AddrModeFlat;
284 frame.AddrFrame.Offset = context.Ebp;
285 frame.AddrFrame.Mode = AddrModeFlat;
286
287 // Now walk the stack and map the pc's to symbol names
288 int skip = 2;
289 while (1) {
290 ok = _StackWalk(IMAGE_FILE_MACHINE_I386,
291 myProcess,
292 myThread,
293 &frame,
294 &context,
295 0, // read process memory routine
296 _SymFunctionTableAccess, // function table access routine
297 _SymGetModuleBase, // module base routine
298 0); // translate address routine
299
300 if (!ok) {
301 LPVOID lpMsgBuf;
302 FormatMessage(
303 FORMAT_MESSAGE_ALLOCATE_BUFFER |
304 FORMAT_MESSAGE_FROM_SYSTEM |
305 FORMAT_MESSAGE_IGNORE_INSERTS,
306 NULL,
307 GetLastError(),
308 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
309 (LPTSTR) &lpMsgBuf,
310 0,
311 NULL
312 );
313 fprintf(aStream, "### ERROR: WalkStack: %s", lpMsgBuf);
314 fflush(aStream);
315 LocalFree( lpMsgBuf );
316 }
317 if (!ok || frame.AddrPC.Offset == 0)
318 break;
319
320 if (skip-- > 0)
321 continue;
322
323 //
324 // Attempt to load module info before we attempt to reolve the symbol.
325 // This just makes sure we get good info if available.
326 //
327 IMAGEHLP_MODULE modInfo;
328 modInfo.SizeOfStruct = sizeof(modInfo);
329 BOOL modInfoRes = TRUE;
330 modInfoRes = SymGetModuleInfoEspecial(myProcess, frame.AddrPC.Offset, &modInfo, nsnull);
331
332 char buf[sizeof(IMAGEHLP_SYMBOL) + 512];
333 PIMAGEHLP_SYMBOL symbol = (PIMAGEHLP_SYMBOL) buf;
334 symbol->SizeOfStruct = sizeof(buf);
335 symbol->MaxNameLength = 512;
336
337 DWORD displacement;
338 ok = _SymGetSymFromAddr(myProcess,
339 frame.AddrPC.Offset,
340 &displacement,
341 symbol);
342
343 if (ok) {
344 fprintf(aStream, "%s+0x%08X\n", symbol->Name, displacement);
345 }
346 else {
347 fprintf(aStream, "0x%08X\n", frame.AddrPC.Offset);
348 }
349 }
350}
351
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette