From b0bbd261455dd9cdb5575610d5fe8a3975289ed8 Mon Sep 17 00:00:00 2001 From: Mihai Carabas Date: Wed, 22 Aug 2012 13:07:18 +0000 Subject: [PATCH] ktr - add KTR_COND_LOG * KTR_COND_LOG provides conditional logging; the second parameter passed in is a condition which determines whether the entry is logged or not. * It provides a neater way to log conditionally than having to wrap a KTR in an if, which may then end up being empty if KTR is disbled. --- share/man/man9/ktr.9 | 22 ++++++++++++++++++++-- sys/sys/ktr.h | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/share/man/man9/ktr.9 b/share/man/man9/ktr.9 index 9c72cdf51d..c15f672029 100644 --- a/share/man/man9/ktr.9 +++ b/share/man/man9/ktr.9 @@ -24,14 +24,15 @@ .\" .\" $FreeBSD: src/share/man/man9/ktr.9,v 1.8 2005/03/08 01:37:36 hmp Exp $ .\" -.Dd January 2, 2012 +.Dd August 22, 2012 .Dt KTR 9 .Os .Sh NAME .Nm KTR_INFO_MASTER , .Nm KTR_INFO_MASTER_EXTERN , .Nm KTR_INFO , -.Nm KTR_LOG +.Nm KTR_LOG , +.Nm KTR_COND_LOG .Nd kernel tracing facility .Sh SYNOPSIS .In sys/ktr.h @@ -42,6 +43,7 @@ .Fn KTR_INFO_MASTER_EXTERN "master" .Fn KTR_INFO "compile" "master" "name" "maskbit" "format" "type name" "..." .Fn KTR_LOG "info" "arg ..." +.Fn KTR_COND_LOG "info" "cond" "arg ..." .Sh DESCRIPTION The .Nm ktr @@ -104,6 +106,14 @@ string passed to the associated call. .Pp The +.Fn KTR_COND_LOG +macro is equivalent to +.Fn KTR_LOG +except it logs only when the condition specified in +.Fa cond +evaluates to true. +.Pp +The .Va ktr_entries variable contains the number of entries in the .Va ktr_buf @@ -129,6 +139,7 @@ This example demonstrates a simple usage of the KTR facility: KTR_INFO_MASTER(foo); KTR_INFO(KTR_FOO, foo, func1, 0, "func1()"); KTR_INFO(KTR_FOO, foo, func2, 1, "func2(%d)", int arg); +KTR_INFO(KTR_FOO, foo, func3, 2, "func3: arg positive: %d", int arg); \&... @@ -145,6 +156,13 @@ func2(int arg) KTR_LOG(foo_func2, arg); ... } + +void +func3(int arg) +{ + KTR_COND_LOG(foo_func3, arg >= 0, arg); + ... +} .Ed .Sh SEE ALSO .Xr gdb 1 , diff --git a/sys/sys/ktr.h b/sys/sys/ktr.h index 8becde37ef..7b32bee932 100644 --- a/sys/sys/ktr.h +++ b/sys/sys/ktr.h @@ -197,6 +197,25 @@ SYSCTL_DECL(_debug_ktr); } \ } while(0) +#define KTR_COND_LOG(name, cond, ...) \ + do { \ + __ktr_info_ ## name ## _fmtcheck (__ktr_ ## name ## _fmt, ##__VA_ARGS__); \ + if ((cond) && \ + ktr_ ## name ## _enable && \ + (ktr_ ## name ## _mask & *ktr_info_ ## name .kf_master_enable)) { \ + struct ktr_entry *entry; \ + entry = ktr_begin_write_entry(&ktr_info_ ## name, __FILE__, __LINE__); \ + if (!entry) \ + break; \ + *(struct ktr_info_ ## name ## _args *)&entry->ktr_data[0] = \ + (struct ktr_info_ ## name ## _args){ __VA_ARGS__}; \ + if (ktr_finish_write_entry(&ktr_info_ ## name, entry)) { \ + kprintf(ktr_info_ ## name .kf_format, ##__VA_ARGS__); \ + kprintf("\n"); \ + } \ + } \ + } while(0) + #else #define KTR_INFO_MASTER(master) \ @@ -211,6 +230,8 @@ SYSCTL_DECL(_debug_ktr); #define KTR_LOG(info, args...) +#define KTR_COND_LOG(info, args...) + #endif #endif /* !LOCORE */ -- 2.41.0