aboutsummaryrefslogtreecommitdiff
path: root/libctf/ctf-types.c
diff options
context:
space:
mode:
authorNick Alcock <nick.alcock@oracle.com>2021-01-05 13:25:56 +0000
committerNick Alcock <nick.alcock@oracle.com>2021-01-05 14:53:40 +0000
commit6c3a38777b38a2ad87e2b2bcec4567578d1c83ec (patch)
tree92e04a38161e85109281a2b3579036ca663865d1 /libctf/ctf-types.c
parentabed0b0718a6a9cd24cc68fb1f73baf6b31d8ff4 (diff)
downloadgdb-6c3a38777b38a2ad87e2b2bcec4567578d1c83ec.zip
gdb-6c3a38777b38a2ad87e2b2bcec4567578d1c83ec.tar.gz
gdb-6c3a38777b38a2ad87e2b2bcec4567578d1c83ec.tar.bz2
libctf, include: support unnamed structure members better
libctf has no intrinsic support for the GCC unnamed structure member extension. This principally means that you can't look up named members inside unnamed struct or union members via ctf_member_info: you have to tiresomely find out the type ID of the unnamed members via iteration, then look in each of these. This is ridiculous. Fix it by extending ctf_member_info so that it recurses into unnamed members for you: this is still unambiguous because GCC won't let you create ambiguously-named members even in the presence of this extension. For consistency, and because the release hasn't happened and we can still do this, break the ctf_member_next API and add flags: we specify one flag, CTF_MN_RECURSE, which if set causes ctf_member_next to automatically recurse into unnamed members for you, returning not only the members themselves but all their contained members, so that you can use ctf_member_next to identify every member that it would be valid to call ctf_member_info with. New lookup tests are added for all of this. include/ChangeLog 2021-01-05 Nick Alcock <nick.alcock@oracle.com> * ctf-api.h (CTF_MN_RECURSE): New. (ctf_member_next): Add flags argument. libctf/ChangeLog 2021-01-05 Nick Alcock <nick.alcock@oracle.com> * ctf-impl.h (struct ctf_next) <u.ctn_next>: Move to... <ctn_next>: ... here. * ctf-util.c (ctf_next_destroy): Unconditionally destroy it. * ctf-lookup.c (ctf_symbol_next): Adjust accordingly. * ctf-types.c (ctf_member_iter): Reimplement in terms of... (ctf_member_next): ... this. Support recursive unnamed member iteration (off by default). (ctf_member_info): Look up members in unnamed sub-structs. * ctf-dedup.c (ctf_dedup_rhash_type): Adjust ctf_member_next call. (ctf_dedup_emit_struct_members): Likewise. * testsuite/libctf-lookup/struct-iteration-ctf.c: Test empty unnamed members, and a normal member after the end. * testsuite/libctf-lookup/struct-iteration.c: Verify that ctf_member_count is consistent with the number of successful returns from a non-recursive ctf_member_next. * testsuite/libctf-lookup/struct-iteration-*: New, test iteration over struct members. * testsuite/libctf-lookup/struct-lookup.c: New test. * testsuite/libctf-lookup/struct-lookup.lk: New test.
Diffstat (limited to 'libctf/ctf-types.c')
-rw-r--r--libctf/ctf-types.c227
1 files changed, 138 insertions, 89 deletions
diff --git a/libctf/ctf-types.c b/libctf/ctf-types.c
index a3d824b..6275be0 100644
--- a/libctf/ctf-types.c
+++ b/libctf/ctf-types.c
@@ -41,76 +41,33 @@ ctf_type_ischild (ctf_dict_t * fp, ctf_id_t id)
int
ctf_member_iter (ctf_dict_t *fp, ctf_id_t type, ctf_member_f *func, void *arg)
{
- ctf_dict_t *ofp = fp;
- const ctf_type_t *tp;
- ctf_dtdef_t *dtd;
- ssize_t size, increment;
- uint32_t kind, n;
+ ctf_next_t *i = NULL;
+ ssize_t offset;
+ const char *name;
+ ctf_id_t membtype;
int rc;
- if ((type = ctf_type_resolve (fp, type)) == CTF_ERR)
- return -1; /* errno is set for us. */
-
- if ((tp = ctf_lookup_by_id (&fp, type)) == NULL)
- return -1; /* errno is set for us. */
-
- (void) ctf_get_ctt_size (fp, tp, &size, &increment);
- kind = LCTF_INFO_KIND (fp, tp->ctt_info);
-
- if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
- return (ctf_set_errno (ofp, ECTF_NOTSOU));
-
- if ((dtd = ctf_dynamic_type (fp, type)) == NULL)
+ while ((offset = ctf_member_next (fp, type, &i, &name, &membtype, 0)) >= 0)
{
- if (size < CTF_LSTRUCT_THRESH)
- {
- const ctf_member_t *mp = (const ctf_member_t *) ((uintptr_t) tp +
- increment);
-
- for (n = LCTF_INFO_VLEN (fp, tp->ctt_info); n != 0; n--, mp++)
- {
- const char *name = ctf_strptr (fp, mp->ctm_name);
- if ((rc = func (name, mp->ctm_type, mp->ctm_offset, arg)) != 0)
- return rc;
- }
- }
- else
- {
- const ctf_lmember_t *lmp = (const ctf_lmember_t *) ((uintptr_t) tp +
- increment);
-
- for (n = LCTF_INFO_VLEN (fp, tp->ctt_info); n != 0; n--, lmp++)
- {
- const char *name = ctf_strptr (fp, lmp->ctlm_name);
- if ((rc = func (name, lmp->ctlm_type,
- (unsigned long) CTF_LMEM_OFFSET (lmp), arg)) != 0)
- return rc;
- }
- }
- }
- else
- {
- ctf_dmdef_t *dmd;
-
- for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
- dmd != NULL; dmd = ctf_list_next (dmd))
+ if ((rc = func (name, membtype, offset, arg)) != 0)
{
- if ((rc = func (dmd->dmd_name, dmd->dmd_type,
- dmd->dmd_offset, arg)) != 0)
- return rc;
+ ctf_next_destroy (i);
+ return rc;
}
}
+ if (ctf_errno (fp) != ECTF_NEXT_END)
+ return -1; /* errno is set for us. */
return 0;
}
/* Iterate over the members of a STRUCT or UNION, returning each member's
offset and optionally name and member type in turn. On end-of-iteration,
- returns -1. */
+ returns -1. If FLAGS is CTF_MN_RECURSE, recurse into unnamed members. */
ssize_t
ctf_member_next (ctf_dict_t *fp, ctf_id_t type, ctf_next_t **it,
- const char **name, ctf_id_t *membtype)
+ const char **name, ctf_id_t *membtype, int flags)
{
ctf_dict_t *ofp = fp;
uint32_t kind;
@@ -121,6 +78,7 @@ ctf_member_next (ctf_dict_t *fp, ctf_id_t type, ctf_next_t **it,
{
const ctf_type_t *tp;
ctf_dtdef_t *dtd;
+ ssize_t increment;
if ((type = ctf_type_resolve (fp, type)) == CTF_ERR)
return -1; /* errno is set for us. */
@@ -132,8 +90,7 @@ ctf_member_next (ctf_dict_t *fp, ctf_id_t type, ctf_next_t **it,
return ctf_set_errno (ofp, ENOMEM);
i->cu.ctn_fp = ofp;
- (void) ctf_get_ctt_size (fp, tp, &i->ctn_size,
- &i->ctn_increment);
+ (void) ctf_get_ctt_size (fp, tp, &i->ctn_size, &increment);
kind = LCTF_INFO_KIND (fp, tp->ctt_info);
if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
@@ -156,11 +113,9 @@ ctf_member_next (ctf_dict_t *fp, ctf_id_t type, ctf_next_t **it,
i->ctn_n = LCTF_INFO_VLEN (fp, tp->ctt_info);
if (i->ctn_size < CTF_LSTRUCT_THRESH)
- i->u.ctn_mp = (const ctf_member_t *) ((uintptr_t) tp +
- i->ctn_increment);
+ i->u.ctn_mp = (const ctf_member_t *) ((uintptr_t) tp + increment);
else
- i->u.ctn_lmp = (const ctf_lmember_t *) ((uintptr_t) tp +
- i->ctn_increment);
+ i->u.ctn_lmp = (const ctf_lmember_t *) ((uintptr_t) tp + increment);
}
else
i->u.ctn_dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
@@ -178,41 +133,112 @@ ctf_member_next (ctf_dict_t *fp, ctf_id_t type, ctf_next_t **it,
if ((fp = ctf_get_dict (ofp, type)) == NULL)
return (ctf_set_errno (ofp, ECTF_NOPARENT));
- if (!(fp->ctf_flags & LCTF_RDWR))
- {
- if (i->ctn_n == 0)
- goto end_iter;
+ /* When we hit an unnamed struct/union member, we set ctn_type to indicate
+ that we are inside one, then return the unnamed member: on the next call,
+ we must skip over top-level member iteration in favour of iteration within
+ the sub-struct until it later turns out that that iteration has ended. */
- if (i->ctn_size < CTF_LSTRUCT_THRESH)
+ retry:
+ if (!i->ctn_type)
+ {
+ if (!(fp->ctf_flags & LCTF_RDWR))
{
- if (name)
- *name = ctf_strptr (fp, i->u.ctn_mp->ctm_name);
- if (membtype)
- *membtype = i->u.ctn_mp->ctm_type;
- offset = i->u.ctn_mp->ctm_offset;
- i->u.ctn_mp++;
+ if (i->ctn_n == 0)
+ goto end_iter;
+
+ if (i->ctn_size < CTF_LSTRUCT_THRESH)
+ {
+ const char *membname = ctf_strptr (fp, i->u.ctn_mp->ctm_name);
+
+ if (name)
+ *name = membname;
+ if (membtype)
+ *membtype = i->u.ctn_mp->ctm_type;
+ offset = i->u.ctn_mp->ctm_offset;
+
+ if (membname[0] == 0
+ && (ctf_type_kind (fp, i->u.ctn_mp->ctm_type) == CTF_K_STRUCT
+ || ctf_type_kind (fp, i->u.ctn_mp->ctm_type) == CTF_K_UNION))
+ i->ctn_type = i->u.ctn_mp->ctm_type;
+
+ i->u.ctn_mp++;
+ }
+ else
+ {
+ const char *membname = ctf_strptr (fp, i->u.ctn_lmp->ctlm_name);
+
+ if (name)
+ *name = membname;
+ if (membtype)
+ *membtype = i->u.ctn_lmp->ctlm_type;
+ offset = (unsigned long) CTF_LMEM_OFFSET (i->u.ctn_lmp);
+
+ if (membname[0] == 0
+ && (ctf_type_kind (fp, i->u.ctn_lmp->ctlm_type) == CTF_K_STRUCT
+ || ctf_type_kind (fp, i->u.ctn_lmp->ctlm_type) == CTF_K_UNION))
+ i->ctn_type = i->u.ctn_lmp->ctlm_type;
+
+ i->u.ctn_lmp++;
+ }
+ i->ctn_n--;
}
else
{
+ if (i->u.ctn_dmd == NULL)
+ goto end_iter;
+ /* The dmd contains a NULL for unnamed dynamic members. Don't inflict
+ this on our callers. */
if (name)
- *name = ctf_strptr (fp, i->u.ctn_lmp->ctlm_name);
+ {
+ if (i->u.ctn_dmd->dmd_name)
+ *name = i->u.ctn_dmd->dmd_name;
+ else
+ *name = "";
+ }
if (membtype)
- *membtype = i->u.ctn_lmp->ctlm_type;
- offset = (unsigned long) CTF_LMEM_OFFSET (i->u.ctn_lmp);
- i->u.ctn_lmp++;
+ *membtype = i->u.ctn_dmd->dmd_type;
+ offset = i->u.ctn_dmd->dmd_offset;
+
+ if (i->u.ctn_dmd->dmd_name == NULL
+ && (ctf_type_kind (fp, i->u.ctn_dmd->dmd_type) == CTF_K_STRUCT
+ || ctf_type_kind (fp, i->u.ctn_dmd->dmd_type) == CTF_K_UNION))
+ i->ctn_type = i->u.ctn_dmd->dmd_type;
+
+ i->u.ctn_dmd = ctf_list_next (i->u.ctn_dmd);
}
- i->ctn_n--;
+
+ /* The callers might want automatic recursive sub-struct traversal. */
+ if (!(flags & CTF_MN_RECURSE))
+ i->ctn_type = 0;
+
+ /* Sub-struct traversal starting? Take note of the offset of this member,
+ for later boosting of sub-struct members' offsets. */
+ if (i->ctn_type)
+ i->ctn_increment = offset;
}
+ /* Traversing a sub-struct? Just return it, with the offset adjusted. */
else
{
- if (i->u.ctn_dmd == NULL)
- goto end_iter;
- if (name)
- *name = i->u.ctn_dmd->dmd_name;
- if (membtype)
- *membtype = i->u.ctn_dmd->dmd_type;
- offset = i->u.ctn_dmd->dmd_offset;
- i->u.ctn_dmd = ctf_list_next (i->u.ctn_dmd);
+ ssize_t ret = ctf_member_next (fp, i->ctn_type, &i->ctn_next, name,
+ membtype, flags);
+
+ if (ret >= 0)
+ return ret + i->ctn_increment;
+
+ if (ctf_errno (fp) != ECTF_NEXT_END)
+ {
+ ctf_next_destroy (i);
+ *it = NULL;
+ i->ctn_type = 0;
+ return ret; /* errno is set for us. */
+ }
+
+ if (!ctf_assert (fp, (i->ctn_next == NULL)))
+ return -1; /* errno is set for us. */
+
+ i->ctn_type = 0;
+ /* This sub-struct has ended: on to the next real member. */
+ goto retry;
}
return offset;
@@ -1377,7 +1403,7 @@ ctf_type_compat (ctf_dict_t *lfp, ctf_id_t ltype,
}
/* Return the number of members in a STRUCT or UNION, or the number of
- enumerators in an ENUM. */
+ enumerators in an ENUM. The count does not include unnamed sub-members. */
int
ctf_member_count (ctf_dict_t *fp, ctf_id_t type)
@@ -1433,7 +1459,15 @@ ctf_member_info (ctf_dict_t *fp, ctf_id_t type, const char *name,
for (n = LCTF_INFO_VLEN (fp, tp->ctt_info); n != 0; n--, mp++)
{
- if (strcmp (ctf_strptr (fp, mp->ctm_name), name) == 0)
+ const char *membname = ctf_strptr (fp, mp->ctm_name);
+
+ if (membname[0] == 0
+ && (ctf_type_kind (fp, mp->ctm_type) == CTF_K_STRUCT
+ || ctf_type_kind (fp, mp->ctm_type) == CTF_K_UNION)
+ && (ctf_member_info (fp, mp->ctm_type, name, mip) == 0))
+ return 0;
+
+ if (strcmp (membname, name) == 0)
{
mip->ctm_type = mp->ctm_type;
mip->ctm_offset = mp->ctm_offset;
@@ -1448,7 +1482,15 @@ ctf_member_info (ctf_dict_t *fp, ctf_id_t type, const char *name,
for (n = LCTF_INFO_VLEN (fp, tp->ctt_info); n != 0; n--, lmp++)
{
- if (strcmp (ctf_strptr (fp, lmp->ctlm_name), name) == 0)
+ const char *membname = ctf_strptr (fp, lmp->ctlm_name);
+
+ if (membname[0] == 0
+ && (ctf_type_kind (fp, lmp->ctlm_type) == CTF_K_STRUCT
+ || ctf_type_kind (fp, lmp->ctlm_type) == CTF_K_UNION)
+ && (ctf_member_info (fp, lmp->ctlm_type, name, mip) == 0))
+ return 0;
+
+ if (strcmp (membname, name) == 0)
{
mip->ctm_type = lmp->ctlm_type;
mip->ctm_offset = (unsigned long) CTF_LMEM_OFFSET (lmp);
@@ -1464,7 +1506,14 @@ ctf_member_info (ctf_dict_t *fp, ctf_id_t type, const char *name,
for (dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
dmd != NULL; dmd = ctf_list_next (dmd))
{
- if (strcmp (dmd->dmd_name, name) == 0)
+ if (dmd->dmd_name == NULL
+ && (ctf_type_kind (fp, dmd->dmd_type) == CTF_K_STRUCT
+ || ctf_type_kind (fp, dmd->dmd_type) == CTF_K_UNION)
+ && (ctf_member_info (fp, dmd->dmd_type, name, mip) == 0))
+ return 0;
+
+ if (dmd->dmd_name != NULL
+ && strcmp (dmd->dmd_name, name) == 0)
{
mip->ctm_type = dmd->dmd_type;
mip->ctm_offset = dmd->dmd_offset;