linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: James Clark <james.clark@arm.com>
To: Ian Rogers <irogers@google.com>,
	Chaitanya S Prakash <ChaitanyaS.Prakash@arm.com>
Cc: linux-perf-users@vger.kernel.org, anshuman.khandual@arm.com,
	Arnaldo Carvalho de Melo <acme@redhat.com>
Subject: Re: [PATCH 2/3] perf tools: Only treat files as map files when they have the extension .map
Date: Mon, 25 Mar 2024 10:21:33 +0000	[thread overview]
Message-ID: <6fc0c346-530f-476f-af2e-4f9d06828b64@arm.com> (raw)
In-Reply-To: <CAP-5=fUFmeoTjLuZTgcaV23iGQU1AdddG+7Rw=d6buMU007+1Q@mail.gmail.com>



On 20/02/2024 14:40, Ian Rogers wrote:
> On Mon, Feb 19, 2024 at 8:30 PM Chaitanya S Prakash
> <ChaitanyaS.Prakash@arm.com> wrote:
>>
>> From: Chaitanya S Prakash <chaitanyas.prakash@arm.com>
>>
>> Perf currently treats any file beginning with "/tmp/perf-" as a map
>> file, even when map files always have the extension ".map". This causes
>> the test "perf probe of function from different CU" to fail because it
>> generates a binary with a filename in that format. When the test passes
>> it as an argument to be opened as an elf file, it's parsed as a map file
>> and results in garbage symbol names. Fix this by only considering files
>> ending in ".map" as map files.
>>
>> The above failure is masked if Perf is built with dwarf support. But
>> with NO_DWARF=1, try_to_find_probe_trace_events() is skipped over in
>> convert_to_probe_trace_events() falling into
>> find_probe_trace_events_from_map() which results in the bad map file
>> parse data being consumed and the inability to create the probe event.
>>
>> Signed-off-by: Chaitanya S Prakash <chaitanyas.prakash@arm.com>
>> ---
>>  tools/perf/builtin-script.c | 15 +--------------
>>  tools/perf/util/string.c    | 18 ++++++++++++++++++
>>  tools/perf/util/string2.h   |  1 +
>>  tools/perf/util/symbol.c    |  5 ++++-
>>  4 files changed, 24 insertions(+), 15 deletions(-)
>>
>> diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
>> index b1f57401ff23..c1c38a3df37a 100644
>> --- a/tools/perf/builtin-script.c
>> +++ b/tools/perf/builtin-script.c
>> @@ -48,6 +48,7 @@
>>  #include <errno.h>
>>  #include <inttypes.h>
>>  #include <signal.h>
>> +#include <string2.h>
>>  #include <sys/param.h>
>>  #include <sys/types.h>
>>  #include <sys/stat.h>
>> @@ -3240,20 +3241,6 @@ static struct script_desc *script_desc__findnew(const char *name)
>>         return s;
>>  }
>>
>> -static const char *ends_with(const char *str, const char *suffix)
>> -{
>> -       size_t suffix_len = strlen(suffix);
>> -       const char *p = str;
>> -
>> -       if (strlen(str) > suffix_len) {
>> -               p = str + strlen(str) - suffix_len;
>> -               if (!strncmp(p, suffix, suffix_len))
>> -                       return p;
>> -       }
>> -
>> -       return NULL;
>> -}
>> -
>>  static int read_script_info(struct script_desc *desc, const char *filename)
>>  {
>>         char line[BUFSIZ], *p;
>> diff --git a/tools/perf/util/string.c b/tools/perf/util/string.c
>> index 116a642ad99d..94c7af70e636 100644
>> --- a/tools/perf/util/string.c
>> +++ b/tools/perf/util/string.c
>> @@ -349,3 +349,21 @@ char *strreplace_chars(char needle, const char *haystack, const char *replace)
>>
>>         return new_s;
>>  }
>> +
>> +/*
>> + * Returns a pointer to the first character of the suffix in str if the string
>> + * ends with suffix, otherwise returns NULL.
>> + */
>> +const char *ends_with(const char *str, const char *suffix)
>> +{
>> +       size_t suffix_len = strlen(suffix);
>> +       const char *p = str;
>> +
>> +       if (strlen(str) > suffix_len) {
>> +               p = str + strlen(str) - suffix_len;
> 
> nit: I know this is just moving code but it seems unnecessary here to
> call "strlen(str)" twice rather than just hold the result in a local
> variable. A reasonable compiler should optimize this away, but it'd be
> better to have the local imo.
> 
>> +               if (!strncmp(p, suffix, suffix_len))
>> +                       return p;
>> +       }
>> +
>> +       return NULL;
>> +}
>> diff --git a/tools/perf/util/string2.h b/tools/perf/util/string2.h
>> index 52cb8ba057c7..0b9d1579a701 100644
>> --- a/tools/perf/util/string2.h
>> +++ b/tools/perf/util/string2.h
>> @@ -40,5 +40,6 @@ char *strdup_esc(const char *str);
>>
>>  unsigned int hex(char c);
>>  char *strreplace_chars(char needle, const char *haystack, const char *replace);
>> +const char *ends_with(const char *str, const char *suffix);
> 
> nit: string2.h is an extension of linux's string.h. The tools copy of
> that is missing functions in the kernel version:
> https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/include/linux/string.h?h=perf-tools-next
> https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/include/linux/string.h?h=perf-tools-next#n398
> specifically str_has_prefix.
> 
> The naming ends_with makes sense but there is also strstarts and
> str_has_prefix, perhaps str_has_suffix would be the most consistent
> and intention revealing name?
> 
> Also, we have strtailcmp which behaves like a reverse strcmp that
> doesn't compare the lengths of the strings. It seems all uses of
> strtailcmp are just for a "str_has_suffix". It would make sense to me
> to remove that function and switch to a common str_has_suffix function
> which I think is a more intention revealing way of naming what the
> code is doing.
> 

I glossed over this before, but looking in more detail I couldn't see
that strtailcmp() was definitely being used as str_has_suffix(). Was it
done this way because either the created probe or the dwarf entry could
be using the tail as the filename, but it's not always known which is which?

I couldn't see that function being hit in any of the tests either, so it
seems quite risky to change it and it doesn't cost anything to leave it
as it is seeing as it is a function that does something different to
str_has_suffix().

  parent reply	other threads:[~2024-03-25 10:21 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-20  4:29 [PATCH 0/3] perf tools: Fix test "perf probe of function from different CU" Chaitanya S Prakash
2024-02-20  4:29 ` [PATCH 1/3] perf tools: Enable configs required for test_uprobe_from_different_cu.sh Chaitanya S Prakash
2024-02-20  4:29 ` [PATCH 2/3] perf tools: Only treat files as map files when they have the extension .map Chaitanya S Prakash
2024-02-20 14:40   ` Ian Rogers
2024-02-21 14:58     ` Arnaldo Carvalho de Melo
2024-02-23 12:10       ` Chaitanya S Prakash
2024-03-15 20:34         ` Arnaldo Carvalho de Melo
2024-03-19  2:26           ` Chaitanya S Prakash
2024-03-25 10:21     ` James Clark [this message]
2024-02-20  4:29 ` [PATCH 3/3] perf test: Check output of the probe ... --funcs command Chaitanya S Prakash
2024-02-20 10:16 ` [PATCH 0/3] perf tools: Fix test "perf probe of function from different CU" James Clark

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6fc0c346-530f-476f-af2e-4f9d06828b64@arm.com \
    --to=james.clark@arm.com \
    --cc=ChaitanyaS.Prakash@arm.com \
    --cc=acme@redhat.com \
    --cc=anshuman.khandual@arm.com \
    --cc=irogers@google.com \
    --cc=linux-perf-users@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).