]> granicus.if.org Git - strace/commitdiff
bpf: honor xlat styles when printing kernel version
authorShankara Pailoor <shankarapailoor@gmail.com>
Sun, 9 Dec 2018 15:41:43 +0000 (07:41 -0800)
committerDmitry V. Levin <ldv@altlinux.org>
Mon, 10 Dec 2018 23:45:11 +0000 (23:45 +0000)
* print_kernel_version.c: New file.
* Makefile.am (strace_SOURCES): Add it.
* defs.h (print_kernel_version): New prototype.
* bpf.c (BEGIN_BPF_CMD_DECODER(BPF_PROG_LOAD)):
Use print_kernel_version.
* tests/kernel_version.c: New file.
* tests/kernel_version-Xabbrev.c: Likewise.
* tests/kernel_version-Xraw.c: Likewise.
* tests/kernel_version-Xverbose.c: Likewise.
* tests/gen_tests.in (kernel_version, kernel_version-Xabbrev,
kernel_version-Xraw, kernel_version-Xverbose): New tests.
* tests/pure_executables.list: Add kernel_version,
kernel_version-Xabbrev, kernel_version-Xraw, and kernel_version-Xverbose.
* tests/.gitignore: Likewise.

Co-Authored-by: Dmitry V. Levin <ldv@altlinux.org>
Makefile.am
bpf.c
defs.h
print_kernel_version.c [new file with mode: 0644]
tests/.gitignore
tests/gen_tests.in
tests/kernel_version-Xabbrev.c [new file with mode: 0644]
tests/kernel_version-Xraw.c [new file with mode: 0644]
tests/kernel_version-Xverbose.c [new file with mode: 0644]
tests/kernel_version.c [new file with mode: 0644]
tests/pure_executables.list

index cfcbe7c9cb7cf238b198acb842a64a88e240090c..2dd2f5f21a277e31539903571ad9d39f253918bf 100644 (file)
@@ -249,6 +249,7 @@ strace_SOURCES =    \
        print_fields.h  \
        print_ifindex.c \
        print_instruction_pointer.c \
+       print_kernel_version.c \
        print_mac.c     \
        print_mq_attr.c \
        print_msgbuf.c  \
diff --git a/bpf.c b/bpf.c
index e5dc4eeb1bd037aeebd96b62e8cb3fc5c78d656a..bfe4c0c8a78772911887db136f6f9e697e0b20a4 100644 (file)
--- a/bpf.c
+++ b/bpf.c
@@ -292,11 +292,8 @@ BEGIN_BPF_CMD_DECODER(BPF_PROG_LOAD)
        /* kern_version field was added in Linux commit v4.1-rc1~84^2~50.  */
        if (len <= offsetof(struct BPF_PROG_LOAD_struct, kern_version))
                break;
-       tprintf(", kern_version=KERNEL_VERSION(%u, %u, %u)",
-               attr.kern_version >> 16,
-               (attr.kern_version >> 8) & 0xFF,
-               attr.kern_version & 0xFF);
-
+       tprints(", kern_version=");
+       print_kernel_version(attr.kern_version);
        /* prog_flags field was added in Linux commit v4.12-rc2~34^2~29^2~2.  */
        if (len <= offsetof(struct BPF_PROG_LOAD_struct, prog_flags))
                break;
diff --git a/defs.h b/defs.h
index 25c9f4db37d3ad25082109d57114793b796092ae..d8434095af9f52470fe4e57de500da328468ef3b 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -814,6 +814,7 @@ extern void print_symbolic_mode_t(unsigned int);
 extern void print_numeric_umode_t(unsigned short);
 extern void print_numeric_long_umask(unsigned long);
 extern void print_dev_t(unsigned long long dev);
+extern void print_kernel_version(unsigned long version);
 extern void print_abnormal_hi(kernel_ulong_t);
 
 extern bool print_int32_array_member(struct tcb *, void *elem_buf,
diff --git a/print_kernel_version.c b/print_kernel_version.c
new file mode 100644 (file)
index 0000000..5faedd9
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Kernel version printing routine.
+ *
+ * Copyright (c) 2018 The strace developers.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "defs.h"
+
+void
+print_kernel_version(const unsigned long version)
+{
+       if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
+               tprintf("%#lx", version);
+
+       if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
+               return;
+
+       if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE)
+               tprints(" /* ");
+
+       tprintf("KERNEL_VERSION(%lu, %lu, %lu)",
+                       version >> 16,
+                       (version >> 8) & 0xFF,
+                       version & 0xFF);
+
+       if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE)
+               tprints(" */");
+}
index c8609c39a73907fd61fc20de5dcdef021c7399da..c9dbabb81e736b707529652ab4da15e7ac59f87b 100644 (file)
@@ -198,6 +198,10 @@ is_linux_mips_n64
 kcmp
 kcmp-y
 kern_features
+kernel_version
+kernel_version-Xabbrev
+kernel_version-Xraw
+kernel_version-Xverbose
 kexec_file_load
 kexec_load
 keyctl
index f50b53a80ec58cf60a86bd47f9d40afb8bc7902d..33e76077e3b781353debe5b7d36cb90274f19296 100644 (file)
@@ -187,6 +187,10 @@ ipc_shm-Xraw       +ipc.sh -Xraw -a19
 ipc_shm-Xverbose       +ipc.sh -Xverbose -a36
 kcmp   -a22
 kcmp-y -a22 -y -e trace=kcmp
+kernel_version -a16 -v -e trace=bpf
+kernel_version-Xabbrev -a16 -Xabbrev -v -e trace=bpf
+kernel_version-Xraw    -a16 -Xraw -v -e trace=bpf
+kernel_version-Xverbose        -a16 -Xverbose -v -e trace=bpf
 kern_features -a16
 kexec_file_load        -s9
 kexec_load     -s9
diff --git a/tests/kernel_version-Xabbrev.c b/tests/kernel_version-Xabbrev.c
new file mode 100644 (file)
index 0000000..2d6da35
--- /dev/null
@@ -0,0 +1 @@
+#include "kernel_version.c"
diff --git a/tests/kernel_version-Xraw.c b/tests/kernel_version-Xraw.c
new file mode 100644 (file)
index 0000000..cf27608
--- /dev/null
@@ -0,0 +1,2 @@
+#define XLAT_RAW 1
+#include "kernel_version.c"
diff --git a/tests/kernel_version-Xverbose.c b/tests/kernel_version-Xverbose.c
new file mode 100644 (file)
index 0000000..cc6eb1b
--- /dev/null
@@ -0,0 +1,2 @@
+#define XLAT_VERBOSE 1
+#include "kernel_version.c"
diff --git a/tests/kernel_version.c b/tests/kernel_version.c
new file mode 100644 (file)
index 0000000..e7caeaa
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ * Check kernel version decoding.
+ *
+ * Copyright (c) 2015-2018 The strace developers.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "tests.h"
+
+#include <stddef.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <asm/unistd.h>
+#include "scno.h"
+
+#ifdef HAVE_LINUX_BPF_H
+# include <linux/bpf.h>
+#endif
+
+#include "bpf_attr.h"
+#include "print_fields.h"
+
+#include "xlat.h"
+#include "xlat/bpf_commands.h"
+
+#define CMD_STR(x) #x
+static const char *errstr;
+
+static void
+print_bpf_attr(void)
+{
+#if XLAT_RAW
+       printf("{prog_type=0x15"
+#else
+       printf("{prog_type=0x15 /* BPF_PROG_TYPE_??? */"
+#endif
+               ", insn_cnt=3134983661"
+               ", insns=NULL"
+               ", license=NULL"
+               ", log_level=24"
+               ", log_size=3141592653"
+               ", log_buf=NULL"
+#if XLAT_RAW
+               ", kern_version=0xcafef00d"
+#elif XLAT_VERBOSE
+               ", kern_version=0xcafef00d"
+               " /* KERNEL_VERSION(51966, 240, 13) */"
+#else
+               ", kern_version=KERNEL_VERSION(51966, 240, 13)"
+#endif
+               ", prog_flags=0"
+               ", prog_name=\"\""
+               ", prog_ifindex=0"
+               ", expected_attach_type="
+#if XLAT_RAW
+               "0}"
+#elif XLAT_VERBOSE
+               "0 /* BPF_CGROUP_INET_INGRESS */}"
+#else /* XLAT_ABBREV */
+               "BPF_CGROUP_INET_INGRESS}"
+#endif
+               );
+}
+
+int
+main(void)
+{
+       long ret;
+       struct BPF_PROG_LOAD_struct prog = {
+               .prog_type = 21,
+               .insn_cnt = 0xbadc0ded,
+               .insns = 0,
+               .license = 0,
+               .log_level = 24,
+               .log_size = 3141592653U,
+               .log_buf = 0,
+               .kern_version = 0xcafef00d,
+               .prog_flags = 0,
+       };
+       ret = syscall(__NR_bpf, BPF_PROG_LOAD, &prog, sizeof(prog));
+       errstr = sprintrc(ret);
+#if XLAT_RAW
+       printf("bpf(%#x, ", BPF_PROG_LOAD);
+#elif XLAT_VERBOSE
+       printf("bpf(%#x /* %s */, ", BPF_PROG_LOAD, CMD_STR(BPF_PROG_LOAD));
+#else
+       printf("bpf(%s, ", CMD_STR(BPF_PROG_LOAD));
+#endif
+       print_bpf_attr();
+       printf(", %u) = %s\n", (unsigned int) sizeof(prog), errstr);
+       puts("+++ exited with 0 +++");
+       return 0;
+}
index c6c1b0b52edac4bfb5ba47e1ae1775fbd37c156b..c1ac75ed52bae8b31f3542e6e02c7a5b27fc2301 100755 (executable)
@@ -159,6 +159,10 @@ ipc_shm-Xverbose
 kcmp
 kcmp-y
 kern_features
+kernel_version
+kernel_version-Xabbrev
+kernel_version-Xraw
+kernel_version-Xverbose
 kexec_file_load
 kexec_load
 keyctl