Commit | Line | Data |
---|---|---|
2e94b056 NA |
1 | /* Public API to libctf. |
2 | Copyright (C) 2019 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of libctf. | |
5 | ||
6 | libctf is free software; you can redistribute it and/or modify it under | |
7 | the terms of the GNU General Public License as published by the Free | |
8 | Software Foundation; either version 3, or (at your option) any later | |
9 | version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
14 | See the GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; see the file COPYING. If not see | |
18 | <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | /* This header file defines the interfaces available from the CTF debugger | |
21 | library, libctf. This API can be used by a debugger to operate on data in | |
22 | the Compact ANSI-C Type Format (CTF). */ | |
23 | ||
24 | #ifndef _CTF_API_H | |
25 | #define _CTF_API_H | |
26 | ||
27 | #include <sys/param.h> | |
28 | #include <sys/types.h> | |
29 | #include <ctf.h> | |
47d546f4 | 30 | #include <zlib.h> |
2e94b056 NA |
31 | |
32 | #ifdef __cplusplus | |
33 | extern "C" | |
34 | { | |
35 | #endif | |
36 | ||
37 | /* Clients can open one or more CTF containers and obtain a pointer to an | |
38 | opaque ctf_file_t. Types are identified by an opaque ctf_id_t token. | |
39 | They can also open or create read-only archives of CTF containers in a | |
40 | ctf_archive_t. | |
41 | ||
42 | These opaque definitions allow libctf to evolve without breaking clients. */ | |
43 | ||
44 | typedef struct ctf_file ctf_file_t; | |
45 | typedef struct ctf_archive_internal ctf_archive_t; | |
a0486bac | 46 | typedef unsigned long ctf_id_t; |
2e94b056 | 47 | |
143dce84 NA |
48 | /* This opaque definition allows libctf to accept BFD data structures without |
49 | importing all the BFD noise into users' namespaces. */ | |
50 | ||
51 | struct bfd; | |
52 | ||
47d546f4 NA |
53 | /* If the debugger needs to provide the CTF library with a set of raw buffers |
54 | for use as the CTF data, symbol table, and string table, it can do so by | |
55 | filling in ctf_sect_t structures and passing them to ctf_bufopen(). | |
56 | ||
57 | The contents of this structure must always be in native endianness (no | |
58 | byteswapping is performed). */ | |
59 | ||
60 | typedef struct ctf_sect | |
61 | { | |
62 | const char *cts_name; /* Section name (if any). */ | |
47d546f4 NA |
63 | const void *cts_data; /* Pointer to section data. */ |
64 | size_t cts_size; /* Size of data in bytes. */ | |
65 | size_t cts_entsize; /* Size of each section entry (symtab only). */ | |
47d546f4 NA |
66 | } ctf_sect_t; |
67 | ||
68 | /* Symbolic names for CTF sections. */ | |
69 | ||
70 | typedef enum ctf_sect_names | |
71 | { | |
72 | CTF_SECT_HEADER, | |
73 | CTF_SECT_LABEL, | |
74 | CTF_SECT_OBJT, | |
75 | CTF_SECT_FUNC, | |
76 | CTF_SECT_VAR, | |
77 | CTF_SECT_TYPE, | |
78 | CTF_SECT_STR | |
79 | } ctf_sect_names_t; | |
80 | ||
81 | /* Encoding information for integers, floating-point values, and certain other | |
82 | intrinsics can be obtained by calling ctf_type_encoding(), below. The flags | |
83 | field will contain values appropriate for the type defined in <ctf.h>. */ | |
84 | ||
85 | typedef struct ctf_encoding | |
86 | { | |
87 | uint32_t cte_format; /* Data format (CTF_INT_* or CTF_FP_* flags). */ | |
88 | uint32_t cte_offset; /* Offset of value in bits. */ | |
89 | uint32_t cte_bits; /* Size of storage in bits. */ | |
90 | } ctf_encoding_t; | |
91 | ||
92 | typedef struct ctf_membinfo | |
93 | { | |
94 | ctf_id_t ctm_type; /* Type of struct or union member. */ | |
95 | unsigned long ctm_offset; /* Offset of member in bits. */ | |
96 | } ctf_membinfo_t; | |
97 | ||
98 | typedef struct ctf_arinfo | |
99 | { | |
100 | ctf_id_t ctr_contents; /* Type of array contents. */ | |
101 | ctf_id_t ctr_index; /* Type of array index. */ | |
102 | uint32_t ctr_nelems; /* Number of elements. */ | |
103 | } ctf_arinfo_t; | |
104 | ||
105 | typedef struct ctf_funcinfo | |
106 | { | |
107 | ctf_id_t ctc_return; /* Function return type. */ | |
108 | uint32_t ctc_argc; /* Number of typed arguments to function. */ | |
109 | uint32_t ctc_flags; /* Function attributes (see below). */ | |
110 | } ctf_funcinfo_t; | |
111 | ||
112 | typedef struct ctf_lblinfo | |
113 | { | |
114 | ctf_id_t ctb_type; /* Last type associated with the label. */ | |
115 | } ctf_lblinfo_t; | |
116 | ||
117 | typedef struct ctf_snapshot_id | |
118 | { | |
119 | unsigned long dtd_id; /* Highest DTD ID at time of snapshot. */ | |
120 | unsigned long snapshot_id; /* Snapshot id at time of snapshot. */ | |
121 | } ctf_snapshot_id_t; | |
122 | ||
123 | #define CTF_FUNC_VARARG 0x1 /* Function arguments end with varargs. */ | |
124 | ||
a0486bac JM |
125 | /* Functions that return a ctf_id_t use the following value to indicate failure. |
126 | ctf_errno() can be used to obtain an error code. Functions that return | |
127 | a straight integral -1 also use ctf_errno(). */ | |
128 | #define CTF_ERR ((ctf_id_t) -1L) | |
2e94b056 NA |
129 | |
130 | #define ECTF_BASE 1000 /* Base value for libctf errnos. */ | |
131 | ||
132 | ||
133 | enum | |
134 | { | |
135 | ECTF_FMT = ECTF_BASE, /* File is not in CTF or ELF format. */ | |
136 | ECTF_BFDERR, /* BFD error. */ | |
137 | ECTF_CTFVERS, /* CTF version is more recent than libctf. */ | |
138 | ECTF_BFD_AMBIGUOUS, /* Ambiguous BFD target. */ | |
139 | ECTF_SYMTAB, /* Symbol table uses invalid entry size. */ | |
140 | ECTF_SYMBAD, /* Symbol table data buffer invalid. */ | |
141 | ECTF_STRBAD, /* String table data buffer invalid. */ | |
142 | ECTF_CORRUPT, /* File data corruption detected. */ | |
143 | ECTF_NOCTFDATA, /* ELF file does not contain CTF data. */ | |
144 | ECTF_NOCTFBUF, /* Buffer does not contain CTF data. */ | |
145 | ECTF_NOSYMTAB, /* Symbol table data is not available. */ | |
146 | ECTF_NOPARENT, /* Parent CTF container is not available. */ | |
147 | ECTF_DMODEL, /* Data model mismatch. */ | |
148 | ECTF_UNUSED, /* Unused error. */ | |
149 | ECTF_ZALLOC, /* Failed to allocate (de)compression buffer. */ | |
150 | ECTF_DECOMPRESS, /* Failed to decompress CTF data. */ | |
151 | ECTF_STRTAB, /* String table for this string is missing. */ | |
152 | ECTF_BADNAME, /* String offset is corrupt w.r.t. strtab. */ | |
153 | ECTF_BADID, /* Invalid type ID number. */ | |
154 | ECTF_NOTSOU, /* Type is not a struct or union. */ | |
155 | ECTF_NOTENUM, /* Type is not an enum. */ | |
156 | ECTF_NOTSUE, /* Type is not a struct, union, or enum. */ | |
157 | ECTF_NOTINTFP, /* Type is not an integer, float, or enum. */ | |
158 | ECTF_NOTARRAY, /* Type is not an array. */ | |
159 | ECTF_NOTREF, /* Type does not reference another type. */ | |
160 | ECTF_NAMELEN, /* Buffer is too small to hold type name. */ | |
161 | ECTF_NOTYPE, /* No type found corresponding to name. */ | |
162 | ECTF_SYNTAX, /* Syntax error in type name. */ | |
163 | ECTF_NOTFUNC, /* Symtab entry does not refer to a function. */ | |
164 | ECTF_NOFUNCDAT, /* No func info available for function. */ | |
165 | ECTF_NOTDATA, /* Symtab entry does not refer to a data obj. */ | |
166 | ECTF_NOTYPEDAT, /* No type info available for object. */ | |
167 | ECTF_NOLABEL, /* No label found corresponding to name. */ | |
168 | ECTF_NOLABELDATA, /* File does not contain any labels. */ | |
169 | ECTF_NOTSUP, /* Feature not supported. */ | |
170 | ECTF_NOENUMNAM, /* Enum element name not found. */ | |
171 | ECTF_NOMEMBNAM, /* Member name not found. */ | |
172 | ECTF_RDONLY, /* CTF container is read-only. */ | |
173 | ECTF_DTFULL, /* CTF type is full (no more members allowed). */ | |
174 | ECTF_FULL, /* CTF container is full. */ | |
175 | ECTF_DUPLICATE, /* Duplicate member or variable name. */ | |
176 | ECTF_CONFLICT, /* Conflicting type definition present. */ | |
177 | ECTF_OVERROLLBACK, /* Attempt to roll back past a ctf_update. */ | |
178 | ECTF_COMPRESS, /* Failed to compress CTF data. */ | |
179 | ECTF_ARCREATE, /* Error creating CTF archive. */ | |
180 | ECTF_ARNNAME, /* Name not found in CTF archive. */ | |
181 | ECTF_SLICEOVERFLOW, /* Overflow of type bitness or offset in slice. */ | |
182 | ECTF_DUMPSECTUNKNOWN, /* Unknown section number in dump. */ | |
183 | ECTF_DUMPSECTCHANGED /* Section changed in middle of dump. */ | |
184 | }; | |
185 | ||
186 | /* The CTF data model is inferred to be the caller's data model or the data | |
187 | model of the given object, unless ctf_setmodel() is explicitly called. */ | |
188 | #define CTF_MODEL_ILP32 1 /* Object data model is ILP32. */ | |
189 | #define CTF_MODEL_LP64 2 /* Object data model is LP64. */ | |
190 | #ifdef _LP64 | |
191 | # define CTF_MODEL_NATIVE CTF_MODEL_LP64 | |
192 | #else | |
193 | # define CTF_MODEL_NATIVE CTF_MODEL_ILP32 | |
194 | #endif | |
195 | ||
196 | /* Dynamic CTF containers can be created using ctf_create(). The ctf_add_* | |
197 | routines can be used to add new definitions to the dynamic container. | |
198 | New types are labeled as root or non-root to determine whether they are | |
199 | visible at the top-level program scope when subsequently doing a lookup. */ | |
200 | ||
201 | #define CTF_ADD_NONROOT 0 /* Type only visible in nested scope. */ | |
202 | #define CTF_ADD_ROOT 1 /* Type visible at top-level scope. */ | |
203 | ||
9402cc59 NA |
204 | /* These typedefs are used to define the signature for callback functions |
205 | that can be used with the iteration and visit functions below. */ | |
206 | ||
316afdb1 NA |
207 | typedef int ctf_visit_f (const char *name, ctf_id_t type, unsigned long offset, |
208 | int depth, void *arg); | |
209 | typedef int ctf_member_f (const char *name, ctf_id_t membtype, | |
210 | unsigned long offset, void *arg); | |
211 | typedef int ctf_enum_f (const char *name, int val, void *arg); | |
212 | typedef int ctf_variable_f (const char *name, ctf_id_t type, void *arg); | |
213 | typedef int ctf_type_f (ctf_id_t type, void *arg); | |
6dbf2b73 NA |
214 | typedef int ctf_label_f (const char *name, const ctf_lblinfo_t *info, |
215 | void *arg); | |
9402cc59 NA |
216 | typedef int ctf_archive_member_f (ctf_file_t *fp, const char *name, void *arg); |
217 | typedef int ctf_archive_raw_member_f (const char *name, const void *content, | |
218 | size_t len, void *arg); | |
a30b3e18 NA |
219 | typedef char *ctf_dump_decorate_f (ctf_sect_names_t sect, |
220 | char *line, void *arg); | |
221 | ||
222 | typedef struct ctf_dump_state ctf_dump_state_t; | |
9402cc59 | 223 | |
143dce84 NA |
224 | /* Opening. These mostly return an abstraction over both CTF files and CTF |
225 | archives: so they can be used to open both. CTF files will appear to be an | |
226 | archive with one member named '.ctf'. The low-level functions | |
227 | ctf_simple_open() and ctf_bufopen() return ctf_file_t's directly, and cannot | |
228 | be used on CTF archives. */ | |
229 | ||
230 | extern ctf_archive_t *ctf_bfdopen (struct bfd *, int *); | |
231 | extern ctf_archive_t *ctf_bfdopen_ctfsect (struct bfd *, const ctf_sect_t *, | |
232 | int *); | |
233 | extern ctf_archive_t *ctf_fdopen (int fd, const char *filename, | |
234 | const char *target, int *errp); | |
235 | extern ctf_archive_t *ctf_open (const char *filename, | |
236 | const char *target, int *errp); | |
237 | extern void ctf_close (ctf_archive_t *); | |
72f33921 | 238 | extern ctf_sect_t ctf_getdatasect (const ctf_file_t *); |
9402cc59 | 239 | extern ctf_archive_t *ctf_get_arc (const ctf_file_t *); |
143dce84 | 240 | extern ctf_archive_t *ctf_arc_open (const char *, int *); |
9402cc59 NA |
241 | extern void ctf_arc_close (ctf_archive_t *); |
242 | extern ctf_file_t *ctf_arc_open_by_name (const ctf_archive_t *, | |
243 | const char *, int *); | |
244 | extern ctf_file_t *ctf_arc_open_by_name_sections (const ctf_archive_t *, | |
245 | const ctf_sect_t *, | |
246 | const ctf_sect_t *, | |
247 | const char *, int *); | |
248 | ||
249 | /* The next functions return or close real CTF files, or write out CTF archives, | |
250 | not opaque containers around either. */ | |
251 | ||
47d546f4 NA |
252 | extern ctf_file_t *ctf_simple_open (const char *, size_t, const char *, size_t, |
253 | size_t, const char *, size_t, int *); | |
254 | extern ctf_file_t *ctf_bufopen (const ctf_sect_t *, const ctf_sect_t *, | |
255 | const ctf_sect_t *, int *); | |
72f33921 NA |
256 | extern void ctf_file_close (ctf_file_t *); |
257 | ||
9402cc59 NA |
258 | extern int ctf_arc_write (const char *, ctf_file_t **, size_t, |
259 | const char **, size_t); | |
260 | ||
72f33921 NA |
261 | extern ctf_file_t *ctf_parent_file (ctf_file_t *); |
262 | extern const char *ctf_parent_name (ctf_file_t *); | |
263 | extern void ctf_parent_name_set (ctf_file_t *, const char *); | |
316afdb1 NA |
264 | extern int ctf_type_isparent (ctf_file_t *, ctf_id_t); |
265 | extern int ctf_type_ischild (ctf_file_t *, ctf_id_t); | |
72f33921 NA |
266 | |
267 | extern int ctf_import (ctf_file_t *, ctf_file_t *); | |
268 | extern int ctf_setmodel (ctf_file_t *, int); | |
269 | extern int ctf_getmodel (ctf_file_t *); | |
270 | ||
271 | extern void ctf_setspecific (ctf_file_t *, void *); | |
272 | extern void *ctf_getspecific (ctf_file_t *); | |
47d546f4 | 273 | |
479604f4 NA |
274 | extern int ctf_errno (ctf_file_t *); |
275 | extern const char *ctf_errmsg (int); | |
6c33b742 | 276 | extern int ctf_version (int); |
316afdb1 | 277 | |
b437bfe0 NA |
278 | extern int ctf_func_info (ctf_file_t *, unsigned long, ctf_funcinfo_t *); |
279 | extern int ctf_func_args (ctf_file_t *, unsigned long, uint32_t, ctf_id_t *); | |
280 | ||
281 | extern ctf_id_t ctf_lookup_by_name (ctf_file_t *, const char *); | |
282 | extern ctf_id_t ctf_lookup_by_symbol (ctf_file_t *, unsigned long); | |
283 | extern ctf_id_t ctf_lookup_variable (ctf_file_t *, const char *); | |
284 | ||
316afdb1 NA |
285 | extern ctf_id_t ctf_type_resolve (ctf_file_t *, ctf_id_t); |
286 | extern char *ctf_type_aname (ctf_file_t *, ctf_id_t); | |
287 | extern ssize_t ctf_type_lname (ctf_file_t *, ctf_id_t, char *, size_t); | |
288 | extern char *ctf_type_name (ctf_file_t *, ctf_id_t, char *, size_t); | |
289 | extern ssize_t ctf_type_size (ctf_file_t *, ctf_id_t); | |
290 | extern ssize_t ctf_type_align (ctf_file_t *, ctf_id_t); | |
291 | extern int ctf_type_kind (ctf_file_t *, ctf_id_t); | |
292 | extern ctf_id_t ctf_type_reference (ctf_file_t *, ctf_id_t); | |
293 | extern ctf_id_t ctf_type_pointer (ctf_file_t *, ctf_id_t); | |
294 | extern int ctf_type_encoding (ctf_file_t *, ctf_id_t, ctf_encoding_t *); | |
295 | extern int ctf_type_visit (ctf_file_t *, ctf_id_t, ctf_visit_f *, void *); | |
296 | extern int ctf_type_cmp (ctf_file_t *, ctf_id_t, ctf_file_t *, ctf_id_t); | |
297 | extern int ctf_type_compat (ctf_file_t *, ctf_id_t, ctf_file_t *, ctf_id_t); | |
298 | ||
299 | extern int ctf_member_info (ctf_file_t *, ctf_id_t, const char *, | |
300 | ctf_membinfo_t *); | |
301 | extern int ctf_array_info (ctf_file_t *, ctf_id_t, ctf_arinfo_t *); | |
302 | ||
303 | extern const char *ctf_enum_name (ctf_file_t *, ctf_id_t, int); | |
304 | extern int ctf_enum_value (ctf_file_t *, ctf_id_t, const char *, int *); | |
305 | ||
6dbf2b73 NA |
306 | extern void ctf_label_set (ctf_file_t *, const char *); |
307 | extern const char *ctf_label_get (ctf_file_t *); | |
308 | ||
309 | extern const char *ctf_label_topmost (ctf_file_t *); | |
310 | extern int ctf_label_info (ctf_file_t *, const char *, ctf_lblinfo_t *); | |
311 | ||
316afdb1 NA |
312 | extern int ctf_member_iter (ctf_file_t *, ctf_id_t, ctf_member_f *, void *); |
313 | extern int ctf_enum_iter (ctf_file_t *, ctf_id_t, ctf_enum_f *, void *); | |
314 | extern int ctf_type_iter (ctf_file_t *, ctf_type_f *, void *); | |
6dbf2b73 | 315 | extern int ctf_label_iter (ctf_file_t *, ctf_label_f *, void *); |
316afdb1 | 316 | extern int ctf_variable_iter (ctf_file_t *, ctf_variable_f *, void *); |
9402cc59 NA |
317 | extern int ctf_archive_iter (const ctf_archive_t *, ctf_archive_member_f *, |
318 | void *); | |
319 | /* This function alone does not currently operate on CTF files masquerading | |
320 | as archives, and returns -EINVAL: the raw data is no longer available. It is | |
321 | expected to be used only by archiving tools, in any case, which have no need | |
322 | to deal with non-archives at all. */ | |
323 | extern int ctf_archive_raw_iter (const ctf_archive_t *, | |
324 | ctf_archive_raw_member_f *, void *); | |
a30b3e18 NA |
325 | extern char *ctf_dump (ctf_file_t *, ctf_dump_state_t **state, |
326 | ctf_sect_names_t sect, ctf_dump_decorate_f *, | |
327 | void *arg); | |
316afdb1 | 328 | |
47d546f4 NA |
329 | extern ctf_id_t ctf_add_array (ctf_file_t *, uint32_t, |
330 | const ctf_arinfo_t *); | |
331 | extern ctf_id_t ctf_add_const (ctf_file_t *, uint32_t, ctf_id_t); | |
332 | extern ctf_id_t ctf_add_enum_encoded (ctf_file_t *, uint32_t, const char *, | |
333 | const ctf_encoding_t *); | |
334 | extern ctf_id_t ctf_add_enum (ctf_file_t *, uint32_t, const char *); | |
335 | extern ctf_id_t ctf_add_float (ctf_file_t *, uint32_t, | |
336 | const char *, const ctf_encoding_t *); | |
337 | extern ctf_id_t ctf_add_forward (ctf_file_t *, uint32_t, const char *, | |
338 | uint32_t); | |
339 | extern ctf_id_t ctf_add_function (ctf_file_t *, uint32_t, | |
340 | const ctf_funcinfo_t *, const ctf_id_t *); | |
341 | extern ctf_id_t ctf_add_integer (ctf_file_t *, uint32_t, const char *, | |
342 | const ctf_encoding_t *); | |
343 | extern ctf_id_t ctf_add_slice (ctf_file_t *, uint32_t, ctf_id_t, const ctf_encoding_t *); | |
344 | extern ctf_id_t ctf_add_pointer (ctf_file_t *, uint32_t, ctf_id_t); | |
345 | extern ctf_id_t ctf_add_type (ctf_file_t *, ctf_file_t *, ctf_id_t); | |
346 | extern ctf_id_t ctf_add_typedef (ctf_file_t *, uint32_t, const char *, | |
347 | ctf_id_t); | |
348 | extern ctf_id_t ctf_add_restrict (ctf_file_t *, uint32_t, ctf_id_t); | |
349 | extern ctf_id_t ctf_add_struct (ctf_file_t *, uint32_t, const char *); | |
350 | extern ctf_id_t ctf_add_union (ctf_file_t *, uint32_t, const char *); | |
351 | extern ctf_id_t ctf_add_struct_sized (ctf_file_t *, uint32_t, const char *, | |
352 | size_t); | |
353 | extern ctf_id_t ctf_add_union_sized (ctf_file_t *, uint32_t, const char *, | |
354 | size_t); | |
355 | extern ctf_id_t ctf_add_volatile (ctf_file_t *, uint32_t, ctf_id_t); | |
356 | ||
357 | extern int ctf_add_enumerator (ctf_file_t *, ctf_id_t, const char *, int); | |
358 | extern int ctf_add_member (ctf_file_t *, ctf_id_t, const char *, ctf_id_t); | |
359 | extern int ctf_add_member_offset (ctf_file_t *, ctf_id_t, const char *, | |
360 | ctf_id_t, unsigned long); | |
361 | extern int ctf_add_member_encoded (ctf_file_t *, ctf_id_t, const char *, | |
362 | ctf_id_t, unsigned long, | |
363 | const ctf_encoding_t); | |
364 | ||
365 | extern int ctf_add_variable (ctf_file_t *, const char *, ctf_id_t); | |
366 | ||
367 | extern int ctf_set_array (ctf_file_t *, ctf_id_t, const ctf_arinfo_t *); | |
368 | ||
369 | extern ctf_file_t *ctf_create (int *); | |
370 | extern int ctf_update (ctf_file_t *); | |
371 | extern ctf_snapshot_id_t ctf_snapshot (ctf_file_t *); | |
372 | extern int ctf_rollback (ctf_file_t *, ctf_snapshot_id_t); | |
373 | extern int ctf_discard (ctf_file_t *); | |
374 | extern int ctf_write (ctf_file_t *, int); | |
375 | extern int ctf_gzwrite (ctf_file_t * fp, gzFile fd); | |
376 | extern int ctf_compress_write (ctf_file_t * fp, int fd); | |
2e94b056 | 377 | |
60da9d95 NA |
378 | extern void ctf_setdebug (int debug); |
379 | extern int ctf_getdebug (void); | |
380 | ||
2e94b056 NA |
381 | #ifdef __cplusplus |
382 | } | |
383 | #endif | |
384 | ||
385 | #endif /* _CTF_API_H */ |