【发布时间】:2011-04-28 07:50:35
【问题描述】:
比如有源:
void my_special_debugging_function(const char* function_name, const char* file_name, int line_number);
void func1() {
func3();
func4();
}
void foo() {
func1();
if(qqq) {
func2();
};
func3();
func4();
for(...) {
func5();
}
}
它应该编译为:
void my_special_debugging_function(const char* function_name, const char* file_name, int line_number);
void func1() {
my_special_debugging_function("func1", "prog.c", 3);
func3();
my_special_debugging_function("func1", "prog.c", 4);
func4();
my_special_debugging_function("func1", "prog.c", 5);
}
void foo() {
my_special_debugging_function("foo", "prog.c", 8);
func1();
my_special_debugging_function("foo", "prog.c", 9);
if(qqq) {
my_special_debugging_function("foo", "prog.c", 10);
func2();
my_special_debugging_function("foo", "prog.c", 11);
};
my_special_debugging_function("foo", "prog.c", 12);
func3();
my_special_debugging_function("foo", "prog.c", 13);
func4();
my_special_debugging_function("foo", "prog.c", 14);
for(...) {
my_special_debugging_function("foo", "prog.c", 15);
func5();
my_special_debugging_function("foo", "prog.c", 16);
}
my_special_debugging_function("foo", "prog.c", 17);
}
当然,my_special_debugging_function 应该可以使用backtrace 函数。
有 gcc 的选项吗?或者是否有工具可以在源代码级别执行此操作? (例如,用我的函数生成其他 C 源代码)
@相关 How to "interleave" C/C++ souce with my string (only inside functions at appropriate places)?
【问题讨论】:
-
不完全是这样。不过,您应该注意
__FILE__、__LINE__和__func__宏。 -
@simonhf 它是否在函数内进行检测?它可以将它的东西添加到函数内部的每个源代码行吗?
-
它通过
-finstrument-functions自动检测每个函数的入口和出口点。但是,此外,您可以在函数中手动添加永久检测宏。自动和手动添加的宏都尊重为函数设置的相同的单个详细程度。它与您上面描述的非常相似,除了 func1() 中的所有宏都尊重为 func1() 设置的详细程度,而 foo() 中的所有宏都尊重为 foo() 设置的详细程度。
标签: c gcc profiling instrumentation