From nobody Tue Feb 10 02:59:05 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9383DC76196 for ; Mon, 3 Apr 2023 18:41:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232301AbjDCSlC (ORCPT ); Mon, 3 Apr 2023 14:41:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233293AbjDCSk5 (ORCPT ); Mon, 3 Apr 2023 14:40:57 -0400 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 89ECF26B1 for ; Mon, 3 Apr 2023 11:40:54 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-544781e30easo296014137b3.1 for ; Mon, 03 Apr 2023 11:40:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680547253; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=OQK7cbtnodbre4T5ZwtzhEhf4nO9+j+KlezsUdFmQZQ=; b=tVpMl+fJ2xR7lGshQRG8xRUYufXwnq8dpRGhpAI0fUvm1QubgYJszmtczDFduajoEc Gjb+Mxoj/C1y43MH1OvB9FeAvS8xd/j5xCiPvF9H5sTVhFSyInMI5EG8AH3j4uMCFk9O dLHSQdjGXWWGeYgmMgU+S+moRUoj9DRdGJJLvS7aaFCwIxHlu3B0qD/xi3zB3FrYx1gR mPn2HrRXHHS1UXbtC3m5VrkqpE8XKejgsW/RQQN5qDpS9+fyZkm4qbUl6u9hSTKM2o65 jWYyaoY97OoN/LgCizXu2GS77wjShHgrnCeDBvlsXli4xhG7xFS7zty9OoUc46HiatTB rH8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680547253; h=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=OQK7cbtnodbre4T5ZwtzhEhf4nO9+j+KlezsUdFmQZQ=; b=KYtmrKoaUYPW3Tv3qlqNvI1CdeVIkgcF4FOf9fDuF2uMgLiw0FY0JSTjNoI9EWLV2z z7cKrVwQELns5f+f4gfrYdMLrydjRwhPuMWSIcIDAhfTv8IAJ+DvmXUJ6W2ev2fr2NOE p0vzhgGXKjgiJwlZVsFhpoV0ABejxezm0wSUQPpN0f4NzcSmNfckTxmfPpkdeafJVWMk JIq4pxZUFzNgW6cfAzdKgH3QTvUFl2qx6ut2IZg27eeh+awfyAXEYwHUlvsOS8fkRqJ/ WlfQHcDhIRyK8tfnIFNp0B7RBnpcW8OesT2WjAY2gRlonoQ2fff1Xvt0zrwLJj51OVze I6bA== X-Gm-Message-State: AAQBX9dCyx7T9GM/GaRELYffcZ4bJ5gMabEHLffglkD+gEV2XqMy68GW pXi+212dUz3j699kg3bGQcFbLLmpSt9L X-Google-Smtp-Source: AKy350btQSLJY7hMC9l8QtYL9jayZ1N7Wt61vjnXmuX90sothMMahfnjDpIneYhVgGZY+63cibM1MdKNrvU7 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:9dc:a977:817d:341]) (user=irogers job=sendgmr) by 2002:a05:690c:dd0:b0:533:a15a:d33e with SMTP id db16-20020a05690c0dd000b00533a15ad33emr11091433ywb.5.1680547253518; Mon, 03 Apr 2023 11:40:53 -0700 (PDT) Date: Mon, 3 Apr 2023 11:40:30 -0700 In-Reply-To: <20230403184033.1836023-1-irogers@google.com> Message-Id: <20230403184033.1836023-2-irogers@google.com> Mime-Version: 1.0 References: <20230403184033.1836023-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Subject: [PATCH v2 1/4] tools api: Add io__getline From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Tom Rix , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Reads a line to allocated memory up to a newline following the getline API. Signed-off-by: Ian Rogers Acked-by: Namhyung Kim --- tools/lib/api/io.h | 45 +++++++++++++++++++++++++++++++++++++++ tools/perf/tests/api-io.c | 36 +++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/tools/lib/api/io.h b/tools/lib/api/io.h index 777c20f6b604..d5e8cf0dada0 100644 --- a/tools/lib/api/io.h +++ b/tools/lib/api/io.h @@ -7,7 +7,9 @@ #ifndef __API_IO__ #define __API_IO__ =20 +#include #include +#include #include =20 struct io { @@ -112,4 +114,47 @@ static inline int io__get_dec(struct io *io, __u64 *de= c) } } =20 +/* Read up to and including the first newline following the pattern of get= line. */ +static inline ssize_t io__getline(struct io *io, char **line_out, size_t *= line_len_out) +{ + char buf[128]; + int buf_pos =3D 0; + char *line =3D NULL, *temp; + size_t line_len =3D 0; + int ch =3D 0; + + /* TODO: reuse previously allocated memory. */ + free(*line_out); + while (ch !=3D '\n') { + ch =3D io__get_char(io); + + if (ch < 0) + break; + + if (buf_pos =3D=3D sizeof(buf)) { + temp =3D realloc(line, line_len + sizeof(buf)); + if (!temp) + goto err_out; + line =3D temp; + memcpy(&line[line_len], buf, sizeof(buf)); + line_len +=3D sizeof(buf); + buf_pos =3D 0; + } + buf[buf_pos++] =3D (char)ch; + } + temp =3D realloc(line, line_len + buf_pos + 1); + if (!temp) + goto err_out; + line =3D temp; + memcpy(&line[line_len], buf, buf_pos); + line[line_len + buf_pos] =3D '\0'; + line_len +=3D buf_pos; + *line_out =3D line; + *line_len_out =3D line_len; + return line_len; +err_out: + free(line); + return -ENOMEM; +} + #endif /* __API_IO__ */ diff --git a/tools/perf/tests/api-io.c b/tools/perf/tests/api-io.c index e91cf2c127f1..6aea84ca6673 100644 --- a/tools/perf/tests/api-io.c +++ b/tools/perf/tests/api-io.c @@ -289,6 +289,40 @@ static int test_get_dec(void) return ret; } =20 +static int test_get_line(void) +{ + char path[PATH_MAX]; + struct io io; + char test_string[1024]; + char *line =3D NULL; + size_t i, line_len =3D 0; + size_t buf_size =3D 128; + int ret =3D 0; + + for (i =3D 0; i < 512; i++) + test_string[i] =3D 'a'; + test_string[512] =3D '\n'; + for (i =3D 513; i < 1023; i++) + test_string[i] =3D 'b'; + test_string[1023] =3D '\0'; + + if (setup_test(path, test_string, buf_size, &io)) + return -1; + + EXPECT_EQUAL((int)io__getline(&io, &line, &line_len), 513); + EXPECT_EQUAL((int)strlen(line), 513); + for (i =3D 0; i < 512; i++) + EXPECT_EQUAL(line[i], 'a'); + EXPECT_EQUAL(line[512], '\n'); + EXPECT_EQUAL((int)io__getline(&io, &line, &line_len), 510); + for (i =3D 0; i < 510; i++) + EXPECT_EQUAL(line[i], 'b'); + + free(line); + cleanup_test(path, &io); + return ret; +} + static int test__api_io(struct test_suite *test __maybe_unused, int subtest __maybe_unused) { @@ -300,6 +334,8 @@ static int test__api_io(struct test_suite *test __maybe= _unused, ret =3D TEST_FAIL; if (test_get_dec()) ret =3D TEST_FAIL; + if (test_get_line()) + ret =3D TEST_FAIL; return ret; } =20 --=20 2.40.0.348.gf938b09366-goog From nobody Tue Feb 10 02:59:05 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9771C76188 for ; Mon, 3 Apr 2023 18:41:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233351AbjDCSlL (ORCPT ); Mon, 3 Apr 2023 14:41:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233335AbjDCSlH (ORCPT ); Mon, 3 Apr 2023 14:41:07 -0400 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 6258A2D64 for ; Mon, 3 Apr 2023 11:41:03 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id i11-20020a256d0b000000b0086349255277so29623569ybc.8 for ; Mon, 03 Apr 2023 11:41:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680547262; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=nxpnc6V+BLhZie01D44ojVhVFOFzbhA9j9GSFH/Nu8Y=; b=hwj88vaJ4+Y4t44Pc7JAEIdjdu8O+kDcQlAMsCQw7f3eC4J4s2p1hGBaWc/NXwC63O DsyOegPMB/uCFpPEuRNW+gZBhzRnTCzpSZVLrEZFjyuRWjIyzuTBHnYw1Pv8070uFG26 qhGj4p3dV5TmAtSKqpGa17nztcr9TOGMDgRtAgghoJ+7Bdx8ZyceLKefYRf7FyiPEJIu +OYg+ZOltwIrfH0g5uNmwX6r+ImNZfZ/Z6cKXUUzQFIs8bCAGgaUTOmooJ/N8f5Ncilq f1qVdKHUurqvdGC+d6KWT+Z/nOGsLwKH+2aRDRlAB97AhRt13ViY1G4vgKxve0i50N6j IP9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680547262; h=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=nxpnc6V+BLhZie01D44ojVhVFOFzbhA9j9GSFH/Nu8Y=; b=EA8sQ7Hjtr4erzAPzwFIKWLoFwUYElhAXyqp4R3T692o8XdwNRAWzpq8B3c6NMEFBG Qg2H2GFCrXUnNPznjoK0d1goLCPjaOwuA1euHxwzwdKvM8Ku8eG7IU9OaAcF1CVOtnR7 jTtiTy3+shkzDoEnFxFD9ZVUQGWFNeivEoJYFX0/D6dVsWL99uGs80OhHGMoiAf0X7w/ +vpAa69ryA0gBMrxlNvlggcA6UX1orElgrAgbVrmyQnFQsHuPy+9LfQsoeYDSWWUW938 CtzO0p6TOUWvYA8lPTLF+w1l2n8ydT4W3/vvg2t7ut57LWRWPP0+SoKMWixAxMtGXHCz X1Jw== X-Gm-Message-State: AAQBX9eOVQ/rQa657dog+tYtRgJW8wwr2OYMVYIzIppge3cCDFCmm0Y4 BY7DGyJ14SVczqDrQa6BguVrBmuzBopv X-Google-Smtp-Source: AKy350a5kQmmVSvp0tyGx5e/hXylOZJGTBzQf0fmA0bgB9ai4hvhQLfXnGtmA6gY28gBxX+rjE3YN9CRxmVL X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:9dc:a977:817d:341]) (user=irogers job=sendgmr) by 2002:a81:b20a:0:b0:541:7237:6e6b with SMTP id q10-20020a81b20a000000b0054172376e6bmr14ywh.0.1680547261957; Mon, 03 Apr 2023 11:41:01 -0700 (PDT) Date: Mon, 3 Apr 2023 11:40:31 -0700 In-Reply-To: <20230403184033.1836023-1-irogers@google.com> Message-Id: <20230403184033.1836023-3-irogers@google.com> Mime-Version: 1.0 References: <20230403184033.1836023-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Subject: [PATCH v2 2/4] perf srcline: Simplify addr2line subprocess From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Tom Rix , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Don't wrap stdin and stdout of subprocess with streams, use the api/io library for buffering. Signed-off-by: Ian Rogers Acked-by: Namhyung Kim --- tools/perf/util/srcline.c | 103 ++++++++++++++------------------------ 1 file changed, 38 insertions(+), 65 deletions(-) diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c index f0a96a834e4b..5339ab4c5e12 100644 --- a/tools/perf/util/srcline.c +++ b/tools/perf/util/srcline.c @@ -10,6 +10,8 @@ #include #include =20 +#include + #include "util/dso.h" #include "util/debug.h" #include "util/callchain.h" @@ -366,12 +368,6 @@ void dso__free_a2l(struct dso *dso) =20 #else /* HAVE_LIBBFD_SUPPORT */ =20 -struct a2l_subprocess { - struct child_process addr2line; - FILE *to_child; - FILE *from_child; -}; - static int filename_split(char *filename, unsigned int *line_nr) { char *sep; @@ -393,28 +389,18 @@ static int filename_split(char *filename, unsigned in= t *line_nr) return 0; } =20 -static void addr2line_subprocess_cleanup(struct a2l_subprocess *a2l) +static void addr2line_subprocess_cleanup(struct child_process *a2l) { - if (a2l->addr2line.pid !=3D -1) { - kill(a2l->addr2line.pid, SIGKILL); - finish_command(&a2l->addr2line); /* ignore result, we don't care */ - a2l->addr2line.pid =3D -1; - } - - if (a2l->to_child !=3D NULL) { - fclose(a2l->to_child); - a2l->to_child =3D NULL; - } - - if (a2l->from_child !=3D NULL) { - fclose(a2l->from_child); - a2l->from_child =3D NULL; + if (a2l->pid !=3D -1) { + kill(a2l->pid, SIGKILL); + finish_command(a2l); /* ignore result, we don't care */ + a2l->pid =3D -1; } =20 free(a2l); } =20 -static struct a2l_subprocess *addr2line_subprocess_init(const char *addr2l= ine_path, +static struct child_process *addr2line_subprocess_init(const char *addr2li= ne_path, const char *binary_path) { const char *argv[] =3D { @@ -422,54 +408,34 @@ static struct a2l_subprocess *addr2line_subprocess_in= it(const char *addr2line_pa "-e", binary_path, "-i", "-f", NULL }; - struct a2l_subprocess *a2l =3D zalloc(sizeof(*a2l)); + struct child_process *a2l =3D zalloc(sizeof(*a2l)); int start_command_status =3D 0; =20 - if (a2l =3D=3D NULL) - goto out; - - a2l->to_child =3D NULL; - a2l->from_child =3D NULL; + if (a2l =3D=3D NULL) { + pr_err("Failed to allocate memory for addr2line"); + return NULL; + } =20 - a2l->addr2line.pid =3D -1; - a2l->addr2line.in =3D -1; - a2l->addr2line.out =3D -1; - a2l->addr2line.no_stderr =3D 1; + a2l->pid =3D -1; + a2l->in =3D -1; + a2l->out =3D -1; + a2l->no_stderr =3D 1; =20 - a2l->addr2line.argv =3D argv; - start_command_status =3D start_command(&a2l->addr2line); - a2l->addr2line.argv =3D NULL; /* it's not used after start_command; avoid= dangling pointers */ + a2l->argv =3D argv; + start_command_status =3D start_command(a2l); + a2l->argv =3D NULL; /* it's not used after start_command; avoid dangling = pointers */ =20 if (start_command_status !=3D 0) { pr_warning("could not start addr2line (%s) for %s: start_command return = code %d\n", addr2line_path, binary_path, start_command_status); - goto out; - } - - a2l->to_child =3D fdopen(a2l->addr2line.in, "w"); - if (a2l->to_child =3D=3D NULL) { - pr_warning("could not open write-stream to addr2line (%s) of %s\n", - addr2line_path, binary_path); - goto out; - } - - a2l->from_child =3D fdopen(a2l->addr2line.out, "r"); - if (a2l->from_child =3D=3D NULL) { - pr_warning("could not open read-stream from addr2line (%s) of %s\n", - addr2line_path, binary_path); - goto out; + addr2line_subprocess_cleanup(a2l); + return NULL; } =20 return a2l; - -out: - if (a2l) - addr2line_subprocess_cleanup(a2l); - - return NULL; } =20 -static int read_addr2line_record(struct a2l_subprocess *a2l, +static int read_addr2line_record(struct io *io, char **function, char **filename, unsigned int *line_nr) @@ -494,7 +460,7 @@ static int read_addr2line_record(struct a2l_subprocess = *a2l, if (line_nr !=3D NULL) *line_nr =3D 0; =20 - if (getline(&line, &line_len, a2l->from_child) < 0 || !line_len) + if (io__getline(io, &line, &line_len) < 0 || !line_len) goto error; if (function !=3D NULL) *function =3D strdup(strim(line)); @@ -502,7 +468,7 @@ static int read_addr2line_record(struct a2l_subprocess = *a2l, zfree(&line); line_len =3D 0; =20 - if (getline(&line, &line_len, a2l->from_child) < 0 || !line_len) + if (io__getline(io, &line, &line_len) < 0 || !line_len) goto error; =20 if (filename_split(line, line_nr =3D=3D NULL ? &dummy_line_nr : line_nr) = =3D=3D 0) { @@ -546,13 +512,17 @@ static int addr2line(const char *dso_name, u64 addr, struct inline_node *node, struct symbol *sym __maybe_unused) { - struct a2l_subprocess *a2l =3D dso->a2l; + struct child_process *a2l =3D dso->a2l; char *record_function =3D NULL; char *record_filename =3D NULL; unsigned int record_line_nr =3D 0; int record_status =3D -1; int ret =3D 0; size_t inline_count =3D 0; + int len; + char buf[128]; + ssize_t written; + struct io io; =20 if (!a2l) { if (!filename__has_section(dso_name, ".debug_line")) @@ -578,13 +548,16 @@ static int addr2line(const char *dso_name, u64 addr, * though, because it may be genuinely unknown, in which case we'll get t= wo sets of * "??"/"??:0" lines. */ - if (fprintf(a2l->to_child, "%016"PRIx64"\n,\n", addr) < 0 || fflush(a2l->= to_child) !=3D 0) { + len =3D snprintf(buf, sizeof(buf), "%016"PRIx64"\n,\n", addr); + written =3D len > 0 ? write(a2l->in, buf, len) : -1; + if (written !=3D len) { if (!symbol_conf.disable_add2line_warn) pr_warning("%s %s: could not send request\n", __func__, dso_name); goto out; } + io__init(&io, a2l->out, buf, sizeof(buf)); =20 - switch (read_addr2line_record(a2l, &record_function, &record_filename, &r= ecord_line_nr)) { + switch (read_addr2line_record(&io, &record_function, &record_filename, &r= ecord_line_nr)) { case -1: if (!symbol_conf.disable_add2line_warn) pr_warning("%s %s: could not read first record\n", __func__, dso_name); @@ -594,7 +567,7 @@ static int addr2line(const char *dso_name, u64 addr, * The first record was invalid, so return failure, but first read anoth= er * record, since we asked a junk question and have to clear the answer o= ut. */ - switch (read_addr2line_record(a2l, NULL, NULL, NULL)) { + switch (read_addr2line_record(&io, NULL, NULL, NULL)) { case -1: if (!symbol_conf.disable_add2line_warn) pr_warning("%s %s: could not read delimiter record\n", @@ -632,7 +605,7 @@ static int addr2line(const char *dso_name, u64 addr, } =20 /* We have to read the records even if we don't care about the inline inf= o. */ - while ((record_status =3D read_addr2line_record(a2l, + while ((record_status =3D read_addr2line_record(&io, &record_function, &record_filename, &record_line_nr)) =3D=3D 1) { @@ -656,7 +629,7 @@ static int addr2line(const char *dso_name, u64 addr, =20 void dso__free_a2l(struct dso *dso) { - struct a2l_subprocess *a2l =3D dso->a2l; + struct child_process *a2l =3D dso->a2l; =20 if (!a2l) return; --=20 2.40.0.348.gf938b09366-goog From nobody Tue Feb 10 02:59:05 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7DBA9C761A6 for ; Mon, 3 Apr 2023 18:41:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233277AbjDCSlV (ORCPT ); Mon, 3 Apr 2023 14:41:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233350AbjDCSlR (ORCPT ); Mon, 3 Apr 2023 14:41:17 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3E3A2103 for ; Mon, 3 Apr 2023 11:41:10 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id f66-20020a255145000000b00b714602d43fso29960462ybb.10 for ; Mon, 03 Apr 2023 11:41:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680547270; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=sfQ3ydjQvMhzFReVFE6Rco19saqYJT8yxFgkuZOml8M=; b=Gu9wGljsOntmdKnEQFzbEEJ5s2QA2ynGBRSQkiTtPlLcurHe8D5nMKz98D9i7XuBnc r3Sz6X+VA75m02Auo2Tb7nU8ThH887Qgj9ekDqvBArbZJd311nc8zJK9cxoHeDTimYyI 5Kj+WyMHMCPMTJIoNEG5Uolq5LB1SvlqrNa4j1wpdcB8c33q5tKfuM1psYsZYJDKUrI3 MXJfYMJ8/oHr9T4IGC5l/F/KdbqW1xN4GwgmkGFy1E0uWUST7UWAd/dktPn/0VFa26Wx +/IFcIAcoLylzrHMNok9MdS9sjBHJvyFjQBeSizhogSnQFvOP2N1qtxElngXbkqhhAzb wHmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680547270; h=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=sfQ3ydjQvMhzFReVFE6Rco19saqYJT8yxFgkuZOml8M=; b=hM5KSG/j4uUYx5q9Kid7RHJxjkaLYvgcM3fyXAYoRY/qV/9JwWHEn3+TeazJbTLT5J SJ5aUlcdGolRYDakvUiSGnsOurSKspnDcOdmQ0Grtqp6+WloSH49ZV6lloDYeQ22c21L UVKi+6AKRJk09NvrEjhZWi9FeeDLEEeQaNNfOd8Jwq1Ckq9x5gxcczusP+DSPvG5LaC7 o4A7i1Vu64rooPhm/e27qRlQlKOgi4yAHd6DMolwq/O6x4vOtg/605N1KMp+IP05UbBc bvSzkrdzbTXqC852rav0gtxc2IaXMjl72+YtK2J/Rkjuj1KBVZYzO6pYYrgLTgxG4fjU xwjQ== X-Gm-Message-State: AAQBX9eaAO5KWFKVLPv/OdGRx74oXmm/4Mz13BQ2pjFigxkzbJ7jjOlO SF3sWaJDO5j6tRFfd2NOJzd/L5+3Hsmr X-Google-Smtp-Source: AKy350Z1ASPKD1sZeo9v/RbnHxaoRVHKJh3KAyrhCIfp3t0FuCVIKe5XpEH/+L8x6G9opfogGkMCdXjVWb// X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:9dc:a977:817d:341]) (user=irogers job=sendgmr) by 2002:a25:d412:0:b0:b76:3b21:b1dc with SMTP id m18-20020a25d412000000b00b763b21b1dcmr78538ybf.0.1680547270082; Mon, 03 Apr 2023 11:41:10 -0700 (PDT) Date: Mon, 3 Apr 2023 11:40:32 -0700 In-Reply-To: <20230403184033.1836023-1-irogers@google.com> Message-Id: <20230403184033.1836023-4-irogers@google.com> Mime-Version: 1.0 References: <20230403184033.1836023-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Subject: [PATCH v2 3/4] perf srcline: Support for llvm-addr2line From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Tom Rix , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The sentinel value differs for llvm-addr2line. Configure this once and then detect when reading records. Signed-off-by: Ian Rogers Acked-by: Namhyung Kim --- tools/perf/util/srcline.c | 74 +++++++++++++++++++++++++++++++++++---- 1 file changed, 67 insertions(+), 7 deletions(-) diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c index 5339ab4c5e12..f4fcdada821b 100644 --- a/tools/perf/util/srcline.c +++ b/tools/perf/util/srcline.c @@ -435,7 +435,50 @@ static struct child_process *addr2line_subprocess_init= (const char *addr2line_pat return a2l; } =20 +enum a2l_style { + BROKEN, + GNU_BINUTILS, + LLVM, +}; + +static enum a2l_style addr2line_configure(struct child_process *a2l) +{ + static bool cached; + static enum a2l_style style; + + if (!cached) { + char buf[128]; + struct io io; + int ch; + + if (write(a2l->in, ",\n", 2) !=3D 2) + return BROKEN; + + io__init(&io, a2l->out, buf, sizeof(buf)); + ch =3D io__get_char(&io); + if (ch =3D=3D ',') { + style =3D LLVM; + cached =3D true; + } else if (ch =3D=3D '?') { + style =3D GNU_BINUTILS; + cached =3D true; + } else { + style =3D BROKEN; + } + do { + ch =3D io__get_char(&io); + } while (ch > 0 && ch !=3D '\n'); + if (style =3D=3D GNU_BINUTILS) { + do { + ch =3D io__get_char(&io); + } while (ch > 0 && ch !=3D '\n'); + } + } + return style; +} + static int read_addr2line_record(struct io *io, + enum a2l_style style, char **function, char **filename, unsigned int *line_nr) @@ -462,6 +505,12 @@ static int read_addr2line_record(struct io *io, =20 if (io__getline(io, &line, &line_len) < 0 || !line_len) goto error; + + if (style =3D=3D LLVM && line_len =3D=3D 2 && line[0] =3D=3D ',') { + zfree(&line); + return 0; + } + if (function !=3D NULL) *function =3D strdup(strim(line)); =20 @@ -471,7 +520,8 @@ static int read_addr2line_record(struct io *io, if (io__getline(io, &line, &line_len) < 0 || !line_len) goto error; =20 - if (filename_split(line, line_nr =3D=3D NULL ? &dummy_line_nr : line_nr) = =3D=3D 0) { + if (filename_split(line, line_nr =3D=3D NULL ? &dummy_line_nr : line_nr) = =3D=3D 0 && + style =3D=3D GNU_BINUTILS) { ret =3D 0; goto error; } @@ -523,6 +573,7 @@ static int addr2line(const char *dso_name, u64 addr, char buf[128]; ssize_t written; struct io io; + enum a2l_style a2l_style; =20 if (!a2l) { if (!filename__has_section(dso_name, ".debug_line")) @@ -538,15 +589,22 @@ static int addr2line(const char *dso_name, u64 addr, pr_warning("%s %s: addr2line_subprocess_init failed\n", __func__, dso_n= ame); goto out; } + a2l_style =3D addr2line_configure(a2l); + if (a2l_style =3D=3D BROKEN) { + if (!symbol_conf.disable_add2line_warn) + pr_warning("%s: addr2line configuration failed\n", __func__); + goto out; + } =20 /* * Send our request and then *deliberately* send something that can't be = interpreted as * a valid address to ask addr2line about (namely, ","). This causes addr= 2line to first * write out the answer to our request, in an unbounded/unknown number of= records, and - * then to write out the lines "??" and "??:0", so that we can detect whe= n it has - * finished giving us anything useful. We have to be careful about the fi= rst record, - * though, because it may be genuinely unknown, in which case we'll get t= wo sets of - * "??"/"??:0" lines. + * then to write out the lines "??" and "??:0", for GNU binutils, or "," = for + * llvm-addr2line, so that we can detect when it has finished giving us a= nything + * useful. With GNU binutils, we have to be careful about the first recor= d, though, + * because it may be genuinely unknown, in which case we'll get two sets = of "??"/"??:0" + * lines. */ len =3D snprintf(buf, sizeof(buf), "%016"PRIx64"\n,\n", addr); written =3D len > 0 ? write(a2l->in, buf, len) : -1; @@ -557,7 +615,8 @@ static int addr2line(const char *dso_name, u64 addr, } io__init(&io, a2l->out, buf, sizeof(buf)); =20 - switch (read_addr2line_record(&io, &record_function, &record_filename, &r= ecord_line_nr)) { + switch (read_addr2line_record(&io, a2l_style, + &record_function, &record_filename, &record_line_nr)) { case -1: if (!symbol_conf.disable_add2line_warn) pr_warning("%s %s: could not read first record\n", __func__, dso_name); @@ -567,7 +626,7 @@ static int addr2line(const char *dso_name, u64 addr, * The first record was invalid, so return failure, but first read anoth= er * record, since we asked a junk question and have to clear the answer o= ut. */ - switch (read_addr2line_record(&io, NULL, NULL, NULL)) { + switch (read_addr2line_record(&io, a2l_style, NULL, NULL, NULL)) { case -1: if (!symbol_conf.disable_add2line_warn) pr_warning("%s %s: could not read delimiter record\n", @@ -606,6 +665,7 @@ static int addr2line(const char *dso_name, u64 addr, =20 /* We have to read the records even if we don't care about the inline inf= o. */ while ((record_status =3D read_addr2line_record(&io, + a2l_style, &record_function, &record_filename, &record_line_nr)) =3D=3D 1) { --=20 2.40.0.348.gf938b09366-goog From nobody Tue Feb 10 02:59:05 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 843D5C76188 for ; Mon, 3 Apr 2023 18:41:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233311AbjDCSlf (ORCPT ); Mon, 3 Apr 2023 14:41:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233373AbjDCSl3 (ORCPT ); Mon, 3 Apr 2023 14:41:29 -0400 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 047E911D for ; Mon, 3 Apr 2023 11:41:18 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-546422bd3ceso124306957b3.21 for ; Mon, 03 Apr 2023 11:41:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680547277; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=8W0bFO1La/7HK/HpWnbmeoTx54QZk6D+Z078ZNMeyCg=; b=m6LfHyI56ByW91W423utUqnJXSW4puezbyRvLd6hsvF1zXWMY7ZofDsdmw/fYmx7FE sJe5baFpA9RtIsAPV/TuuKyopzHqf8x5UkNGMNP+GpGoiGynVSQnFjaM+qXQySgIvwx+ PpviiDCXQkgIgjISkk1CcHi80s4Dp3E3eRHHQqKOqoDDJgMb1sSV6jObKGSiFd96xoaz efTEqDOKLltwL2YvagCx265klOH3KpeZ04hy6yRfwooPWtXeqEVIBeKYS7iklOuGYFB+ pE3VKKQoBOJHo7uXBa68D7TOgCN2ChVGEJEngsRm7GT4i0MP++uce4te53qFdGmSdqL+ GkJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680547277; h=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=8W0bFO1La/7HK/HpWnbmeoTx54QZk6D+Z078ZNMeyCg=; b=IPkSkgpDnNljxDWhE7qC/pIqvWYQvDjmTPUBjju2rojlR73rON/UwN0ZD6Extf029T PWr94MHDhTDYSMruMZKF9m7uCYCUrF2RTH6S9MjR/T4H5iWBeZRF5dm7ICn5U8578US9 ge75S44eAyT7Dm5ZwHvvl9bC5OuGvuPBxKfwoWdCPdkvMVzuKTzZrHdHCRzoXjQB3liG xvMR4y65T65dUm/HjU3ftN3evxYhkCQNea7nZy6bL6rOpvupslKJ3DOvMb+W9DhzP1rI 2CTHKxP6kURjULP8Um7voEy0D9bSHrxtWljXY7obzdjxkfXCwDIoCuf8aqvUCs1O2AsN auUQ== X-Gm-Message-State: AAQBX9dUGReCM5Tkv11G0mMlELuK+HNrgEnlKGMS55W6W8N8+tu7QQ8F HboXy4jmIoZPPk+T5GEGkFDg58wqs0Ro X-Google-Smtp-Source: AKy350YA7iS2fx3kOTsfSZF1PwKeWeZXwx2DADzFWTKQU5tx6UHKq2lzlgfua94c/o+4RdLMBwIC0QQq9qCc X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:9dc:a977:817d:341]) (user=irogers job=sendgmr) by 2002:a25:d9c7:0:b0:b46:4a5e:365f with SMTP id q190-20020a25d9c7000000b00b464a5e365fmr131859ybg.8.1680547277315; Mon, 03 Apr 2023 11:41:17 -0700 (PDT) Date: Mon, 3 Apr 2023 11:40:33 -0700 In-Reply-To: <20230403184033.1836023-1-irogers@google.com> Message-Id: <20230403184033.1836023-5-irogers@google.com> Mime-Version: 1.0 References: <20230403184033.1836023-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Subject: [PATCH v2 4/4] perf srcline: Avoid addr2line SIGPIPEs From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Tom Rix , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Ignore SIGPIPEs when addr2line is configured. Signed-off-by: Ian Rogers Acked-by: Namhyung Kim --- tools/perf/util/srcline.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c index f4fcdada821b..cfca03abd6f8 100644 --- a/tools/perf/util/srcline.c +++ b/tools/perf/util/srcline.c @@ -473,6 +473,8 @@ static enum a2l_style addr2line_configure(struct child_= process *a2l) ch =3D io__get_char(&io); } while (ch > 0 && ch !=3D '\n'); } + /* Ignore SIGPIPE in the event addr2line exits. */ + signal(SIGPIPE, SIG_IGN); } return style; } --=20 2.40.0.348.gf938b09366-goog