[DTrace-devel] [PATCH v4 2/2] test: Add print() tests

Alan Maguire alan.maguire at oracle.com
Thu Nov 23 10:52:37 UTC 2023


From: Eugene Loh <eugene.loh at oracle.com>

use local type declaration + alloca() to create custom types for
testing, or use kernel types + alloca().  Verify

- various user-defined types are printed correctly
- zeroed values are skipped
- we truncate display at offset <value> with -x printsize=<value>,
  and can dynamically alter printsize via setopt()
- we can print kernel-defined types
- multiple print()s do not exhaust register needs

For error testing we verify errors are seen

- with a zero-sized type (need to use a kernel type as D compiler
  rejects attempts to create a zero-sized struct)
- with an invalid argument (1)
- with too few arguments (0)
- with too many arguments (2)

Thanks to Eugene for reworking the tests to use the much
cleaner .d/.r format, and providing err.* tests.

Co-developed-by: Eugene Loh <eugene.loh at oracle.com>
Signed-off-by: Alan Maguire <alan.maguire at oracle.com>
---
 test/unittest/print/err.D_PRINT_SIZE.zero.d   | 15 ++++
 test/unittest/print/err.D_PRINT_SIZE.zero.r   |  2 +
 test/unittest/print/err.D_PROTO_ARG.arg.d     | 12 +++
 test/unittest/print/err.D_PROTO_ARG.arg.r     |  4 +
 test/unittest/print/err.D_PROTO_LEN.toofew.d  | 12 +++
 test/unittest/print/err.D_PROTO_LEN.toofew.r  |  2 +
 test/unittest/print/err.D_PROTO_LEN.toomany.d | 13 +++
 test/unittest/print/err.D_PROTO_LEN.toomany.r |  2 +
 test/unittest/print/tst.print.local.d         | 45 +++++++++++
 test/unittest/print/tst.print.local.r         | 21 +++++
 test/unittest/print/tst.print.local.trunc.d   | 49 +++++++++++
 test/unittest/print/tst.print.local.trunc.r   | 27 +++++++
 test/unittest/print/tst.print.local.zeroed.d  | 46 +++++++++++
 test/unittest/print/tst.print.local.zeroed.r  |  8 ++
 test/unittest/print/tst.print.regleak.d       | 37 +++++++++
 test/unittest/print/tst.print.regleak.r       | 81 +++++++++++++++++++
 test/unittest/print/tst.print.skb.d           | 19 +++++
 test/unittest/print/tst.print.skb.r           | 14 ++++
 18 files changed, 409 insertions(+)
 create mode 100644 test/unittest/print/err.D_PRINT_SIZE.zero.d
 create mode 100644 test/unittest/print/err.D_PRINT_SIZE.zero.r
 create mode 100644 test/unittest/print/err.D_PROTO_ARG.arg.d
 create mode 100644 test/unittest/print/err.D_PROTO_ARG.arg.r
 create mode 100644 test/unittest/print/err.D_PROTO_LEN.toofew.d
 create mode 100644 test/unittest/print/err.D_PROTO_LEN.toofew.r
 create mode 100644 test/unittest/print/err.D_PROTO_LEN.toomany.d
 create mode 100644 test/unittest/print/err.D_PROTO_LEN.toomany.r
 create mode 100644 test/unittest/print/tst.print.local.d
 create mode 100644 test/unittest/print/tst.print.local.r
 create mode 100644 test/unittest/print/tst.print.local.trunc.d
 create mode 100644 test/unittest/print/tst.print.local.trunc.r
 create mode 100644 test/unittest/print/tst.print.local.zeroed.d
 create mode 100644 test/unittest/print/tst.print.local.zeroed.r
 create mode 100644 test/unittest/print/tst.print.regleak.d
 create mode 100644 test/unittest/print/tst.print.regleak.r
 create mode 100644 test/unittest/print/tst.print.skb.d
 create mode 100644 test/unittest/print/tst.print.skb.r

diff --git a/test/unittest/print/err.D_PRINT_SIZE.zero.d b/test/unittest/print/err.D_PRINT_SIZE.zero.d
new file mode 100644
index 00000000..bbd8a217
--- /dev/null
+++ b/test/unittest/print/err.D_PRINT_SIZE.zero.d
@@ -0,0 +1,15 @@
+/*
+ * Oracle Linux DTrace.
+ * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
+ * Licensed under the Universal Permissive License v 1.0 as shown at
+ * http://oss.oracle.com/licenses/upl.
+ */
+
+#pragma D option quiet
+
+BEGIN
+{
+	t = (int *)alloca(sizeof(int));
+	print((struct bpf_raw_tracepoint_args *)t);
+	exit(0);
+}
diff --git a/test/unittest/print/err.D_PRINT_SIZE.zero.r b/test/unittest/print/err.D_PRINT_SIZE.zero.r
new file mode 100644
index 00000000..d882b74a
--- /dev/null
+++ b/test/unittest/print/err.D_PRINT_SIZE.zero.r
@@ -0,0 +1,2 @@
+-- @@stderr --
+dtrace: failed to compile script test/unittest/print/err.D_PRINT_SIZE.zero.d: [D_PRINT_SIZE] line 13: print( ) argument #1 reference has type 'struct bpf_raw_tracepoint_args' with size 0; cannot print( ) it.
diff --git a/test/unittest/print/err.D_PROTO_ARG.arg.d b/test/unittest/print/err.D_PROTO_ARG.arg.d
new file mode 100644
index 00000000..7a32dd13
--- /dev/null
+++ b/test/unittest/print/err.D_PROTO_ARG.arg.d
@@ -0,0 +1,12 @@
+/*
+ * Oracle Linux DTrace.
+ * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
+ * Licensed under the Universal Permissive License v 1.0 as shown at
+ * http://oss.oracle.com/licenses/upl.
+ */
+
+BEGIN
+{
+	print(1);
+	exit(1);
+}
diff --git a/test/unittest/print/err.D_PROTO_ARG.arg.r b/test/unittest/print/err.D_PROTO_ARG.arg.r
new file mode 100644
index 00000000..cbac2cdb
--- /dev/null
+++ b/test/unittest/print/err.D_PROTO_ARG.arg.r
@@ -0,0 +1,4 @@
+-- @@stderr --
+dtrace: failed to compile script test/unittest/print/err.D_PROTO_ARG.arg.d: [D_PROTO_ARG] line 10: print( ) argument #1 is incompatible with prototype:
+	prototype: void *
+	 argument: int
diff --git a/test/unittest/print/err.D_PROTO_LEN.toofew.d b/test/unittest/print/err.D_PROTO_LEN.toofew.d
new file mode 100644
index 00000000..0785ed52
--- /dev/null
+++ b/test/unittest/print/err.D_PROTO_LEN.toofew.d
@@ -0,0 +1,12 @@
+/*
+ * Oracle Linux DTrace.
+ * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
+ * Licensed under the Universal Permissive License v 1.0 as shown at
+ * http://oss.oracle.com/licenses/upl.
+ */
+
+BEGIN
+{
+	print();
+	exit(1);
+}
diff --git a/test/unittest/print/err.D_PROTO_LEN.toofew.r b/test/unittest/print/err.D_PROTO_LEN.toofew.r
new file mode 100644
index 00000000..d01a3194
--- /dev/null
+++ b/test/unittest/print/err.D_PROTO_LEN.toofew.r
@@ -0,0 +1,2 @@
+-- @@stderr --
+dtrace: failed to compile script test/unittest/print/err.D_PROTO_LEN.toofew.d: [D_PROTO_LEN] line 10: print( ) prototype mismatch: 0 args passed, 1 expected
diff --git a/test/unittest/print/err.D_PROTO_LEN.toomany.d b/test/unittest/print/err.D_PROTO_LEN.toomany.d
new file mode 100644
index 00000000..81d600bc
--- /dev/null
+++ b/test/unittest/print/err.D_PROTO_LEN.toomany.d
@@ -0,0 +1,13 @@
+/*
+ * Oracle Linux DTrace.
+ * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
+ * Licensed under the Universal Permissive License v 1.0 as shown at
+ * http://oss.oracle.com/licenses/upl.
+ */
+
+BEGIN
+{
+	x = 1;
+	print(x, x);
+	exit(1);
+}
diff --git a/test/unittest/print/err.D_PROTO_LEN.toomany.r b/test/unittest/print/err.D_PROTO_LEN.toomany.r
new file mode 100644
index 00000000..a618f20f
--- /dev/null
+++ b/test/unittest/print/err.D_PROTO_LEN.toomany.r
@@ -0,0 +1,2 @@
+-- @@stderr --
+dtrace: failed to compile script test/unittest/print/err.D_PROTO_LEN.toomany.d: [D_PROTO_LEN] line 11: print( ) prototype mismatch: 2 args passed, 1 expected
diff --git a/test/unittest/print/tst.print.local.d b/test/unittest/print/tst.print.local.d
new file mode 100644
index 00000000..ba37be7a
--- /dev/null
+++ b/test/unittest/print/tst.print.local.d
@@ -0,0 +1,45 @@
+/*
+ * Oracle Linux DTrace.
+ * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
+ * Licensed under the Universal Permissive License v 1.0 as shown at
+ * http://oss.oracle.com/licenses/upl.
+ */
+/* @@nosort */
+
+#pragma D option quiet
+
+enum eh { FIRSTVAL = 0, SECONDVAL = 1, THIRDVAL = 3 };
+
+/* DTrace will not parse nested anonymous structs/unions; also bitfields are not supported. */
+struct test_struct {
+	int     a;
+	char    b[5];
+	union {
+		__u8 c;
+		__u64 d;
+	} u;
+	struct {
+		void *e;
+		uint64_t f;
+	} s;
+	bool g;
+	enum eh h;
+};
+
+BEGIN
+{
+	t = (struct test_struct *)alloca(sizeof (struct test_struct));
+	t->a = 1;
+	t->b[0] = 't';
+	t->b[1] = 'e';
+	t->b[2] = 's';
+	t->b[3] = 't';
+	t->b[4] = '\0';
+	t->u.d = 123456789;
+	t->s.e = (void *)0xffff9b7aca7e0000,
+	t->s.f = 987654321;
+	t->g = 1;
+	t->h = SECONDVAL;
+	print(t);
+	exit(0);
+}
diff --git a/test/unittest/print/tst.print.local.r b/test/unittest/print/tst.print.local.r
new file mode 100644
index 00000000..e634bbea
--- /dev/null
+++ b/test/unittest/print/tst.print.local.r
@@ -0,0 +1,21 @@
+{ptr} = *
+                                            (struct test_struct) {
+                                             .a = (int)1,
+                                             .b = (char [5]) [
+                                              (char)'t',
+                                              (char)'e',
+                                              (char)'s',
+                                              (char)'t',
+                                             ],
+                                             .u = (union) {
+                                              .c = (__u8)21,
+                                              .d = (__u64)123456789,
+                                             },
+                                             .s = (struct) {
+                                              .e = (void *){ptr},
+                                              .f = (uint64_t)987654321,
+                                             },
+                                             .g = (bool)1,
+                                             .h = (enum eh)SECONDVAL,
+                                            }
+
diff --git a/test/unittest/print/tst.print.local.trunc.d b/test/unittest/print/tst.print.local.trunc.d
new file mode 100644
index 00000000..25ad54b0
--- /dev/null
+++ b/test/unittest/print/tst.print.local.trunc.d
@@ -0,0 +1,49 @@
+/*
+ * Oracle Linux DTrace.
+ * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
+ * Licensed under the Universal Permissive License v 1.0 as shown at
+ * http://oss.oracle.com/licenses/upl.
+ */
+/* @@nosort */
+
+#pragma D option quiet
+#pragma D option printsize=8
+
+enum eh { FIRSTVAL = 1, SECONDVAL = 2, THIRDVAL= 3 };
+
+/* DTrace will not parse nested anonymous structs/unions; also bitfields are not supported. */
+struct test_struct {
+	int	a;
+	char	b[5];
+	union {
+		__u8 c;
+		__u64 d;
+	} u;
+	struct {
+		void *e;
+		uint64_t f;
+	} s;
+	bool g;
+	enum eh h;
+};
+
+BEGIN
+{
+	t = (struct test_struct *)alloca(sizeof (struct test_struct));
+	t->a = 1;
+	t->b[0] = 't';
+	t->b[1] = 'e';
+	t->b[2] = 's';
+	t->b[3] = 't';
+	t->b[4] = '\0';
+	t->u.d = 12345678,
+	t->s.e = (void *)0xfeedfacefeedface;
+	t->s.f = 100,
+	t->g = 1;
+	t->h = FIRSTVAL;
+	print(t);
+	/* ensure printsize can be dynamically changed */
+	setopt("printsize", "16");
+	print(t);
+	exit(0);
+}
diff --git a/test/unittest/print/tst.print.local.trunc.r b/test/unittest/print/tst.print.local.trunc.r
new file mode 100644
index 00000000..df5708d9
--- /dev/null
+++ b/test/unittest/print/tst.print.local.trunc.r
@@ -0,0 +1,27 @@
+{ptr} = *
+                                            (struct test_struct) {
+                                             .a = (int)1,
+                                             .b = (char [5]) [
+                                              (char)'t',
+                                              (char)'e',
+                                              (char)'s',
+                                              (char)'t',
+                                             ],
+                                             ...
+                                            }
+{ptr} = *
+                                            (struct test_struct) {
+                                             .a = (int)1,
+                                             .b = (char [5]) [
+                                              (char)'t',
+                                              (char)'e',
+                                              (char)'s',
+                                              (char)'t',
+                                             ],
+                                             .u = (union) {
+                                              .c = (__u8)78,
+                                              .d = (__u64)12345678,
+                                             ...
+                                             },
+                                            }
+
diff --git a/test/unittest/print/tst.print.local.zeroed.d b/test/unittest/print/tst.print.local.zeroed.d
new file mode 100644
index 00000000..0372f581
--- /dev/null
+++ b/test/unittest/print/tst.print.local.zeroed.d
@@ -0,0 +1,46 @@
+/*
+ * Oracle Linux DTrace.
+ * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
+ * Licensed under the Universal Permissive License v 1.0 as shown at
+ * http://oss.oracle.com/licenses/upl.
+ */
+/* @@nosort */
+
+#pragma D option quiet
+
+enum eh { FIRSTVAL = 1, SECONDVAL = 2, THIRDVAL= 3 };
+
+/* DTrace will not parse nested anonymous structs/unions; also bitfields are not supported. */
+struct test_struct {
+	int	a;
+	char	b[5];
+	union {
+		__u8 c;
+		__u64 d;
+	} u;
+	struct {
+		void *e;
+		uint64_t f;
+	} s;
+	bool g;
+	enum eh h;
+};
+
+BEGIN
+{
+	t = (struct test_struct *)alloca(sizeof (struct test_struct));
+	t->a = 0;
+	/* initial null should prevent displaying rest of char array */
+	t->b[0] = '\0';
+	t->b[1] = 'e';
+	t->b[2] = 's';
+	t->b[3] = 't';
+	t->b[4] = '\0';
+	t->u.d = 0,
+	t->s.e = (void *)NULL,
+	t->s.f = 0,
+	t->g = 1;
+	t->h = FIRSTVAL;
+	print(t);
+	exit(0);
+}
diff --git a/test/unittest/print/tst.print.local.zeroed.r b/test/unittest/print/tst.print.local.zeroed.r
new file mode 100644
index 00000000..c4e87680
--- /dev/null
+++ b/test/unittest/print/tst.print.local.zeroed.r
@@ -0,0 +1,8 @@
+{ptr} = *
+                                            (struct test_struct) {
+                                             .b = (char [5]) [
+                                             ],
+                                             .g = (bool)1,
+                                             .h = (enum eh)FIRSTVAL,
+                                            }
+
diff --git a/test/unittest/print/tst.print.regleak.d b/test/unittest/print/tst.print.regleak.d
new file mode 100644
index 00000000..287d9c13
--- /dev/null
+++ b/test/unittest/print/tst.print.regleak.d
@@ -0,0 +1,37 @@
+/*
+ * Oracle Linux DTrace.
+ * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
+ * Licensed under the Universal Permissive License v 1.0 as shown at
+ * http://oss.oracle.com/licenses/upl.
+ */
+/* @@nosort */
+
+#pragma D option quiet
+
+BEGIN
+{
+	skb = (struct sk_buff *)alloca(sizeof (struct sk_buff));
+	skb->len = 123;
+	/* check for register leaks by doing a lot of print()s */
+	print(skb);
+	print(skb);
+	print(skb);
+	print(skb);
+	print(skb);
+	print(skb);
+	print(skb);
+	print(skb);
+	print(skb);
+	print(skb);
+	print(skb);
+	print(skb);
+	print(skb);
+	print(skb);
+	print(skb);
+	print(skb);
+	print(skb);
+	print(skb);
+	print(skb);
+	print(skb);
+	exit(0);
+}
diff --git a/test/unittest/print/tst.print.regleak.r b/test/unittest/print/tst.print.regleak.r
new file mode 100644
index 00000000..7a11eafb
--- /dev/null
+++ b/test/unittest/print/tst.print.regleak.r
@@ -0,0 +1,81 @@
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                            }
+
diff --git a/test/unittest/print/tst.print.skb.d b/test/unittest/print/tst.print.skb.d
new file mode 100644
index 00000000..f09686e8
--- /dev/null
+++ b/test/unittest/print/tst.print.skb.d
@@ -0,0 +1,19 @@
+/*
+ * Oracle Linux DTrace.
+ * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
+ * Licensed under the Universal Permissive License v 1.0 as shown at
+ * http://oss.oracle.com/licenses/upl.
+ */
+/* @@nosort */
+
+#pragma D option quiet
+
+BEGIN
+{
+        skb = (struct sk_buff *)alloca(sizeof (struct sk_buff));
+        skb->len = 123;
+        skb->network_header = 32;
+        skb->data = (unsigned char *)0xfeedfacefeedface;
+        print(skb);
+        exit(0);
+}
diff --git a/test/unittest/print/tst.print.skb.r b/test/unittest/print/tst.print.skb.r
new file mode 100644
index 00000000..f389bc4f
--- /dev/null
+++ b/test/unittest/print/tst.print.skb.r
@@ -0,0 +1,14 @@
+{ptr} = *
+                                            (struct sk_buff) {
+                                             .len = (unsigned int)123,
+                                             (struct) {
+                                              (struct) {
+                                               .network_header = (__u16)32,
+                                              },
+                                              .headers = (struct) {
+                                               .network_header = (__u16)32,
+                                              },
+                                             },
+                                             .data = (unsigned char *){ptr},
+                                            }
+
-- 
2.39.3




More information about the DTrace-devel mailing list