Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp629238rwr; Thu, 20 Apr 2023 04:28:40 -0700 (PDT) X-Google-Smtp-Source: AKy350Yv84I04JfbB8hTzsv16en6YqxEqtzU0U4kd8OE/83tJrUfFANSEVlG/QoJEKGszd7cU1ob X-Received: by 2002:a05:6a00:1150:b0:63d:23dc:7760 with SMTP id b16-20020a056a00115000b0063d23dc7760mr1088027pfm.2.1681990119627; Thu, 20 Apr 2023 04:28:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681990119; cv=none; d=google.com; s=arc-20160816; b=bOeXYy1aTPpiURUHsHiA1+yk9E8pFmoUYlFwzU4xtkVmLTlzPNA9l2pGt/x++2dGAm 6Ecn0Fkie381dlxLVLdOeZ+i8j4aVrMGG+JnLTBmEASTICgxROTbqxdSsjFhVWVLBTQH 9NMR7Pe62rSSNbsD8iUse5mrWR0rQwpI+LB+t7WS2K4Cdc/hn7x3eHsETkYdBFiM0vWw uH2+/rfmKqLibTLVLAuxAIwRH5G0vvbuLiT+2ovnbvbR8ZrHt89hyfLQCy8lV3/n16cl U1MTUBhKismI+8vBLmofUOgmydBOFcW6sghnuo5TI7pm5voN35zmOSj6AecSYyVA9eBN u8Sw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=B+Lg4T3PuKoef705SAHEz3zZTtQLyxIERmhn48MF6jY=; b=Vc8yrLgp3EG3gk9clY4HP9ZLg6oz2G5AQtVnO0Z59cj8SiAlQ7gOWSClzldQRnJPQ1 gE3e8nkyd/ActWWXNTvdnR++JHKXA+Pud0XYvvnfc78iEGaFLcFyLd0uNvbxyxi+sO1t GEuYDFXwCtVs4G0vqb89zZPxyNfUWv6vyJVnhcP5tyRHLjxsxa32cIfPQP/fEcxNr/+M fYviQrddtF3lzUF7ll8w08DaWigWyCgzvUn/XECHn6JRSxNXSw6DJjeP6Ef8MlTFrn/r rdmefCgSg4hLwjcKYkGF7oWRBdht0byGZpheX12BjWxg/O6oeGm4U17vwtmX85fO5hmg JvIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KaEUyXjS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k191-20020a6384c8000000b00520538cbd30si1295575pgd.682.2023.04.20.04.28.25; Thu, 20 Apr 2023 04:28:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KaEUyXjS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234791AbjDTL1r (ORCPT + 99 others); Thu, 20 Apr 2023 07:27:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234704AbjDTL1B (ORCPT ); Thu, 20 Apr 2023 07:27:01 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5ED86E4A; Thu, 20 Apr 2023 04:26:45 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E07A561447; Thu, 20 Apr 2023 11:26:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 99BFFC433EF; Thu, 20 Apr 2023 11:26:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1681990004; bh=QZYnfbeALSvSIgbnqQEltOWDeVPaOq+fJuxwhAyhMIk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KaEUyXjSaZMCQY3mV7AQEgk2EWbwFGJNWXD/Qt3a5c9eTR6FnqBVj2UfeIRbUL0Nd A4UQoGHZxUCac2AtZ2ys+bL0MiiADoulEj3HLE8cze24vZlDLXtz+lQoZ724AOOHz+ AUJl8uMGNBhuFzM84ocDGYK4wOX5T1t9i0GPB3KSZUeF9VimLuY6AuHv1DhQmX7dB1 JttLUlq00azZ3zXe4Roiov02dbpJG/9quvNuQ60PxXMz2IWlBUHCKHFbl+RzySCDPd 2vwWg784XuyFbM3OrVPDs82+yZAPk/HsKEzhIqERMnlceZz4s9GknTJEqrGya+CvnU DIxlhsGk+NXxQ== From: "Masami Hiramatsu (Google)" To: linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Steven Rostedt , mhiramat@kernel.org, Florent Revest , Mark Rutland , Will Deacon , Mathieu Desnoyers , Martin KaFai Lau , bpf@vger.kernel.org Subject: [PATCH v5 7/9] tracing/probes: Add $$args meta argument for all function args Date: Thu, 20 Apr 2023 20:26:39 +0900 Message-ID: <168198999987.1795549.14741801929317161436.stgit@mhiramat.roam.corp.google.com> X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog In-Reply-To: <168198993129.1795549.8306571027057356176.stgit@mhiramat.roam.corp.google.com> References: <168198993129.1795549.8306571027057356176.stgit@mhiramat.roam.corp.google.com> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Masami Hiramatsu (Google) Add the '$$args' fetch argument for function-entry probe events. This will be expanded to all arguments using BTF function argument information. e.g. # echo 'p vfs_read $$args' >> dynamic_events # echo 'f vfs_write $$args' >> dynamic_events # echo 't sched_overutilized_tp $$args' >> dynamic_events # cat dynamic_events p:kprobes/p_vfs_read_0 vfs_read file=file buf=buf count=count pos=pos f:fprobes/vfs_write__entry vfs_write file=file buf=buf count=count pos=pos t:tracepoints/sched_overutilized_tp sched_overutilized_tp rd=rd overutilized=overutilized Signed-off-by: Masami Hiramatsu (Google) --- kernel/trace/trace_fprobe.c | 21 ++++++++-- kernel/trace/trace_kprobe.c | 23 +++++++++-- kernel/trace/trace_probe.c | 93 +++++++++++++++++++++++++++++++++++++++++++ kernel/trace/trace_probe.h | 9 ++++ 4 files changed, 138 insertions(+), 8 deletions(-) diff --git a/kernel/trace/trace_fprobe.c b/kernel/trace/trace_fprobe.c index d88079c2d2e3..3d0b96862963 100644 --- a/kernel/trace/trace_fprobe.c +++ b/kernel/trace/trace_fprobe.c @@ -944,14 +944,16 @@ static int __trace_fprobe_create(int argc, const char *argv[]) * FETCHARG:TYPE : use TYPE instead of unsigned long. */ struct trace_fprobe *tf = NULL; - int i, len, ret = 0; + int i, len, new_argc = 0, ret = 0; bool is_return = false; char *symbol = NULL, *tmp = NULL; const char *event = NULL, *group = FPROBE_EVENT_SYSTEM; + const char **new_argv = NULL; int maxactive = 0; char buf[MAX_EVENT_NAME_LEN]; char gbuf[MAX_EVENT_NAME_LEN]; char sbuf[KSYM_NAME_LEN]; + char abuf[MAX_BTF_ARGS_LEN]; bool is_tracepoint = false; struct tracepoint *tpoint = NULL; struct traceprobe_parse_context ctx = { @@ -1057,9 +1059,22 @@ static int __trace_fprobe_create(int argc, const char *argv[]) } else ctx.funcname = symbol; + argc -= 2; argv += 2; + new_argv = traceprobe_expand_meta_args(argc, argv, &new_argc, + abuf, MAX_BTF_ARGS_LEN, &ctx); + if (IS_ERR(new_argv)) { + ret = PTR_ERR(new_argv); + new_argv = NULL; + goto out; + } + if (new_argv) { + argc = new_argc; + argv = new_argv; + } + /* setup a probe */ tf = alloc_trace_fprobe(group, event, symbol, tpoint, maxactive, - argc - 2, is_return); + argc, is_return); if (IS_ERR(tf)) { ret = PTR_ERR(tf); /* This must return -ENOMEM, else there is a bug */ @@ -1071,7 +1086,6 @@ static int __trace_fprobe_create(int argc, const char *argv[]) tf->mod = __module_text_address( (unsigned long)tf->tpoint->probestub); - argc -= 2; argv += 2; /* parse arguments */ for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { trace_probe_log_set_index(i + 2); @@ -1100,6 +1114,7 @@ static int __trace_fprobe_create(int argc, const char *argv[]) out: trace_probe_log_clear(); + kfree(new_argv); kfree(symbol); return ret; diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c index aff6c1a5e161..2d7c0188c2b1 100644 --- a/kernel/trace/trace_kprobe.c +++ b/kernel/trace/trace_kprobe.c @@ -732,9 +732,10 @@ static int __trace_kprobe_create(int argc, const char *argv[]) * FETCHARG:TYPE : use TYPE instead of unsigned long. */ struct trace_kprobe *tk = NULL; - int i, len, ret = 0; + int i, len, new_argc = 0, ret = 0; bool is_return = false; char *symbol = NULL, *tmp = NULL; + const char **new_argv = NULL; const char *event = NULL, *group = KPROBE_EVENT_SYSTEM; enum probe_print_type ptype; int maxactive = 0; @@ -742,6 +743,7 @@ static int __trace_kprobe_create(int argc, const char *argv[]) void *addr = NULL; char buf[MAX_EVENT_NAME_LEN]; char gbuf[MAX_EVENT_NAME_LEN]; + char abuf[MAX_BTF_ARGS_LEN]; struct traceprobe_parse_context ctx = { .flags = TPARG_FL_KERNEL }; switch (argv[0][0]) { @@ -854,19 +856,31 @@ static int __trace_kprobe_create(int argc, const char *argv[]) event = buf; } + argc -= 2; argv += 2; + ctx.funcname = symbol; + new_argv = traceprobe_expand_meta_args(argc, argv, &new_argc, + abuf, MAX_BTF_ARGS_LEN, &ctx); + if (IS_ERR(new_argv)) { + ret = PTR_ERR(new_argv); + new_argv = NULL; + goto out; + } + if (new_argv) { + argc = new_argc; + argv = new_argv; + } + /* setup a probe */ tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive, - argc - 2, is_return); + argc, is_return); if (IS_ERR(tk)) { ret = PTR_ERR(tk); /* This must return -ENOMEM, else there is a bug */ WARN_ON_ONCE(ret != -ENOMEM); goto out; /* We know tk is not allocated */ } - argc -= 2; argv += 2; /* parse arguments */ - ctx.funcname = symbol; for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { trace_probe_log_set_index(i + 2); ctx.offset = 0; @@ -894,6 +908,7 @@ static int __trace_kprobe_create(int argc, const char *argv[]) out: trace_probe_log_clear(); + kfree(new_argv); kfree(symbol); return ret; diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c index f55d633b3e2a..c75b6a1bd5ea 100644 --- a/kernel/trace/trace_probe.c +++ b/kernel/trace/trace_probe.c @@ -452,12 +452,18 @@ static const struct fetch_type *parse_btf_arg_type(int arg_idx, return find_fetch_type(typestr, ctx->flags); } + #else static struct btf *traceprobe_get_btf(void) { return NULL; } +static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr) +{ + return ERR_PTR(-EOPNOTSUPP); +} + static int parse_btf_arg(const char *varname, struct fetch_insn *code, struct traceprobe_parse_context *ctx) { @@ -1081,6 +1087,93 @@ void traceprobe_free_probe_arg(struct probe_arg *arg) kfree(arg->fmt); } +/* Return new_argv which must be freed after use */ +const char **traceprobe_expand_meta_args(int argc, const char *argv[], + int *new_argc, char *buf, int bufsize, + struct traceprobe_parse_context *ctx) +{ + struct btf *btf = traceprobe_get_btf(); + const struct btf_param *params = NULL; + int i, j, used, ret, args_idx = -1; + const char **new_argv = NULL; + int nr_skipped; + + /* The first argument of tracepoint should be skipped. */ + nr_skipped = ctx->flags & TPARG_FL_TPOINT ? 1 : 0; + for (i = 0; i < argc; i++) + if (!strcmp(argv[i], "$$args")) { + trace_probe_log_set_index(i + 2); + + if (!tparg_is_function_entry(ctx->flags)) { + trace_probe_log_err(0, NOFENTRY_ARGS); + return ERR_PTR(-EINVAL); + } + + if (args_idx >= 0) { + trace_probe_log_err(0, DOUBLE_ARGS); + return ERR_PTR(-EINVAL); + } + + args_idx = i; + params = find_btf_func_param(ctx->funcname, &ctx->nr_params); + if (IS_ERR(params)) { + trace_probe_log_err(0, NOSUP_BTFARG); + return (const char **)params; + } + ctx->params = params; + } + + /* If target has no arguments, return NULL and the original argc. */ + if (args_idx < 0 || ctx->nr_params < nr_skipped) { + *new_argc = argc; + return NULL; + } + + *new_argc = argc - 1 + ctx->nr_params - nr_skipped; + + new_argv = kcalloc(*new_argc, sizeof(char *), GFP_KERNEL); + if (!new_argv) + return ERR_PTR(-ENOMEM); + + for (i = 0; i < args_idx; i++) + new_argv[i] = argv[i]; + + used = 0; + trace_probe_log_set_index(args_idx + 2); + for (i = 0; i < ctx->nr_params - nr_skipped; i++) { + const char *name; + + name = btf_name_by_offset(btf, params[i + nr_skipped].name_off); + if (!name) { + trace_probe_log_err(0, NO_BTF_ENTRY); + ret = -ENOENT; + goto error; + } + ret = snprintf(buf + used, bufsize - used, "%s", name); + if (ret >= bufsize - used) { + trace_probe_log_err(0, ARGS_2LONG); + ret = -E2BIG; + goto error; + } + new_argv[args_idx + i] = buf + used; + used += ret + 1; /* include null byte */ + } + + /* Note: we have to skip $$args */ + j = args_idx + ctx->nr_params - nr_skipped; + for (i = args_idx + 1; i < argc; i++, j++) { + if (WARN_ON(j >= *new_argc)) + goto error; + new_argv[j] = argv[i]; + } + + return new_argv; + +error: + kfree(new_argv); + return ERR_PTR(ret); +} + int traceprobe_update_arg(struct probe_arg *arg) { struct fetch_insn *code = arg->code; diff --git a/kernel/trace/trace_probe.h b/kernel/trace/trace_probe.h index 9ea5c7e8753f..8c5b029c5d62 100644 --- a/kernel/trace/trace_probe.h +++ b/kernel/trace/trace_probe.h @@ -33,6 +33,7 @@ #define MAX_ARGSTR_LEN 63 #define MAX_ARRAY_LEN 64 #define MAX_ARG_NAME_LEN 32 +#define MAX_BTF_ARGS_LEN 128 #define MAX_STRING_SIZE PATH_MAX /* Reserved field names */ @@ -387,6 +388,9 @@ struct traceprobe_parse_context { extern int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *argv, struct traceprobe_parse_context *ctx); +const char **traceprobe_expand_meta_args(int argc, const char *argv[], + int *new_argc, char *buf, int bufsize, + struct traceprobe_parse_context *ctx); extern int traceprobe_update_arg(struct probe_arg *arg); extern void traceprobe_free_probe_arg(struct probe_arg *arg); @@ -481,7 +485,10 @@ extern int traceprobe_define_arg_fields(struct trace_event_call *event_call, C(NO_EP_FILTER, "No filter rule after 'if'"), \ C(NOSUP_BTFARG, "BTF is not available or not supported"), \ C(NO_BTFARG, "This variable is not found at this probe point"),\ - C(NO_BTF_ENTRY, "No BTF entry for this probe point"), + C(NO_BTF_ENTRY, "No BTF entry for this probe point"), \ + C(NOFENTRY_ARGS, "$$args can be used only on function entry"), \ + C(DOUBLE_ARGS, "$$args can be used only once in the parameter"), \ + C(ARGS_2LONG, "$$args failed because the argument is too long"), #undef C #define C(a, b) TP_ERR_##a