diff options
author | mpi <mpi@openbsd.org> | 2020-01-27 14:15:25 +0000 |
---|---|---|
committer | mpi <mpi@openbsd.org> | 2020-01-27 14:15:25 +0000 |
commit | a378a2aa206b37b7de1f40f09d4797595a2afb4c (patch) | |
tree | 6cf0b412b48af1a747c7fe4407ed915c9da7d2c8 /usr.sbin/btrace | |
parent | Rename advminphys function to adv_minphys. The prototype and one usage were (diff) | |
download | wireguard-openbsd-a378a2aa206b37b7de1f40f09d4797595a2afb4c.tar.xz wireguard-openbsd-a378a2aa206b37b7de1f40f09d4797595a2afb4c.zip |
Implement builtin time() function.
Diffstat (limited to 'usr.sbin/btrace')
-rw-r--r-- | usr.sbin/btrace/TODO | 1 | ||||
-rw-r--r-- | usr.sbin/btrace/bt.5 | 22 | ||||
-rw-r--r-- | usr.sbin/btrace/bt_parse.y | 6 | ||||
-rw-r--r-- | usr.sbin/btrace/bt_parser.h | 3 | ||||
-rw-r--r-- | usr.sbin/btrace/btrace.c | 41 |
5 files changed, 65 insertions, 8 deletions
diff --git a/usr.sbin/btrace/TODO b/usr.sbin/btrace/TODO index ba0ee5aa0dd..4cd60171f47 100644 --- a/usr.sbin/btrace/TODO +++ b/usr.sbin/btrace/TODO @@ -5,7 +5,6 @@ Missing features: - scratch variable ($name) - `args', tracepoint arguments support (requires kernel work) - str() -- time() - @ = sum(x) - @ = hist(x) - @ = lhist(x, min, max, step) diff --git a/usr.sbin/btrace/bt.5 b/usr.sbin/btrace/bt.5 index 8a811445de0..5cf08c58f72 100644 --- a/usr.sbin/btrace/bt.5 +++ b/usr.sbin/btrace/bt.5 @@ -1,4 +1,4 @@ -.\" $OpenBSD: bt.5,v 1.1 2020/01/21 16:24:55 mpi Exp $ +.\" $OpenBSD: bt.5,v 1.2 2020/01/27 14:15:25 mpi Exp $ .\" .\" Copyright (c) 2019 Martin Pieuchot <mpi@openbsd.org> .\" @@ -14,12 +14,12 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: January 21 2020 $ +.Dd $Mdocdate: January 27 2020 $ .Dt BT 5 .Os .Sh NAME .Nm BT -.Nd B Tracing language +.Nd Bug Tracing language .Sh SYNTAX .D1 Ar probe Ic \&/ Ar filter Ic \&/ \&{ Ar action Ic \&} .Sh DESCRIPTION @@ -104,6 +104,22 @@ Argument .Va N of the corresponding probe .El +.Pp +Functions: +.Pp +.Bl -tag -width "printf fmt ... " -compact +.It Fn clear "@map" +Delete all (key, value) pairs from map +.Va @map +.It Fn print "@map" +Print all (key, value) pairs from map +.Va @map +.It Fn printf "fmt" ... +print formatted string +.Va fmt +.It Fn time +print formatted time +.El .Sh SEE ALSO .Xr awk 1 , .Xr dt 4 , diff --git a/usr.sbin/btrace/bt_parse.y b/usr.sbin/btrace/bt_parse.y index e6f50f0cab4..6b17b0ca3ad 100644 --- a/usr.sbin/btrace/bt_parse.y +++ b/usr.sbin/btrace/bt_parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: bt_parse.y,v 1.1 2020/01/21 16:24:55 mpi Exp $ */ +/* $OpenBSD: bt_parse.y,v 1.2 2020/01/27 14:15:25 mpi Exp $ */ /* * Copyright (c) 2019 - 2020 Martin Pieuchot <mpi@openbsd.org> @@ -100,7 +100,7 @@ static int yylex(void); %token ARG0 ARG1 ARG2 ARG3 ARG4 ARG5 ARG6 ARG7 ARG8 ARG9 %token COMM HZ KSTACK USTACK NSECS PID RETVAL TID /* Functions */ -%token F_CLEAR F_DELETE F_EXIT F_PRINT F_PRINTF F_ZERO +%token F_CLEAR F_DELETE F_EXIT F_PRINT F_PRINTF F_TIME F_ZERO /* Map funcitons */ %token M_COUNT %token <v.string> STRING CSTRING @@ -170,6 +170,7 @@ func0 : F_EXIT { $$ = B_AC_EXIT; } ; func1 : F_CLEAR { $$ = B_AC_CLEAR; } + | F_TIME { $$ = B_AC_TIME; } | F_ZERO { $$ = B_AC_ZERO; } ; @@ -500,6 +501,7 @@ lookup(char *s) { "printf", F_PRINTF }, { "retval", RETVAL }, { "tid", TID }, + { "time", F_TIME }, { "ustack", USTACK }, { "zero", F_ZERO }, }; diff --git a/usr.sbin/btrace/bt_parser.h b/usr.sbin/btrace/bt_parser.h index fd800577ca4..4083faddbc6 100644 --- a/usr.sbin/btrace/bt_parser.h +++ b/usr.sbin/btrace/bt_parser.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bt_parser.h,v 1.1 2020/01/21 16:24:55 mpi Exp $ */ +/* $OpenBSD: bt_parser.h,v 1.2 2020/01/27 14:15:25 mpi Exp $ */ /* * Copyright (c) 2019 Martin Pieuchot <mpi@openbsd.org> @@ -147,6 +147,7 @@ struct bt_stmt { B_AC_EXIT, /* exit() */ B_AC_PRINT, /* print(@map, 10) */ B_AC_PRINTF, /* printf("hello!\n") */ + B_AC_TIME, /* time("%H:%M:%S ") */ B_AC_ZERO, /* zero(@map) */ } bs_act; }; diff --git a/usr.sbin/btrace/btrace.c b/usr.sbin/btrace/btrace.c index 006fd6628ae..13175d0dba3 100644 --- a/usr.sbin/btrace/btrace.c +++ b/usr.sbin/btrace/btrace.c @@ -1,4 +1,4 @@ -/* $OpenBSD: btrace.c,v 1.1 2020/01/21 16:24:55 mpi Exp $ */ +/* $OpenBSD: btrace.c,v 1.2 2020/01/27 14:15:25 mpi Exp $ */ /* * Copyright (c) 2019 - 2020 Martin Pieuchot <mpi@openbsd.org> @@ -32,6 +32,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <time.h> #include <unistd.h> #include <dev/dt/dtvar.h> @@ -80,6 +81,7 @@ void stmt_delete(struct bt_stmt *, struct dt_evt *); void stmt_insert(struct bt_stmt *, struct dt_evt *); void stmt_print(struct bt_stmt *, struct dt_evt *); void stmt_store(struct bt_stmt *, struct dt_evt *); +void stmt_time(struct bt_stmt *, struct dt_evt *); void stmt_zero(struct bt_stmt *); struct bt_arg *ba_read(struct bt_arg *); long ba2long(struct bt_arg *, struct dt_evt *); @@ -566,6 +568,9 @@ rule_eval(struct bt_rule *r, struct dt_evt *dtev) case B_AC_PRINTF: stmt_printf(bs, dtev); break; + case B_AC_TIME: + stmt_time(bs, dtev); + break; case B_AC_ZERO: stmt_zero(bs); break; @@ -575,6 +580,19 @@ rule_eval(struct bt_rule *r, struct dt_evt *dtev) } } +time_t +builtin_gettime(struct dt_evt *dtev) +{ + struct timespec ts; + + if (dtev == NULL) { + clock_gettime(CLOCK_REALTIME, &ts); + return ts.tv_sec; + } + + return dtev->dtev_tsp.tv_sec; +} + static inline uint64_t TIMESPEC_TO_NSEC(struct timespec *ts) { @@ -750,6 +768,27 @@ stmt_store(struct bt_stmt *bs, struct dt_evt *dtev) debug("bv=%p var '%s' store (%p) \n", bv, bv->bv_name, bv->bv_value); } +/* + * Print time: { time("%H:%M:%S"); } + */ +void +stmt_time(struct bt_stmt *bs, struct dt_evt *dtev) +{ + struct bt_arg *ba = SLIST_FIRST(&bs->bs_args); + time_t time; + struct tm *tm; + char buf[64]; + + assert(bs->bs_var == NULL); + assert(ba->ba_type = B_AT_STR); + assert(strlen(ba2str(ba, dtev)) < (sizeof(buf) - 1)); + + time = builtin_gettime(dtev); + tm = localtime(&time); + strftime(buf, sizeof(buf), ba2str(ba, dtev), tm); + printf("%s", buf); +} + void stmt_zero(struct bt_stmt *bs) { |