From: dv Date: Sat, 2 Sep 2023 15:16:12 +0000 (+0000) Subject: btrace(8): fix probe builtin in BEGIN/END. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=9e79f3a0ebd11a25b4bff61e900cb6de9e7795e9;p=openbsd btrace(8): fix probe builtin in BEGIN/END. Resolves segfaults when using probe within BEGIN or END. ok mpi@ --- diff --git a/regress/usr.sbin/btrace/beginend.bt b/regress/usr.sbin/btrace/beginend.bt index a2469966995..fb603b1555a 100644 --- a/regress/usr.sbin/btrace/beginend.bt +++ b/regress/usr.sbin/btrace/beginend.bt @@ -3,11 +3,11 @@ BEGIN { - printf("Hello from BEGIN\n"); + printf("Hello from %s\n", probe); } END { // Comment inside of a block - printf("Bye from END\n"); + printf("Bye from %s\n", probe); } diff --git a/usr.sbin/btrace/btrace.c b/usr.sbin/btrace/btrace.c index 45e485b6c48..f57091107e6 100644 --- a/usr.sbin/btrace/btrace.c +++ b/usr.sbin/btrace/btrace.c @@ -1,4 +1,4 @@ -/* $OpenBSD: btrace.c,v 1.74 2023/08/16 13:07:11 dv Exp $ */ +/* $OpenBSD: btrace.c,v 1.75 2023/09/02 15:16:12 dv Exp $ */ /* * Copyright (c) 2019 - 2021 Martin Pieuchot @@ -117,6 +117,8 @@ size_t dt_ndtpi; /* # of elements in the array */ struct dtioc_arg_info **dt_args; /* array of probe arguments */ struct dt_evt bt_devt; /* fake event for BEGIN/END */ +#define EVENT_BEGIN 0 +#define EVENT_END (unsigned int)(-1) uint64_t bt_filtered; /* # of events filtered out */ struct syms *kelf, *uelf; @@ -543,7 +545,7 @@ rules_setup(int fd) kelf = kelf_open(_PATH_KSYMS); /* Initialize "fake" event for BEGIN/END */ - bt_devt.dtev_pbn = -1; + bt_devt.dtev_pbn = EVENT_BEGIN; strlcpy(bt_devt.dtev_comm, getprogname(), sizeof(bt_devt.dtev_comm)); bt_devt.dtev_pid = getpid(); bt_devt.dtev_tid = getthrid(); @@ -623,6 +625,7 @@ rules_teardown(int fd) uelf = NULL; /* Update "fake" event for BEGIN/END */ + bt_devt.dtev_pbn = EVENT_END; clock_gettime(CLOCK_REALTIME, &bt_devt.dtev_tsp); if (rend) @@ -1600,6 +1603,13 @@ ba2str(struct bt_arg *ba, struct dt_evt *dtev) str = buf; break; case B_AT_BI_PROBE: + if (dtev->dtev_pbn == EVENT_BEGIN) { + str = "BEGIN"; + break; + } else if (dtev->dtev_pbn == EVENT_END) { + str = "END"; + break; + } dtpi = &dt_dtpis[dtev->dtev_pbn - 1]; if (dtpi != NULL) snprintf(buf, sizeof(buf), "%s:%s:%s",