📄 commonroutines.cpp
字号:
/*----------------------------------------------------------------------
"Debugging Applications" (Microsoft Press)
Copyright (c) 1997-2000 John Robbins -- All rights reserved.
With excellent updates from Scott Bloom, Ching Ming Kwok,
Jeff Shanholtz, and Pablo Presedo!
----------------------------------------------------------------------*/
#include "stdafx.h"
#include "Resource.h"
#include "CommonRoutines.h"
#include "CrashFinder.h"
BOOL InitializeSymbolEngine ( CSymbolEngine & cSymEng )
{
// Since I am not using the symbol engine with a real process, I
// will generate a unique value just for this document to use as the
// hProcess.
srand ( (unsigned)time ( NULL ) ) ;
HANDLE hRandHandle = (HANDLE)rand ( ) ;
BOOL bRet = cSymEng.SymInitialize ( hRandHandle , NULL , FALSE ) ;
if ( FALSE == bRet )
{
ASSERT ( TRUE == bRet ) ;
TRACE ( "m_cSymEng.SymInitialize FAILED!!!\n" ) ;
return ( FALSE ) ;
}
// Set the symbol engine to load line information. This must be
// because the symbol engine does not load source-lines by default.
// I also turn on the OMAP lookups so that the super-special OMAP
// moved blocks will get looked at as well. Trust me, don't ask.
DWORD dwOpts = SymGetOptions ( ) ;
SymSetOptions ( dwOpts |
SYMOPT_LOAD_LINES |
SYMOPT_OMAP_FIND_NEAREST ) ;
return ( TRUE ) ;
}
int LookUpAddressInformation ( CSymbolEngine & cSymEng ,
DWORD dwAddr ,
CString & sModule ,
IMAGEHLP_SYMBOL & stIHSym ,
DWORD & dwFnDispl ,
IMAGEHLP_LINE & stIHLine ,
DWORD & dwSrcDispl )
{
BOOL bSymFound = TRUE ;
BOOL bLineFound = TRUE ;
BOOL bModFound = TRUE ;
// Try and find the module. If this cannot be found, then it is no
// use looking anything else up.
CImageHlp_Module cMod ;
bModFound = cSymEng.SymGetModuleInfo ( dwAddr , &cMod ) ;
if ( FALSE == bModFound )
{
bSymFound = FALSE ;
bLineFound = FALSE ;
}
else
{
// Pull out the module.
sModule = cMod.ImageName ;
// Look up the symbol information.
BOOL bRet = cSymEng.SymGetSymFromAddr ( dwAddr ,
&dwFnDispl ,
&stIHSym ) ;
if ( FALSE == bRet )
{
bSymFound = FALSE ;
}
// Look up the line stuff.
bRet = cSymEng.SymGetLineFromAddr ( dwAddr ,
&dwSrcDispl ,
&stIHLine ) ;
if ( FALSE == bRet )
{
bLineFound = FALSE ;
}
}
int iRet = 0 ;
if ( TRUE == bSymFound )
{
iRet |= eLUSYM ;
}
if ( TRUE == bLineFound )
{
iRet |= eLULINE ;
}
if ( TRUE == bModFound )
{
iRet |= eLUMOD ;
}
return ( iRet ) ;
}
BOOL GetAddressSymbolString ( CSymbolEngine & cSymEng ,
DWORD dwAddr ,
CString & strOutString ,
LPCTSTR szSeparators )
{
// Since the largest symbol that the MS code generators can
// handle is 256 that is all that is needed.
IMAGEHLP_SYMBOL * pstIHSym = (IMAGEHLP_SYMBOL *)
new BYTE [ sizeof ( IMAGEHLP_SYMBOL ) + 256 ] ;
memset ( pstIHSym , NULL , sizeof ( IMAGEHLP_SYMBOL ) + 256 ) ;
pstIHSym->SizeOfStruct = sizeof ( IMAGEHLP_SYMBOL ) + 256 ;
pstIHSym->MaxNameLength = 256 ;
// The line information.
CImageHlp_Line stIHLine ;
// The displacements.
DWORD dwFnDispl ;
DWORD dwSrcDispl ;
// The module string.
CString sModule ;
// Do the look up.
int iRes = LookUpAddressInformation ( cSymEng ,
dwAddr ,
sModule ,
*pstIHSym ,
dwFnDispl ,
stIHLine ,
dwSrcDispl ) ;
if ( iRes < 0 )
{
delete [] pstIHSym ;
return ( FALSE ) ;
}
CCrashFinderApp * pApp = (CCrashFinderApp*)AfxGetApp ( ) ;
ASSERT ( NULL != pApp ) ;
if ( NULL == pApp )
{
delete [] pstIHSym ;
return ( FALSE ) ;
}
CString strTemp ;
strOutString = _T ( "" ) ;
// The address can always be displayed.
strTemp.Format ( _T ( "%08X" ) , dwAddr ) ;
strOutString += strTemp ;
strOutString += szSeparators ;
if ( eLUMOD == ( eLUMOD & iRes ) )
{
// Pull off the path information so the dialog does not
// have to be a mile wide.
TCHAR szFile[ MAX_PATH ] ;
TCHAR szExt[ MAX_PATH ] ;
_tsplitpath ( (LPCTSTR)sModule ,
NULL ,
NULL ,
szFile ,
szExt ) ;
strOutString += szFile ;
strOutString += szExt ;
}
else
{
strTemp.LoadString ( IDS_MODNOTFOUND ) ;
strOutString += strTemp ;
}
strOutString += szSeparators ;
if ( eLUSYM == ( eLUSYM & iRes ) )
{
strOutString += (LPCTSTR)pstIHSym->Name ;
if ( ( TRUE == pApp->m_bShowDisplacements ) &&
( 0 != dwFnDispl ) )
{
strTemp.Format ( IDS_DISPLACEMENTFMT , dwFnDispl ) ;
strOutString += strTemp ;
}
}
else
{
strTemp.LoadString ( IDS_FUNCNOTFOUND ) ;
strOutString += strTemp ;
}
strOutString += szSeparators ;
if ( eLULINE == ( eLULINE & iRes ) )
{
strOutString += stIHLine.FileName ;
if ( eLULINE == ( eLULINE & iRes ) )
{
strTemp.Format ( IDS_LINEFMT , stIHLine.LineNumber ) ;
strOutString += strTemp ;
if ( ( TRUE == pApp->m_bShowDisplacements ) &&
( 0 != dwSrcDispl ) )
{
strTemp.Format ( IDS_DISPLACEMENTFMT ,
dwSrcDispl ) ;
}
}
}
// I am done so get rid of the memory that I allocated earlier.
delete [] pstIHSym ;
return ( TRUE ) ;
}
BOOL CrashFinderTreeDisplay ::
InitializeTreeControl ( CTreeCtrl * pTree ,
CSymbolEngine * pSymEng )
{
// Set the tree and the symbol engine.
m_pTree = pTree ;
m_pSymEng = pSymEng ;
// Create the font to use.
m_pFont = new CFont ;
// TODO TODO - Make this a system wide option!
BOOL bRet =
m_pFont->CreatePointFont ( 100 , _T ( "Courier New" ) ) ;
ASSERT ( bRet ) ;
m_pTree->SetFont ( m_pFont , TRUE ) ;
// Add the image list to the tree control.
CImageList * pImageList = new CImageList ( ) ;
VERIFY ( pImageList->Create ( BITMAP_WIDTH ,
BITMAP_HEIGHT ,
ILC_MASK ,
2 ,
4 ) ) ;
CBitmap cBitmap ;
int iRet ;
VERIFY ( cBitmap.LoadBitmap ( IDB_BADITEM ) ) ;
iRet = pImageList->Add ( &cBitmap , (COLORREF)0xFFFFFF ) ;
ASSERT ( -1 != iRet ) ;
cBitmap.DeleteObject ( ) ;
VERIFY ( cBitmap.LoadBitmap ( IDB_GOODITEM ) ) ;
iRet = pImageList->Add ( &cBitmap , (COLORREF)0xFFFFFF ) ;
ASSERT ( -1 != iRet ) ;
cBitmap.DeleteObject ( ) ;
VERIFY ( cBitmap.LoadBitmap ( IDB_INFOITEM ) ) ;
iRet = pImageList->Add ( &cBitmap , (COLORREF)0xFFFFFF ) ;
ASSERT ( -1 != iRet ) ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -