内核调试常会打印 文件名 函数名 行号

本文介绍了一个简单的打印函数,该函数能够输出当前文件名、函数名及行号,方便开发者进行调试和定位问题。
部署运行你感兴趣的模型镜像
printk("%s %s %d \n",__FILE__,__FUNCTION__,__LINE__);

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.9

TensorFlow-v2.9

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

在不同的编程语言中,可以通过特定的预定义宏或内置函数来打印当前文件名函数名行号。以下是几种常见编程语言中的实现方式: ### C/C++ 在 C/C++ 中,可以使用 `__FILE__`、`__LINE__` 和 `__FUNCTION__` 这些预定义宏来获取文件名行号函数名。例如,可以定义一个调试打印宏来输出这些信息: ```cpp #include <cstdio> #define DEBUG_LOG(fmt, ...) \ printf("[File: %s, Line: %d, Function: %s] " fmt "\n", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__) ``` 这样可以在调试时输出类似 `[File: main.cpp, Line: 10, Function: main] Debug message` 的信息[^2]。 ### Python 在 Python 中,可以通过 `inspect` 模块获取当前的堆栈信息,从而获取文件名函数名行号。例如: ```python import inspect def debug_info(): frame = inspect.currentframe() info = inspect.getframeinfo(frame.f_back) print(f"File: {info.filename}, Function: {info.function}, Line: {info.lineno}") def test_function(): debug_info() test_function() ``` 运行后会输出类似 `File: test.py, Function: test_function, Line: 8` 的信息[^3]。 ### Java 在 Java 中,可以通过 `Throwable` 类的 `getStackTrace()` 方法来获取堆栈跟踪信息。例如: ```java public class DebugInfo { public static void printDebugInfo() { StackTraceElement[] stackTrace = new Throwable().getStackTrace(); for (StackTraceElement element : stackTrace) { System.out.println("Class: " + element.getClassName() + ", Method: " + element.getMethodName() + ", File: " + element.getFileName() + ", Line: " + element.getLineNumber()); } } public static void main(String[] args) { printDebugInfo(); } } ``` 这段代码会输出完整的堆栈信息,包括类、方法文件名行号。 ### Go 在 Go 中,可以通过 `runtime.Caller()` 函数来获取调用者的文件名函数名行号。例如: ```go package main import ( "fmt" "runtime" "strings" ) func getDebugInfo() (string, string, int) { pc, file, line, ok := runtime.Caller(1) if !ok { return "", "", 0 } fn := runtime.FuncForPC(pc) return file, fn.Name(), line } func main() { file, function, line := getDebugInfo() fmt.Printf("File: %s, Function: %s, Line: %d\n", file, function, line) } ``` 运行后会输出类似 `File: /path/to/file.go, Function: main.main, Line: 15` 的信息[^4]。 ### Qt (C++) 在 Qt 中,可以使用 `qDebug()` 并结合 `__FILE__`、`__LINE__` 和 `__FUNCTION__` 来打印调试信息: ```cpp #include <QDebug> #define MyDebug qDebug() << "[File: " << __FILE__ << ", Line: " << __LINE__ << ", Function: " << __FUNCTION__ << "]" void testFunction() { MyDebug << "This is a debug message."; } int main() { testFunction(); return 0; } ``` 输出结果会类似于 `[File: main.cpp, Line: 5, Function: testFunction] This is a debug message.`[^5]。 ### JavaScript (Node.js) 在 Node.js 中,可以通过 `Error` 对象获取堆栈信息并提取文件名函数名行号: ```javascript function getDebugInfo() { const err = new Error(); const stack = err.stack.split('\n')[2]; const match = stack.match(/at (?:(.+?)\s+\()?(?:(.+?):(\d+):\d+)/); return { function: match[1] || 'unknown', file: match[2], line: match[3] }; } function testFunction() { const info = getDebugInfo(); console.log(`File: ${info.file}, Function: ${info.function}, Line: ${info.line}`); } testFunction(); ``` 运行后会输出类似 `File: /path/to/file.js, Function: testFunction, Line: 10` 的信息。 ### C# 在 C# 中,可以使用 `System.Diagnostics.StackTrace` 来获取调用者的文件名函数名行号: ```csharp using System; using System.Diagnostics; class Program { static void PrintDebugInfo() { StackTrace stackTrace = new StackTrace(); StackFrame frame = stackTrace.GetFrame(1); MethodBase method = frame.GetMethod(); Console.WriteLine($"Class: {method.DeclaringType}, Method: {method.Name}, File: {frame.GetFileName()}, Line: {frame.GetFileLineNumber()}"); } static void Main(string[] args) { PrintDebugInfo(); } } ``` 输出结果会包括类、方法文件名行号
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值