From nobody Tue Feb 10 02:58:45 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 AD8D5C7619A for ; Fri, 31 Mar 2023 00:49:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229786AbjCaAtY (ORCPT ); Thu, 30 Mar 2023 20:49:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39554 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229788AbjCaAtV (ORCPT ); Thu, 30 Mar 2023 20:49:21 -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 914F212CD7 for ; Thu, 30 Mar 2023 17:49:15 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id g187-20020a2520c4000000b00b74680a7904so20391436ybg.15 for ; Thu, 30 Mar 2023 17:49:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680223754; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=spG6zzO9g3fs5SXpot8q/2pK6ppWq86BL6g1AzlYlI8=; b=YDgFT/UrDT40A0mDZesfEQ/18wK9sghzlxZyTzqDXJtagBsUXXW7uOHai3aXA+7z9S Z51z50bOdAOZ8tOfzQqho705eBRCnPx6P8AoG25bnWd8lKORMgJcfkAhQ5rC6ZVxxvsT LbscONL7fa3MASMs8uquM61pwvdWV8mRwQ404jRkWqzN6cgMSTcRdcPqhjv7pIbJLjTV lfX9lYOJDAiLblUzxkG/f/C6Ww4RXfPbn82NAtJRwgA0b+YMxUU5kVcf4DGkr60g6pCj W4niAtw6HwFKiSoooyZxDrmF/sXF9KPl8atICH9rwNHalrpjJlLnOk5UOpirjegwXpOu ukVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680223754; 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=spG6zzO9g3fs5SXpot8q/2pK6ppWq86BL6g1AzlYlI8=; b=qR528XXhXb+LW3OwKEGo5uEfZiYXBwCa7W6LFWKT0Qn2dd+QL3nXVhpIahAgO+rIpr G222HKVoD0P70/6m4c7KP6Q31UGYSIVzdbNDDQqmxUNn0IBksVbOHXM6LBb+prm+o9jZ lQSqcwQ5kZnYl0ojrYyYpCeTDQ0HlGFkth8uS7ZF4ICa+QqHoe2rf83LW/Jj/fm1By0Y dzGn01gGBGVlsYskSKxAOVysLaqxl3VYBmTWTRGS8VbTAaaJWOwtXuXVd8cVgr4sEiKo AGGTGlmZKMKjd4cRB5hmvera3OXl+WiFJ9yVA1EhwM1QoqjpW0d3+3bX9J2HTTIJqdg/ gbQg== X-Gm-Message-State: AAQBX9ep9A7WObEhWW4fbLaoX0DkFgC6bNNTAik1IL79TA0brgIyPqzk 9gkzylcGq8wB3wiVB4yt/KKhfl4IvXEO X-Google-Smtp-Source: AKy350YKhXH+IKNk7Jv7yNdb04XZfK5AsWImdFuWAxZk8IvkJBcCpajhlUVxiR93uF/jRtxrE+YNpTLeEw4E X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:11ea:eb75:e48c:2c04]) (user=irogers job=sendgmr) by 2002:a05:6902:1501:b0:b6d:80ab:8bb6 with SMTP id q1-20020a056902150100b00b6d80ab8bb6mr16975618ybu.1.1680223754659; Thu, 30 Mar 2023 17:49:14 -0700 (PDT) Date: Thu, 30 Mar 2023 17:48:41 -0700 In-Reply-To: <20230331004844.1592789-1-irogers@google.com> Message-Id: <20230331004844.1592789-2-irogers@google.com> Mime-Version: 1.0 References: <20230331004844.1592789-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Subject: [PATCH v1 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 --- tools/lib/api/io.h | 40 +++++++++++++++++++++++++++++++++++++++ tools/perf/tests/api-io.c | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/tools/lib/api/io.h b/tools/lib/api/io.h index 777c20f6b604..d874e8fa8b07 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,42 @@ 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(char **line_out, size_t *line_len_out, s= truct io *io) +{ + char buf[128]; + int buf_pos =3D 0; + char *line =3D NULL; + 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)) { + line =3D realloc(line, line_len + sizeof(buf)); + if (!line) + return -ENOMEM; + memcpy(&line[line_len], buf, sizeof(buf)); + line_len +=3D sizeof(buf); + buf_pos =3D 0; + } + buf[buf_pos++] =3D (char)ch; + } + line =3D realloc(line, line_len + buf_pos + 1); + if (!line) + return -ENOMEM; + 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; +} + #endif /* __API_IO__ */ diff --git a/tools/perf/tests/api-io.c b/tools/perf/tests/api-io.c index e91cf2c127f1..0ff39cdfcb01 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(&line, &line_len, &io), 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(&line, &line_len, &io), 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:58:45 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 E051CC761AF for ; Fri, 31 Mar 2023 00:49:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229810AbjCaAtg (ORCPT ); Thu, 30 Mar 2023 20:49:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229788AbjCaAte (ORCPT ); Thu, 30 Mar 2023 20:49:34 -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 AFD6C1207F for ; Thu, 30 Mar 2023 17:49:23 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id w5-20020a253005000000b00aedd4305ff2so20601714ybw.13 for ; Thu, 30 Mar 2023 17:49:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680223763; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=r8kDVr0L3/jz3PKx2/Xe0c5OesyXlwiHmE5YPaDrqp0=; b=hFAdw/K0Go/isitryy0LSHmnD7fhXsz6n3po1ZSyOfHRKVuSNM5iVw+qzblpNVH8yK GgL6o5uLCXZvmcAFyfPz3JBPLTw0eMlCG2qqY1m16DWdaGNHzawprr4+y2ey38eunBer RL5zjRvWS2cNNW8HGaPz4aVYxSRoClBE6FRl0BXcQZ6bQQmJrKRAEELHWmuaGm+r99Dy mlrfvTFbxAT9IJd6Wko+gkL9o+7NJIog04d/2w34avGJjqU8AfK8gbPBzjtGPWeq+wx2 7eR5TUc4VXzVqAQbUrJB6KWkbYIdmonFVIBRBweNMozheXLLgY4LqL79KPDAs9JmK1Pb QeGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680223763; 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=r8kDVr0L3/jz3PKx2/Xe0c5OesyXlwiHmE5YPaDrqp0=; b=iJmDGssFS+tV50z1QPYHtGK4hMw6/MTHSWR7oRzb7N7kEJVdTB5G+0NTvboXydLZzp K45RcqN1x2hOZjwgfu1E2Rf6RDAedZj9H7O3MwYCfQU+pO/xL21m9+/uhi0sCjVtpUWX Ch0Q3Wh0v+ySGdRA39oMLzedSqpA2mcKksE6Oe22pfgA7Zz7zmjBUep2GSNXgESccMyb R35xBUspazPSxDEvXBhl0phq4uDbSoctzSMlZAyNW7X8oXnF/QmmhsKyAN8Wxlnn7ajD h3gHwcU3zoLk4HsgOAY5+zX0wqo79b156sLDKb+ikyZsQ7+O6+Nb8KnGi2FMbtv159Co QfSg== X-Gm-Message-State: AAQBX9c0bTkBkNaLfsmheHVfTH9yLX2RXQnQiMpB7lToGdcbaGGJ4Tvx YDUrIummutSsuHRt/fBRppjJKvRNUKPF X-Google-Smtp-Source: AKy350aJkuyRGOFqyRqlaNJdTuRJxkuDhuBr5i7y2eTM0OAROBvmVidnwWbq21tgIgl3OOw+ykZgzlmpCkHY X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:11ea:eb75:e48c:2c04]) (user=irogers job=sendgmr) by 2002:a05:6902:168d:b0:b6d:1483:bc18 with SMTP id bx13-20020a056902168d00b00b6d1483bc18mr16244213ybb.7.1680223762986; Thu, 30 Mar 2023 17:49:22 -0700 (PDT) Date: Thu, 30 Mar 2023 17:48:42 -0700 In-Reply-To: <20230331004844.1592789-1-irogers@google.com> Message-Id: <20230331004844.1592789-3-irogers@google.com> Mime-Version: 1.0 References: <20230331004844.1592789-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Subject: [PATCH v1 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 --- 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..2da86e57215d 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(&line, &line_len, io) < 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(&line, &line_len, io) < 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:58:45 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 55311C7619A for ; Fri, 31 Mar 2023 00:49:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229837AbjCaAtr (ORCPT ); Thu, 30 Mar 2023 20:49:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229822AbjCaAtn (ORCPT ); Thu, 30 Mar 2023 20:49:43 -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 B742611E8C for ; Thu, 30 Mar 2023 17:49:31 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-544781e30easo202380017b3.1 for ; Thu, 30 Mar 2023 17:49:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680223771; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=7oxd+W11NuknN5NF+IipV44WcyCkZNF/Dx6bcDvAiT8=; b=KsdyLKvSEFbnoBYCu2r2O888p0VE6efe0Pok+IhIFZthAU349DLJX2KPXHXB/YmZ7v mqWXRt6Cui7RbnAl+H+RBTwTdjrkrkoOdWmkqGI60wgpg/VK4A0DjJkm1PxmV5wmsOx1 /TeYIQRcpu8HeSdv+xV9cGuDj+CPMoM2H8xJezT2JtwjmZHXevlnRAxN2onqgLnXMkeA EJDZ/Nl86FZA8YtiYDFWL2Jm/bKeDJQwcYdL68R3O+T3zup/3G56JVWmzwmNq/aDsw+n uwF5zl6K2NJEFZLTHkC4NMlAfkl8xzX0aeG22x4iUU1azRjNX2TV5k4/fX69Dedt2TWV xFeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680223771; 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=7oxd+W11NuknN5NF+IipV44WcyCkZNF/Dx6bcDvAiT8=; b=xlltQcVVNsE8U3tHsWSDCXhZReXyhn1GowxnbT81rQBydLLMbjylWbOmJMO3lb1uVW pFOrEzVfNLShLZqcCyEkjGgPihkb6mA1gtYcfb6aqqZyMobxrT6lDJmKMPR4YMLWqCAk 3pnjWMq8vqJPN8EXzN6hf16CbWAoSIvaos6YIZziNuSuGQQWNglzeB/DmuDC6wSU5wC0 epG6DHV/W1XHyFKp4P4NE/iRYuAIkGSMzCYnTVQtInCCpOX2kPT+byrt3+Dwo0hBzXVS AGB0tv+/tIYrDZaw0xju/L1bw+3qqb2BzczwXs6xzB0Lf5TAU+Sq2ve2v4EZe1aYTOLc nd3Q== X-Gm-Message-State: AAQBX9feaByQrDpgmbASKfCauWULR3b0mgTT3z0vRiQuPMHaGwYldcRK ez7C1iBXOgFe1DNV7EeijUv/Xa2lPvU3 X-Google-Smtp-Source: AKy350bDVwr8m1Oc+vefnZXhKJxJxJ5yTGusejTrFkjb+CBfOpCsR2pfVEtuUYmLYwF0e9gCesDEytfcOd9V X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:11ea:eb75:e48c:2c04]) (user=irogers job=sendgmr) by 2002:a05:6902:18cd:b0:b78:45fd:5f01 with SMTP id ck13-20020a05690218cd00b00b7845fd5f01mr15780898ybb.7.1680223770977; Thu, 30 Mar 2023 17:49:30 -0700 (PDT) Date: Thu, 30 Mar 2023 17:48:43 -0700 In-Reply-To: <20230331004844.1592789-1-irogers@google.com> Message-Id: <20230331004844.1592789-4-irogers@google.com> Mime-Version: 1.0 References: <20230331004844.1592789-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Subject: [PATCH v1 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 --- tools/perf/util/srcline.c | 65 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c index 2da86e57215d..423ddf3967b0 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(&line, &line_len, io) < 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(&line, &line_len, io) < 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,6 +589,12 @@ 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 @@ -557,7 +614,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 +625,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 +664,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:58:45 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 B59F5C761AF for ; Fri, 31 Mar 2023 00:49:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229822AbjCaAtz (ORCPT ); Thu, 30 Mar 2023 20:49:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229831AbjCaAtr (ORCPT ); Thu, 30 Mar 2023 20:49:47 -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 2EA1412CF4 for ; Thu, 30 Mar 2023 17:49:40 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5417f156cb9so205503247b3.8 for ; Thu, 30 Mar 2023 17:49:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680223779; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=ZjS2ubyipVi2ZMDZ77od8b4DQTs3LsPG+UyJYia9quE=; b=Z3sRXpowh/3nWuTnA9ABQqlwKz9e/xaBcX8rB1G+JuHoqvIkDMhppRkNVRu2bUEHZD DcC346N6icjGxjlxtlLeXPBaAoXHoIDXDh/8n1hZk1rYidPWX5OaeuTCkDt50Bv0q+Lu 80Xjxt8eqOOsrqHIuSE0bNDLJRDkZXu6yTVVZ62h9kqTeGsLVb9Yo0Ov9CtNO8orwV9A 0LmbNG+Wz6nzzMPQ19TvVwytQedxgKy0Nm7IRaiblHe2QebEnrZhCIxN1WOuvKyBciAi d/wPJXHFQHM9MVHHwCPIjIIKK7gCJj02pIeZBIQipfsYZR+VBL4ws1tnoHf+kPTyKWXD gnRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680223779; 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=ZjS2ubyipVi2ZMDZ77od8b4DQTs3LsPG+UyJYia9quE=; b=mTKOcF6zRZA2XS9ttMyQn/vjDa7e0Ee+wLRhbK6KqWrNkUz6rmoR03QMAJ4JmRc5Vf IAibeS1nwmpfB/sTvFaDcRMbwHwIfFHau681ulGmxv7ZM8Hq0x7unJzF9snCjBQR5xCc qrFzd89GXuM7mRdjGbIxsW2KUUyzXgJo7XKj5vQcjoHMGq18fCXw9/y7GEPKDU4xlYb+ RBoaZCJOXE5YAUgQJCTzO/qjoj2hyCyTPzcVe5nntgCpMDh4sXhhZ95QymuzYsPcngeg OIlVvAz/URHnn5vA4WFfjRK2mz/G3oZJtkI6Wo7x1/plh4Iy/k4q82b2l8hzLdzz3ZiV /kvg== X-Gm-Message-State: AAQBX9cHcue8/ZhKcVpxmtvJ7u6N9B36RAppii0AhzFLMBUBTyJO0J7V ETHurni9FU+6lz74a7Ae62T41Hee8cns X-Google-Smtp-Source: AKy350a3m5BHFnMmj6UwcvTHHQb2LbUjX3nn1IHOfBofEyRM+5fd1eyYoe8wYYAs+1wdFn70nEN5YmCfcHDW X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:11ea:eb75:e48c:2c04]) (user=irogers job=sendgmr) by 2002:a81:ac64:0:b0:545:6106:5334 with SMTP id z36-20020a81ac64000000b0054561065334mr12667656ywj.8.1680223779432; Thu, 30 Mar 2023 17:49:39 -0700 (PDT) Date: Thu, 30 Mar 2023 17:48:44 -0700 In-Reply-To: <20230331004844.1592789-1-irogers@google.com> Message-Id: <20230331004844.1592789-5-irogers@google.com> Mime-Version: 1.0 References: <20230331004844.1592789-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Subject: [PATCH v1 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 --- 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 423ddf3967b0..bc3e48afe054 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