From f009162fd133d155c413e008742513bf8fecbed2 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Thu, 24 Jun 2021 10:01:27 -0300 Subject: [PATCH] core: Make base_type->name a real string For the threaded code we want to access strings in tags at the same time that the string table may grow in another thread making the previous pointer invalid, so, to avoid excessive locking, use plain strings. The way the tools work will either consume the just produced CU straight away or keep just one copy of each data structure when we keep all CUs in memory, so lets try stopping using strings_t for strings. For the base_type->name case we get the bonus of removing some more functions related base types and a user of dwarves__active_loader. Signed-off-by: Arnaldo Carvalho de Melo --- btf_encoder.c | 6 ++++-- btf_loader.c | 6 +++--- ctf_loader.c | 6 +++--- dwarf_loader.c | 5 ++--- dwarves.c | 38 ++++++++++++-------------------------- dwarves.h | 5 +++-- 6 files changed, 27 insertions(+), 39 deletions(-) diff --git a/btf_encoder.c b/btf_encoder.c index c79c5d2..2ed7217 100644 --- a/btf_encoder.c +++ b/btf_encoder.c @@ -812,12 +812,14 @@ static int btf_encoder__encode_tag(struct btf_encoder *encoder, struct cu *cu, s { /* single out type 0 as it represents special type "void" */ uint32_t ref_type_id = tag->type == 0 ? 0 : type_id_off + tag->type; + struct base_type *bt; const char *name; switch (tag->tag) { case DW_TAG_base_type: - name = dwarves__active_loader->strings__ptr(cu, tag__base_type(tag)->name); - return btf_encoder__add_base_type(encoder, tag__base_type(tag), name); + bt = tag__base_type(tag); + name = __base_type__name(bt); + return btf_encoder__add_base_type(encoder, bt, name); case DW_TAG_const_type: return btf_encoder__add_ref_type(encoder, BTF_KIND_CONST, ref_type_id, NULL, false); case DW_TAG_pointer_type: diff --git a/btf_loader.c b/btf_loader.c index 3fc2a4a..660125e 100644 --- a/btf_loader.c +++ b/btf_loader.c @@ -101,7 +101,7 @@ static int create_new_function(struct cu *cu, const struct btf_type *tp, uint32_ return 0; } -static struct base_type *base_type__new(strings_t name, uint32_t attrs, +static struct base_type *base_type__new(const char *name, uint32_t attrs, uint8_t float_type, size_t size) { struct base_type *bt = tag__alloc(sizeof(*bt)); @@ -167,7 +167,7 @@ static struct variable *variable__new(strings_t name, uint32_t linkage) static int create_new_int_type(struct cu *cu, const struct btf_type *tp, uint32_t id) { uint32_t attrs = btf_int_encoding(tp); - strings_t name = tp->name_off; + const char *name = cu__btf_str(cu, tp->name_off); struct base_type *base = base_type__new(name, attrs, 0, btf_int_bits(tp)); if (base == NULL) @@ -181,7 +181,7 @@ static int create_new_int_type(struct cu *cu, const struct btf_type *tp, uint32_ static int create_new_float_type(struct cu *cu, const struct btf_type *tp, uint32_t id) { - strings_t name = tp->name_off; + const char *name = cu__btf_str(cu, tp->name_off); struct base_type *base = base_type__new(name, 0, BT_FP_SINGLE, tp->size * 8); if (base == NULL) diff --git a/ctf_loader.c b/ctf_loader.c index 44b70d3..5bd5b52 100644 --- a/ctf_loader.c +++ b/ctf_loader.c @@ -150,7 +150,7 @@ static int ctf__load_funcs(struct ctf *ctf) return 0; } -static struct base_type *base_type__new(strings_t name, uint32_t attrs, +static struct base_type *base_type__new(const char *name, uint32_t attrs, uint8_t float_type, size_t size) { struct base_type *bt = tag__alloc(sizeof(*bt)); @@ -207,7 +207,7 @@ static int create_new_base_type(struct ctf *ctf, void *ptr, uint32_t eval = ctf__get32(ctf, enc); uint32_t attrs = CTF_TYPE_INT_ATTRS(eval); strings_t name = ctf__get32(ctf, &tp->base.ctf_name); - struct base_type *base = base_type__new(name, attrs, 0, + struct base_type *base = base_type__new(ctf__string(ctf, name), attrs, 0, CTF_TYPE_INT_BITS(eval)); if (base == NULL) return -ENOMEM; @@ -224,7 +224,7 @@ static int create_new_base_type_float(struct ctf *ctf, void *ptr, { strings_t name = ctf__get32(ctf, &tp->base.ctf_name); uint32_t *enc = ptr, eval = ctf__get32(ctf, enc); - struct base_type *base = base_type__new(name, 0, eval, + struct base_type *base = base_type__new(ctf__string(ctf, name), 0, eval, CTF_TYPE_FP_BITS(eval)); if (base == NULL) return -ENOMEM; diff --git a/dwarf_loader.c b/dwarf_loader.c index 40ac80b..5a149cb 100644 --- a/dwarf_loader.c +++ b/dwarf_loader.c @@ -533,7 +533,7 @@ static struct base_type *base_type__new(Dwarf_Die *die, struct cu *cu) if (bt != NULL) { tag__init(&bt->tag, cu, die); - bt->name = strings__add(strings, attr_string(die, DW_AT_name)); + bt->name = strdup_attr_string(die, DW_AT_name); bt->bit_size = attr_numeric(die, DW_AT_byte_size) * 8; uint64_t encoding = attr_numeric(die, DW_AT_encoding); bt->is_bool = encoding == DW_ATE_boolean; @@ -788,7 +788,7 @@ static int tag__recode_dwarf_bitfield(struct tag *tag, struct cu *cu, uint16_t b recoded = (struct tag *)new_bt; recoded->tag = DW_TAG_base_type; recoded->top_level = 1; - new_bt->name = sname; + new_bt->name = strdup(name); new_bt->bit_size = bit_size; break; @@ -2449,7 +2449,6 @@ static int class_member__cache_byte_size(struct tag *tag, struct cu *cu, static int finalize_cu(struct cus *cus, struct cu *cu, struct dwarf_cu *dcu, struct conf_load *conf) { - base_type_name_to_size_table__init(strings); cu__for_all_tags(cu, class_member__cache_byte_size, conf); if (conf && conf->steal) { return conf->steal(cu, conf); diff --git a/dwarves.c b/dwarves.c index d78ce2e..8f6dceb 100644 --- a/dwarves.c +++ b/dwarves.c @@ -184,7 +184,7 @@ size_t __tag__id_not_found_fprintf(FILE *fp, type_id_t id, return fprintf(fp, "\n", fn, line, id); } -static struct base_type_name_to_size { +static struct ase_type_name_to_size { const char *name; strings_t sname; size_t size; @@ -227,19 +227,6 @@ static struct base_type_name_to_size { { .name = NULL }, }; -void base_type_name_to_size_table__init(struct strings *strings) -{ - int i = 0; - - while (base_type_name_to_size_table[i].name != NULL) { - if (base_type_name_to_size_table[i].sname == 0) - base_type_name_to_size_table[i].sname = - strings__find(strings, - base_type_name_to_size_table[i].name); - ++i; - } -} - size_t base_type__name_to_size(struct base_type *bt, struct cu *cu) { int i = 0; @@ -247,22 +234,21 @@ size_t base_type__name_to_size(struct base_type *bt, struct cu *cu) const char *name, *orig_name; if (bt->name_has_encoding) - name = s(cu, bt->name); + name = bt->name; else name = base_type__name(bt, cu, bf, sizeof(bf)); orig_name = name; try_again: while (base_type_name_to_size_table[i].name != NULL) { if (bt->name_has_encoding) { - if (base_type_name_to_size_table[i].sname == bt->name) { + if (strcmp(base_type_name_to_size_table[i].name, bt->name) == 0) { size_t size; found: size = base_type_name_to_size_table[i].size; return size ?: ((size_t)cu->addr_size * 8); } - } else if (strcmp(base_type_name_to_size_table[i].name, - name) == 0) + } else if (strcmp(base_type_name_to_size_table[i].name, name) == 0) goto found; ++i; } @@ -293,21 +279,21 @@ static const char *base_type_fp_type_str[] = { [BT_FP_IMGRY_LDBL] = "imaginary long double", }; +const char *__base_type__name(const struct base_type *bt) +{ + return bt->name; +} + const char *base_type__name(const struct base_type *bt, const struct cu *cu, char *bf, size_t len) { if (bt->name_has_encoding) - return s(cu, bt->name); + return __base_type__name(bt); if (bt->float_type) - snprintf(bf, len, "%s %s", - base_type_fp_type_str[bt->float_type], - s(cu, bt->name)); + snprintf(bf, len, "%s %s", base_type_fp_type_str[bt->float_type], bt->name); else - snprintf(bf, len, "%s%s%s", - bt->is_bool ? "bool " : "", - bt->is_varargs ? "... " : "", - s(cu, bt->name)); + snprintf(bf, len, "%s%s%s", bt->is_bool ? "bool " : "", bt->is_varargs ? "... " : "", bt->name); return bf; } diff --git a/dwarves.h b/dwarves.h index ea2a88a..b66bf7c 100644 --- a/dwarves.h +++ b/dwarves.h @@ -1245,7 +1245,7 @@ enum base_type_float_type { struct base_type { struct tag tag; - strings_t name; + const char *name; uint16_t bit_size; uint8_t name_has_encoding:1; uint8_t is_signed:1; @@ -1264,10 +1264,11 @@ static inline uint16_t base_type__size(const struct tag *tag) return tag__base_type(tag)->bit_size / 8; } +const char *__base_type__name(const struct base_type *bt); + const char *base_type__name(const struct base_type *btype, const struct cu *cu, char *bf, size_t len); -void base_type_name_to_size_table__init(struct strings *strings); size_t base_type__name_to_size(struct base_type *btype, struct cu *cu); struct array_type {