On Tue, Jan 18, 2022 at 01:03:04AM +0100, Alexander Hall wrote:
> On Mon, Jan 17, 2022 at 02:23:52PM +0000, Klemens Nanni wrote:
> > I keep doing `btrace -l | sort' to help myself searching for traces.
> >
> > Would it be worh making btrace sort the list of probes
> > lexicographically in the first place or is there any value in seeing
> > them sorted by id?
>
> I believe you're ruining the party for dtpi_get_by_id(), which only
> seems to care about the id.
Thanks, btrace would just print the probe'd id when using the `probe'
variable, i.e.
btrace -e 'syscall:ioctl:entry { @[probe] = count(); }'
would print
@[109]: 4
rather than
@[syscall:ioctl:entry]: 4
The new diff uses a second name based compare function to sort and
linear-searches `probe' names via now that the global list is sorted by
name not id anymore.
Index: btrace.c
===================================================================
RCS file: /cvs/src/usr.sbin/btrace/btrace.c,v
retrieving revision 1.61
diff -u -p -r1.61 btrace.c
--- btrace.c 7 Dec 2021 22:17:03 -0000 1.61
+++ btrace.c 18 Jan 2022 03:55:06 -0000
@@ -29,6 +29,7 @@
#include <limits.h>
#include <locale.h>
#include <paths.h>
+#include <search.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
@@ -62,7 +63,7 @@ char *read_btfile(const char *, size_t
*/
void dtpi_cache(int);
void dtpi_print_list(void);
-const char *dtpi_func(struct dtioc_probe_info *);
+const char *dtpi_func(const struct dtioc_probe_info *);
int dtpi_is_unit(const char *);
struct dtioc_probe_info *dtpi_get_by_value(const char *, const char *,
const char *);
@@ -257,7 +258,7 @@ read_btfile(const char *filename, size_t
}
static int
-dtpi_cmp(const void *a, const void *b)
+dtpi_cmp_id(const void *a, const void *b)
{
const struct dtioc_probe_info *ai = a, *bi = b;
@@ -267,6 +268,32 @@ dtpi_cmp(const void *a, const void *b)
return -1;
return 0;
}
+static int
+dtpi_cmp_str(const void *a, const void *b)
+{
+ const struct dtioc_probe_info *ai = a, *bi = b;
+ int i;
+
+ i = strcmp(ai->dtpi_prov, bi->dtpi_prov);
+ if (i > 0)
+ return 1;
+ if (i < 0)
+ return -1;
+
+ i = strcmp(dtpi_func(ai), dtpi_func(bi));
+ if (i > 0)
+ return 1;
+ if (i < 0)
+ return -1;
+
+ i = strcmp(ai->dtpi_name, bi->dtpi_name);
+ if (i > 0)
+ return 1;
+ if (i < 0)
+ return -1;
+
+ return 0;
+}
void
dtpi_cache(int fd)
@@ -289,7 +316,7 @@ dtpi_cache(int fd)
if (ioctl(fd, DTIOCGPLIST, &dtpr))
err(1, "DTIOCGPLIST");
- qsort(dt_dtpis, dt_ndtpi, sizeof(*dt_dtpis), dtpi_cmp);
+ qsort(dt_dtpis, dt_ndtpi, sizeof(*dt_dtpis), dtpi_cmp_str);
}
void
@@ -306,7 +333,7 @@ dtpi_print_list(void)
}
const char *
-dtpi_func(struct dtioc_probe_info *dtpi)
+dtpi_func(const struct dtioc_probe_info *dtpi)
{
char *sysnb, func[DTNAMESIZE];
const char *errstr;
@@ -371,7 +398,7 @@ dtpi_get_by_id(unsigned int pbn)
struct dtioc_probe_info d;
d.dtpi_pbn = pbn;
- return bsearch(&d, dt_dtpis, dt_ndtpi, sizeof(*dt_dtpis), dtpi_cmp);
+ return lfind(&d, dt_dtpis, &dt_ndtpi, sizeof(*dt_dtpis), dtpi_cmp_id);
}
void