【发布时间】:2016-05-05 13:11:44
【问题描述】:
我正在尝试从在 cygwin 下编译的 c 代码打印回溯。 我从这里遵循了第一个答案: Win32 - Backtrace from C code
而且我在cygwin shell下写过:
User@WIN-EO6FNSIC9C1 ~
$ cat test.c
#include <windows.h>
#include <dbghelp.h>
#include <assert.h>
#include <stdio.h>
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, 50, 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 );
}
int foo2() {
printStack();
}
void foo() {
foo2();
}
int main() {
foo();
}
User@WIN-EO6FNSIC9C1 ~
$ gcc test.c -ldbghelp -g
test.c: In function ‘printStack’:
test.c:22:32: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
SymFromAddr( process, ( DWORD64 )( stack[ i ] ), 0, symbol );
^
User@WIN-EO6FNSIC9C1 ~
$ ./a.exe
4: - 0x0
3: - 0x0
2: - 0x0
1: - 0x0
0: cygwin_dll_init - 0x61006C30
为什么它打印空行并将地址归零?是否可以在 cygwin 下使用 CaptureStackBackTrace 或类似的东西打印回溯?
【问题讨论】:
-
乔尔,对不起,我已将其删除。
-
你写的 C 程序不是 cygwin 的,它是 windows 的,所以你需要 mingw64-i686 或 mingw64-x86_64 头文件和编译器
-
@matzeri,它在cygwin下工作,看我的回答
标签: c debugging cygwin backtrace