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

📄 dia2dump.cpp

📁 一个 windows 内核级别的调试器
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// dia2dump.cpp : Dump contents of a pdb file using DIA.
//
//       Copyright (c) Microsoft Corporation. All rights reserved.
//
//---------------------------------------------------------------
//

#include "stdafx.h"
#include "diacreate.h"

void Fatal( const char *msg )
{
    printf( msg );
    printf( "\n" );
    exit( -1 );
}

#ifdef NOCOM

#define freeString LocalFree

#else

#define freeString SysFreeString

#endif

CComPtr<IDiaSession> psession;
CComPtr<IDiaSymbol> pglobal;

void printVariant( VARIANT& v )
{
    switch( v.vt )
    {
 //*    LONGLONG       VT_I8
    case VT_I8:
        printf( "%ld", v.llVal );
        break;
 //*    LONG           VT_I4
    case VT_I4:
        printf( "%d", v.lVal );
        break;
 //*    BYTE           VT_UI1
    case VT_UI1:
        printf( "%d", v.bVal);
        break;
 //*    SHORT          VT_I2
    case VT_I2:
        printf( "%d", v.iVal);
        break;
 //*    CHAR           VT_I1
    case VT_I1:
        printf( "%d", v.cVal);
        break;
 //*    USHORT         VT_UI2
    case VT_UI2:
        printf( "%d", v.uiVal);
        break;
//*    ULONG          VT_UI4
    case VT_UI4:
        printf( "%d", v.ulVal);
        break;
 //*    ULONGLONG      VT_UI8
    case VT_UI8:
        printf( "%ld", v.ullVal);
        break;
 //*    INT            VT_INT
    case VT_INT:
        printf( "%d", v.intVal);
        break;
 //*    UINT           VT_UINT
    case VT_UINT:
        printf( "%d", v.uintVal);
        break;
    default:
        printf( "<Not implemented>" );
        break;
    }
}

void printBound( IDiaSymbol* pBound )
{
    DWORD tag = 0;
    BSTR name;
    DWORD kind;
    pBound->get_symTag( &tag );
    pBound->get_locationType( &kind );
    if ( tag == SymTagData && kind == LocIsConstant ) {
        VARIANT v;
        pBound->get_value( &v );
        printVariant( v );
    } else if ( pBound->get_name( &name ) == S_OK ) {
        printf( "%ws", name );
        freeString( name );
    }
}

void printBasicType( IDiaSymbol* pType, BSTR name=0 );

void printType( IDiaSymbol* pType )
{
    CComPtr< IDiaEnumSymbols > pEnum;
    DWORD tag = 0;
    pType->get_symTag( &tag );
    BSTR name;
    ULONGLONG size=0;
	BOOL packed=0;

	if(tag == SymTagEnum)
	{
		pType->get_name( &name );
		printf("enum %ws {\n",name);

		DWORD celt;
        LONG count;
        CComPtr< IDiaEnumSymbols > pEnum;

		if ( pType->findChildren( SymTagNull, NULL, nsNone, &pEnum ) == S_OK && pEnum != NULL && SUCCEEDED( pEnum->get_Count( &count ) ) && count > 0 ){
                CComPtr< IDiaSymbol > pSym;
                while ( pEnum->Next( 1, &pSym, &celt ) == S_OK && celt == 1 ) {
					printf("\t");
                    printBasicType( pSym );
					printf( "\n" );
                    pSym = NULL;
                }
		}
			
		printf("};\n");
		return;
	}

	if(tag == SymTagUDT)
	{
		pType->get_length( &size );
		pType->get_packed( &packed );
	}

    if ( pType->get_name( &name ) == S_OK && name != NULL ) {
		printf( "struct %ws {", name);
		printf("\n");
	}
	DWORD celt;
    LONG count;
	if ( pType->findChildren( SymTagNull, NULL, nsNone, &pEnum ) == S_OK && pEnum != NULL && SUCCEEDED( pEnum->get_Count( &count ) ) && count > 0 )
	{
		CComPtr< IDiaSymbol > pSym;
        while ( pEnum->Next( 1, &pSym, &celt ) == S_OK && celt == 1 ) {
			printf("\t");
			CComPtr<IDiaSymbol> pType;
			if(pSym->get_name( &name ) != S_OK)
				name=L"";

            if ( pSym->get_type( &pType ) == S_OK )
				printBasicType( pType, name );
			else
				printf( "invalid" );
			if(name[0])
			{
				printf( " %ws", name );
				freeString( name );
			} 
			LONG offset=0;
			ULONGLONG len=0;
			pSym->get_length(&len);
			pSym->get_offset(&offset);
			if(!offset)
			{
				DWORD bp=0;
				pSym->get_bitPosition(&bp);
				offset=bp;
			}

			DWORD loct=0;
			pSym->get_locationType(&loct);

			if(loct==LocIsBitField)
				printf(":%d",len);

			printf(";");

			if(len)
			{
				if(loct==LocIsBitField)
					printf(" \t// bit offset: %02X",(int)offset);
				else
					printf(" \t// offset: %02X",(int)offset);

				if(len)
					printf(", len=%d",(int)len);
			}

			printf("\n");
            pSym = NULL;
        }
	}
	printf("};\n");
}

void printBasicType( IDiaSymbol* pType, BSTR fname)
{
	char *BasicTypes[]={
		"NoType",	//	0
		"void",
		"char",
		"wchar_t",	//	3
		"err4",
		"err5",
		"int",		//	6
		"DWORD",
		"float",
		"BCD",		
		"bool",		//	10
		"err11", "err12", "err13", "err14", "err15", 
		"err16", "err17", "err18", "err19", "err20", 
		"err21", "err22", "err23", "err24", 
		"Currency",
		"Date",
		"VARIANT",
		"Complex",
		"Bit",
		"BSTR",
		"HRESULT",
		"err32"
	};
    DWORD tag = 0;
    pType->get_symTag( &tag );
    BSTR name;
    pType->get_name( &name );
	if ( tag == SymTagPointerType ) {
        CComPtr<IDiaSymbol> pBaseType;
        if (  pType->get_type( &pBaseType ) == S_OK ) {
			DWORD tag1 = 0;
			pBaseType->get_symTag( &tag1 );
			if(tag1!=SymTagFunctionType)
			{
				printBasicType( pBaseType );
				printf( "*" );
			} else
			{
				if(fname==0)
				{
					fname=new wchar_t[2];
					fname[0]=0;
					fname[1]=0;
				}
				wchar_t Buff[1024]=L"(*";
				wcscpy(Buff+2,fname);
				wcscat(Buff,L")");
				printBasicType( pBaseType, Buff );
				fname[0]=0;
			}
        } else {
            Fatal( "pointer get_type" );
        }
    } else if ( tag == SymTagEnum ) {
		printf("enum %ws",name);
    } else if ( tag == SymTagBaseType ) {
        ULONGLONG size;
        DWORD bt;
        if ( pType->get_length( &size ) != S_OK )
            Fatal( "Internal error, no Length" );
        if ( pType->get_baseType( &bt ) != S_OK )
            Fatal( "Internal error, no baseType" );
		if(bt<32)
			printf("%s",BasicTypes[bt]);
		else
			wprintf( L"(base type=%d, len=%ld)  ", bt, size );
    } else if ( tag == SymTagArrayType ) {
        CComPtr<IDiaSymbol> pBaseType;
        if ( pType->get_type( &pBaseType ) == S_OK ) {
            printBasicType( pBaseType );
        } else {
            Fatal( "array get_type" );
        }

		if(fname)
		{
			printf(" %ws",fname);
			fname[0]=0;
		}

        DWORD rank;
        DWORD celt;
        LONG count;
        CComPtr< IDiaEnumSymbols > pEnum;
        if ( pType->get_rank( &rank ) == S_OK ) {
            if ( pType->findChildren( SymTagDimension, NULL, nsNone, &pEnum ) == S_OK && pEnum != NULL ) {
                CComPtr< IDiaSymbol > pSym;
                while ( pEnum->Next( 1, &pSym, &celt ) == S_OK && celt == 1 ) {
                    CComPtr< IDiaSymbol > pBound;
                    printf( "[" );
                    if ( pSym->get_lowerBound( &pBound ) == S_OK ) {
                        printBound( pBound );
                        printf("..");
                    }
                    pBound = NULL;
                    if ( pSym->get_upperBound( &pBound ) == S_OK ) {
                        printBound( pBound );
                    }
                    pBound = NULL;
                    printf( "]" );
                    pSym = NULL;

                }
            } 
        } else if ( pType->findChildren( SymTagCustomType, NULL, nsNone, &pEnum ) == S_OK && pEnum != NULL && SUCCEEDED( pEnum->get_Count( &count ) ) && count > 0 ){
                CComPtr< IDiaSymbol > pSym;
                while ( pEnum->Next( 1, &pSym, &celt ) == S_OK && celt == 1 ) {
                    printf( "[" );
                    printBasicType( pSym );
                    printf( "]" );
                    pSym = NULL;
                }
        } else {
            ULONGLONG lenArray;
            ULONGLONG lenElem;
            if ( pType->get_length( &lenArray ) != S_OK
                || pBaseType->get_length( &lenElem ) != S_OK ) {
                Fatal( "No type length." );
            }
            printf( "[%ld]", lenArray/lenElem );
        }
    } else if ( tag == SymTagUDT ) {
	    printf( "%ws", name );
    } else if ( tag == SymTagFunctionType ) {
        DWORD celt;
        LONG count;

		CComPtr< IDiaEnumSymbols > pEnum;
		CComPtr< IDiaSymbol > pRetType;
		pType->get_type(&pRetType);
		printBasicType(pRetType);
		DWORD dcount=0;
		pType->get_count(&dcount);

		printf(" %ws(", fname);
		if(fname)
			fname[0]=0;

		if ( pType->findChildren( SymTagNull, NULL, nsNone, &pEnum ) == S_OK && pEnum != NULL && SUCCEEDED( pEnum->get_Count( &count ) ) && count > 0 ){
                CComPtr< IDiaSymbol > pSym;
                while ( pEnum->Next( 1, &pSym, &celt ) == S_OK && celt == 1 ) {
	                CComPtr< IDiaSymbol > pSym1;
					pSym->get_type(&pSym1);
					BSTR tmp=L"bad_func";
					BSTR nname=0;
					pSym1->get_name(&nname);
					if(nname=0)
						nname=tmp;
                    printBasicType( pSym1, nname );
					if(--dcount)
						printf( ", " );
                    pSym = NULL;
                }
		}
		printf(")");
/*    } else if ( tag == SymTagConstant ) {
		printf("%ws",name);
		VARIANT v;
		pType->get_value(&v);
		printVariant(v);*/
    } else if ( tag == SymTagData ) {
		printf("%ws,",name);
		DWORD len=0;
        if ( pType->get_dataBytes( 0, &len, NULL ) == S_OK && len > 0 ) {
            BYTE* pdata = new BYTE[ len ];
            pType->get_dataBytes( len, &len, pdata );
            printf( "<data" );
            for ( DWORD i = 0; i < len; ++i ) {
                printf( " %02x", pdata[i] );
            }
            printf( " data>" );
            delete [] pdata;
        }
    } else if ( tag == SymTagCustomType ) {
        printf( "Custom Type: " );
        DWORD id;
        DWORD rec;
        GUID guid;
        if ( pType->get_guid( &guid ) == S_OK ) {
            wchar_t wszGuid[64];
            StringFromGUID2(guid, wszGuid, 64);
            printf( "%ws", wszGuid );

⌨️ 快捷键说明

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