ktr - add KTR_COND_LOG
authorMihai Carabas <mihai.carabas@gmail.com>
Wed, 22 Aug 2012 13:07:18 +0000 (13:07 +0000)
committerAlex Hornung <alex@alexhornung.com>
Wed, 22 Aug 2012 15:06:55 +0000 (15:06 +0000)
 * 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
sys/sys/ktr.h

index 9c72cdf..c15f672 100644 (file)
 .\"
 .\" $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 ,
index 8becde3..7b32bee 100644 (file)
@@ -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 */