From 244912e4d56c8e343a5d57e51b50cc6fd6fbf17a Mon Sep 17 00:00:00 2001 From: mpi Date: Wed, 15 Apr 2020 16:59:04 +0000 Subject: Make map functions accept a "struct map *" instead of a "struct bt_var *". This is a step towards reducing the type casting crazyness. --- usr.sbin/btrace/btrace.c | 30 +++++++++++++++++-------- usr.sbin/btrace/btrace.h | 15 +++++++------ usr.sbin/btrace/map.c | 58 ++++++++++++++++++++++-------------------------- 3 files changed, 55 insertions(+), 48 deletions(-) (limited to 'usr.sbin/btrace') diff --git a/usr.sbin/btrace/btrace.c b/usr.sbin/btrace/btrace.c index 4d9f316850a..1eb097dd7d2 100644 --- a/usr.sbin/btrace/btrace.c +++ b/usr.sbin/btrace/btrace.c @@ -1,4 +1,4 @@ -/* $OpenBSD: btrace.c,v 1.13 2020/04/15 14:50:14 mpi Exp $ */ +/* $OpenBSD: btrace.c,v 1.14 2020/04/15 16:59:04 mpi Exp $ */ /* * Copyright (c) 2019 - 2020 Martin Pieuchot @@ -564,11 +564,18 @@ rule_printmaps(struct bt_rule *r) struct bt_arg *ba; SLIST_FOREACH(ba, &bs->bs_args, ba_next) { + struct bt_var *bv = ba->ba_value; + if (ba->ba_type != B_AT_MAP) continue; - map_print(ba->ba_value, SIZE_T_MAX); - map_clear(ba->ba_value); + if (bv->bv_value != NULL) { + struct map *map = (struct map *)bv->bv_value; + + map_print(map, SIZE_T_MAX, bv_name(bv)); + map_clear(map); + bv->bv_value = NULL; + } } } } @@ -646,7 +653,8 @@ stmt_clear(struct bt_stmt *bs) assert(bs->bs_var == NULL); assert(ba->ba_type == B_AT_VAR); - map_clear(bv); + map_clear((struct map *)bv->bv_value); + bv->bv_value = NULL; debug("map=%p '%s' clear\n", bv->bv_value, bv_name(bv)); } @@ -669,7 +677,7 @@ stmt_delete(struct bt_stmt *bs, struct dt_evt *dtev) debug("map=%p '%s' delete key=%p '%s'\n", bv->bv_value, bv_name(bv), bkey, ba2hash(bkey, dtev)); - map_delete(bv, ba2hash(bkey, dtev)); + map_delete((struct map *)bv->bv_value, ba2hash(bkey, dtev)); } /* @@ -692,7 +700,8 @@ stmt_insert(struct bt_stmt *bs, struct dt_evt *dtev) debug("map=%p '%s' insert key=%p '%s' bval=%p\n", bv->bv_value, bv_name(bv), bkey, ba2hash(bkey, dtev), bval); - map_insert(bv, ba2hash(bkey, dtev), bval); + bv->bv_value = (struct bt_arg *)map_insert((struct map *)bv->bv_value, + ba2hash(bkey, dtev), bval); } /* @@ -718,7 +727,7 @@ stmt_print(struct bt_stmt *bs, struct dt_evt *dtev) top = ba2long(btop, dtev); } - map_print(bv, top); + map_print((struct map *)bv->bv_value, top, bv_name(bv)); debug("map=%p '%s' print (top=%d)\n", bv->bv_value, bv_name(bv), top); } @@ -790,7 +799,7 @@ stmt_zero(struct bt_stmt *bs) assert(bs->bs_var == NULL); assert(ba->ba_type == B_AT_VAR); - map_zero(bv); + map_zero((struct map *)bv->bv_value); debug("map=%p '%s' zero\n", bv->bv_value, bv_name(bv)); } @@ -891,6 +900,7 @@ const char * ba2str(struct bt_arg *ba, struct dt_evt *dtev) { static char buf[sizeof("18446744073709551615")]; /* UINT64_MAX */ + struct bt_var *bv; const char *str; switch (ba->ba_type) { @@ -930,7 +940,9 @@ ba2str(struct bt_arg *ba, struct dt_evt *dtev) str = buf; break; case B_AT_MAP: - str = ba2str(map_get(ba->ba_value, ba2str(ba->ba_key, dtev)), dtev); + bv = ba->ba_value; + str = ba2str(map_get((struct map *)bv->bv_value, + ba2str(ba->ba_key, dtev)), dtev); break; case B_AT_VAR: str = ba2str(ba_read(ba), dtev); diff --git a/usr.sbin/btrace/btrace.h b/usr.sbin/btrace/btrace.h index 8665ee870fc..6206e4beae2 100644 --- a/usr.sbin/btrace/btrace.h +++ b/usr.sbin/btrace/btrace.h @@ -1,4 +1,4 @@ -/* $OpenBSD: btrace.h,v 1.3 2020/01/28 16:39:51 mpi Exp $ */ +/* $OpenBSD: btrace.h,v 1.4 2020/04/15 16:59:04 mpi Exp $ */ /* * Copyright (c) 2019 - 2020 Martin Pieuchot @@ -39,13 +39,14 @@ void kelf_close(void); int kelf_snprintsym(char *, size_t, unsigned long); /* map.c */ -void map_clear(struct bt_var *); -void map_delete(struct bt_var *, const char *); -struct bt_arg *map_get(struct bt_var *, const char *); -void map_insert(struct bt_var *, const char *, +struct map; +void map_clear(struct map *); +void map_delete(struct map *, const char *); +struct bt_arg *map_get(struct map *, const char *); +struct map *map_insert(struct map *, const char *, struct bt_arg *); -void map_print(struct bt_var *, size_t); -void map_zero(struct bt_var *); +void map_print(struct map *, size_t, const char *); +void map_zero(struct map *); /* printf.c */ int stmt_printf(struct bt_stmt *, struct dt_evt *); diff --git a/usr.sbin/btrace/map.c b/usr.sbin/btrace/map.c index 297c4a721d0..f57efe3a348 100644 --- a/usr.sbin/btrace/map.c +++ b/usr.sbin/btrace/map.c @@ -1,4 +1,4 @@ -/* $OpenBSD: map.c,v 1.5 2020/04/15 14:51:45 mpi Exp $ */ +/* $OpenBSD: map.c,v 1.6 2020/04/15 16:59:04 mpi Exp $ */ /* * Copyright (c) 2020 Martin Pieuchot @@ -41,7 +41,7 @@ #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b)) #endif -RB_HEAD(mtree, mentry); +RB_HEAD(map, mentry); #define KLEN 256 @@ -52,9 +52,9 @@ struct mentry { }; int mcmp(struct mentry *, struct mentry *); -struct mentry *mget(struct mtree *, const char *); +struct mentry *mget(struct map *, const char *); -RB_GENERATE(mtree, mentry, mlink, mcmp); +RB_GENERATE(map, mentry, mlink, mcmp); int mcmp(struct mentry *me0, struct mentry *me1) @@ -63,59 +63,54 @@ mcmp(struct mentry *me0, struct mentry *me1) } struct mentry * -mget(struct mtree *map, const char *key) +mget(struct map *map, const char *key) { struct mentry me, *mep; strlcpy(me.mkey, key, KLEN); - mep = RB_FIND(mtree, map, &me); + mep = RB_FIND(map, map, &me); if (mep == NULL) { mep = calloc(1, sizeof(struct mentry)); if (mep == NULL) err(1, "mentry: calloc"); strlcpy(mep->mkey, key, KLEN); - RB_INSERT(mtree, map, mep); + RB_INSERT(map, map, mep); } return mep; } void -map_clear(struct bt_var *bv) +map_clear(struct map *map) { - struct mtree *map = (struct mtree *)bv->bv_value; struct mentry *mep; - while ((mep = RB_MIN(mtree, map)) != NULL) { - RB_REMOVE(mtree, map, mep); + while ((mep = RB_MIN(map, map)) != NULL) { + RB_REMOVE(map, map, mep); free(mep); } assert(RB_EMPTY(map)); free(map); - - bv->bv_value = NULL; } void -map_delete(struct bt_var *bv, const char *key) +map_delete(struct map *map, const char *key) { - struct mtree *map = (struct mtree *)bv->bv_value; struct mentry me, *mep; strlcpy(me.mkey, key, KLEN); - mep = RB_FIND(mtree, map, &me); + mep = RB_FIND(map, map, &me); if (mep != NULL) { - RB_REMOVE(mtree, map, mep); + RB_REMOVE(map, map, mep); free(mep); } } struct bt_arg * -map_get(struct bt_var *bv, const char *key) +map_get(struct map *map, const char *key) { - struct mtree *map = (struct mtree *)bv->bv_value; struct mentry *mep; mep = mget(map, key); @@ -124,18 +119,17 @@ map_get(struct bt_var *bv, const char *key) return mep->mval; } -void -map_insert(struct bt_var *bv, const char *key, struct bt_arg *bval) + +struct map * +map_insert(struct map *map, const char *key, struct bt_arg *bval) { - struct mtree *map = (struct mtree *)bv->bv_value; struct mentry *mep; long val; if (map == NULL) { - map = calloc(1, sizeof(struct mtree)); + map = calloc(1, sizeof(struct map)); if (map == NULL) - err(1, "mtree: calloc"); - bv->bv_value = (struct bt_arg *)map; + err(1, "map: calloc"); } mep = mget(map, key); @@ -176,6 +170,8 @@ map_insert(struct bt_var *bv, const char *key, struct bt_arg *bval) default: errx(1, "no insert support for type %d", bval->ba_type); } + + return map; } static struct bt_arg nullba = { {NULL }, (void *)0, NULL, B_AT_LONG }; @@ -183,9 +179,8 @@ static struct bt_arg maxba = { { NULL }, (void *)LONG_MAX, NULL, B_AT_LONG }; /* Print at most `top' entries of the map ordered by value. */ void -map_print(struct bt_var *bv, size_t top) +map_print(struct map *map, size_t top, const char *mapname) { - struct mtree *map = (void *)bv->bv_value; struct mentry *mep, *mcur; struct bt_arg *bhigh, *bprev; size_t i; @@ -197,7 +192,7 @@ map_print(struct bt_var *bv, size_t top) for (i = 0; i < top; i++) { mcur = NULL; bhigh = &nullba; - RB_FOREACH(mep, mtree, map) { + RB_FOREACH(mep, map, map) { if (bacmp(mep->mval, bhigh) >= 0 && bacmp(mep->mval, bprev) < 0 && mep->mval != bprev) { @@ -207,19 +202,18 @@ map_print(struct bt_var *bv, size_t top) } if (mcur == NULL) break; - printf("@%s[%s]: %s\n", bv_name(bv), mcur->mkey, + printf("@%s[%s]: %s\n", mapname, mcur->mkey, ba2str(mcur->mval, NULL)); bprev = mcur->mval; } } void -map_zero(struct bt_var *bv) +map_zero(struct map *map) { - struct mtree *map = (struct mtree *)bv->bv_value; struct mentry *mep; - RB_FOREACH(mep, mtree, map) { + RB_FOREACH(mep, map, map) { mep->mval->ba_value = 0; mep->mval->ba_type = B_AT_LONG; } -- cgit v1.2.3-59-g8ed1b