From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Ingo Molnar <mingo@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>
Subject: [for-next][PATCH 12/25] tracing: Use TRACE_FLAGS macro to keep enums and strings matched
Date: Thu, 01 Oct 2015 07:55:36 -0400 [thread overview]
Message-ID: <20151001115637.990566670@goodmis.org> (raw)
In-Reply-To: <20151001115524.066462793@goodmis.org>
[-- Attachment #1: 0012-tracing-Use-TRACE_FLAGS-macro-to-keep-enums-and-stri.patch --]
[-- Type: text/plain, Size: 7169 bytes --]
From: "Steven Rostedt (Red Hat)" <rostedt@goodmis.org>
Use a cute little macro trick to keep the names of the trace flags file
guaranteed to match the corresponding masks.
The macro TRACE_FLAGS is defined as a serious of enum names followed by
the string name of the file that matches it. For example:
#define TRACE_FLAGS \
C(PRINT_PARENT, "print-parent"), \
C(SYM_OFFSET, "sym-offset"), \
C(SYM_ADDR, "sym-addr"), \
C(VERBOSE, "verbose"),
Now we can define the following:
#undef C
#define C(a, b) TRACE_ITER_##a##_BIT
enum trace_iterator_bits { TRACE_FLAGS };
The above creates:
enum trace_iterator_bits {
TRACE_ITER_PRINT_PARENT_BIT,
TRACE_ITER_SYM_OFFSET_BIT,
TRACE_ITER_SYM_ADDR_BIT,
TRACE_ITER_VERBOSE_BIT,
};
Then we can redefine C as:
#undef C
#define C(a, b) TRACE_ITER_##a = (1 << TRACE_ITER_##a##_BIT)
enum trace_iterator_flags { TRACE_FLAGS };
Which creates:
enum trace_iterator_flags {
TRACE_ITER_PRINT_PARENT = (1 << TRACE_ITER_PRINT_PARENT_BIT),
TRACE_ITER_SYM_OFFSET = (1 << TRACE_ITER_SYM_OFFSET_BIT),
TRACE_ITER_SYM_ADDR = (1 << TRACE_ITER_SYM_ADDR_BIT),
TRACE_ITER_VERBOSE = (1 << TRACE_ITER_VERBOSE_BIT),
};
Then finally we can create the list of file names:
#undef C
#define C(a, b) b
static const char *trace_options[] = {
TRACE_FLAGS
NULL
};
Which creates:
static const char *trace_options[] = {
"print-parent",
"sym-offset",
"sym-addr",
"verbose",
NULL
};
The importance of this is that the strings match the bit index.
trace_options[TRACE_ITER_SYM_ADDR_BIT] == "sym-addr"
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
---
kernel/trace/trace.c | 36 +++++-------------
kernel/trace/trace.h | 102 +++++++++++++++++++++++----------------------------
2 files changed, 55 insertions(+), 83 deletions(-)
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index f2fbf610d20e..e80e380d0238 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -854,34 +854,18 @@ unsigned long nsecs_to_usecs(unsigned long nsecs)
return nsecs / 1000;
}
+/*
+ * TRACE_FLAGS is defined as a tuple matching bit masks with strings.
+ * It uses C(a, b) where 'a' is the enum name and 'b' is the string that
+ * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list
+ * of strings in the order that the enums were defined.
+ */
+#undef C
+#define C(a, b) b
+
/* These must match the bit postions in trace_iterator_flags */
static const char *trace_options[] = {
- "print-parent",
- "sym-offset",
- "sym-addr",
- "verbose",
- "raw",
- "hex",
- "bin",
- "block",
- "stacktrace",
- "trace_printk",
- "branch",
- "annotate",
- "userstacktrace",
- "sym-userobj",
- "printk-msg-only",
- "context-info",
- "latency-format",
- "sleep-time",
- "graph-time",
- "record-cmd",
- "overwrite",
- "disable_on_free",
- "irq-info",
- "markers",
- "function-trace",
- "display-graph",
+ TRACE_FLAGS
NULL
};
diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h
index 31d8395c8dc5..d164845edddd 100644
--- a/kernel/trace/trace.h
+++ b/kernel/trace/trace.h
@@ -884,65 +884,53 @@ extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
* positions into trace_flags that controls the output.
*
* NOTE: These bits must match the trace_options array in
- * trace.c.
+ * trace.c (this macro guarantees it).
*/
-enum trace_iterator_bits {
- TRACE_ITER_PRINT_PARENT_BIT = 0,
- TRACE_ITER_SYM_OFFSET_BIT,
- TRACE_ITER_SYM_ADDR_BIT,
- TRACE_ITER_VERBOSE_BIT,
- TRACE_ITER_RAW_BIT,
- TRACE_ITER_HEX_BIT,
- TRACE_ITER_BIN_BIT,
- TRACE_ITER_BLOCK_BIT,
- TRACE_ITER_STACKTRACE_BIT,
- TRACE_ITER_PRINTK_BIT,
- TRACE_ITER_BRANCH_BIT,
- TRACE_ITER_ANNOTATE_BIT,
- TRACE_ITER_USERSTACKTRACE_BIT,
- TRACE_ITER_SYM_USEROBJ_BIT,
- TRACE_ITER_PRINTK_MSGONLY_BIT,
- TRACE_ITER_CONTEXT_INFO_BIT, /* Print pid/cpu/time */
- TRACE_ITER_LATENCY_FMT_BIT,
- TRACE_ITER_SLEEP_TIME_BIT,
- TRACE_ITER_GRAPH_TIME_BIT,
- TRACE_ITER_RECORD_CMD_BIT,
- TRACE_ITER_OVERWRITE_BIT,
- TRACE_ITER_STOP_ON_FREE_BIT,
- TRACE_ITER_IRQ_INFO_BIT,
- TRACE_ITER_MARKERS_BIT,
- TRACE_ITER_FUNCTION_BIT,
- TRACE_ITER_DISPLAY_GRAPH_BIT,
-};
+#define TRACE_FLAGS \
+ C(PRINT_PARENT, "print-parent"), \
+ C(SYM_OFFSET, "sym-offset"), \
+ C(SYM_ADDR, "sym-addr"), \
+ C(VERBOSE, "verbose"), \
+ C(RAW, "raw"), \
+ C(HEX, "hex"), \
+ C(BIN, "bin"), \
+ C(BLOCK, "block"), \
+ C(STACKTRACE, "stacktrace"), \
+ C(PRINTK, "trace_printk"), \
+ C(BRANCH, "branch"), \
+ C(ANNOTATE, "annotate"), \
+ C(USERSTACKTRACE, "userstacktrace"), \
+ C(SYM_USEROBJ, "sym-userobj"), \
+ C(PRINTK_MSGONLY, "printk-msg-only"), \
+ C(CONTEXT_INFO, "context-info"), /* Print pid/cpu/time */ \
+ C(LATENCY_FMT, "latency-format"), \
+ C(SLEEP_TIME, "sleep-time"), \
+ C(GRAPH_TIME, "graph-time"), \
+ C(RECORD_CMD, "record-cmd"), \
+ C(OVERWRITE, "overwrite"), \
+ C(STOP_ON_FREE, "disable_on_free"), \
+ C(IRQ_INFO, "irq-info"), \
+ C(MARKERS, "markers"), \
+ C(FUNCTION, "function-trace"), \
+ C(DISPLAY_GRAPH, "display-graph"),
-enum trace_iterator_flags {
- TRACE_ITER_PRINT_PARENT = (1 << TRACE_ITER_PRINT_PARENT_BIT),
- TRACE_ITER_SYM_OFFSET = (1 << TRACE_ITER_SYM_OFFSET_BIT),
- TRACE_ITER_SYM_ADDR = (1 << TRACE_ITER_SYM_ADDR_BIT),
- TRACE_ITER_VERBOSE = (1 << TRACE_ITER_VERBOSE_BIT),
- TRACE_ITER_RAW = (1 << TRACE_ITER_RAW_BIT),
- TRACE_ITER_HEX = (1 << TRACE_ITER_HEX_BIT),
- TRACE_ITER_BIN = (1 << TRACE_ITER_BIN_BIT),
- TRACE_ITER_BLOCK = (1 << TRACE_ITER_BLOCK_BIT),
- TRACE_ITER_STACKTRACE = (1 << TRACE_ITER_STACKTRACE_BIT),
- TRACE_ITER_PRINTK = (1 << TRACE_ITER_PRINTK_BIT),
- TRACE_ITER_BRANCH = (1 << TRACE_ITER_BRANCH_BIT),
- TRACE_ITER_ANNOTATE = (1 << TRACE_ITER_ANNOTATE_BIT),
- TRACE_ITER_USERSTACKTRACE = (1 << TRACE_ITER_USERSTACKTRACE_BIT),
- TRACE_ITER_SYM_USEROBJ = (1 << TRACE_ITER_SYM_USEROBJ_BIT),
- TRACE_ITER_PRINTK_MSGONLY = (1 << TRACE_ITER_PRINTK_MSGONLY_BIT),
- TRACE_ITER_CONTEXT_INFO = (1 << TRACE_ITER_CONTEXT_INFO_BIT),
- TRACE_ITER_LATENCY_FMT = (1 << TRACE_ITER_LATENCY_FMT_BIT),
- TRACE_ITER_SLEEP_TIME = (1 << TRACE_ITER_SLEEP_TIME_BIT),
- TRACE_ITER_GRAPH_TIME = (1 << TRACE_ITER_GRAPH_TIME_BIT),
- TRACE_ITER_RECORD_CMD = (1 << TRACE_ITER_RECORD_CMD_BIT),
- TRACE_ITER_OVERWRITE = (1 << TRACE_ITER_OVERWRITE_BIT),
- TRACE_ITER_STOP_ON_FREE = (1 << TRACE_ITER_STOP_ON_FREE_BIT),
- TRACE_ITER_IRQ_INFO = (1 << TRACE_ITER_IRQ_INFO_BIT),
- TRACE_ITER_MARKERS = (1 << TRACE_ITER_MARKERS_BIT),
- TRACE_ITER_FUNCTION = (1 << TRACE_ITER_FUNCTION_BIT),
- TRACE_ITER_DISPLAY_GRAPH = (1 << TRACE_ITER_DISPLAY_GRAPH_BIT),
-};
+/*
+ * By defining C, we can make TRACE_FLAGS a list of bit names
+ * that will define the bits for the flag masks.
+ */
+#undef C
+#define C(a, b) TRACE_ITER_##a##_BIT
+
+enum trace_iterator_bits { TRACE_FLAGS };
+
+/*
+ * By redefining C, we can make TRACE_FLAGS a list of masks that
+ * use the bits as defined above.
+ */
+#undef C
+#define C(a, b) TRACE_ITER_##a = (1 << TRACE_ITER_##a##_BIT)
+
+enum trace_iterator_flags { TRACE_FLAGS };
/*
* TRACE_ITER_SYM_MASK masks the options in trace_flags that
--
2.5.1
next prev parent reply other threads:[~2015-10-01 11:59 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-01 11:55 [for-next][PATCH 00/25] tracing: Have instances have their own options + clean ups Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 01/25] kernel/trace_probe: is_good_name can be boolean Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 02/25] tracing: Move non perf code out of perf.h Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 03/25] tracing: Remove ftrace_trace_stack_regs() Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 04/25] tracing: Remove unused function trace_current_buffer_lock_reserve() Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 05/25] tracing: Pass trace_array into trace_buffer_unlock_commit() Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 06/25] tracing: Make ftrace_trace_stack() static Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 07/25] tracing: Inject seq_print_userip_objs() into its only user Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 08/25] tracing: Turn seq_print_user_ip() into a static function Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 09/25] tracing: Move "display-graph" option to main options Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 10/25] tracing: Remove unused tracing option "ftrace_preempt" Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 11/25] tracing: Use enums instead of hard coded bitmasks for TRACE_ITER flags Steven Rostedt
2015-10-01 11:55 ` Steven Rostedt [this message]
2015-10-01 11:55 ` [for-next][PATCH 13/25] tracing: Only create function graph options when it is compiled in Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 14/25] tracing: Only create branch tracer options when " Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 15/25] tracing: Do not create function tracer options when not " Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 16/25] tracing: Only create stacktrace option when STACKTRACE is configured Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 17/25] tracing: Always show all tracer options in the options directory Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 18/25] tracing: Add build bug if we have more trace_flags than bits Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 19/25] tracing: Remove access to trace_flags in trace_printk.c Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 20/25] tracing: Move sleep-time and graph-time options out of the core trace_flags Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 21/25] tracing: Move trace_flags from global to a trace_array field Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 22/25] tracing: Add a method to pass in trace_array descriptor to option files Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 23/25] tracing: Make ftrace_trace_stack() depend on general trace_array flag Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 24/25] tracing: Add trace options for core options to instances Steven Rostedt
2015-10-01 11:55 ` [for-next][PATCH 25/25] tracing: Add trace options for tracer " Steven Rostedt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20151001115637.990566670@goodmis.org \
--to=rostedt@goodmis.org \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome