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

Configure Feed

Select the types of activity you want to include in your feed.

perf llvm: Allow dump llvm output object file using llvm.dump-obj

Add a 'llvm.dump-obj' config option to enable perf dump BPF object files
compiled by LLVM.

This option is useful when using BPF objects in embedded platforms.
LLVM compiler won't be deployed in these platforms, and currently we
don't support dynamic compiling library.

Before this patch users have to explicitly issue llvm commands to
compile BPF scripts, and can't use helpers (like include path detection
and default macros) in perf. With this option, user is allowed to use
perf to compile their BPF objects then copy them into their embedded
platforms.

Committer notice:

Testing it:

# cat ~/.perfconfig
[llvm]
dump-obj = true
#
# ls -la filter.o
ls: cannot access filter.o: No such file or directory
# cat filter.c
#include <uapi/linux/bpf.h>
#define SEC(NAME) __attribute__((section(NAME), used))

SEC("func=hrtimer_nanosleep rqtp->tv_nsec")
int func(void *ctx, int err, long nsec)
{
return nsec > 1000;
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;
# trace -e nanosleep --event filter.c usleep 6
LLVM: dumping filter.o
0.007 ( 0.007 ms): usleep/13976 nanosleep(rqtp: 0x7ffc5847f640 ) ...
0.007 ( ): perf_bpf_probe:func:(ffffffff811137d0) tv_nsec=6000)
0.070 ( 0.070 ms): usleep/13976 ... [continued]: nanosleep()) = 0
# ls -la filter.o
-rw-r--r--. 1 root root 776 Jun 20 17:01 filter.o
# readelf -SW filter.o
There are 7 section headers, starting at offset 0x148:

Section Headers:
[Nr] Name Type Address Off Size ES Flg Lk Inf Al
[ 0] NULL 0000000000000000 000000 000000 00 0 0 0
[ 1] .strtab STRTAB 0000000000000000 0000e8 00005a 00 0 0 1
[ 2] .text PROGBITS 0000000000000000 000040 000000 00 AX 0 0 4
[ 3] func=hrtimer_nanosleep rqtp->tv_nsec PROGBITS 0000000000000000 000040 000028 00 AX 0 0 8
[ 4] license PROGBITS 0000000000000000 000068 000004 00 WA 0 0 1
[ 5] version PROGBITS 0000000000000000 00006c 000004 00 WA 0 0 4
[ 6] .symtab SYMTAB 0000000000000000 000070 000078 18 1 2 8
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
#

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1466064161-48553-2-git-send-email-wangnan0@huawei.com
[ s/dumpping/dumping/g ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Wang Nan and committed by
Arnaldo Carvalho de Melo
f0784649 e861964a

+47
+42
tools/perf/util/llvm-utils.c
··· 42 42 llvm_param.kbuild_dir = strdup(value); 43 43 else if (!strcmp(var, "kbuild-opts")) 44 44 llvm_param.kbuild_opts = strdup(value); 45 + else if (!strcmp(var, "dump-obj")) 46 + llvm_param.dump_obj = !!perf_config_bool(var, value); 45 47 else 46 48 return -1; 47 49 llvm_param.user_set_param = true; ··· 328 326 pr_debug("include option is set to %s\n", *kbuild_include_opts); 329 327 } 330 328 329 + static void 330 + dump_obj(const char *path, void *obj_buf, size_t size) 331 + { 332 + char *obj_path = strdup(path); 333 + FILE *fp; 334 + char *p; 335 + 336 + if (!obj_path) { 337 + pr_warning("WARNING: No enough memory, skip object dumping\n"); 338 + return; 339 + } 340 + 341 + p = strrchr(obj_path, '.'); 342 + if (!p || (strcmp(p, ".c") != 0)) { 343 + pr_warning("WARNING: invalid llvm source path: '%s', skip object dumping\n", 344 + obj_path); 345 + goto out; 346 + } 347 + 348 + p[1] = 'o'; 349 + fp = fopen(obj_path, "wb"); 350 + if (!fp) { 351 + pr_warning("WARNING: failed to open '%s': %s, skip object dumping\n", 352 + obj_path, strerror(errno)); 353 + goto out; 354 + } 355 + 356 + pr_info("LLVM: dumping %s\n", obj_path); 357 + if (fwrite(obj_buf, size, 1, fp) != 1) 358 + pr_warning("WARNING: failed to write to file '%s': %s, skip object dumping\n", 359 + obj_path, strerror(errno)); 360 + fclose(fp); 361 + out: 362 + free(obj_path); 363 + } 364 + 331 365 int llvm__compile_bpf(const char *path, void **p_obj_buf, 332 366 size_t *p_obj_buf_sz) 333 367 { ··· 449 411 450 412 free(kbuild_dir); 451 413 free(kbuild_include_opts); 414 + 415 + if (llvm_param.dump_obj) 416 + dump_obj(path, obj_buf, obj_buf_sz); 417 + 452 418 if (!p_obj_buf) 453 419 free(obj_buf); 454 420 else
+5
tools/perf/util/llvm-utils.h
··· 30 30 */ 31 31 const char *kbuild_opts; 32 32 /* 33 + * Default is false. If set to true, write compiling result 34 + * to object file. 35 + */ 36 + bool dump_obj; 37 + /* 33 38 * Default is false. If one of the above fields is set by user 34 39 * explicitly then user_set_llvm is set to true. This is used 35 40 * for perf test. If user doesn't set anything in .perfconfig