如何从WinDbg扩展中提取用户流?

时间:2011-03-30 13:53:45

标签: c++ com windbg dbgeng

我在转储中嵌入了一个自定义流(即将UserStreamParam参数传递给MiniDumpWriteDump函数)。现在,我正在尝试从WinDbg扩展中提取流。 (请注意,我已经验证我可以使用MiniDumpReadDumpStream函数检索流。)

我正在使用IDebugAdvanced2::Request方法和DEBUG_REQUEST_READ_USER_MINIDUMP_STREAM请求。我能够从标准流中检索数据。例如,以下代码段将正确检索misc信息流的内容。

DEBUG_READ_USER_MINIDUMP_STREAM rums = {};
rums.StreamType = MiscInfoStream;
rums.Buffer = &buf;
rums.BufferSize = sizeof buf;
hr = p->Request(DEBUG_REQUEST_READ_USER_MINIDUMP_STREAM,
    &rums, sizeof rums, 0, 0, 0);

但是,尝试检索自己的流会导致错误(0x80070570,ERROR_FILE_CORRUPT)和WinDbg输出

Dir entry 11, ??? stream has unknown stream type 6381921

请注意,相同的消息会显示为.dumpdebug输出的一部分。

Stream 11: type ??? (6381921), size 00000038, RVA 00033FA9
Dir entry 11, ??? stream has unknown stream type 6381921

有什么问题?如何检索用户流的内容?

2 个答案:

答案 0 :(得分:1)

很晚才回答

StreamType cannot be UserDefined StreamTypes

jen-lung chiu of ms张贴在osronline windbg列出的长片

不知道最新的dbgeng是否已消除此限制

您可以使用独立的dbghelp函数检索它

(建议不要在windbg扩展名中使用dbghelp函数)

或者用fopen()fread()解析你自己的流,如下所示

userstream:\>type ..\usrstr.cpp


#include <stdio.h>
#include <engextcpp.hpp>
#include <dbghelp.h>

const ULONG MBUFFSIZE = 0x1000;
PVOID   Buff = 0;

int __cdecl ReadUserStream (char *dmpfile)
{
    PMINIDUMP_HEADER MiniHeader = 0;
    PMINIDUMP_DIRECTORY MiniDir = 0;
    PMINIDUMP_USER_STREAM userstream = 0;
    size_t result = 0;
    ULONG Streams =0;
    ULONG i = 0;
    FILE * fp = fopen(dmpfile,"rb");
    if (fp)
    {
        result = fread(Buff, 1, sizeof(MINIDUMP_HEADER), fp );
        if ( result == sizeof(MINIDUMP_HEADER) )
        {
            MiniHeader = (PMINIDUMP_HEADER) Buff;
            Streams = MiniHeader->NumberOfStreams;
            for (i  = 0; i < Streams; i++ )
            {
                result = fread( Buff, 1, sizeof(MINIDUMP_DIRECTORY), fp );
                if ( result == sizeof(MINIDUMP_DIRECTORY) )
                {
                    MiniDir = (PMINIDUMP_DIRECTORY) Buff;
                    if ( MiniDir->StreamType > LastReservedStream )
                    {
                        userstream = (PMINIDUMP_USER_STREAM)Buff;
                        ULONG savedbuffsize = userstream->BufferSize;
                        ULONG savedtype = userstream->Type;
                        PCHAR savedbufferptr = (PCHAR)userstream->Buffer;
                        long pos = ftell(fp);
                        fseek(fp, (long)savedbufferptr,SEEK_SET);
                        result = fread( Buff, 1, savedbuffsize, fp );
                        if ( result == savedbuffsize )
                        {
                            printf(
                                "\n"
                                "Datastream Type = %.8x\n"
                                "Buffer Size     = %.8x\n"
                                "Buffer          = %p\n"
                                "Buffer content  = %s\n"
                                "\n",
                                savedtype,
                                savedbuffsize,
                                savedbufferptr,
                                Buff
                                );
                            fseek(fp,pos,SEEK_SET);
                            continue;
                        }
                        else
                        {
                            printf(
                                "failed to read buffer contents at offset %p of
user stream %x\n",
                                savedbufferptr,
                                savedtype);
                            fseek(fp,pos,SEEK_SET);
                            continue;
                        }
                    }

                }
                else
                {
                    printf("failed to fread Minidump directory exiting \n");
                    goto getout;
                }

            }
        }
        else
        {
            printf("failed to fread Minidump header exiting \n");
            goto getout;
        }
    }
    else
    {
        printf("failed to open dmp file exiting \n");
        goto getout;
    }
getout:
    if (fp)
        fclose(fp);
    return 0;
}

int __cdecl main (int argc, char * argv[])
{
    if (argc !=2)
    {
        printf("Usage %s %s\n",argv[0],"somedump.dmp");
        return 0;
    }
    Buff = malloc( MBUFFSIZE );
    if (Buff)
    {
        ReadUserStream(argv[1]);
        free(Buff);
        return 0;
    }
    else
    {
        printf("malloc failed exiting\n");
        return 0;
    }
}   

从其中包含userStreams的userdump输出

oleg staradumov debuginfo.com writeuserstream.cpp

userstream:\>usrstr.exe
Usage usrstr.exe somedump.dmp

userstream:\>usrstr.exe test.dmp
Datastream Type = 00010000
Buffer Size     = 00000021
Buffer          = 000010B6
Buffer content  = This is the first data stream...


Datastream Type = 00010001
Buffer Size     = 00000023
Buffer          = 000010D7
Buffer content  = and this is the second data stream

答案 1 :(得分:0)

即使没有经过测试,如果用自定义值(大于LastReservedStream = 0xFFFF)而不是MiscInfoStream填充StreamType,它应该可以工作。