xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_ctf.h (revision 7b93ed1c50c3745a68994137a0da1afdd61a0e4b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
27  * Copyright 2018 Joyent, Inc.
28  * Copyright 2025 Oxide Computer Company
29  */
30 
31 #ifndef	_MDB_CTF_H
32 #define	_MDB_CTF_H
33 
34 #include <mdb/mdb_target.h>
35 #include <libctf.h>
36 
37 #ifdef _MDB
38 #include <sys/machelf.h>
39 #include <mdb/mdb_modapi.h>
40 #endif
41 
42 /*
43  * The following directive tells the mapfile generator that prototypes and
44  * declarations ending with an "Internal" comment should be excluded from the
45  * mapfile.
46  *
47  * MAPFILE: exclude "Internal"
48  */
49 
50 #ifdef	__cplusplus
51 extern "C" {
52 #endif
53 
54 typedef struct mdb_ctf_id {
55 	void *_opaque[2];
56 } mdb_ctf_id_t;
57 
58 typedef struct mdb_ctf_funcinfo {
59 	mdb_ctf_id_t mtf_return;	/* function return type */
60 	uint_t mtf_argc;		/* number of arguments */
61 	uint_t mtf_flags;		/* function attributes (see libctf.h) */
62 	uint_t mtf_symidx;		/* for ctf_func_args */
63 } mdb_ctf_funcinfo_t;
64 
65 typedef struct mdb_ctf_arinfo {
66 	mdb_ctf_id_t mta_contents;	/* type of array conents */
67 	mdb_ctf_id_t mta_index;		/* type of array index */
68 	uint_t mta_nelems;		/* number of elements */
69 } mdb_ctf_arinfo_t;
70 
71 typedef int mdb_ctf_visit_f(const char *, mdb_ctf_id_t, mdb_ctf_id_t, ulong_t,
72     int, void *);
73 typedef int mdb_ctf_member_f(const char *, mdb_ctf_id_t, ulong_t, void *);
74 typedef int mdb_ctf_enum_f(const char *, int, void *);
75 typedef int mdb_ctf_type_f(mdb_ctf_id_t, void *);
76 
77 extern int mdb_ctf_enabled_by_object(const char *);
78 
79 extern int mdb_ctf_lookup_by_name(const char *, mdb_ctf_id_t *);
80 extern int mdb_ctf_lookup_by_addr(uintptr_t, mdb_ctf_id_t *);
81 
82 extern int mdb_ctf_module_lookup(const char *, mdb_ctf_id_t *);
83 
84 extern int mdb_ctf_func_info(const GElf_Sym *, const mdb_syminfo_t *,
85     mdb_ctf_funcinfo_t *);
86 extern int mdb_ctf_func_args(const mdb_ctf_funcinfo_t *, uint_t,
87     mdb_ctf_id_t *);
88 
89 extern void mdb_ctf_type_invalidate(mdb_ctf_id_t *);
90 extern int mdb_ctf_type_valid(mdb_ctf_id_t);
91 extern int mdb_ctf_type_cmp(mdb_ctf_id_t, mdb_ctf_id_t);
92 
93 extern int mdb_ctf_type_resolve(mdb_ctf_id_t, mdb_ctf_id_t *);
94 extern char *mdb_ctf_type_name(mdb_ctf_id_t, char *, size_t);
95 extern ssize_t mdb_ctf_type_lname(mdb_ctf_id_t, char *, size_t);
96 extern ssize_t mdb_ctf_type_size(mdb_ctf_id_t);
97 extern int mdb_ctf_type_kind(mdb_ctf_id_t);
98 extern int mdb_ctf_type_reference(const mdb_ctf_id_t, mdb_ctf_id_t *);
99 extern int mdb_ctf_type_encoding(mdb_ctf_id_t, ctf_encoding_t *);
100 extern int mdb_ctf_type_visit(mdb_ctf_id_t, mdb_ctf_visit_f *, void *);
101 
102 extern int mdb_ctf_array_info(mdb_ctf_id_t, mdb_ctf_arinfo_t *);
103 extern const char *mdb_ctf_enum_name(mdb_ctf_id_t, int);
104 
105 extern int mdb_ctf_member_iter(mdb_ctf_id_t, mdb_ctf_member_f *, void *,
106     uint32_t);
107 extern int mdb_ctf_enum_iter(mdb_ctf_id_t, mdb_ctf_enum_f *, void *);
108 extern int mdb_ctf_type_iter(const char *, mdb_ctf_type_f *, void *);
109 extern int mdb_ctf_type_delete(const mdb_ctf_id_t *);
110 
111 /*
112  * Flags to mdb_ctf_member_iter to change iteration behavior.
113  *
114  * MDB_CTF_F_ITER_ANON		This controls whether or not type member
115  *				iteration will descend into anonymous structs
116  *				or unions recursively. This is used for cases
117  *				where one is trying to reason about member
118  *				references (ala ::print) in a similar way to a C
119  *				compiler.
120  */
121 #define	MDB_CTF_F_ITER_ANON	(1 << 0)
122 
123 /*
124  * Special values for mdb_ctf_type_iter.
125  */
126 #define	MDB_CTF_SYNTHETIC_ITER	(const char *)(-1L)
127 
128 #define	SYNTHETIC_ILP32	1
129 #define	SYNTHETIC_LP64	2
130 extern int mdb_ctf_synthetics_create_base(int);
131 extern int mdb_ctf_synthetics_reset(void);
132 
133 /*
134  * Synthetic creation routines
135  */
136 extern int mdb_ctf_add_typedef(const char *, const mdb_ctf_id_t *,
137     mdb_ctf_id_t *);
138 extern int mdb_ctf_add_struct(const char *, mdb_ctf_id_t *);
139 extern int mdb_ctf_add_union(const char *, mdb_ctf_id_t *);
140 extern int mdb_ctf_add_member(const mdb_ctf_id_t *, const char *,
141     const mdb_ctf_id_t *, mdb_ctf_id_t *);
142 extern int mdb_ctf_add_array(const mdb_ctf_arinfo_t *, mdb_ctf_id_t *);
143 extern int mdb_ctf_add_pointer(const mdb_ctf_id_t *, mdb_ctf_id_t *);
144 
145 /* utility stuff */
146 extern ctf_id_t mdb_ctf_type_id(mdb_ctf_id_t);
147 extern ctf_file_t *mdb_ctf_type_file(mdb_ctf_id_t);
148 
149 
150 extern int mdb_ctf_member_info(mdb_ctf_id_t, const char *,
151     ulong_t *, mdb_ctf_id_t *);
152 extern int mdb_ctf_offsetof(mdb_ctf_id_t, const char *, ulong_t *);
153 extern int mdb_ctf_num_members(mdb_ctf_id_t);
154 extern int mdb_ctf_offsetof_by_name(const char *, const char *);
155 extern ssize_t mdb_ctf_sizeof_by_name(const char *);
156 
157 extern ssize_t mdb_ctf_offset_to_name(mdb_ctf_id_t, ulong_t, char *, size_t,
158     int, mdb_ctf_id_t *, ulong_t *);
159 
160 #define	MDB_CTF_VREAD_QUIET		0x100
161 #define	MDB_CTF_VREAD_IGNORE_ABSENT	0x200
162 
163 extern int mdb_ctf_vread(void *, const char *, const char *,
164     uintptr_t, uint_t);
165 extern int mdb_ctf_readsym(void *, const char *, const char *, uint_t);
166 
167 #ifdef _MDB
168 
169 extern ctf_file_t *mdb_ctf_open(const char *, int *);		/* Internal */
170 extern ctf_file_t *mdb_ctf_bufopen(const void *, size_t,	/* Internal */
171     const void *, Shdr *, const void *, Shdr *, int *);
172 extern int mdb_ctf_write(const char *, ctf_file_t *fp);		/* Internal */
173 extern void mdb_ctf_close(ctf_file_t *fp);			/* Internal */
174 extern int mdb_ctf_synthetics_init(void);			/* Internal */
175 extern void mdb_ctf_synthetics_fini(void);			/* Internal */
176 extern int mdb_ctf_synthetics_from_file(const char *);		/* Internal */
177 extern int mdb_ctf_synthetics_to_file(const char *);		/* Internal */
178 extern int cmd_typelist(uintptr_t, uint_t, int,			/* Internal */
179     const mdb_arg_t *);
180 
181 #endif
182 
183 #ifdef	__cplusplus
184 }
185 #endif
186 
187 #endif	/* _MDB_CTF_H */
188