From: Namhyung Kim <namhyung@kernel.org>
To: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: acme@kernel.org, jolsa@kernel.org, adrian.hunter@intel.com,
irogers@google.com, segher@kernel.crashing.org,
christophe.leroy@csgroup.eu, linux-kernel@vger.kernel.org,
linux-perf-users@vger.kernel.org, linuxppc-dev@lists.ozlabs.org,
akanksha@linux.ibm.com, maddy@linux.ibm.com, kjain@linux.ibm.com,
disgoel@linux.vnet.ibm.com
Subject: Re: [V4 13/16] tools/perf: Add support to use libcapstone in powerpc
Date: Mon, 24 Jun 2024 23:08:24 -0700 [thread overview]
Message-ID: <Znpe2BHCUXvfR5Bk@google.com> (raw)
In-Reply-To: <20240614172631.56803-14-atrajeev@linux.vnet.ibm.com>
On Fri, Jun 14, 2024 at 10:56:28PM +0530, Athira Rajeev wrote:
> Now perf uses the capstone library to disassemble the instructions in
> x86. capstone is used (if available) for perf annotate to speed up.
> Currently it only supports x86 architecture. Patch includes changes to
> enable this in powerpc. For now, only for data type sort keys, this
> method is used and only binary code (raw instruction) is read. This is
> because powerpc approach to understand instructions and reg fields uses
> raw instruction. The "cs_disasm" is currently not enabled. While
> attempting to do cs_disasm, observation is that some of the instructions
> were not identified (ex: extswsli, maddld) and it had to fallback to use
> objdump. Hence enabling "cs_disasm" is added in comment section as a
> TODO for powerpc.
Well.. I'm not sure if I understand it correctly but it seems this
function effectively does nothing more than the raw disassemble.
Can we simply drop this patch for now? Or did I miss something?
Thanks,
Namhyung
>
> Signed-off-by: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
> ---
> tools/perf/util/disasm.c | 143 +++++++++++++++++++++++++++++++++++++++
> 1 file changed, 143 insertions(+)
>
> diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
> index 43743ca4bdc9..987bff9f71c3 100644
> --- a/tools/perf/util/disasm.c
> +++ b/tools/perf/util/disasm.c
> @@ -1592,6 +1592,144 @@ static void print_capstone_detail(cs_insn *insn, char *buf, size_t len,
> }
> }
>
> +static int symbol__disassemble_capstone_powerpc(char *filename, struct symbol *sym,
> + struct annotate_args *args)
> +{
> + struct annotation *notes = symbol__annotation(sym);
> + struct map *map = args->ms.map;
> + struct dso *dso = map__dso(map);
> + struct nscookie nsc;
> + u64 start = map__rip_2objdump(map, sym->start);
> + u64 end = map__rip_2objdump(map, sym->end);
> + u64 len = end - start;
> + u64 offset;
> + int i, fd, count;
> + bool is_64bit = false;
> + bool needs_cs_close = false;
> + u8 *buf = NULL;
> + struct find_file_offset_data data = {
> + .ip = start,
> + };
> + csh handle;
> + char disasm_buf[512];
> + struct disasm_line *dl;
> + u32 *line;
> + bool disassembler_style = false;
> +
> + if (args->options->objdump_path)
> + return -1;
> +
> + nsinfo__mountns_enter(dso->nsinfo, &nsc);
> + fd = open(filename, O_RDONLY);
> + nsinfo__mountns_exit(&nsc);
> + if (fd < 0)
> + return -1;
> +
> + if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data,
> + &is_64bit) == 0)
> + goto err;
> +
> + if (!args->options->disassembler_style ||
> + !strcmp(args->options->disassembler_style, "att"))
> + disassembler_style = true;
> +
> + if (capstone_init(maps__machine(args->ms.maps), &handle, is_64bit, disassembler_style) < 0)
> + goto err;
> +
> + needs_cs_close = true;
> +
> + buf = malloc(len);
> + if (buf == NULL)
> + goto err;
> +
> + count = pread(fd, buf, len, data.offset);
> + close(fd);
> + fd = -1;
> +
> + if ((u64)count != len)
> + goto err;
> +
> + line = (u32 *)buf;
> +
> + /* add the function address and name */
> + scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
> + start, sym->name);
> +
> + args->offset = -1;
> + args->line = disasm_buf;
> + args->line_nr = 0;
> + args->fileloc = NULL;
> + args->ms.sym = sym;
> +
> + dl = disasm_line__new(args);
> + if (dl == NULL)
> + goto err;
> +
> + annotation_line__add(&dl->al, ¬es->src->source);
> +
> + /*
> + * TODO: enable disassm for powerpc
> + * count = cs_disasm(handle, buf, len, start, len, &insn);
> + *
> + * For now, only binary code is saved in disassembled line
> + * to be used in "type" and "typeoff" sort keys. Each raw code
> + * is 32 bit instruction. So use "len/4" to get the number of
> + * entries.
> + */
> + count = len/4;
> +
> + for (i = 0, offset = 0; i < count; i++) {
> + args->offset = offset;
> + sprintf(args->line, "%x", line[i]);
> +
> + dl = disasm_line__new(args);
> + if (dl == NULL)
> + goto err;
> +
> + annotation_line__add(&dl->al, ¬es->src->source);
> +
> + offset += 4;
> + }
> +
> + /* It failed in the middle */
> + if (offset != len) {
> + struct list_head *list = ¬es->src->source;
> +
> + /* Discard all lines and fallback to objdump */
> + while (!list_empty(list)) {
> + dl = list_first_entry(list, struct disasm_line, al.node);
> +
> + list_del_init(&dl->al.node);
> + disasm_line__free(dl);
> + }
> + count = -1;
> + }
> +
> +out:
> + if (needs_cs_close)
> + cs_close(&handle);
> + free(buf);
> + return count < 0 ? count : 0;
> +
> +err:
> + if (fd >= 0)
> + close(fd);
> + if (needs_cs_close) {
> + struct disasm_line *tmp;
> +
> + /*
> + * It probably failed in the middle of the above loop.
> + * Release any resources it might add.
> + */
> + list_for_each_entry_safe(dl, tmp, ¬es->src->source, al.node) {
> + list_del(&dl->al.node);
> + free(dl);
> + }
> + }
> + count = -1;
> + goto out;
> +}
> +
> static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
> struct annotate_args *args)
> {
> @@ -1949,6 +2087,11 @@ int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
> err = symbol__disassemble_dso(symfs_filename, sym, args);
> if (err == 0)
> goto out_remove_tmp;
> +#ifdef HAVE_LIBCAPSTONE_SUPPORT
> + err = symbol__disassemble_capstone_powerpc(symfs_filename, sym, args);
> + if (err == 0)
> + goto out_remove_tmp;
> +#endif
> }
> }
>
> --
> 2.43.0
>
next prev parent reply other threads:[~2024-06-25 6:08 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-14 17:26 [V4 00/16] Add data type profiling support for powerpc Athira Rajeev
2024-06-14 17:26 ` [V4 01/16] tools/perf: Move the data structures related to register type to header file Athira Rajeev
2024-06-25 5:15 ` Namhyung Kim
2024-06-25 10:54 ` Athira Rajeev
2024-06-14 17:26 ` [V4 02/16] tools/perf: Add "update_insn_state" callback function to handle arch specific instruction tracking Athira Rajeev
2024-06-14 17:26 ` [V4 03/16] tools/perf: Add support to capture and parse raw instruction in powerpc using dso__data_read_offset utility Athira Rajeev
2024-06-25 5:29 ` Namhyung Kim
2024-06-25 12:38 ` Athira Rajeev
2024-06-25 18:39 ` Namhyung Kim
2024-06-26 4:09 ` Athira Rajeev
2024-06-14 17:26 ` [V4 04/16] tools/perf: Use sort keys to determine whether to pick objdump to disassemble Athira Rajeev
2024-06-25 5:32 ` Namhyung Kim
2024-06-14 17:26 ` [V4 05/16] tools/perf: Add disasm_line__parse to parse raw instruction for powerpc Athira Rajeev
2024-06-25 5:39 ` Namhyung Kim
2024-06-25 12:42 ` Athira Rajeev
2024-06-25 18:45 ` Namhyung Kim
2024-06-26 4:08 ` Athira Rajeev
2024-06-26 21:17 ` Namhyung Kim
2024-06-27 9:28 ` Athira Rajeev
2024-06-30 11:10 ` Athira Rajeev
2024-06-14 17:26 ` [V4 06/16] tools/perf: Update parameters for reg extract functions to use raw instruction on powerpc Athira Rajeev
2024-06-25 6:00 ` Namhyung Kim
2024-06-25 12:43 ` Athira Rajeev
2024-06-14 17:26 ` [V4 07/16] tools/perf: Add support to identify memory instructions of opcode 31 in powerpc Athira Rajeev
2024-06-14 17:26 ` [V4 08/16] tools/perf: Add some of the arithmetic instructions to support instruction tracking " Athira Rajeev
2024-06-14 17:26 ` [V4 09/16] tools/perf: Add more instructions for instruction tracking Athira Rajeev
2024-06-14 17:26 ` [V4 10/16] tools/perf: Update instruction tracking for powerpc Athira Rajeev
2024-06-14 17:26 ` [V4 11/16] tools/perf: Make capstone_init non-static so that it can be used during symbol disassemble Athira Rajeev
2024-06-14 17:26 ` [V4 12/16] tools/perf: Use capstone_init and remove open_capstone_handle from disasm.c Athira Rajeev
2024-06-14 17:26 ` [V4 13/16] tools/perf: Add support to use libcapstone in powerpc Athira Rajeev
2024-06-25 6:08 ` Namhyung Kim [this message]
2024-06-25 12:44 ` Athira Rajeev
2024-06-14 17:26 ` [V4 14/16] tools/perf: Add support to find global register variables using find_data_type_global_reg Athira Rajeev
2024-06-25 6:17 ` Namhyung Kim
2024-06-25 12:45 ` Athira Rajeev
2024-06-14 17:26 ` [V4 15/16] tools/perf: Add support for global_die to capture name of variable in case of register defined variable Athira Rajeev
2024-06-14 17:26 ` [V4 16/16] tools/perf: Set instruction name to be used with insn-stat when using raw instruction Athira Rajeev
2024-06-20 15:31 ` [V4 00/16] Add data type profiling support for powerpc Athira Rajeev
2024-06-22 0:06 ` Namhyung Kim
2024-06-25 11:48 ` Athira Rajeev
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=Znpe2BHCUXvfR5Bk@google.com \
--to=namhyung@kernel.org \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=akanksha@linux.ibm.com \
--cc=atrajeev@linux.vnet.ibm.com \
--cc=christophe.leroy@csgroup.eu \
--cc=disgoel@linux.vnet.ibm.com \
--cc=irogers@google.com \
--cc=jolsa@kernel.org \
--cc=kjain@linux.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=maddy@linux.ibm.com \
--cc=segher@kernel.crashing.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).