本文将介绍__func__
和__LINE__
和格式化打印%p
、%pf
、%pF
的用法。
func和LINE
在调试过程中我们经常要加入如下的打印语句:
__func__
可以打印出当前printk
语句所在的函数名称;__LINE__
可以打印出当前printk
语句所在的行号。
关于__func__
的解释,具体请看:https://gcc.gnu.org/onlinedocs/gcc/Function-Names.html
GCC provides three magic constants that hold the name of the current function as a string. In C++11 and later modes, all three are treated as constant expressions and can be used in constexpr constexts. The first of these constants is
__func__
, which is part of the C99 standard:
The identifier__func__
is implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration
appeared, where function-name is the name of the lexically-enclosing function. This name is the unadorned name of the function. As an extension, at file (or, in C++, namespace scope),__func__
evaluates to the empty string.static const char __func__[] = "function-name";
__FUNCTION__
is another name for__func__
, provided for backward compatibility with old versions of GCC.
另外,__func__
是一个变量,而不是预处理的宏。
These identifiers are variables, not preprocessor macros, and may not be used to initialize char arrays or be concatenated with string literals.
关于__LINE__
的解释,可以参照:https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html
This macro expands to the current input line number, in the form of a decimal integer constant. While we call it a predefined macro, it’s a pretty strange macro, since its “definition” changes with each new line of source code.
当然,想要弄清楚__func__
和__LINE__
是怎么实现的要费很大的力气,这里面涉及到编译原理。等有机会再学习学习。