como obter um rastreamento de pilha de arquivo de despejo programaticamente no Windows

StackOverflow https://stackoverflow.com/questions/838947

  •  22-07-2019
  •  | 
  •  

Pergunta

Eu quero recuperar rastreamento de pilha de um arquivo de despejo do usuário por meio de programação. Há este utilizador despejo no local conhecido e eu quero extrair apenas o rastreamento de pilha de fora e colocar isso em um arquivo de texto simples - há uma maneira de fazer isso?

NOTA: Eu posso fazê-lo manualmente - Open windbg e tipo de comando "k" - mas Como mencionei anteriormente eu quero fazer isso programaticamente.

Graças

Foi útil?

Solução

você deve verificar o windbg SDK subpasta com exemplos de como Dbgeng.dll pode ser usado de forma programática.
amostra de código:

 PSTR g_DumpFile;
 PSTR g_ImagePath;
 PSTR g_SymbolPath;

 ULONG64 g_TraceFrom[3];

 IDebugClient* g_Client;
 IDebugControl* g_Control;
 IDebugSymbols* g_Symbols;

  void CreateInterfaces(void)
  {
    HRESULT Status;

    // Start things off by getting an initial interface from
    // the engine.  This can be any engine interface but is
    // generally IDebugClient as the client interface is
    // where sessions are started.
    if ((Status = DebugCreate(__uuidof(IDebugClient),
                              (void**)&g_Client)) != S_OK)
    {
        Exit(1, "DebugCreate failed, 0x%X\n", Status);
    }

    // Query for some other interfaces that we'll need.
    if ((Status = g_Client->QueryInterface(__uuidof(IDebugControl),
                                           (void**)&g_Control)) != S_OK ||
        (Status = g_Client->QueryInterface(__uuidof(IDebugSymbols),
                                           (void**)&g_Symbols)) != S_OK)
    {
        Exit(1, "QueryInterface failed, 0x%X\n", Status);
    }
 }

 void
 DumpStack(void)
 {
    HRESULT Status;
    PDEBUG_STACK_FRAME Frames = NULL;
    int Count = 50;

    printf("\nFirst %d frames of the call stack:\n", Count);

    if (g_TraceFrom[0] || g_TraceFrom[1] || g_TraceFrom[2])
    {
        ULONG Filled;

        Frames = new DEBUG_STACK_FRAME[Count];
        if (Frames == NULL)
        {
            Exit(1, "Unable to allocate stack frames\n");
        }

        if ((Status = g_Control->
             GetStackTrace(g_TraceFrom[0], g_TraceFrom[1], g_TraceFrom[2],
                           Frames, Count, &Filled)) != S_OK)
        {
            Exit(1, "GetStackTrace failed, 0x%X\n", Status);
        }

        Count = Filled;
    }

    // Print the call stack.
    if ((Status = g_Control->
         OutputStackTrace(DEBUG_OUTCTL_ALL_CLIENTS, Frames,
                          Count, DEBUG_STACK_SOURCE_LINE |
                          DEBUG_STACK_FRAME_ADDRESSES |
                          DEBUG_STACK_COLUMN_NAMES |
                          DEBUG_STACK_FRAME_NUMBERS)) != S_OK)
    {
        Exit(1, "OutputStackTrace failed, 0x%X\n", Status);
    }

    delete[] Frames;
 }


 void __cdecl main(int Argc, __in_ecount(Argc) char** Argv)
 {
    CreateInterfaces();

    ParseCommandLine(Argc, Argv);

    ApplyCommandLineArguments();

    DumpStack();

    Exit(0, NULL);
 }

Outras dicas

Eu escrevi um artigo sobre despejo da pilha em C / C ++ com o Windows e Unix / Linux em DDJ há alguns anos. Ele não usa um cordump, mas ele grava quadros de pilha em um arquivo de log, por erros internos, ou quando o sistema operacional determina uma falha de aplicativo.

Talvez ele ajuda você:

Consulte http://www.ddj.com/architect/185300443

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top