hc
2024-05-10 9999e48639b3cecb08ffb37358bcba3b48161b29
kernel/tools/perf/util/llvm-utils.c
....@@ -8,7 +8,10 @@
88 #include <limits.h>
99 #include <stdio.h>
1010 #include <stdlib.h>
11
+#include <unistd.h>
1112 #include <linux/err.h>
13
+#include <linux/string.h>
14
+#include <linux/zalloc.h>
1215 #include "debug.h"
1316 #include "llvm-utils.h"
1417 #include "config.h"
....@@ -19,7 +22,7 @@
1922 #define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
2023 "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
2124 "-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \
22
- "$CLANG_OPTIONS $KERNEL_INC_OPTIONS $PERF_BPF_INC_OPTIONS " \
25
+ "$CLANG_OPTIONS $PERF_BPF_INC_OPTIONS $KERNEL_INC_OPTIONS " \
2326 "-Wno-unused-value -Wno-pointer-sign " \
2427 "-working-directory $WORKING_DIR " \
2528 "-c \"$CLANG_SOURCE\" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE"
....@@ -262,6 +265,8 @@
262265 return -ENOMEM;
263266 return 0;
264267 }
268
+ pr_debug("%s: Couldn't find \"%s\", missing kernel-devel package?.\n",
269
+ __func__, autoconf_path);
265270 free(autoconf_path);
266271 return -ENOENT;
267272 }
....@@ -285,6 +290,7 @@
285290 "obj-y := dummy.o\n"
286291 "\\$(obj)/%.o: \\$(src)/%.c\n"
287292 "\t@echo -n \"\\$(NOSTDINC_FLAGS) \\$(LINUXINCLUDE) \\$(EXTRA_CFLAGS)\"\n"
293
+"\t\\$(CC) -c -o \\$@ \\$<\n"
288294 "EOF\n"
289295 "touch $TMPDIR/dummy.c\n"
290296 "make -s -C $KBUILD_DIR M=$TMPDIR $KBUILD_OPTS dummy.o 2>/dev/null\n"
....@@ -352,8 +358,7 @@
352358 " \toption in [llvm] to \"\" to suppress this detection.\n\n",
353359 *kbuild_dir);
354360
355
- free(*kbuild_dir);
356
- *kbuild_dir = NULL;
361
+ zfree(kbuild_dir);
357362 goto errout;
358363 }
359364
....@@ -416,10 +421,9 @@
416421 goto out;
417422 }
418423
419
- pr_info("LLVM: dumping %s\n", obj_path);
424
+ pr_debug("LLVM: dumping %s\n", obj_path);
420425 if (fwrite(obj_buf, size, 1, fp) != 1)
421
- pr_warning("WARNING: failed to write to file '%s': %s, skip object dumping\n",
422
- obj_path, strerror(errno));
426
+ pr_debug("WARNING: failed to write to file '%s': %s, skip object dumping\n", obj_path, strerror(errno));
423427 fclose(fp);
424428 out:
425429 free(obj_path);
....@@ -521,14 +525,37 @@
521525
522526 pr_debug("llvm compiling command template: %s\n", template);
523527
528
+ /*
529
+ * Below, substitute control characters for values that can cause the
530
+ * echo to misbehave, then substitute the values back.
531
+ */
524532 err = -ENOMEM;
525
- if (asprintf(&command_echo, "echo -n \"%s\"", template) < 0)
533
+ if (asprintf(&command_echo, "echo -n \a%s\a", template) < 0)
526534 goto errout;
527535
536
+#define SWAP_CHAR(a, b) do { if (*p == a) *p = b; } while (0)
537
+ for (char *p = command_echo; *p; p++) {
538
+ SWAP_CHAR('<', '\001');
539
+ SWAP_CHAR('>', '\002');
540
+ SWAP_CHAR('"', '\003');
541
+ SWAP_CHAR('\'', '\004');
542
+ SWAP_CHAR('|', '\005');
543
+ SWAP_CHAR('&', '\006');
544
+ SWAP_CHAR('\a', '"');
545
+ }
528546 err = read_from_pipe(command_echo, (void **) &command_out, NULL);
529547 if (err)
530548 goto errout;
531549
550
+ for (char *p = command_out; *p; p++) {
551
+ SWAP_CHAR('\001', '<');
552
+ SWAP_CHAR('\002', '>');
553
+ SWAP_CHAR('\003', '"');
554
+ SWAP_CHAR('\004', '\'');
555
+ SWAP_CHAR('\005', '|');
556
+ SWAP_CHAR('\006', '&');
557
+ }
558
+#undef SWAP_CHAR
532559 pr_debug("llvm compiling command : %s\n", command_out);
533560
534561 err = read_from_pipe(template, &obj_buf, &obj_buf_sz);