printk()用法

本文将介绍__func____LINE__和格式化打印%p%pf%pF的用法。


funcLINE

在调试过程中我们经常要加入如下的打印语句:

  • __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__是怎么实现的要费很大的力气,这里面涉及到编译原理。等有机会再学习学习。

Title:printk()用法

Author:Victor Huang

Time:2019-03-17 / 16:03

Link:http://wowothink.com/42fd7a7f/

License: Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)