⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dia2dump.cpp

📁 一个 windows 内核级别的调试器
💻 CPP
📖 第 1 页 / 共 3 页
字号:
        rva = 0;	// no rva, must be an absolute value
    DWORD tag;
    pSym->get_symTag( &tag );
    BSTR name;
    if ( pSym->get_name( &name ) != S_OK )
        printf( "\t0x%08X (%ws) <no name>\n", rva, szTags[ tag ] );
	else {
		printf( "\t0x%08X (%ws) %ws\n", rva, szTags[ tag ], name );
		freeString( name );
	}
    return rva;
}

void		
DumpLex( IDiaSymbol* pscope, int tabs )
{
	HRESULT	hr;

    CComPtr<IDiaEnumSymbols> pSymbols;    
    if ( SUCCEEDED( pscope->findChildren( SymTagNull, NULL, nsNone, &pSymbols ) ) ) {
        CComPtr<IDiaSymbol> pSymbol;
        ULONG celt = 0;
        while ( pSymbols != NULL && SUCCEEDED( hr = pSymbols->Next( 1, &pSymbol, &celt ) ) && celt == 1 ) {
            for ( int i = 0; i < tabs; ++i )
                printf( "\t" );
            BSTR name;
            if ( pSymbol->get_name( &name ) == S_OK && name != NULL ) {
                printf( "%ws", name );
                freeString( name );
            }
            DWORD symTag;
            pSymbol->get_symTag( &symTag );
            printf( " : %ws\n", szTags[ symTag ] );
            switch ( symTag ) {
            case SymTagExe:
                assert( false );
                break;
            case SymTagCompiland:
	        case SymTagFunction:
	        case SymTagBlock:
                DumpLex( pSymbol, tabs+1 );
                break;
            default:
                break;
            }
            pSymbol = NULL;
        }
    }
}

void		
Dump(
	char *szFilename,
    IDiaDataSource* pSource,
    wchar_t* szLookup
)
{
	HRESULT	hr;
    wchar_t wszFilename[ _MAX_PATH ];
    mbstowcs( wszFilename, szFilename, sizeof( wszFilename ) );
    if ( FAILED( pSource->loadDataFromPdb( wszFilename ) ) ) 
        if ( FAILED( pSource->loadDataForExe( wszFilename, NULL, NULL ) ) ) 
            Fatal( "loadDataFromPdb/Exe" );

    if ( FAILED( pSource->openSession( &psession ) ) ) 
        Fatal( "openSession" );
    if ( FAILED( psession->get_globalScope( &pglobal) ) )
        Fatal( "get_globalScope" );
    CComPtr<IDiaEnumTables> pTables;
    if ( FAILED( psession->getEnumTables( &pTables ) ) )
        Fatal( "getEnumTables" );
    CComPtr< IDiaTable > pTable;
    ULONG celt = 0;

    while ( SUCCEEDED( hr = pTables->Next( 1, &pTable, &celt ) ) && celt == 1 ) {
        BSTR name;
        if ( pTable->get_name( &name ) != 0 )
            Fatal( "get_name" );
//        printf( "Found table: %ws\n", name );
        freeString( name );
        CComPtr<IDiaEnumSymbols> pSymbols;
        CComPtr<IDiaEnumSourceFiles> pSourceFiles;
        CComPtr<IDiaEnumSegments> pSegments;
        CComPtr<IDiaEnumSectionContribs> pSecContribs;
        if ( SUCCEEDED( pTable->QueryInterface( IID_IDiaEnumSymbols, (void**)&pSymbols ) ) ) {
            CComPtr<IDiaSymbol> pSymbol;
            while ( SUCCEEDED( hr = pSymbols->Next( 1, &pSymbol, &celt ) ) && celt == 1 ) {
                DWORD symIndex;
                pSymbol->get_symIndexId( &symIndex );
                DWORD symTag;
                pSymbol->get_symTag( &symTag );
                if ( pSymbol->get_name( &name ) == S_OK && name != NULL ) {
/*                    printf( "sym name: %ws", name );
                    freeString( name );
                    if ( symTag == SymTagPublicSymbol ) {
                        pSymbol->get_undecoratedName( &name );
                        printf( " (%ws)", name );
                        freeString( name );
                    }
*/
                    if ( symTag == SymTagUDT ) {
                        printType( pSymbol );
						printf("\n");
					}

/*                    if ( symTag == SymTagConstant ) {
						printf("const ");
                        printBasicType( pSymbol );
						printf(";\n");
					}*/

					if ( symTag == SymTagFunctionType) {
                        printBasicType( pSymbol );
						printf("\n");
					}

					if ( symTag==SymTagEnum ) {
                        printType( pSymbol );
						printf("\n");
					}				

					CComPtr<IDiaSymbol> pType;
                    if ( symTag == SymTagTypedef && pSymbol->get_type( &pType ) == S_OK ) {
                        printf( "typedef " );
                        printBasicType( pType, name );
						if(name[0])
							printf( " %ws;\n", name );
						else
							printf( ";\n");
						freeString( name );
					}
/*
						DWORD id = 0;
						CComPtr< IDiaSymbol > ptypeSym;
						pSymbol->get_typeId( &id );
						if ( FAILED( psession->symbolById( id, &ptypeSym ) ) ||
							psession->symsAreEquiv( pType, ptypeSym ) != S_OK ) {
								Fatal( "Sym type equality" );
						}
                    }
                    printf( "\n" );
*/
/*
                    if ( symTag == SymTagFunction ) {
                        dumpFunctionLines( pSymbol, psession );
                        DWORD loctype;
                        if ( pSymbol->get_locationType( &loctype ) == S_OK && loctype == LocIsStatic ) {
                            DWORD rva;
                            pSymbol->get_relativeVirtualAddress( &rva );
							if ( rva != 0 ) {
								dumpLocalVars( rva );

								CComPtr< IDiaSymbol > pfuncSym;
								if ( SUCCEEDED( psession->findSymbolByRVA( rva, SymTagNull, &pfuncSym ) ) ) {
									if ( psession->symsAreEquiv( pfuncSym, pSymbol ) != S_OK ) {
										BSTR name;
										DWORD tag;
										pfuncSym->get_symTag( &tag );
										pfuncSym->get_name( &name );
										printf( "\tfound alt symbol: %ws (%ws)\n", name != NULL ? name : L"", szTags[ tag ] );
										if ( name )
											freeString( name );
									}
								} else {
									Fatal( "No function symbol found by rva." );
								}
							}
                        }
                    }       
*/
                }
                if ( isScopeSym( symTag ) && szLookup ) {
                    FindCppNameInScope( szLookup, pSymbol );
                }
                pSymbol = NULL;
            }
        }/* else if ( SUCCEEDED( pTable->QueryInterface( IID_IDiaEnumSourceFiles, (void**)&pSourceFiles ) ) ) {
            CComPtr<IDiaSourceFile> pSourceFile;
            while ( SUCCEEDED( hr = pSourceFiles->Next( 1, &pSourceFile, &celt ) ) && celt == 1 ) {
                pSourceFile->get_fileName( &name );
                if ( name != NULL ) {
                    printf( "file name: %ws\n", name );
                    freeString( name );
                }
                pSourceFile = NULL;
            }
        } else if ( SUCCEEDED( pTable->QueryInterface( IID_IDiaEnumSegments, (void**)&pSegments ) ) ) {
            CComPtr<IDiaSegment> pSegment;
            while ( SUCCEEDED( hr = pSegments->Next( 1, &pSegment, &celt ) ) && celt == 1 ) {
                DWORD rva;
                DWORD seg;
                pSegment->get_addressSection( &seg );
                if ( pSegment->get_relativeVirtualAddress( &rva ) == S_OK ) {
                    printf( "Segment %i addr: 0x%.8X\n", seg, rva );
                    pSegment = NULL;
				    CComPtr<IDiaSymbol> pSym;
				    if ( psession->findSymbolByRVA( rva, SymTagNull, &pSym ) == S_OK ) {
					    BSTR name;
					    DWORD tag;
					    pSym->get_symTag( &tag );
					    pSym->get_name( &name );
					    printf( "\tClosest symbol: %ws (%ws)\n", name != NULL ? name : L"", szTags[ tag ] );
					    if ( name )
						    freeString( name );
                    }
                } else {
                    printf( "Segment %i \n", seg );
                    pSegment = NULL;
				    CComPtr<IDiaSymbol> pSym;
				    if ( SUCCEEDED( psession->findSymbolByAddr( seg, 0, SymTagNull, &pSym ) ) ) {
					    BSTR name;
					    DWORD tag;
					    pSym->get_symTag( &tag );
					    pSym->get_name( &name );
					    printf( "\tClosest symbol: %ws (%ws)\n", name != NULL ? name : L"", szTags[ tag ] );
					    if ( name )
						    freeString( name );
                    }
                }
            }
        } else if ( SUCCEEDED( pTable->QueryInterface( IID_IDiaEnumSectionContribs, (void**)&pSecContribs ) ) ) {
			printf( "SecContribs\n" );
            CComPtr<IDiaSectionContrib> pSecContrib;
            while ( SUCCEEDED( hr = pSecContribs->Next( 1, &pSecContrib, &celt ) ) && celt == 1 ) {
                DWORD rva;
                if ( pSecContrib->get_relativeVirtualAddress( &rva ) == S_OK ) {
                    printf( "\taddr: 0x%.8X", rva );
                    pSecContrib = NULL;
				    CComPtr<IDiaSymbol> pSym;
				    if ( psession->findSymbolByRVA( rva, SymTagNull, &pSym ) == S_OK ) {
					    BSTR name;
					    DWORD tag;
					    pSym->get_symTag( &tag );
					    pSym->get_name( &name );
					    printf( "     symbol: %ws (%ws)\n", name != NULL ? name : L"", szTags[ tag ] );
					    if ( name )
						    freeString( name );
                    } else {
					    printf( "<no symbol found?>\n" );
				    }
                } else {
                    DWORD isect;
                    DWORD offset;
                    pSecContrib->get_addressSection( &isect );
                    pSecContrib->get_addressOffset( &offset );
                    printf( "\taddr: 0x%.4X:0x%.8X", isect, offset );
                    pSecContrib = NULL;
				    CComPtr<IDiaSymbol> pSym;
				    if ( SUCCEEDED( psession->findSymbolByAddr( isect, offset, SymTagNull, &pSym ) ) ) {
					    BSTR name;
					    DWORD tag;
					    pSym->get_symTag( &tag );
					    pSym->get_name( &name );
					    printf( "     symbol: %ws (%ws)\n", name != NULL ? name : L"", szTags[ tag ] );
					    if ( name )
						    freeString( name );
                    } else {
					    printf( "<no symbol found?>\n" );
				    }            
                }
            }
        }
		*/

        pTable = NULL;
    }
    CComPtr<IDiaEnumSymbolsByAddr> pEnumByAddr;
	/*
    if ( FAILED( psession->getSymbolsByAddr( &pEnumByAddr ) ) )
        Fatal( "getSymbolsByAddr" );
    CComPtr<IDiaSymbol> pSym;
    if ( FAILED( pEnumByAddr->symbolByAddr( 0, 0, &pSym ) ) )
        Fatal( "symbolByAddr" );
    DWORD rvaLast = 0;
    if ( pSym->get_relativeVirtualAddress( &rvaLast ) == S_OK ) {
        pSym = 0;
        if ( FAILED( pEnumByAddr->symbolByRVA( rvaLast, &pSym ) ) )
            Fatal( "symbolByAddr" );
        printf( "Symbols in order\n" );
        do { 
            rvaLast = PrintNameAddr( pSym );
            pSym = 0;
            celt = 0;
            if ( FAILED( hr = pEnumByAddr->Next( 1, &pSym, &celt ) ) ) 
                break;
        } while ( celt == 1 );

        printf( "Symbols in reverse order\n" );
        pSym = 0;
        if ( FAILED( pEnumByAddr->symbolByRVA( rvaLast, &pSym ) ) )
            Fatal( "symbolByRva" );
        printf( "Symbols in order\n" );
        do { 
            rvaLast = PrintNameAddr( pSym );
            pSym = 0;
            celt = 0;
            if ( FAILED( hr = pEnumByAddr->Prev( 1, &pSym, &celt ) ) ) 
                break;
        } while ( celt == 1 );

        if ( FAILED( hr ) )
            Fatal( "Next" );
    }
	*/

}

void usage( int argc, char* argv[] )
{
    printf( "usage: %s <pdb-filename>\n", argv[0] );
    exit( -1 );
}

const char* diaPaths[] = {
        "msdia20.dll",
        "..\\bin\\msdia20.dll",
        "..\\..\\bin\\msdia20.dll",
        0
};

int main(int argc, char* argv[])
{
//	argc=2;
//	argv[1]="e:\\Progs\\BLINDS~1\\Utils\\TestDIA\\Sample\\Debug\\ntoskrnl.pdb";

    if ( argc < 2 ) {
        usage(argc, argv);
    }

	HRESULT	hr;
#ifndef NOCOM
    hr = CoInitialize(NULL);
	if (FAILED(hr))
	{
		Fatal("CoInitialize failed\n");
	}
#endif

    {

        CComPtr<IDiaDataSource> pSource;
	    // Initialize The Component Object Module Library

#ifdef NOCOM

#ifdef NOREG
        hr = NoRegCoCreate( "msdia20.dll", CLSID_DiaSourceAlt, IID_IDiaDataSource, (void **) &pSource );
#else
        hr = NoOleCoCreate( CLSID_DiaSourceAlt, IID_IDiaDataSource, (void **) &pSource );
#endif

#else
	    // Obtain Access To The Provider
	    hr = CoCreateInstance(CLSID_DiaSource, NULL, CLSCTX_INPROC_SERVER, 
		    IID_IDiaDataSource, (void **) &pSource);
        if ( hr == REGDB_E_CLASSNOTREG ) {
            for ( int i = 0; FAILED( hr ) && diaPaths[i] != 0; ++i ) {
                hr = NoRegCoCreate( diaPaths[i], CLSID_DiaSource, IID_IDiaDataSource, (void **) &pSource );
            }
        }
#endif
	    if (FAILED(hr))
	    {	
		    Fatal("Could not CoCreate CLSID_DiaSource(Alt). Register msdia20.dll or add its location to your PATH." );
	    }
	    if ( argc > 2 ) {
		    wchar_t name[ 256 ];
		    mbstowcs( name, argv[ 2 ], 256 );
		    Dump( argv[ 1 ], pSource, name );
            DumpLex( pglobal, 0 );
	    } else {
		    Dump( argv[ 1 ], pSource, NULL );
	    }
    }
    pglobal = 0;
    psession= 0;
#ifndef NOCOM
    CoUninitialize();
#endif
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -