xref: /netbsd-src/external/gpl3/gdb/dist/libctf/testsuite/libctf-writable/pptrtab.c (revision 4b169a6ba595ae283ca507b26b15fdff40495b1c)
1 #include <ctf-api.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 
5 int
main(int argc,char * argv[])6 main (int argc, char *argv[])
7 {
8   ctf_dict_t *pfp;
9   ctf_dict_t *cfp;
10   ctf_id_t base, base2, ptr, type, last_type;
11   ctf_encoding_t encoding = { CTF_INT_SIGNED, 0, sizeof (int) };
12   ctf_encoding_t encoding2 = { CTF_INT_SIGNED, 0, sizeof (long) };
13   char *type_name;
14   int err;
15 
16   if ((pfp = ctf_create (&err)) == NULL)
17     goto create_err;
18 
19   if ((cfp = ctf_create (&err)) == NULL)
20     goto create_err;
21 
22   if (ctf_import (cfp, pfp) < 0)
23     goto create_child;
24 
25   /* First, try an int in the parent with a pointer in the child.  Also make
26      another pair of types we will chain to later: these end up before the
27      pptrtab lazy-update watermark.  */
28 
29   if ((base = ctf_add_integer (pfp, CTF_ADD_ROOT, "int", &encoding)) == CTF_ERR)
30     goto create_parent;
31 
32   if ((base2 = ctf_add_integer (pfp, CTF_ADD_ROOT, "long int", &encoding2)) == CTF_ERR)
33     goto create_parent;
34 
35   if ((ptr = ctf_add_pointer (cfp, CTF_ADD_ROOT, base)) == CTF_ERR)
36     goto create_child;
37 
38   if ((type = ctf_lookup_by_name (cfp, "int *") ) == CTF_ERR)
39     goto err;
40 
41   type_name = ctf_type_aname (cfp, type);
42   printf ("First lookup: %s in the child points to a type of kind %i\n",
43 	  type_name, ctf_type_kind (cfp, ctf_type_reference (cfp, type)));
44   free (type_name);
45 
46   if (ctf_type_reference (cfp, type) != base)
47     printf ("First lookup ref diff: %lx versus %lx\n", base,
48 	    ctf_type_reference (cfp, type));
49   last_type = type;
50 
51   /* Add another pointer to the same type in the parent and try a lookup.  */
52 
53   if ((ptr = ctf_add_pointer (pfp, CTF_ADD_ROOT, base2)) == CTF_ERR)
54     goto create_parent;
55 
56   if ((type = ctf_lookup_by_name (cfp, "long int *") ) == CTF_ERR)
57     goto err;
58 
59   type_name = ctf_type_aname (cfp, type);
60   printf ("Second lookup: %s in the child points to a type of kind %i\n",
61 	  type_name, ctf_type_kind (cfp, ctf_type_reference (cfp, type)));
62   free (type_name);
63 
64   if (ctf_type_reference (cfp, type) != base2)
65     printf ("Second lookup ref diff: %lx versus %lx\n", base2,
66 	    ctf_type_reference (cfp, type));
67   if (last_type == type)
68     printf ("Second lookup should not return the same type as the first: %lx\n", type);
69 
70   last_type = type;
71 
72   /* Add another pointer to the same type in the child and try a lookup.  */
73 
74   if ((ptr = ctf_add_pointer (cfp, CTF_ADD_ROOT, base2)) == CTF_ERR)
75     goto create_child;
76 
77   if ((type = ctf_lookup_by_name (cfp, "long int *") ) == CTF_ERR)
78     goto err;
79 
80   type_name = ctf_type_aname (cfp, type);
81   printf ("Third lookup: %s in the child points to a type of kind %i\n",
82 	  type_name, ctf_type_kind (cfp, ctf_type_reference (cfp, type)));
83   free (type_name);
84 
85   if (ctf_type_reference (cfp, type) != base2)
86     printf ("Third lookup ref diff: %lx versus %lx\n", base2,
87 	    ctf_type_reference (cfp, type));
88 
89   if (last_type == type)
90     printf ("Third lookup should not return the same type as the second: %lx\n", type);
91 
92   ctf_file_close (cfp);
93   ctf_file_close (pfp);
94 
95   return 0;
96 
97  create_err:
98   fprintf (stderr, "Creation failed: %s\n", ctf_errmsg (err));
99   return 1;
100  create_parent:
101   fprintf (stderr, "Cannot create type: %s\n", ctf_errmsg (ctf_errno (pfp)));
102   return 1;
103  create_child:
104   fprintf (stderr, "Cannot create type: %s\n", ctf_errmsg (ctf_errno (cfp)));
105   return 1;
106  err:
107   fprintf (stderr, "Lookup failed: %s\n", ctf_errmsg (ctf_errno (cfp)));
108   return 1;
109 }
110