aboutsummaryrefslogtreecommitdiff
path: root/libctf/testsuite/libctf-lookup
diff options
context:
space:
mode:
authorNick Alcock <nick.alcock@oracle.com>2021-01-05 13:25:56 +0000
committerNick Alcock <nick.alcock@oracle.com>2021-01-05 14:53:40 +0000
commitc59e30ed1727135f8efb79890f2c458f73709757 (patch)
treeed4d06b6438829589f49f3141791debf18b95149 /libctf/testsuite/libctf-lookup
parent1038406a8f6609ad0a449746da70393b0835f699 (diff)
downloadbinutils-c59e30ed1727135f8efb79890f2c458f73709757.zip
binutils-c59e30ed1727135f8efb79890f2c458f73709757.tar.gz
binutils-c59e30ed1727135f8efb79890f2c458f73709757.tar.bz2
libctf: new testsuite
This introduces a new lookup testsuite under libctf, which operates by compiling (with libtool) a "lookup" .c file that uses libctf to analyze some other program, then compiling some number of test object files with CTF and optionally linking them together and running the lookup program on the test object files (or linked test binary), before diffing the result much as run_dump_test does. This lets us test the portions of libctf that are not previously testable, notably the portions that do lookup on linked output and that create dynamic dictionaries and then do lookup on them before writing them out, something that is not tested by the ld-ctf testsuite because the linker never does this. A couple of simple tests are added: one testing the functionality of enum lookups, and one testing that the recently-added commit adding extra paranoia to incomplete type handling doesn't break linking and that the result of the link is an (otherwise-impossible) array of forward type in the shared CTF dict. ChangeLog 2021-01-05 Nick Alcock <nick.alcock@oracle.com> * Makefile.def (libctf): No longer no_check. Checking depends on all-ld. * Makefile.in: Regenerated. libctf/ChangeLog 2021-01-05 Nick Alcock <nick.alcock@oracle.com> * Makefile.am (EXPECT): New. (RUNTEST): Likewise. (RUNTESTFLAGS): Likewise. (CC_FOR_TARGET): Likewise. (check-DEJAGNU): Likewise. (AUTOMAKE_OPTIONS): Add dejagnu. * Makefile.in: Regenerated. * testsuite/config/default.exp: New. * testsuite/lib/ctf-lib.exp: Likewise. * testsuite/libctf-lookup/enum.lk: New test. * testsuite/libctf-lookup/enum-ctf.c: New CTF input. * testsuite/libctf-lookup/enum.c: New lookup test. * testsuite/libctf-lookup/ambiguous-struct*.c: New test. * testsuite/libctf-lookup/lookup.exp: New.
Diffstat (limited to 'libctf/testsuite/libctf-lookup')
-rw-r--r--libctf/testsuite/libctf-lookup/ambiguous-struct-A.c8
-rw-r--r--libctf/testsuite/libctf-lookup/ambiguous-struct-B.c5
-rw-r--r--libctf/testsuite/libctf-lookup/ambiguous-struct.c51
-rw-r--r--libctf/testsuite/libctf-lookup/ambiguous-struct.lk4
-rw-r--r--libctf/testsuite/libctf-lookup/enum-ctf.c8
-rw-r--r--libctf/testsuite/libctf-lookup/enum.c78
-rw-r--r--libctf/testsuite/libctf-lookup/enum.lk10
-rw-r--r--libctf/testsuite/libctf-lookup/lookup.exp43
8 files changed, 207 insertions, 0 deletions
diff --git a/libctf/testsuite/libctf-lookup/ambiguous-struct-A.c b/libctf/testsuite/libctf-lookup/ambiguous-struct-A.c
new file mode 100644
index 0000000..67047c4
--- /dev/null
+++ b/libctf/testsuite/libctf-lookup/ambiguous-struct-A.c
@@ -0,0 +1,8 @@
+struct A;
+struct B { struct A *a; };
+struct A { struct B b; long foo; long bar; struct B b2; };
+
+typedef struct A a_array[50];
+a_array *foo __attribute__((__used__));
+
+static struct A a __attribute ((__used__));
diff --git a/libctf/testsuite/libctf-lookup/ambiguous-struct-B.c b/libctf/testsuite/libctf-lookup/ambiguous-struct-B.c
new file mode 100644
index 0000000..95a9346
--- /dev/null
+++ b/libctf/testsuite/libctf-lookup/ambiguous-struct-B.c
@@ -0,0 +1,5 @@
+struct A;
+struct B { struct A *a; };
+struct A { struct B b; int foo; struct B b2; };
+
+static struct A a __attribute__((__used__));
diff --git a/libctf/testsuite/libctf-lookup/ambiguous-struct.c b/libctf/testsuite/libctf-lookup/ambiguous-struct.c
new file mode 100644
index 0000000..05b471e
--- /dev/null
+++ b/libctf/testsuite/libctf-lookup/ambiguous-struct.c
@@ -0,0 +1,51 @@
+/* Test ambiguous forward lookups post-deduplication.
+
+ This also makes sure that deduplication succeeds in this case and does not
+ throw spurious errors. */
+
+#include <ctf-api.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int
+main (int argc, char *argv[])
+{
+ ctf_dict_t *fp;
+ ctf_archive_t *ctf;
+ ctf_id_t type;
+ ctf_arinfo_t ar;
+ int err;
+
+ if (argc != 2)
+ {
+ fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]);
+ exit(1);
+ }
+
+ if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
+ goto open_err;
+ if ((fp = ctf_dict_open (ctf, NULL, &err)) == NULL)
+ goto open_err;
+
+ /* Dig out the array type and resolve its element type. */
+
+ if ((type = ctf_lookup_by_name (fp, "a_array") ) == CTF_ERR)
+ goto err;
+ if ((type = ctf_type_resolve (fp, type)) == CTF_ERR)
+ goto err;
+ if (ctf_array_info (fp, type, &ar) < 0)
+ goto err;
+ printf ("Kind of array element is %i\n", ctf_type_kind (fp, ar.ctr_contents));
+
+ ctf_dict_close (fp);
+ ctf_close (ctf);
+
+ return 0;
+
+ open_err:
+ fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
+ return 1;
+ err:
+ fprintf (stderr, "Lookup failed: %s\n", ctf_errmsg (ctf_errno (fp)));
+ return 1;
+}
diff --git a/libctf/testsuite/libctf-lookup/ambiguous-struct.lk b/libctf/testsuite/libctf-lookup/ambiguous-struct.lk
new file mode 100644
index 0000000..84f296d
--- /dev/null
+++ b/libctf/testsuite/libctf-lookup/ambiguous-struct.lk
@@ -0,0 +1,4 @@
+# source: ambiguous-struct-A.c
+# source: ambiguous-struct-B.c
+# link: on
+Kind of array element is 9
diff --git a/libctf/testsuite/libctf-lookup/enum-ctf.c b/libctf/testsuite/libctf-lookup/enum-ctf.c
new file mode 100644
index 0000000..aa60d72
--- /dev/null
+++ b/libctf/testsuite/libctf-lookup/enum-ctf.c
@@ -0,0 +1,8 @@
+/* Looked up item by item. */
+enum e { ENUMSAMPLE_1 = 0, ENUMSAMPLE_2 = 1 };
+
+/* Looked up via both sorts of iterator in turn. */
+enum ie { IENUMSAMPLE_1 = -10, IENUMSAMPLE_2, IENUMSAMPLE_3 };
+
+enum e foo;
+enum ie bar;
diff --git a/libctf/testsuite/libctf-lookup/enum.c b/libctf/testsuite/libctf-lookup/enum.c
new file mode 100644
index 0000000..1804b23
--- /dev/null
+++ b/libctf/testsuite/libctf-lookup/enum.c
@@ -0,0 +1,78 @@
+#include <ctf-api.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+static int
+print_enum (const char *name, int val, void *unused)
+{
+ printf ("iter test: %s has value %i\n", name, val);
+ return 0;
+}
+
+int
+main (int argc, char *argv[])
+{
+ ctf_dict_t *fp;
+ ctf_archive_t *ctf;
+ ctf_id_t type;
+ const char *name;
+ ctf_next_t *i = NULL;
+ int val;
+ int err;
+
+ if (argc != 2)
+ {
+ fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]);
+ exit(1);
+ }
+
+ if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
+ goto open_err;
+ if ((fp = ctf_dict_open (ctf, NULL, &err)) == NULL)
+ goto open_err;
+
+ /* Try getting some enum values by hand. */
+
+ if ((type = ctf_lookup_by_name (fp, "enum e") ) == CTF_ERR)
+ goto err;
+ if (ctf_enum_value (fp, type, "ENUMSAMPLE_1", &val) < 0)
+ goto err;
+ printf ("Enum e enumerand ENUMSAMPLE_1 has value %i\n", val);
+
+ if ((name = ctf_enum_name (fp, type, 1)) == NULL)
+ goto err;
+ printf ("Enum e enumerand %s has value 1\n", name);
+
+ /* Try getting some values using both sorts of iterator. */
+
+ if ((type = ctf_lookup_by_name (fp, "enum ie") ) == CTF_ERR)
+ goto err;
+
+ if ((ctf_enum_iter (fp, type, print_enum, NULL)) < 0)
+ goto ierr;
+
+ while ((name = ctf_enum_next (fp, type, &i, &val)) != NULL)
+ {
+ printf ("next test: %s has value %i\n", name, val);
+ }
+ if (ctf_errno (fp) != ECTF_NEXT_END)
+ goto nerr;
+
+ ctf_dict_close (fp);
+ ctf_close (ctf);
+
+ return 0;
+
+ open_err:
+ fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
+ return 1;
+ err:
+ fprintf (stderr, "Lookup failed: %s\n", ctf_errmsg (ctf_errno (fp)));
+ return 1;
+ ierr:
+ fprintf (stderr, "_iter iteration failed: %s\n", ctf_errmsg (ctf_errno (fp)));
+ return 1;
+ nerr:
+ fprintf (stderr, "_next iteration failed: %s\n", ctf_errmsg (ctf_errno (fp)));
+ return 1;
+}
diff --git a/libctf/testsuite/libctf-lookup/enum.lk b/libctf/testsuite/libctf-lookup/enum.lk
new file mode 100644
index 0000000..0b2b157
--- /dev/null
+++ b/libctf/testsuite/libctf-lookup/enum.lk
@@ -0,0 +1,10 @@
+# source: enum-ctf.c
+# link: on
+Enum e enumerand ENUMSAMPLE_1 has value 0
+Enum e enumerand ENUMSAMPLE_2 has value 1
+iter test: IENUMSAMPLE_1 has value -10
+iter test: IENUMSAMPLE_2 has value -9
+iter test: IENUMSAMPLE_3 has value -8
+next test: IENUMSAMPLE_1 has value -10
+next test: IENUMSAMPLE_2 has value -9
+next test: IENUMSAMPLE_3 has value -8
diff --git a/libctf/testsuite/libctf-lookup/lookup.exp b/libctf/testsuite/libctf-lookup/lookup.exp
new file mode 100644
index 0000000..51ad257
--- /dev/null
+++ b/libctf/testsuite/libctf-lookup/lookup.exp
@@ -0,0 +1,43 @@
+# Copyright (C) 2021 Free Software Foundation, Inc.
+#
+# This file is part of the GNU Binutils.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+# MA 02110-1301, USA.
+#
+
+if ![is_elf_format] {
+ unsupported "CTF needs bfd changes to be emitted on non-ELF"
+ return 0
+}
+
+if {[info exists env(LC_ALL)]} {
+ set old_lc_all $env(LC_ALL)
+}
+set env(LC_ALL) "C"
+
+set ctf_test_list [lsort [glob -nocomplain $srcdir/$subdir/*.lk]]
+
+foreach ctf_test $ctf_test_list {
+ verbose [file rootname $ctf_test]
+ verbose running lookup test on $ctf_test
+ run_lookup_test [file rootname $ctf_test]
+}
+
+if {[info exists old_lc_all]} {
+ set env(LC_ALL) $old_lc_all
+} else {
+ unset env(LC_ALL)
+}