From nobody Sun Feb 8 15:28:33 2026 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 2BB1233C53D for ; Tue, 6 Jan 2026 13:41:05 +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=1767706867; cv=none; b=i89+ZpVAz5oFTkn3bnTZRSHmgylysjngu9vzCFB/YHUdCTn06gwLmiP/+xRjb0L9Vb0ZedBVs/d7pUYz3dj0KZ0iulrn4eOuMqSBpqIQG5lJRFaOnK8IWVYLqczxGbu/SVY+ITKC91fygkeKvCNfQtu30AiRKWZgfOvud4lRLwE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767706867; c=relaxed/simple; bh=ijhR3i1gavKundTTF+SyXMl11p58jCX5UvqYieE4d9g=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oPOZw2v5IJWCCdcp1F4YA7zcdTgEkVbyobz8HgSZmUHeVHE53ks4qDv6QnOkWHE129dRIj+Q0z2IzuMlwL01o5bNxgCkPWQ9TJelcPhvphu19dVhJ4ryKWBaDBGD5VWlDnjSueFtc8g12YqXXyhbq1pjsAesBHa0Hs9IzkXmcdI= 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=Ew3/qBF1; 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="Ew3/qBF1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767706865; 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=WL7clRY6ZrnFyBLMlVvLXH34kZISJ+Yx3LR2pif4tUA=; b=Ew3/qBF1qcB3n+Kzumu6MeOasrppaDptKsqPUwXn0QiA0MzL2XbxHc3ztIDQDWp/FzRtjw mvLRBOfTXPmU5HHbdy+gi6lQ850MVfgTHkI2SbkcyLhKecOcQCKjymEy1qdHjnRVz5ae5H BZYFdxQQs4r/aLBE8umwozXYRZDHnFw= Received: from mx-prod-mc-06.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-647-bOnU7qCXNTu2bRFet3SiNw-1; Tue, 06 Jan 2026 08:41:01 -0500 X-MC-Unique: bOnU7qCXNTu2bRFet3SiNw-1 X-Mimecast-MFC-AGG-ID: bOnU7qCXNTu2bRFet3SiNw_1767706859 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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C2B991800358; Tue, 6 Jan 2026 13:40:59 +0000 (UTC) Received: from wcosta-thinkpadt14gen4.rmtbr.csb (unknown [10.22.89.23]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8E05D1800367; Tue, 6 Jan 2026 13:40:55 +0000 (UTC) From: Wander Lairson Costa To: Steven Rostedt , Tomas Glozar , Wander Lairson Costa , Ivan Pravdin , Crystal Wood , Costa Shulyupin , John Kacur , Tiezhu Yang , linux-trace-kernel@vger.kernel.org (open list:Real-time Linux Analysis (RTLA) tools), linux-kernel@vger.kernel.org (open list:Real-time Linux Analysis (RTLA) tools), bpf@vger.kernel.org (open list:BPF [MISC]:Keyword:(?:\b|_)bpf(?:\b|_)) Subject: [PATCH v2 05/18] rtla: Simplify argument parsing Date: Tue, 6 Jan 2026 08:49:41 -0300 Message-ID: <20260106133655.249887-6-wander@redhat.com> In-Reply-To: <20260106133655.249887-1-wander@redhat.com> References: <20260106133655.249887-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 | 57 +++++++++++++++++++++++--------- tools/tracing/rtla/src/utils.h | 14 ++++++-- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/tools/tracing/rtla/src/actions.c b/tools/tracing/rtla/src/acti= ons.c index e933c2c68b208..b50bf9f9adf28 100644 --- a/tools/tracing/rtla/src/actions.c +++ b/tools/tracing/rtla/src/actions.c @@ -113,6 +113,29 @@ actions_add_continue(struct actions *self) action->type =3D ACTION_CONTINUE; } =20 +static inline const char *__extract_arg(const char *token, const char *opt= , size_t opt_len) +{ + const size_t tok_len =3D strlen(token); + + if (tok_len <=3D opt_len) + return NULL; + + if (strncmp(token, opt, opt_len)) + return NULL; + + return token + opt_len; +} + +/* + * 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) __extract_arg(token, opt "=3D", STRING_LEN= GTH(opt "=3D")) + /* * actions_parse - add an action based on text specification */ @@ -122,6 +145,7 @@ actions_parse(struct actions *self, const char *trigger= , const char *tracefn) 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; @@ -152,12 +176,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) @@ -169,17 +191,21 @@ 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)) - return -1; - } else if (strlen(token) > 4 && strncmp(token, "pid=3D", 4) =3D=3D 0) { - if (strncmp(token + 4, "parent", 7) =3D=3D 0) - pid =3D -1; - else if (strtoi(token + 4, &pid)) + arg_value =3D extract_arg(token, "num"); + if (arg_value) { + if (strtoi(arg_value, &signal)) return -1; } else { - /* Invalid argument */ - return -1; + arg_value =3D extract_arg(token, "pid"); + if (arg_value) { + if (strncmp_static(arg_value, "parent") =3D=3D 0) + pid =3D -1; + else if (strtoi(arg_value, &pid)) + return -1; + } else { + /* Invalid argument */ + return -1; + } } =20 token =3D strtok(NULL, ","); @@ -194,9 +220,10 @@ actions_parse(struct actions *self, const char *trigge= r, const char *tracefn) case ACTION_SHELL: if (token =3D=3D NULL) return -1; - if (strlen(token) > 8 && strncmp(token, "command=3D", 8)) + arg_value =3D extract_arg(token, "command"); + if (!arg_value) return -1; - actions_add_shell(self, token + 8); + actions_add_shell(self, arg_value); break; case ACTION_CONTINUE: /* Takes no argument */ diff --git a/tools/tracing/rtla/src/utils.h b/tools/tracing/rtla/src/utils.h index efbf798650306..7fa3ac5e0bfb6 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.52.0