Skip to content

Commit a3418a3

Browse files
committed
tracing: Use TRACE_FLAGS macro to keep enums and strings matched
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>
1 parent ce3fed6 commit a3418a3

2 files changed

Lines changed: 55 additions & 83 deletions

File tree

‎kernel/trace/trace.c‎

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -854,34 +854,18 @@ unsigned long nsecs_to_usecs(unsigned long nsecs)
854854
return nsecs / 1000;
855855
}
856856

857+
/*
858+
* TRACE_FLAGS is defined as a tuple matching bit masks with strings.
859+
* It uses C(a, b) where 'a' is the enum name and 'b' is the string that
860+
* matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list
861+
* of strings in the order that the enums were defined.
862+
*/
863+
#undef C
864+
#define C(a, b) b
865+
857866
/* These must match the bit postions in trace_iterator_flags */
858867
static const char *trace_options[] = {
859-
"print-parent",
860-
"sym-offset",
861-
"sym-addr",
862-
"verbose",
863-
"raw",
864-
"hex",
865-
"bin",
866-
"block",
867-
"stacktrace",
868-
"trace_printk",
869-
"branch",
870-
"annotate",
871-
"userstacktrace",
872-
"sym-userobj",
873-
"printk-msg-only",
874-
"context-info",
875-
"latency-format",
876-
"sleep-time",
877-
"graph-time",
878-
"record-cmd",
879-
"overwrite",
880-
"disable_on_free",
881-
"irq-info",
882-
"markers",
883-
"function-trace",
884-
"display-graph",
868+
TRACE_FLAGS
885869
NULL
886870
};
887871

‎kernel/trace/trace.h‎

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -884,65 +884,53 @@ extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
884884
* positions into trace_flags that controls the output.
885885
*
886886
* NOTE: These bits must match the trace_options array in
887-
* trace.c.
887+
* trace.c (this macro guarantees it).
888888
*/
889-
enum trace_iterator_bits {
890-
TRACE_ITER_PRINT_PARENT_BIT = 0,
891-
TRACE_ITER_SYM_OFFSET_BIT,
892-
TRACE_ITER_SYM_ADDR_BIT,
893-
TRACE_ITER_VERBOSE_BIT,
894-
TRACE_ITER_RAW_BIT,
895-
TRACE_ITER_HEX_BIT,
896-
TRACE_ITER_BIN_BIT,
897-
TRACE_ITER_BLOCK_BIT,
898-
TRACE_ITER_STACKTRACE_BIT,
899-
TRACE_ITER_PRINTK_BIT,
900-
TRACE_ITER_BRANCH_BIT,
901-
TRACE_ITER_ANNOTATE_BIT,
902-
TRACE_ITER_USERSTACKTRACE_BIT,
903-
TRACE_ITER_SYM_USEROBJ_BIT,
904-
TRACE_ITER_PRINTK_MSGONLY_BIT,
905-
TRACE_ITER_CONTEXT_INFO_BIT, /* Print pid/cpu/time */
906-
TRACE_ITER_LATENCY_FMT_BIT,
907-
TRACE_ITER_SLEEP_TIME_BIT,
908-
TRACE_ITER_GRAPH_TIME_BIT,
909-
TRACE_ITER_RECORD_CMD_BIT,
910-
TRACE_ITER_OVERWRITE_BIT,
911-
TRACE_ITER_STOP_ON_FREE_BIT,
912-
TRACE_ITER_IRQ_INFO_BIT,
913-
TRACE_ITER_MARKERS_BIT,
914-
TRACE_ITER_FUNCTION_BIT,
915-
TRACE_ITER_DISPLAY_GRAPH_BIT,
916-
};
889+
#define TRACE_FLAGS \
890+
C(PRINT_PARENT, "print-parent"), \
891+
C(SYM_OFFSET, "sym-offset"), \
892+
C(SYM_ADDR, "sym-addr"), \
893+
C(VERBOSE, "verbose"), \
894+
C(RAW, "raw"), \
895+
C(HEX, "hex"), \
896+
C(BIN, "bin"), \
897+
C(BLOCK, "block"), \
898+
C(STACKTRACE, "stacktrace"), \
899+
C(PRINTK, "trace_printk"), \
900+
C(BRANCH, "branch"), \
901+
C(ANNOTATE, "annotate"), \
902+
C(USERSTACKTRACE, "userstacktrace"), \
903+
C(SYM_USEROBJ, "sym-userobj"), \
904+
C(PRINTK_MSGONLY, "printk-msg-only"), \
905+
C(CONTEXT_INFO, "context-info"), /* Print pid/cpu/time */ \
906+
C(LATENCY_FMT, "latency-format"), \
907+
C(SLEEP_TIME, "sleep-time"), \
908+
C(GRAPH_TIME, "graph-time"), \
909+
C(RECORD_CMD, "record-cmd"), \
910+
C(OVERWRITE, "overwrite"), \
911+
C(STOP_ON_FREE, "disable_on_free"), \
912+
C(IRQ_INFO, "irq-info"), \
913+
C(MARKERS, "markers"), \
914+
C(FUNCTION, "function-trace"), \
915+
C(DISPLAY_GRAPH, "display-graph"),
917916

918-
enum trace_iterator_flags {
919-
TRACE_ITER_PRINT_PARENT = (1 << TRACE_ITER_PRINT_PARENT_BIT),
920-
TRACE_ITER_SYM_OFFSET = (1 << TRACE_ITER_SYM_OFFSET_BIT),
921-
TRACE_ITER_SYM_ADDR = (1 << TRACE_ITER_SYM_ADDR_BIT),
922-
TRACE_ITER_VERBOSE = (1 << TRACE_ITER_VERBOSE_BIT),
923-
TRACE_ITER_RAW = (1 << TRACE_ITER_RAW_BIT),
924-
TRACE_ITER_HEX = (1 << TRACE_ITER_HEX_BIT),
925-
TRACE_ITER_BIN = (1 << TRACE_ITER_BIN_BIT),
926-
TRACE_ITER_BLOCK = (1 << TRACE_ITER_BLOCK_BIT),
927-
TRACE_ITER_STACKTRACE = (1 << TRACE_ITER_STACKTRACE_BIT),
928-
TRACE_ITER_PRINTK = (1 << TRACE_ITER_PRINTK_BIT),
929-
TRACE_ITER_BRANCH = (1 << TRACE_ITER_BRANCH_BIT),
930-
TRACE_ITER_ANNOTATE = (1 << TRACE_ITER_ANNOTATE_BIT),
931-
TRACE_ITER_USERSTACKTRACE = (1 << TRACE_ITER_USERSTACKTRACE_BIT),
932-
TRACE_ITER_SYM_USEROBJ = (1 << TRACE_ITER_SYM_USEROBJ_BIT),
933-
TRACE_ITER_PRINTK_MSGONLY = (1 << TRACE_ITER_PRINTK_MSGONLY_BIT),
934-
TRACE_ITER_CONTEXT_INFO = (1 << TRACE_ITER_CONTEXT_INFO_BIT),
935-
TRACE_ITER_LATENCY_FMT = (1 << TRACE_ITER_LATENCY_FMT_BIT),
936-
TRACE_ITER_SLEEP_TIME = (1 << TRACE_ITER_SLEEP_TIME_BIT),
937-
TRACE_ITER_GRAPH_TIME = (1 << TRACE_ITER_GRAPH_TIME_BIT),
938-
TRACE_ITER_RECORD_CMD = (1 << TRACE_ITER_RECORD_CMD_BIT),
939-
TRACE_ITER_OVERWRITE = (1 << TRACE_ITER_OVERWRITE_BIT),
940-
TRACE_ITER_STOP_ON_FREE = (1 << TRACE_ITER_STOP_ON_FREE_BIT),
941-
TRACE_ITER_IRQ_INFO = (1 << TRACE_ITER_IRQ_INFO_BIT),
942-
TRACE_ITER_MARKERS = (1 << TRACE_ITER_MARKERS_BIT),
943-
TRACE_ITER_FUNCTION = (1 << TRACE_ITER_FUNCTION_BIT),
944-
TRACE_ITER_DISPLAY_GRAPH = (1 << TRACE_ITER_DISPLAY_GRAPH_BIT),
945-
};
917+
/*
918+
* By defining C, we can make TRACE_FLAGS a list of bit names
919+
* that will define the bits for the flag masks.
920+
*/
921+
#undef C
922+
#define C(a, b) TRACE_ITER_##a##_BIT
923+
924+
enum trace_iterator_bits { TRACE_FLAGS };
925+
926+
/*
927+
* By redefining C, we can make TRACE_FLAGS a list of masks that
928+
* use the bits as defined above.
929+
*/
930+
#undef C
931+
#define C(a, b) TRACE_ITER_##a = (1 << TRACE_ITER_##a##_BIT)
932+
933+
enum trace_iterator_flags { TRACE_FLAGS };
946934

947935
/*
948936
* TRACE_ITER_SYM_MASK masks the options in trace_flags that

0 commit comments

Comments
 (0)