#include <stdio.h>
#import <execinfo.h>
void __cyg_profile_func_enter (void *, void *) __attribute__((no_instrument_function));
void __cyg_profile_func_exit (void *, void *) __attribute__((no_instrument_function));
int depth = -1;
void __cyg_profile_func_enter (void *func, void *caller)
{
int n;
depth++;
for (n = 0; n < depth; n++)
printf (" ");
printf ("-> %p\n", func);
void *bt[2];
int bt_size;
char **bt_syms;
bt_size = backtrace(bt, 2);
bt_syms = backtrace_symbols(bt, bt_size);
if(bt_size>=2){
printf("a%s\n",bt_syms[1]);
}
if(bt_syms) free((void*)bt_syms);
}
void __cyg_profile_func_exit (void *func, void *caller)
{
int n;
for (n = 0; n < depth; n++)
printf (" ");
printf ("<- %p\n", func);
depth--;
}
void bar(void)
{
}
void foo (void)
{
int x;
for (x = 0; x < 4; x++)
bar ();
}
int main (int argc, char *argv[])
{
foo ();
bar ();
return 0;
}
#import <execinfo.h>
void __cyg_profile_func_enter (void *, void *) __attribute__((no_instrument_function));
void __cyg_profile_func_exit (void *, void *) __attribute__((no_instrument_function));
int depth = -1;
void __cyg_profile_func_enter (void *func, void *caller)
{
int n;
depth++;
for (n = 0; n < depth; n++)
printf (" ");
printf ("-> %p\n", func);
void *bt[2];
int bt_size;
char **bt_syms;
bt_size = backtrace(bt, 2);
bt_syms = backtrace_symbols(bt, bt_size);
if(bt_size>=2){
printf("a%s\n",bt_syms[1]);
}
if(bt_syms) free((void*)bt_syms);
}
void __cyg_profile_func_exit (void *func, void *caller)
{
int n;
for (n = 0; n < depth; n++)
printf (" ");
printf ("<- %p\n", func);
depth--;
}
void bar(void)
{
}
void foo (void)
{
int x;
for (x = 0; x < 4; x++)
bar ();
}
int main (int argc, char *argv[])
{
foo ();
bar ();
return 0;
}
本文深入探讨了一段使用C语言编写的程序,通过详细解析`__cyg_profile_func_enter`和`__cyg_profile_func_exit`函数的作用,展示了如何进行函数调用的跟踪和调试。文章介绍了程序中涉及的变量作用、函数嵌套调用以及使用`backtrace`和`backtrace_symbols`获取调用堆栈的方法,旨在帮助开发者更高效地理解和维护复杂的C语言程序。
808

被折叠的 条评论
为什么被折叠?



