From patchwork Thu Oct 12 03:51:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 151731 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp968050vqb; Wed, 11 Oct 2023 21:02:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHnJwB/zugoGHrvqpAx8zyQfak1MzZsplOwpQf2CkzIpdzRSCsqpxCRd1IGWxRdUvsKYSQU X-Received: by 2002:a05:6a00:d46:b0:68e:369b:a123 with SMTP id n6-20020a056a000d4600b0068e369ba123mr24974000pfv.1.1697083366802; Wed, 11 Oct 2023 21:02:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697083366; cv=none; d=google.com; s=arc-20160816; b=QmwmeDsuBU237OrLfpbYLyG3RH5UExL8vrTmKyMMc0EBycIH1xdlJBGG6YFcBBaHGo nddPZx7FOaMjo+NGXcS0b8prCIGB2eBPzwhkYogq44RvziAONnOwG9sd/RScdQai9+Tf xM5+hey3iFK41Na5rf7IOYxcNnMfmMhy5QAZCB3sjizYeiq5mwbLtsG9pDeVKxbA1tns bXTg52UGtp8OmxMR+ANtYmgz1eY8QfasA+bshYe6hAdd9AqEB9AaWV4VIP8XfVNL4VVf SmqKXT2f8pumgBmZQTVKXCZdVVIeEu2chap34Qb0kPevR0TzdrmqRfv7xlm94k/hLvR6 7XDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=MfHvfZCOPkeeX0/2Z0PzUnXJQ0Ykg0RYecE8el64+KU=; fh=7Id7wL2M+oCJXD4imOzx4OKioxBXSOZqypDprm7aO4E=; b=XuIasbytScEHk+g4YxMaDwl4aIwCi4+88yBEcW8Jwf9O/V9dNUVInAP1keibnQVUUA Mj2MCYnKV0VyhdQ4obNaa2ATMX4vKrrr2Ey3yG/+xf91JnFvfsCRjJxflmGYOT3nVppn niUQJaE94cp37Jbk6ZM055wJd5thMvy1V55da6vSDSe6C5k9uXSDABvnk4QzSICYcnEd PWJUNkLd+DXE03FPhHEmtsG3rfTeIFXlEoJhgJLmNc9/P62bmQI0ioilGACSTlkSdE0r XZbn04xrB/BCE/yud+MpAx+TOTcrCjSYRlfr4l42XJJbzg6qfpAueERIgNMhdaLXW1/4 KAhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=H3XLs9KS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id bx13-20020a056a00428d00b00690c0055246si13468678pfb.294.2023.10.11.21.02.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 21:02:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=H3XLs9KS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 7089280CFD02; Wed, 11 Oct 2023 21:02:43 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377294AbjJLECW (ORCPT + 18 others); Thu, 12 Oct 2023 00:02:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235422AbjJLEBe (ORCPT ); Thu, 12 Oct 2023 00:01:34 -0400 Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C7F4198B; Wed, 11 Oct 2023 20:52:12 -0700 (PDT) Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-68fb85afef4so455928b3a.1; Wed, 11 Oct 2023 20:52:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697082731; x=1697687531; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=MfHvfZCOPkeeX0/2Z0PzUnXJQ0Ykg0RYecE8el64+KU=; b=H3XLs9KSraE69cE0ezVbHgibcYknGNKa5UJHcbWTr6FyxGlmeYoq0c3wQfm8NJnYXE wTcbQjQUMaPbGyb0p9Ez6M3GZaPWUBE/zYj6mBPXVyCNw7/mxT3R8V/dQh8tVIGIiCVG EH6ldiwq8GifUzpNO4wlLvPvviAwa+DJfmUak5Y/5Qc/J9uanGYngfnuqSa8Wst09AOI K8OHmaX9keIUU7tKd2B9N8LvVS18C5PzF/bZ7XoVBPHHjGjtaQ/uKfdQWlyC1p/dsDfi 9viK1+S8HbzMHaGoJ9twruBs7Q7rXowI3+1hTOr0OPkK3GMo7FvckqPx0Vc4CmjB55nm k2+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697082731; x=1697687531; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=MfHvfZCOPkeeX0/2Z0PzUnXJQ0Ykg0RYecE8el64+KU=; b=TMhInYquiOmR+O24xrbNwW1nPjVAqn5r3krPAIWuvy2OyJwavMn+wMwGS4cmPQur91 kA8/9saTHzRPOCYESjXbqzbP/ng+4dayC79jHhYE3PQmKP208FI5AOu74nhBlfmodJGm 4aGgOJXobvjYaWxUuDvL6158W+S3Od9hi7Zf5J4OCrWmi/twWnRo/NFD3XsHyzQ7qG8p qCfaiT4QfjjNEbbMrOkuL4/qpztDVliQtNFaf+ClgD1/56Fu7N43njOqA+S8OZsEQNdQ IGm0qLXeD/NGwrZ4uLSCYWHez50hel6vuNIvHf8jopgC5HbhHzfSJ10KHPhuyp5PGWn4 s2Zg== X-Gm-Message-State: AOJu0Yxq+enBdA02n6IOoD3A2cJcN/a8xvB1P9H/AU2TKHByaLLWCAMH IVSLtqm1usZTJJMqPnCs9Fg= X-Received: by 2002:a05:6a20:f3b0:b0:15e:9c2f:5294 with SMTP id qr48-20020a056a20f3b000b0015e9c2f5294mr18897918pzb.56.1697082730976; Wed, 11 Oct 2023 20:52:10 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:42e0:b1b9:d490:2f5e:be06]) by smtp.gmail.com with ESMTPSA id w8-20020a170902d70800b001bc18e579aesm711374ply.101.2023.10.11.20.52.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 20:52:10 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa , Peter Zijlstra Cc: Ian Rogers , Adrian Hunter , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Linus Torvalds , Stephane Eranian , Masami Hiramatsu , linux-toolchains@vger.kernel.org, linux-trace-devel@vger.kernel.org Subject: [PATCH 43/48] perf annotate-data: Implement instruction tracking Date: Wed, 11 Oct 2023 20:51:06 -0700 Message-ID: <20231012035111.676789-44-namhyung@kernel.org> X-Mailer: git-send-email 2.42.0.655.g421f12c284-goog In-Reply-To: <20231012035111.676789-1-namhyung@kernel.org> References: <20231012035111.676789-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 11 Oct 2023 21:02:43 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779520888507430411 X-GMAIL-MSGID: 1779520888507430411 If it failed to find a variable for the location directly, it might be due to a missing variable in the source code. For example, accessing pointer variables in a chain can result in the case like below: struct foo *foo = ...; int i = foo->bar->baz; The DWARF debug information is created for each variable so it'd have one for 'foo'. But there's no variable for 'foo->bar' and then it cannot know the type of 'bar' and 'baz'. The above source code can be compiled to the follow x86 instructions: mov 0x8(%rax), %rcx mov 0x4(%rcx), %rdx <=== PMU sample mov %rdx, -4(%rbp) Let's say 'foo' is located in the %rax and it has a pointer to struct foo. But perf sample is captured in the second instruction and there is no variable or type info for the %rcx. It'd be great if compiler could generate debug info for %rcx, but we should handle it on our side. So this patch implements the logic to iterate instructions and update the type table for each location. As it already collected a list of scopes including the target instruction, we can use it to construct the type table smartly. +---------------- scope[0] subprogram | | +-------------- scope[1] lexical_block | | | | +------------ scope[2] inlined_subroutine | | | | | | +---------- scope[3] inlined_subroutine | | | | | | | | +-------- scope[4] lexical_block | | | | | | | | | | *** target instruction ... Image the target instruction has 5 scopes, each scope will have its own variables and parameters. Then it can start with the innermost scope (4). So it'd search the shortest path from the start of scope[4] to the target address and build a list of basic blocks. Then it iterates the basic blocks with the variables in the scope and update the table. If it finds a type at the target instruction, then returns it. Otherwise, it moves to the upper scope[3]. Now it'd search the shortest path from the start of scope[3] to the start of scope[4]. Then connect it to the existing basic block list. Then it'd iterate the blocks with variables for both scopes. It can repeat this until it finds a type at the target instruction or reaches to the top scope[0]. As the basic blocks contain the shortest path, it won't worry about branches and can update the table simply. With this change, the stat now looks like below: Annotate data type stats: total 294, ok 185 (62.9%), bad 109 (37.1%) ----------------------------------------------------------- 30 : no_sym 32 : no_mem_ops 27 : no_var 13 : no_typeinfo 7 : bad_offset Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 232 ++++++++++++++++++++++++++++++++ 1 file changed, 232 insertions(+) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-data.c index f3f85cb9ac00..1992ef20f71d 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -614,6 +614,231 @@ void update_insn_state(struct type_state *state, struct data_loc_info *dloc, /* Case 4. memory to memory transfers (not handled for now) */ } +/* Prepend this_list to full_list, removing duplicate disasm line */ +static void prepend_basic_blocks(struct list_head *this_blocks, + struct list_head *full_blocks) +{ + struct annotated_basic_block *first_bb, *last_bb; + + last_bb = list_last_entry(this_blocks, typeof(*last_bb), list); + first_bb = list_first_entry(full_blocks, typeof(*first_bb), list); + + if (list_empty(full_blocks)) + goto out; + + if (last_bb->end != first_bb->begin) { + pr_debug("prepend basic blocks: mismatched disasm line %lx -> %lx\n", + last_bb->end->al.offset, first_bb->begin->al.offset); + goto out; + } + + /* Is the basic block have only one disasm_line? */ + if (last_bb->begin == last_bb->end) { + list_del(&last_bb->list); + free(last_bb); + goto out; + } + + last_bb->end = list_prev_entry(last_bb->end, al.node); + +out: + list_splice(this_blocks, full_blocks); +} + +static void delete_basic_blocks(struct list_head *basic_blocks) +{ + struct annotated_basic_block *bb, *tmp; + + list_for_each_entry_safe(bb, tmp, basic_blocks, list) { + list_del(&bb->list); + free(bb); + } +} + +/* Make sure all variables have a valid start address */ +static void fixup_var_address(struct die_var_type *var_types, u64 addr) +{ + while (var_types) { + /* + * Some variables have no address range meaning it's always + * available in the whole scope. Let's adjust the start + * address to the start of the scope. + */ + if (var_types->addr == 0) + var_types->addr = addr; + + var_types = var_types->next; + } +} + +static void delete_var_types(struct die_var_type *var_types) +{ + while (var_types) { + struct die_var_type *next = var_types->next; + + free(var_types); + var_types = next; + } +} + +/* It's at the target address, check if it has a matching type */ +static bool find_matching_type(struct type_state *state, + struct data_loc_info *dloc, int reg, + Dwarf_Die *type_die) +{ + Dwarf_Word size; + + if (state->regs[reg].ok) { + int tag = dwarf_tag(&state->regs[reg].type); + + /* + * Normal registers should hold a pointer (or array) to + * dereference a memory location. + */ + if (tag != DW_TAG_pointer_type && tag != DW_TAG_array_type) + return false; + + if (die_get_real_type(&state->regs[reg].type, type_die) == NULL) + return false; + + dloc->type_offset = dloc->op->offset; + + /* Get the size of the actual type */ + if (dwarf_aggregate_size(type_die, &size) < 0 || + (unsigned)dloc->type_offset >= size) + return false; + + return true; + } + + if (reg == dloc->fbreg) { + struct type_state_stack *stack; + + stack = find_stack_state(state, dloc->type_offset); + if (stack == NULL) + return false; + + *type_die = stack->type; + /* Update the type offset from the start of slot */ + dloc->type_offset -= stack->offset; + return true; + } + + if (dloc->fb_cfa) { + struct type_state_stack *stack; + u64 pc = map__rip_2objdump(dloc->ms->map, dloc->ip); + int fbreg, fboff; + + if (die_get_cfa(dloc->di->dbg, pc, &fbreg, &fboff) < 0) + fbreg = -1; + + if (reg != fbreg) + return false; + + stack = find_stack_state(state, dloc->type_offset - fboff); + if (stack == NULL) + return false; + + *type_die = stack->type; + /* Update the type offset from the start of slot */ + dloc->type_offset -= fboff + stack->offset; + return true; + } + + return false; +} + +/* Iterate instructions in basic blocks and update type table */ +static bool find_data_type_insn(struct data_loc_info *dloc, int reg, + struct list_head *basic_blocks, + struct die_var_type *var_types, + Dwarf_Die *cu_die, Dwarf_Die *type_die) +{ + struct type_state state; + struct symbol *sym = dloc->ms->sym; + struct annotation *notes = symbol__annotation(sym); + struct annotated_basic_block *bb; + bool found = false; + + init_type_state(&state, dloc->arch); + + list_for_each_entry(bb, basic_blocks, list) { + struct disasm_line *dl = bb->begin; + + list_for_each_entry_from(dl, ¬es->src->source, al.node) { + u64 this_ip = sym->start + dl->al.offset; + u64 addr = map__rip_2objdump(dloc->ms->map, this_ip); + + /* Update variable type at this address */ + update_var_state(&state, dloc, addr, var_types); + + if (this_ip == dloc->ip) { + found = find_matching_type(&state, dloc, reg, + type_die); + goto out; + } + + /* Update type table after processing the instruction */ + update_insn_state(&state, dloc, cu_die, dl); + if (dl == bb->end) + break; + } + } + +out: + exit_type_state(&state); + return found; +} + +/* + * Construct a list of basic blocks for each scope with variables and try to find + * the data type by updating a type state table through instructions. + */ +static int find_data_type_block(struct data_loc_info *dloc, int reg, + Dwarf_Die *cu_die, Dwarf_Die *scopes, + int nr_scopes, Dwarf_Die *type_die) +{ + LIST_HEAD(basic_blocks); + struct die_var_type *var_types = NULL; + u64 src_ip, dst_ip; + int ret = -1; + + dst_ip = dloc->ip; + for (int i = nr_scopes - 1; i >= 0; i--) { + Dwarf_Addr base, start, end; + LIST_HEAD(this_blocks); + + if (dwarf_ranges(&scopes[i], 0, &base, &start, &end) < 0) + break; + + src_ip = map__objdump_2rip(dloc->ms->map, start); + + /* Get basic blocks for this scope */ + if (annotate_get_basic_blocks(dloc->ms->sym, src_ip, dst_ip, + &this_blocks) < 0) + continue; + prepend_basic_blocks(&this_blocks, &basic_blocks); + + /* Get variable info for this scope and add to var_types list */ + die_collect_vars(&scopes[i], &var_types); + fixup_var_address(var_types, start); + + /* Find from start of this scope to the target instruction */ + if (find_data_type_insn(dloc, reg, &basic_blocks, var_types, + cu_die, type_die)) { + ret = 0; + break; + } + + /* Go up to the next scope and find blocks to the start */ + dst_ip = src_ip; + } + + delete_basic_blocks(&basic_blocks); + delete_var_types(var_types); + return ret; +} + /* The result will be saved in @type_die */ static int find_data_type_die(struct data_loc_info *dloc, Dwarf_Die *type_die) { @@ -714,6 +939,13 @@ static int find_data_type_die(struct data_loc_info *dloc, Dwarf_Die *type_die) goto out; } + if (reg != DWARF_REG_PC) { + ret = find_data_type_block(dloc, reg, &cu_die, scopes, + nr_scopes, type_die); + if (ret == 0) + goto out; + } + if (loc->multi_regs && reg == loc->reg1 && loc->reg1 != loc->reg2) { reg = loc->reg2; goto retry;