perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
/*
|
|
|
|
* Convert sample address to data type using DWARF debug info.
|
|
|
|
*
|
|
|
|
* Written by Namhyung Kim <namhyung@kernel.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <inttypes.h>
|
2024-05-07 00:04:06 -03:00
|
|
|
#include <linux/zalloc.h>
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
|
2024-01-16 22:26:51 -08:00
|
|
|
#include "annotate.h"
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
#include "annotate-data.h"
|
|
|
|
#include "debuginfo.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "dso.h"
|
2024-01-16 22:26:53 -08:00
|
|
|
#include "dwarf-regs.h"
|
2023-12-12 16:13:17 -08:00
|
|
|
#include "evsel.h"
|
|
|
|
#include "evlist.h"
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
#include "map.h"
|
|
|
|
#include "map_symbol.h"
|
2024-04-10 20:32:52 -07:00
|
|
|
#include "sort.h"
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
#include "strbuf.h"
|
|
|
|
#include "symbol.h"
|
2023-12-12 16:13:20 -08:00
|
|
|
#include "symbol_conf.h"
|
2024-03-18 22:51:03 -07:00
|
|
|
#include "thread.h"
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
|
2024-04-12 11:33:10 -07:00
|
|
|
/* register number of the stack pointer */
|
|
|
|
#define X86_REG_SP 7
|
|
|
|
|
2024-05-01 23:00:07 -07:00
|
|
|
static void delete_var_types(struct die_var_type *var_types);
|
|
|
|
|
2024-03-18 22:51:00 -07:00
|
|
|
#define pr_debug_dtp(fmt, ...) \
|
|
|
|
do { \
|
|
|
|
if (debug_type_profile) \
|
|
|
|
pr_info(fmt, ##__VA_ARGS__); \
|
|
|
|
else \
|
|
|
|
pr_debug3(fmt, ##__VA_ARGS__); \
|
|
|
|
} while (0)
|
|
|
|
|
2024-07-18 14:13:45 +05:30
|
|
|
void pr_debug_type_name(Dwarf_Die *die, enum type_state_kind kind)
|
2024-03-18 22:51:00 -07:00
|
|
|
{
|
|
|
|
struct strbuf sb;
|
|
|
|
char *str;
|
2024-04-12 11:33:07 -07:00
|
|
|
Dwarf_Word size = 0;
|
2024-03-18 22:51:00 -07:00
|
|
|
|
|
|
|
if (!debug_type_profile && verbose < 3)
|
|
|
|
return;
|
|
|
|
|
2024-03-18 22:51:11 -07:00
|
|
|
switch (kind) {
|
|
|
|
case TSR_KIND_INVALID:
|
|
|
|
pr_info("\n");
|
|
|
|
return;
|
|
|
|
case TSR_KIND_PERCPU_BASE:
|
|
|
|
pr_info(" percpu base\n");
|
|
|
|
return;
|
perf annotate-data: Handle ADD instructions
There are different patterns for percpu variable access using a constant
value added to the base.
2aeb: mov -0x7da0f7e0(,%rax,8),%r14 # r14 = __per_cpu_offset[cpu]
2af3: mov $0x34740,%rax # rax = address of runqueues
* 2afa: add %rax,%r14 # r14 = &per_cpu(runqueues, cpu)
2bfd: cmpl $0x0,0x10(%r14) # cpu_rq(cpu)->has_blocked_load
2b03: je 0x2b36
At the first instruction, r14 has the __per_cpu_offset. And then rax
has an immediate value and then added to r14 to calculate the address of
a per-cpu variable. So it needs to track the immediate values and ADD
instructions.
Similar but a little different case is to use "this_cpu_off" instead of
"__per_cpu_offset" for the current CPU. This time the variable address
comes with PC-rel addressing.
89: mov $0x34740,%rax # rax = address of runqueues
* 90: add %gs:0x7f015f60(%rip),%rax # 19a78 <this_cpu_off>
98: incl 0xd8c(%rax) # cpu_rq(cpu)->sched_count
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-21-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:12 -07:00
|
|
|
case TSR_KIND_CONST:
|
|
|
|
pr_info(" constant\n");
|
|
|
|
return;
|
|
|
|
case TSR_KIND_POINTER:
|
|
|
|
pr_info(" pointer");
|
|
|
|
/* it also prints the type info */
|
|
|
|
break;
|
2024-03-18 22:51:13 -07:00
|
|
|
case TSR_KIND_CANARY:
|
|
|
|
pr_info(" stack canary\n");
|
|
|
|
return;
|
2024-03-18 22:51:11 -07:00
|
|
|
case TSR_KIND_TYPE:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-04-12 11:33:07 -07:00
|
|
|
dwarf_aggregate_size(die, &size);
|
|
|
|
|
2024-03-18 22:51:00 -07:00
|
|
|
strbuf_init(&sb, 32);
|
|
|
|
die_get_typename_from_type(die, &sb);
|
|
|
|
str = strbuf_detach(&sb, NULL);
|
2024-04-12 11:33:07 -07:00
|
|
|
pr_info(" type='%s' size=%#lx (die:%#lx)\n",
|
|
|
|
str, (long)size, (long)dwarf_dieoffset(die));
|
2024-03-18 22:51:00 -07:00
|
|
|
free(str);
|
|
|
|
}
|
|
|
|
|
2024-04-12 11:33:07 -07:00
|
|
|
static void pr_debug_location(Dwarf_Die *die, u64 pc, int reg)
|
|
|
|
{
|
|
|
|
ptrdiff_t off = 0;
|
|
|
|
Dwarf_Attribute attr;
|
|
|
|
Dwarf_Addr base, start, end;
|
|
|
|
Dwarf_Op *ops;
|
|
|
|
size_t nops;
|
|
|
|
|
|
|
|
if (!debug_type_profile && verbose < 3)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (dwarf_attr(die, DW_AT_location, &attr) == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while ((off = dwarf_getlocations(&attr, off, &base, &start, &end, &ops, &nops)) > 0) {
|
2024-08-16 16:58:32 -07:00
|
|
|
if (reg != DWARF_REG_PC && end <= pc)
|
2024-04-12 11:33:07 -07:00
|
|
|
continue;
|
|
|
|
if (reg != DWARF_REG_PC && start > pc)
|
|
|
|
break;
|
|
|
|
|
|
|
|
pr_info(" variable location: ");
|
|
|
|
switch (ops->atom) {
|
|
|
|
case DW_OP_reg0 ...DW_OP_reg31:
|
|
|
|
pr_info("reg%d\n", ops->atom - DW_OP_reg0);
|
|
|
|
break;
|
|
|
|
case DW_OP_breg0 ...DW_OP_breg31:
|
|
|
|
pr_info("base=reg%d, offset=%#lx\n",
|
|
|
|
ops->atom - DW_OP_breg0, (long)ops->number);
|
|
|
|
break;
|
|
|
|
case DW_OP_regx:
|
|
|
|
pr_info("reg%ld\n", (long)ops->number);
|
|
|
|
break;
|
|
|
|
case DW_OP_bregx:
|
|
|
|
pr_info("base=reg%ld, offset=%#lx\n",
|
|
|
|
(long)ops->number, (long)ops->number2);
|
|
|
|
break;
|
|
|
|
case DW_OP_fbreg:
|
|
|
|
pr_info("use frame base, offset=%#lx\n", (long)ops->number);
|
|
|
|
break;
|
|
|
|
case DW_OP_addr:
|
|
|
|
pr_info("address=%#lx\n", (long)ops->number);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pr_info("unknown: code=%#x, number=%#lx\n",
|
|
|
|
ops->atom, (long)ops->number);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-09 14:42:51 -07:00
|
|
|
static void pr_debug_scope(Dwarf_Die *scope_die)
|
|
|
|
{
|
|
|
|
int tag;
|
|
|
|
|
|
|
|
if (!debug_type_profile && verbose < 3)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pr_info("(die:%lx) ", (long)dwarf_dieoffset(scope_die));
|
|
|
|
|
|
|
|
tag = dwarf_tag(scope_die);
|
|
|
|
if (tag == DW_TAG_subprogram)
|
|
|
|
pr_info("[function] %s\n", dwarf_diename(scope_die));
|
|
|
|
else if (tag == DW_TAG_inlined_subroutine)
|
|
|
|
pr_info("[inlined] %s\n", dwarf_diename(scope_die));
|
|
|
|
else if (tag == DW_TAG_lexical_block)
|
|
|
|
pr_info("[block]\n");
|
|
|
|
else
|
|
|
|
pr_info("[unknown] tag=%x\n", tag);
|
|
|
|
}
|
|
|
|
|
2024-07-18 14:13:44 +05:30
|
|
|
bool has_reg_type(struct type_state *state, int reg)
|
2024-03-18 22:51:01 -07:00
|
|
|
{
|
|
|
|
return (unsigned)reg < ARRAY_SIZE(state->regs);
|
|
|
|
}
|
|
|
|
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
static void init_type_state(struct type_state *state, struct arch *arch)
|
2024-03-18 22:51:01 -07:00
|
|
|
{
|
|
|
|
memset(state, 0, sizeof(*state));
|
|
|
|
INIT_LIST_HEAD(&state->stack_vars);
|
2024-03-18 22:51:05 -07:00
|
|
|
|
|
|
|
if (arch__is(arch, "x86")) {
|
|
|
|
state->regs[0].caller_saved = true;
|
|
|
|
state->regs[1].caller_saved = true;
|
|
|
|
state->regs[2].caller_saved = true;
|
|
|
|
state->regs[4].caller_saved = true;
|
|
|
|
state->regs[5].caller_saved = true;
|
|
|
|
state->regs[8].caller_saved = true;
|
|
|
|
state->regs[9].caller_saved = true;
|
|
|
|
state->regs[10].caller_saved = true;
|
|
|
|
state->regs[11].caller_saved = true;
|
|
|
|
state->ret_reg = 0;
|
2024-04-12 11:33:10 -07:00
|
|
|
state->stack_reg = X86_REG_SP;
|
2024-03-18 22:51:05 -07:00
|
|
|
}
|
2024-03-18 22:51:01 -07:00
|
|
|
}
|
|
|
|
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
static void exit_type_state(struct type_state *state)
|
2024-03-18 22:51:01 -07:00
|
|
|
{
|
|
|
|
struct type_state_stack *stack, *tmp;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(stack, tmp, &state->stack_vars, list) {
|
|
|
|
list_del(&stack->list);
|
|
|
|
free(stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
perf annotate-data: Add dso->data_types tree
To aggregate accesses to the same data type, add 'data_types' tree in
DSO to maintain data types and find it by name and size.
It might have different data types that happen to have the same name,
so it also compares the size of the type.
Even if it doesn't 100% guarantee, it reduces the possibility of
mis-handling of such conflicts.
And I don't think it's common to have different types with the same
name.
Committer notes:
Very few cases on the Linux kernel, but there are some different types
with the same name, unsure if there is a debug mode in libbpf dedup that
warns about such cases, but there are provisions in pahole for that,
see:
"emit: Notice type shadowing, i.e. multiple types with the same name (enum, struct, union, etc)"
https://git.kernel.org/pub/scm/devel/pahole/pahole.git/commit/?id=4f332dbfd02072e4f410db7bdcda8d6e3422974b
$ pahole --compile > vmlinux.h
$ rm -f a ; make a
cc a.c -o a
$ grep __[0-9] vmlinux.h
union irte__1 {
struct map_info__1;
struct map_info__1 {
struct map_info__1 * next; /* 0 8 */
$
drivers/iommu/amd/amd_iommu_types.h 'union irte'
include/linux/dmar.h 'struct irte'
include/linux/device-mapper.h:
union map_info {
void *ptr;
};
include/linux/mtd/map.h:
struct map_info {
const char *name;
unsigned long size;
resource_size_t phys;
<SNIP>
kernel/events/uprobes.c:
struct map_info {
struct map_info *next;
struct mm_struct *mm;
unsigned long vaddr;
};
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-5-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:10 -08:00
|
|
|
/*
|
|
|
|
* Compare type name and size to maintain them in a tree.
|
|
|
|
* I'm not sure if DWARF would have information of a single type in many
|
|
|
|
* different places (compilation units). If not, it could compare the
|
|
|
|
* offset of the type entry in the .debug_info section.
|
|
|
|
*/
|
|
|
|
static int data_type_cmp(const void *_key, const struct rb_node *node)
|
|
|
|
{
|
|
|
|
const struct annotated_data_type *key = _key;
|
|
|
|
struct annotated_data_type *type;
|
|
|
|
|
|
|
|
type = rb_entry(node, struct annotated_data_type, node);
|
|
|
|
|
2023-12-12 16:13:16 -08:00
|
|
|
if (key->self.size != type->self.size)
|
|
|
|
return key->self.size - type->self.size;
|
|
|
|
return strcmp(key->self.type_name, type->self.type_name);
|
perf annotate-data: Add dso->data_types tree
To aggregate accesses to the same data type, add 'data_types' tree in
DSO to maintain data types and find it by name and size.
It might have different data types that happen to have the same name,
so it also compares the size of the type.
Even if it doesn't 100% guarantee, it reduces the possibility of
mis-handling of such conflicts.
And I don't think it's common to have different types with the same
name.
Committer notes:
Very few cases on the Linux kernel, but there are some different types
with the same name, unsure if there is a debug mode in libbpf dedup that
warns about such cases, but there are provisions in pahole for that,
see:
"emit: Notice type shadowing, i.e. multiple types with the same name (enum, struct, union, etc)"
https://git.kernel.org/pub/scm/devel/pahole/pahole.git/commit/?id=4f332dbfd02072e4f410db7bdcda8d6e3422974b
$ pahole --compile > vmlinux.h
$ rm -f a ; make a
cc a.c -o a
$ grep __[0-9] vmlinux.h
union irte__1 {
struct map_info__1;
struct map_info__1 {
struct map_info__1 * next; /* 0 8 */
$
drivers/iommu/amd/amd_iommu_types.h 'union irte'
include/linux/dmar.h 'struct irte'
include/linux/device-mapper.h:
union map_info {
void *ptr;
};
include/linux/mtd/map.h:
struct map_info {
const char *name;
unsigned long size;
resource_size_t phys;
<SNIP>
kernel/events/uprobes.c:
struct map_info {
struct map_info *next;
struct mm_struct *mm;
unsigned long vaddr;
};
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-5-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool data_type_less(struct rb_node *node_a, const struct rb_node *node_b)
|
|
|
|
{
|
|
|
|
struct annotated_data_type *a, *b;
|
|
|
|
|
|
|
|
a = rb_entry(node_a, struct annotated_data_type, node);
|
|
|
|
b = rb_entry(node_b, struct annotated_data_type, node);
|
|
|
|
|
2023-12-12 16:13:16 -08:00
|
|
|
if (a->self.size != b->self.size)
|
|
|
|
return a->self.size < b->self.size;
|
|
|
|
return strcmp(a->self.type_name, b->self.type_name) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recursively add new members for struct/union */
|
|
|
|
static int __add_member_cb(Dwarf_Die *die, void *arg)
|
|
|
|
{
|
|
|
|
struct annotated_member *parent = arg;
|
|
|
|
struct annotated_member *member;
|
|
|
|
Dwarf_Die member_type, die_mem;
|
perf annotate-data: Set bitfield member offset and size properly
The bitfield members might not have DW_AT_data_member_location. Let's
use DW_AT_data_bit_offset to set the member offset correct. Also use
DW_AT_bit_size for the name like in a C program.
Before:
Annotate type: 'struct sk_buff' (1 samples)
Percent Offset Size Field
- 100.00 0 232 struct sk_buff {
+ 0.00 0 24 union ;
+ 0.00 24 8 union ;
+ 0.00 32 8 union ;
0.00 40 48 char[] cb;
+ 0.00 88 16 union ;
0.00 104 8 long unsigned int _nfct;
100.00 112 4 unsigned int len;
0.00 116 4 unsigned int data_len;
0.00 120 2 __u16 mac_len;
0.00 122 2 __u16 hdr_len;
0.00 124 2 __u16 queue_mapping;
0.00 126 0 __u8[] __cloned_offset;
0.00 0 1 __u8 cloned;
0.00 0 1 __u8 nohdr;
0.00 0 1 __u8 fclone;
0.00 0 1 __u8 peeked;
0.00 0 1 __u8 head_frag;
0.00 0 1 __u8 pfmemalloc;
0.00 0 1 __u8 pp_recycle;
0.00 127 1 __u8 active_extensions;
+ 0.00 128 60 union ;
0.00 188 4 sk_buff_data_t tail;
0.00 192 4 sk_buff_data_t end;
0.00 200 8 unsigned char* head;
After:
Annotate type: 'struct sk_buff' (1 samples)
Percent Offset Size Field
- 100.00 0 232 struct sk_buff {
+ 0.00 0 24 union ;
+ 0.00 24 8 union ;
+ 0.00 32 8 union ;
0.00 40 48 char[] cb
+ 0.00 88 16 union ;
0.00 104 8 long unsigned int _nfct;
100.00 112 4 unsigned int len;
0.00 116 4 unsigned int data_len;
0.00 120 2 __u16 mac_len;
0.00 122 2 __u16 hdr_len;
0.00 124 2 __u16 queue_mapping;
0.00 126 0 __u8[] __cloned_offset;
0.00 126 1 __u8 cloned:1;
0.00 126 1 __u8 nohdr:1;
0.00 126 1 __u8 fclone:2;
0.00 126 1 __u8 peeked:1;
0.00 126 1 __u8 head_frag:1;
0.00 126 1 __u8 pfmemalloc:1;
0.00 126 1 __u8 pp_recycle:1;
0.00 127 1 __u8 active_extensions;
+ 0.00 128 60 union ;
0.00 188 4 sk_buff_data_t tail;
0.00 192 4 sk_buff_data_t end;
0.00 200 8 unsigned char* head;
Commiter notes:
Collect some data:
root@number:~# perf mem record -a --ldlat 5 -- ping -s 8193 -f 192.168.86.1
Memory events are enabled on a subset of CPUs: 16-27
PING 192.168.86.1 (192.168.86.1) 8193(8221) bytes of data.
.^C
--- 192.168.86.1 ping statistics ---
13881 packets transmitted, 13880 received, 0.00720409% packet loss, time 8664ms
rtt min/avg/max/mdev = 0.510/0.599/7.768/0.115 ms, ipg/ewma 0.624/0.593 ms
[ perf record: Woken up 8 times to write data ]
[ perf record: Captured and wrote 14.877 MB perf.data (46785 samples) ]
root@number:~#
root@number:~# perf evlist
cpu_atom/mem-loads,ldlat=5/P
cpu_atom/mem-stores/P
dummy:u
root@number:~# perf evlist -v
cpu_atom/mem-loads,ldlat=5/P: type: 10 (cpu_atom), size: 136, config: 0x5d0 (mem-loads), { sample_period, sample_freq }: 4000, sample_type: IP|TID|TIME|ADDR|CPU|PERIOD|IDENTIFIER|DATA_SRC|WEIGHT_STRUCT, read_format: ID|LOST, disabled: 1, inherit: 1, freq: 1, precise_ip: 3, sample_id_all: 1, { bp_addr, config1 }: 0x7
cpu_atom/mem-stores/P: type: 10 (cpu_atom), size: 136, config: 0x6d0 (mem-stores), { sample_period, sample_freq }: 4000, sample_type: IP|TID|TIME|ADDR|CPU|PERIOD|IDENTIFIER|DATA_SRC|WEIGHT_STRUCT, read_format: ID|LOST, disabled: 1, inherit: 1, freq: 1, precise_ip: 3, sample_id_all: 1
dummy:u: type: 1 (software), size: 136, config: 0x9 (PERF_COUNT_SW_DUMMY), { sample_period, sample_freq }: 1, sample_type: IP|TID|TIME|ADDR|CPU|IDENTIFIER|DATA_SRC|WEIGHT_STRUCT, read_format: ID|LOST, inherit: 1, exclude_kernel: 1, exclude_hv: 1, mmap: 1, comm: 1, task: 1, mmap_data: 1, sample_id_all: 1, exclude_guest: 1, mmap2: 1, comm_exec: 1, ksymbol: 1, bpf_event: 1
root@number:~#
Ok, now lets see what changes from before this patch to after it:
root@number:~# perf annotate --data-type > /tmp/before
Apply the patch, build:
root@number:~# perf annotate --data-type > /tmp/after
The first hunk of the diff, for a glib data structure, in userspace,
look at those bitfields:
root@number:~# diff -u10 /tmp/before /tmp/after | head -20
--- /tmp/before 2024-08-20 17:29:58.306765780 -0300
+++ /tmp/after 2024-08-20 17:33:13.210582596 -0300
@@ -163,22 +163,22 @@
Annotate type: 'GHashTable' in /usr/lib64/libglib-2.0.so.0.8000.3 (1 samples):
============================================================================
Percent offset size field
100.00 0 96 GHashTable {
0.00 0 8 gsize size;
0.00 8 4 gint mod;
100.00 12 4 guint mask;
0.00 16 4 guint nnodes;
0.00 20 4 guint noccupied;
- 0.00 0 4 guint have_big_keys;
- 0.00 0 4 guint have_big_values;
+ 0.00 24 1 guint have_big_keys:1;
+ 0.00 24 1 guint have_big_values:1;
0.00 32 8 gpointer keys;
0.00 40 8 guint* hashes;
0.00 48 8 gpointer values;
root@number:~#
As advertised :-)
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240815223823.2402285-1-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-15 15:38:23 -07:00
|
|
|
Dwarf_Word size, loc, bit_size = 0;
|
2023-12-12 16:13:16 -08:00
|
|
|
Dwarf_Attribute attr;
|
|
|
|
struct strbuf sb;
|
|
|
|
int tag;
|
|
|
|
|
|
|
|
if (dwarf_tag(die) != DW_TAG_member)
|
|
|
|
return DIE_FIND_CB_SIBLING;
|
|
|
|
|
|
|
|
member = zalloc(sizeof(*member));
|
|
|
|
if (member == NULL)
|
|
|
|
return DIE_FIND_CB_END;
|
|
|
|
|
|
|
|
strbuf_init(&sb, 32);
|
|
|
|
die_get_typename(die, &sb);
|
|
|
|
|
2024-08-07 15:31:29 -07:00
|
|
|
__die_get_real_type(die, &member_type);
|
|
|
|
if (dwarf_tag(&member_type) == DW_TAG_typedef)
|
|
|
|
die_get_real_type(&member_type, &die_mem);
|
|
|
|
else
|
|
|
|
die_mem = member_type;
|
|
|
|
|
|
|
|
if (dwarf_aggregate_size(&die_mem, &size) < 0)
|
2023-12-12 16:13:16 -08:00
|
|
|
size = 0;
|
|
|
|
|
perf annotate-data: Set bitfield member offset and size properly
The bitfield members might not have DW_AT_data_member_location. Let's
use DW_AT_data_bit_offset to set the member offset correct. Also use
DW_AT_bit_size for the name like in a C program.
Before:
Annotate type: 'struct sk_buff' (1 samples)
Percent Offset Size Field
- 100.00 0 232 struct sk_buff {
+ 0.00 0 24 union ;
+ 0.00 24 8 union ;
+ 0.00 32 8 union ;
0.00 40 48 char[] cb;
+ 0.00 88 16 union ;
0.00 104 8 long unsigned int _nfct;
100.00 112 4 unsigned int len;
0.00 116 4 unsigned int data_len;
0.00 120 2 __u16 mac_len;
0.00 122 2 __u16 hdr_len;
0.00 124 2 __u16 queue_mapping;
0.00 126 0 __u8[] __cloned_offset;
0.00 0 1 __u8 cloned;
0.00 0 1 __u8 nohdr;
0.00 0 1 __u8 fclone;
0.00 0 1 __u8 peeked;
0.00 0 1 __u8 head_frag;
0.00 0 1 __u8 pfmemalloc;
0.00 0 1 __u8 pp_recycle;
0.00 127 1 __u8 active_extensions;
+ 0.00 128 60 union ;
0.00 188 4 sk_buff_data_t tail;
0.00 192 4 sk_buff_data_t end;
0.00 200 8 unsigned char* head;
After:
Annotate type: 'struct sk_buff' (1 samples)
Percent Offset Size Field
- 100.00 0 232 struct sk_buff {
+ 0.00 0 24 union ;
+ 0.00 24 8 union ;
+ 0.00 32 8 union ;
0.00 40 48 char[] cb
+ 0.00 88 16 union ;
0.00 104 8 long unsigned int _nfct;
100.00 112 4 unsigned int len;
0.00 116 4 unsigned int data_len;
0.00 120 2 __u16 mac_len;
0.00 122 2 __u16 hdr_len;
0.00 124 2 __u16 queue_mapping;
0.00 126 0 __u8[] __cloned_offset;
0.00 126 1 __u8 cloned:1;
0.00 126 1 __u8 nohdr:1;
0.00 126 1 __u8 fclone:2;
0.00 126 1 __u8 peeked:1;
0.00 126 1 __u8 head_frag:1;
0.00 126 1 __u8 pfmemalloc:1;
0.00 126 1 __u8 pp_recycle:1;
0.00 127 1 __u8 active_extensions;
+ 0.00 128 60 union ;
0.00 188 4 sk_buff_data_t tail;
0.00 192 4 sk_buff_data_t end;
0.00 200 8 unsigned char* head;
Commiter notes:
Collect some data:
root@number:~# perf mem record -a --ldlat 5 -- ping -s 8193 -f 192.168.86.1
Memory events are enabled on a subset of CPUs: 16-27
PING 192.168.86.1 (192.168.86.1) 8193(8221) bytes of data.
.^C
--- 192.168.86.1 ping statistics ---
13881 packets transmitted, 13880 received, 0.00720409% packet loss, time 8664ms
rtt min/avg/max/mdev = 0.510/0.599/7.768/0.115 ms, ipg/ewma 0.624/0.593 ms
[ perf record: Woken up 8 times to write data ]
[ perf record: Captured and wrote 14.877 MB perf.data (46785 samples) ]
root@number:~#
root@number:~# perf evlist
cpu_atom/mem-loads,ldlat=5/P
cpu_atom/mem-stores/P
dummy:u
root@number:~# perf evlist -v
cpu_atom/mem-loads,ldlat=5/P: type: 10 (cpu_atom), size: 136, config: 0x5d0 (mem-loads), { sample_period, sample_freq }: 4000, sample_type: IP|TID|TIME|ADDR|CPU|PERIOD|IDENTIFIER|DATA_SRC|WEIGHT_STRUCT, read_format: ID|LOST, disabled: 1, inherit: 1, freq: 1, precise_ip: 3, sample_id_all: 1, { bp_addr, config1 }: 0x7
cpu_atom/mem-stores/P: type: 10 (cpu_atom), size: 136, config: 0x6d0 (mem-stores), { sample_period, sample_freq }: 4000, sample_type: IP|TID|TIME|ADDR|CPU|PERIOD|IDENTIFIER|DATA_SRC|WEIGHT_STRUCT, read_format: ID|LOST, disabled: 1, inherit: 1, freq: 1, precise_ip: 3, sample_id_all: 1
dummy:u: type: 1 (software), size: 136, config: 0x9 (PERF_COUNT_SW_DUMMY), { sample_period, sample_freq }: 1, sample_type: IP|TID|TIME|ADDR|CPU|IDENTIFIER|DATA_SRC|WEIGHT_STRUCT, read_format: ID|LOST, inherit: 1, exclude_kernel: 1, exclude_hv: 1, mmap: 1, comm: 1, task: 1, mmap_data: 1, sample_id_all: 1, exclude_guest: 1, mmap2: 1, comm_exec: 1, ksymbol: 1, bpf_event: 1
root@number:~#
Ok, now lets see what changes from before this patch to after it:
root@number:~# perf annotate --data-type > /tmp/before
Apply the patch, build:
root@number:~# perf annotate --data-type > /tmp/after
The first hunk of the diff, for a glib data structure, in userspace,
look at those bitfields:
root@number:~# diff -u10 /tmp/before /tmp/after | head -20
--- /tmp/before 2024-08-20 17:29:58.306765780 -0300
+++ /tmp/after 2024-08-20 17:33:13.210582596 -0300
@@ -163,22 +163,22 @@
Annotate type: 'GHashTable' in /usr/lib64/libglib-2.0.so.0.8000.3 (1 samples):
============================================================================
Percent offset size field
100.00 0 96 GHashTable {
0.00 0 8 gsize size;
0.00 8 4 gint mod;
100.00 12 4 guint mask;
0.00 16 4 guint nnodes;
0.00 20 4 guint noccupied;
- 0.00 0 4 guint have_big_keys;
- 0.00 0 4 guint have_big_values;
+ 0.00 24 1 guint have_big_keys:1;
+ 0.00 24 1 guint have_big_values:1;
0.00 32 8 gpointer keys;
0.00 40 8 guint* hashes;
0.00 48 8 gpointer values;
root@number:~#
As advertised :-)
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240815223823.2402285-1-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-15 15:38:23 -07:00
|
|
|
if (dwarf_attr_integrate(die, DW_AT_data_member_location, &attr))
|
2023-12-12 16:13:16 -08:00
|
|
|
dwarf_formudata(&attr, &loc);
|
perf annotate-data: Set bitfield member offset and size properly
The bitfield members might not have DW_AT_data_member_location. Let's
use DW_AT_data_bit_offset to set the member offset correct. Also use
DW_AT_bit_size for the name like in a C program.
Before:
Annotate type: 'struct sk_buff' (1 samples)
Percent Offset Size Field
- 100.00 0 232 struct sk_buff {
+ 0.00 0 24 union ;
+ 0.00 24 8 union ;
+ 0.00 32 8 union ;
0.00 40 48 char[] cb;
+ 0.00 88 16 union ;
0.00 104 8 long unsigned int _nfct;
100.00 112 4 unsigned int len;
0.00 116 4 unsigned int data_len;
0.00 120 2 __u16 mac_len;
0.00 122 2 __u16 hdr_len;
0.00 124 2 __u16 queue_mapping;
0.00 126 0 __u8[] __cloned_offset;
0.00 0 1 __u8 cloned;
0.00 0 1 __u8 nohdr;
0.00 0 1 __u8 fclone;
0.00 0 1 __u8 peeked;
0.00 0 1 __u8 head_frag;
0.00 0 1 __u8 pfmemalloc;
0.00 0 1 __u8 pp_recycle;
0.00 127 1 __u8 active_extensions;
+ 0.00 128 60 union ;
0.00 188 4 sk_buff_data_t tail;
0.00 192 4 sk_buff_data_t end;
0.00 200 8 unsigned char* head;
After:
Annotate type: 'struct sk_buff' (1 samples)
Percent Offset Size Field
- 100.00 0 232 struct sk_buff {
+ 0.00 0 24 union ;
+ 0.00 24 8 union ;
+ 0.00 32 8 union ;
0.00 40 48 char[] cb
+ 0.00 88 16 union ;
0.00 104 8 long unsigned int _nfct;
100.00 112 4 unsigned int len;
0.00 116 4 unsigned int data_len;
0.00 120 2 __u16 mac_len;
0.00 122 2 __u16 hdr_len;
0.00 124 2 __u16 queue_mapping;
0.00 126 0 __u8[] __cloned_offset;
0.00 126 1 __u8 cloned:1;
0.00 126 1 __u8 nohdr:1;
0.00 126 1 __u8 fclone:2;
0.00 126 1 __u8 peeked:1;
0.00 126 1 __u8 head_frag:1;
0.00 126 1 __u8 pfmemalloc:1;
0.00 126 1 __u8 pp_recycle:1;
0.00 127 1 __u8 active_extensions;
+ 0.00 128 60 union ;
0.00 188 4 sk_buff_data_t tail;
0.00 192 4 sk_buff_data_t end;
0.00 200 8 unsigned char* head;
Commiter notes:
Collect some data:
root@number:~# perf mem record -a --ldlat 5 -- ping -s 8193 -f 192.168.86.1
Memory events are enabled on a subset of CPUs: 16-27
PING 192.168.86.1 (192.168.86.1) 8193(8221) bytes of data.
.^C
--- 192.168.86.1 ping statistics ---
13881 packets transmitted, 13880 received, 0.00720409% packet loss, time 8664ms
rtt min/avg/max/mdev = 0.510/0.599/7.768/0.115 ms, ipg/ewma 0.624/0.593 ms
[ perf record: Woken up 8 times to write data ]
[ perf record: Captured and wrote 14.877 MB perf.data (46785 samples) ]
root@number:~#
root@number:~# perf evlist
cpu_atom/mem-loads,ldlat=5/P
cpu_atom/mem-stores/P
dummy:u
root@number:~# perf evlist -v
cpu_atom/mem-loads,ldlat=5/P: type: 10 (cpu_atom), size: 136, config: 0x5d0 (mem-loads), { sample_period, sample_freq }: 4000, sample_type: IP|TID|TIME|ADDR|CPU|PERIOD|IDENTIFIER|DATA_SRC|WEIGHT_STRUCT, read_format: ID|LOST, disabled: 1, inherit: 1, freq: 1, precise_ip: 3, sample_id_all: 1, { bp_addr, config1 }: 0x7
cpu_atom/mem-stores/P: type: 10 (cpu_atom), size: 136, config: 0x6d0 (mem-stores), { sample_period, sample_freq }: 4000, sample_type: IP|TID|TIME|ADDR|CPU|PERIOD|IDENTIFIER|DATA_SRC|WEIGHT_STRUCT, read_format: ID|LOST, disabled: 1, inherit: 1, freq: 1, precise_ip: 3, sample_id_all: 1
dummy:u: type: 1 (software), size: 136, config: 0x9 (PERF_COUNT_SW_DUMMY), { sample_period, sample_freq }: 1, sample_type: IP|TID|TIME|ADDR|CPU|IDENTIFIER|DATA_SRC|WEIGHT_STRUCT, read_format: ID|LOST, inherit: 1, exclude_kernel: 1, exclude_hv: 1, mmap: 1, comm: 1, task: 1, mmap_data: 1, sample_id_all: 1, exclude_guest: 1, mmap2: 1, comm_exec: 1, ksymbol: 1, bpf_event: 1
root@number:~#
Ok, now lets see what changes from before this patch to after it:
root@number:~# perf annotate --data-type > /tmp/before
Apply the patch, build:
root@number:~# perf annotate --data-type > /tmp/after
The first hunk of the diff, for a glib data structure, in userspace,
look at those bitfields:
root@number:~# diff -u10 /tmp/before /tmp/after | head -20
--- /tmp/before 2024-08-20 17:29:58.306765780 -0300
+++ /tmp/after 2024-08-20 17:33:13.210582596 -0300
@@ -163,22 +163,22 @@
Annotate type: 'GHashTable' in /usr/lib64/libglib-2.0.so.0.8000.3 (1 samples):
============================================================================
Percent offset size field
100.00 0 96 GHashTable {
0.00 0 8 gsize size;
0.00 8 4 gint mod;
100.00 12 4 guint mask;
0.00 16 4 guint nnodes;
0.00 20 4 guint noccupied;
- 0.00 0 4 guint have_big_keys;
- 0.00 0 4 guint have_big_values;
+ 0.00 24 1 guint have_big_keys:1;
+ 0.00 24 1 guint have_big_values:1;
0.00 32 8 gpointer keys;
0.00 40 8 guint* hashes;
0.00 48 8 gpointer values;
root@number:~#
As advertised :-)
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240815223823.2402285-1-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-15 15:38:23 -07:00
|
|
|
else {
|
|
|
|
/* bitfield member */
|
|
|
|
if (dwarf_attr_integrate(die, DW_AT_data_bit_offset, &attr) &&
|
|
|
|
dwarf_formudata(&attr, &loc) == 0)
|
|
|
|
loc /= 8;
|
|
|
|
else
|
|
|
|
loc = 0;
|
|
|
|
|
|
|
|
if (dwarf_attr_integrate(die, DW_AT_bit_size, &attr) &&
|
|
|
|
dwarf_formudata(&attr, &bit_size) == 0)
|
|
|
|
size = (bit_size + 7) / 8;
|
|
|
|
}
|
2023-12-12 16:13:16 -08:00
|
|
|
|
|
|
|
member->type_name = strbuf_detach(&sb, NULL);
|
|
|
|
/* member->var_name can be NULL */
|
perf annotate-data: Set bitfield member offset and size properly
The bitfield members might not have DW_AT_data_member_location. Let's
use DW_AT_data_bit_offset to set the member offset correct. Also use
DW_AT_bit_size for the name like in a C program.
Before:
Annotate type: 'struct sk_buff' (1 samples)
Percent Offset Size Field
- 100.00 0 232 struct sk_buff {
+ 0.00 0 24 union ;
+ 0.00 24 8 union ;
+ 0.00 32 8 union ;
0.00 40 48 char[] cb;
+ 0.00 88 16 union ;
0.00 104 8 long unsigned int _nfct;
100.00 112 4 unsigned int len;
0.00 116 4 unsigned int data_len;
0.00 120 2 __u16 mac_len;
0.00 122 2 __u16 hdr_len;
0.00 124 2 __u16 queue_mapping;
0.00 126 0 __u8[] __cloned_offset;
0.00 0 1 __u8 cloned;
0.00 0 1 __u8 nohdr;
0.00 0 1 __u8 fclone;
0.00 0 1 __u8 peeked;
0.00 0 1 __u8 head_frag;
0.00 0 1 __u8 pfmemalloc;
0.00 0 1 __u8 pp_recycle;
0.00 127 1 __u8 active_extensions;
+ 0.00 128 60 union ;
0.00 188 4 sk_buff_data_t tail;
0.00 192 4 sk_buff_data_t end;
0.00 200 8 unsigned char* head;
After:
Annotate type: 'struct sk_buff' (1 samples)
Percent Offset Size Field
- 100.00 0 232 struct sk_buff {
+ 0.00 0 24 union ;
+ 0.00 24 8 union ;
+ 0.00 32 8 union ;
0.00 40 48 char[] cb
+ 0.00 88 16 union ;
0.00 104 8 long unsigned int _nfct;
100.00 112 4 unsigned int len;
0.00 116 4 unsigned int data_len;
0.00 120 2 __u16 mac_len;
0.00 122 2 __u16 hdr_len;
0.00 124 2 __u16 queue_mapping;
0.00 126 0 __u8[] __cloned_offset;
0.00 126 1 __u8 cloned:1;
0.00 126 1 __u8 nohdr:1;
0.00 126 1 __u8 fclone:2;
0.00 126 1 __u8 peeked:1;
0.00 126 1 __u8 head_frag:1;
0.00 126 1 __u8 pfmemalloc:1;
0.00 126 1 __u8 pp_recycle:1;
0.00 127 1 __u8 active_extensions;
+ 0.00 128 60 union ;
0.00 188 4 sk_buff_data_t tail;
0.00 192 4 sk_buff_data_t end;
0.00 200 8 unsigned char* head;
Commiter notes:
Collect some data:
root@number:~# perf mem record -a --ldlat 5 -- ping -s 8193 -f 192.168.86.1
Memory events are enabled on a subset of CPUs: 16-27
PING 192.168.86.1 (192.168.86.1) 8193(8221) bytes of data.
.^C
--- 192.168.86.1 ping statistics ---
13881 packets transmitted, 13880 received, 0.00720409% packet loss, time 8664ms
rtt min/avg/max/mdev = 0.510/0.599/7.768/0.115 ms, ipg/ewma 0.624/0.593 ms
[ perf record: Woken up 8 times to write data ]
[ perf record: Captured and wrote 14.877 MB perf.data (46785 samples) ]
root@number:~#
root@number:~# perf evlist
cpu_atom/mem-loads,ldlat=5/P
cpu_atom/mem-stores/P
dummy:u
root@number:~# perf evlist -v
cpu_atom/mem-loads,ldlat=5/P: type: 10 (cpu_atom), size: 136, config: 0x5d0 (mem-loads), { sample_period, sample_freq }: 4000, sample_type: IP|TID|TIME|ADDR|CPU|PERIOD|IDENTIFIER|DATA_SRC|WEIGHT_STRUCT, read_format: ID|LOST, disabled: 1, inherit: 1, freq: 1, precise_ip: 3, sample_id_all: 1, { bp_addr, config1 }: 0x7
cpu_atom/mem-stores/P: type: 10 (cpu_atom), size: 136, config: 0x6d0 (mem-stores), { sample_period, sample_freq }: 4000, sample_type: IP|TID|TIME|ADDR|CPU|PERIOD|IDENTIFIER|DATA_SRC|WEIGHT_STRUCT, read_format: ID|LOST, disabled: 1, inherit: 1, freq: 1, precise_ip: 3, sample_id_all: 1
dummy:u: type: 1 (software), size: 136, config: 0x9 (PERF_COUNT_SW_DUMMY), { sample_period, sample_freq }: 1, sample_type: IP|TID|TIME|ADDR|CPU|IDENTIFIER|DATA_SRC|WEIGHT_STRUCT, read_format: ID|LOST, inherit: 1, exclude_kernel: 1, exclude_hv: 1, mmap: 1, comm: 1, task: 1, mmap_data: 1, sample_id_all: 1, exclude_guest: 1, mmap2: 1, comm_exec: 1, ksymbol: 1, bpf_event: 1
root@number:~#
Ok, now lets see what changes from before this patch to after it:
root@number:~# perf annotate --data-type > /tmp/before
Apply the patch, build:
root@number:~# perf annotate --data-type > /tmp/after
The first hunk of the diff, for a glib data structure, in userspace,
look at those bitfields:
root@number:~# diff -u10 /tmp/before /tmp/after | head -20
--- /tmp/before 2024-08-20 17:29:58.306765780 -0300
+++ /tmp/after 2024-08-20 17:33:13.210582596 -0300
@@ -163,22 +163,22 @@
Annotate type: 'GHashTable' in /usr/lib64/libglib-2.0.so.0.8000.3 (1 samples):
============================================================================
Percent offset size field
100.00 0 96 GHashTable {
0.00 0 8 gsize size;
0.00 8 4 gint mod;
100.00 12 4 guint mask;
0.00 16 4 guint nnodes;
0.00 20 4 guint noccupied;
- 0.00 0 4 guint have_big_keys;
- 0.00 0 4 guint have_big_values;
+ 0.00 24 1 guint have_big_keys:1;
+ 0.00 24 1 guint have_big_values:1;
0.00 32 8 gpointer keys;
0.00 40 8 guint* hashes;
0.00 48 8 gpointer values;
root@number:~#
As advertised :-)
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240815223823.2402285-1-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-15 15:38:23 -07:00
|
|
|
if (dwarf_diename(die)) {
|
|
|
|
if (bit_size) {
|
|
|
|
if (asprintf(&member->var_name, "%s:%ld",
|
|
|
|
dwarf_diename(die), (long)bit_size) < 0)
|
|
|
|
member->var_name = NULL;
|
|
|
|
} else {
|
|
|
|
member->var_name = strdup(dwarf_diename(die));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (member->var_name == NULL) {
|
|
|
|
free(member);
|
|
|
|
return DIE_FIND_CB_END;
|
|
|
|
}
|
|
|
|
}
|
2023-12-12 16:13:16 -08:00
|
|
|
member->size = size;
|
|
|
|
member->offset = loc + parent->offset;
|
|
|
|
INIT_LIST_HEAD(&member->children);
|
|
|
|
list_add_tail(&member->node, &parent->children);
|
|
|
|
|
2024-08-07 15:31:29 -07:00
|
|
|
tag = dwarf_tag(&die_mem);
|
2023-12-12 16:13:16 -08:00
|
|
|
switch (tag) {
|
|
|
|
case DW_TAG_structure_type:
|
|
|
|
case DW_TAG_union_type:
|
2024-08-07 15:31:29 -07:00
|
|
|
die_find_child(&die_mem, __add_member_cb, member, &die_mem);
|
2023-12-12 16:13:16 -08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return DIE_FIND_CB_SIBLING;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_member_types(struct annotated_data_type *parent, Dwarf_Die *type)
|
|
|
|
{
|
|
|
|
Dwarf_Die die_mem;
|
|
|
|
|
|
|
|
die_find_child(type, __add_member_cb, &parent->self, &die_mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void delete_members(struct annotated_member *member)
|
|
|
|
{
|
|
|
|
struct annotated_member *child, *tmp;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(child, tmp, &member->children, node) {
|
|
|
|
list_del(&child->node);
|
|
|
|
delete_members(child);
|
2024-05-07 00:04:06 -03:00
|
|
|
zfree(&child->type_name);
|
|
|
|
zfree(&child->var_name);
|
2023-12-12 16:13:16 -08:00
|
|
|
free(child);
|
|
|
|
}
|
perf annotate-data: Add dso->data_types tree
To aggregate accesses to the same data type, add 'data_types' tree in
DSO to maintain data types and find it by name and size.
It might have different data types that happen to have the same name,
so it also compares the size of the type.
Even if it doesn't 100% guarantee, it reduces the possibility of
mis-handling of such conflicts.
And I don't think it's common to have different types with the same
name.
Committer notes:
Very few cases on the Linux kernel, but there are some different types
with the same name, unsure if there is a debug mode in libbpf dedup that
warns about such cases, but there are provisions in pahole for that,
see:
"emit: Notice type shadowing, i.e. multiple types with the same name (enum, struct, union, etc)"
https://git.kernel.org/pub/scm/devel/pahole/pahole.git/commit/?id=4f332dbfd02072e4f410db7bdcda8d6e3422974b
$ pahole --compile > vmlinux.h
$ rm -f a ; make a
cc a.c -o a
$ grep __[0-9] vmlinux.h
union irte__1 {
struct map_info__1;
struct map_info__1 {
struct map_info__1 * next; /* 0 8 */
$
drivers/iommu/amd/amd_iommu_types.h 'union irte'
include/linux/dmar.h 'struct irte'
include/linux/device-mapper.h:
union map_info {
void *ptr;
};
include/linux/mtd/map.h:
struct map_info {
const char *name;
unsigned long size;
resource_size_t phys;
<SNIP>
kernel/events/uprobes.c:
struct map_info {
struct map_info *next;
struct mm_struct *mm;
unsigned long vaddr;
};
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-5-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct annotated_data_type *dso__findnew_data_type(struct dso *dso,
|
|
|
|
Dwarf_Die *type_die)
|
|
|
|
{
|
|
|
|
struct annotated_data_type *result = NULL;
|
|
|
|
struct annotated_data_type key;
|
|
|
|
struct rb_node *node;
|
|
|
|
struct strbuf sb;
|
|
|
|
char *type_name;
|
|
|
|
Dwarf_Word size;
|
|
|
|
|
|
|
|
strbuf_init(&sb, 32);
|
|
|
|
if (die_get_typename_from_type(type_die, &sb) < 0)
|
|
|
|
strbuf_add(&sb, "(unknown type)", 14);
|
|
|
|
type_name = strbuf_detach(&sb, NULL);
|
2024-08-07 15:31:29 -07:00
|
|
|
|
|
|
|
if (dwarf_tag(type_die) == DW_TAG_typedef)
|
|
|
|
die_get_real_type(type_die, type_die);
|
|
|
|
|
perf annotate-data: Add dso->data_types tree
To aggregate accesses to the same data type, add 'data_types' tree in
DSO to maintain data types and find it by name and size.
It might have different data types that happen to have the same name,
so it also compares the size of the type.
Even if it doesn't 100% guarantee, it reduces the possibility of
mis-handling of such conflicts.
And I don't think it's common to have different types with the same
name.
Committer notes:
Very few cases on the Linux kernel, but there are some different types
with the same name, unsure if there is a debug mode in libbpf dedup that
warns about such cases, but there are provisions in pahole for that,
see:
"emit: Notice type shadowing, i.e. multiple types with the same name (enum, struct, union, etc)"
https://git.kernel.org/pub/scm/devel/pahole/pahole.git/commit/?id=4f332dbfd02072e4f410db7bdcda8d6e3422974b
$ pahole --compile > vmlinux.h
$ rm -f a ; make a
cc a.c -o a
$ grep __[0-9] vmlinux.h
union irte__1 {
struct map_info__1;
struct map_info__1 {
struct map_info__1 * next; /* 0 8 */
$
drivers/iommu/amd/amd_iommu_types.h 'union irte'
include/linux/dmar.h 'struct irte'
include/linux/device-mapper.h:
union map_info {
void *ptr;
};
include/linux/mtd/map.h:
struct map_info {
const char *name;
unsigned long size;
resource_size_t phys;
<SNIP>
kernel/events/uprobes.c:
struct map_info {
struct map_info *next;
struct mm_struct *mm;
unsigned long vaddr;
};
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-5-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:10 -08:00
|
|
|
dwarf_aggregate_size(type_die, &size);
|
|
|
|
|
|
|
|
/* Check existing nodes in dso->data_types tree */
|
2023-12-12 16:13:16 -08:00
|
|
|
key.self.type_name = type_name;
|
|
|
|
key.self.size = size;
|
2024-05-04 14:38:01 -07:00
|
|
|
node = rb_find(&key, dso__data_types(dso), data_type_cmp);
|
perf annotate-data: Add dso->data_types tree
To aggregate accesses to the same data type, add 'data_types' tree in
DSO to maintain data types and find it by name and size.
It might have different data types that happen to have the same name,
so it also compares the size of the type.
Even if it doesn't 100% guarantee, it reduces the possibility of
mis-handling of such conflicts.
And I don't think it's common to have different types with the same
name.
Committer notes:
Very few cases on the Linux kernel, but there are some different types
with the same name, unsure if there is a debug mode in libbpf dedup that
warns about such cases, but there are provisions in pahole for that,
see:
"emit: Notice type shadowing, i.e. multiple types with the same name (enum, struct, union, etc)"
https://git.kernel.org/pub/scm/devel/pahole/pahole.git/commit/?id=4f332dbfd02072e4f410db7bdcda8d6e3422974b
$ pahole --compile > vmlinux.h
$ rm -f a ; make a
cc a.c -o a
$ grep __[0-9] vmlinux.h
union irte__1 {
struct map_info__1;
struct map_info__1 {
struct map_info__1 * next; /* 0 8 */
$
drivers/iommu/amd/amd_iommu_types.h 'union irte'
include/linux/dmar.h 'struct irte'
include/linux/device-mapper.h:
union map_info {
void *ptr;
};
include/linux/mtd/map.h:
struct map_info {
const char *name;
unsigned long size;
resource_size_t phys;
<SNIP>
kernel/events/uprobes.c:
struct map_info {
struct map_info *next;
struct mm_struct *mm;
unsigned long vaddr;
};
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-5-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:10 -08:00
|
|
|
if (node) {
|
|
|
|
result = rb_entry(node, struct annotated_data_type, node);
|
|
|
|
free(type_name);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If not, add a new one */
|
|
|
|
result = zalloc(sizeof(*result));
|
|
|
|
if (result == NULL) {
|
|
|
|
free(type_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-12-12 16:13:16 -08:00
|
|
|
result->self.type_name = type_name;
|
|
|
|
result->self.size = size;
|
|
|
|
INIT_LIST_HEAD(&result->self.children);
|
|
|
|
|
2023-12-12 16:13:20 -08:00
|
|
|
if (symbol_conf.annotate_data_member)
|
|
|
|
add_member_types(result, type_die);
|
perf annotate-data: Add dso->data_types tree
To aggregate accesses to the same data type, add 'data_types' tree in
DSO to maintain data types and find it by name and size.
It might have different data types that happen to have the same name,
so it also compares the size of the type.
Even if it doesn't 100% guarantee, it reduces the possibility of
mis-handling of such conflicts.
And I don't think it's common to have different types with the same
name.
Committer notes:
Very few cases on the Linux kernel, but there are some different types
with the same name, unsure if there is a debug mode in libbpf dedup that
warns about such cases, but there are provisions in pahole for that,
see:
"emit: Notice type shadowing, i.e. multiple types with the same name (enum, struct, union, etc)"
https://git.kernel.org/pub/scm/devel/pahole/pahole.git/commit/?id=4f332dbfd02072e4f410db7bdcda8d6e3422974b
$ pahole --compile > vmlinux.h
$ rm -f a ; make a
cc a.c -o a
$ grep __[0-9] vmlinux.h
union irte__1 {
struct map_info__1;
struct map_info__1 {
struct map_info__1 * next; /* 0 8 */
$
drivers/iommu/amd/amd_iommu_types.h 'union irte'
include/linux/dmar.h 'struct irte'
include/linux/device-mapper.h:
union map_info {
void *ptr;
};
include/linux/mtd/map.h:
struct map_info {
const char *name;
unsigned long size;
resource_size_t phys;
<SNIP>
kernel/events/uprobes.c:
struct map_info {
struct map_info *next;
struct mm_struct *mm;
unsigned long vaddr;
};
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-5-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:10 -08:00
|
|
|
|
2024-05-04 14:38:01 -07:00
|
|
|
rb_add(&result->node, dso__data_types(dso), data_type_less);
|
perf annotate-data: Add dso->data_types tree
To aggregate accesses to the same data type, add 'data_types' tree in
DSO to maintain data types and find it by name and size.
It might have different data types that happen to have the same name,
so it also compares the size of the type.
Even if it doesn't 100% guarantee, it reduces the possibility of
mis-handling of such conflicts.
And I don't think it's common to have different types with the same
name.
Committer notes:
Very few cases on the Linux kernel, but there are some different types
with the same name, unsure if there is a debug mode in libbpf dedup that
warns about such cases, but there are provisions in pahole for that,
see:
"emit: Notice type shadowing, i.e. multiple types with the same name (enum, struct, union, etc)"
https://git.kernel.org/pub/scm/devel/pahole/pahole.git/commit/?id=4f332dbfd02072e4f410db7bdcda8d6e3422974b
$ pahole --compile > vmlinux.h
$ rm -f a ; make a
cc a.c -o a
$ grep __[0-9] vmlinux.h
union irte__1 {
struct map_info__1;
struct map_info__1 {
struct map_info__1 * next; /* 0 8 */
$
drivers/iommu/amd/amd_iommu_types.h 'union irte'
include/linux/dmar.h 'struct irte'
include/linux/device-mapper.h:
union map_info {
void *ptr;
};
include/linux/mtd/map.h:
struct map_info {
const char *name;
unsigned long size;
resource_size_t phys;
<SNIP>
kernel/events/uprobes.c:
struct map_info {
struct map_info *next;
struct mm_struct *mm;
unsigned long vaddr;
};
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-5-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:10 -08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
static bool find_cu_die(struct debuginfo *di, u64 pc, Dwarf_Die *cu_die)
|
|
|
|
{
|
|
|
|
Dwarf_Off off, next_off;
|
|
|
|
size_t header_size;
|
|
|
|
|
|
|
|
if (dwarf_addrdie(di->dbg, pc, cu_die) != NULL)
|
|
|
|
return cu_die;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There are some kernels don't have full aranges and contain only a few
|
|
|
|
* aranges entries. Fallback to iterate all CU entries in .debug_info
|
|
|
|
* in case it's missing.
|
|
|
|
*/
|
|
|
|
off = 0;
|
|
|
|
while (dwarf_nextcu(di->dbg, off, &next_off, &header_size,
|
|
|
|
NULL, NULL, NULL) == 0) {
|
|
|
|
if (dwarf_offdie(di->dbg, off + header_size, cu_die) &&
|
|
|
|
dwarf_haspc(cu_die, pc))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
off = next_off;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-08-16 16:58:33 -07:00
|
|
|
enum type_match_result {
|
|
|
|
PERF_TMR_UNKNOWN = 0,
|
|
|
|
PERF_TMR_OK,
|
|
|
|
PERF_TMR_NO_TYPE,
|
|
|
|
PERF_TMR_NO_POINTER,
|
|
|
|
PERF_TMR_NO_SIZE,
|
|
|
|
PERF_TMR_BAD_OFFSET,
|
2024-08-16 16:58:35 -07:00
|
|
|
PERF_TMR_BAIL_OUT,
|
2024-08-16 16:58:33 -07:00
|
|
|
};
|
|
|
|
|
2024-08-16 16:58:34 -07:00
|
|
|
static const char *match_result_str(enum type_match_result tmr)
|
|
|
|
{
|
|
|
|
switch (tmr) {
|
|
|
|
case PERF_TMR_OK:
|
|
|
|
return "Good!";
|
|
|
|
case PERF_TMR_NO_TYPE:
|
|
|
|
return "no type information";
|
|
|
|
case PERF_TMR_NO_POINTER:
|
|
|
|
return "no/void pointer";
|
|
|
|
case PERF_TMR_NO_SIZE:
|
|
|
|
return "type size is unknown";
|
|
|
|
case PERF_TMR_BAD_OFFSET:
|
|
|
|
return "offset bigger than size";
|
|
|
|
case PERF_TMR_UNKNOWN:
|
2024-08-16 16:58:35 -07:00
|
|
|
case PERF_TMR_BAIL_OUT:
|
2024-08-16 16:58:34 -07:00
|
|
|
default:
|
|
|
|
return "invalid state";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 16:58:36 -07:00
|
|
|
static bool is_pointer_type(Dwarf_Die *type_die)
|
|
|
|
{
|
|
|
|
int tag = dwarf_tag(type_die);
|
|
|
|
|
|
|
|
return tag == DW_TAG_pointer_type || tag == DW_TAG_array_type;
|
|
|
|
}
|
|
|
|
|
2024-08-20 23:54:07 -07:00
|
|
|
static bool is_compound_type(Dwarf_Die *type_die)
|
|
|
|
{
|
|
|
|
int tag = dwarf_tag(type_die);
|
|
|
|
|
|
|
|
return tag == DW_TAG_structure_type || tag == DW_TAG_union_type;
|
|
|
|
}
|
|
|
|
|
2024-08-16 16:58:37 -07:00
|
|
|
/* returns if Type B has better information than Type A */
|
|
|
|
static bool is_better_type(Dwarf_Die *type_a, Dwarf_Die *type_b)
|
|
|
|
{
|
|
|
|
Dwarf_Word size_a, size_b;
|
|
|
|
Dwarf_Die die_a, die_b;
|
|
|
|
|
|
|
|
/* pointer type is preferred */
|
|
|
|
if (is_pointer_type(type_a) != is_pointer_type(type_b))
|
|
|
|
return is_pointer_type(type_b);
|
|
|
|
|
|
|
|
if (is_pointer_type(type_b)) {
|
|
|
|
/*
|
|
|
|
* We want to compare the target type, but 'void *' can fail to
|
|
|
|
* get the target type.
|
|
|
|
*/
|
|
|
|
if (die_get_real_type(type_a, &die_a) == NULL)
|
|
|
|
return true;
|
|
|
|
if (die_get_real_type(type_b, &die_b) == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
type_a = &die_a;
|
|
|
|
type_b = &die_b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bigger type is preferred */
|
|
|
|
if (dwarf_aggregate_size(type_a, &size_a) < 0 ||
|
|
|
|
dwarf_aggregate_size(type_b, &size_b) < 0)
|
|
|
|
return false;
|
|
|
|
|
2024-08-20 23:54:07 -07:00
|
|
|
if (size_a != size_b)
|
|
|
|
return size_a < size_b;
|
|
|
|
|
|
|
|
/* struct or union is preferred */
|
|
|
|
if (is_compound_type(type_a) != is_compound_type(type_b))
|
|
|
|
return is_compound_type(type_b);
|
|
|
|
|
|
|
|
/* typedef is preferred */
|
|
|
|
if (dwarf_tag(type_b) == DW_TAG_typedef)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2024-08-16 16:58:37 -07:00
|
|
|
}
|
|
|
|
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
/* The type info will be saved in @type_die */
|
2024-08-16 16:58:33 -07:00
|
|
|
static enum type_match_result check_variable(struct data_loc_info *dloc,
|
|
|
|
Dwarf_Die *var_die,
|
|
|
|
Dwarf_Die *type_die, int reg,
|
|
|
|
int offset, bool is_fbreg)
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
{
|
|
|
|
Dwarf_Word size;
|
2024-08-16 16:58:36 -07:00
|
|
|
bool needs_pointer = true;
|
2024-08-07 15:31:29 -07:00
|
|
|
Dwarf_Die sized_type;
|
2024-04-12 11:33:10 -07:00
|
|
|
|
|
|
|
if (reg == DWARF_REG_PC)
|
2024-08-16 16:58:36 -07:00
|
|
|
needs_pointer = false;
|
2024-04-12 11:33:10 -07:00
|
|
|
else if (reg == dloc->fbreg || is_fbreg)
|
2024-08-16 16:58:36 -07:00
|
|
|
needs_pointer = false;
|
2024-04-12 11:33:10 -07:00
|
|
|
else if (arch__is(dloc->arch, "x86") && reg == X86_REG_SP)
|
2024-08-16 16:58:36 -07:00
|
|
|
needs_pointer = false;
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
|
|
|
|
/* Get the type of the variable */
|
2024-08-16 16:58:39 -07:00
|
|
|
if (__die_get_real_type(var_die, type_die) == NULL)
|
2024-08-16 16:58:33 -07:00
|
|
|
return PERF_TMR_NO_TYPE;
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
|
|
|
|
/*
|
2024-01-16 22:26:53 -08:00
|
|
|
* Usually it expects a pointer type for a memory access.
|
|
|
|
* Convert to a real type it points to. But global variables
|
2024-01-16 22:26:56 -08:00
|
|
|
* and local variables are accessed directly without a pointer.
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
*/
|
2024-08-16 16:58:36 -07:00
|
|
|
if (needs_pointer) {
|
|
|
|
if (!is_pointer_type(type_die) ||
|
2024-08-16 16:58:39 -07:00
|
|
|
__die_get_real_type(type_die, type_die) == NULL)
|
2024-08-16 16:58:33 -07:00
|
|
|
return PERF_TMR_NO_POINTER;
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
}
|
|
|
|
|
2024-08-07 15:31:29 -07:00
|
|
|
if (dwarf_tag(type_die) == DW_TAG_typedef)
|
|
|
|
die_get_real_type(type_die, &sized_type);
|
|
|
|
else
|
|
|
|
sized_type = *type_die;
|
|
|
|
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
/* Get the size of the actual type */
|
2024-08-16 16:58:39 -07:00
|
|
|
if (dwarf_aggregate_size(&sized_type, &size) < 0)
|
2024-08-16 16:58:33 -07:00
|
|
|
return PERF_TMR_NO_SIZE;
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
|
|
|
|
/* Minimal sanity check */
|
2024-08-16 16:58:39 -07:00
|
|
|
if ((unsigned)offset >= size)
|
2024-08-16 16:58:33 -07:00
|
|
|
return PERF_TMR_BAD_OFFSET;
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
|
2024-08-16 16:58:33 -07:00
|
|
|
return PERF_TMR_OK;
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
}
|
|
|
|
|
2024-07-18 14:13:45 +05:30
|
|
|
struct type_state_stack *find_stack_state(struct type_state *state,
|
2024-03-18 22:51:01 -07:00
|
|
|
int offset)
|
|
|
|
{
|
|
|
|
struct type_state_stack *stack;
|
|
|
|
|
|
|
|
list_for_each_entry(stack, &state->stack_vars, list) {
|
|
|
|
if (offset == stack->offset)
|
|
|
|
return stack;
|
|
|
|
|
|
|
|
if (stack->compound && stack->offset < offset &&
|
|
|
|
offset < stack->offset + stack->size)
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-07-18 14:13:45 +05:30
|
|
|
void set_stack_state(struct type_state_stack *stack, int offset, u8 kind,
|
2024-03-18 22:51:01 -07:00
|
|
|
Dwarf_Die *type_die)
|
|
|
|
{
|
|
|
|
int tag;
|
|
|
|
Dwarf_Word size;
|
|
|
|
|
|
|
|
if (dwarf_aggregate_size(type_die, &size) < 0)
|
|
|
|
size = 0;
|
|
|
|
|
|
|
|
tag = dwarf_tag(type_die);
|
|
|
|
|
|
|
|
stack->type = *type_die;
|
|
|
|
stack->size = size;
|
|
|
|
stack->offset = offset;
|
2024-03-18 22:51:11 -07:00
|
|
|
stack->kind = kind;
|
2024-03-18 22:51:01 -07:00
|
|
|
|
|
|
|
switch (tag) {
|
|
|
|
case DW_TAG_structure_type:
|
|
|
|
case DW_TAG_union_type:
|
perf annotate-data: Handle ADD instructions
There are different patterns for percpu variable access using a constant
value added to the base.
2aeb: mov -0x7da0f7e0(,%rax,8),%r14 # r14 = __per_cpu_offset[cpu]
2af3: mov $0x34740,%rax # rax = address of runqueues
* 2afa: add %rax,%r14 # r14 = &per_cpu(runqueues, cpu)
2bfd: cmpl $0x0,0x10(%r14) # cpu_rq(cpu)->has_blocked_load
2b03: je 0x2b36
At the first instruction, r14 has the __per_cpu_offset. And then rax
has an immediate value and then added to r14 to calculate the address of
a per-cpu variable. So it needs to track the immediate values and ADD
instructions.
Similar but a little different case is to use "this_cpu_off" instead of
"__per_cpu_offset" for the current CPU. This time the variable address
comes with PC-rel addressing.
89: mov $0x34740,%rax # rax = address of runqueues
* 90: add %gs:0x7f015f60(%rip),%rax # 19a78 <this_cpu_off>
98: incl 0xd8c(%rax) # cpu_rq(cpu)->sched_count
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-21-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:12 -07:00
|
|
|
stack->compound = (kind != TSR_KIND_POINTER);
|
2024-03-18 22:51:01 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
stack->compound = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 14:13:45 +05:30
|
|
|
struct type_state_stack *findnew_stack_state(struct type_state *state,
|
2024-03-18 22:51:11 -07:00
|
|
|
int offset, u8 kind,
|
|
|
|
Dwarf_Die *type_die)
|
2024-03-18 22:51:01 -07:00
|
|
|
{
|
|
|
|
struct type_state_stack *stack = find_stack_state(state, offset);
|
|
|
|
|
|
|
|
if (stack) {
|
2024-03-18 22:51:11 -07:00
|
|
|
set_stack_state(stack, offset, kind, type_die);
|
2024-03-18 22:51:01 -07:00
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
stack = malloc(sizeof(*stack));
|
|
|
|
if (stack) {
|
2024-03-18 22:51:11 -07:00
|
|
|
set_stack_state(stack, offset, kind, type_die);
|
2024-03-18 22:51:01 -07:00
|
|
|
list_add(&stack->list, &state->stack_vars);
|
|
|
|
}
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
2024-03-18 22:51:14 -07:00
|
|
|
/* Maintain a cache for quick global variable lookup */
|
|
|
|
struct global_var_entry {
|
|
|
|
struct rb_node node;
|
|
|
|
char *name;
|
|
|
|
u64 start;
|
|
|
|
u64 end;
|
|
|
|
u64 die_offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int global_var_cmp(const void *_key, const struct rb_node *node)
|
|
|
|
{
|
|
|
|
const u64 addr = (uintptr_t)_key;
|
|
|
|
struct global_var_entry *gvar;
|
|
|
|
|
|
|
|
gvar = rb_entry(node, struct global_var_entry, node);
|
|
|
|
|
|
|
|
if (gvar->start <= addr && addr < gvar->end)
|
|
|
|
return 0;
|
|
|
|
return gvar->start > addr ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool global_var_less(struct rb_node *node_a, const struct rb_node *node_b)
|
|
|
|
{
|
|
|
|
struct global_var_entry *gvar_a, *gvar_b;
|
|
|
|
|
|
|
|
gvar_a = rb_entry(node_a, struct global_var_entry, node);
|
|
|
|
gvar_b = rb_entry(node_b, struct global_var_entry, node);
|
|
|
|
|
|
|
|
return gvar_a->start < gvar_b->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct global_var_entry *global_var__find(struct data_loc_info *dloc, u64 addr)
|
|
|
|
{
|
|
|
|
struct dso *dso = map__dso(dloc->ms->map);
|
|
|
|
struct rb_node *node;
|
|
|
|
|
2024-05-04 14:38:01 -07:00
|
|
|
node = rb_find((void *)(uintptr_t)addr, dso__global_vars(dso), global_var_cmp);
|
2024-03-18 22:51:14 -07:00
|
|
|
if (node == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return rb_entry(node, struct global_var_entry, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool global_var__add(struct data_loc_info *dloc, u64 addr,
|
|
|
|
const char *name, Dwarf_Die *type_die)
|
|
|
|
{
|
|
|
|
struct dso *dso = map__dso(dloc->ms->map);
|
|
|
|
struct global_var_entry *gvar;
|
|
|
|
Dwarf_Word size;
|
|
|
|
|
|
|
|
if (dwarf_aggregate_size(type_die, &size) < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
gvar = malloc(sizeof(*gvar));
|
|
|
|
if (gvar == NULL)
|
|
|
|
return false;
|
|
|
|
|
2024-05-01 23:00:07 -07:00
|
|
|
gvar->name = name ? strdup(name) : NULL;
|
|
|
|
if (name && gvar->name == NULL) {
|
2024-03-18 22:51:14 -07:00
|
|
|
free(gvar);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
gvar->start = addr;
|
|
|
|
gvar->end = addr + size;
|
|
|
|
gvar->die_offset = dwarf_dieoffset(type_die);
|
|
|
|
|
2024-05-04 14:38:01 -07:00
|
|
|
rb_add(&gvar->node, dso__global_vars(dso), global_var_less);
|
2024-03-18 22:51:14 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void global_var_type__tree_delete(struct rb_root *root)
|
|
|
|
{
|
|
|
|
struct global_var_entry *gvar;
|
|
|
|
|
|
|
|
while (!RB_EMPTY_ROOT(root)) {
|
|
|
|
struct rb_node *node = rb_first(root);
|
|
|
|
|
|
|
|
rb_erase(node, root);
|
|
|
|
gvar = rb_entry(node, struct global_var_entry, node);
|
2024-05-07 00:04:06 -03:00
|
|
|
zfree(&gvar->name);
|
2024-03-18 22:51:14 -07:00
|
|
|
free(gvar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 14:13:45 +05:30
|
|
|
bool get_global_var_info(struct data_loc_info *dloc, u64 addr,
|
2024-03-18 22:51:11 -07:00
|
|
|
const char **var_name, int *var_offset)
|
|
|
|
{
|
|
|
|
struct addr_location al;
|
|
|
|
struct symbol *sym;
|
|
|
|
u64 mem_addr;
|
|
|
|
|
|
|
|
/* Kernel symbols might be relocated */
|
|
|
|
mem_addr = addr + map__reloc(dloc->ms->map);
|
|
|
|
|
|
|
|
addr_location__init(&al);
|
|
|
|
sym = thread__find_symbol_fb(dloc->thread, dloc->cpumode,
|
|
|
|
mem_addr, &al);
|
|
|
|
if (sym) {
|
|
|
|
*var_name = sym->name;
|
|
|
|
/* Calculate type offset from the start of variable */
|
|
|
|
*var_offset = mem_addr - map__unmap_ip(al.map, sym->start);
|
|
|
|
} else {
|
|
|
|
*var_name = NULL;
|
|
|
|
}
|
|
|
|
addr_location__exit(&al);
|
|
|
|
if (*var_name == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-05-01 23:00:07 -07:00
|
|
|
static void global_var__collect(struct data_loc_info *dloc)
|
|
|
|
{
|
|
|
|
Dwarf *dwarf = dloc->di->dbg;
|
|
|
|
Dwarf_Off off, next_off;
|
|
|
|
Dwarf_Die cu_die, type_die;
|
|
|
|
size_t header_size;
|
|
|
|
|
|
|
|
/* Iterate all CU and collect global variables that have no location in a register. */
|
|
|
|
off = 0;
|
|
|
|
while (dwarf_nextcu(dwarf, off, &next_off, &header_size,
|
|
|
|
NULL, NULL, NULL) == 0) {
|
|
|
|
struct die_var_type *var_types = NULL;
|
|
|
|
struct die_var_type *pos;
|
|
|
|
|
|
|
|
if (dwarf_offdie(dwarf, off + header_size, &cu_die) == NULL) {
|
|
|
|
off = next_off;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
die_collect_global_vars(&cu_die, &var_types);
|
|
|
|
|
|
|
|
for (pos = var_types; pos; pos = pos->next) {
|
|
|
|
const char *var_name = NULL;
|
|
|
|
int var_offset = 0;
|
|
|
|
|
|
|
|
if (pos->reg != -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!dwarf_offdie(dwarf, pos->die_off, &type_die))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!get_global_var_info(dloc, pos->addr, &var_name,
|
|
|
|
&var_offset))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (var_offset != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
global_var__add(dloc, pos->addr, var_name, &type_die);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete_var_types(var_types);
|
|
|
|
|
|
|
|
off = next_off;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 14:13:45 +05:30
|
|
|
bool get_global_var_type(Dwarf_Die *cu_die, struct data_loc_info *dloc,
|
2024-03-18 22:51:03 -07:00
|
|
|
u64 ip, u64 var_addr, int *var_offset,
|
|
|
|
Dwarf_Die *type_die)
|
|
|
|
{
|
2024-03-18 22:51:11 -07:00
|
|
|
u64 pc;
|
2024-03-18 22:51:03 -07:00
|
|
|
int offset;
|
2024-03-18 22:51:14 -07:00
|
|
|
const char *var_name = NULL;
|
|
|
|
struct global_var_entry *gvar;
|
2024-05-01 23:00:07 -07:00
|
|
|
struct dso *dso = map__dso(dloc->ms->map);
|
2024-03-18 22:51:03 -07:00
|
|
|
Dwarf_Die var_die;
|
|
|
|
|
2024-05-04 14:38:01 -07:00
|
|
|
if (RB_EMPTY_ROOT(dso__global_vars(dso)))
|
2024-05-01 23:00:07 -07:00
|
|
|
global_var__collect(dloc);
|
|
|
|
|
2024-03-18 22:51:14 -07:00
|
|
|
gvar = global_var__find(dloc, var_addr);
|
|
|
|
if (gvar) {
|
|
|
|
if (!dwarf_offdie(dloc->di->dbg, gvar->die_offset, type_die))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*var_offset = var_addr - gvar->start;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-03-18 22:51:03 -07:00
|
|
|
/* Try to get the variable by address first */
|
|
|
|
if (die_find_variable_by_addr(cu_die, var_addr, &var_die, &offset) &&
|
2024-04-12 11:33:10 -07:00
|
|
|
check_variable(dloc, &var_die, type_die, DWARF_REG_PC, offset,
|
2024-08-16 16:58:33 -07:00
|
|
|
/*is_fbreg=*/false) == PERF_TMR_OK) {
|
2024-03-18 22:51:14 -07:00
|
|
|
var_name = dwarf_diename(&var_die);
|
2024-03-18 22:51:03 -07:00
|
|
|
*var_offset = offset;
|
2024-03-18 22:51:14 -07:00
|
|
|
goto ok;
|
2024-03-18 22:51:03 -07:00
|
|
|
}
|
|
|
|
|
2024-03-18 22:51:11 -07:00
|
|
|
if (!get_global_var_info(dloc, var_addr, &var_name, var_offset))
|
2024-03-18 22:51:03 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
pc = map__rip_2objdump(dloc->ms->map, ip);
|
|
|
|
|
|
|
|
/* Try to get the name of global variable */
|
|
|
|
if (die_find_variable_at(cu_die, var_name, pc, &var_die) &&
|
2024-04-12 11:33:10 -07:00
|
|
|
check_variable(dloc, &var_die, type_die, DWARF_REG_PC, *var_offset,
|
2024-08-16 16:58:33 -07:00
|
|
|
/*is_fbreg=*/false) == PERF_TMR_OK)
|
2024-03-18 22:51:14 -07:00
|
|
|
goto ok;
|
2024-03-18 22:51:03 -07:00
|
|
|
|
|
|
|
return false;
|
2024-03-18 22:51:14 -07:00
|
|
|
|
|
|
|
ok:
|
|
|
|
/* The address should point to the start of the variable */
|
|
|
|
global_var__add(dloc, var_addr - *var_offset, var_name, type_die);
|
|
|
|
return true;
|
2024-03-18 22:51:03 -07:00
|
|
|
}
|
|
|
|
|
perf annotate-data: Copy back variable types after move
In some cases, compilers don't set the location expression in DWARF
precisely. For instance, it may assign a variable to a register after
copying it from a different register. Then it should use the register
for the new type but still uses the old register. This makes hard to
track the type information properly.
This is an example I found in __tcp_transmit_skb(). The first argument
(sk) of this function is a pointer to sock and there's a variable (tp)
for tcp_sock.
static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
int clone_it, gfp_t gfp_mask, u32 rcv_nxt)
{
...
struct tcp_sock *tp;
BUG_ON(!skb || !tcp_skb_pcount(skb));
tp = tcp_sk(sk);
prior_wstamp = tp->tcp_wstamp_ns;
tp->tcp_wstamp_ns = max(tp->tcp_wstamp_ns, tp->tcp_clock_cache);
...
So it basically calls tcp_sk(sk) to get the tcp_sock pointer from sk.
But it turned out to be the same value because tcp_sock embeds sock as
the first member. The sk is located in reg5 (RDI) and tp is in reg3
(RBX). The offset of tcp_wstamp_ns is 0x748 and tcp_clock_cache is
0x750. So you need to use RBX (reg3) to access the fields in the
tcp_sock. But the code used RDI (reg5) as it has the same value.
$ pahole --hex -C tcp_sock vmlinux | grep -e 748 -e 750
u64 tcp_wstamp_ns; /* 0x748 0x8 */
u64 tcp_clock_cache; /* 0x750 0x8 */
And this is the disassembly of the part of the function.
<__tcp_transmit_skb>:
...
44: mov %rdi, %rbx
47: mov 0x748(%rdi), %rsi
4e: mov 0x750(%rdi), %rax
55: cmp %rax, %rsi
Because compiler put the debug info to RBX, it only knows RDI is a
pointer to sock and accessing those two fields resulted in error
due to offset being beyond the type size.
-----------------------------------------------------------
find data type for 0x748(reg5) at __tcp_transmit_skb+0x63
CU for net/ipv4/tcp_output.c (die:0x817f543)
frame base: cfa=0 fbreg=6
scope: [1/1] (die:81aac3e)
bb: [0 - 30]
var [0] -0x98(stack) type='struct tcp_out_options' size=0x28 (die:0x81af3df)
var [5] reg8 type='unsigned int' size=0x4 (die:0x8180ed6)
var [5] reg2 type='unsigned int' size=0x4 (die:0x8180ed6)
var [5] reg1 type='int' size=0x4 (die:0x818059e)
var [5] reg4 type='struct sk_buff*' size=0x8 (die:0x8181360)
var [5] reg5 type='struct sock*' size=0x8 (die:0x8181a0c) <<<--- the first argument ('sk' at %RDI)
mov [19] reg8 -> -0xa8(stack) type='unsigned int' size=0x4 (die:0x8180ed6)
mov [20] stack canary -> reg0
mov [29] reg0 -> -0x30(stack) stack canary
bb: [36 - 3e]
mov [36] reg4 -> reg15 type='struct sk_buff*' size=0x8 (die:0x8181360)
bb: [44 - 63]
mov [44] reg5 -> reg3 type='struct sock*' size=0x8 (die:0x8181a0c) <<<--- calling tcp_sk()
var [47] reg3 type='struct tcp_sock*' size=0x8 (die:0x819eead) <<<--- new variable ('tp' at %RBX)
var [4e] reg4 type='unsigned long long' size=0x8 (die:0x8180edd)
mov [58] reg4 -> -0xc0(stack) type='unsigned long long' size=0x8 (die:0x8180edd)
chk [63] reg5 offset=0x748 ok=1 kind=1 (struct sock*) : offset bigger than size <<<--- access with old variable
final result: offset bigger than size
While it's a fault in the compiler, we could work around this issue by
using the type of new variable when it's copied directly. So I've added
copied_from field in the register state to track those direct register
to register copies. After that new register gets a new type and the old
register still has the same type, it'll update (copy it back) the type
of the old register.
For example, if we can update type of reg5 at __tcp_transmit_skb+0x47,
we can find the target type of the instruction at 0x63 like below:
-----------------------------------------------------------
find data type for 0x748(reg5) at __tcp_transmit_skb+0x63
...
bb: [44 - 63]
mov [44] reg5 -> reg3 type='struct sock*' size=0x8 (die:0x8181a0c)
var [47] reg3 type='struct tcp_sock*' size=0x8 (die:0x819eead)
var [47] copyback reg5 type='struct tcp_sock*' size=0x8 (die:0x819eead) <<<--- here
mov [47] 0x748(reg5) -> reg4 type='unsigned long long' size=0x8 (die:0x8180edd)
mov [4e] 0x750(reg5) -> reg0 type='unsigned long long' size=0x8 (die:0x8180edd)
mov [58] reg4 -> -0xc0(stack) type='unsigned long long' size=0x8 (die:0x8180edd)
chk [63] reg5 offset=0x748 ok=1 kind=1 (struct tcp_sock*) : Good! <<<--- new type
found by insn track: 0x748(reg5) type-offset=0x748
final result: type='struct tcp_sock' size=0xa98 (die:0x819eeb2)
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240821232628.353177-5-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-21 16:26:28 -07:00
|
|
|
static bool die_is_same(Dwarf_Die *die_a, Dwarf_Die *die_b)
|
|
|
|
{
|
|
|
|
return (die_a->cu == die_b->cu) && (die_a->addr == die_b->addr);
|
|
|
|
}
|
|
|
|
|
2024-03-18 22:51:01 -07:00
|
|
|
/**
|
|
|
|
* update_var_state - Update type state using given variables
|
|
|
|
* @state: type state table
|
|
|
|
* @dloc: data location info
|
|
|
|
* @addr: instruction address to match with variable
|
|
|
|
* @insn_offset: instruction offset (for debug)
|
|
|
|
* @var_types: list of variables with type info
|
|
|
|
*
|
|
|
|
* This function fills the @state table using @var_types info. Each variable
|
|
|
|
* is used only at the given location and updates an entry in the table.
|
|
|
|
*/
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
static void update_var_state(struct type_state *state, struct data_loc_info *dloc,
|
|
|
|
u64 addr, u64 insn_offset, struct die_var_type *var_types)
|
2024-03-18 22:51:01 -07:00
|
|
|
{
|
|
|
|
Dwarf_Die mem_die;
|
|
|
|
struct die_var_type *var;
|
|
|
|
int fbreg = dloc->fbreg;
|
|
|
|
int fb_offset = 0;
|
|
|
|
|
|
|
|
if (dloc->fb_cfa) {
|
|
|
|
if (die_get_cfa(dloc->di->dbg, addr, &fbreg, &fb_offset) < 0)
|
|
|
|
fbreg = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var = var_types; var != NULL; var = var->next) {
|
|
|
|
if (var->addr != addr)
|
|
|
|
continue;
|
|
|
|
/* Get the type DIE using the offset */
|
|
|
|
if (!dwarf_offdie(dloc->di->dbg, var->die_off, &mem_die))
|
|
|
|
continue;
|
|
|
|
|
2024-08-16 16:58:37 -07:00
|
|
|
if (var->reg == DWARF_REG_FB || var->reg == fbreg) {
|
|
|
|
int offset = var->offset;
|
|
|
|
struct type_state_stack *stack;
|
2024-03-18 22:51:01 -07:00
|
|
|
|
2024-08-16 16:58:37 -07:00
|
|
|
if (var->reg != DWARF_REG_FB)
|
|
|
|
offset -= fb_offset;
|
|
|
|
|
|
|
|
stack = find_stack_state(state, offset);
|
|
|
|
if (stack && stack->kind == TSR_KIND_TYPE &&
|
|
|
|
!is_better_type(&stack->type, &mem_die))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
findnew_stack_state(state, offset, TSR_KIND_TYPE,
|
|
|
|
&mem_die);
|
2024-03-18 22:51:01 -07:00
|
|
|
|
2024-03-18 22:51:02 -07:00
|
|
|
pr_debug_dtp("var [%"PRIx64"] -%#x(stack)",
|
2024-08-16 16:58:37 -07:00
|
|
|
insn_offset, -offset);
|
2024-03-18 22:51:11 -07:00
|
|
|
pr_debug_type_name(&mem_die, TSR_KIND_TYPE);
|
2024-03-18 22:51:01 -07:00
|
|
|
} else if (has_reg_type(state, var->reg) && var->offset == 0) {
|
|
|
|
struct type_state_reg *reg;
|
perf annotate-data: Copy back variable types after move
In some cases, compilers don't set the location expression in DWARF
precisely. For instance, it may assign a variable to a register after
copying it from a different register. Then it should use the register
for the new type but still uses the old register. This makes hard to
track the type information properly.
This is an example I found in __tcp_transmit_skb(). The first argument
(sk) of this function is a pointer to sock and there's a variable (tp)
for tcp_sock.
static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
int clone_it, gfp_t gfp_mask, u32 rcv_nxt)
{
...
struct tcp_sock *tp;
BUG_ON(!skb || !tcp_skb_pcount(skb));
tp = tcp_sk(sk);
prior_wstamp = tp->tcp_wstamp_ns;
tp->tcp_wstamp_ns = max(tp->tcp_wstamp_ns, tp->tcp_clock_cache);
...
So it basically calls tcp_sk(sk) to get the tcp_sock pointer from sk.
But it turned out to be the same value because tcp_sock embeds sock as
the first member. The sk is located in reg5 (RDI) and tp is in reg3
(RBX). The offset of tcp_wstamp_ns is 0x748 and tcp_clock_cache is
0x750. So you need to use RBX (reg3) to access the fields in the
tcp_sock. But the code used RDI (reg5) as it has the same value.
$ pahole --hex -C tcp_sock vmlinux | grep -e 748 -e 750
u64 tcp_wstamp_ns; /* 0x748 0x8 */
u64 tcp_clock_cache; /* 0x750 0x8 */
And this is the disassembly of the part of the function.
<__tcp_transmit_skb>:
...
44: mov %rdi, %rbx
47: mov 0x748(%rdi), %rsi
4e: mov 0x750(%rdi), %rax
55: cmp %rax, %rsi
Because compiler put the debug info to RBX, it only knows RDI is a
pointer to sock and accessing those two fields resulted in error
due to offset being beyond the type size.
-----------------------------------------------------------
find data type for 0x748(reg5) at __tcp_transmit_skb+0x63
CU for net/ipv4/tcp_output.c (die:0x817f543)
frame base: cfa=0 fbreg=6
scope: [1/1] (die:81aac3e)
bb: [0 - 30]
var [0] -0x98(stack) type='struct tcp_out_options' size=0x28 (die:0x81af3df)
var [5] reg8 type='unsigned int' size=0x4 (die:0x8180ed6)
var [5] reg2 type='unsigned int' size=0x4 (die:0x8180ed6)
var [5] reg1 type='int' size=0x4 (die:0x818059e)
var [5] reg4 type='struct sk_buff*' size=0x8 (die:0x8181360)
var [5] reg5 type='struct sock*' size=0x8 (die:0x8181a0c) <<<--- the first argument ('sk' at %RDI)
mov [19] reg8 -> -0xa8(stack) type='unsigned int' size=0x4 (die:0x8180ed6)
mov [20] stack canary -> reg0
mov [29] reg0 -> -0x30(stack) stack canary
bb: [36 - 3e]
mov [36] reg4 -> reg15 type='struct sk_buff*' size=0x8 (die:0x8181360)
bb: [44 - 63]
mov [44] reg5 -> reg3 type='struct sock*' size=0x8 (die:0x8181a0c) <<<--- calling tcp_sk()
var [47] reg3 type='struct tcp_sock*' size=0x8 (die:0x819eead) <<<--- new variable ('tp' at %RBX)
var [4e] reg4 type='unsigned long long' size=0x8 (die:0x8180edd)
mov [58] reg4 -> -0xc0(stack) type='unsigned long long' size=0x8 (die:0x8180edd)
chk [63] reg5 offset=0x748 ok=1 kind=1 (struct sock*) : offset bigger than size <<<--- access with old variable
final result: offset bigger than size
While it's a fault in the compiler, we could work around this issue by
using the type of new variable when it's copied directly. So I've added
copied_from field in the register state to track those direct register
to register copies. After that new register gets a new type and the old
register still has the same type, it'll update (copy it back) the type
of the old register.
For example, if we can update type of reg5 at __tcp_transmit_skb+0x47,
we can find the target type of the instruction at 0x63 like below:
-----------------------------------------------------------
find data type for 0x748(reg5) at __tcp_transmit_skb+0x63
...
bb: [44 - 63]
mov [44] reg5 -> reg3 type='struct sock*' size=0x8 (die:0x8181a0c)
var [47] reg3 type='struct tcp_sock*' size=0x8 (die:0x819eead)
var [47] copyback reg5 type='struct tcp_sock*' size=0x8 (die:0x819eead) <<<--- here
mov [47] 0x748(reg5) -> reg4 type='unsigned long long' size=0x8 (die:0x8180edd)
mov [4e] 0x750(reg5) -> reg0 type='unsigned long long' size=0x8 (die:0x8180edd)
mov [58] reg4 -> -0xc0(stack) type='unsigned long long' size=0x8 (die:0x8180edd)
chk [63] reg5 offset=0x748 ok=1 kind=1 (struct tcp_sock*) : Good! <<<--- new type
found by insn track: 0x748(reg5) type-offset=0x748
final result: type='struct tcp_sock' size=0xa98 (die:0x819eeb2)
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240821232628.353177-5-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-21 16:26:28 -07:00
|
|
|
Dwarf_Die orig_type;
|
2024-03-18 22:51:01 -07:00
|
|
|
|
|
|
|
reg = &state->regs[var->reg];
|
2024-08-16 16:58:37 -07:00
|
|
|
|
|
|
|
if (reg->ok && reg->kind == TSR_KIND_TYPE &&
|
|
|
|
!is_better_type(®->type, &mem_die))
|
|
|
|
continue;
|
|
|
|
|
perf annotate-data: Copy back variable types after move
In some cases, compilers don't set the location expression in DWARF
precisely. For instance, it may assign a variable to a register after
copying it from a different register. Then it should use the register
for the new type but still uses the old register. This makes hard to
track the type information properly.
This is an example I found in __tcp_transmit_skb(). The first argument
(sk) of this function is a pointer to sock and there's a variable (tp)
for tcp_sock.
static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
int clone_it, gfp_t gfp_mask, u32 rcv_nxt)
{
...
struct tcp_sock *tp;
BUG_ON(!skb || !tcp_skb_pcount(skb));
tp = tcp_sk(sk);
prior_wstamp = tp->tcp_wstamp_ns;
tp->tcp_wstamp_ns = max(tp->tcp_wstamp_ns, tp->tcp_clock_cache);
...
So it basically calls tcp_sk(sk) to get the tcp_sock pointer from sk.
But it turned out to be the same value because tcp_sock embeds sock as
the first member. The sk is located in reg5 (RDI) and tp is in reg3
(RBX). The offset of tcp_wstamp_ns is 0x748 and tcp_clock_cache is
0x750. So you need to use RBX (reg3) to access the fields in the
tcp_sock. But the code used RDI (reg5) as it has the same value.
$ pahole --hex -C tcp_sock vmlinux | grep -e 748 -e 750
u64 tcp_wstamp_ns; /* 0x748 0x8 */
u64 tcp_clock_cache; /* 0x750 0x8 */
And this is the disassembly of the part of the function.
<__tcp_transmit_skb>:
...
44: mov %rdi, %rbx
47: mov 0x748(%rdi), %rsi
4e: mov 0x750(%rdi), %rax
55: cmp %rax, %rsi
Because compiler put the debug info to RBX, it only knows RDI is a
pointer to sock and accessing those two fields resulted in error
due to offset being beyond the type size.
-----------------------------------------------------------
find data type for 0x748(reg5) at __tcp_transmit_skb+0x63
CU for net/ipv4/tcp_output.c (die:0x817f543)
frame base: cfa=0 fbreg=6
scope: [1/1] (die:81aac3e)
bb: [0 - 30]
var [0] -0x98(stack) type='struct tcp_out_options' size=0x28 (die:0x81af3df)
var [5] reg8 type='unsigned int' size=0x4 (die:0x8180ed6)
var [5] reg2 type='unsigned int' size=0x4 (die:0x8180ed6)
var [5] reg1 type='int' size=0x4 (die:0x818059e)
var [5] reg4 type='struct sk_buff*' size=0x8 (die:0x8181360)
var [5] reg5 type='struct sock*' size=0x8 (die:0x8181a0c) <<<--- the first argument ('sk' at %RDI)
mov [19] reg8 -> -0xa8(stack) type='unsigned int' size=0x4 (die:0x8180ed6)
mov [20] stack canary -> reg0
mov [29] reg0 -> -0x30(stack) stack canary
bb: [36 - 3e]
mov [36] reg4 -> reg15 type='struct sk_buff*' size=0x8 (die:0x8181360)
bb: [44 - 63]
mov [44] reg5 -> reg3 type='struct sock*' size=0x8 (die:0x8181a0c) <<<--- calling tcp_sk()
var [47] reg3 type='struct tcp_sock*' size=0x8 (die:0x819eead) <<<--- new variable ('tp' at %RBX)
var [4e] reg4 type='unsigned long long' size=0x8 (die:0x8180edd)
mov [58] reg4 -> -0xc0(stack) type='unsigned long long' size=0x8 (die:0x8180edd)
chk [63] reg5 offset=0x748 ok=1 kind=1 (struct sock*) : offset bigger than size <<<--- access with old variable
final result: offset bigger than size
While it's a fault in the compiler, we could work around this issue by
using the type of new variable when it's copied directly. So I've added
copied_from field in the register state to track those direct register
to register copies. After that new register gets a new type and the old
register still has the same type, it'll update (copy it back) the type
of the old register.
For example, if we can update type of reg5 at __tcp_transmit_skb+0x47,
we can find the target type of the instruction at 0x63 like below:
-----------------------------------------------------------
find data type for 0x748(reg5) at __tcp_transmit_skb+0x63
...
bb: [44 - 63]
mov [44] reg5 -> reg3 type='struct sock*' size=0x8 (die:0x8181a0c)
var [47] reg3 type='struct tcp_sock*' size=0x8 (die:0x819eead)
var [47] copyback reg5 type='struct tcp_sock*' size=0x8 (die:0x819eead) <<<--- here
mov [47] 0x748(reg5) -> reg4 type='unsigned long long' size=0x8 (die:0x8180edd)
mov [4e] 0x750(reg5) -> reg0 type='unsigned long long' size=0x8 (die:0x8180edd)
mov [58] reg4 -> -0xc0(stack) type='unsigned long long' size=0x8 (die:0x8180edd)
chk [63] reg5 offset=0x748 ok=1 kind=1 (struct tcp_sock*) : Good! <<<--- new type
found by insn track: 0x748(reg5) type-offset=0x748
final result: type='struct tcp_sock' size=0xa98 (die:0x819eeb2)
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240821232628.353177-5-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-21 16:26:28 -07:00
|
|
|
orig_type = reg->type;
|
|
|
|
|
2024-03-18 22:51:01 -07:00
|
|
|
reg->type = mem_die;
|
2024-03-18 22:51:11 -07:00
|
|
|
reg->kind = TSR_KIND_TYPE;
|
2024-03-18 22:51:01 -07:00
|
|
|
reg->ok = true;
|
|
|
|
|
2024-03-18 22:51:02 -07:00
|
|
|
pr_debug_dtp("var [%"PRIx64"] reg%d",
|
2024-03-18 22:51:01 -07:00
|
|
|
insn_offset, var->reg);
|
2024-03-18 22:51:11 -07:00
|
|
|
pr_debug_type_name(&mem_die, TSR_KIND_TYPE);
|
perf annotate-data: Copy back variable types after move
In some cases, compilers don't set the location expression in DWARF
precisely. For instance, it may assign a variable to a register after
copying it from a different register. Then it should use the register
for the new type but still uses the old register. This makes hard to
track the type information properly.
This is an example I found in __tcp_transmit_skb(). The first argument
(sk) of this function is a pointer to sock and there's a variable (tp)
for tcp_sock.
static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
int clone_it, gfp_t gfp_mask, u32 rcv_nxt)
{
...
struct tcp_sock *tp;
BUG_ON(!skb || !tcp_skb_pcount(skb));
tp = tcp_sk(sk);
prior_wstamp = tp->tcp_wstamp_ns;
tp->tcp_wstamp_ns = max(tp->tcp_wstamp_ns, tp->tcp_clock_cache);
...
So it basically calls tcp_sk(sk) to get the tcp_sock pointer from sk.
But it turned out to be the same value because tcp_sock embeds sock as
the first member. The sk is located in reg5 (RDI) and tp is in reg3
(RBX). The offset of tcp_wstamp_ns is 0x748 and tcp_clock_cache is
0x750. So you need to use RBX (reg3) to access the fields in the
tcp_sock. But the code used RDI (reg5) as it has the same value.
$ pahole --hex -C tcp_sock vmlinux | grep -e 748 -e 750
u64 tcp_wstamp_ns; /* 0x748 0x8 */
u64 tcp_clock_cache; /* 0x750 0x8 */
And this is the disassembly of the part of the function.
<__tcp_transmit_skb>:
...
44: mov %rdi, %rbx
47: mov 0x748(%rdi), %rsi
4e: mov 0x750(%rdi), %rax
55: cmp %rax, %rsi
Because compiler put the debug info to RBX, it only knows RDI is a
pointer to sock and accessing those two fields resulted in error
due to offset being beyond the type size.
-----------------------------------------------------------
find data type for 0x748(reg5) at __tcp_transmit_skb+0x63
CU for net/ipv4/tcp_output.c (die:0x817f543)
frame base: cfa=0 fbreg=6
scope: [1/1] (die:81aac3e)
bb: [0 - 30]
var [0] -0x98(stack) type='struct tcp_out_options' size=0x28 (die:0x81af3df)
var [5] reg8 type='unsigned int' size=0x4 (die:0x8180ed6)
var [5] reg2 type='unsigned int' size=0x4 (die:0x8180ed6)
var [5] reg1 type='int' size=0x4 (die:0x818059e)
var [5] reg4 type='struct sk_buff*' size=0x8 (die:0x8181360)
var [5] reg5 type='struct sock*' size=0x8 (die:0x8181a0c) <<<--- the first argument ('sk' at %RDI)
mov [19] reg8 -> -0xa8(stack) type='unsigned int' size=0x4 (die:0x8180ed6)
mov [20] stack canary -> reg0
mov [29] reg0 -> -0x30(stack) stack canary
bb: [36 - 3e]
mov [36] reg4 -> reg15 type='struct sk_buff*' size=0x8 (die:0x8181360)
bb: [44 - 63]
mov [44] reg5 -> reg3 type='struct sock*' size=0x8 (die:0x8181a0c) <<<--- calling tcp_sk()
var [47] reg3 type='struct tcp_sock*' size=0x8 (die:0x819eead) <<<--- new variable ('tp' at %RBX)
var [4e] reg4 type='unsigned long long' size=0x8 (die:0x8180edd)
mov [58] reg4 -> -0xc0(stack) type='unsigned long long' size=0x8 (die:0x8180edd)
chk [63] reg5 offset=0x748 ok=1 kind=1 (struct sock*) : offset bigger than size <<<--- access with old variable
final result: offset bigger than size
While it's a fault in the compiler, we could work around this issue by
using the type of new variable when it's copied directly. So I've added
copied_from field in the register state to track those direct register
to register copies. After that new register gets a new type and the old
register still has the same type, it'll update (copy it back) the type
of the old register.
For example, if we can update type of reg5 at __tcp_transmit_skb+0x47,
we can find the target type of the instruction at 0x63 like below:
-----------------------------------------------------------
find data type for 0x748(reg5) at __tcp_transmit_skb+0x63
...
bb: [44 - 63]
mov [44] reg5 -> reg3 type='struct sock*' size=0x8 (die:0x8181a0c)
var [47] reg3 type='struct tcp_sock*' size=0x8 (die:0x819eead)
var [47] copyback reg5 type='struct tcp_sock*' size=0x8 (die:0x819eead) <<<--- here
mov [47] 0x748(reg5) -> reg4 type='unsigned long long' size=0x8 (die:0x8180edd)
mov [4e] 0x750(reg5) -> reg0 type='unsigned long long' size=0x8 (die:0x8180edd)
mov [58] reg4 -> -0xc0(stack) type='unsigned long long' size=0x8 (die:0x8180edd)
chk [63] reg5 offset=0x748 ok=1 kind=1 (struct tcp_sock*) : Good! <<<--- new type
found by insn track: 0x748(reg5) type-offset=0x748
final result: type='struct tcp_sock' size=0xa98 (die:0x819eeb2)
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240821232628.353177-5-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-21 16:26:28 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If this register is directly copied from another and it gets a
|
|
|
|
* better type, also update the type of the source register. This
|
|
|
|
* is usually the case of container_of() macro with offset of 0.
|
|
|
|
*/
|
|
|
|
if (has_reg_type(state, reg->copied_from)) {
|
|
|
|
struct type_state_reg *copy_reg;
|
|
|
|
|
|
|
|
copy_reg = &state->regs[reg->copied_from];
|
|
|
|
|
|
|
|
/* TODO: check if type is compatible or embedded */
|
|
|
|
if (!copy_reg->ok || (copy_reg->kind != TSR_KIND_TYPE) ||
|
|
|
|
!die_is_same(©_reg->type, &orig_type) ||
|
|
|
|
!is_better_type(©_reg->type, &mem_die))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
copy_reg->type = mem_die;
|
|
|
|
|
|
|
|
pr_debug_dtp("var [%"PRIx64"] copyback reg%d",
|
|
|
|
insn_offset, reg->copied_from);
|
|
|
|
pr_debug_type_name(&mem_die, TSR_KIND_TYPE);
|
|
|
|
}
|
2024-03-18 22:51:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-18 22:51:04 -07:00
|
|
|
/**
|
|
|
|
* update_insn_state - Update type state for an instruction
|
|
|
|
* @state: type state table
|
|
|
|
* @dloc: data location info
|
|
|
|
* @cu_die: compile unit debug entry
|
|
|
|
* @dl: disasm line for the instruction
|
|
|
|
*
|
|
|
|
* This function updates the @state table for the target operand of the
|
|
|
|
* instruction at @dl if it transfers the type like MOV on x86. Since it
|
|
|
|
* tracks the type, it won't care about the values like in arithmetic
|
|
|
|
* instructions like ADD/SUB/MUL/DIV and INC/DEC.
|
|
|
|
*
|
|
|
|
* Note that ops->reg2 is only available when both mem_ref and multi_regs
|
|
|
|
* are true.
|
|
|
|
*/
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
static void update_insn_state(struct type_state *state, struct data_loc_info *dloc,
|
|
|
|
Dwarf_Die *cu_die, struct disasm_line *dl)
|
2024-03-18 22:51:02 -07:00
|
|
|
{
|
2024-07-18 14:13:45 +05:30
|
|
|
if (dloc->arch->update_insn_state)
|
|
|
|
dloc->arch->update_insn_state(state, dloc, cu_die, dl);
|
2024-03-18 22:51:02 -07:00
|
|
|
}
|
|
|
|
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
/*
|
|
|
|
* Prepend this_blocks (from the outer scope) to full_blocks, 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;
|
|
|
|
|
|
|
|
/* Last insn in this_blocks should be same as first insn in full_blocks */
|
|
|
|
if (last_bb->end != first_bb->begin) {
|
|
|
|
pr_debug("prepend basic blocks: mismatched disasm line %"PRIx64" -> %"PRIx64"\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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Point to the insn before the last when adding this block to full_blocks */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-18 22:51:13 -07:00
|
|
|
/* should match to is_stack_canary() in util/annotate.c */
|
|
|
|
static void setup_stack_canary(struct data_loc_info *dloc)
|
|
|
|
{
|
|
|
|
if (arch__is(dloc->arch, "x86")) {
|
|
|
|
dloc->op->segment = INSN_SEG_X86_GS;
|
|
|
|
dloc->op->imm = true;
|
|
|
|
dloc->op->offset = 40;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-18 22:51:15 -07:00
|
|
|
/*
|
|
|
|
* It's at the target address, check if it has a matching type.
|
2024-08-16 16:58:35 -07:00
|
|
|
* It returns PERF_TMR_BAIL_OUT when it looks up per-cpu variables which
|
2024-03-18 22:51:15 -07:00
|
|
|
* are similar to global variables and no additional info is needed.
|
|
|
|
*/
|
2024-08-16 16:58:35 -07:00
|
|
|
static enum type_match_result check_matching_type(struct type_state *state,
|
|
|
|
struct data_loc_info *dloc,
|
|
|
|
Dwarf_Die *cu_die,
|
2024-08-21 16:26:27 -07:00
|
|
|
struct disasm_line *dl,
|
2024-08-16 16:58:35 -07:00
|
|
|
Dwarf_Die *type_die)
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
{
|
2024-03-18 22:51:07 -07:00
|
|
|
Dwarf_Word size;
|
2024-08-21 16:26:27 -07:00
|
|
|
u32 insn_offset = dl->al.offset;
|
2024-05-01 23:00:10 -07:00
|
|
|
int reg = dloc->op->reg1;
|
2024-08-21 16:26:26 -07:00
|
|
|
int offset = dloc->op->offset;
|
|
|
|
const char *offset_sign = "";
|
2024-08-21 16:26:27 -07:00
|
|
|
bool retry = true;
|
2024-03-18 22:51:07 -07:00
|
|
|
|
2024-08-21 16:26:26 -07:00
|
|
|
if (offset < 0) {
|
|
|
|
offset = -offset;
|
|
|
|
offset_sign = "-";
|
|
|
|
}
|
|
|
|
|
2024-08-21 16:26:27 -07:00
|
|
|
again:
|
2024-08-21 16:26:26 -07:00
|
|
|
pr_debug_dtp("chk [%x] reg%d offset=%s%#x ok=%d kind=%d ",
|
|
|
|
insn_offset, reg, offset_sign, offset,
|
2024-03-18 22:51:11 -07:00
|
|
|
state->regs[reg].ok, state->regs[reg].kind);
|
2024-03-18 22:51:07 -07:00
|
|
|
|
2024-08-20 23:54:08 -07:00
|
|
|
if (!state->regs[reg].ok)
|
|
|
|
goto check_non_register;
|
|
|
|
|
|
|
|
if (state->regs[reg].kind == TSR_KIND_TYPE) {
|
2024-08-07 15:31:29 -07:00
|
|
|
Dwarf_Die sized_type;
|
2024-08-21 16:26:26 -07:00
|
|
|
struct strbuf sb;
|
|
|
|
|
|
|
|
strbuf_init(&sb, 32);
|
|
|
|
die_get_typename_from_type(&state->regs[reg].type, &sb);
|
|
|
|
pr_debug_dtp("(%s)", sb.buf);
|
|
|
|
strbuf_release(&sb);
|
2024-03-18 22:51:07 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Normal registers should hold a pointer (or array) to
|
|
|
|
* dereference a memory location.
|
|
|
|
*/
|
2024-08-16 16:58:36 -07:00
|
|
|
if (!is_pointer_type(&state->regs[reg].type)) {
|
2024-05-01 23:00:08 -07:00
|
|
|
if (dloc->op->offset < 0 && reg != state->stack_reg)
|
|
|
|
goto check_kernel;
|
|
|
|
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_NO_POINTER;
|
2024-05-01 23:00:08 -07:00
|
|
|
}
|
|
|
|
|
2024-03-18 22:51:07 -07:00
|
|
|
/* Remove the pointer and get the target type */
|
2024-08-07 15:31:29 -07:00
|
|
|
if (__die_get_real_type(&state->regs[reg].type, type_die) == NULL)
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_NO_POINTER;
|
2024-03-18 22:51:07 -07:00
|
|
|
|
|
|
|
dloc->type_offset = dloc->op->offset;
|
|
|
|
|
2024-08-07 15:31:29 -07:00
|
|
|
if (dwarf_tag(type_die) == DW_TAG_typedef)
|
|
|
|
die_get_real_type(type_die, &sized_type);
|
|
|
|
else
|
|
|
|
sized_type = *type_die;
|
|
|
|
|
2024-03-18 22:51:07 -07:00
|
|
|
/* Get the size of the actual type */
|
2024-08-07 15:31:29 -07:00
|
|
|
if (dwarf_aggregate_size(&sized_type, &size) < 0 ||
|
2024-03-18 22:51:07 -07:00
|
|
|
(unsigned)dloc->type_offset >= size)
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_BAD_OFFSET;
|
2024-03-18 22:51:07 -07:00
|
|
|
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_OK;
|
2024-03-18 22:51:07 -07:00
|
|
|
}
|
|
|
|
|
2024-08-20 23:54:08 -07:00
|
|
|
if (state->regs[reg].kind == TSR_KIND_POINTER) {
|
|
|
|
pr_debug_dtp("percpu ptr");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It's actaully pointer but the address was calculated using
|
|
|
|
* some arithmetic. So it points to the actual type already.
|
|
|
|
*/
|
|
|
|
*type_die = state->regs[reg].type;
|
|
|
|
|
|
|
|
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 PERF_TMR_BAIL_OUT;
|
|
|
|
|
|
|
|
return PERF_TMR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state->regs[reg].kind == TSR_KIND_CANARY) {
|
|
|
|
pr_debug_dtp("stack canary");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a saved value of the stack canary which will be handled
|
|
|
|
* in the outer logic when it returns failure here. Pretend it's
|
|
|
|
* from the stack canary directly.
|
|
|
|
*/
|
|
|
|
setup_stack_canary(dloc);
|
|
|
|
|
|
|
|
return PERF_TMR_BAIL_OUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state->regs[reg].kind == TSR_KIND_PERCPU_BASE) {
|
|
|
|
u64 var_addr = dloc->op->offset;
|
|
|
|
int var_offset;
|
|
|
|
|
|
|
|
pr_debug_dtp("percpu var");
|
|
|
|
|
|
|
|
if (dloc->op->multi_regs) {
|
|
|
|
int reg2 = dloc->op->reg2;
|
|
|
|
|
|
|
|
if (dloc->op->reg2 == reg)
|
|
|
|
reg2 = dloc->op->reg1;
|
|
|
|
|
|
|
|
if (has_reg_type(state, reg2) && state->regs[reg2].ok &&
|
|
|
|
state->regs[reg2].kind == TSR_KIND_CONST)
|
|
|
|
var_addr += state->regs[reg2].imm_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_global_var_type(cu_die, dloc, dloc->ip, var_addr,
|
|
|
|
&var_offset, type_die)) {
|
|
|
|
dloc->type_offset = var_offset;
|
|
|
|
return PERF_TMR_OK;
|
|
|
|
}
|
|
|
|
/* No need to retry per-cpu (global) variables */
|
|
|
|
return PERF_TMR_BAIL_OUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
check_non_register:
|
2024-03-18 22:51:07 -07:00
|
|
|
if (reg == dloc->fbreg) {
|
|
|
|
struct type_state_stack *stack;
|
|
|
|
|
2024-08-16 16:58:35 -07:00
|
|
|
pr_debug_dtp("fbreg");
|
2024-03-18 22:51:07 -07:00
|
|
|
|
|
|
|
stack = find_stack_state(state, dloc->type_offset);
|
2024-08-21 16:26:27 -07:00
|
|
|
if (stack == NULL) {
|
|
|
|
if (retry) {
|
|
|
|
pr_debug_dtp(" : retry\n");
|
|
|
|
retry = false;
|
|
|
|
|
|
|
|
/* update type info it's the first store to the stack */
|
|
|
|
update_insn_state(state, dloc, cu_die, dl);
|
|
|
|
goto again;
|
|
|
|
}
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_NO_TYPE;
|
2024-08-21 16:26:27 -07:00
|
|
|
}
|
2024-03-18 22:51:07 -07:00
|
|
|
|
2024-03-18 22:51:13 -07:00
|
|
|
if (stack->kind == TSR_KIND_CANARY) {
|
|
|
|
setup_stack_canary(dloc);
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_BAIL_OUT;
|
2024-03-18 22:51:13 -07:00
|
|
|
}
|
|
|
|
|
2024-05-01 23:00:11 -07:00
|
|
|
if (stack->kind != TSR_KIND_TYPE)
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_NO_TYPE;
|
2024-05-01 23:00:11 -07:00
|
|
|
|
2024-03-18 22:51:07 -07:00
|
|
|
*type_die = stack->type;
|
|
|
|
/* Update the type offset from the start of slot */
|
|
|
|
dloc->type_offset -= stack->offset;
|
|
|
|
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_OK;
|
2024-03-18 22:51:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dloc->fb_cfa) {
|
|
|
|
struct type_state_stack *stack;
|
|
|
|
u64 pc = map__rip_2objdump(dloc->ms->map, dloc->ip);
|
|
|
|
int fbreg, fboff;
|
|
|
|
|
2024-08-16 16:58:35 -07:00
|
|
|
pr_debug_dtp("cfa");
|
2024-03-18 22:51:07 -07:00
|
|
|
|
|
|
|
if (die_get_cfa(dloc->di->dbg, pc, &fbreg, &fboff) < 0)
|
|
|
|
fbreg = -1;
|
|
|
|
|
|
|
|
if (reg != fbreg)
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_NO_TYPE;
|
2024-03-18 22:51:07 -07:00
|
|
|
|
|
|
|
stack = find_stack_state(state, dloc->type_offset - fboff);
|
2024-08-21 16:26:27 -07:00
|
|
|
if (stack == NULL) {
|
|
|
|
if (retry) {
|
|
|
|
pr_debug_dtp(" : retry\n");
|
|
|
|
retry = false;
|
|
|
|
|
|
|
|
/* update type info it's the first store to the stack */
|
|
|
|
update_insn_state(state, dloc, cu_die, dl);
|
|
|
|
goto again;
|
|
|
|
}
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_NO_TYPE;
|
2024-08-21 16:26:27 -07:00
|
|
|
}
|
2024-03-18 22:51:07 -07:00
|
|
|
|
2024-03-18 22:51:13 -07:00
|
|
|
if (stack->kind == TSR_KIND_CANARY) {
|
|
|
|
setup_stack_canary(dloc);
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_BAIL_OUT;
|
2024-03-18 22:51:13 -07:00
|
|
|
}
|
|
|
|
|
2024-05-01 23:00:11 -07:00
|
|
|
if (stack->kind != TSR_KIND_TYPE)
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_NO_TYPE;
|
2024-05-01 23:00:11 -07:00
|
|
|
|
2024-03-18 22:51:07 -07:00
|
|
|
*type_die = stack->type;
|
|
|
|
/* Update the type offset from the start of slot */
|
|
|
|
dloc->type_offset -= fboff + stack->offset;
|
|
|
|
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_OK;
|
2024-03-18 22:51:07 -07:00
|
|
|
}
|
|
|
|
|
2024-05-01 23:00:08 -07:00
|
|
|
check_kernel:
|
2024-05-04 14:38:01 -07:00
|
|
|
if (dso__kernel(map__dso(dloc->ms->map))) {
|
2024-03-18 22:51:09 -07:00
|
|
|
u64 addr;
|
|
|
|
|
2024-03-18 22:51:11 -07:00
|
|
|
/* Direct this-cpu access like "%gs:0x34740" */
|
2024-05-01 23:00:08 -07:00
|
|
|
if (dloc->op->segment == INSN_SEG_X86_GS && dloc->op->imm &&
|
|
|
|
arch__is(dloc->arch, "x86")) {
|
2024-08-16 16:58:35 -07:00
|
|
|
pr_debug_dtp("this-cpu var");
|
2024-03-18 22:51:09 -07:00
|
|
|
|
|
|
|
addr = dloc->op->offset;
|
|
|
|
|
|
|
|
if (get_global_var_type(cu_die, dloc, dloc->ip, addr,
|
|
|
|
&offset, type_die)) {
|
|
|
|
dloc->type_offset = offset;
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_OK;
|
2024-03-18 22:51:09 -07:00
|
|
|
}
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_BAIL_OUT;
|
2024-03-18 22:51:11 -07:00
|
|
|
}
|
|
|
|
|
2024-05-01 23:00:08 -07:00
|
|
|
/* Access to global variable like "-0x7dcf0500(,%rdx,8)" */
|
2024-03-18 22:51:11 -07:00
|
|
|
if (dloc->op->offset < 0 && reg != state->stack_reg) {
|
|
|
|
addr = (s64) dloc->op->offset;
|
|
|
|
|
2024-05-01 23:00:08 -07:00
|
|
|
if (get_global_var_type(cu_die, dloc, dloc->ip, addr,
|
2024-03-18 22:51:11 -07:00
|
|
|
&offset, type_die)) {
|
2024-08-16 16:58:35 -07:00
|
|
|
pr_debug_dtp("global var");
|
2024-03-18 22:51:11 -07:00
|
|
|
|
|
|
|
dloc->type_offset = offset;
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_OK;
|
2024-03-18 22:51:11 -07:00
|
|
|
}
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_BAIL_OUT;
|
2024-03-18 22:51:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_UNKNOWN;
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Iterate instructions in basic blocks and update type table */
|
2024-08-16 16:58:35 -07:00
|
|
|
static enum type_match_result find_data_type_insn(struct data_loc_info *dloc,
|
|
|
|
struct list_head *basic_blocks,
|
|
|
|
struct die_var_type *var_types,
|
|
|
|
Dwarf_Die *cu_die,
|
|
|
|
Dwarf_Die *type_die)
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
{
|
|
|
|
struct type_state state;
|
|
|
|
struct symbol *sym = dloc->ms->sym;
|
|
|
|
struct annotation *notes = symbol__annotation(sym);
|
|
|
|
struct annotated_basic_block *bb;
|
2024-08-16 16:58:35 -07:00
|
|
|
enum type_match_result ret = PERF_TMR_UNKNOWN;
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
|
|
|
|
init_type_state(&state, dloc->arch);
|
|
|
|
|
|
|
|
list_for_each_entry(bb, basic_blocks, list) {
|
|
|
|
struct disasm_line *dl = bb->begin;
|
|
|
|
|
2024-04-05 14:17:59 -07:00
|
|
|
BUG_ON(bb->begin->al.offset == -1 || bb->end->al.offset == -1);
|
|
|
|
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
pr_debug_dtp("bb: [%"PRIx64" - %"PRIx64"]\n",
|
|
|
|
bb->begin->al.offset, bb->end->al.offset);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-04-05 14:17:59 -07:00
|
|
|
/* Skip comment or debug info lines */
|
|
|
|
if (dl->al.offset == -1)
|
|
|
|
continue;
|
|
|
|
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
/* Update variable type at this address */
|
|
|
|
update_var_state(&state, dloc, addr, dl->al.offset, var_types);
|
|
|
|
|
|
|
|
if (this_ip == dloc->ip) {
|
2024-05-01 23:00:10 -07:00
|
|
|
ret = check_matching_type(&state, dloc,
|
2024-08-21 16:26:27 -07:00
|
|
|
cu_die, dl, type_die);
|
2024-08-16 16:58:35 -07:00
|
|
|
pr_debug_dtp(" : %s\n", match_result_str(ret));
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
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);
|
2024-03-18 22:51:15 -07:00
|
|
|
return ret;
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
}
|
|
|
|
|
2024-07-18 14:13:54 +05:30
|
|
|
static int arch_supports_insn_tracking(struct data_loc_info *dloc)
|
|
|
|
{
|
|
|
|
if ((arch__is(dloc->arch, "x86")) || (arch__is(dloc->arch, "powerpc")))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2024-08-16 16:58:35 -07:00
|
|
|
static enum type_match_result find_data_type_block(struct data_loc_info *dloc,
|
|
|
|
Dwarf_Die *cu_die,
|
|
|
|
Dwarf_Die *scopes,
|
|
|
|
int nr_scopes,
|
|
|
|
Dwarf_Die *type_die)
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
{
|
|
|
|
LIST_HEAD(basic_blocks);
|
|
|
|
struct die_var_type *var_types = NULL;
|
|
|
|
u64 src_ip, dst_ip, prev_dst_ip;
|
2024-08-16 16:58:35 -07:00
|
|
|
enum type_match_result ret = PERF_TMR_UNKNOWN;
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
|
|
|
|
/* TODO: other architecture support */
|
2024-07-18 14:13:54 +05:30
|
|
|
if (!arch_supports_insn_tracking(dloc))
|
2024-08-16 16:58:35 -07:00
|
|
|
return PERF_TMR_BAIL_OUT;
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
|
|
|
|
prev_dst_ip = 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;
|
|
|
|
|
2024-09-09 14:42:51 -07:00
|
|
|
pr_debug_dtp("scope: [%d/%d] ", i + 1, nr_scopes);
|
|
|
|
pr_debug_scope(&scopes[i]);
|
|
|
|
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
src_ip = map__objdump_2rip(dloc->ms->map, start);
|
|
|
|
|
|
|
|
again:
|
|
|
|
/* Get basic blocks for this scope */
|
|
|
|
if (annotate_get_basic_blocks(dloc->ms->sym, src_ip, dst_ip,
|
|
|
|
&this_blocks) < 0) {
|
|
|
|
/* Try previous block if they are not connected */
|
|
|
|
if (prev_dst_ip != dst_ip) {
|
|
|
|
dst_ip = prev_dst_ip;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_debug_dtp("cannot find a basic block from %"PRIx64" to %"PRIx64"\n",
|
|
|
|
src_ip - dloc->ms->sym->start,
|
|
|
|
dst_ip - dloc->ms->sym->start);
|
|
|
|
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 */
|
2024-08-16 16:58:35 -07:00
|
|
|
ret = find_data_type_insn(dloc, &basic_blocks, var_types,
|
2024-03-18 22:51:15 -07:00
|
|
|
cu_die, type_die);
|
2024-08-16 16:58:35 -07:00
|
|
|
if (ret == PERF_TMR_OK) {
|
2024-05-01 23:00:09 -07:00
|
|
|
char buf[64];
|
2024-08-21 16:26:26 -07:00
|
|
|
int offset = dloc->op->offset;
|
|
|
|
const char *offset_sign = "";
|
|
|
|
|
|
|
|
if (offset < 0) {
|
|
|
|
offset = -offset;
|
|
|
|
offset_sign = "-";
|
|
|
|
}
|
2024-05-01 23:00:09 -07:00
|
|
|
|
|
|
|
if (dloc->op->multi_regs)
|
|
|
|
snprintf(buf, sizeof(buf), "reg%d, reg%d",
|
|
|
|
dloc->op->reg1, dloc->op->reg2);
|
|
|
|
else
|
|
|
|
snprintf(buf, sizeof(buf), "reg%d", dloc->op->reg1);
|
|
|
|
|
2024-08-21 16:26:26 -07:00
|
|
|
pr_debug_dtp("found by insn track: %s%#x(%s) type-offset=%#x\n",
|
|
|
|
offset_sign, offset, buf, dloc->type_offset);
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-08-16 16:58:35 -07:00
|
|
|
if (ret == PERF_TMR_BAIL_OUT)
|
2024-03-18 22:51:15 -07:00
|
|
|
break;
|
|
|
|
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
/* Go up to the next scope and find blocks to the start */
|
|
|
|
prev_dst_ip = dst_ip;
|
|
|
|
dst_ip = src_ip;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete_basic_blocks(&basic_blocks);
|
|
|
|
delete_var_types(var_types);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
/* The result will be saved in @type_die */
|
2024-03-18 22:50:58 -07:00
|
|
|
static int find_data_type_die(struct data_loc_info *dloc, Dwarf_Die *type_die)
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
{
|
2024-03-18 22:50:58 -07:00
|
|
|
struct annotated_op_loc *loc = dloc->op;
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
Dwarf_Die cu_die, var_die;
|
|
|
|
Dwarf_Die *scopes = NULL;
|
2024-08-21 16:26:26 -07:00
|
|
|
int reg, offset = loc->offset;
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
int ret = -1;
|
|
|
|
int i, nr_scopes;
|
2024-01-16 22:26:56 -08:00
|
|
|
int fbreg = -1;
|
|
|
|
int fb_offset = 0;
|
2024-03-18 22:50:58 -07:00
|
|
|
bool is_fbreg = false;
|
perf annotate-data: Check variables in every scope
Sometimes it matches a variable in the inner scope but it fails because
the actual access can be on a different type. Let's try variables in
every scope and choose the best one using is_better_type().
I have an example with update_blocked_averages(), at first it found a
variable (__mptr) but it's a void pointer. So it moved on to the upper
scope and found another variable (cfs_rq).
$ perf --debug type-profile annotate --data-type --stdio
...
-----------------------------------------------------------
find data type for 0x140(reg14) at update_blocked_averages+0x2db
CU for kernel/sched/fair.c (die:0x12dd892)
frame base: cfa=1 fbreg=7
found "__mptr" (die: 0x13022f1) in scope=4/4 (die: 0x13022e8) failed: no/void pointer
variable location: base=reg14, offset=0x140
type='void*' size=0x8 (die:0x12dd8f9)
found "cfs_rq" (die: 0x1301721) in scope=3/4 (die: 0x130171c) type_offset=0x140
variable location: reg14
type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
final type: type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
IIUC the scope is like below:
1: update_blocked_averages
2: __update_blocked_fair
3: for_each_leaf_cfs_rq_safe
4: list_entry -> (container_of)
The container_of is implemented like:
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
That's why we see the __mptr variable first but it failed since it has
no type information.
Then for_each_leaf_cfs_rq_safe() is defined as
#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
leaf_cfs_rq_list)
Note that the access was 0x140(r14). And the cfs_rq has
leaf_cfs_rq_list at the 0x140. So it converts the list_head pointer to
a pointer to struct cfs_rq here.
$ pahole --hex -C cfs_rq vmlinux | grep 140
struct cfs_rq struct list_head leaf_cfs_rq_list; /* 0x140 0x10 */
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240816235840.2754937-9-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-16 16:58:38 -07:00
|
|
|
bool found = false;
|
2024-03-18 22:50:58 -07:00
|
|
|
u64 pc;
|
2024-03-18 22:51:00 -07:00
|
|
|
char buf[64];
|
2024-08-16 16:58:39 -07:00
|
|
|
enum type_match_result result = PERF_TMR_UNKNOWN;
|
2024-08-21 16:26:26 -07:00
|
|
|
const char *offset_sign = "";
|
2024-03-18 22:51:00 -07:00
|
|
|
|
|
|
|
if (dloc->op->multi_regs)
|
2024-04-12 11:33:07 -07:00
|
|
|
snprintf(buf, sizeof(buf), "reg%d, reg%d", dloc->op->reg1, dloc->op->reg2);
|
2024-03-18 22:51:00 -07:00
|
|
|
else if (dloc->op->reg1 == DWARF_REG_PC)
|
2024-04-12 11:33:07 -07:00
|
|
|
snprintf(buf, sizeof(buf), "PC");
|
2024-03-18 22:51:00 -07:00
|
|
|
else
|
2024-04-12 11:33:07 -07:00
|
|
|
snprintf(buf, sizeof(buf), "reg%d", dloc->op->reg1);
|
2024-03-18 22:51:00 -07:00
|
|
|
|
2024-08-21 16:26:26 -07:00
|
|
|
if (offset < 0) {
|
|
|
|
offset = -offset;
|
|
|
|
offset_sign = "-";
|
|
|
|
}
|
|
|
|
|
2024-03-18 22:51:00 -07:00
|
|
|
pr_debug_dtp("-----------------------------------------------------------\n");
|
2024-08-21 16:26:26 -07:00
|
|
|
pr_debug_dtp("find data type for %s%#x(%s) at %s+%#"PRIx64"\n",
|
|
|
|
offset_sign, offset, buf,
|
|
|
|
dloc->ms->sym->name, dloc->ip - dloc->ms->sym->start);
|
2024-03-18 22:50:58 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* IP is a relative instruction address from the start of the map, as
|
|
|
|
* it can be randomized/relocated, it needs to translate to PC which is
|
|
|
|
* a file address for DWARF processing.
|
|
|
|
*/
|
|
|
|
pc = map__rip_2objdump(dloc->ms->map, dloc->ip);
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
|
|
|
|
/* Get a compile_unit for this address */
|
2024-03-18 22:50:58 -07:00
|
|
|
if (!find_cu_die(dloc->di, pc, &cu_die)) {
|
2024-03-18 22:51:00 -07:00
|
|
|
pr_debug_dtp("cannot find CU for address %"PRIx64"\n", pc);
|
2023-12-12 16:13:22 -08:00
|
|
|
ann_data_stat.no_cuinfo++;
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-01-16 22:26:51 -08:00
|
|
|
reg = loc->reg1;
|
|
|
|
offset = loc->offset;
|
|
|
|
|
2024-04-12 11:33:07 -07:00
|
|
|
pr_debug_dtp("CU for %s (die:%#lx)\n",
|
|
|
|
dwarf_diename(&cu_die), (long)dwarf_dieoffset(&cu_die));
|
2024-03-18 22:51:00 -07:00
|
|
|
|
2024-01-16 22:26:54 -08:00
|
|
|
if (reg == DWARF_REG_PC) {
|
2024-03-18 22:51:03 -07:00
|
|
|
if (get_global_var_type(&cu_die, dloc, dloc->ip, dloc->var_addr,
|
|
|
|
&offset, type_die)) {
|
2024-03-18 22:50:58 -07:00
|
|
|
dloc->type_offset = offset;
|
2024-03-18 22:51:00 -07:00
|
|
|
|
2024-04-12 11:33:07 -07:00
|
|
|
pr_debug_dtp("found by addr=%#"PRIx64" type_offset=%#x\n",
|
2024-03-18 22:51:00 -07:00
|
|
|
dloc->var_addr, offset);
|
2024-04-05 14:17:58 -07:00
|
|
|
pr_debug_type_name(type_die, TSR_KIND_TYPE);
|
2024-08-16 16:58:39 -07:00
|
|
|
found = true;
|
2024-01-16 22:26:54 -08:00
|
|
|
goto out;
|
|
|
|
}
|
2024-01-16 22:26:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a list of nested scopes - i.e. (inlined) functions and blocks. */
|
|
|
|
nr_scopes = die_get_scopes(&cu_die, pc, &scopes);
|
|
|
|
|
2024-01-16 22:26:56 -08:00
|
|
|
if (reg != DWARF_REG_PC && dwarf_hasattr(&scopes[0], DW_AT_frame_base)) {
|
|
|
|
Dwarf_Attribute attr;
|
|
|
|
Dwarf_Block block;
|
|
|
|
|
|
|
|
/* Check if the 'reg' is assigned as frame base register */
|
|
|
|
if (dwarf_attr(&scopes[0], DW_AT_frame_base, &attr) != NULL &&
|
|
|
|
dwarf_formblock(&attr, &block) == 0 && block.length == 1) {
|
|
|
|
switch (*block.data) {
|
|
|
|
case DW_OP_reg0 ... DW_OP_reg31:
|
2024-03-18 22:50:58 -07:00
|
|
|
fbreg = dloc->fbreg = *block.data - DW_OP_reg0;
|
2024-01-16 22:26:56 -08:00
|
|
|
break;
|
|
|
|
case DW_OP_call_frame_cfa:
|
2024-03-18 22:50:58 -07:00
|
|
|
dloc->fb_cfa = true;
|
|
|
|
if (die_get_cfa(dloc->di->dbg, pc, &fbreg,
|
2024-01-16 22:26:56 -08:00
|
|
|
&fb_offset) < 0)
|
|
|
|
fbreg = -1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2024-03-18 22:51:00 -07:00
|
|
|
|
|
|
|
pr_debug_dtp("frame base: cfa=%d fbreg=%d\n",
|
|
|
|
dloc->fb_cfa, fbreg);
|
2024-01-16 22:26:56 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-16 22:26:51 -08:00
|
|
|
retry:
|
2024-01-16 22:26:56 -08:00
|
|
|
is_fbreg = (reg == fbreg);
|
|
|
|
if (is_fbreg)
|
|
|
|
offset = loc->offset - fb_offset;
|
|
|
|
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
/* Search from the inner-most scope to the outer */
|
|
|
|
for (i = nr_scopes - 1; i >= 0; i--) {
|
perf annotate-data: Check variables in every scope
Sometimes it matches a variable in the inner scope but it fails because
the actual access can be on a different type. Let's try variables in
every scope and choose the best one using is_better_type().
I have an example with update_blocked_averages(), at first it found a
variable (__mptr) but it's a void pointer. So it moved on to the upper
scope and found another variable (cfs_rq).
$ perf --debug type-profile annotate --data-type --stdio
...
-----------------------------------------------------------
find data type for 0x140(reg14) at update_blocked_averages+0x2db
CU for kernel/sched/fair.c (die:0x12dd892)
frame base: cfa=1 fbreg=7
found "__mptr" (die: 0x13022f1) in scope=4/4 (die: 0x13022e8) failed: no/void pointer
variable location: base=reg14, offset=0x140
type='void*' size=0x8 (die:0x12dd8f9)
found "cfs_rq" (die: 0x1301721) in scope=3/4 (die: 0x130171c) type_offset=0x140
variable location: reg14
type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
final type: type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
IIUC the scope is like below:
1: update_blocked_averages
2: __update_blocked_fair
3: for_each_leaf_cfs_rq_safe
4: list_entry -> (container_of)
The container_of is implemented like:
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
That's why we see the __mptr variable first but it failed since it has
no type information.
Then for_each_leaf_cfs_rq_safe() is defined as
#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
leaf_cfs_rq_list)
Note that the access was 0x140(r14). And the cfs_rq has
leaf_cfs_rq_list at the 0x140. So it converts the list_head pointer to
a pointer to struct cfs_rq here.
$ pahole --hex -C cfs_rq vmlinux | grep 140
struct cfs_rq struct list_head leaf_cfs_rq_list; /* 0x140 0x10 */
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240816235840.2754937-9-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-16 16:58:38 -07:00
|
|
|
Dwarf_Die mem_die;
|
|
|
|
int type_offset = offset;
|
|
|
|
|
2024-01-16 22:26:53 -08:00
|
|
|
if (reg == DWARF_REG_PC) {
|
2024-03-18 22:50:58 -07:00
|
|
|
if (!die_find_variable_by_addr(&scopes[i], dloc->var_addr,
|
perf annotate-data: Check variables in every scope
Sometimes it matches a variable in the inner scope but it fails because
the actual access can be on a different type. Let's try variables in
every scope and choose the best one using is_better_type().
I have an example with update_blocked_averages(), at first it found a
variable (__mptr) but it's a void pointer. So it moved on to the upper
scope and found another variable (cfs_rq).
$ perf --debug type-profile annotate --data-type --stdio
...
-----------------------------------------------------------
find data type for 0x140(reg14) at update_blocked_averages+0x2db
CU for kernel/sched/fair.c (die:0x12dd892)
frame base: cfa=1 fbreg=7
found "__mptr" (die: 0x13022f1) in scope=4/4 (die: 0x13022e8) failed: no/void pointer
variable location: base=reg14, offset=0x140
type='void*' size=0x8 (die:0x12dd8f9)
found "cfs_rq" (die: 0x1301721) in scope=3/4 (die: 0x130171c) type_offset=0x140
variable location: reg14
type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
final type: type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
IIUC the scope is like below:
1: update_blocked_averages
2: __update_blocked_fair
3: for_each_leaf_cfs_rq_safe
4: list_entry -> (container_of)
The container_of is implemented like:
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
That's why we see the __mptr variable first but it failed since it has
no type information.
Then for_each_leaf_cfs_rq_safe() is defined as
#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
leaf_cfs_rq_list)
Note that the access was 0x140(r14). And the cfs_rq has
leaf_cfs_rq_list at the 0x140. So it converts the list_head pointer to
a pointer to struct cfs_rq here.
$ pahole --hex -C cfs_rq vmlinux | grep 140
struct cfs_rq struct list_head leaf_cfs_rq_list; /* 0x140 0x10 */
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240816235840.2754937-9-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-16 16:58:38 -07:00
|
|
|
&var_die, &type_offset))
|
2024-01-16 22:26:53 -08:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* Look up variables/parameters in this scope */
|
|
|
|
if (!die_find_variable_by_reg(&scopes[i], pc, reg,
|
perf annotate-data: Check variables in every scope
Sometimes it matches a variable in the inner scope but it fails because
the actual access can be on a different type. Let's try variables in
every scope and choose the best one using is_better_type().
I have an example with update_blocked_averages(), at first it found a
variable (__mptr) but it's a void pointer. So it moved on to the upper
scope and found another variable (cfs_rq).
$ perf --debug type-profile annotate --data-type --stdio
...
-----------------------------------------------------------
find data type for 0x140(reg14) at update_blocked_averages+0x2db
CU for kernel/sched/fair.c (die:0x12dd892)
frame base: cfa=1 fbreg=7
found "__mptr" (die: 0x13022f1) in scope=4/4 (die: 0x13022e8) failed: no/void pointer
variable location: base=reg14, offset=0x140
type='void*' size=0x8 (die:0x12dd8f9)
found "cfs_rq" (die: 0x1301721) in scope=3/4 (die: 0x130171c) type_offset=0x140
variable location: reg14
type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
final type: type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
IIUC the scope is like below:
1: update_blocked_averages
2: __update_blocked_fair
3: for_each_leaf_cfs_rq_safe
4: list_entry -> (container_of)
The container_of is implemented like:
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
That's why we see the __mptr variable first but it failed since it has
no type information.
Then for_each_leaf_cfs_rq_safe() is defined as
#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
leaf_cfs_rq_list)
Note that the access was 0x140(r14). And the cfs_rq has
leaf_cfs_rq_list at the 0x140. So it converts the list_head pointer to
a pointer to struct cfs_rq here.
$ pahole --hex -C cfs_rq vmlinux | grep 140
struct cfs_rq struct list_head leaf_cfs_rq_list; /* 0x140 0x10 */
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240816235840.2754937-9-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-16 16:58:38 -07:00
|
|
|
&type_offset, is_fbreg, &var_die))
|
2024-01-16 22:26:53 -08:00
|
|
|
continue;
|
|
|
|
}
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
|
2024-08-16 16:58:34 -07:00
|
|
|
pr_debug_dtp("found \"%s\" (die: %#lx) in scope=%d/%d (die: %#lx) ",
|
|
|
|
dwarf_diename(&var_die), (long)dwarf_dieoffset(&var_die),
|
|
|
|
i+1, nr_scopes, (long)dwarf_dieoffset(&scopes[i]));
|
|
|
|
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
/* Found a variable, see if it's correct */
|
perf annotate-data: Check variables in every scope
Sometimes it matches a variable in the inner scope but it fails because
the actual access can be on a different type. Let's try variables in
every scope and choose the best one using is_better_type().
I have an example with update_blocked_averages(), at first it found a
variable (__mptr) but it's a void pointer. So it moved on to the upper
scope and found another variable (cfs_rq).
$ perf --debug type-profile annotate --data-type --stdio
...
-----------------------------------------------------------
find data type for 0x140(reg14) at update_blocked_averages+0x2db
CU for kernel/sched/fair.c (die:0x12dd892)
frame base: cfa=1 fbreg=7
found "__mptr" (die: 0x13022f1) in scope=4/4 (die: 0x13022e8) failed: no/void pointer
variable location: base=reg14, offset=0x140
type='void*' size=0x8 (die:0x12dd8f9)
found "cfs_rq" (die: 0x1301721) in scope=3/4 (die: 0x130171c) type_offset=0x140
variable location: reg14
type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
final type: type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
IIUC the scope is like below:
1: update_blocked_averages
2: __update_blocked_fair
3: for_each_leaf_cfs_rq_safe
4: list_entry -> (container_of)
The container_of is implemented like:
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
That's why we see the __mptr variable first but it failed since it has
no type information.
Then for_each_leaf_cfs_rq_safe() is defined as
#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
leaf_cfs_rq_list)
Note that the access was 0x140(r14). And the cfs_rq has
leaf_cfs_rq_list at the 0x140. So it converts the list_head pointer to
a pointer to struct cfs_rq here.
$ pahole --hex -C cfs_rq vmlinux | grep 140
struct cfs_rq struct list_head leaf_cfs_rq_list; /* 0x140 0x10 */
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240816235840.2754937-9-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-16 16:58:38 -07:00
|
|
|
result = check_variable(dloc, &var_die, &mem_die, reg, type_offset, is_fbreg);
|
2024-08-16 16:58:33 -07:00
|
|
|
if (result == PERF_TMR_OK) {
|
2024-04-12 11:33:07 -07:00
|
|
|
if (reg == DWARF_REG_PC) {
|
|
|
|
pr_debug_dtp("addr=%#"PRIx64" type_offset=%#x\n",
|
perf annotate-data: Check variables in every scope
Sometimes it matches a variable in the inner scope but it fails because
the actual access can be on a different type. Let's try variables in
every scope and choose the best one using is_better_type().
I have an example with update_blocked_averages(), at first it found a
variable (__mptr) but it's a void pointer. So it moved on to the upper
scope and found another variable (cfs_rq).
$ perf --debug type-profile annotate --data-type --stdio
...
-----------------------------------------------------------
find data type for 0x140(reg14) at update_blocked_averages+0x2db
CU for kernel/sched/fair.c (die:0x12dd892)
frame base: cfa=1 fbreg=7
found "__mptr" (die: 0x13022f1) in scope=4/4 (die: 0x13022e8) failed: no/void pointer
variable location: base=reg14, offset=0x140
type='void*' size=0x8 (die:0x12dd8f9)
found "cfs_rq" (die: 0x1301721) in scope=3/4 (die: 0x130171c) type_offset=0x140
variable location: reg14
type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
final type: type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
IIUC the scope is like below:
1: update_blocked_averages
2: __update_blocked_fair
3: for_each_leaf_cfs_rq_safe
4: list_entry -> (container_of)
The container_of is implemented like:
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
That's why we see the __mptr variable first but it failed since it has
no type information.
Then for_each_leaf_cfs_rq_safe() is defined as
#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
leaf_cfs_rq_list)
Note that the access was 0x140(r14). And the cfs_rq has
leaf_cfs_rq_list at the 0x140. So it converts the list_head pointer to
a pointer to struct cfs_rq here.
$ pahole --hex -C cfs_rq vmlinux | grep 140
struct cfs_rq struct list_head leaf_cfs_rq_list; /* 0x140 0x10 */
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240816235840.2754937-9-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-16 16:58:38 -07:00
|
|
|
dloc->var_addr, type_offset);
|
2024-04-12 11:33:07 -07:00
|
|
|
} else if (reg == DWARF_REG_FB || is_fbreg) {
|
|
|
|
pr_debug_dtp("stack_offset=%#x type_offset=%#x\n",
|
perf annotate-data: Check variables in every scope
Sometimes it matches a variable in the inner scope but it fails because
the actual access can be on a different type. Let's try variables in
every scope and choose the best one using is_better_type().
I have an example with update_blocked_averages(), at first it found a
variable (__mptr) but it's a void pointer. So it moved on to the upper
scope and found another variable (cfs_rq).
$ perf --debug type-profile annotate --data-type --stdio
...
-----------------------------------------------------------
find data type for 0x140(reg14) at update_blocked_averages+0x2db
CU for kernel/sched/fair.c (die:0x12dd892)
frame base: cfa=1 fbreg=7
found "__mptr" (die: 0x13022f1) in scope=4/4 (die: 0x13022e8) failed: no/void pointer
variable location: base=reg14, offset=0x140
type='void*' size=0x8 (die:0x12dd8f9)
found "cfs_rq" (die: 0x1301721) in scope=3/4 (die: 0x130171c) type_offset=0x140
variable location: reg14
type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
final type: type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
IIUC the scope is like below:
1: update_blocked_averages
2: __update_blocked_fair
3: for_each_leaf_cfs_rq_safe
4: list_entry -> (container_of)
The container_of is implemented like:
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
That's why we see the __mptr variable first but it failed since it has
no type information.
Then for_each_leaf_cfs_rq_safe() is defined as
#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
leaf_cfs_rq_list)
Note that the access was 0x140(r14). And the cfs_rq has
leaf_cfs_rq_list at the 0x140. So it converts the list_head pointer to
a pointer to struct cfs_rq here.
$ pahole --hex -C cfs_rq vmlinux | grep 140
struct cfs_rq struct list_head leaf_cfs_rq_list; /* 0x140 0x10 */
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240816235840.2754937-9-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-16 16:58:38 -07:00
|
|
|
fb_offset, type_offset);
|
2024-04-12 11:33:07 -07:00
|
|
|
} else {
|
perf annotate-data: Check variables in every scope
Sometimes it matches a variable in the inner scope but it fails because
the actual access can be on a different type. Let's try variables in
every scope and choose the best one using is_better_type().
I have an example with update_blocked_averages(), at first it found a
variable (__mptr) but it's a void pointer. So it moved on to the upper
scope and found another variable (cfs_rq).
$ perf --debug type-profile annotate --data-type --stdio
...
-----------------------------------------------------------
find data type for 0x140(reg14) at update_blocked_averages+0x2db
CU for kernel/sched/fair.c (die:0x12dd892)
frame base: cfa=1 fbreg=7
found "__mptr" (die: 0x13022f1) in scope=4/4 (die: 0x13022e8) failed: no/void pointer
variable location: base=reg14, offset=0x140
type='void*' size=0x8 (die:0x12dd8f9)
found "cfs_rq" (die: 0x1301721) in scope=3/4 (die: 0x130171c) type_offset=0x140
variable location: reg14
type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
final type: type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
IIUC the scope is like below:
1: update_blocked_averages
2: __update_blocked_fair
3: for_each_leaf_cfs_rq_safe
4: list_entry -> (container_of)
The container_of is implemented like:
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
That's why we see the __mptr variable first but it failed since it has
no type information.
Then for_each_leaf_cfs_rq_safe() is defined as
#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
leaf_cfs_rq_list)
Note that the access was 0x140(r14). And the cfs_rq has
leaf_cfs_rq_list at the 0x140. So it converts the list_head pointer to
a pointer to struct cfs_rq here.
$ pahole --hex -C cfs_rq vmlinux | grep 140
struct cfs_rq struct list_head leaf_cfs_rq_list; /* 0x140 0x10 */
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240816235840.2754937-9-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-16 16:58:38 -07:00
|
|
|
pr_debug_dtp("type_offset=%#x\n", type_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found || is_better_type(type_die, &mem_die)) {
|
|
|
|
*type_die = mem_die;
|
|
|
|
dloc->type_offset = type_offset;
|
|
|
|
found = true;
|
2024-04-12 11:33:07 -07:00
|
|
|
}
|
|
|
|
} else {
|
2024-08-16 16:58:34 -07:00
|
|
|
pr_debug_dtp("failed: %s\n", match_result_str(result));
|
2024-03-18 22:51:00 -07:00
|
|
|
}
|
perf annotate-data: Check variables in every scope
Sometimes it matches a variable in the inner scope but it fails because
the actual access can be on a different type. Let's try variables in
every scope and choose the best one using is_better_type().
I have an example with update_blocked_averages(), at first it found a
variable (__mptr) but it's a void pointer. So it moved on to the upper
scope and found another variable (cfs_rq).
$ perf --debug type-profile annotate --data-type --stdio
...
-----------------------------------------------------------
find data type for 0x140(reg14) at update_blocked_averages+0x2db
CU for kernel/sched/fair.c (die:0x12dd892)
frame base: cfa=1 fbreg=7
found "__mptr" (die: 0x13022f1) in scope=4/4 (die: 0x13022e8) failed: no/void pointer
variable location: base=reg14, offset=0x140
type='void*' size=0x8 (die:0x12dd8f9)
found "cfs_rq" (die: 0x1301721) in scope=3/4 (die: 0x130171c) type_offset=0x140
variable location: reg14
type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
final type: type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
IIUC the scope is like below:
1: update_blocked_averages
2: __update_blocked_fair
3: for_each_leaf_cfs_rq_safe
4: list_entry -> (container_of)
The container_of is implemented like:
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
That's why we see the __mptr variable first but it failed since it has
no type information.
Then for_each_leaf_cfs_rq_safe() is defined as
#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
leaf_cfs_rq_list)
Note that the access was 0x140(r14). And the cfs_rq has
leaf_cfs_rq_list at the 0x140. So it converts the list_head pointer to
a pointer to struct cfs_rq here.
$ pahole --hex -C cfs_rq vmlinux | grep 140
struct cfs_rq struct list_head leaf_cfs_rq_list; /* 0x140 0x10 */
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240816235840.2754937-9-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-16 16:58:38 -07:00
|
|
|
|
2024-08-16 16:58:34 -07:00
|
|
|
pr_debug_location(&var_die, pc, reg);
|
perf annotate-data: Check variables in every scope
Sometimes it matches a variable in the inner scope but it fails because
the actual access can be on a different type. Let's try variables in
every scope and choose the best one using is_better_type().
I have an example with update_blocked_averages(), at first it found a
variable (__mptr) but it's a void pointer. So it moved on to the upper
scope and found another variable (cfs_rq).
$ perf --debug type-profile annotate --data-type --stdio
...
-----------------------------------------------------------
find data type for 0x140(reg14) at update_blocked_averages+0x2db
CU for kernel/sched/fair.c (die:0x12dd892)
frame base: cfa=1 fbreg=7
found "__mptr" (die: 0x13022f1) in scope=4/4 (die: 0x13022e8) failed: no/void pointer
variable location: base=reg14, offset=0x140
type='void*' size=0x8 (die:0x12dd8f9)
found "cfs_rq" (die: 0x1301721) in scope=3/4 (die: 0x130171c) type_offset=0x140
variable location: reg14
type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
final type: type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
IIUC the scope is like below:
1: update_blocked_averages
2: __update_blocked_fair
3: for_each_leaf_cfs_rq_safe
4: list_entry -> (container_of)
The container_of is implemented like:
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
That's why we see the __mptr variable first but it failed since it has
no type information.
Then for_each_leaf_cfs_rq_safe() is defined as
#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
leaf_cfs_rq_list)
Note that the access was 0x140(r14). And the cfs_rq has
leaf_cfs_rq_list at the 0x140. So it converts the list_head pointer to
a pointer to struct cfs_rq here.
$ pahole --hex -C cfs_rq vmlinux | grep 140
struct cfs_rq struct list_head leaf_cfs_rq_list; /* 0x140 0x10 */
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240816235840.2754937-9-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-16 16:58:38 -07:00
|
|
|
pr_debug_type_name(&mem_die, TSR_KIND_TYPE);
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
}
|
2024-01-16 22:26:51 -08:00
|
|
|
|
perf annotate-data: Check variables in every scope
Sometimes it matches a variable in the inner scope but it fails because
the actual access can be on a different type. Let's try variables in
every scope and choose the best one using is_better_type().
I have an example with update_blocked_averages(), at first it found a
variable (__mptr) but it's a void pointer. So it moved on to the upper
scope and found another variable (cfs_rq).
$ perf --debug type-profile annotate --data-type --stdio
...
-----------------------------------------------------------
find data type for 0x140(reg14) at update_blocked_averages+0x2db
CU for kernel/sched/fair.c (die:0x12dd892)
frame base: cfa=1 fbreg=7
found "__mptr" (die: 0x13022f1) in scope=4/4 (die: 0x13022e8) failed: no/void pointer
variable location: base=reg14, offset=0x140
type='void*' size=0x8 (die:0x12dd8f9)
found "cfs_rq" (die: 0x1301721) in scope=3/4 (die: 0x130171c) type_offset=0x140
variable location: reg14
type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
final type: type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
IIUC the scope is like below:
1: update_blocked_averages
2: __update_blocked_fair
3: for_each_leaf_cfs_rq_safe
4: list_entry -> (container_of)
The container_of is implemented like:
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
That's why we see the __mptr variable first but it failed since it has
no type information.
Then for_each_leaf_cfs_rq_safe() is defined as
#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
leaf_cfs_rq_list)
Note that the access was 0x140(r14). And the cfs_rq has
leaf_cfs_rq_list at the 0x140. So it converts the list_head pointer to
a pointer to struct cfs_rq here.
$ pahole --hex -C cfs_rq vmlinux | grep 140
struct cfs_rq struct list_head leaf_cfs_rq_list; /* 0x140 0x10 */
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240816235840.2754937-9-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-16 16:58:38 -07:00
|
|
|
if (!found && loc->multi_regs && reg == loc->reg1 && loc->reg1 != loc->reg2) {
|
2024-05-01 23:00:10 -07:00
|
|
|
reg = loc->reg2;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
perf annotate-data: Check variables in every scope
Sometimes it matches a variable in the inner scope but it fails because
the actual access can be on a different type. Let's try variables in
every scope and choose the best one using is_better_type().
I have an example with update_blocked_averages(), at first it found a
variable (__mptr) but it's a void pointer. So it moved on to the upper
scope and found another variable (cfs_rq).
$ perf --debug type-profile annotate --data-type --stdio
...
-----------------------------------------------------------
find data type for 0x140(reg14) at update_blocked_averages+0x2db
CU for kernel/sched/fair.c (die:0x12dd892)
frame base: cfa=1 fbreg=7
found "__mptr" (die: 0x13022f1) in scope=4/4 (die: 0x13022e8) failed: no/void pointer
variable location: base=reg14, offset=0x140
type='void*' size=0x8 (die:0x12dd8f9)
found "cfs_rq" (die: 0x1301721) in scope=3/4 (die: 0x130171c) type_offset=0x140
variable location: reg14
type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
final type: type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
IIUC the scope is like below:
1: update_blocked_averages
2: __update_blocked_fair
3: for_each_leaf_cfs_rq_safe
4: list_entry -> (container_of)
The container_of is implemented like:
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
That's why we see the __mptr variable first but it failed since it has
no type information.
Then for_each_leaf_cfs_rq_safe() is defined as
#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
leaf_cfs_rq_list)
Note that the access was 0x140(r14). And the cfs_rq has
leaf_cfs_rq_list at the 0x140. So it converts the list_head pointer to
a pointer to struct cfs_rq here.
$ pahole --hex -C cfs_rq vmlinux | grep 140
struct cfs_rq struct list_head leaf_cfs_rq_list; /* 0x140 0x10 */
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240816235840.2754937-9-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-16 16:58:38 -07:00
|
|
|
if (!found && reg != DWARF_REG_PC) {
|
2024-08-16 16:58:35 -07:00
|
|
|
result = find_data_type_block(dloc, &cu_die, scopes,
|
perf annotate-data: Check variables in every scope
Sometimes it matches a variable in the inner scope but it fails because
the actual access can be on a different type. Let's try variables in
every scope and choose the best one using is_better_type().
I have an example with update_blocked_averages(), at first it found a
variable (__mptr) but it's a void pointer. So it moved on to the upper
scope and found another variable (cfs_rq).
$ perf --debug type-profile annotate --data-type --stdio
...
-----------------------------------------------------------
find data type for 0x140(reg14) at update_blocked_averages+0x2db
CU for kernel/sched/fair.c (die:0x12dd892)
frame base: cfa=1 fbreg=7
found "__mptr" (die: 0x13022f1) in scope=4/4 (die: 0x13022e8) failed: no/void pointer
variable location: base=reg14, offset=0x140
type='void*' size=0x8 (die:0x12dd8f9)
found "cfs_rq" (die: 0x1301721) in scope=3/4 (die: 0x130171c) type_offset=0x140
variable location: reg14
type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
final type: type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
IIUC the scope is like below:
1: update_blocked_averages
2: __update_blocked_fair
3: for_each_leaf_cfs_rq_safe
4: list_entry -> (container_of)
The container_of is implemented like:
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
That's why we see the __mptr variable first but it failed since it has
no type information.
Then for_each_leaf_cfs_rq_safe() is defined as
#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
leaf_cfs_rq_list)
Note that the access was 0x140(r14). And the cfs_rq has
leaf_cfs_rq_list at the 0x140. So it converts the list_head pointer to
a pointer to struct cfs_rq here.
$ pahole --hex -C cfs_rq vmlinux | grep 140
struct cfs_rq struct list_head leaf_cfs_rq_list; /* 0x140 0x10 */
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240816235840.2754937-9-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-16 16:58:38 -07:00
|
|
|
nr_scopes, type_die);
|
2024-08-16 16:58:35 -07:00
|
|
|
if (result == PERF_TMR_OK) {
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
ann_data_stat.insn_track++;
|
perf annotate-data: Check variables in every scope
Sometimes it matches a variable in the inner scope but it fails because
the actual access can be on a different type. Let's try variables in
every scope and choose the best one using is_better_type().
I have an example with update_blocked_averages(), at first it found a
variable (__mptr) but it's a void pointer. So it moved on to the upper
scope and found another variable (cfs_rq).
$ perf --debug type-profile annotate --data-type --stdio
...
-----------------------------------------------------------
find data type for 0x140(reg14) at update_blocked_averages+0x2db
CU for kernel/sched/fair.c (die:0x12dd892)
frame base: cfa=1 fbreg=7
found "__mptr" (die: 0x13022f1) in scope=4/4 (die: 0x13022e8) failed: no/void pointer
variable location: base=reg14, offset=0x140
type='void*' size=0x8 (die:0x12dd8f9)
found "cfs_rq" (die: 0x1301721) in scope=3/4 (die: 0x130171c) type_offset=0x140
variable location: reg14
type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
final type: type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
IIUC the scope is like below:
1: update_blocked_averages
2: __update_blocked_fair
3: for_each_leaf_cfs_rq_safe
4: list_entry -> (container_of)
The container_of is implemented like:
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
That's why we see the __mptr variable first but it failed since it has
no type information.
Then for_each_leaf_cfs_rq_safe() is defined as
#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
leaf_cfs_rq_list)
Note that the access was 0x140(r14). And the cfs_rq has
leaf_cfs_rq_list at the 0x140. So it converts the list_head pointer to
a pointer to struct cfs_rq here.
$ pahole --hex -C cfs_rq vmlinux | grep 140
struct cfs_rq struct list_head leaf_cfs_rq_list; /* 0x140 0x10 */
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240816235840.2754937-9-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-16 16:58:38 -07:00
|
|
|
found = true;
|
perf annotate-data: Implement instruction tracking
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.
The final check will be done by find_matching_type() in the next patch.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-15-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-03-18 22:51:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 16:58:39 -07:00
|
|
|
out:
|
2024-08-21 16:26:26 -07:00
|
|
|
pr_debug_dtp("final result: ");
|
perf annotate-data: Check variables in every scope
Sometimes it matches a variable in the inner scope but it fails because
the actual access can be on a different type. Let's try variables in
every scope and choose the best one using is_better_type().
I have an example with update_blocked_averages(), at first it found a
variable (__mptr) but it's a void pointer. So it moved on to the upper
scope and found another variable (cfs_rq).
$ perf --debug type-profile annotate --data-type --stdio
...
-----------------------------------------------------------
find data type for 0x140(reg14) at update_blocked_averages+0x2db
CU for kernel/sched/fair.c (die:0x12dd892)
frame base: cfa=1 fbreg=7
found "__mptr" (die: 0x13022f1) in scope=4/4 (die: 0x13022e8) failed: no/void pointer
variable location: base=reg14, offset=0x140
type='void*' size=0x8 (die:0x12dd8f9)
found "cfs_rq" (die: 0x1301721) in scope=3/4 (die: 0x130171c) type_offset=0x140
variable location: reg14
type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
final type: type='struct cfs_rq' size=0x1c0 (die:0x12e37e5)
IIUC the scope is like below:
1: update_blocked_averages
2: __update_blocked_fair
3: for_each_leaf_cfs_rq_safe
4: list_entry -> (container_of)
The container_of is implemented like:
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })
That's why we see the __mptr variable first but it failed since it has
no type information.
Then for_each_leaf_cfs_rq_safe() is defined as
#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
leaf_cfs_rq_list)
Note that the access was 0x140(r14). And the cfs_rq has
leaf_cfs_rq_list at the 0x140. So it converts the list_head pointer to
a pointer to struct cfs_rq here.
$ pahole --hex -C cfs_rq vmlinux | grep 140
struct cfs_rq struct list_head leaf_cfs_rq_list; /* 0x140 0x10 */
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240816235840.2754937-9-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-08-16 16:58:38 -07:00
|
|
|
if (found) {
|
|
|
|
pr_debug_type_name(type_die, TSR_KIND_TYPE);
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
2024-08-16 16:58:39 -07:00
|
|
|
switch (result) {
|
|
|
|
case PERF_TMR_NO_TYPE:
|
|
|
|
case PERF_TMR_NO_POINTER:
|
|
|
|
pr_debug_dtp("%s\n", match_result_str(result));
|
|
|
|
ann_data_stat.no_typeinfo++;
|
|
|
|
break;
|
|
|
|
case PERF_TMR_NO_SIZE:
|
|
|
|
pr_debug_dtp("%s\n", match_result_str(result));
|
|
|
|
ann_data_stat.invalid_size++;
|
|
|
|
break;
|
|
|
|
case PERF_TMR_BAD_OFFSET:
|
|
|
|
pr_debug_dtp("%s\n", match_result_str(result));
|
|
|
|
ann_data_stat.bad_offset++;
|
|
|
|
break;
|
|
|
|
case PERF_TMR_UNKNOWN:
|
|
|
|
case PERF_TMR_BAIL_OUT:
|
|
|
|
case PERF_TMR_OK: /* should not reach here */
|
|
|
|
default:
|
|
|
|
pr_debug_dtp("no variable found\n");
|
|
|
|
ann_data_stat.no_var++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret = -1;
|
2024-03-18 22:51:00 -07:00
|
|
|
}
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
|
|
|
|
free(scopes);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* find_data_type - Return a data type at the location
|
2024-03-18 22:50:58 -07:00
|
|
|
* @dloc: data location
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
*
|
|
|
|
* This functions searches the debug information of the binary to get the data
|
2024-03-18 22:50:58 -07:00
|
|
|
* type it accesses. The exact location is expressed by (ip, reg, offset)
|
|
|
|
* for pointer variables or (ip, addr) for global variables. Note that global
|
|
|
|
* variables might update the @dloc->type_offset after finding the start of the
|
|
|
|
* variable. If it cannot find a global variable by address, it tried to find
|
|
|
|
* a declaration of the variable using var_name. In that case, @dloc->offset
|
|
|
|
* won't be updated.
|
2024-01-16 22:26:54 -08:00
|
|
|
*
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
* It return %NULL if not found.
|
|
|
|
*/
|
2024-03-18 22:50:58 -07:00
|
|
|
struct annotated_data_type *find_data_type(struct data_loc_info *dloc)
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
{
|
2024-03-18 22:50:58 -07:00
|
|
|
struct dso *dso = map__dso(dloc->ms->map);
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
Dwarf_Die type_die;
|
|
|
|
|
|
|
|
/*
|
2024-03-18 22:50:58 -07:00
|
|
|
* The type offset is the same as instruction offset by default.
|
|
|
|
* But when finding a global variable, the offset won't be valid.
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
*/
|
2024-03-18 22:51:03 -07:00
|
|
|
dloc->type_offset = dloc->op->offset;
|
2024-03-18 22:50:58 -07:00
|
|
|
|
|
|
|
dloc->fbreg = -1;
|
|
|
|
|
|
|
|
if (find_data_type_die(dloc, &type_die) < 0)
|
2024-08-05 16:46:48 -07:00
|
|
|
return NULL;
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
|
2024-08-05 16:46:48 -07:00
|
|
|
return dso__findnew_data_type(dso, &type_die);
|
perf annotate-data: Add find_data_type() to get type from memory access
The find_data_type() is to get a data type from the memory access at the
given address (IP) using a register and an offset.
It requires DWARF debug info in the DSO and searches the list of
variables and function parameters in the scope.
In a pseudo code, it does basically the following:
find_data_type(dso, ip, reg, offset)
{
pc = map__rip_2objdump(ip);
CU = dwarf_addrdie(dso->dwarf, pc);
scopes = die_get_scopes(CU, pc);
for_each_scope(S, scopes) {
V = die_find_variable_by_reg(S, pc, reg);
if (V && V.type == pointer_type) {
T = die_get_real_type(V);
if (offset < T.size)
return T;
}
}
return NULL;
}
Committer notes:
The 'size' variable in check_variable() is 64-bit, so use PRIu64 and
inttypes.h to debug it.
Ditto at find_data_type_die().
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:09 -08:00
|
|
|
}
|
perf annotate-data: Add dso->data_types tree
To aggregate accesses to the same data type, add 'data_types' tree in
DSO to maintain data types and find it by name and size.
It might have different data types that happen to have the same name,
so it also compares the size of the type.
Even if it doesn't 100% guarantee, it reduces the possibility of
mis-handling of such conflicts.
And I don't think it's common to have different types with the same
name.
Committer notes:
Very few cases on the Linux kernel, but there are some different types
with the same name, unsure if there is a debug mode in libbpf dedup that
warns about such cases, but there are provisions in pahole for that,
see:
"emit: Notice type shadowing, i.e. multiple types with the same name (enum, struct, union, etc)"
https://git.kernel.org/pub/scm/devel/pahole/pahole.git/commit/?id=4f332dbfd02072e4f410db7bdcda8d6e3422974b
$ pahole --compile > vmlinux.h
$ rm -f a ; make a
cc a.c -o a
$ grep __[0-9] vmlinux.h
union irte__1 {
struct map_info__1;
struct map_info__1 {
struct map_info__1 * next; /* 0 8 */
$
drivers/iommu/amd/amd_iommu_types.h 'union irte'
include/linux/dmar.h 'struct irte'
include/linux/device-mapper.h:
union map_info {
void *ptr;
};
include/linux/mtd/map.h:
struct map_info {
const char *name;
unsigned long size;
resource_size_t phys;
<SNIP>
kernel/events/uprobes.c:
struct map_info {
struct map_info *next;
struct mm_struct *mm;
unsigned long vaddr;
};
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-5-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:10 -08:00
|
|
|
|
2023-12-12 16:13:17 -08:00
|
|
|
static int alloc_data_type_histograms(struct annotated_data_type *adt, int nr_entries)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
size_t sz = sizeof(struct type_hist);
|
|
|
|
|
|
|
|
sz += sizeof(struct type_hist_entry) * adt->self.size;
|
|
|
|
|
|
|
|
/* Allocate a table of pointers for each event */
|
|
|
|
adt->histograms = calloc(nr_entries, sizeof(*adt->histograms));
|
|
|
|
if (adt->histograms == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Each histogram is allocated for the whole size of the type.
|
|
|
|
* TODO: Probably we can move the histogram to members.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < nr_entries; i++) {
|
|
|
|
adt->histograms[i] = zalloc(sz);
|
|
|
|
if (adt->histograms[i] == NULL)
|
|
|
|
goto err;
|
|
|
|
}
|
2024-05-10 14:04:52 -07:00
|
|
|
|
|
|
|
adt->nr_histograms = nr_entries;
|
2023-12-12 16:13:17 -08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
while (--i >= 0)
|
2024-05-07 00:04:06 -03:00
|
|
|
zfree(&(adt->histograms[i]));
|
|
|
|
zfree(&adt->histograms);
|
2023-12-12 16:13:17 -08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void delete_data_type_histograms(struct annotated_data_type *adt)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < adt->nr_histograms; i++)
|
2024-05-07 00:04:06 -03:00
|
|
|
zfree(&(adt->histograms[i]));
|
2024-05-10 14:04:52 -07:00
|
|
|
|
2024-05-07 00:04:06 -03:00
|
|
|
zfree(&adt->histograms);
|
2024-05-10 14:04:52 -07:00
|
|
|
adt->nr_histograms = 0;
|
2023-12-12 16:13:17 -08:00
|
|
|
}
|
|
|
|
|
perf annotate-data: Add dso->data_types tree
To aggregate accesses to the same data type, add 'data_types' tree in
DSO to maintain data types and find it by name and size.
It might have different data types that happen to have the same name,
so it also compares the size of the type.
Even if it doesn't 100% guarantee, it reduces the possibility of
mis-handling of such conflicts.
And I don't think it's common to have different types with the same
name.
Committer notes:
Very few cases on the Linux kernel, but there are some different types
with the same name, unsure if there is a debug mode in libbpf dedup that
warns about such cases, but there are provisions in pahole for that,
see:
"emit: Notice type shadowing, i.e. multiple types with the same name (enum, struct, union, etc)"
https://git.kernel.org/pub/scm/devel/pahole/pahole.git/commit/?id=4f332dbfd02072e4f410db7bdcda8d6e3422974b
$ pahole --compile > vmlinux.h
$ rm -f a ; make a
cc a.c -o a
$ grep __[0-9] vmlinux.h
union irte__1 {
struct map_info__1;
struct map_info__1 {
struct map_info__1 * next; /* 0 8 */
$
drivers/iommu/amd/amd_iommu_types.h 'union irte'
include/linux/dmar.h 'struct irte'
include/linux/device-mapper.h:
union map_info {
void *ptr;
};
include/linux/mtd/map.h:
struct map_info {
const char *name;
unsigned long size;
resource_size_t phys;
<SNIP>
kernel/events/uprobes.c:
struct map_info {
struct map_info *next;
struct mm_struct *mm;
unsigned long vaddr;
};
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-5-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:10 -08:00
|
|
|
void annotated_data_type__tree_delete(struct rb_root *root)
|
|
|
|
{
|
|
|
|
struct annotated_data_type *pos;
|
|
|
|
|
|
|
|
while (!RB_EMPTY_ROOT(root)) {
|
|
|
|
struct rb_node *node = rb_first(root);
|
|
|
|
|
|
|
|
rb_erase(node, root);
|
|
|
|
pos = rb_entry(node, struct annotated_data_type, node);
|
2023-12-12 16:13:16 -08:00
|
|
|
delete_members(&pos->self);
|
2023-12-12 16:13:17 -08:00
|
|
|
delete_data_type_histograms(pos);
|
2024-05-07 00:04:06 -03:00
|
|
|
zfree(&pos->self.type_name);
|
perf annotate-data: Add dso->data_types tree
To aggregate accesses to the same data type, add 'data_types' tree in
DSO to maintain data types and find it by name and size.
It might have different data types that happen to have the same name,
so it also compares the size of the type.
Even if it doesn't 100% guarantee, it reduces the possibility of
mis-handling of such conflicts.
And I don't think it's common to have different types with the same
name.
Committer notes:
Very few cases on the Linux kernel, but there are some different types
with the same name, unsure if there is a debug mode in libbpf dedup that
warns about such cases, but there are provisions in pahole for that,
see:
"emit: Notice type shadowing, i.e. multiple types with the same name (enum, struct, union, etc)"
https://git.kernel.org/pub/scm/devel/pahole/pahole.git/commit/?id=4f332dbfd02072e4f410db7bdcda8d6e3422974b
$ pahole --compile > vmlinux.h
$ rm -f a ; make a
cc a.c -o a
$ grep __[0-9] vmlinux.h
union irte__1 {
struct map_info__1;
struct map_info__1 {
struct map_info__1 * next; /* 0 8 */
$
drivers/iommu/amd/amd_iommu_types.h 'union irte'
include/linux/dmar.h 'struct irte'
include/linux/device-mapper.h:
union map_info {
void *ptr;
};
include/linux/mtd/map.h:
struct map_info {
const char *name;
unsigned long size;
resource_size_t phys;
<SNIP>
kernel/events/uprobes.c:
struct map_info {
struct map_info *next;
struct mm_struct *mm;
unsigned long vaddr;
};
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: linux-toolchains@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Link: https://lore.kernel.org/r/20231213001323.718046-5-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-12-12 16:13:10 -08:00
|
|
|
free(pos);
|
|
|
|
}
|
|
|
|
}
|
2023-12-12 16:13:17 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* annotated_data_type__update_samples - Update histogram
|
|
|
|
* @adt: Data type to update
|
|
|
|
* @evsel: Event to update
|
|
|
|
* @offset: Offset in the type
|
|
|
|
* @nr_samples: Number of samples at this offset
|
|
|
|
* @period: Event count at this offset
|
|
|
|
*
|
|
|
|
* This function updates type histogram at @ofs for @evsel. Samples are
|
|
|
|
* aggregated before calling this function so it can be called with more
|
|
|
|
* than one samples at a certain offset.
|
|
|
|
*/
|
|
|
|
int annotated_data_type__update_samples(struct annotated_data_type *adt,
|
|
|
|
struct evsel *evsel, int offset,
|
|
|
|
int nr_samples, u64 period)
|
|
|
|
{
|
|
|
|
struct type_hist *h;
|
|
|
|
|
|
|
|
if (adt == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (adt->histograms == NULL) {
|
|
|
|
int nr = evsel->evlist->core.nr_entries;
|
|
|
|
|
|
|
|
if (alloc_data_type_histograms(adt, nr) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset < 0 || offset >= adt->self.size)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
h = adt->histograms[evsel->core.idx];
|
|
|
|
|
|
|
|
h->nr_samples += nr_samples;
|
|
|
|
h->addr[offset].nr_samples += nr_samples;
|
|
|
|
h->period += period;
|
|
|
|
h->addr[offset].period += period;
|
|
|
|
return 0;
|
|
|
|
}
|
2024-04-10 20:32:52 -07:00
|
|
|
|
|
|
|
static void print_annotated_data_header(struct hist_entry *he, struct evsel *evsel)
|
|
|
|
{
|
|
|
|
struct dso *dso = map__dso(he->ms.map);
|
|
|
|
int nr_members = 1;
|
|
|
|
int nr_samples = he->stat.nr_events;
|
|
|
|
int width = 7;
|
|
|
|
const char *val_hdr = "Percent";
|
|
|
|
|
|
|
|
if (evsel__is_group_event(evsel)) {
|
|
|
|
struct hist_entry *pair;
|
|
|
|
|
|
|
|
list_for_each_entry(pair, &he->pairs.head, pairs.node)
|
|
|
|
nr_samples += pair->stat.nr_events;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Annotate type: '%s' in %s (%d samples):\n",
|
2024-05-04 14:38:01 -07:00
|
|
|
he->mem_type->self.type_name, dso__name(dso), nr_samples);
|
2024-04-10 20:32:52 -07:00
|
|
|
|
|
|
|
if (evsel__is_group_event(evsel)) {
|
|
|
|
struct evsel *pos;
|
|
|
|
int i = 0;
|
|
|
|
|
2024-08-06 23:17:13 -07:00
|
|
|
nr_members = 0;
|
|
|
|
for_each_group_evsel(pos, evsel) {
|
|
|
|
if (symbol_conf.skip_empty &&
|
|
|
|
evsel__hists(pos)->stats.nr_samples == 0)
|
|
|
|
continue;
|
2024-04-10 20:32:52 -07:00
|
|
|
|
2024-08-06 23:17:13 -07:00
|
|
|
printf(" event[%d] = %s\n", i++, pos->name);
|
|
|
|
nr_members++;
|
|
|
|
}
|
2024-04-10 20:32:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (symbol_conf.show_total_period) {
|
|
|
|
width = 11;
|
|
|
|
val_hdr = "Period";
|
|
|
|
} else if (symbol_conf.show_nr_samples) {
|
|
|
|
width = 7;
|
|
|
|
val_hdr = "Samples";
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("============================================================================\n");
|
|
|
|
printf("%*s %10s %10s %s\n", (width + 1) * nr_members, val_hdr,
|
|
|
|
"offset", "size", "field");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_annotated_data_value(struct type_hist *h, u64 period, int nr_samples)
|
|
|
|
{
|
|
|
|
double percent = h->period ? (100.0 * period / h->period) : 0;
|
|
|
|
const char *color = get_percent_color(percent);
|
|
|
|
|
|
|
|
if (symbol_conf.show_total_period)
|
|
|
|
color_fprintf(stdout, color, " %11" PRIu64, period);
|
|
|
|
else if (symbol_conf.show_nr_samples)
|
|
|
|
color_fprintf(stdout, color, " %7d", nr_samples);
|
|
|
|
else
|
|
|
|
color_fprintf(stdout, color, " %7.2f", percent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_annotated_data_type(struct annotated_data_type *mem_type,
|
|
|
|
struct annotated_member *member,
|
|
|
|
struct evsel *evsel, int indent)
|
|
|
|
{
|
|
|
|
struct annotated_member *child;
|
|
|
|
struct type_hist *h = mem_type->histograms[evsel->core.idx];
|
2024-08-06 23:17:13 -07:00
|
|
|
int i, nr_events = 0, samples = 0;
|
2024-04-10 20:32:52 -07:00
|
|
|
u64 period = 0;
|
|
|
|
int width = symbol_conf.show_total_period ? 11 : 7;
|
2024-08-06 23:17:13 -07:00
|
|
|
struct evsel *pos;
|
2024-04-10 20:32:52 -07:00
|
|
|
|
2024-08-06 23:17:13 -07:00
|
|
|
for_each_group_evsel(pos, evsel) {
|
|
|
|
h = mem_type->histograms[pos->core.idx];
|
2024-04-10 20:32:52 -07:00
|
|
|
|
2024-08-06 23:17:13 -07:00
|
|
|
if (symbol_conf.skip_empty &&
|
|
|
|
evsel__hists(pos)->stats.nr_samples == 0)
|
|
|
|
continue;
|
2024-04-10 20:32:52 -07:00
|
|
|
|
2024-08-06 23:17:13 -07:00
|
|
|
samples = 0;
|
|
|
|
period = 0;
|
|
|
|
for (i = 0; i < member->size; i++) {
|
|
|
|
samples += h->addr[member->offset + i].nr_samples;
|
|
|
|
period += h->addr[member->offset + i].period;
|
2024-04-10 20:32:52 -07:00
|
|
|
}
|
2024-08-06 23:17:13 -07:00
|
|
|
print_annotated_data_value(h, period, samples);
|
|
|
|
nr_events++;
|
2024-04-10 20:32:52 -07:00
|
|
|
}
|
|
|
|
|
2024-08-19 16:36:02 -07:00
|
|
|
printf(" %#10x %#10x %*s%s\t%s",
|
2024-04-10 20:32:52 -07:00
|
|
|
member->offset, member->size, indent, "", member->type_name,
|
|
|
|
member->var_name ?: "");
|
|
|
|
|
|
|
|
if (!list_empty(&member->children))
|
|
|
|
printf(" {\n");
|
|
|
|
|
|
|
|
list_for_each_entry(child, &member->children, node)
|
|
|
|
print_annotated_data_type(mem_type, child, evsel, indent + 4);
|
|
|
|
|
|
|
|
if (!list_empty(&member->children))
|
|
|
|
printf("%*s}", (width + 1) * nr_events + 24 + indent, "");
|
|
|
|
printf(";\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int hist_entry__annotate_data_tty(struct hist_entry *he, struct evsel *evsel)
|
|
|
|
{
|
|
|
|
print_annotated_data_header(he, evsel);
|
|
|
|
print_annotated_data_type(he->mem_type, &he->mem_type->self, evsel, 0);
|
|
|
|
printf("\n");
|
|
|
|
|
2024-04-10 20:32:53 -07:00
|
|
|
/* move to the next entry */
|
|
|
|
return '>';
|
2024-04-10 20:32:52 -07:00
|
|
|
}
|