宏中未使用的参数会发生什么?

时间:2017-05-01 19:08:29

标签: c kernel tracepoint

这个问题来自于阅读内核,更具体地说是跟踪宏。当我研究内核模块如何执行二进制,elf和脚本文件(fs / exec.c)时,我得到了它们。

出于某种原因,我不记得哪个是tracepoint.h文件,其中定义了宏TRACE_EVENT等。我使用trace_event作为示例,因为内核中的跟踪示例使用此宏。该示例使用了宏

    TRACE_EVENT(foo_bar,

    TP_PROTO(const char *foo, int bar, const int *lst,
         const char *string, const struct cpumask *mask),

    TP_ARGS(foo, bar, lst, string, mask),

    TP_STRUCT__entry(
        __array(    char,   foo,    10      )
        __field(    int,    bar         )
        __dynamic_array(int,    list,   __length_of(lst))
        __string(   str,    string          )
        __bitmask(  cpus,   num_possible_cpus() )
    ),

    TP_fast_assign(
        strlcpy(__entry->foo, foo, 10);
        __entry->bar    = bar;
        memcpy(__get_dynamic_array(list), lst,
               __length_of(lst) * sizeof(int));
        __assign_str(str, string);
        __assign_bitmask(cpus, cpumask_bits(mask), num_possible_cpus());
    ),

    TP_printk("foo %s %d %s %s %s %s (%s)", __entry->foo, __entry->bar,
/*
 * Notice here the use of some helper functions. This includes:
 *
 *  __print_symbolic( variable, { value, "string" }, ... ),
 *
 *    The variable is tested against each value of the { } pair. If
 *    the variable matches one of the values, then it will print the
 *    string in that pair. If non are matched, it returns a string
 *    version of the number (if __entry->bar == 7 then "7" is returned).
 */
          __print_symbolic(__entry->bar,
                   { 0, "zero" },
                   { TRACE_SAMPLE_FOO, "TWO" },
                   { TRACE_SAMPLE_BAR, "FOUR" },
                   { TRACE_SAMPLE_ZOO, "EIGHT" },
                   { 10, "TEN" }
              ),

/*
 *  __print_flags( variable, "delim", { value, "flag" }, ... ),
 *
 *    This is similar to __print_symbolic, except that it tests the bits
 *    of the value. If ((FLAG & variable) == FLAG) then the string is
 *    printed. If more than one flag matches, then each one that does is
 *    also printed with delim in between them.
 *    If not all bits are accounted for, then the not found bits will be
 *    added in hex format: 0x506 will show BIT2|BIT4|0x500
 */
          __print_flags(__entry->bar, "|",
                { 1, "BIT1" },
                { 2, "BIT2" },
                { 4, "BIT3" },
                { 8, "BIT4" }
              ),
/*
 *  __print_array( array, len, element_size )
 *
 *    This prints out the array that is defined by __array in a nice format.
 */
          __print_array(__get_dynamic_array(list),
                __get_dynamic_array_len(list) / sizeof(int),
                sizeof(int)),
          __get_str(str), __get_bitmask(cpus))
);

所以,自然,在此之后我去了TRACE_EVENT的定义并找到了这个

#define TRACE_EVENT(name, proto, args, struct, assign, print) \ DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))

如您所见,trace_event宏不使用print参数(或参数?),以及struct和assign参数。

非常清楚地表明宏需要它们,但在它下面它只是调用不需要它们的declare_trace宏。

至于宏扩展的其余部分,我对它没问题,没有任何意外,但宏的特殊用法让我感到困惑。所有其他领域是出于某种目的还是只是......在没有任何理由存在的情况下呢?

1 个答案:

答案 0 :(得分:1)

正如@pvg指出的那样,linux/tracepoint.h只是跟踪点的冰山一角。此标题仅声明适当的函数和类型。这就是为什么它不处理TRACE_EVENT的某些args。

但是带有跟踪定义的标头处理了两次(或甚至更多次),并且下次处理了TRACE_EVENT的所有参数。

有关Linux内核中跟踪点的更多信息,请参阅Documentation/trace/tracepoints.txt