1 /* Public API to libctf.
2 Copyright (C) 2019 Free Software Foundation, Inc.
4 This file is part of libctf.
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
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.
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/>. */
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). */
27 #include <sys/param.h>
28 #include <sys/types.h>
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
42 These opaque definitions allow libctf to evolve without breaking clients. */
44 typedef struct ctf_file ctf_file_t
;
45 typedef struct ctf_archive_internal ctf_archive_t
;
46 typedef long ctf_id_t
;
48 /* This opaque definition allows libctf to accept BFD data structures without
49 importing all the BFD noise into users' namespaces. */
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().
57 The contents of this structure must always be in native endianness (no
58 byteswapping is performed). */
60 typedef struct ctf_sect
62 const char *cts_name
; /* Section name (if any). */
63 unsigned long cts_type
; /* Section type (ELF SHT_... value). */
64 unsigned long cts_flags
; /* Section flags (ELF SHF_... value). */
65 const void *cts_data
; /* Pointer to section data. */
66 size_t cts_size
; /* Size of data in bytes. */
67 size_t cts_entsize
; /* Size of each section entry (symtab only). */
68 off64_t cts_offset
; /* File offset of this section (if any). */
71 /* Symbolic names for CTF sections. */
73 typedef enum ctf_sect_names
84 /* Encoding information for integers, floating-point values, and certain other
85 intrinsics can be obtained by calling ctf_type_encoding(), below. The flags
86 field will contain values appropriate for the type defined in <ctf.h>. */
88 typedef struct ctf_encoding
90 uint32_t cte_format
; /* Data format (CTF_INT_* or CTF_FP_* flags). */
91 uint32_t cte_offset
; /* Offset of value in bits. */
92 uint32_t cte_bits
; /* Size of storage in bits. */
95 typedef struct ctf_membinfo
97 ctf_id_t ctm_type
; /* Type of struct or union member. */
98 unsigned long ctm_offset
; /* Offset of member in bits. */
101 typedef struct ctf_arinfo
103 ctf_id_t ctr_contents
; /* Type of array contents. */
104 ctf_id_t ctr_index
; /* Type of array index. */
105 uint32_t ctr_nelems
; /* Number of elements. */
108 typedef struct ctf_funcinfo
110 ctf_id_t ctc_return
; /* Function return type. */
111 uint32_t ctc_argc
; /* Number of typed arguments to function. */
112 uint32_t ctc_flags
; /* Function attributes (see below). */
115 typedef struct ctf_lblinfo
117 ctf_id_t ctb_type
; /* Last type associated with the label. */
120 typedef struct ctf_snapshot_id
122 unsigned long dtd_id
; /* Highest DTD ID at time of snapshot. */
123 unsigned long snapshot_id
; /* Snapshot id at time of snapshot. */
126 #define CTF_FUNC_VARARG 0x1 /* Function arguments end with varargs. */
128 /* Functions that return integer status or a ctf_id_t use the following value
129 to indicate failure. ctf_errno() can be used to obtain an error code. */
130 #define CTF_ERR (-1L)
132 #define ECTF_BASE 1000 /* Base value for libctf errnos. */
137 ECTF_FMT
= ECTF_BASE
, /* File is not in CTF or ELF format. */
138 ECTF_BFDERR
, /* BFD error. */
139 ECTF_CTFVERS
, /* CTF version is more recent than libctf. */
140 ECTF_BFD_AMBIGUOUS
, /* Ambiguous BFD target. */
141 ECTF_SYMTAB
, /* Symbol table uses invalid entry size. */
142 ECTF_SYMBAD
, /* Symbol table data buffer invalid. */
143 ECTF_STRBAD
, /* String table data buffer invalid. */
144 ECTF_CORRUPT
, /* File data corruption detected. */
145 ECTF_NOCTFDATA
, /* ELF file does not contain CTF data. */
146 ECTF_NOCTFBUF
, /* Buffer does not contain CTF data. */
147 ECTF_NOSYMTAB
, /* Symbol table data is not available. */
148 ECTF_NOPARENT
, /* Parent CTF container is not available. */
149 ECTF_DMODEL
, /* Data model mismatch. */
150 ECTF_UNUSED
, /* Unused error. */
151 ECTF_ZALLOC
, /* Failed to allocate (de)compression buffer. */
152 ECTF_DECOMPRESS
, /* Failed to decompress CTF data. */
153 ECTF_STRTAB
, /* String table for this string is missing. */
154 ECTF_BADNAME
, /* String offset is corrupt w.r.t. strtab. */
155 ECTF_BADID
, /* Invalid type ID number. */
156 ECTF_NOTSOU
, /* Type is not a struct or union. */
157 ECTF_NOTENUM
, /* Type is not an enum. */
158 ECTF_NOTSUE
, /* Type is not a struct, union, or enum. */
159 ECTF_NOTINTFP
, /* Type is not an integer, float, or enum. */
160 ECTF_NOTARRAY
, /* Type is not an array. */
161 ECTF_NOTREF
, /* Type does not reference another type. */
162 ECTF_NAMELEN
, /* Buffer is too small to hold type name. */
163 ECTF_NOTYPE
, /* No type found corresponding to name. */
164 ECTF_SYNTAX
, /* Syntax error in type name. */
165 ECTF_NOTFUNC
, /* Symtab entry does not refer to a function. */
166 ECTF_NOFUNCDAT
, /* No func info available for function. */
167 ECTF_NOTDATA
, /* Symtab entry does not refer to a data obj. */
168 ECTF_NOTYPEDAT
, /* No type info available for object. */
169 ECTF_NOLABEL
, /* No label found corresponding to name. */
170 ECTF_NOLABELDATA
, /* File does not contain any labels. */
171 ECTF_NOTSUP
, /* Feature not supported. */
172 ECTF_NOENUMNAM
, /* Enum element name not found. */
173 ECTF_NOMEMBNAM
, /* Member name not found. */
174 ECTF_RDONLY
, /* CTF container is read-only. */
175 ECTF_DTFULL
, /* CTF type is full (no more members allowed). */
176 ECTF_FULL
, /* CTF container is full. */
177 ECTF_DUPLICATE
, /* Duplicate member or variable name. */
178 ECTF_CONFLICT
, /* Conflicting type definition present. */
179 ECTF_OVERROLLBACK
, /* Attempt to roll back past a ctf_update. */
180 ECTF_COMPRESS
, /* Failed to compress CTF data. */
181 ECTF_ARCREATE
, /* Error creating CTF archive. */
182 ECTF_ARNNAME
, /* Name not found in CTF archive. */
183 ECTF_SLICEOVERFLOW
, /* Overflow of type bitness or offset in slice. */
184 ECTF_DUMPSECTUNKNOWN
, /* Unknown section number in dump. */
185 ECTF_DUMPSECTCHANGED
/* Section changed in middle of dump. */
188 /* The CTF data model is inferred to be the caller's data model or the data
189 model of the given object, unless ctf_setmodel() is explicitly called. */
190 #define CTF_MODEL_ILP32 1 /* Object data model is ILP32. */
191 #define CTF_MODEL_LP64 2 /* Object data model is LP64. */
193 # define CTF_MODEL_NATIVE CTF_MODEL_LP64
195 # define CTF_MODEL_NATIVE CTF_MODEL_ILP32
198 /* Dynamic CTF containers can be created using ctf_create(). The ctf_add_*
199 routines can be used to add new definitions to the dynamic container.
200 New types are labeled as root or non-root to determine whether they are
201 visible at the top-level program scope when subsequently doing a lookup. */
203 #define CTF_ADD_NONROOT 0 /* Type only visible in nested scope. */
204 #define CTF_ADD_ROOT 1 /* Type visible at top-level scope. */
206 /* These typedefs are used to define the signature for callback functions
207 that can be used with the iteration and visit functions below. */
209 typedef int ctf_visit_f (const char *name
, ctf_id_t type
, unsigned long offset
,
210 int depth
, void *arg
);
211 typedef int ctf_member_f (const char *name
, ctf_id_t membtype
,
212 unsigned long offset
, void *arg
);
213 typedef int ctf_enum_f (const char *name
, int val
, void *arg
);
214 typedef int ctf_variable_f (const char *name
, ctf_id_t type
, void *arg
);
215 typedef int ctf_type_f (ctf_id_t type
, void *arg
);
216 typedef int ctf_label_f (const char *name
, const ctf_lblinfo_t
*info
,
218 typedef int ctf_archive_member_f (ctf_file_t
*fp
, const char *name
, void *arg
);
219 typedef int ctf_archive_raw_member_f (const char *name
, const void *content
,
220 size_t len
, void *arg
);
222 /* Opening. These mostly return an abstraction over both CTF files and CTF
223 archives: so they can be used to open both. CTF files will appear to be an
224 archive with one member named '.ctf'. The low-level functions
225 ctf_simple_open() and ctf_bufopen() return ctf_file_t's directly, and cannot
226 be used on CTF archives. */
228 extern ctf_archive_t
*ctf_bfdopen (struct bfd
*, int *);
229 extern ctf_archive_t
*ctf_bfdopen_ctfsect (struct bfd
*, const ctf_sect_t
*,
231 extern ctf_archive_t
*ctf_fdopen (int fd
, const char *filename
,
232 const char *target
, int *errp
);
233 extern ctf_archive_t
*ctf_open (const char *filename
,
234 const char *target
, int *errp
);
235 extern void ctf_close (ctf_archive_t
*);
236 extern ctf_sect_t
ctf_getdatasect (const ctf_file_t
*);
237 extern ctf_archive_t
*ctf_get_arc (const ctf_file_t
*);
238 extern ctf_archive_t
*ctf_arc_open (const char *, int *);
239 extern void ctf_arc_close (ctf_archive_t
*);
240 extern ctf_file_t
*ctf_arc_open_by_name (const ctf_archive_t
*,
241 const char *, int *);
242 extern ctf_file_t
*ctf_arc_open_by_name_sections (const ctf_archive_t
*,
245 const char *, int *);
247 /* The next functions return or close real CTF files, or write out CTF archives,
248 not opaque containers around either. */
250 extern ctf_file_t
*ctf_simple_open (const char *, size_t, const char *, size_t,
251 size_t, const char *, size_t, int *);
252 extern ctf_file_t
*ctf_bufopen (const ctf_sect_t
*, const ctf_sect_t
*,
253 const ctf_sect_t
*, int *);
254 extern void ctf_file_close (ctf_file_t
*);
256 extern int ctf_arc_write (const char *, ctf_file_t
**, size_t,
257 const char **, size_t);
259 extern ctf_file_t
*ctf_parent_file (ctf_file_t
*);
260 extern const char *ctf_parent_name (ctf_file_t
*);
261 extern void ctf_parent_name_set (ctf_file_t
*, const char *);
262 extern int ctf_type_isparent (ctf_file_t
*, ctf_id_t
);
263 extern int ctf_type_ischild (ctf_file_t
*, ctf_id_t
);
265 extern int ctf_import (ctf_file_t
*, ctf_file_t
*);
266 extern int ctf_setmodel (ctf_file_t
*, int);
267 extern int ctf_getmodel (ctf_file_t
*);
269 extern void ctf_setspecific (ctf_file_t
*, void *);
270 extern void *ctf_getspecific (ctf_file_t
*);
272 extern int ctf_errno (ctf_file_t
*);
273 extern const char *ctf_errmsg (int);
274 extern int ctf_version (int);
276 extern int ctf_func_info (ctf_file_t
*, unsigned long, ctf_funcinfo_t
*);
277 extern int ctf_func_args (ctf_file_t
*, unsigned long, uint32_t, ctf_id_t
*);
279 extern ctf_id_t
ctf_lookup_by_name (ctf_file_t
*, const char *);
280 extern ctf_id_t
ctf_lookup_by_symbol (ctf_file_t
*, unsigned long);
281 extern ctf_id_t
ctf_lookup_variable (ctf_file_t
*, const char *);
283 extern ctf_id_t
ctf_type_resolve (ctf_file_t
*, ctf_id_t
);
284 extern char *ctf_type_aname (ctf_file_t
*, ctf_id_t
);
285 extern ssize_t
ctf_type_lname (ctf_file_t
*, ctf_id_t
, char *, size_t);
286 extern char *ctf_type_name (ctf_file_t
*, ctf_id_t
, char *, size_t);
287 extern ssize_t
ctf_type_size (ctf_file_t
*, ctf_id_t
);
288 extern ssize_t
ctf_type_align (ctf_file_t
*, ctf_id_t
);
289 extern int ctf_type_kind (ctf_file_t
*, ctf_id_t
);
290 extern ctf_id_t
ctf_type_reference (ctf_file_t
*, ctf_id_t
);
291 extern ctf_id_t
ctf_type_pointer (ctf_file_t
*, ctf_id_t
);
292 extern int ctf_type_encoding (ctf_file_t
*, ctf_id_t
, ctf_encoding_t
*);
293 extern int ctf_type_visit (ctf_file_t
*, ctf_id_t
, ctf_visit_f
*, void *);
294 extern int ctf_type_cmp (ctf_file_t
*, ctf_id_t
, ctf_file_t
*, ctf_id_t
);
295 extern int ctf_type_compat (ctf_file_t
*, ctf_id_t
, ctf_file_t
*, ctf_id_t
);
297 extern int ctf_member_info (ctf_file_t
*, ctf_id_t
, const char *,
299 extern int ctf_array_info (ctf_file_t
*, ctf_id_t
, ctf_arinfo_t
*);
301 extern const char *ctf_enum_name (ctf_file_t
*, ctf_id_t
, int);
302 extern int ctf_enum_value (ctf_file_t
*, ctf_id_t
, const char *, int *);
304 extern void ctf_label_set (ctf_file_t
*, const char *);
305 extern const char *ctf_label_get (ctf_file_t
*);
307 extern const char *ctf_label_topmost (ctf_file_t
*);
308 extern int ctf_label_info (ctf_file_t
*, const char *, ctf_lblinfo_t
*);
310 extern int ctf_member_iter (ctf_file_t
*, ctf_id_t
, ctf_member_f
*, void *);
311 extern int ctf_enum_iter (ctf_file_t
*, ctf_id_t
, ctf_enum_f
*, void *);
312 extern int ctf_type_iter (ctf_file_t
*, ctf_type_f
*, void *);
313 extern int ctf_label_iter (ctf_file_t
*, ctf_label_f
*, void *);
314 extern int ctf_variable_iter (ctf_file_t
*, ctf_variable_f
*, void *);
315 extern int ctf_archive_iter (const ctf_archive_t
*, ctf_archive_member_f
*,
317 /* This function alone does not currently operate on CTF files masquerading
318 as archives, and returns -EINVAL: the raw data is no longer available. It is
319 expected to be used only by archiving tools, in any case, which have no need
320 to deal with non-archives at all. */
321 extern int ctf_archive_raw_iter (const ctf_archive_t
*,
322 ctf_archive_raw_member_f
*, void *);
324 extern ctf_id_t
ctf_add_array (ctf_file_t
*, uint32_t,
325 const ctf_arinfo_t
*);
326 extern ctf_id_t
ctf_add_const (ctf_file_t
*, uint32_t, ctf_id_t
);
327 extern ctf_id_t
ctf_add_enum_encoded (ctf_file_t
*, uint32_t, const char *,
328 const ctf_encoding_t
*);
329 extern ctf_id_t
ctf_add_enum (ctf_file_t
*, uint32_t, const char *);
330 extern ctf_id_t
ctf_add_float (ctf_file_t
*, uint32_t,
331 const char *, const ctf_encoding_t
*);
332 extern ctf_id_t
ctf_add_forward (ctf_file_t
*, uint32_t, const char *,
334 extern ctf_id_t
ctf_add_function (ctf_file_t
*, uint32_t,
335 const ctf_funcinfo_t
*, const ctf_id_t
*);
336 extern ctf_id_t
ctf_add_integer (ctf_file_t
*, uint32_t, const char *,
337 const ctf_encoding_t
*);
338 extern ctf_id_t
ctf_add_slice (ctf_file_t
*, uint32_t, ctf_id_t
, const ctf_encoding_t
*);
339 extern ctf_id_t
ctf_add_pointer (ctf_file_t
*, uint32_t, ctf_id_t
);
340 extern ctf_id_t
ctf_add_type (ctf_file_t
*, ctf_file_t
*, ctf_id_t
);
341 extern ctf_id_t
ctf_add_typedef (ctf_file_t
*, uint32_t, const char *,
343 extern ctf_id_t
ctf_add_restrict (ctf_file_t
*, uint32_t, ctf_id_t
);
344 extern ctf_id_t
ctf_add_struct (ctf_file_t
*, uint32_t, const char *);
345 extern ctf_id_t
ctf_add_union (ctf_file_t
*, uint32_t, const char *);
346 extern ctf_id_t
ctf_add_struct_sized (ctf_file_t
*, uint32_t, const char *,
348 extern ctf_id_t
ctf_add_union_sized (ctf_file_t
*, uint32_t, const char *,
350 extern ctf_id_t
ctf_add_volatile (ctf_file_t
*, uint32_t, ctf_id_t
);
352 extern int ctf_add_enumerator (ctf_file_t
*, ctf_id_t
, const char *, int);
353 extern int ctf_add_member (ctf_file_t
*, ctf_id_t
, const char *, ctf_id_t
);
354 extern int ctf_add_member_offset (ctf_file_t
*, ctf_id_t
, const char *,
355 ctf_id_t
, unsigned long);
356 extern int ctf_add_member_encoded (ctf_file_t
*, ctf_id_t
, const char *,
357 ctf_id_t
, unsigned long,
358 const ctf_encoding_t
);
360 extern int ctf_add_variable (ctf_file_t
*, const char *, ctf_id_t
);
362 extern int ctf_set_array (ctf_file_t
*, ctf_id_t
, const ctf_arinfo_t
*);
364 extern ctf_file_t
*ctf_create (int *);
365 extern int ctf_update (ctf_file_t
*);
366 extern ctf_snapshot_id_t
ctf_snapshot (ctf_file_t
*);
367 extern int ctf_rollback (ctf_file_t
*, ctf_snapshot_id_t
);
368 extern int ctf_discard (ctf_file_t
*);
369 extern int ctf_write (ctf_file_t
*, int);
370 extern int ctf_gzwrite (ctf_file_t
* fp
, gzFile fd
);
371 extern int ctf_compress_write (ctf_file_t
* fp
, int fd
);
373 extern void ctf_setdebug (int debug
);
374 extern int ctf_getdebug (void);
380 #endif /* _CTF_API_H */