diff options
author | Nick Alcock <nick.alcock@oracle.com> | 2019-04-23 22:45:46 +0100 |
---|---|---|
committer | Nick Alcock <nick.alcock@oracle.com> | 2019-05-28 17:07:40 +0100 |
commit | 47d546f427d0d3bf9f503b5b118ae05b49d73d28 (patch) | |
tree | 69f964b1297b2260e92412ca31f8941159ddebfe /libctf/ctf-lookup.c | |
parent | a5be9bbe896d4adb6077e9bb2ebcaaa4426bdde1 (diff) | |
download | gdb-47d546f427d0d3bf9f503b5b118ae05b49d73d28.zip gdb-47d546f427d0d3bf9f503b5b118ae05b49d73d28.tar.gz gdb-47d546f427d0d3bf9f503b5b118ae05b49d73d28.tar.bz2 |
libctf: creation functions
The CTF creation process looks roughly like (error handling elided):
int err;
ctf_file_t *foo = ctf_create (&err);
ctf_id_t type = ctf_add_THING (foo, ...);
ctf_update (foo);
ctf_*write (...);
Some ctf_add_THING functions accept other type IDs as arguments,
depending on the type: cv-quals, pointers, and structure and union
members all take other types as arguments. So do 'slices', which
let you take an existing integral type and recast it as a type
with a different bitness or offset within a byte, for bitfields.
One class of THING is not a type: "variables", which are mappings
of names (in the internal string table) to types. These are mostly
useful when encoding variables that do not appear in a symbol table
but which some external user has some other way to figure out the
address of at runtime (dynamic symbol lookup or querying a VM
interpreter or something).
You can snapshot the creation process at any point: rolling back to a
snapshot deletes all types and variables added since that point.
You can make arbitrary type queries on the CTF container during the
creation process, but you must call ctf_update() first, which
translates the growing dynamic container into a static one (this uses
the CTF opening machinery, added in a later commit), which is quite
expensive. This function must also be called after adding types
and before writing the container out.
Because addition of types involves looking up existing types, we add a
little of the type lookup machinery here, as well: only enough to
look up types in dynamic containers under construction.
libctf/
* ctf-create.c: New file.
* ctf-lookup.c: New file.
include/
* ctf-api.h (zlib.h): New include.
(ctf_sect_t): New.
(ctf_sect_names_t): Likewise.
(ctf_encoding_t): Likewise.
(ctf_membinfo_t): Likewise.
(ctf_arinfo_t): Likewise.
(ctf_funcinfo_t): Likewise.
(ctf_lblinfo_t): Likewise.
(ctf_snapshot_id_t): Likewise.
(CTF_FUNC_VARARG): Likewise.
(ctf_simple_open): Likewise.
(ctf_bufopen): Likewise.
(ctf_create): Likewise.
(ctf_add_array): Likewise.
(ctf_add_const): Likewise.
(ctf_add_enum_encoded): Likewise.
(ctf_add_enum): Likewise.
(ctf_add_float): Likewise.
(ctf_add_forward): Likewise.
(ctf_add_function): Likewise.
(ctf_add_integer): Likewise.
(ctf_add_slice): Likewise.
(ctf_add_pointer): Likewise.
(ctf_add_type): Likewise.
(ctf_add_typedef): Likewise.
(ctf_add_restrict): Likewise.
(ctf_add_struct): Likewise.
(ctf_add_union): Likewise.
(ctf_add_struct_sized): Likewise.
(ctf_add_union_sized): Likewise.
(ctf_add_volatile): Likewise.
(ctf_add_enumerator): Likewise.
(ctf_add_member): Likewise.
(ctf_add_member_offset): Likewise.
(ctf_add_member_encoded): Likewise.
(ctf_add_variable): Likewise.
(ctf_set_array): Likewise.
(ctf_update): Likewise.
(ctf_snapshot): Likewise.
(ctf_rollback): Likewise.
(ctf_discard): Likewise.
(ctf_write): Likewise.
(ctf_gzwrite): Likewise.
(ctf_compress_write): Likewise.
Diffstat (limited to 'libctf/ctf-lookup.c')
-rw-r--r-- | libctf/ctf-lookup.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/libctf/ctf-lookup.c b/libctf/ctf-lookup.c new file mode 100644 index 0000000..e76afb6 --- /dev/null +++ b/libctf/ctf-lookup.c @@ -0,0 +1,63 @@ +/* Type lookup. + Copyright (C) 2019 Free Software Foundation, Inc. + + This file is part of libctf. + + libctf 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, 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; see the file COPYING. If not see + <http://www.gnu.org/licenses/>. */ + +#include <ctf-impl.h> +#include <elf.h> +#include <string.h> + +/* Return the pointer to the internal CTF type data corresponding to the + given type ID. If the ID is invalid, the function returns NULL. + This function is not exported outside of the library. */ + +const ctf_type_t * +ctf_lookup_by_id (ctf_file_t **fpp, ctf_id_t type) +{ + ctf_file_t *fp = *fpp; /* Caller passes in starting CTF container. */ + ctf_id_t idx; + + if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT (fp, type) + && (fp = fp->ctf_parent) == NULL) + { + (void) ctf_set_errno (*fpp, ECTF_NOPARENT); + return NULL; + } + + idx = LCTF_TYPE_TO_INDEX (fp, type); + if (idx > 0 && (unsigned long) idx <= fp->ctf_typemax) + { + *fpp = fp; /* Function returns ending CTF container. */ + return (LCTF_INDEX_TO_TYPEPTR (fp, idx)); + } + + /* If this container is writable, check for a dynamic type. */ + + if (fp->ctf_flags & LCTF_RDWR) + { + ctf_dtdef_t *dtd; + + if ((dtd = ctf_dynamic_type (fp, type)) != NULL) + { + *fpp = fp; + return &dtd->dtd_data; + } + } + (void) ctf_set_errno (*fpp, ECTF_BADID); + return NULL; +} + |