Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract a user stream from a WinDbg extension?

I have embedded a custom stream in a dump (i.e. passed the UserStreamParam argument to MiniDumpWriteDump function). Now, I'm trying to extract the stream from a WinDbg extension. (Note that I have verified that I can retrieve the stream using the MiniDumpReadDumpStream function).

I'm using the IDebugAdvanced2::Request method with DEBUG_REQUEST_READ_USER_MINIDUMP_STREAM request. I am able to retrieve data from standard streams. For example, the following snippet will correctly retrieve the contents of the misc info stream.

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);

However, trying to retrieve my own stream will result in an error (0x80070570, ERROR_FILE_CORRUPT) and WinDbg outputs

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

Note that the same message appears as a part of the .dumpdebug output.

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

What is the problem? How do I retrieve contents of my user stream?

like image 532
avakar Avatar asked Nov 15 '22 01:11

avakar


1 Answers

very late answer

StreamType cannot be UserDefined StreamTypes

jen-lung chiu of ms posted so in osronline windbg lists long back

do not know if the latest dbgeng has this limitation eliminated

you either retrieve it with a dbghelp function independently

(using dbghelp functions inside windbg extensions are not recommended )

or parse the stream yourself with fopen() fread() like below

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;
    }
}   

output from an userdump that has userStreams in it

(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
like image 181
blabb Avatar answered Dec 15 '22 05:12

blabb