From patchwork Tue Jan 10 22:19:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 41679 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2996885wrt; Tue, 10 Jan 2023 14:22:29 -0800 (PST) X-Google-Smtp-Source: AMrXdXtMAJYYF4ovRAdxliBZMkh14PMxIH+h+lqhFHf4exv+hrl8w0QKJqD9hdP0zioC2GszIzVC X-Received: by 2002:a17:907:6e15:b0:7c0:fb3d:11a6 with SMTP id sd21-20020a1709076e1500b007c0fb3d11a6mr76028734ejc.38.1673389348760; Tue, 10 Jan 2023 14:22:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673389348; cv=none; d=google.com; s=arc-20160816; b=xl9bPXYTBx58a3VCWLGds9uwESe/jJwkQw2oiXwZVHDSRm8/OG3JMi0UOYE/pE+BDn 3kCqozCAy8zK+9fH2xbIfZmWvH2K17YZVKvz3q79YegqHWXIsn7Byr7AnTyr6bDD0fcf lv/7oExv6FYEoq40R4mFsvktOb7KsiPjvl2gCNjKH/QdLXhtaZRY1yqgee87OTzkDEy1 mZIxEbNDki6A1hEB1hAq04oqWInWF6eSpsc6h92ueXDadKuwH04s8l9k4ItLGE+nvF68 +DntDO/R6BImfZWHzRMTvOqe18vyoUs4fK2J7FJOnYF36O2nwzxRZDD4ER3gQkb60dZT dsfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=7v/gEXdtTx+UxkZcAUgCVgz4MqYf6krsrfnmQSxx5Nk=; b=cMaCEeCwAjcd74lzk0DoGFynghyvrDZNle/43slyR9S29ALVKFNODU1rzoaSKPHdrp QDLocqXlf2dhB/qN5VU0fA/m7Hd01C7erakWrNYBOOeOnhNHDCHCjIj76Af5OGrSQG7J 1NlAtWON7wVQuo4meY0JbiC9OOt1tyQK5hx+B8nmSVsjFVOQb/FSAqeIOj/xFRr3w5cR iJdYogOXk6/dwzAigHNb4CVMvI8SNpwixnsW0x0CAKMuK1dkdCEOJdssLf1jcwU0KmBH bwPFk4e3As5KyDNrRcQG5RceQLQX0xxa/bN/vbSwKZZF0IlkrcpnJBWbFC/3SCjTnNLP HCWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=PVrvr2Oh; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gb23-20020a170907961700b007bbf9652314si15266685ejc.435.2023.01.10.14.22.05; Tue, 10 Jan 2023 14:22:28 -0800 (PST) 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=@google.com header.s=20210112 header.b=PVrvr2Oh; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233693AbjAJWUg (ORCPT + 99 others); Tue, 10 Jan 2023 17:20:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37684 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234667AbjAJWU0 (ORCPT ); Tue, 10 Jan 2023 17:20:26 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5DC55D8AF for ; Tue, 10 Jan 2023 14:20:25 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id i10-20020a25f20a000000b006ea4f43c0ddso14348034ybe.21 for ; Tue, 10 Jan 2023 14:20:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7v/gEXdtTx+UxkZcAUgCVgz4MqYf6krsrfnmQSxx5Nk=; b=PVrvr2OhQFTUO9zDrH5+2bNwVFqzBYaqjf4hDvEr10wr+zRc5//vzbPS9ggZSCWPfC 36TUo5BG7B8TkedHGtGVN2hi3FKU0/0IPbtRMt6oLWxVbgyfiBi/JwhyOQ9mrRzQ+rmn U0DRvEz36y6B+ETuwktmXn/HAr2e4DC7uDZ17DROPT89WL6WZa3Q5woCWaCwOIKp+ep7 rA8MJ/3ZD7oa6ukMV9OmnaQ0IzvWquQc/+RCW1cJJN5pz4NzTHh14Tjt4c7U82HtGv1k QabcgkXXuEptpfHctVAR8MIzbxgVBLAYpU14CFESsjoWuv0T+Eo4zNIMmKB/cGnUXsWh utwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7v/gEXdtTx+UxkZcAUgCVgz4MqYf6krsrfnmQSxx5Nk=; b=SQtVOS9HsPDnsPKtluNAayyIdohomRGQlcGCoq2DImToRVhgQYAHmtG7ZFXQ7vncAL KdckK76YCv9lOSV8H0leIpU58VJmcCD8HFZHb2bLwX75HtlNPtHwWLFwfDmH+/OHj1hU 3uB/WALUVL+Yv9+szDaVA8J9qFrbLAcUOpSoi9RnB8jbzFVn4Ll4ScPUR3rlqIOkk2QN 3bIX8QIX83yN6+Uv0ZvPJsN4DjjU65biIxrQY2AyZxFdq9OVQ/TvNEtBGf0ogBllh+Mi L/Y2jgHmHvwXgeR04FNPRogSMH+ICDzXT+e9BSwhFxib2evytPlM7QaMpF5r8THr9N2d ju+g== X-Gm-Message-State: AFqh2kqO8Px+LQy7vFQjiVE1Q+A0XfvhgPAbfj28Bfe9RXtX8bjoAQbU ucQFHpVya2e4Rf9rIC/LLWmFMUCmdX/D X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:cebf:c37e:8184:56]) (user=irogers job=sendgmr) by 2002:a0d:d583:0:b0:3ec:a0cb:550 with SMTP id x125-20020a0dd583000000b003eca0cb0550mr1240169ywd.3.1673389225104; Tue, 10 Jan 2023 14:20:25 -0800 (PST) Date: Tue, 10 Jan 2023 14:19:57 -0800 In-Reply-To: <20230110222003.1591436-1-irogers@google.com> Message-Id: <20230110222003.1591436-2-irogers@google.com> Mime-Version: 1.0 References: <20230110222003.1591436-1-irogers@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Subject: [PATCH v1 1/7] perf llvm: Fix inadvertent file creation From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Nathan Chancellor , Nick Desaulniers , Tom Rix , Nicolas Schier , Masahiro Yamada , Athira Rajeev , Christy Lee , Andrii Nakryiko , Ravi Bangoria , Leo Yan , Yang Jihong , Qi Liu , James Clark , Adrian Hunter , "Masami Hiramatsu (Google)" , Kan Liang , Sean Christopherson , Zhengjun Xing , Rob Herring , Xin Gao , Zechuan Chen , Jason Wang , Christophe JAILLET , Stephane Eranian , German Gomez , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754675909841168223?= X-GMAIL-MSGID: =?utf-8?q?1754675909841168223?= The LLVM template is first echo-ed into command_out and then command_out executed. The echo surrounds the template with double quotes, however, the template itself may contain quotes. This is generally innocuous but in tools/perf/tests/bpf-script-test-prologue.c we see: ... SEC("func=null_lseek file->f_mode offset orig") ... where the first double quote ends the double quote of the echo, then the > redirects output into a file called f_mode. To avoid this inadvertent behavior substitute redirects and similar characters to be ASCII control codes, then substitute the output in the echo back again. Fixes: 5eab5a7ee032 ("perf llvm: Display eBPF compiling command in debug output") Signed-off-by: Ian Rogers --- tools/perf/util/llvm-utils.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c index 650ffe336f3a..4e8e243a6e4b 100644 --- a/tools/perf/util/llvm-utils.c +++ b/tools/perf/util/llvm-utils.c @@ -531,14 +531,37 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf, pr_debug("llvm compiling command template: %s\n", template); + /* + * Below, substitute control characters for values that can cause the + * echo to misbehave, then substitute the values back. + */ err = -ENOMEM; - if (asprintf(&command_echo, "echo -n \"%s\"", template) < 0) + if (asprintf(&command_echo, "echo -n \a%s\a", template) < 0) goto errout; +#define SWAP_CHAR(a, b) do { if (*p == a) *p = b; } while (0) + for (char *p = command_echo; *p; p++) { + SWAP_CHAR('<', '\001'); + SWAP_CHAR('>', '\002'); + SWAP_CHAR('"', '\003'); + SWAP_CHAR('\'', '\004'); + SWAP_CHAR('|', '\005'); + SWAP_CHAR('&', '\006'); + SWAP_CHAR('\a', '"'); + } err = read_from_pipe(command_echo, (void **) &command_out, NULL); if (err) goto errout; + for (char *p = command_out; *p; p++) { + SWAP_CHAR('\001', '<'); + SWAP_CHAR('\002', '>'); + SWAP_CHAR('\003', '"'); + SWAP_CHAR('\004', '\''); + SWAP_CHAR('\005', '|'); + SWAP_CHAR('\006', '&'); + } +#undef SWAP_CHAR pr_debug("llvm compiling command : %s\n", command_out); err = read_from_pipe(template, &obj_buf, &obj_buf_sz); From patchwork Tue Jan 10 22:19:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 41677 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2996875wrt; Tue, 10 Jan 2023 14:22:26 -0800 (PST) X-Google-Smtp-Source: AMrXdXuYkcvfHyyb1FDmfu9buhnD5ZW0Avf40r2ygBPTX1nXUkvalvc/oGC6FmFITjVN76QTgpFV X-Received: by 2002:a17:907:7676:b0:7c1:7183:2d32 with SMTP id kk22-20020a170907767600b007c171832d32mr62303155ejc.56.1673389346749; Tue, 10 Jan 2023 14:22:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673389346; cv=none; d=google.com; s=arc-20160816; b=xw65H+A7INMRvS53IKzfH0zO8EGaes+p34YHiNoQLfG9MFLSaAgNCqOFUsSIX+HHMX 4uKOlp3icBiK3Zh1SouPC8JifqdxE3FbsWzafDH8GPLf8O+dLseQVcCKk4NjuskQcz9+ xFeBX5Sr2S5478UP+jXBU8UfuMdady4/YLlRglgqryq99cooJ0WFSBCtvpZmL1LWn0Hd WEH0gGuKVo6wCTwbNErvrIw1hZvQkf0SYInCySlQdr8Ok68x+cVyKWPKJEBCzYoRqr2/ W37sgiUsNb7S5J+9D/TUTiIZiuM+RBYQbRmTbfPYHGYMkAmdp7bmv6uLnNQl3h8hgMp/ EwFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=wXlKtQGeWTp7vou3jM8pEXClHMn0X8ibzVPnBb6ixuM=; b=HkUI8esWR+zjVtE3cXKLcGofUC8ienkKbNH6CnY0xKxDZQKT/nKiyyDsWAxsIvj5WF WgSfJSBmLe+GgvEGWn8jbKLLEt3VYFXQc43fXu/jp1rBvWMjtyVqgxxlDvbFza3yw5wL faF3oau+ilUajruh1STmtgJMf6awexxaefrM5Nves3hDjj9rlmw2dUOmDjPmXW19BN6B zoVOq759vypEG8t9Hv2wZuvJRbpwwiC2qFaNhMdLhT6CD2QrU3snmCs4kNXG0KG7653v psnN7Ri1uMiFNEnpQzg+EXT1WVj10292YsElukOr8NJq1S8uR6viMnVHhMMpDav0fKNF 3Z6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=hjTIUiM3; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v17-20020a056402185100b00469fd4323a4si11712462edy.246.2023.01.10.14.22.03; Tue, 10 Jan 2023 14:22:26 -0800 (PST) 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=@google.com header.s=20210112 header.b=hjTIUiM3; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234449AbjAJWUw (ORCPT + 99 others); Tue, 10 Jan 2023 17:20:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234841AbjAJWUi (ORCPT ); Tue, 10 Jan 2023 17:20:38 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DAECA633B7 for ; Tue, 10 Jan 2023 14:20:36 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-4ce566db73eso73147557b3.11 for ; Tue, 10 Jan 2023 14:20:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=wXlKtQGeWTp7vou3jM8pEXClHMn0X8ibzVPnBb6ixuM=; b=hjTIUiM3nzSYOud7leDVomzWDX3omcRH7XxTp1cWtIJhcikMVufN+ADzaTNdzeYkj3 HC8m5Q1q0N4tWXzZdz62Ir1LPgLAO+IhU/NZoaVR4GFeUM6+9gdxTBxUG6xXuEsppqxe 4vnw6QbBUijTFsZg9n4sHratLsuRyEm4BjeV3UDRMt9++3Yh3Cq54hMnLSk3/4AROLWi fsELZGJJQe97BgTCi3vctbH4g6noIJFn734K3HKJyuYmN0ckIZ3X1C+cnuvxMG2MVHUU +jYZGmTxMrdoDqn9BRXhS5pXUVJNJf1poMGdTSp9IsSQGq4MmAoB+vrv5a5KcpnoI+PE Ogzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wXlKtQGeWTp7vou3jM8pEXClHMn0X8ibzVPnBb6ixuM=; b=EYwnKxCaHbB21INzWpY8bwnjXUB/bGw2+gt8GFZwbAyFasxPjK7wA3GtNdxeo5OH5N MNIoqvhsFn+e8KXh8fjd2cE+AEnSAtDGFeMFPh7/TYCH/du4wiei6ete+XiXvIIyeYKk c1mE/XcPTlaSXrWabSaomlcwNlnI6S4ALUG145deNPRNL/yJx5kmhrjSx+rER/YzOcNA jxyPVHQ70dI6TjXGHcnM07k3URvbggEA2rSKLDYbgaVPtoZb6a1VK3d7a+9dpAsA/D83 rEiMSpFs+elFHj4eZJ0k3RHfSR26w01Or9wrFWulOACkC8elaQwjSmWdelWLqwD3fEn8 cTLg== X-Gm-Message-State: AFqh2kozPJygsXV2/saziJpg4HX4MUC/1Kho87J9lrnVEThv2bI9atdk tPI6Xy+FXbkMPlERuElrUkiVYU+LOpsH X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:cebf:c37e:8184:56]) (user=irogers job=sendgmr) by 2002:a0d:e60b:0:b0:3ec:2e89:409c with SMTP id p11-20020a0de60b000000b003ec2e89409cmr1362309ywe.20.1673389236102; Tue, 10 Jan 2023 14:20:36 -0800 (PST) Date: Tue, 10 Jan 2023 14:19:58 -0800 In-Reply-To: <20230110222003.1591436-1-irogers@google.com> Message-Id: <20230110222003.1591436-3-irogers@google.com> Mime-Version: 1.0 References: <20230110222003.1591436-1-irogers@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Subject: [PATCH v1 2/7] tools lib: Move strbuf to libapi From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Nathan Chancellor , Nick Desaulniers , Tom Rix , Nicolas Schier , Masahiro Yamada , Athira Rajeev , Christy Lee , Andrii Nakryiko , Ravi Bangoria , Leo Yan , Yang Jihong , Qi Liu , James Clark , Adrian Hunter , "Masami Hiramatsu (Google)" , Kan Liang , Sean Christopherson , Zhengjun Xing , Rob Herring , Xin Gao , Zechuan Chen , Jason Wang , Christophe JAILLET , Stephane Eranian , German Gomez , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754675907595853403?= X-GMAIL-MSGID: =?utf-8?q?1754675907595853403?= Move strbuf, appendable C strings, to libapi so that other libraries may use it. Signed-off-by: Ian Rogers --- tools/lib/api/Build | 1 + tools/lib/api/Makefile | 2 +- tools/{perf/util => lib/api}/strbuf.c | 5 +++-- tools/{perf/util => lib/api}/strbuf.h | 0 tools/perf/bench/evlist-open-close.c | 2 +- tools/perf/builtin-help.c | 2 +- tools/perf/builtin-list.c | 2 +- tools/perf/util/Build | 1 - tools/perf/util/cache.h | 2 +- tools/perf/util/dwarf-aux.c | 2 +- tools/perf/util/env.c | 2 +- tools/perf/util/header.c | 2 +- tools/perf/util/metricgroup.c | 2 +- tools/perf/util/pfm.c | 2 +- tools/perf/util/pmu.c | 2 +- tools/perf/util/probe-event.c | 2 +- tools/perf/util/probe-file.c | 2 +- tools/perf/util/probe-finder.c | 2 +- tools/perf/util/sort.c | 2 +- 19 files changed, 19 insertions(+), 18 deletions(-) rename tools/{perf/util => lib/api}/strbuf.c (97%) rename tools/{perf/util => lib/api}/strbuf.h (100%) diff --git a/tools/lib/api/Build b/tools/lib/api/Build index 6e2373db5598..2eab5abbad50 100644 --- a/tools/lib/api/Build +++ b/tools/lib/api/Build @@ -3,6 +3,7 @@ libapi-y += fs/ libapi-y += cpu.o libapi-y += debug.o libapi-y += str_error_r.o +libapi-y += strbuf.o $(OUTPUT)str_error_r.o: ../str_error_r.c FORCE $(call rule_mkdir) diff --git a/tools/lib/api/Makefile b/tools/lib/api/Makefile index 044860ac1ed1..dc2d810dfbad 100644 --- a/tools/lib/api/Makefile +++ b/tools/lib/api/Makefile @@ -99,7 +99,7 @@ install_lib: $(LIBFILE) $(call do_install_mkdir,$(libdir_SQ)); \ cp -fpR $(LIBFILE) $(DESTDIR)$(libdir_SQ) -HDRS := cpu.h debug.h io.h +HDRS := cpu.h debug.h io.h strbuf.h FD_HDRS := fd/array.h FS_HDRS := fs/fs.h fs/tracing_path.h INSTALL_HDRS_PFX := $(DESTDIR)$(prefix)/include/api diff --git a/tools/perf/util/strbuf.c b/tools/lib/api/strbuf.c similarity index 97% rename from tools/perf/util/strbuf.c rename to tools/lib/api/strbuf.c index a64a37628f12..4639b2d02e62 100644 --- a/tools/perf/util/strbuf.c +++ b/tools/lib/api/strbuf.c @@ -1,6 +1,5 @@ // SPDX-License-Identifier: GPL-2.0 -#include "cache.h" -#include "debug.h" +#include "debug-internal.h" #include "strbuf.h" #include #include @@ -43,6 +42,7 @@ char *strbuf_detach(struct strbuf *sb, size_t *sz) return res; } +#define alloc_nr(x) (((x)+16)*3/2) int strbuf_grow(struct strbuf *sb, size_t extra) { char *buf; @@ -69,6 +69,7 @@ int strbuf_grow(struct strbuf *sb, size_t extra) sb->alloc = nr; return 0; } +#undef alloc_nr int strbuf_addch(struct strbuf *sb, int c) { diff --git a/tools/perf/util/strbuf.h b/tools/lib/api/strbuf.h similarity index 100% rename from tools/perf/util/strbuf.h rename to tools/lib/api/strbuf.h diff --git a/tools/perf/bench/evlist-open-close.c b/tools/perf/bench/evlist-open-close.c index 5a27691469ed..d8a8fcadb9ca 100644 --- a/tools/perf/bench/evlist-open-close.c +++ b/tools/perf/bench/evlist-open-close.c @@ -8,7 +8,7 @@ #include "../util/stat.h" #include "../util/evlist.h" #include "../util/evsel.h" -#include "../util/strbuf.h" +#include #include "../util/record.h" #include "../util/parse-events.h" #include "internal/threadmap.h" diff --git a/tools/perf/builtin-help.c b/tools/perf/builtin-help.c index 3976aebe3677..8874e1e0335b 100644 --- a/tools/perf/builtin-help.c +++ b/tools/perf/builtin-help.c @@ -6,7 +6,7 @@ */ #include "util/cache.h" #include "util/config.h" -#include "util/strbuf.h" +#include #include "builtin.h" #include #include "common-cmds.h" diff --git a/tools/perf/builtin-list.c b/tools/perf/builtin-list.c index 137d73edb541..ca52227f311c 100644 --- a/tools/perf/builtin-list.c +++ b/tools/perf/builtin-list.c @@ -17,7 +17,7 @@ #include "util/metricgroup.h" #include "util/string2.h" #include "util/strlist.h" -#include "util/strbuf.h" +#include #include #include #include diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 79b9498886a2..5c68ab8c69f8 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -37,7 +37,6 @@ perf-y += libstring.o perf-y += bitmap.o perf-y += hweight.o perf-y += smt.o -perf-y += strbuf.o perf-y += string.o perf-y += strlist.o perf-y += strfilter.o diff --git a/tools/perf/util/cache.h b/tools/perf/util/cache.h index 9f2e36ef5072..19e60decb24c 100644 --- a/tools/perf/util/cache.h +++ b/tools/perf/util/cache.h @@ -2,7 +2,7 @@ #ifndef __PERF_CACHE_H #define __PERF_CACHE_H -#include "strbuf.h" +#include #include #include "../ui/ui.h" diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index b07414409771..673ddfeb938d 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -9,7 +9,7 @@ #include #include "debug.h" #include "dwarf-aux.h" -#include "strbuf.h" +#include #include "string2.h" /** diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c index 5b8cf6a421a4..3dc1c51a8335 100644 --- a/tools/perf/util/env.c +++ b/tools/perf/util/env.c @@ -10,7 +10,7 @@ #include #include #include -#include "strbuf.h" +#include struct perf_env perf_env; diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 404d816ca124..35067c22a47f 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -38,7 +38,7 @@ #include "cpumap.h" #include "pmu.h" #include "vdso.h" -#include "strbuf.h" +#include #include "build-id.h" #include "data.h" #include diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index b9c273ed080a..d1d21605715a 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -9,7 +9,7 @@ #include "debug.h" #include "evlist.h" #include "evsel.h" -#include "strbuf.h" +#include #include "pmu.h" #include "pmu-hybrid.h" #include "print-events.h" diff --git a/tools/perf/util/pfm.c b/tools/perf/util/pfm.c index ac3227ba769c..c82e7bc7c5ea 100644 --- a/tools/perf/util/pfm.c +++ b/tools/perf/util/pfm.c @@ -12,7 +12,7 @@ #include "util/parse-events.h" #include "util/pmu.h" #include "util/pfm.h" -#include "util/strbuf.h" +#include #include #include diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 2bdeb89352e7..4648ccf0b50a 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -27,7 +27,7 @@ #include "print-events.h" #include "header.h" #include "string2.h" -#include "strbuf.h" +#include #include "fncache.h" #include "pmu-hybrid.h" diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index 0c24bc7afbca..e609970e2113 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c @@ -38,7 +38,7 @@ #include "probe-file.h" #include "session.h" #include "string2.h" -#include "strbuf.h" +#include #include #include diff --git a/tools/perf/util/probe-file.c b/tools/perf/util/probe-file.c index 3d50de3217d5..c1f1ef3f48d4 100644 --- a/tools/perf/util/probe-file.c +++ b/tools/perf/util/probe-file.c @@ -20,7 +20,7 @@ #include "dso.h" #include "color.h" #include "symbol.h" -#include "strbuf.h" +#include #include #include #include "probe-event.h" diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c index 54b49ce85c9f..4368a9dffc35 100644 --- a/tools/perf/util/probe-finder.c +++ b/tools/perf/util/probe-finder.c @@ -24,7 +24,7 @@ #include "dso.h" #include "debug.h" #include "intlist.h" -#include "strbuf.h" +#include #include "strlist.h" #include "symbol.h" #include "probe-finder.h" diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c index e188f74698dd..32f00a340c0d 100644 --- a/tools/perf/util/sort.c +++ b/tools/perf/util/sort.c @@ -21,7 +21,7 @@ #include "evlist.h" #include "srcline.h" #include "strlist.h" -#include "strbuf.h" +#include #include "mem-events.h" #include "annotate.h" #include "event.h" From patchwork Tue Jan 10 22:19:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 41678 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2996876wrt; Tue, 10 Jan 2023 14:22:27 -0800 (PST) X-Google-Smtp-Source: AMrXdXsW7DvMDnXAszkCyvywScdKe681wSPIkMNA3iJYNlp/vDurcF4ERsRA3X37nC0S0vIYWkEw X-Received: by 2002:a17:90a:aa92:b0:226:b425:3540 with SMTP id l18-20020a17090aaa9200b00226b4253540mr23766251pjq.36.1673389346977; Tue, 10 Jan 2023 14:22:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673389346; cv=none; d=google.com; s=arc-20160816; b=IjZf5Py2weThRdVVueXO4/4i4Em4eDHrce+b3fYe0R7kPMUbMfDlQSCZHXKpyHQ6+n Yd4T+l5BmW2NDQxCWbbHLyUjSvXfI1psK4svG/wT6q1z2g15uzQ+p2+6H/8lcm/ynMl+ dyMAQHCKIiVYvtBpHBD3hBuyb/0XW7/bA5B6fer/NiOAnovtt3QGQwLBJ22L7eVnXhAW jD/Tk8n+ci4cnjYMMxoeheprUrOawbvF/dSiA9RJj8QmsbX9f/oja2BJ7nyUnQnwArak B31EGs7Ys6ITKYVn6dV+qg4bJ55RDyHRQ23JOdGoCIhtcq6N9VkBnVNXPGCUQHwLbPD0 O5hQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=e+OL21tRhOI5FC8OP8rOHe4TMF297jaFfbFeRu1wlqg=; b=ZOVtp+ayXZmpzKR/0WzvCQoFenBHzFXEzMv9DfcQMFArffzM51rJir1DugrVq06Xpg BVMFqs1cYnQyLjt3NtN14dQXCf9QQZ+gqI7x7r0T8HdQN21sidqAsFWuZ+gGVFRnH2CA nWD1BUhVUdGdx9SQp48CRzELQK1+zfaqATNVOzuwjaWIEZL4vuh6/3OzHIO5k+QWTwTv /5uqHFD+Sq23Ti3rQFsQXLiAbmu6hSz+G5z4ijTU2Aoixrni7GdMxj4rYFAMvzutJEgF 8OFJM+U9AUToZlxEeT1EvUIo9XmqaA2Khm7rJVgxZ1xwa2F+rz7qPnKR9AEZ5PNcEMdg yrog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=pAopMpFh; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lr8-20020a17090b4b8800b00218ac70fdefsi14229104pjb.183.2023.01.10.14.22.14; Tue, 10 Jan 2023 14:22:26 -0800 (PST) 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=@google.com header.s=20210112 header.b=pAopMpFh; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234940AbjAJWU7 (ORCPT + 99 others); Tue, 10 Jan 2023 17:20:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234788AbjAJWUq (ORCPT ); Tue, 10 Jan 2023 17:20:46 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E87D63F48 for ; Tue, 10 Jan 2023 14:20:45 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-4c8e781bc0aso91860487b3.22 for ; Tue, 10 Jan 2023 14:20:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=e+OL21tRhOI5FC8OP8rOHe4TMF297jaFfbFeRu1wlqg=; b=pAopMpFhTfX0jzenqkNBx67vQjfjd9TY0dEwmq4dnUUW/5FoxhH/tVN7EA6o41wxrx rsUBnMfLZxm5PQ3Gm0Z5qpVo9n/Dnw2t+GjNw3fXdMd4EbvEybnsF61808WRKPLXGEPX BP/2/BT6IOMQEJFjn/Laru90RARJ4roIZIz7Einhd/Z0P1NaLFrKvEfC1eWc4LMqSZvs b0T7fdriG8IC3vY8yB0wM/np9pfhaT10oOvYNSasEBMAIwRVJOVL3zjz2LA6Lhv6zmQq XBNkFoUi1yjwG7YzAhVBeRgAhvFBI7sfazm/WV9x3buWT4x5HgobueNTp6jLXWkx3t0D 8dBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=e+OL21tRhOI5FC8OP8rOHe4TMF297jaFfbFeRu1wlqg=; b=q7FuZWm0llHgxwGtaxK3J9j2wrYY4Ycsnn8508DEATVf9xzzStNyIMiPUXlAwFHKPQ SvXiMOm4VgksSGEm0UeNUyStP1l4cogJ54/vApZyC+nFNtVWMryWeEIW5surjjTs9bOm JG5TOhVg9MMdEX7ySnOQ4hLeOKWeZfmJZdEmc2i7NEeZ1TWhQqj/4g44EHgYtM/9CEii /5OMeqlRxPV7HFoDgFZ+cF4LCr/MwbjxlqShOXC8zf0V3/f9J1PhslCah0hq8oY3wKen oyspVRg0+qesbYWhAtLrucJHDVnIIXwsUOBC3wkCX2bB/XlLugsg3T9GPrdx7GxwCJ0H Nn0Q== X-Gm-Message-State: AFqh2kpaOaJc9B4Q2KEvMq7xp7B19sVYUDZztvk97dRZPQ4GlkvtZUF+ Px+GH+JiIehVSGCKN0oJ+TiwqoqFtWOH X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:cebf:c37e:8184:56]) (user=irogers job=sendgmr) by 2002:a25:804:0:b0:767:3057:9533 with SMTP id 4-20020a250804000000b0076730579533mr8371214ybi.454.1673389244250; Tue, 10 Jan 2023 14:20:44 -0800 (PST) Date: Tue, 10 Jan 2023 14:19:59 -0800 In-Reply-To: <20230110222003.1591436-1-irogers@google.com> Message-Id: <20230110222003.1591436-4-irogers@google.com> Mime-Version: 1.0 References: <20230110222003.1591436-1-irogers@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Subject: [PATCH v1 3/7] tools lib subcmd: Add run_command_strbuf From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Nathan Chancellor , Nick Desaulniers , Tom Rix , Nicolas Schier , Masahiro Yamada , Athira Rajeev , Christy Lee , Andrii Nakryiko , Ravi Bangoria , Leo Yan , Yang Jihong , Qi Liu , James Clark , Adrian Hunter , "Masami Hiramatsu (Google)" , Kan Liang , Sean Christopherson , Zhengjun Xing , Rob Herring , Xin Gao , Zechuan Chen , Jason Wang , Christophe JAILLET , Stephane Eranian , German Gomez , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754675908138636724?= X-GMAIL-MSGID: =?utf-8?q?1754675908138636724?= Often a command wants to be run in a shell with stdout placed in a string. This API mimics that of stdio's popen, running the given command (not argv array) with "/bin/sh -c" then appending the output from stdout to the buf argument. Signed-off-by: Ian Rogers --- tools/lib/subcmd/Makefile | 32 +++++++++++++++++++++++++++++--- tools/lib/subcmd/run-command.c | 30 ++++++++++++++++++++++++++++++ tools/lib/subcmd/run-command.h | 14 ++++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/tools/lib/subcmd/Makefile b/tools/lib/subcmd/Makefile index b87213263a5e..23174d013519 100644 --- a/tools/lib/subcmd/Makefile +++ b/tools/lib/subcmd/Makefile @@ -13,6 +13,7 @@ CC ?= $(CROSS_COMPILE)gcc LD ?= $(CROSS_COMPILE)ld AR ?= $(CROSS_COMPILE)ar +MKDIR = mkdir RM = rm -f MAKEFLAGS += --no-print-directory @@ -55,6 +56,17 @@ CFLAGS += -I$(srctree)/tools/include/ CFLAGS += $(EXTRA_WARNINGS) $(EXTRA_CFLAGS) +LIBAPI_DIR = $(srctree)/tools/lib/api/ +ifneq ($(OUTPUT),) + LIBAPI_OUTPUT = $(abspath $(OUTPUT))/libapi +else + LIBAPI_OUTPUT = $(CURDIR)/libapi +endif +LIBAPI_DESTDIR = $(LIBAPI_OUTPUT) +LIBAPI_INCLUDE = $(LIBAPI_DESTDIR)/include +LIBAPI = $(LIBAPI_OUTPUT)/libapi.a +CFLAGS += -I$(LIBAPI_OUTPUT)/include + SUBCMD_IN := $(OUTPUT)libsubcmd-in.o ifeq ($(LP64), 1) @@ -76,7 +88,9 @@ include $(srctree)/tools/build/Makefile.include all: fixdep $(LIBFILE) -$(SUBCMD_IN): FORCE +prepare: $(LIBAPI) + +$(SUBCMD_IN): FORCE prepare @$(MAKE) $(build)=libsubcmd $(LIBFILE): $(SUBCMD_IN) @@ -113,10 +127,22 @@ install_headers: $(INSTALL_HDRS) install: install_lib install_headers -clean: +$(LIBAPI_OUTPUT): + $(Q)$(MKDIR) -p $@ + +$(LIBAPI): FORCE | $(LIBAPI_OUTPUT) + $(Q)$(MAKE) -C $(LIBAPI_DIR) O=$(LIBAPI_OUTPUT) \ + DESTDIR=$(LIBAPI_DESTDIR) prefix= \ + $@ install_headers + +$(LIBAPI)-clean: + $(call QUIET_CLEAN, libapi) + $(Q)$(RM) -r -- $(LIBAPI_OUTPUT) + +clean: $(LIBAPI)-clean $(call QUIET_CLEAN, libsubcmd) $(RM) $(LIBFILE); \ find $(or $(OUTPUT),.) -name \*.o -or -name \*.o.cmd -or -name \*.o.d | xargs $(RM) FORCE: -.PHONY: clean FORCE +.PHONY: clean FORCE prepare diff --git a/tools/lib/subcmd/run-command.c b/tools/lib/subcmd/run-command.c index 5cdac2162532..e90b285b6720 100644 --- a/tools/lib/subcmd/run-command.c +++ b/tools/lib/subcmd/run-command.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "subcmd-util.h" #include "run-command.h" #include "exec-cmd.h" @@ -227,3 +228,32 @@ int run_command_v_opt(const char **argv, int opt) prepare_run_command_v_opt(&cmd, argv, opt); return run_command(&cmd); } + +int run_command_strbuf(const char *cmd, struct strbuf *buf) +{ + const char *argv[4] = { + "/bin/sh", + "-c", + cmd, + NULL + }; + struct child_process child = { + .argv = argv, + .out = -1, + }; + int err; + ssize_t read_sz; + + err = start_command(&child); + if (err) + return err; + + read_sz = strbuf_read(buf, child.out, 0); + + err = finish_command(&child); + close(child.out); + if (read_sz < 0) + return (int)read_sz; + + return err; +} diff --git a/tools/lib/subcmd/run-command.h b/tools/lib/subcmd/run-command.h index 17d969c6add3..1f7a2af9248c 100644 --- a/tools/lib/subcmd/run-command.h +++ b/tools/lib/subcmd/run-command.h @@ -58,4 +58,18 @@ int run_command(struct child_process *); #define RUN_COMMAND_STDOUT_TO_STDERR 4 int run_command_v_opt(const char **argv, int opt); +struct strbuf; +/** + * run_command_strbuf() - Run cmd using /bin/sh and place stdout in strbuf. + * @cmd: The command to run by "/bin/sh -c". + * @buf: The strbuf appended to by reading stdout. + * + * Similar to popen with fread, run the given command reading the stdout output + * to buf. As with popen, stderr output goes to the current processes stderr but + * may be redirected in cmd by using "2>&1". + * + * Return: 0 on success or a negative error code on failure. + */ +int run_command_strbuf(const char *cmd, struct strbuf *buf); + #endif /* __SUBCMD_RUN_COMMAND_H */ From patchwork Tue Jan 10 22:20:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 41680 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2996978wrt; Tue, 10 Jan 2023 14:22:44 -0800 (PST) X-Google-Smtp-Source: AMrXdXvmQ+zciKO+3Q0FB7J1guLQ7KliBTl4RCI2WYONiuQh9fiEH1NpuSm9ChwrhTjuVobvSj7v X-Received: by 2002:a17:902:f791:b0:192:5ec4:6656 with SMTP id q17-20020a170902f79100b001925ec46656mr72310585pln.3.1673389364607; Tue, 10 Jan 2023 14:22:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673389364; cv=none; d=google.com; s=arc-20160816; b=hqKHXyC4O6IL64Dd1OqCqy6EQgn1TQNEv5SaNeqmapsvCHIlNraZl03wraYhM1wkZ+ XMO2CqoGiN+DjxP5ogT+lFXBEBRPpAhj8n/z8EYzi0EPE/DlDuijSNClR7K/LSvA4Wx4 M71wRrBypFYAYkK+Jay9rDvBb2JmrwYyDr9l5SxHE+TrfW/hTtm6qk0ztYEhNmLlasXU ipONp8J46SFXaLg8gL8TS71nonEMBvEFCXKKQ7w5l3WKWoykakYLPHZkfo/KRXjUu1+V SFPoo9Z8RDz3InnYIqJQ4W51g4l2M79WxXgJDE+ZfMcZcSQt3py+OVn+rrmKCrtFv8no B0yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=b4cDFSVYG43croOnEV27nNQKcdSJ0iRzIK+gxT25DWQ=; b=HOqQa200bOtLpJOT5bYQETCv9GVu29JxFtw0q69zZ6atA5D6VJ0GBLm1+XBMDwQ7/g HIDKR1bnHB/7q9OLzNjtse/kUZK0Le9CkO7TsIG1jzEHkgWA7SXZvOBArwzwJpso9mKO ZgZFIQ+pAKV12hhik9VjPpbL8fMeY4GXEgWbl4ywXpZ+TC9vqVXhRpm5VcqH/ZeIoXds wGr+jrS/axCj0+DcB+tKnYe7DxB3lB1vDQFw0IMGAOsiy0+9oPcLegCkOu+0eFazeyef fjHZO9M5RbRrr026Op4tW1zlaMqWKbICqM1QV06BMH8jld2/o/aoH1hBw70xmQIlDkJO jH3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=oWwqPFHv; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q6-20020a17090311c600b0018938988ea9si13137011plh.520.2023.01.10.14.22.31; Tue, 10 Jan 2023 14:22:44 -0800 (PST) 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=@google.com header.s=20210112 header.b=oWwqPFHv; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235189AbjAJWVy (ORCPT + 99 others); Tue, 10 Jan 2023 17:21:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235259AbjAJWVV (ORCPT ); Tue, 10 Jan 2023 17:21:21 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06E12101D6 for ; Tue, 10 Jan 2023 14:20:57 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 11-20020a25050b000000b007b7968a6423so14250800ybf.13 for ; Tue, 10 Jan 2023 14:20:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=b4cDFSVYG43croOnEV27nNQKcdSJ0iRzIK+gxT25DWQ=; b=oWwqPFHv+dZCSeNo+f2delVO4bkS5CysJ1Fb9cTlI9zPxRA7uKaiJ7fzrQAE/gTePN yQdJfBq26Z53aSri1L4flICMFvFNUevMxBBdhNvHv8Ijb0Oku7b8R3i1GIZwAS7jcJ22 jMmWxbFtkBzDL/R9uaLOc+rwZEKLhEkSnycWiK+bJoz5ZUvP8k/r3gzwpyc58iJZ/qIR uFv/RK4kFlujjtJHI9F8csecrHrl/8oIz/UTVfp75WDyghQbB2noKVROCgEIL9/hytzp 05/J55rtJDAGcO6Q4BwMKuBUc0CZ06wbMj2tK5aFbb8NTedwer0JBGvwhthbuu/kH2CO qRSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=b4cDFSVYG43croOnEV27nNQKcdSJ0iRzIK+gxT25DWQ=; b=v8yJgBnlAbfPahz2UL5L00cZhIHn3/MtSzqdTyS01dfG8Zu+OE754HGaNNkTibOCig edyzwsMNCT2573IEBuqcKz3n4VyjIv+Hk7axlR93MZQoi5HgS6ismI8usaLp4zG+ASO1 yYgGyDAIdSHWaZh3Ox3AqFNLtYgB5u0vKcCoKIZNZDaTRusUwZp089tE5+ybTWrOBRtJ PoWDOyggX+RaJDhUtIdaxBMRMzCnFZi6NNLox9N1gGkoGZjrV2FHEVCFVdNJ9Nd8FS38 eZYcYE221Zv59JRXxXvIF65fjAQ4TjVxacyT7pP/rL4ZjgkkPaoJ/6iwpWILgssmpmMt /VzA== X-Gm-Message-State: AFqh2kq6s8RJAMG3E/2HYpNRafG0Jv9WHsUKpS3M4KPYzy/cJl7FcGIP cayPSUAIIgAtPLj1AO4loFLG0V1JbMPN X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:cebf:c37e:8184:56]) (user=irogers job=sendgmr) by 2002:a25:9a02:0:b0:7ba:f028:b325 with SMTP id x2-20020a259a02000000b007baf028b325mr1547877ybn.452.1673389256480; Tue, 10 Jan 2023 14:20:56 -0800 (PST) Date: Tue, 10 Jan 2023 14:20:00 -0800 In-Reply-To: <20230110222003.1591436-1-irogers@google.com> Message-Id: <20230110222003.1591436-5-irogers@google.com> Mime-Version: 1.0 References: <20230110222003.1591436-1-irogers@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Subject: [PATCH v1 4/7] tools lib api: Minor strbuf_read improvements From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Nathan Chancellor , Nick Desaulniers , Tom Rix , Nicolas Schier , Masahiro Yamada , Athira Rajeev , Christy Lee , Andrii Nakryiko , Ravi Bangoria , Leo Yan , Yang Jihong , Qi Liu , James Clark , Adrian Hunter , "Masami Hiramatsu (Google)" , Kan Liang , Sean Christopherson , Zhengjun Xing , Rob Herring , Xin Gao , Zechuan Chen , Jason Wang , Christophe JAILLET , Stephane Eranian , German Gomez , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754675926509340526?= X-GMAIL-MSGID: =?utf-8?q?1754675926509340526?= If a read is smaller than the remaining space, don't grow the buffer as it is likely we've reached the end of the file. Make the grow amounts a single page rather than just over 2 once the null terminator is included. Signed-off-by: Ian Rogers --- tools/lib/api/strbuf.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/tools/lib/api/strbuf.c b/tools/lib/api/strbuf.c index 4639b2d02e62..eafa2c01f46a 100644 --- a/tools/lib/api/strbuf.c +++ b/tools/lib/api/strbuf.c @@ -143,25 +143,28 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, ssize_t hint) size_t oldalloc = sb->alloc; int ret; - ret = strbuf_grow(sb, hint ? hint : 8192); + ret = strbuf_grow(sb, hint ? hint : 4095); if (ret) return ret; for (;;) { - ssize_t cnt; + ssize_t read_size; + size_t sb_remaining = sb->alloc - sb->len - 1; - cnt = read(fd, sb->buf + sb->len, sb->alloc - sb->len - 1); - if (cnt < 0) { + read_size = read(fd, sb->buf + sb->len, sb_remaining); + if (read_size < 0) { if (oldalloc == 0) strbuf_release(sb); else strbuf_setlen(sb, oldlen); - return cnt; + return read_size; } - if (!cnt) + if (read_size == 0) break; - sb->len += cnt; - ret = strbuf_grow(sb, 8192); + sb->len += read_size; + if ((size_t)read_size < sb_remaining) + continue; + ret = strbuf_grow(sb, 4095); if (ret) return ret; } From patchwork Tue Jan 10 22:20:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 41681 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2997166wrt; Tue, 10 Jan 2023 14:23:20 -0800 (PST) X-Google-Smtp-Source: AMrXdXvxV+r90UYBbJGhdS9PVXtdhQBpAaCb169qKdJ6OUkxa6jsmbeN04HqCRjIHdQZWQYXEfw2 X-Received: by 2002:a05:6402:1005:b0:47e:5b70:dea5 with SMTP id c5-20020a056402100500b0047e5b70dea5mr63350623edu.18.1673389399897; Tue, 10 Jan 2023 14:23:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673389399; cv=none; d=google.com; s=arc-20160816; b=XxiqUagX75TaG1ZTWmtKPhHRs2eJ+LLFuo2cnsHveVLPVNTEDoXWlXTrhZ8/YmReTF ijfa7tGmRUzH/7NUOcdV22EQURaRMBiksNP5OLqefh6Fez4kPJMNwJAgtM/BeMeKF1WO cMlOwUNtWqLeuEqm4eC7/uCcxwyraV3SZX0KgRORrCpwVk9cfUb7DG+Z5YRLOYBj92w6 opQr0NxkzqNSQFU3Kd+X4fmr5DAeObQ7zuchP3HybSR5UfCqQvmQWHuYa3U1IK51rCZH IpMSKSLquD3IpAaGoJznBr5S9fc4IZxcOXxjTwMJf5RvjsQMUsZ7QA3SBPUwNH26oO6f I+3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=4sg2Aa5uCHWcDC8jm9J4EAv2OJFGtXnNHMXMT9mO4Gw=; b=HZwLF5WX+O7FNp/xUaiFoUXjG1T5kBZK5Afsx1LawuqBtzxs9dJGh4RYqTXOBMkYEb zI2xGulxLX4RbxKkKtI05HqbUVKFEHaa7pgBr2MQFFRkRF8v4R0I9iDZ0FSdJdnzvQYv wksK2ZsEtpbktaGa6suIvZxo1pOWHozg38tvYgNXoQYLtASgCYHcojNdTqRArU770WXG XRTn/NxtsCQgCbh5lKhxVX+sUddYLYsOVBD8RiGX9QlJg8hkpUpUMYTlyLikdylMKQ+y iSVcaAjaeXg+0Kt3atq5Oepb9PIrmtWHJPCSve960i6pVDrfrHpw00J0206e5P/rPLxQ vwOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=AW29Ki0i; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z16-20020a056402275000b00483b197bbe2si16874828edd.122.2023.01.10.14.22.55; Tue, 10 Jan 2023 14:23:19 -0800 (PST) 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=@google.com header.s=20210112 header.b=AW29Ki0i; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235385AbjAJWW1 (ORCPT + 99 others); Tue, 10 Jan 2023 17:22:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235236AbjAJWVu (ORCPT ); Tue, 10 Jan 2023 17:21:50 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8309863F7D for ; Tue, 10 Jan 2023 14:21:13 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-487b0bf1117so141681217b3.5 for ; Tue, 10 Jan 2023 14:21:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4sg2Aa5uCHWcDC8jm9J4EAv2OJFGtXnNHMXMT9mO4Gw=; b=AW29Ki0ipdxS14sshMxXhqz4FZFcNVmZ3WvoBGdx8Bla7+yjxNrYSNmR1ihsyWRsrf X60XFLMewiYm9IGefUm4eEzu+c8CeyIivN4qFu4JpwnYwB3jA2QMO7bOOM3Ko7g1jq8K C5RnWXn1KhH66oHffsmy/GqlYcVahiBR3eqjUSNwhLqIpkauqFmAyRmy+3bD+cnZC0dZ KFqMPnIG2GLgHMH6PYQiwuTfPPwsF8vA5I0JxA/flSZfhMIfeqXMaRE2xMVljQClk+CN NAwV6KvZTl/6ClD/vaGcAe7VaWC8QwAk0u7Hs7wmIUwxTE33GdGdAYSdTQfnpepXNN/1 khRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4sg2Aa5uCHWcDC8jm9J4EAv2OJFGtXnNHMXMT9mO4Gw=; b=quQGUIJnIRJcBLOpYedVJsaGlUh7bOlsgx0SJ983BvnwyURHTEYcKt6uT31Sh1VZTM IBOOhzG5zVeapbF3BcOpitduHnsATktvdQOXvnIULP8wNSYL0u6FKyr1PozUutdKtvqx oaFyD+FpTmkzg3NmlfjmBNKsU31rkWld/f7B5H0rpXA2G1nYUNZnyxqDXf0vNE0ItACi 8xzNYy0uMlp1xpkr3sd1/UemsepvyhY267U61hJuyXA1QQ93N+dXeEIt/4ydUWU4eSza MXtOWyQQdoLfivrqzlaqO33qbIfM5qQMuf93XpLN9yzr3xBV5//PNUUYN9s6u2uSwZOu 33ag== X-Gm-Message-State: AFqh2kpsV1pXnuTe0RIE9P9PChn//xcjMWc9JwxL4VHZ8Rnx8iy69YMi iu+9kNIUs5Q9OhPMiZPbWtM1M7esWE+5 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:cebf:c37e:8184:56]) (user=irogers job=sendgmr) by 2002:a25:449:0:b0:7b6:f8d5:82f4 with SMTP id 70-20020a250449000000b007b6f8d582f4mr2181307ybe.646.1673389264926; Tue, 10 Jan 2023 14:21:04 -0800 (PST) Date: Tue, 10 Jan 2023 14:20:01 -0800 In-Reply-To: <20230110222003.1591436-1-irogers@google.com> Message-Id: <20230110222003.1591436-6-irogers@google.com> Mime-Version: 1.0 References: <20230110222003.1591436-1-irogers@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Subject: [PATCH v1 5/7] tools lib api: Tweak strbuf allocation size computation From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Nathan Chancellor , Nick Desaulniers , Tom Rix , Nicolas Schier , Masahiro Yamada , Athira Rajeev , Christy Lee , Andrii Nakryiko , Ravi Bangoria , Leo Yan , Yang Jihong , Qi Liu , James Clark , Adrian Hunter , "Masami Hiramatsu (Google)" , Kan Liang , Sean Christopherson , Zhengjun Xing , Rob Herring , Xin Gao , Zechuan Chen , Jason Wang , Christophe JAILLET , Stephane Eranian , German Gomez , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754675963292655707?= X-GMAIL-MSGID: =?utf-8?q?1754675963292655707?= alloc_nr gives an estimate of the actual memory behind an allocation but isn't accurate. Use malloc_usable_size to accurately set the strbuf alloc, which potentially avoids realloc calls. Signed-off-by: Ian Rogers --- tools/lib/api/strbuf.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tools/lib/api/strbuf.c b/tools/lib/api/strbuf.c index eafa2c01f46a..a3d7f96d8b9f 100644 --- a/tools/lib/api/strbuf.c +++ b/tools/lib/api/strbuf.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -42,7 +43,6 @@ char *strbuf_detach(struct strbuf *sb, size_t *sz) return res; } -#define alloc_nr(x) (((x)+16)*3/2) int strbuf_grow(struct strbuf *sb, size_t extra) { char *buf; @@ -54,9 +54,6 @@ int strbuf_grow(struct strbuf *sb, size_t extra) if (nr <= sb->len) return -E2BIG; - if (alloc_nr(sb->alloc) > nr) - nr = alloc_nr(sb->alloc); - /* * Note that sb->buf == strbuf_slopbuf if sb->alloc == 0, and it is * a static variable. Thus we have to avoid passing it to realloc. @@ -66,10 +63,9 @@ int strbuf_grow(struct strbuf *sb, size_t extra) return -ENOMEM; sb->buf = buf; - sb->alloc = nr; + sb->alloc = malloc_usable_size(buf); return 0; } -#undef alloc_nr int strbuf_addch(struct strbuf *sb, int c) { From patchwork Tue Jan 10 22:20:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 41682 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2997404wrt; Tue, 10 Jan 2023 14:24:10 -0800 (PST) X-Google-Smtp-Source: AMrXdXt6qWgprhv2xxFuynxpNCaUE/HeQGzFTOsTLuKX5BTuXOVff2hrPAUM+kw7sHxo+PQMYmvI X-Received: by 2002:aa7:d28c:0:b0:497:4311:59c7 with SMTP id w12-20020aa7d28c000000b00497431159c7mr13990776edq.18.1673389450047; Tue, 10 Jan 2023 14:24:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673389450; cv=none; d=google.com; s=arc-20160816; b=GhHNTZ4HBQ7/fVeA3O+jYvH5geXa7QMiyJWDPXxX65BVTrS+mRgw1HNWMdZha5O9MJ Gz0ZHDuAnQyAJjg+YQwh5OMxx6Dyw8DWNK3yWLjcXqgAocEsogomJRV88JMpIU73ZGIE Zt7mMqYr0xQYtGXccmjts7FuMM+5XI3xJH0ebLwYGNHnEH8VZrL2VgRoQweS+DgtFt/b LFcSnpDrBaLWPOLAW/ePVduJ1eLLFTQcLtu+EFSwOGIe4Fy/UPKXYYYVh4UNfbpF2Xt4 j8JqkkLDxyidf0JZd3onLJFqpgqmEPibB5rvAwDPqNk4ds3gqxVTCNPoi62Tc+TiGC6y vegw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=/FtIYr0/RheXwrssHV0cFATW8LMtUvVk/N1uI6ptIqc=; b=ZOWLKMIBuk8uuzrnQ7yqaP3J3OFFPXckwQg2SXs52SS26nsyfJ9+FKvoDdIQR9PnzS ocbI/zU23/WfRyiR02/ZFJ9wuMoOII30Mm0G/7uBgpUBp7TkoH7gb3OEAglwgcCkkePd IyfFALDzkLLmn+VZRCb886OPMnyEfVwZ76P8NAe878o/zaeWJ+EtBINAHdlnrfoQGoYi e9LtKQyGUjWiL5hoOUxr2XyLW6RG/2xV1qNMemORXJiopO270OQ1FIc9san0g7qtPXVW vsI4Xmtnlt2IEwO2TPn82hLtRF/UW/6jNaW+cwKrDL9DqfH7Pn/7mH+8Be1zMsURC8Yr 4U2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=RNYP627o; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z14-20020a05640235ce00b00498af0a0ce7si10641667edc.466.2023.01.10.14.23.45; Tue, 10 Jan 2023 14:24:10 -0800 (PST) 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=@google.com header.s=20210112 header.b=RNYP627o; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234848AbjAJWWv (ORCPT + 99 others); Tue, 10 Jan 2023 17:22:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235275AbjAJWWH (ORCPT ); Tue, 10 Jan 2023 17:22:07 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D7341A3B1 for ; Tue, 10 Jan 2023 14:21:21 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-4d336074463so25498887b3.12 for ; Tue, 10 Jan 2023 14:21:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=/FtIYr0/RheXwrssHV0cFATW8LMtUvVk/N1uI6ptIqc=; b=RNYP627ovo2gwEzsZZbFylrtQb+tRstmx8+kwVlpER/K5C5tNb2oBe50zVPZpE1xuZ NU9zXXblOxx6EAKLVYDrvaw3SP0ZyRrbegvCU8UFWhy7tdpYKJsjdqHS8Q8fCYTB2AA1 XfxIcjNfCJVewHShT9ogfUdkxa5rGgTTPcO795wa5pyopE3PY7OwUHSfWNK2py18qEHb lU9rAZ5slO3ij3YjOrOQ5FnhJPYOoz0FdvRAzlSJcgOtT3Q85Tvra/8+nQofX8xCCpMS FEo/8EYdWVgxNvEjlwcyYar4md8dE1bKiVeg7zyJe6xTPO25BRQ85Hz4YMsQr4Ew/YJj ifFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/FtIYr0/RheXwrssHV0cFATW8LMtUvVk/N1uI6ptIqc=; b=m5mUBevCkZfDXDcUMsE0KNi2CGkZ8s7Od8SLZPS8ZQ/S1/2r6NeiroWQHU7xlkPrW2 f0B6zgZCDSAus4DJx1b1uK66nI9dYzzEnFw8XbuNvGwyN8TP3Rqu6gXSrO6fyHDJ6s8q T0QgzGLpmTE/e4HTS6tnmx97NFSu8R68X+Kaqpwh8u4NISC17Z3qfKMjGHkqyqAGkhy+ OZqS5mw1MXktt5PaECtAACxuQTJLMJg/B2Py9wbVFcr0+8MXc7bwvq8kvvwIvk3VLIXC YukauB5TE+BhTzI0rA9CEU/6E7MlbevHGixAc9vhYDJSl8iQHQub7/OKQ9esKR2hyuFc YD1w== X-Gm-Message-State: AFqh2koWvwok5cphPdAO9BTdhQJAhmNt5oRpNxYC+/Bo/hdA9EvLFtw4 D2U1y8EqgYMKoVawttScoblcgPR8cEEB X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:cebf:c37e:8184:56]) (user=irogers job=sendgmr) by 2002:a25:8110:0:b0:6f6:ec71:8ede with SMTP id o16-20020a258110000000b006f6ec718edemr5909003ybk.422.1673389272586; Tue, 10 Jan 2023 14:21:12 -0800 (PST) Date: Tue, 10 Jan 2023 14:20:02 -0800 In-Reply-To: <20230110222003.1591436-1-irogers@google.com> Message-Id: <20230110222003.1591436-7-irogers@google.com> Mime-Version: 1.0 References: <20230110222003.1591436-1-irogers@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Subject: [PATCH v1 6/7] perf help: Use run_command_strbuf From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Nathan Chancellor , Nick Desaulniers , Tom Rix , Nicolas Schier , Masahiro Yamada , Athira Rajeev , Christy Lee , Andrii Nakryiko , Ravi Bangoria , Leo Yan , Yang Jihong , Qi Liu , James Clark , Adrian Hunter , "Masami Hiramatsu (Google)" , Kan Liang , Sean Christopherson , Zhengjun Xing , Rob Herring , Xin Gao , Zechuan Chen , Jason Wang , Christophe JAILLET , Stephane Eranian , German Gomez , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754676016077287910?= X-GMAIL-MSGID: =?utf-8?q?1754676016077287910?= Remove boiler plate by using library routine. Signed-off-by: Ian Rogers --- tools/perf/builtin-help.c | 47 ++++++++++++--------------------------- 1 file changed, 14 insertions(+), 33 deletions(-) diff --git a/tools/perf/builtin-help.c b/tools/perf/builtin-help.c index 8874e1e0335b..1cb87358cd20 100644 --- a/tools/perf/builtin-help.c +++ b/tools/perf/builtin-help.c @@ -70,46 +70,27 @@ static const char *get_man_viewer_info(const char *name) static int check_emacsclient_version(void) { struct strbuf buffer = STRBUF_INIT; - struct child_process ec_process; - const char *argv_ec[] = { "emacsclient", "--version", NULL }; - int version; int ret = -1; - /* emacsclient prints its version number on stderr */ - memset(&ec_process, 0, sizeof(ec_process)); - ec_process.argv = argv_ec; - ec_process.err = -1; - ec_process.stdout_to_stderr = 1; - if (start_command(&ec_process)) { - fprintf(stderr, "Failed to start emacsclient.\n"); - return -1; - } - if (strbuf_read(&buffer, ec_process.err, 20) < 0) { - fprintf(stderr, "Failed to read emacsclient version\n"); - goto out; - } - close(ec_process.err); - /* - * Don't bother checking return value, because "emacsclient --version" - * seems to always exits with code 1. + * emacsclient may print its version number on stderr. Don't bother + * checking return value, because some "emacsclient --version" commands + * seem to always exits with code 1. */ - finish_command(&ec_process); + run_command_strbuf("emacsclient --version 2>&1", &buffer); - if (!strstarts(buffer.buf, "emacsclient")) { + if (!strstarts(buffer.buf, "emacsclient")) fprintf(stderr, "Failed to parse emacsclient version.\n"); - goto out; - } - - version = atoi(buffer.buf + strlen("emacsclient")); + else { + int version = atoi(buffer.buf + strlen("emacsclient")); - if (version < 22) { - fprintf(stderr, - "emacsclient version '%d' too old (< 22).\n", - version); - } else - ret = 0; -out: + if (version < 22) { + fprintf(stderr, + "emacsclient version '%d' too old (< 22).\n", + version); + } else + ret = 0; + } strbuf_release(&buffer); return ret; } From patchwork Tue Jan 10 22:20:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 41683 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2997564wrt; Tue, 10 Jan 2023 14:24:36 -0800 (PST) X-Google-Smtp-Source: AMrXdXvZIS6PP71oG0ZM6Z07qmeXX6HOkZpNeKY+ZCOXQOpPFRdpXw+O8Qc/rgzj32M4QCBytEPO X-Received: by 2002:a17:907:a688:b0:84d:1c67:97d7 with SMTP id vv8-20020a170907a68800b0084d1c6797d7mr14119917ejc.30.1673389476225; Tue, 10 Jan 2023 14:24:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673389476; cv=none; d=google.com; s=arc-20160816; b=vajXbO+EvmOU+ci1voZPBS8Pw1Uo+VjP/LUOxb9VhlKTyjRvcePJYoVJu/a28jaGM+ MZM40n/1qkjBOvPWnRhhWttWUlacLHfNI3aUQARxTjQzI7uL61WPDhgc6PAfyEyGXvwL sBCLdz/gL4XCyEcwtWwLi37pj6Z2hZLxQISQxngAxpkWNNUW7loBPAQbyHEuXVNsYFpu ChjcC3uaPbgZul5JLXsrggtoxZ4akWurlcDAG7gBtgza8YQBX64tYdNu49LKtoAp3FoL Ko7Br3WCwuLp3zfIV71rErh07XU9OdO5EPUhgRkhx1GK74J/bmBMDaeb7hph6jSNj/mG 9EGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=kvPH1BN0wc7R6CBYDwvNtRpXbxlXsspJN7UfOLukVhk=; b=E4YRMPK3XLTNk1dl3ryd5i8rw5g+hTqj/vPEAzZAF9zq/ZTnYk3+seRsUG8y9CuVkc ibxLjXgN8vVpicUVMIOftKb/aQ1uGxjbtdWBQnjULM8EUVB9IQHojfwO8gFeSKmwe9TF E5x9SVW3+E6+ndPC29TPl/90Gg1We/5xM16PyR29FRb6zcKxKXO17nxdyQc4L9ayfQpK u67kh5c9yhVIwN5L6UuzxTNYpqIaNwOxkj3lkCONYDQ3bVIvS/IK/E1yIlusQcQEeKjX euLtWHh9zV42bwfo8qL0/drtJNb7ryD4FUN16m8pl843HLhE8sVEQRylHpRZ7Axvqz5j ms3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=XtuABPKv; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o17-20020aa7c7d1000000b0047035379d79si12041856eds.556.2023.01.10.14.24.12; Tue, 10 Jan 2023 14:24:36 -0800 (PST) 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=@google.com header.s=20210112 header.b=XtuABPKv; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235162AbjAJWXB (ORCPT + 99 others); Tue, 10 Jan 2023 17:23:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234757AbjAJWWO (ORCPT ); Tue, 10 Jan 2023 17:22:14 -0500 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5039C616D for ; Tue, 10 Jan 2023 14:21:26 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id k9-20020aa79989000000b00586c3ab4ce6so4174091pfh.5 for ; Tue, 10 Jan 2023 14:21:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kvPH1BN0wc7R6CBYDwvNtRpXbxlXsspJN7UfOLukVhk=; b=XtuABPKvMijNQr9fb8jkyGTZ/KLwAHAmMrQd/RXO5du+uXQNtal+CFXdNPElXfFRfq Yil7XJbmdb4gkUwZGETai6Xd3/w7NaCTqYiScLj43krV4Tr2qCVjRPs+wN0MoLj3Lnbi HYD69GprkAHubvAoU9hkO4t79fGtf7ApeUGgOOofTHQNaYzKRffHUuKJF0vhdivE5JZp qlyK2l1QnqzLZso72zbqC0VMrpPUBaXPJraf+c/Jnhrg/MkduX8srZ+oHs3KJ4FW1acv 5hTxwj6tQUEVMmqcfxqU9iUdy/eUxkc+GC6xYg5qwA9Bnkwlu+5BbOAL19kFVEDTBaAG 78VQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kvPH1BN0wc7R6CBYDwvNtRpXbxlXsspJN7UfOLukVhk=; b=EqeqKzUNHnzyDSnwGqmfMc285ab3fM6wGB/bRhVK/tu7RHCjGtW0DI9Mmffc36CE20 CgMoP4rE1vzTg5THevxJp1NNgNKObWXqdPdfx9Suq0Vj8FbBCku2ouMAJ0Tr+ohcGa/9 9aaN2DRyrTLLe1y4sg/BNy6FdAdaqULXzMcONDTnWvtLhDGmYrzbPDOpBRh7cdAJADSx Y5QqUvUA+Ti60ETyhVBWtzbKQjKLD9BEYHizU+3xWq5WxK9AoeLzpl8iWxWHp0AOHMG9 1WBeU+0mhcN2wXM23551fdGcZE3yJ/Do9QwOOAG4/E7NtnZ9xtTI+BbMJqRxZWKhWyKf kkUg== X-Gm-Message-State: AFqh2kpdvqsyM1O3xyd23V8h1x3vrDcGDBLJS0NYokeLsxkp8T60/UoN Mu7cwbB1ar3t3JXf8r+OLaNjvb2mmpVB X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:cebf:c37e:8184:56]) (user=irogers job=sendgmr) by 2002:a05:6a00:1630:b0:586:906e:4dd5 with SMTP id e16-20020a056a00163000b00586906e4dd5mr1350224pfc.80.1673389281412; Tue, 10 Jan 2023 14:21:21 -0800 (PST) Date: Tue, 10 Jan 2023 14:20:03 -0800 In-Reply-To: <20230110222003.1591436-1-irogers@google.com> Message-Id: <20230110222003.1591436-8-irogers@google.com> Mime-Version: 1.0 References: <20230110222003.1591436-1-irogers@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Subject: [PATCH v1 7/7] perf llvm: Remove read_from_pipe From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Nathan Chancellor , Nick Desaulniers , Tom Rix , Nicolas Schier , Masahiro Yamada , Athira Rajeev , Christy Lee , Andrii Nakryiko , Ravi Bangoria , Leo Yan , Yang Jihong , Qi Liu , James Clark , Adrian Hunter , "Masami Hiramatsu (Google)" , Kan Liang , Sean Christopherson , Zhengjun Xing , Rob Herring , Xin Gao , Zechuan Chen , Jason Wang , Christophe JAILLET , Stephane Eranian , German Gomez , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754676043487353840?= X-GMAIL-MSGID: =?utf-8?q?1754676043487353840?= Switch to the common run_command_strbuf utility. Signed-off-by: Ian Rogers --- tools/perf/tests/bpf.c | 12 +-- tools/perf/tests/llvm.c | 18 ++-- tools/perf/tests/llvm.h | 3 +- tools/perf/util/bpf-loader.c | 9 +- tools/perf/util/llvm-utils.c | 184 +++++++---------------------------- tools/perf/util/llvm-utils.h | 6 +- 6 files changed, 61 insertions(+), 171 deletions(-) diff --git a/tools/perf/tests/bpf.c b/tools/perf/tests/bpf.c index 17c023823713..b4a6afb41e40 100644 --- a/tools/perf/tests/bpf.c +++ b/tools/perf/tests/bpf.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include "tests.h" @@ -216,14 +217,13 @@ prepare_bpf(void *obj_buf, size_t obj_buf_sz, const char *name) static int __test__bpf(int idx) { int ret; - void *obj_buf; - size_t obj_buf_sz; + struct strbuf obj_buf = STRBUF_INIT; struct bpf_object *obj; - ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz, + ret = test_llvm__fetch_bpf_obj(&obj_buf, bpf_testcase_table[idx].prog_id, false, NULL); - if (ret != TEST_OK || !obj_buf || !obj_buf_sz) { + if (ret != TEST_OK || !obj_buf.len) { pr_debug("Unable to get BPF object, %s\n", bpf_testcase_table[idx].msg_compile_fail); if ((idx == 0) || (ret == TEST_SKIP)) @@ -232,7 +232,7 @@ static int __test__bpf(int idx) return TEST_FAIL; } - obj = prepare_bpf(obj_buf, obj_buf_sz, + obj = prepare_bpf(obj_buf.buf, obj_buf.len, bpf_testcase_table[idx].name); if ((!!bpf_testcase_table[idx].target_func) != (!!obj)) { if (!obj) @@ -274,7 +274,7 @@ static int __test__bpf(int idx) } out: - free(obj_buf); + strbuf_release(&obj_buf); bpf__clear(); return ret; } diff --git a/tools/perf/tests/llvm.c b/tools/perf/tests/llvm.c index 0bc25a56cfef..97090850b7f9 100644 --- a/tools/perf/tests/llvm.c +++ b/tools/perf/tests/llvm.c @@ -4,6 +4,7 @@ #include #include "tests.h" #include "debug.h" +#include #ifdef HAVE_LIBBPF_SUPPORT #include @@ -45,8 +46,7 @@ static struct { }; int -test_llvm__fetch_bpf_obj(void **p_obj_buf, - size_t *p_obj_buf_sz, +test_llvm__fetch_bpf_obj(struct strbuf *obj_buf, enum test_llvm__testcase idx, bool force, bool *should_load_fail) @@ -83,9 +83,6 @@ test_llvm__fetch_bpf_obj(void **p_obj_buf, if (verbose == 0) verbose = -1; - *p_obj_buf = NULL; - *p_obj_buf_sz = 0; - if (!llvm_param.clang_bpf_cmd_template) goto out; @@ -106,7 +103,7 @@ test_llvm__fetch_bpf_obj(void **p_obj_buf, clang_opt_old = llvm_param.clang_opt; llvm_param.clang_opt = clang_opt_new; - err = llvm__compile_bpf("-", p_obj_buf, p_obj_buf_sz); + err = llvm__compile_bpf("-", obj_buf); llvm_param.clang_bpf_cmd_template = tmpl_old; llvm_param.clang_opt = clang_opt_old; @@ -127,24 +124,23 @@ test_llvm__fetch_bpf_obj(void **p_obj_buf, static int test__llvm(int subtest) { int ret; - void *obj_buf = NULL; - size_t obj_buf_sz = 0; + struct strbuf obj_buf = STRBUF_INIT; bool should_load_fail = false; if ((subtest < 0) || (subtest >= __LLVM_TESTCASE_MAX)) return TEST_FAIL; - ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz, + ret = test_llvm__fetch_bpf_obj(&obj_buf, subtest, false, &should_load_fail); if (ret == TEST_OK && !should_load_fail) { - ret = test__bpf_parsing(obj_buf, obj_buf_sz); + ret = test__bpf_parsing(obj_buf.buf, obj_buf.len); if (ret != TEST_OK) { pr_debug("Failed to parse test case '%s'\n", bpf_source_table[subtest].desc); } } - free(obj_buf); + strbuf_release(&obj_buf); return ret; } diff --git a/tools/perf/tests/llvm.h b/tools/perf/tests/llvm.h index f68b0d9b8ae2..2294b66dd0b6 100644 --- a/tools/perf/tests/llvm.h +++ b/tools/perf/tests/llvm.h @@ -22,7 +22,8 @@ enum test_llvm__testcase { __LLVM_TESTCASE_MAX, }; -int test_llvm__fetch_bpf_obj(void **p_obj_buf, size_t *p_obj_buf_sz, +struct strbuf; +int test_llvm__fetch_bpf_obj(struct strbuf *obj_buf, enum test_llvm__testcase index, bool force, bool *should_load_fail); #ifdef __cplusplus diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c index 6e9b06cf06ee..f3a5cf490141 100644 --- a/tools/perf/util/bpf-loader.c +++ b/tools/perf/util/bpf-loader.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "debug.h" #include "evlist.h" #include "bpf-loader.h" @@ -245,10 +246,14 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source) err = perf_clang__compile_bpf(filename, &obj_buf, &obj_buf_sz); perf_clang__cleanup(); if (err) { + struct strbuf str_obj_buf = STRBUF_INIT; + pr_debug("bpf: builtin compilation failed: %d, try external compiler\n", err); - err = llvm__compile_bpf(filename, &obj_buf, &obj_buf_sz); - if (err) + err = llvm__compile_bpf(filename, &str_obj_buf); + if (err < 0) return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE); + obj_buf = str_obj_buf.buf; + obj_buf_sz = str_obj_buf.len; } else pr_debug("bpf: successful builtin compilation\n"); obj = bpf_object__open_mem(obj_buf, obj_buf_sz, &opts); diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c index 4e8e243a6e4b..8a12160320f3 100644 --- a/tools/perf/util/llvm-utils.c +++ b/tools/perf/util/llvm-utils.c @@ -17,7 +17,9 @@ #include "config.h" #include "util.h" #include +#include #include +#include #define CLANG_BPF_CMD_DEFAULT_TEMPLATE \ "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\ @@ -125,92 +127,6 @@ static int search_program_and_warn(const char *def, const char *name, return ret; } -#define READ_SIZE 4096 -static int -read_from_pipe(const char *cmd, void **p_buf, size_t *p_read_sz) -{ - int err = 0; - void *buf = NULL; - FILE *file = NULL; - size_t read_sz = 0, buf_sz = 0; - char serr[STRERR_BUFSIZE]; - - file = popen(cmd, "r"); - if (!file) { - pr_err("ERROR: unable to popen cmd: %s\n", - str_error_r(errno, serr, sizeof(serr))); - return -EINVAL; - } - - while (!feof(file) && !ferror(file)) { - /* - * Make buf_sz always have obe byte extra space so we - * can put '\0' there. - */ - if (buf_sz - read_sz < READ_SIZE + 1) { - void *new_buf; - - buf_sz = read_sz + READ_SIZE + 1; - new_buf = realloc(buf, buf_sz); - - if (!new_buf) { - pr_err("ERROR: failed to realloc memory\n"); - err = -ENOMEM; - goto errout; - } - - buf = new_buf; - } - read_sz += fread(buf + read_sz, 1, READ_SIZE, file); - } - - if (buf_sz - read_sz < 1) { - pr_err("ERROR: internal error\n"); - err = -EINVAL; - goto errout; - } - - if (ferror(file)) { - pr_err("ERROR: error occurred when reading from pipe: %s\n", - str_error_r(errno, serr, sizeof(serr))); - err = -EIO; - goto errout; - } - - err = WEXITSTATUS(pclose(file)); - file = NULL; - if (err) { - err = -EINVAL; - goto errout; - } - - /* - * If buf is string, give it terminal '\0' to make our life - * easier. If buf is not string, that '\0' is out of space - * indicated by read_sz so caller won't even notice it. - */ - ((char *)buf)[read_sz] = '\0'; - - if (!p_buf) - free(buf); - else - *p_buf = buf; - - if (p_read_sz) - *p_read_sz = read_sz; - return 0; - -errout: - if (file) - pclose(file); - free(buf); - if (p_buf) - *p_buf = NULL; - if (p_read_sz) - *p_read_sz = 0; - return err; -} - static inline void force_set_env(const char *var, const char *value) { @@ -244,7 +160,7 @@ version_notice(void) ); } -static int detect_kbuild_dir(char **kbuild_dir) +static int detect_kbuild_dir(struct strbuf *kbuild_dir) { const char *test_dir = llvm_param.kbuild_dir; const char *prefix_dir = ""; @@ -276,10 +192,9 @@ static int detect_kbuild_dir(char **kbuild_dir) if (access(autoconf_path, R_OK) == 0) { free(autoconf_path); - err = asprintf(kbuild_dir, "%s%s%s", prefix_dir, test_dir, - suffix_dir); + err = (int)strbuf_addf(kbuild_dir, "%s%s%s", prefix_dir, test_dir, suffix_dir); if (err < 0) - return -ENOMEM; + return err; return 0; } pr_debug("%s: Couldn't find \"%s\", missing kernel-devel package?.\n", @@ -315,7 +230,7 @@ static const char *kinc_fetch_script = "rm -rf $TMPDIR\n" "exit $RET\n"; -void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts) +void llvm__get_kbuild_opts(struct strbuf *kbuild_dir, struct strbuf *kbuild_include_opts) { static char *saved_kbuild_dir; static char *saved_kbuild_include_opts; @@ -324,22 +239,14 @@ void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts) if (!kbuild_dir || !kbuild_include_opts) return; - *kbuild_dir = NULL; - *kbuild_include_opts = NULL; - if (saved_kbuild_dir && saved_kbuild_include_opts && !IS_ERR(saved_kbuild_dir) && !IS_ERR(saved_kbuild_include_opts)) { - *kbuild_dir = strdup(saved_kbuild_dir); - *kbuild_include_opts = strdup(saved_kbuild_include_opts); + strbuf_addstr(kbuild_dir, saved_kbuild_dir); + strbuf_addstr(kbuild_include_opts, saved_kbuild_include_opts); - if (*kbuild_dir && *kbuild_include_opts) - return; - - zfree(kbuild_dir); - zfree(kbuild_include_opts); /* - * Don't fall through: it may breaks saved_kbuild_dir and - * saved_kbuild_include_opts if detect them again when + * Don't fallthrough: it may break the saved_kbuild_dir and + * saved_kbuild_include_opts if they are detected again when * memory is low. */ return; @@ -361,28 +268,26 @@ void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts) goto errout; } - pr_debug("Kernel build dir is set to %s\n", *kbuild_dir); - force_set_env("KBUILD_DIR", *kbuild_dir); + pr_debug("Kernel build dir is set to %s\n", kbuild_dir->buf); + force_set_env("KBUILD_DIR", kbuild_dir->buf); force_set_env("KBUILD_OPTS", llvm_param.kbuild_opts); - err = read_from_pipe(kinc_fetch_script, - (void **)kbuild_include_opts, - NULL); + err = run_command_strbuf(kinc_fetch_script, kbuild_include_opts); if (err) { pr_warning( "WARNING:\tunable to get kernel include directories from '%s'\n" "Hint:\tTry set clang include options using 'clang-bpf-cmd-template'\n" " \toption in [llvm] section of ~/.perfconfig and set 'kbuild-dir'\n" " \toption in [llvm] to \"\" to suppress this detection.\n\n", - *kbuild_dir); + kbuild_dir->buf); zfree(kbuild_dir); goto errout; } - pr_debug("include option is set to %s\n", *kbuild_include_opts); + pr_debug("include option is set to %s\n", kbuild_include_opts->buf); - saved_kbuild_dir = strdup(*kbuild_dir); - saved_kbuild_include_opts = strdup(*kbuild_include_opts); + saved_kbuild_dir = strdup(kbuild_dir->buf); + saved_kbuild_include_opts = strdup(kbuild_include_opts->buf); if (!saved_kbuild_dir || !saved_kbuild_include_opts) { zfree(&saved_kbuild_dir); @@ -446,24 +351,23 @@ void llvm__dump_obj(const char *path, void *obj_buf, size_t size) free(obj_path); } -int llvm__compile_bpf(const char *path, void **p_obj_buf, - size_t *p_obj_buf_sz) +int llvm__compile_bpf(const char *path, struct strbuf *obj_buf) { - size_t obj_buf_sz; - void *obj_buf = NULL; int err, nr_cpus_avail; unsigned int kernel_version; char linux_version_code_str[64]; const char *clang_opt = llvm_param.clang_opt; char clang_path[PATH_MAX], llc_path[PATH_MAX], abspath[PATH_MAX], nr_cpus_avail_str[64]; char serr[STRERR_BUFSIZE]; - char *kbuild_dir = NULL, *kbuild_include_opts = NULL, - *perf_bpf_include_opts = NULL; + char *perf_bpf_include_opts = NULL; const char *template = llvm_param.clang_bpf_cmd_template; char *pipe_template = NULL; const char *opts = llvm_param.opts; - char *command_echo = NULL, *command_out; + char *command_echo = NULL; char *libbpf_include_dir = system_path(LIBBPF_INCLUDE_DIR); + struct strbuf kbuild_dir = STRBUF_INIT; + struct strbuf kbuild_include_opts = STRBUF_INIT; + struct strbuf command_out = STRBUF_INIT; if (path[0] != '-' && realpath(path, abspath) == NULL) { err = errno; @@ -501,9 +405,9 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf, force_set_env("LINUX_VERSION_CODE", linux_version_code_str); force_set_env("CLANG_EXEC", clang_path); force_set_env("CLANG_OPTIONS", clang_opt); - force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts); + force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts.buf); force_set_env("PERF_BPF_INC_OPTIONS", perf_bpf_include_opts); - force_set_env("WORKING_DIR", kbuild_dir ? : "."); + force_set_env("WORKING_DIR", kbuild_dir.len ? kbuild_dir.buf : "."); if (opts) { err = search_program_and_warn(llvm_param.llc_path, "llc", llc_path); @@ -549,11 +453,11 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf, SWAP_CHAR('&', '\006'); SWAP_CHAR('\a', '"'); } - err = read_from_pipe(command_echo, (void **) &command_out, NULL); - if (err) + err = run_command_strbuf(command_echo, &command_out); + if (err < 0) goto errout; - for (char *p = command_out; *p; p++) { + for (char *p = command_out.buf; *p; p++) { SWAP_CHAR('\001', '<'); SWAP_CHAR('\002', '>'); SWAP_CHAR('\003', '"'); @@ -562,10 +466,10 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf, SWAP_CHAR('\006', '&'); } #undef SWAP_CHAR - pr_debug("llvm compiling command : %s\n", command_out); + pr_debug("llvm compiling command : %s\n", command_out.buf); - err = read_from_pipe(template, &obj_buf, &obj_buf_sz); - if (err) { + err = run_command_strbuf(template, obj_buf); + if (err < 0) { pr_err("ERROR:\tunable to compile %s\n", path); pr_err("Hint:\tCheck error message shown above.\n"); pr_err("Hint:\tYou can also pre-compile it into .o using:\n"); @@ -574,33 +478,15 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf, goto errout; } - free(command_echo); - free(command_out); - free(kbuild_dir); - free(kbuild_include_opts); - free(perf_bpf_include_opts); - free(libbpf_include_dir); - - if (!p_obj_buf) - free(obj_buf); - else - *p_obj_buf = obj_buf; - - if (p_obj_buf_sz) - *p_obj_buf_sz = obj_buf_sz; - return 0; + err = 0; errout: free(command_echo); - free(kbuild_dir); - free(kbuild_include_opts); - free(obj_buf); + strbuf_release(&command_out); + strbuf_release(&kbuild_dir); + strbuf_release(&kbuild_include_opts); free(perf_bpf_include_opts); free(libbpf_include_dir); free(pipe_template); - if (p_obj_buf) - *p_obj_buf = NULL; - if (p_obj_buf_sz) - *p_obj_buf_sz = 0; return err; } diff --git a/tools/perf/util/llvm-utils.h b/tools/perf/util/llvm-utils.h index 7878a0e3fa98..0ad25e42aa6e 100644 --- a/tools/perf/util/llvm-utils.h +++ b/tools/perf/util/llvm-utils.h @@ -56,13 +56,15 @@ struct llvm_param { extern struct llvm_param llvm_param; int perf_llvm_config(const char *var, const char *value); -int llvm__compile_bpf(const char *path, void **p_obj_buf, size_t *p_obj_buf_sz); +struct strbuf; +int llvm__compile_bpf(const char *path, struct strbuf *obj_buf); /* This function is for test__llvm() use only */ int llvm__search_clang(void); /* Following functions are reused by builtin clang support */ -void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts); +struct strbuf; +void llvm__get_kbuild_opts(struct strbuf *kbuild_dir, struct strbuf *kbuild_include_opts); int llvm__get_nr_cpus(void); void llvm__dump_obj(const char *path, void *obj_buf, size_t size);