From 9f8be1f638feede27a975f3c89ac5be68f0d5eea Mon Sep 17 00:00:00 2001 From: Nick Alcock Date: Wed, 3 Dec 2025 15:05:56 +0000 Subject: [PATCH] libctf: error code reduction (still underway) This shows the fruits of the recent ctf_err rework: the fusion of a bunch of error codes for which we had different error codes solely so that users could see different output in the ctf_errmsg(). Now that we have an error-handling scheme which can produce the function name and arbitrary extra printf()ed strings, we can merge a whole bunch of these codes together, making the codes purely semantic and much less granular, since we only really care that an error code is unambiguous with respect to any given API function (e.g. if ctf_add_forward() and ctf_member_count() both return ECTF_WRONGTYPE, you know perfectly well what it means in each case, even though it means something quite different: you can't make a forward to nonforwardable types like ints, and you can't count the members of a basic type.) There is one tiny semantic change of sorts: ctf_member_info(), ctf_enum_name() and ctf_enum_value() used to set an error if the name specified wasn't found. This is a fairly expected case, so we change it to a warning. A message is still logged on the error/warning stream, and ECTF_NONAME is set as the ctf_errno; if the caller handles it, they can use ctf_errwarning_remove() to remove the most ECTF_NONAME from the stream once they've done so. (Among all the removed ECTF_NOT* error codes, we have kept one, ECTF_NOTREF, because it is actually useful to be able to distinguish "you passed in the outright wrong kind" from "this type just doesn't point to another one", and some existing callers do make this distinction.) Error code count: 78 -> 63: 15 removed. More will go. --- include/ctf-api.h | 29 +++------ libctf/api.org | 7 --- libctf/ctf-create.c | 55 ++++++++-------- libctf/ctf-lookup.c | 12 ++-- libctf/ctf-types.c | 63 ++++++++++++------- .../libctf-lookup/enumerator-iteration.c | 6 +- .../testsuite/libctf-lookup/struct-lookup.c | 2 +- 7 files changed, 89 insertions(+), 85 deletions(-) diff --git a/include/ctf-api.h b/include/ctf-api.h index 1bb469a30b5..1388518158b 100644 --- a/include/ctf-api.h +++ b/include/ctf-api.h @@ -293,26 +293,18 @@ typedef int ctf_func_type_flags_t; _CTF_ITEM (ECTF_RANGE, "allowable range exceeded") \ _CTF_ITEM (ECTF_BADNAME, "string name offset is corrupt") \ _CTF_ITEM (ECTF_BADID, "invalid type identifier") \ - _CTF_ITEM (ECTF_NOTSOU, "type is not a struct or union") \ - _CTF_ITEM (ECTF_NOTENUM, "type is not an enum") \ - _CTF_ITEM (ECTF_NOTSUE, "type is not a struct, union, or enum") \ - _CTF_ITEM (ECTF_NOTINTFP, "type is not an integer, float, or enum") \ - _CTF_ITEM (ECTF_NOTARRAY, "type is not an array") \ + _CTF_ITEM (ECTF_WRONGKIND, "inappropriate type kind") \ _CTF_ITEM (ECTF_NOTREF, "type does not reference another type") \ - _CTF_ITEM (ECTF_NOTQUAL, "type is not a cvr-qual") \ _CTF_ITEM (ECTF_NAMELEN, "buffer is too small to hold type name") \ - _CTF_ITEM (ECTF_NOTYPE, "no type found corresponding to name") \ + _CTF_ITEM (ECTF_NOTYPE, "no type found corresponding to name, type ID, or offset") \ _CTF_ITEM (ECTF_SYNTAX, "syntax error in type name") \ _CTF_ITEM (ECTF_NOTFUNC, "symbol table entry or type is not a function") \ _CTF_ITEM (ECTF_NOFUNCDAT, "no function information available for function") \ _CTF_ITEM (ECTF_NOTDATA, "symbol table entry does not refer to a data object") \ _CTF_ITEM (ECTF_NOTYPEDAT, "no type information available for symbol") \ _CTF_ITEM (ECTF_NOTSUP, "feature not supported") \ - _CTF_ITEM (ECTF_NOENUMNAM, "enumerator name not found") \ - _CTF_ITEM (ECTF_NOMEMBNAM, "member name not found") \ - _CTF_ITEM (ECTF_RDONLY, "CTF container is read-only") \ - _CTF_ITEM (ECTF_DTFULL, "CTF type is full (no more members allowed)") \ - _CTF_ITEM (ECTF_FULL, "CTF container is full") \ + _CTF_ITEM (ECTF_RDONLY, "CTF dict is read-only") \ + _CTF_ITEM (ECTF_FULL, "implementation limit: CTF dict or type is full") \ _CTF_ITEM (ECTF_DUPLICATE, "duplicate member, enumerator, datasec, or variable name") \ _CTF_ITEM (ECTF_CONFLICT, "conflicting type is already defined") \ _CTF_ITEM (ECTF_COMPRESS, "failed to compress CTF data") \ @@ -329,7 +321,7 @@ typedef int ctf_func_type_flags_t; _CTF_ITEM (ECTF_FLAGS, "CTF header contains flags unknown to libctf") \ _CTF_ITEM (ECTF_NEEDSBFD, "this feature needs a libctf with BFD support") \ _CTF_ITEM (ECTF_INCOMPLETE, "type is not a complete type") \ - _CTF_ITEM (ECTF_NONAME, "type name must not be empty") \ + _CTF_ITEM (ECTF_NONAME, "invalid or nonexistent name") \ _CTF_ITEM (ECTF_BADFLAG, "invalid CTF dict flag specified") \ _CTF_ITEM (ECTF_CTFVERS_NO_SERIALIZE, "CTFv1 dicts are too old to serialize") \ _CTF_ITEM (ECTF_UNSTABLE, "attempt to write unstable file format version: set I_KNOW_LIBCTF_IS_UNSTABLE in the environment") \ @@ -337,16 +329,9 @@ typedef int ctf_func_type_flags_t; _CTF_ITEM (ECTF_WRONGPARENT, "cannot ctf_import: incorrect parent provided") \ _CTF_ITEM (ECTF_NOTSERIALIZED, "CTF dict must be serialized first") \ _CTF_ITEM (ECTF_BADCOMPONENT, "declaration tag component_idx is invalid") \ - _CTF_ITEM (ECTF_NOTBITSOU, "type is not a bitfield-capable struct or union") \ _CTF_ITEM (ECTF_DESCENDING, "structure offsets may not descend") \ _CTF_ITEM (ECTF_LINKAGE, "invalid linkage") \ _CTF_ITEM (ECTF_LINKKIND, "only functions and variables have linkage") \ - _CTF_ITEM (ECTF_NEVERTAG, "cannot call this function with a tag kind") \ - _CTF_ITEM (ECTF_NOTDATASEC, "this function requires a datasec") \ - _CTF_ITEM (ECTF_NOTVAR, "this function requires a variable") \ - _CTF_ITEM (ECTF_NODATASEC, "variable not found in datasec") \ - _CTF_ITEM (ECTF_NOTDECLTAG, "this function requires a decl tag") \ - _CTF_ITEM (ECTF_NOTTAG, "this function requires a type or decl tag") \ _CTF_ITEM (ECTF_KIND_PROHIBITED, "writeout of suppressed kind attempted") \ _CTF_ITEM (ECTF_NOTBTF, "cannot write out this dict as BTF") \ _CTF_ITEM (ECTF_TOOLARGE, "prefix required for correct representation") @@ -810,7 +795,9 @@ extern int ctf_type_visit (ctf_dict_t *, ctf_id_t, ctf_visit_f *, void *); extern int ctf_type_cmp (ctf_dict_t *, ctf_id_t, ctf_dict_t *, ctf_id_t); /* Get the name of an enumerator given its value, or vice versa. If many - enumerators have the same value, the first with that value is returned. */ + enumerators have the same value, the first with that value is returned. + If no name exists for a given value, or no value for a given name, + ECTF_NONAME is raised. */ extern const char *ctf_enum_name (ctf_dict_t *, ctf_id_t, ctf_enum_value_t); extern ctf_ret_t ctf_enum_value (ctf_dict_t *, ctf_id_t, const char *, diff --git a/libctf/api.org b/libctf/api.org index b4e8e57e39d..c116d4dfb04 100644 --- a/libctf/api.org +++ b/libctf/api.org @@ -125,18 +125,11 @@ implemented, prototype only) *** Additions (so far) _CTF_ITEM (ECTF_BADCOMPONENT, "Declaration tag component_idx is invalid.") \ - _CTF_ITEM (ECTF_NOTBITSOU, "Type is not a bitfield-capable struct or union.") \ _CTF_ITEM (ECTF_DESCENDING, "Structure offsets may not descend.") \ _CTF_ITEM (ECTF_LINKAGE, "Invalid linkage.") \ _CTF_ITEM (ECTF_LINKKIND, "Only functions and variables have linkage.") \ - _CTF_ITEM (ECTF_NEVERTAG, "Cannot call this function with a tag kind.") \ - _CTF_ITEM (ECTF_NOTDATASEC, "This function requires a datasec.") \ - _CTF_ITEM (ECTF_NOTVAR, "This function requires a variable.") \ - _CTF_ITEM (ECTF_NOTDECLTAG, "This function requires a decl tag.") \ - _CTF_ITEM (ECTF_NOTTAG, "This function requires a type or decl tag.") _CTF_ITEM (ECTF_KIND_PROHIBITED, "Writeout of suppressed kind attempted.") _CTF_ITEM (ECTF_NOTBTF, "Cannot write out this dict as BTF.") \ - _CTF_ITEM (ECTF_NODATASEC, "Variable not found in datasec.") _CTF_ITEM (ECTF_TOOLARGE, "Prefix required for correct representation.") A bit of reshuffling of the constants to put ECTF_NEXT_END at the top, for diff --git a/libctf/ctf-create.c b/libctf/ctf-create.c index 438ab737cd8..a736ce14c88 100644 --- a/libctf/ctf-create.c +++ b/libctf/ctf-create.c @@ -542,7 +542,8 @@ ctf_add_generic (ctf_dict_t *fp, const char *name, ctf_kind_t kind, if (fp->ctf_typemax + 1 >= pfp->ctf_provtypemax) { - ctf_set_errno (fp, ECTF_FULL); + ctf_err (err_locus (fp), ECTF_FULL, _("cannot add type named %s of kind %i"), + name ? name : _("unnamed"), kind); return NULL; } @@ -681,7 +682,8 @@ ctf_add_encoded (ctf_dict_t *fp, const char *name, const ctf_encoding_t *ep, return (ctf_set_typed_errno (fp, EINVAL)); if (name == NULL || name[0] == '\0') - return (ctf_set_typed_errno (fp, ECTF_NONAME)); + return ctf_typed_err (err_locus (fp), ECTF_NONAME, + _("type name cannot be empty")); if (!ctf_assert (fp, kind == CTF_K_INTEGER || kind == CTF_K_FLOAT || kind == CTF_K_BTF_FLOAT)) @@ -765,7 +767,8 @@ ctf_add_slice (ctf_dict_t *fp, ctf_id_t ref, const ctf_encoding_t *ep) if ((kind != CTF_K_INTEGER) && (kind != CTF_K_FLOAT) && (kind != CTF_K_ENUM) && (kind != CTF_K_BTF_FLOAT) && (ref != 0)) - return (ctf_set_typed_errno (fp, ECTF_NOTINTFP)); + return ctf_typed_err (type_err_locus (fp, ref), ECTF_WRONGKIND, + _("cannot slice non-integral type kind %i"), kind); if ((dtd = ctf_add_generic (fp, NULL, CTF_K_SLICE, 0, sizeof (ctf_slice_t), 0, NULL)) == NULL) @@ -1044,7 +1047,8 @@ ctf_add_tag (ctf_dict_t *fp, ctf_id_t type, const char *tag, int is_decl, } if (tag == NULL || tag[0] == '\0') - return (ctf_set_typed_errno (fp, ECTF_NONAME)); + return ctf_typed_err (err_locus (fp), ECTF_NONAME, + _("tag name cannot be empty")); if ((dtd = ctf_add_generic (fp, tag, kind, 0, vlen_size, 0, NULL)) == NULL) return CTF_ERR; /* errno is set for us. */ @@ -1194,7 +1198,7 @@ ctf_add_struct (ctf_dict_t *fp, const char *name, if (struct_union_unknown != CTF_K_UNKNOWN && struct_union_unknown != CTF_K_STRUCT && struct_union_unknown != CTF_K_UNION) - return ctf_typed_err (err_locus (fp), ECTF_NOTSOU, + return ctf_typed_err (err_locus (fp), ECTF_WRONGKIND, _("struct %s kind must be one of CTF_K_STRUCT, UNION or UNKNOWN, not %i"), name ? name : "(unnamed)", struct_union_unknown); @@ -1205,7 +1209,7 @@ ctf_add_struct (ctf_dict_t *fp, const char *name, if (bitfield != CTF_STRUCT_NORMAL && bitfield != CTF_STRUCT_BITFIELD) - return ctf_typed_err (err_locus (fp), ECTF_NOTSOU, + return ctf_typed_err (err_locus (fp), ECTF_WRONGKIND, _("struct %s bitfieldness must be one of CTF_STRUCT_NORMAL or CTF_STRUCT_BITFIELD, not %i"), name ? name : "(unnamed)", bitfield); @@ -1254,7 +1258,7 @@ ctf_add_enum (ctf_dict_t *fp, const char *name, ctf_kind_t enum_64_unknown, kind = CTF_K_ENUM64; if (kind != CTF_K_ENUM && kind != CTF_K_ENUM64) - return ctf_typed_err (err_locus (fp), ECTF_NOTENUM, + return ctf_typed_err (err_locus (fp), ECTF_WRONGKIND, _("enum %s kind must be one of CTF_K_ENUM, ENUM64 or UNKNOWN, not %i"), name ? name : "(unnamed)", enum_64_unknown); @@ -1319,10 +1323,13 @@ ctf_add_forward (ctf_dict_t *fp, const char *name, ctf_kind_t kind) ctf_id_t type = 0; if (!ctf_forwardable_kind (kind)) - return (ctf_set_typed_errno (fp, ECTF_NOTSUE)); + return ctf_typed_err (err_locus (fp), ECTF_WRONGKIND, + _("cannot add forward named %s to kind %i"), name, + kind); if (name == NULL || name[0] == '\0') - return (ctf_set_typed_errno (fp, ECTF_NONAME)); + return ctf_typed_err (err_locus (fp), ECTF_NONAME, + _("type name cannot be empty")); if (fp->ctf_flags & LCTF_NO_STR) return (ctf_set_errno (fp, ECTF_NOPARENT)); @@ -1394,7 +1401,8 @@ ctf_add_typedef (ctf_dict_t *fp, const char *name, ctf_id_t ref) return (ctf_set_typed_errno (fp, EINVAL)); if (name == NULL || name[0] == '\0') - return (ctf_set_typed_errno (fp, ECTF_NONAME)); + return ctf_typed_err (err_locus (fp), ECTF_NONAME, + _("type name cannot be empty")); if (ref != 0 && ctf_lookup_by_id (&tmp, ref, NULL) == NULL) return CTF_ERR; /* errno is set for us. */ @@ -1413,7 +1421,7 @@ ctf_id_t ctf_add_qualifier (ctf_dict_t *fp, ctf_kind_t cvr_qual, ctf_id_t ref) { if (cvr_qual != CTF_K_CONST && cvr_qual != CTF_K_VOLATILE && cvr_qual != CTF_K_RESTRICT) - return ctf_typed_err (err_locus (fp), ECTF_NOTQUAL, + return ctf_typed_err (type_err_locus (fp, ref), ECTF_WRONGKIND, _("kind %i is not CTF_K_CONST, CTF_K_VOLATILE or CTF_K_RESTRICT"), cvr_qual); @@ -1465,10 +1473,10 @@ ctf_add_enumerator (ctf_dict_t *fp, ctf_id_t enid, const char *name, } if ((kind != CTF_K_ENUM) && (kind != CTF_K_ENUM64)) - return (ctf_set_errno (ofp, ECTF_NOTENUM)); + return ctf_err (type_err_locus (ofp, enid), ECTF_WRONGKIND, NULL); if (vlen == CTF_MAX_VLEN) - return (ctf_set_errno (ofp, ECTF_DTFULL)); + return ctf_err (type_err_locus (ofp, enid), ECTF_FULL, NULL); if (kind == CTF_K_ENUM) { @@ -1607,13 +1615,15 @@ ctf_add_member_bitfield (ctf_dict_t *fp, ctf_id_t souid, const char *name, vlen = LCTF_VLEN (fp, prefix); if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) - return (ctf_set_errno (ofp, ECTF_NOTSOU)); + return ctf_err (type_err_locus (ofp, souid), ECTF_WRONGKIND, + _("not a struct or union")); if (!kflag && bit_width != 0) - return (ctf_set_errno (ofp, ECTF_NOTBITSOU)); + return ctf_err (type_err_locus (ofp, souid), ECTF_WRONGKIND, + _("not a bitfield-capable struct or union")); if (vlen == CTF_MAX_VLEN) - return (ctf_set_errno (ofp, ECTF_DTFULL)); + return ctf_err (type_err_locus (ofp, souid), ECTF_FULL, NULL); /* Figure out the offset of this field: all structures in DTDs are CTF_K_BIG, which means their offsets are all encoded as @@ -1843,7 +1853,8 @@ ctf_add_section_variable (ctf_dict_t *fp, const char *datasec, return (ctf_set_typed_errno (fp, ECTF_NOPARENT)); if (name == NULL || name[0] == '\0') - return (ctf_set_typed_errno (fp, ECTF_NONAME)); + return ctf_typed_err (err_locus (fp), ECTF_NONAME, + _("datasec name cannot be empty")); if (linkage < 0 || linkage > 2) /* Min/max of ctf_linkages_t. */ return (ctf_set_typed_errno (fp, ECTF_LINKAGE)); @@ -1894,14 +1905,8 @@ ctf_add_section_variable (ctf_dict_t *fp, const char *datasec, if (vlen == CTF_MAX_RAW_VLEN) { - ctf_set_typed_errno (fp, ECTF_DTFULL); - goto err; - } - - /* DATASECs do not support CTF_K_BIG (yet). */ - if (vlen == CTF_MAX_RAW_VLEN) - { - ctf_set_typed_errno (fp, ECTF_DTFULL); + ctf_err (type_err_locus (fp, type), ECTF_FULL, + _("datasec %s (%lx) overflow"), name, (long) datasec_id); goto err; } diff --git a/libctf/ctf-lookup.c b/libctf/ctf-lookup.c index dcf56989071..58856852edb 100644 --- a/libctf/ctf-lookup.c +++ b/libctf/ctf-lookup.c @@ -457,7 +457,8 @@ ctf_lookup_by_kind (ctf_dict_t *fp, ctf_kind_t kind, const char *name) ctf_id_t type; if (kind == CTF_K_TYPE_TAG || kind == CTF_K_DECL_TAG) - return (ctf_set_typed_errno (fp, ECTF_NEVERTAG)); + return ctf_typed_err (err_locus (fp), ECTF_WRONGKIND, + _("cannot call this function with a tag kind")); if ((type = ctf_dynhash_lookup_type (ctf_name_table (fp, kind), name)) != CTF_ERR) @@ -468,7 +469,8 @@ ctf_lookup_by_kind (ctf_dict_t *fp, ctf_kind_t kind, const char *name) name)) != CTF_ERR) return type; - return ctf_set_typed_errno (fp, ECTF_NOTYPE); + return ctf_typed_err (err_locus (fp), ECTF_NOTYPE, + _("type named %s of type %i"), name, kind); } /* Look up a single enumerator by enumeration constant name. Returns the ID of @@ -497,14 +499,16 @@ ctf_lookup_enumerator (ctf_dict_t *fp, const char *name, ctf_enum_value_t *enum_ if (type == 0 && fp->ctf_parent) { if ((type = ctf_lookup_enumerator (fp->ctf_parent, name, enum_value)) == 0) - return ctf_set_typed_errno (fp, ECTF_NOENUMNAM); + return ctf_typed_err (err_locus (fp), ECTF_NONAME, _("enumerator %s"), + name); return type; } /* Nothing more to do if this type didn't exist or we don't have to look up the enum value. */ if (type == 0) - return ctf_set_typed_errno (fp, ECTF_NOENUMNAM); + return ctf_typed_err (err_locus (fp), ECTF_NONAME, _("enumerator %s"), + name); if (enum_value == NULL) return type; diff --git a/libctf/ctf-types.c b/libctf/ctf-types.c index f4ed4558436..3a24171ea95 100644 --- a/libctf/ctf-types.c +++ b/libctf/ctf-types.c @@ -192,7 +192,8 @@ ctf_member_next (ctf_dict_t *fp, ctf_id_t type, ctf_next_t **it, if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) { - ctf_set_errno (ofp, ECTF_NOTSOU); + ctf_err (type_err_locus (fp, type), ECTF_WRONGKIND, + _("not a struct or union")); return CTF_MEMBER_ERR; } @@ -397,7 +398,7 @@ ctf_enum_next (ctf_dict_t *fp, ctf_id_t type, ctf_next_t **it, if ((kind != CTF_K_ENUM) && (kind != CTF_K_ENUM64)) { ctf_next_destroy (i); - ctf_set_errno (ofp, ECTF_NOTENUM); + ctf_err (type_err_locus (ofp, type), ECTF_WRONGKIND, NULL); return NULL; } @@ -630,7 +631,7 @@ ctf_datasec_var_next (ctf_dict_t *fp, ctf_id_t datasec, ctf_next_t **it, return CTF_ERR; /* errno is set for us. */ if (ctf_type_kind (fp, datasec) != CTF_K_DATASEC) - return (ctf_set_typed_errno (ofp, ECTF_NOTDATASEC)); + return ctf_err (type_err_locus (fp, datasec), ECTF_WRONGKIND, NULL); if ((tp = ctf_lookup_by_id (&fp, datasec, NULL)) == NULL) return CTF_ERR; /* errno is set for us. */ @@ -1566,7 +1567,7 @@ ctf_decl_tag (ctf_dict_t *fp, ctf_id_t decl_tag, int64_t *component_idx) return CTF_ERR; /* errno is set for us. */ if (LCTF_KIND (fp, tp) != CTF_K_DECL_TAG) - return (ctf_set_typed_errno (ofp, ECTF_NOTDECLTAG)); + return ctf_typed_err (type_err_locus (ofp, decl_tag), ECTF_WRONGKIND, NULL); vlen = ctf_vlen (fp, decl_tag, tp, NULL); cdt = (ctf_decl_tag_t *) vlen; @@ -1588,7 +1589,7 @@ ctf_tag (ctf_dict_t *fp, ctf_id_t tag) ctf_id_t ref; if (kind != CTF_K_TYPE_TAG && kind != CTF_K_DECL_TAG) - return (ctf_set_typed_errno (fp, ECTF_NOTTAG)); + return ctf_typed_err (type_err_locus (fp, tag), ECTF_WRONGKIND, NULL); if ((ref = ctf_type_reference (fp, tag)) == CTF_ERR) return CTF_ERR; /* errno is set for us. */ @@ -1757,7 +1758,7 @@ ctf_type_encoding (ctf_dict_t *fp, ctf_id_t type, ctf_encoding_t *ep) break; } default: - return (ctf_set_errno (ofp, ECTF_NOTINTFP)); + return ctf_err (type_err_locus (ofp, type), ECTF_WRONGKIND, NULL); } return 0; @@ -1919,7 +1920,7 @@ ctf_member_count (ctf_dict_t *fp, ctf_id_t type) kind = LCTF_KIND (fp, tp); if (kind != CTF_K_STRUCT && kind != CTF_K_UNION && kind != CTF_K_ENUM) - return (ctf_set_errno (ofp, ECTF_NOTSUE)); + return ctf_err (type_err_locus (ofp, type), ECTF_WRONGKIND, NULL); return LCTF_VLEN (fp, tp); } @@ -1951,7 +1952,8 @@ ctf_member_info (ctf_dict_t *fp, ctf_id_t type, const char *name, kind = LCTF_KIND (fp, tp); if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) - return (ctf_set_errno (ofp, ECTF_NOTSOU)); + return ctf_err (type_err_locus (ofp, type), ECTF_WRONGKIND, + _("not a struct or union")); vlen = ctf_vlen (fp, type, tp, &n); @@ -2013,7 +2015,9 @@ ctf_member_info (ctf_dict_t *fp, ctf_id_t type, const char *name, } } - return (ctf_set_errno (ofp, ECTF_NOMEMBNAM)); + ctf_warn (type_err_locus (ofp, type), ECTF_NONAME, + _("member name %s not found"), name); + return (ctf_set_errno (ofp, ECTF_NONAME)); } /* Return the array type, index, and size information for the specified ARRAY. */ @@ -2030,7 +2034,7 @@ ctf_array_info (ctf_dict_t *fp, ctf_id_t type, ctf_arinfo_t *arp) return -1; /* errno is set for us. */ if (LCTF_KIND (fp, tp) != CTF_K_ARRAY) - return (ctf_set_errno (ofp, ECTF_NOTARRAY)); + return ctf_err (type_err_locus (ofp, type), ECTF_WRONGKIND, NULL); vlen = ctf_vlen (fp, type, tp, NULL); ap = (const ctf_array_t *) vlen; @@ -2069,7 +2073,7 @@ ctf_enum_name (ctf_dict_t *fp, ctf_id_t type, ctf_enum_value_t value) kind = LCTF_KIND (fp, tp); if (kind != CTF_K_ENUM && kind != CTF_K_ENUM64) { - ctf_set_errno (ofp, ECTF_NOTENUM); + ctf_err (type_err_locus (ofp, type), ECTF_WRONGKIND, NULL); return NULL; } @@ -2108,7 +2112,12 @@ ctf_enum_name (ctf_dict_t *fp, ctf_id_t type, ctf_enum_value_t value) } } - ctf_set_errno (ofp, ECTF_NOENUMNAM); + ctf_warn (type_err_locus (ofp, type), ECTF_NONAME, + (value.encoding.cte_format & CTF_INT_SIGNED) + ? _("enumerator value %" PRIi64 " has no corresponding name") + : _("enumerator value %" PRIu64 " has no corresponding name"), + value.val); + ctf_set_errno (ofp, ECTF_NONAME); return NULL; } @@ -2136,7 +2145,7 @@ ctf_enum_value (ctf_dict_t *fp, ctf_id_t type, const char *name, kind = LCTF_KIND (fp, tp); if (kind != CTF_K_ENUM && kind != CTF_K_ENUM64) - return ctf_set_errno (ofp, ECTF_NOTENUM); + return ctf_err (type_err_locus (ofp, type), ECTF_WRONGKIND, NULL); vlen = ctf_vlen (fp, type, tp, &n); @@ -2173,7 +2182,9 @@ ctf_enum_value (ctf_dict_t *fp, ctf_id_t type, const char *name, } } - return ctf_set_errno (ofp, ECTF_NOENUMNAM); + ctf_warn (type_err_locus (ofp, type), ECTF_NONAME, + _("enumerator name %s not found"), name); + return ctf_set_errno (ofp, ECTF_NONAME); } /* Determine whether an enum's values are signed. Private interface. */ @@ -2187,7 +2198,7 @@ ctf_enum_unsigned (ctf_dict_t *fp, ctf_id_t type) return -1; /* errno is set for us. */ if (kind != CTF_K_ENUM && kind != CTF_K_ENUM64) - return (ctf_set_errno (fp, ECTF_NOTENUM)); + return ctf_err (type_err_locus (fp, type), ECTF_WRONGKIND, NULL); if (ctf_lookup_by_id (&fp, type, &tp) == NULL) return -1; /* errno is set for us. */ @@ -2197,7 +2208,7 @@ ctf_enum_unsigned (ctf_dict_t *fp, ctf_id_t type) /* Return nonzero if this struct or union uses bitfield encoding. */ ctf_bool_t -ctf_struct_bitfield (ctf_dict_t * fp, ctf_id_t type) +ctf_struct_bitfield (ctf_dict_t *fp, ctf_id_t type) { ctf_kind_t kind; const ctf_type_t *tp; /* The suffixed kind, if prefixed */ @@ -2206,7 +2217,8 @@ ctf_struct_bitfield (ctf_dict_t * fp, ctf_id_t type) return -1; /* errno is set for us. */ if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) - return (ctf_set_errno (fp, ECTF_NOTSOU)); + return ctf_err (type_err_locus (fp, type), ECTF_WRONGKIND, + _("not a struct or union")); if (ctf_lookup_by_id (&fp, type, &tp) == NULL) return -1; /* errno is set for us. */ @@ -2369,11 +2381,12 @@ search_datasec_by_offset (const void *key_, const void *arr_) /* Search a datasec for a variable covering a given offset. - Errors with ECTF_NODATASEC if not found. */ + Errors with ECTF_NOTYPE if not found. */ ctf_id_t ctf_datasec_var_offset (ctf_dict_t *fp, ctf_id_t datasec, uint32_t offset) { + ctf_dict_t *ofp = fp; ctf_dtdef_t *dtd; const ctf_type_t *tp; unsigned char *vlen; @@ -2386,7 +2399,7 @@ ctf_datasec_var_offset (ctf_dict_t *fp, ctf_id_t datasec, uint32_t offset) return -1; /* errno is set for us. */ if (ctf_type_kind (fp, datasec) != CTF_K_DATASEC) - return ctf_set_typed_errno (fp, ECTF_NOTDATASEC); + return ctf_err (type_err_locus (ofp, datasec), ECTF_WRONGKIND, NULL); if ((dtd = ctf_dynamic_type (fp, datasec)) != NULL) { @@ -2399,7 +2412,8 @@ ctf_datasec_var_offset (ctf_dict_t *fp, ctf_id_t datasec, uint32_t offset) if ((el = bsearch (&offset, sec, vlen_len, sizeof (ctf_var_secinfo_t), search_datasec_by_offset)) == NULL) - return ctf_set_typed_errno (fp, ECTF_NODATASEC); + return ctf_typed_err (type_err_locus (ofp, datasec), ECTF_NOTYPE, + _("searched datasec for offset %x"), offset); if (el->cvs_offset == offset) return el->cvs_type; @@ -2408,7 +2422,8 @@ ctf_datasec_var_offset (ctf_dict_t *fp, ctf_id_t datasec, uint32_t offset) if (el->cvs_offset < offset && el->cvs_offset + size > offset) return el->cvs_type; - return ctf_set_typed_errno (fp, ECTF_NODATASEC); + return ctf_typed_err (type_err_locus (ofp, datasec), ECTF_NOTYPE, + _("searched datasec for offset %x"), offset); } /* Return the entry corresponding to a given component_idx in a datasec. @@ -2439,7 +2454,7 @@ ctf_datasec_entry (ctf_dict_t *fp, ctf_id_t datasec, int component_idx) return &sec[component_idx]; } -/* Return the datasec that a given variable appears in, or ECTF_NODATASEC if +/* Return the datasec that a given variable appears in, or ECTF_NOTYPE if none. */ ctf_id_t ctf_variable_datasec (ctf_dict_t *fp, ctf_id_t var) @@ -2447,13 +2462,13 @@ ctf_id_t ctf_variable_datasec (ctf_dict_t *fp, ctf_id_t var) void *sec; if (ctf_type_kind (fp, var) != CTF_K_VAR) - return (ctf_set_typed_errno (fp, ECTF_NOTVAR)); + return ctf_typed_err (type_err_locus (fp, var), ECTF_WRONGKIND, NULL); if (ctf_dynhash_lookup_kv (fp->ctf_var_datasecs, (void *) (ptrdiff_t) var, NULL, &sec)) return (ctf_id_t) sec; - return (ctf_set_typed_errno (fp, ECTF_NODATASEC)); + return ctf_typed_err (type_err_locus (fp, var), ECTF_NOTYPE, NULL); } /* Recursively visit the members of any type. This function is used as the diff --git a/libctf/testsuite/libctf-lookup/enumerator-iteration.c b/libctf/testsuite/libctf-lookup/enumerator-iteration.c index 542d482aa4b..16854fbbc51 100644 --- a/libctf/testsuite/libctf-lookup/enumerator-iteration.c +++ b/libctf/testsuite/libctf-lookup/enumerator-iteration.c @@ -169,17 +169,17 @@ main (int argc, char *argv[]) err, ctf_errmsg (ctf_errno (fp))); if ((type = ctf_lookup_enumerator (fp, "DYNADD3", &val) != CTF_ERR) || - ctf_errno (fp) != ECTF_NOENUMNAM) + ctf_errno (fp) != ECTF_NONAME) { if (type != CTF_ERR) { char *foo; - printf ("direct lookup: hidden lookup did not return ECTF_NOENUMNAM but rather %li in %s\n", + printf ("direct lookup: hidden lookup did not return ECTF_NONAME but rather %li in %s\n", val.val, foo = ctf_type_aname (fp, type)); free (foo); } else - printf ("direct lookup: hidden lookup did not return ECTF_NOENUMNAM but rather %s\n", + printf ("direct lookup: hidden lookup did not return ECTF_NONAME but rather %s\n", ctf_errmsg (ctf_errno (fp))); } diff --git a/libctf/testsuite/libctf-lookup/struct-lookup.c b/libctf/testsuite/libctf-lookup/struct-lookup.c index 9b95317bfa0..2749300e5d0 100644 --- a/libctf/testsuite/libctf-lookup/struct-lookup.c +++ b/libctf/testsuite/libctf-lookup/struct-lookup.c @@ -43,7 +43,7 @@ main (int argc, char *argv[]) free (type_name); if (ctf_member_info (fp, type, "should_not_appear", &mi) >= 0 - || ctf_errno (fp) != ECTF_NOMEMBNAM) + || ctf_errno (fp) != ECTF_NONAME) fprintf (stderr, "should_not_appear appeared.\n"); ctf_dict_close (fp); -- 2.47.3