From 6ca6ae3fc4b07f82789a5ae7067b3cbd0fc03a85 Mon Sep 17 00:00:00 2001 From: mpi Date: Wed, 21 Apr 2021 10:22:36 +0000 Subject: [PATCH] Support first shell argument as $1 in order to use it in filters. Remove '-p' option now that scripts can filter by pid/tid. --- usr.sbin/btrace/TODO | 2 +- usr.sbin/btrace/bt.5 | 8 ++++++-- usr.sbin/btrace/bt_parse.y | 36 +++++++++++++++++++++++++----------- usr.sbin/btrace/btrace.8 | 9 ++------- usr.sbin/btrace/btrace.c | 36 +++++++++++++++++------------------- 5 files changed, 51 insertions(+), 40 deletions(-) diff --git a/usr.sbin/btrace/TODO b/usr.sbin/btrace/TODO index f2e8b2de46f..1e1ea83a090 100644 --- a/usr.sbin/btrace/TODO +++ b/usr.sbin/btrace/TODO @@ -9,7 +9,7 @@ Missing language features: - `args', tracepoint arguments support (requires kernel work) - str(args->buf, args->count) - 'argv' -- $1 support +- $2, $3, $n (with n > 1) support Improvements: diff --git a/usr.sbin/btrace/bt.5 b/usr.sbin/btrace/bt.5 index fab7954abf1..a70f120fd11 100644 --- a/usr.sbin/btrace/bt.5 +++ b/usr.sbin/btrace/bt.5 @@ -1,4 +1,4 @@ -.\" $OpenBSD: bt.5,v 1.10 2020/12/07 20:14:35 anton Exp $ +.\" $OpenBSD: bt.5,v 1.11 2021/04/21 10:22:36 mpi Exp $ .\" .\" Copyright (c) 2019 Martin Pieuchot .\" @@ -14,7 +14,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: December 7 2020 $ +.Dd $Mdocdate: April 21 2021 $ .Dt BT 5 .Os .Sh NAME @@ -87,6 +87,10 @@ by the associated Variable names with special meaning: .Pp .Bl -tag -width "kstack " -compact -offset indent +.It Va $N +Command line argument +.Va N +after the script name. .It Va argN Argument .Va N diff --git a/usr.sbin/btrace/bt_parse.y b/usr.sbin/btrace/bt_parse.y index 64a6cc9fc43..dc814246682 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.23 2021/02/08 09:46:45 mpi Exp $ */ +/* $OpenBSD: bt_parse.y,v 1.24 2021/04/21 10:22:36 mpi Exp $ */ /* * Copyright (c) 2019-2021 Martin Pieuchot @@ -111,13 +111,14 @@ static int pflag; %token NUMBER %type gvar +%type value %type fval testop binop builtin %type BUILTIN F_DELETE F_PRINT FUNC0 FUNC1 FUNCN OP1 OP4 %type MOP0 MOP1 %type probe probeval %type predicate %type action stmt stmtlist -%type expr vargs map mexpr printargs term condition +%type expr vargs map mexpr printargs term variable %type beginend %left '|' @@ -168,14 +169,18 @@ binop : testop | '|' { $$ = B_AT_OP_BOR; } ; +value : NUMBER { $$ = $1; } + | '$' NUMBER { $$ = get_varg($2); } + ; + predicate : /* empty */ { $$ = NULL; } - | '/' fval testop NUMBER '/' { $$ = bf_new($3, $2, $4); } - | '/' NUMBER testop fval '/' { $$ = bf_new($3, $4, $2); } - | '/' condition '/' { $$ = bc_new($2); } + | '/' fval testop value '/' { $$ = bf_new($3, $2, $4); } + | '/' value testop fval '/' { $$ = bf_new($3, $4, $2); } + | '/' variable '/' { $$ = bc_new($2); } ; -condition : gvar { $$ = bv_get($1); } - | map { $$ = $1; } +variable : gvar { $$ = bv_get($1); } + | map ; builtin : PID { $$ = B_AT_BI_PID; } @@ -185,7 +190,7 @@ builtin : PID { $$ = B_AT_BI_PID; } mexpr : MOP0 '(' ')' { $$ = ba_new(NULL, $1); } | MOP1 '(' expr ')' { $$ = ba_new($3, $1); } - | expr { $$ = $1; } + | expr ; expr : CSTRING { $$ = ba_new($1, B_AT_STR); } @@ -194,10 +199,9 @@ expr : CSTRING { $$ = ba_new($1, B_AT_STR); } term : '(' term ')' { $$ = $2; } | term binop term { $$ = ba_op($2, $1, $3); } - | NUMBER { $$ = ba_new($1, B_AT_LONG); } + | value { $$ = ba_new($1, B_AT_LONG); } | builtin { $$ = ba_new(NULL, $1); } - | gvar { $$ = bv_get($1); } - | map { $$ = $1; } + | variable gvar : '@' STRING { $$ = $2; } @@ -238,6 +242,16 @@ action : '{' stmtlist '}' { $$ = $2; } %% +int +get_varg(int index) +{ + extern int vargs[]; + + assert(index == 1); + + return vargs[index - 1]; +} + /* Create a new rule, representing "probe / filter / { action }" */ struct bt_rule * br_new(struct bt_probe *probe, struct bt_filter *filter, struct bt_stmt *head, diff --git a/usr.sbin/btrace/btrace.8 b/usr.sbin/btrace/btrace.8 index 17582cf90e6..e6c57349523 100644 --- a/usr.sbin/btrace/btrace.8 +++ b/usr.sbin/btrace/btrace.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: btrace.8,v 1.4 2021/03/21 06:44:24 jmc Exp $ +.\" $OpenBSD: btrace.8,v 1.5 2021/04/21 10:22:36 mpi Exp $ .\" .\" Copyright (c) 2019 Martin Pieuchot .\" @@ -14,7 +14,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: March 21 2021 $ +.Dd $Mdocdate: April 21 2021 $ .Dt BTRACE 8 .Os .Sh NAME @@ -23,7 +23,6 @@ .Sh SYNOPSIS .Nm btrace .Op Fl lnv -.Op Fl p Ar pid .Op Fl e Ar program | Ar file .Sh DESCRIPTION The @@ -49,10 +48,6 @@ List all available probes. .It Fl n No action. Parse the program and then exit. -.It Fl p Ar pid -Enable tracing on the indicated process ID (only one -.Fl p -flag is permitted). .It Fl v Verbose mode. Causes diff --git a/usr.sbin/btrace/btrace.c b/usr.sbin/btrace/btrace.c index 8c9b04ca17f..9d3a269f5de 100644 --- a/usr.sbin/btrace/btrace.c +++ b/usr.sbin/btrace/btrace.c @@ -1,4 +1,4 @@ -/* $OpenBSD: btrace.c,v 1.30 2021/03/21 01:24:35 jmatthew Exp $ */ +/* $OpenBSD: btrace.c,v 1.31 2021/04/21 10:22:36 mpi Exp $ */ /* * Copyright (c) 2019 - 2020 Martin Pieuchot @@ -68,8 +68,8 @@ struct dtioc_probe_info *dtpi_get_by_value(const char *, const char *, /* * Main loop and rule evaluation. */ -void rules_do(int, int); -void rules_setup(int, int); +void rules_do(int); +void rules_setup(int); void rules_apply(struct dt_evt *); void rules_teardown(int); void rule_eval(struct bt_rule *, struct dt_evt *); @@ -108,6 +108,7 @@ void debug_dump_filter(struct bt_rule *); struct dtioc_probe_info *dt_dtpis; /* array of available probes */ size_t dt_ndtpi; /* # of elements in the array */ +int vargs[1]; int verbose = 0; volatile sig_atomic_t quit_pending; @@ -124,7 +125,7 @@ main(int argc, char *argv[]) int fd = -1, ch, error = 0; const char *filename = NULL, *btscript = NULL; const char *errstr; - int showprobes = 0, tracepid = -1, noaction = 0; + int showprobes = 0, noaction = 0; setlocale(LC_ALL, ""); @@ -144,13 +145,6 @@ main(int argc, char *argv[]) case 'n': noaction = 1; break; - case 'p': - if (tracepid != -1) - usage(); - tracepid = strtonum(optarg, 1, INT_MAX, &errstr); - if (errstr != NULL) - errx(1, "invalid pid %s: %s", optarg, errstr); - break; case 'v': verbose++; break; @@ -172,6 +166,14 @@ main(int argc, char *argv[]) argv++; } + if (argc == 1) { + vargs[0] = strtonum(*argv, 1, INT_MAX, &errstr); + if (errstr != NULL) + errx(1, "invalid argument %s: %s", *argv, errstr); + argc--; + argv++; + } + if (argc != 0 || (btscript == NULL && !showprobes)) usage(); @@ -196,7 +198,7 @@ main(int argc, char *argv[]) } if (!TAILQ_EMPTY(&g_rules)) - rules_do(fd, tracepid); + rules_do(fd); if (fd != -1) close(fd); @@ -331,7 +333,7 @@ dtpi_get_by_value(const char *prov, const char *func, const char *name) } void -rules_do(int fd, int tracepid) +rules_do(int fd) { struct sigaction sa; @@ -342,7 +344,7 @@ rules_do(int fd, int tracepid) if (sigaction(SIGINT, &sa, NULL)) err(1, "sigaction"); - rules_setup(fd, tracepid); + rules_setup(fd); while (!quit_pending && g_nprobes > 0) { static struct dt_evt devtbuf[64]; @@ -405,7 +407,7 @@ dvar2dt(enum bt_filtervar var) void -rules_setup(int fd, int tracepid) +rules_setup(int fd) { struct dtioc_probe_info *dtpi; struct dtioc_req *dtrq; @@ -445,10 +447,6 @@ rules_setup(int fd, int tracepid) dtrq->dtrq_filter.dtf_operand = dop2dt(df->bf_op); dtrq->dtrq_filter.dtf_variable = dvar2dt(df->bf_var); dtrq->dtrq_filter.dtf_value = df->bf_val; - } else if (tracepid != -1) { - dtrq->dtrq_filter.dtf_operand = DT_OP_EQ; - dtrq->dtrq_filter.dtf_variable = DT_FV_PID; - dtrq->dtrq_filter.dtf_value = tracepid; } dtrq->dtrq_rate = r->br_probe->bp_rate; -- 2.20.1