From nobody Tue Dec 2 02:58:56 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 60D1133E377 for ; Mon, 17 Nov 2025 18:55:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763405728; cv=none; b=SYECg03/COt/KkjYQSQVPA8nGKKIEZF6W4zmS8BJilrM9di+hZAWeDyGGuvQmTgjMnAv+zridaMPJBfrIBb2W3Zyp6+GhzOLz5t+69KsWkKA0M2qUTNoX2EyLDBr2iAp/GJmK/TyDhwhWd3sKiP9D9LxUcxgcvha21P131WpmLQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763405728; c=relaxed/simple; bh=tImqdC5OOuw6pbQKrHUSqD+e0Avl+HgeBq15ztTcD7A=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WWl8oR5pWDawVLZh+KstmX9RHVQDFnZ2tohLFDT0FIfM1L44gRXZt9ZfJ70T8hpyd5rE0QgaKUaysFy/6OGBnmgQsYTMmLe88wuWqX20wFCy9Ta11z0FVX7PHWlzLB6ucJQ42tWGRtq8kL/387c211nCCHCL+LcLbc6MIYSef14= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=V5XW+Cuq; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="V5XW+Cuq" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763405725; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1SPweW+DKunR1tbyeKWNvzzIK414Ai0gzFwnlL2yiZc=; b=V5XW+CuqwCI7Nv5CY5MMBS1aDiCIzeuA7MzyWJbuxxSr1G3Fst0QHYOw9XEyQ4Rv20zwUi /N2rkMrrfCX0pABWcELtxVwR6jO06Tg+UpRgHQaQZUf/gC40GdXdiLhW0u7EnA2pPAO3DP V3a3m9xbpaKckxO5Ha5QV2+B+TRNzD4= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-1-4zoGgmFzMQS1WKUexE-_XQ-1; Mon, 17 Nov 2025 13:55:20 -0500 X-MC-Unique: 4zoGgmFzMQS1WKUexE-_XQ-1 X-Mimecast-MFC-AGG-ID: 4zoGgmFzMQS1WKUexE-_XQ_1763405717 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7D51E18002D1; Mon, 17 Nov 2025 18:55:17 +0000 (UTC) Received: from wcosta-thinkpadt14gen4.rmtbr.csb (unknown [10.22.81.153]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id AFCB3180049F; Mon, 17 Nov 2025 18:55:12 +0000 (UTC) From: Wander Lairson Costa To: Steven Rostedt , Wander Lairson Costa , Tomas Glozar , Ivan Pravdin , Crystal Wood , John Kacur , Costa Shulyupin , Tiezhu Yang , linux-trace-kernel@vger.kernel.org (open list:Real-time Linux Analysis (RTLA) tools), linux-kernel@vger.kernel.org (open list), bpf@vger.kernel.org (open list:BPF [MISC]:Keyword:(?:\b|_)bpf(?:\b|_)) Subject: [rtla 05/13] rtla: Simplify argument parsing Date: Mon, 17 Nov 2025 15:41:12 -0300 Message-ID: <20251117184409.42831-6-wander@redhat.com> In-Reply-To: <20251117184409.42831-1-wander@redhat.com> References: <20251117184409.42831-1-wander@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Type: text/plain; charset="utf-8" The actions_parse() function uses open-coded logic to extract arguments from a string. This includes manual length checks and strncmp() calls, which can be verbose and error-prone. To simplify and improve the robustness of argument parsing, introduce a new extract_arg() helper macro. This macro extracts the value from a "key=3Dvalue" pair, making the code more concise and readable. Also, introduce STRING_LENGTH() and strncmp_static() macros to perform compile-time calculations of string lengths and safer string comparisons. Refactor actions_parse() to use these new helpers, resulting in cleaner and more maintainable code. Signed-off-by: Wander Lairson Costa --- tools/tracing/rtla/src/actions.c | 37 ++++++++++++++++++++++---------- tools/tracing/rtla/src/utils.h | 14 ++++++++++-- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/tools/tracing/rtla/src/actions.c b/tools/tracing/rtla/src/acti= ons.c index e23d4f1c5a592..2a01ece78454c 100644 --- a/tools/tracing/rtla/src/actions.c +++ b/tools/tracing/rtla/src/actions.c @@ -143,15 +143,30 @@ actions_add_continue(struct actions *self) return 0; } =20 +/* + * extract_arg - extract argument value from option token + * @token: option token (e.g., "file=3Dtrace.txt") + * @opt: option name to match (e.g., "file") + * + * Returns pointer to argument value after "=3D" if token matches "opt=3D", + * otherwise returns NULL. + */ +#define extract_arg(token, opt) ( \ + strlen(token) > STRING_LENGTH(opt "=3D") && \ + !strncmp_static(token, opt "=3D") \ + ? (token) + STRING_LENGTH(opt "=3D") : NULL ) + /* * actions_parse - add an action based on text specification */ int actions_parse(struct actions *self, const char *trigger, const char *trace= fn) { + enum action_type type =3D ACTION_NONE; const char *token; char trigger_c[strlen(trigger) + 1]; + const char *arg_value; =20 /* For ACTION_SIGNAL */ int signal =3D 0, pid =3D 0; @@ -182,12 +197,10 @@ actions_parse(struct actions *self, const char *trigg= er, const char *tracefn) if (token =3D=3D NULL) trace_output =3D tracefn; else { - if (strlen(token) > 5 && strncmp(token, "file=3D", 5) =3D=3D 0) { - trace_output =3D token + 5; - } else { + trace_output =3D extract_arg(token, "file"); + if (!trace_output) /* Invalid argument */ return -1; - } =20 token =3D strtok(NULL, ","); if (token !=3D NULL) @@ -198,14 +211,15 @@ actions_parse(struct actions *self, const char *trigg= er, const char *tracefn) case ACTION_SIGNAL: /* Takes two arguments, num (signal) and pid */ while (token !=3D NULL) { - if (strlen(token) > 4 && strncmp(token, "num=3D", 4) =3D=3D 0) { - if(!strtoi(token + 4, &signal)) + arg_value =3D extract_arg(token, "num"); + if (arg_value) { + if (!strtoi(arg_value, &signal)) return -1; - } else if (strlen(token) > 4 && strncmp(token, "pid=3D", 4) =3D=3D 0) { - if (strncmp(token + 4, "parent", 7) =3D=3D 0) + } else if ((arg_value =3D extract_arg(token, "pid"))) { + if (strncmp_static(arg_value, "parent") =3D=3D 0) pid =3D -1; else - if (!strtoi(token + 4, &pid)) + if (!strtoi(arg_value, &pid)) return -1; } else { /* Invalid argument */ @@ -223,8 +237,9 @@ actions_parse(struct actions *self, const char *trigger= , const char *tracefn) case ACTION_SHELL: if (token =3D=3D NULL) return -1; - if (strlen(token) > 8 && strncmp(token, "command=3D", 8) =3D=3D 0) - return actions_add_shell(self, token + 8); + arg_value =3D extract_arg(token, "command"); + if (arg_value) + return actions_add_shell(self, arg_value); return -1; case ACTION_CONTINUE: /* Takes no argument */ diff --git a/tools/tracing/rtla/src/utils.h b/tools/tracing/rtla/src/utils.h index 160491f5de91c..f7ff548f7fba7 100644 --- a/tools/tracing/rtla/src/utils.h +++ b/tools/tracing/rtla/src/utils.h @@ -13,8 +13,18 @@ #define MAX_NICE 20 #define MIN_NICE -19 =20 -#define container_of(ptr, type, member)({ \ - const typeof(((type *)0)->member) *__mptr =3D (ptr); \ +#ifndef ARRAY_SIZE +#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) +#endif + +/* Calculate string length at compile time (excluding null terminator) */ +#define STRING_LENGTH(s) (ARRAY_SIZE(s) - sizeof(*(s))) + +/* Compare string with static string, length determined at compile time */ +#define strncmp_static(s1, s2) strncmp(s1, s2, STRING_LENGTH(s2)) + +#define container_of(ptr, type, member)({ \ + const typeof(((type *)0)->member) *__mptr =3D (ptr); \ (type *)((char *)__mptr - offsetof(type, member)) ; }) =20 extern int config_debug; --=20 2.51.1