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

perf tools: Asprintf like functions to format integer filter expression

char *asprintf_expr_in_ints(const char *var, size_t nints, int *ints);
char *asprintf_expr_not_in_ints(const char *var, size_t nints, int *ints);

Example of output formatted with those functions:

# ./tp_filter 6 12 2015
asprintf_expr_in_ints: id == 6 || id == 12 || id == 2015
asprintf_expr_not_in_ints: id != 6 && id != 12 && id != 2015
#

It'll be used with, for instance, perf_evsel__set_filter_in_ints(), that
will be used in turn to ask the kernel to filter out all raw_syscalls:*
except for the ones specified by the user via:

$ perf trace -e some,list,of,syscalls

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: David Ahern <dsahern@gmail.com>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-jt07vfp6bd8y50c05j1t7hrn@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

+51
+39
tools/perf/util/string.c
··· 357 357 358 358 return p; 359 359 } 360 + 361 + char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints) 362 + { 363 + /* 364 + * FIXME: replace this with an expression using log10() when we 365 + * find a suitable implementation, maybe the one in the dvb drivers... 366 + * 367 + * "%s == %d || " = log10(MAXINT) * 2 + 8 chars for the operators 368 + */ 369 + size_t size = nints * 28 + 1; /* \0 */ 370 + size_t i, printed = 0; 371 + char *expr = malloc(size); 372 + 373 + if (expr) { 374 + const char *or_and = "||", *eq_neq = "=="; 375 + char *e = expr; 376 + 377 + if (!in) { 378 + or_and = "&&"; 379 + eq_neq = "!="; 380 + } 381 + 382 + for (i = 0; i < nints; ++i) { 383 + if (printed == size) 384 + goto out_err_overflow; 385 + 386 + if (i > 0) 387 + printed += snprintf(e + printed, size - printed, " %s ", or_and); 388 + printed += scnprintf(e + printed, size - printed, 389 + "%s %s %d", var, eq_neq, ints[i]); 390 + } 391 + } 392 + 393 + return expr; 394 + 395 + out_err_overflow: 396 + free(expr); 397 + return NULL; 398 + }
+12
tools/perf/util/util.h
··· 339 339 int lzma_decompress_to_file(const char *input, int output_fd); 340 340 #endif 341 341 342 + char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints); 343 + 344 + static inline char *asprintf_expr_in_ints(const char *var, size_t nints, int *ints) 345 + { 346 + return asprintf_expr_inout_ints(var, true, nints, ints); 347 + } 348 + 349 + static inline char *asprintf_expr_not_in_ints(const char *var, size_t nints, int *ints) 350 + { 351 + return asprintf_expr_inout_ints(var, false, nints, ints); 352 + } 353 + 342 354 #endif /* GIT_COMPAT_UTIL_H */