Win32的 - 回溯从C code [英] Win32 - Backtrace from C code

查看:129
本文介绍了Win32的 - 回溯从C code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在寻找一种方式来获得信息的回溯Windows下,从C code(没有C ++)。

I'm currently looking for a way to get backtrace information under Windows, from C code (no C++).

我建立一个跨平台的C库,具有引用计数的内存管理。它还具有一个集成的内存调试器,提供了有关内存出错信息( XEOSÇ基础库)。

I'm building a cross-platform C library, with reference-counting memory management. It also have an integrated memory debugger that provides informations about memory mistakes (XEOS C Foundation Library).

当故障发生时,调试器推出时,提供故障信息,并存储记录参与。

When a fault occurs, the debugger is launched, providing information about the fault, and the memory record involved.

在Linux或Mac OS X,我可以看看 execinfo.h ,以使用回溯功能,这样我就可以显示内存故障的附加的相关信息。

On Linux or Mac OS X, I can look for execinfo.h in order to use the backtrace function, so I can display additional infos about the memory fault.

我正在寻找在Windows同样的事情。

I'm looking for the same thing on Windows.

我见过的 一个人怎么可以抢在C堆栈跟踪? 的堆栈溢出。我不希望使用第三方库,因此 CaptureStackBackTrace StackWalk 功能看起来不错。

I've seen How can one grab a stack trace in C? on Stack Overflow. I don't want to use a third-party library, so the CaptureStackBackTrace or StackWalk functions looks good.

唯一的问题是,我只是不明白如何使用它们,甚至与微软的文档。

The only problem is that I just don't get how to use them, even with the Microsoft documentation.

我不习惯到Windows编程,我通常在POSIX兼容的系统上运行。

I'm not used to Windows programming, as I usually work on POSIX compliant systems.

什么是一些解释这些功能,也许一些例子?

What are some explanations for those functions, and maybe some examples?

修改

我现在使用 CaptureStackBackTrace 函数从 DbgHelp.lib ,按原样似乎有少一点考虑架空...

I'm now considering using the CaptureStackBackTrace function from DbgHelp.lib, as is seems there's a little less overhead...

下面是我到目前为止已经试过:

Here's what I've tried so far:

unsigned int   i;
void         * stack[ 100 ];
unsigned short frames;
SYMBOL_INFO    symbol;
HANDLE         process;

process = GetCurrentProcess();

SymInitialize( process, NULL, TRUE );

frames = CaptureStackBackTrace( 0, 100, stack, NULL );

for( i = 0; i < frames; i++ )
{
    SymFromAddr( process, ( DWORD64 )( stack[ i ] ), 0, &symbol );

    printf( "%s\n", symbol.Name );
}

我刚开始垃圾。我想我应该用别的东西比 SymFromAddr

推荐答案

好了,现在我知道了。 :)

Alright, now I got it. : )

的问题是在SYMBOL_INFO结构。它需要在堆上进行分配,为符号的名称预留空间,并正确初始化。

The problem was in the SYMBOL_INFO structure. It needs to be allocated on the heap, reserving space for the symbol name, and initialized properly.

下面是最终code:

void printStack( void );
void printStack( void )
{
     unsigned int   i;
     void         * stack[ 100 ];
     unsigned short frames;
     SYMBOL_INFO  * symbol;
     HANDLE         process;

     process = GetCurrentProcess();

     SymInitialize( process, NULL, TRUE );

     frames               = CaptureStackBackTrace( 0, 100, stack, NULL );
     symbol               = ( SYMBOL_INFO * )calloc( sizeof( SYMBOL_INFO ) + 256 * sizeof( char ), 1 );
     symbol->MaxNameLen   = 255;
     symbol->SizeOfStruct = sizeof( SYMBOL_INFO );

     for( i = 0; i < frames; i++ )
     {
         SymFromAddr( process, ( DWORD64 )( stack[ i ] ), 0, symbol );

         printf( "%i: %s - 0x%0X\n", frames - i - 1, symbol->Name, symbol->Address );
     }

     free( symbol );
}

输出是:

6: printStack - 0xD2430
5: wmain - 0xD28F0
4: __tmainCRTStartup - 0xE5010
3: wmainCRTStartup - 0xE4FF0
2: BaseThreadInitThunk - 0x75BE3665
1: RtlInitializeExceptionChain - 0x770F9D0F
0: RtlInitializeExceptionChain - 0x770F9D0F

这篇关于Win32的 - 回溯从C code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆