Make it possible to store the kstack or ustack in a map (as value, not key).
authorclaudio <claudio@openbsd.org>
Tue, 27 Jun 2023 14:17:00 +0000 (14:17 +0000)
committerclaudio <claudio@openbsd.org>
Tue, 27 Jun 2023 14:17:00 +0000 (14:17 +0000)
Additionally fix the bacmp() function to work on integers and strings.
bacmp() is used when maps are printed out since the output is sorted by value.
Also adjust the rule parser to look into correctly into if branches to figure
out which values to request from the kernel.
OK kn@

usr.sbin/btrace/btrace.c
usr.sbin/btrace/map.c

index 21c9188..25333f9 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: btrace.c,v 1.70 2023/05/12 14:14:16 claudio Exp $ */
+/*     $OpenBSD: btrace.c,v 1.71 2023/06/27 14:17:00 claudio Exp $ */
 
 /*
  * Copyright (c) 2019 - 2021 Martin Pieuchot <mpi@openbsd.org>
@@ -450,6 +450,37 @@ rules_do(int fd)
        }
 }
 
+static uint64_t
+rules_action_scan(struct bt_stmt *bs)
+{
+       struct bt_arg *ba;
+       uint64_t evtflags = 0;
+
+       while (bs != NULL) {
+               SLIST_FOREACH(ba, &bs->bs_args, ba_next)
+                       evtflags |= ba2dtflags(ba);
+
+               /* Also check the value for map/hist insertion */
+               switch (bs->bs_act) {
+               case B_AC_BUCKETIZE:
+               case B_AC_INSERT:
+                       ba = (struct bt_arg *)bs->bs_var;
+                       evtflags |= ba2dtflags(ba);
+                       break;
+               case B_AC_TEST:
+                       evtflags |= rules_action_scan(
+                           (struct bt_stmt *)bs->bs_var);
+                       break;
+               default:
+                       break;
+               }
+
+               bs = SLIST_NEXT(bs, bs_next);
+       }
+
+       return evtflags;
+}
+
 void
 rules_setup(int fd)
 {
@@ -474,21 +505,7 @@ rules_setup(int fd)
                        evtflags |= ba2dtflags(ba);
                }
 
-               SLIST_FOREACH(bs, &r->br_action, bs_next) {
-                       SLIST_FOREACH(ba, &bs->bs_args, ba_next)
-                               evtflags |= ba2dtflags(ba);
-
-                       /* Also check the value for map/hist insertion */
-                       switch (bs->bs_act) {
-                       case B_AC_BUCKETIZE:
-                       case B_AC_INSERT:
-                               ba = (struct bt_arg *)bs->bs_var;
-                               evtflags |= ba2dtflags(ba);
-                               break;
-                       default:
-                               break;
-                       }
-               }
+               evtflags |= rules_action_scan(SLIST_FIRST(&r->br_action));
 
                SLIST_FOREACH(bp, &r->br_probes, bp_next) {
                        debug("parsed probe '%s'", debug_probe_name(bp));
@@ -1685,10 +1702,17 @@ ba2dtflags(struct bt_arg *ba)
 long
 bacmp(struct bt_arg *a, struct bt_arg *b)
 {
-       assert(a->ba_type == b->ba_type);
-       assert(a->ba_type == B_AT_LONG);
+       if (a->ba_type != b->ba_type)
+               return a->ba_type - b->ba_type;
 
-       return ba2long(a, NULL) - ba2long(b, NULL);
+       switch (a->ba_type) {
+       case B_AT_LONG:
+               return ba2long(a, NULL) - ba2long(b, NULL);
+       case B_AT_STR:
+               return strcmp(ba2str(a, NULL), ba2str(b, NULL));
+       default:
+               errx(1, "no compare support for type %d", a->ba_type);
+       }
 }
 
 __dead void
index c35d2a4..be416e2 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: map.c,v 1.20 2022/04/30 01:29:05 tedu Exp $ */
+/*     $OpenBSD: map.c,v 1.21 2023/06/27 14:17:00 claudio Exp $ */
 
 /*
  * Copyright (c) 2020 Martin Pieuchot <mpi@openbsd.org>
@@ -176,6 +176,11 @@ map_insert(struct map *map, const char *key, struct bt_arg *bval,
                val += ba2long(bval->ba_value, dtev);
                mep->mval->ba_value = (void *)val;
                break;
+       case B_AT_BI_KSTACK:
+       case B_AT_BI_USTACK:
+               free(mep->mval);
+               mep->mval = ba_new(ba2str(bval, dtev), B_AT_STR);
+               break;
        default:
                errx(1, "no insert support for type %d", bval->ba_type);
        }