Back to index

lightning-sunbird  0.9+nobinonly
nsStackFrameWin.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is nsStackFrameWin.h code, released
00017  * December 20, 2000.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 2000
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   Michael Judge, 20-December-2000
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either of the GNU General Public License Version 2 or later (the "GPL"),
00029  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 #ifndef nsStackFrameWin_h___
00041 #define nsStackFrameWin_h___
00042 
00043 
00044 #if defined(_WIN32) && (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_IA64))
00045 // WIN32 x86 stack walking code
00046 #include "nspr.h"
00047 #include <windows.h>
00048 #ifdef _M_IX86
00049 #include <imagehlp.h>
00050 // We need a way to know if we are building for WXP (or later), as if we are, we
00051 // need to use the newer 64-bit APIs. API_VERSION_NUMBER seems to fit the bill.
00052 // A value of 9 indicates we want to use the new APIs.
00053 #if API_VERSION_NUMBER >= 9
00054 #define USING_WXP_VERSION 1
00055 #endif
00056 #endif
00057 
00058 // Define these as static pointers so that we can load the DLL on the
00059 // fly (and not introduce a link-time dependency on it). Tip o' the
00060 // hat to Matt Pietrick for this idea. See:
00061 //
00062 //   http://msdn.microsoft.com/library/periodic/period97/F1/D3/S245C6.htm
00063 //
00064 PR_BEGIN_EXTERN_C
00065 
00066 typedef DWORD (__stdcall *SYMSETOPTIONSPROC)(DWORD);
00067 extern SYMSETOPTIONSPROC _SymSetOptions;
00068 
00069 typedef BOOL (__stdcall *SYMINITIALIZEPROC)(HANDLE, LPSTR, BOOL);
00070 extern SYMINITIALIZEPROC _SymInitialize;
00071 
00072 typedef BOOL (__stdcall *SYMCLEANUPPROC)(HANDLE);
00073 extern SYMCLEANUPPROC _SymCleanup;
00074 
00075 typedef BOOL (__stdcall *STACKWALKPROC)(DWORD,
00076                                         HANDLE,
00077                                         HANDLE,
00078                                         LPSTACKFRAME,
00079                                         LPVOID,
00080                                         PREAD_PROCESS_MEMORY_ROUTINE,
00081                                         PFUNCTION_TABLE_ACCESS_ROUTINE,
00082                                         PGET_MODULE_BASE_ROUTINE,
00083                                         PTRANSLATE_ADDRESS_ROUTINE);
00084 extern  STACKWALKPROC _StackWalk;
00085 
00086 #ifdef USING_WXP_VERSION
00087 typedef BOOL (__stdcall *STACKWALKPROC64)(DWORD,
00088                                           HANDLE,
00089                                           HANDLE,
00090                                           LPSTACKFRAME64,
00091                                           PVOID,
00092                                           PREAD_PROCESS_MEMORY_ROUTINE64,
00093                                           PFUNCTION_TABLE_ACCESS_ROUTINE64,
00094                                           PGET_MODULE_BASE_ROUTINE64,
00095                                           PTRANSLATE_ADDRESS_ROUTINE64);
00096 extern  STACKWALKPROC64 _StackWalk64;
00097 #endif
00098 
00099 typedef LPVOID (__stdcall *SYMFUNCTIONTABLEACCESSPROC)(HANDLE, DWORD);
00100 extern  SYMFUNCTIONTABLEACCESSPROC _SymFunctionTableAccess;
00101 
00102 #ifdef USING_WXP_VERSION
00103 typedef LPVOID (__stdcall *SYMFUNCTIONTABLEACCESSPROC64)(HANDLE, DWORD64);
00104 extern  SYMFUNCTIONTABLEACCESSPROC64 _SymFunctionTableAccess64;
00105 #endif
00106 
00107 typedef DWORD (__stdcall *SYMGETMODULEBASEPROC)(HANDLE, DWORD);
00108 extern  SYMGETMODULEBASEPROC _SymGetModuleBase;
00109 
00110 #ifdef USING_WXP_VERSION
00111 typedef DWORD64 (__stdcall *SYMGETMODULEBASEPROC64)(HANDLE, DWORD64);
00112 extern  SYMGETMODULEBASEPROC64 _SymGetModuleBase64;
00113 #endif
00114 
00115 typedef BOOL (__stdcall *SYMGETSYMFROMADDRPROC)(HANDLE, DWORD, PDWORD, PIMAGEHLP_SYMBOL);
00116 extern  SYMGETSYMFROMADDRPROC _SymGetSymFromAddr;
00117 
00118 #ifdef USING_WXP_VERSION
00119 typedef BOOL (__stdcall *SYMFROMADDRPROC)(HANDLE, DWORD64, PDWORD64, PSYMBOL_INFO);
00120 extern  SYMFROMADDRPROC _SymFromAddr;
00121 #endif
00122 
00123 typedef DWORD ( __stdcall *SYMLOADMODULE)(HANDLE, HANDLE, PSTR, PSTR, DWORD, DWORD);
00124 extern  SYMLOADMODULE _SymLoadModule;
00125 
00126 #ifdef USING_WXP_VERSION
00127 typedef DWORD ( __stdcall *SYMLOADMODULE64)(HANDLE, HANDLE, PCSTR, PCSTR, DWORD64, DWORD);
00128 extern  SYMLOADMODULE64 _SymLoadModule64;
00129 #endif
00130 
00131 typedef DWORD ( __stdcall *SYMUNDNAME)(PIMAGEHLP_SYMBOL, PSTR, DWORD);
00132 extern  SYMUNDNAME _SymUnDName;
00133 
00134 typedef DWORD ( __stdcall *SYMGETMODULEINFO)( HANDLE, DWORD, PIMAGEHLP_MODULE);
00135 extern  SYMGETMODULEINFO _SymGetModuleInfo;
00136 
00137 #ifdef USING_WXP_VERSION
00138 typedef BOOL ( __stdcall *SYMGETMODULEINFO64)( HANDLE, DWORD64, PIMAGEHLP_MODULE64);
00139 extern  SYMGETMODULEINFO64 _SymGetModuleInfo64;
00140 #endif
00141 
00142 typedef BOOL ( __stdcall *ENUMLOADEDMODULES)( HANDLE, PENUMLOADED_MODULES_CALLBACK, PVOID);
00143 extern  ENUMLOADEDMODULES _EnumerateLoadedModules;
00144 
00145 #ifdef USING_WXP_VERSION
00146 typedef BOOL ( __stdcall *ENUMLOADEDMODULES64)( HANDLE, PENUMLOADED_MODULES_CALLBACK64, PVOID);
00147 extern  ENUMLOADEDMODULES64 _EnumerateLoadedModules64;
00148 #endif
00149 
00150 typedef BOOL (__stdcall *SYMGETLINEFROMADDRPROC)(HANDLE, DWORD, PDWORD, PIMAGEHLP_LINE);
00151 extern  SYMGETLINEFROMADDRPROC _SymGetLineFromAddr;
00152 
00153 #ifdef USING_WXP_VERSION
00154 typedef BOOL (__stdcall *SYMGETLINEFROMADDRPROC64)(HANDLE, DWORD64, PDWORD, PIMAGEHLP_LINE64);
00155 extern  SYMGETLINEFROMADDRPROC64 _SymGetLineFromAddr64;
00156 #endif
00157 
00158 extern HANDLE hStackWalkMutex; 
00159 
00160 HANDLE GetCurrentPIDorHandle();
00161 
00162 PRBool EnsureSymInitialized();
00163 
00164 PRBool EnsureImageHlpInitialized();
00165 
00166 /*
00167  * SymGetModuleInfoEspecial
00168  *
00169  * Attempt to determine the module information.
00170  * Bug 112196 says this DLL may not have been loaded at the time
00171  *  SymInitialize was called, and thus the module information
00172  *  and symbol information is not available.
00173  * This code rectifies that problem.
00174  * Line information is optional.
00175  */
00176 BOOL SymGetModuleInfoEspecial(HANDLE aProcess, DWORD aAddr, PIMAGEHLP_MODULE aModuleInfo, PIMAGEHLP_LINE aLineInfo);
00177 
00178 struct DumpStackToFileData {
00179   FILE *stream;
00180   HANDLE thread;
00181   HANDLE process;
00182 };
00183 
00184 void PrintError(char *prefix, FILE* out);
00185 void DumpStackToFile(FILE* out);
00186 DWORD WINAPI  DumpStackToFileThread(LPVOID data);
00187 void DumpStackToFileMain64(struct DumpStackToFileData* data);
00188 void DumpStackToFileMain(struct DumpStackToFileData* data);
00189 
00190 
00191 PR_END_EXTERN_C
00192 
00193 #else
00194 #pragma message( "You probably need to fix this file to handle your target platform" )
00195 #endif //WIN32
00196 
00197 #endif //nsStackFrameWin_h___