Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tracing: Add example and documentation for new __vstring() macro

Update the sample trace events to include an example that uses the new
__vstring() helpers for TRACE_EVENTS.

Link: https://lkml.kernel.org/r/20220715175555.16375a3b@gandalf.local.home

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

+40 -6
+12 -2
samples/trace_events/trace-events-sample.c
··· 19 19 "One ring to rule them all" 20 20 }; 21 21 22 - static void simple_thread_func(int cnt) 22 + static void do_simple_thread_func(int cnt, const char *fmt, ...) 23 23 { 24 24 unsigned long bitmask[1] = {0xdeadbeefUL}; 25 + va_list va; 25 26 int array[6]; 26 27 int len = cnt % 5; 27 28 int i; ··· 34 33 array[i] = i + 1; 35 34 array[i] = 0; 36 35 36 + va_start(va, fmt); 37 + 37 38 /* Silly tracepoints */ 38 39 trace_foo_bar("hello", cnt, array, random_strings[len], 39 - current->cpus_ptr); 40 + current->cpus_ptr, fmt, &va); 41 + 42 + va_end(va); 40 43 41 44 trace_foo_with_template_simple("HELLO", cnt); 42 45 ··· 51 46 trace_foo_with_template_print("I have to be different", cnt); 52 47 53 48 trace_foo_rel_loc("Hello __rel_loc", cnt, bitmask); 49 + } 50 + 51 + static void simple_thread_func(int cnt) 52 + { 53 + do_simple_thread_func(cnt, "iter=%d", cnt); 54 54 } 55 55 56 56 static int simple_thread(void *arg)
+28 -4
samples/trace_events/trace-events-sample.h
··· 141 141 * In most cases, the __assign_str() macro will take the same 142 142 * parameters as the __string() macro had to declare the string. 143 143 * 144 + * __vstring: This is similar to __string() but instead of taking a 145 + * dynamic length, it takes a variable list va_list 'va' variable. 146 + * Some event callers already have a message from parameters saved 147 + * in a va_list. Passing in the format and the va_list variable 148 + * will save just enough on the ring buffer for that string. 149 + * Note, the va variable used is a pointer to a va_list, not 150 + * to the va_list directly. 151 + * 152 + * (va_list *va) 153 + * 154 + * __vstring(foo, fmt, va) is similar to: vsnprintf(foo, fmt, va) 155 + * 156 + * To assign the string, use the helper macro __assign_vstr(). 157 + * 158 + * __assign_vstr(foo, fmt, va); 159 + * 160 + * In most cases, the __assign_vstr() macro will take the same 161 + * parameters as the __vstring() macro had to declare the string. 162 + * Use __get_str() to retrieve the __vstring() just like it would for 163 + * __string(). 164 + * 144 165 * __string_len: This is a helper to a __dynamic_array, but it understands 145 166 * that the array has characters in it, and with the combined 146 167 * use of __assign_str_len(), it will allocate 'len' + 1 bytes ··· 277 256 TRACE_EVENT(foo_bar, 278 257 279 258 TP_PROTO(const char *foo, int bar, const int *lst, 280 - const char *string, const struct cpumask *mask), 259 + const char *string, const struct cpumask *mask, 260 + const char *fmt, va_list *va), 281 261 282 - TP_ARGS(foo, bar, lst, string, mask), 262 + TP_ARGS(foo, bar, lst, string, mask, fmt, va), 283 263 284 264 TP_STRUCT__entry( 285 265 __array( char, foo, 10 ) ··· 288 266 __dynamic_array(int, list, __length_of(lst)) 289 267 __string( str, string ) 290 268 __bitmask( cpus, num_possible_cpus() ) 269 + __vstring( vstr, fmt, va ) 291 270 ), 292 271 293 272 TP_fast_assign( ··· 297 274 memcpy(__get_dynamic_array(list), lst, 298 275 __length_of(lst) * sizeof(int)); 299 276 __assign_str(str, string); 277 + __assign_vstr(vstr, fmt, va); 300 278 __assign_bitmask(cpus, cpumask_bits(mask), num_possible_cpus()); 301 279 ), 302 280 303 - TP_printk("foo %s %d %s %s %s %s (%s)", __entry->foo, __entry->bar, 281 + TP_printk("foo %s %d %s %s %s %s (%s) %s", __entry->foo, __entry->bar, 304 282 305 283 /* 306 284 * Notice here the use of some helper functions. This includes: ··· 345 321 __print_array(__get_dynamic_array(list), 346 322 __get_dynamic_array_len(list) / sizeof(int), 347 323 sizeof(int)), 348 - __get_str(str), __get_bitmask(cpus)) 324 + __get_str(str), __get_bitmask(cpus), __get_str(vstr)) 349 325 ); 350 326 351 327 /*