[31/48] perf dwarf-aux: Add die_get_cfa()

Message ID 20231012035111.676789-32-namhyung@kernel.org
State New
Headers
Series perf tools: Introduce data type profiling (v1) |

Commit Message

Namhyung Kim Oct. 12, 2023, 3:50 a.m. UTC
  The die_get_cfa() is to get frame base register and offset at the given
instruction address (pc).  This info will be used to locate stack
variables which have location expression using DW_OP_fbreg.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/dwarf-aux.c | 64 +++++++++++++++++++++++++++++++++++++
 tools/perf/util/dwarf-aux.h |  9 ++++++
 2 files changed, 73 insertions(+)
  

Comments

Masami Hiramatsu (Google) Nov. 7, 2023, 12:50 a.m. UTC | #1
On Wed, 11 Oct 2023 20:50:54 -0700
Namhyung Kim <namhyung@kernel.org> wrote:

> The die_get_cfa() is to get frame base register and offset at the given
> instruction address (pc).  This info will be used to locate stack
> variables which have location expression using DW_OP_fbreg.
> 

In the util/probe-finder.c, I added the elfutils version checker

#if _ELFUTILS_PREREQ(0, 142)

for CFI related code, in commit 7752f1b096e1 ("perf probe: Don't
 compile CFI related code if elfutils is old"). Maybe we'd better to have
a config of HAVE_DWARF_CFI_SUPPORT for this.

Thank you,


> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> ---
>  tools/perf/util/dwarf-aux.c | 64 +++++++++++++++++++++++++++++++++++++
>  tools/perf/util/dwarf-aux.h |  9 ++++++
>  2 files changed, 73 insertions(+)
> 
> diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c
> index 97d9ae56350e..796413eb4e8f 100644
> --- a/tools/perf/util/dwarf-aux.c
> +++ b/tools/perf/util/dwarf-aux.c
> @@ -1408,6 +1408,70 @@ Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die, Dwarf_Addr pc,
>  	return result;
>  }
>  
> +static int reg_from_dwarf_op(Dwarf_Op *op)
> +{
> +	switch (op->atom) {
> +	case DW_OP_reg0 ... DW_OP_reg31:
> +		return op->atom - DW_OP_reg0;
> +	case DW_OP_breg0 ... DW_OP_breg31:
> +		return op->atom - DW_OP_breg0;
> +	case DW_OP_regx:
> +	case DW_OP_bregx:
> +		return op->number;
> +	default:
> +		break;
> +	}
> +	return -1;
> +}
> +
> +static int offset_from_dwarf_op(Dwarf_Op *op)
> +{
> +	switch (op->atom) {
> +	case DW_OP_reg0 ... DW_OP_reg31:
> +	case DW_OP_regx:
> +		return 0;
> +	case DW_OP_breg0 ... DW_OP_breg31:
> +		return op->number;
> +	case DW_OP_bregx:
> +		return op->number2;
> +	default:
> +		break;
> +	}
> +	return -1;
> +}
> +
> +/**
> + * die_get_cfa - Get frame base information
> + * @dwarf: a Dwarf info
> + * @pc: program address
> + * @preg: pointer for saved register
> + * @poffset: pointer for saved offset
> + *
> + * This function gets register and offset for CFA (Canonical Frame Address)
> + * by searching the CIE/FDE info.  The CFA usually points to the start address
> + * of the current stack frame and local variables can be located using an offset
> + * from the CFA.  The @preg and @poffset will be updated if it returns 0.
> + */
> +int die_get_cfa(Dwarf *dwarf, u64 pc, int *preg, int *poffset)
> +{
> +	Dwarf_CFI *cfi;
> +	Dwarf_Frame *frame = NULL;
> +	Dwarf_Op *ops = NULL;
> +	size_t nops;
> +
> +	cfi = dwarf_getcfi(dwarf);
> +	if (cfi == NULL)
> +		return -1;
> +
> +	if (!dwarf_cfi_addrframe(cfi, pc, &frame) &&
> +	    !dwarf_frame_cfa(frame, &ops, &nops) && nops == 1) {
> +		*preg = reg_from_dwarf_op(ops);
> +		*poffset = offset_from_dwarf_op(ops);
> +		return 0;
> +	}
> +	return -1;
> +}
> +
>  #endif
>  
>  /*
> diff --git a/tools/perf/util/dwarf-aux.h b/tools/perf/util/dwarf-aux.h
> index 742098e3ee7e..29a7243b1a45 100644
> --- a/tools/perf/util/dwarf-aux.h
> +++ b/tools/perf/util/dwarf-aux.h
> @@ -149,6 +149,9 @@ Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die, Dwarf_Addr pc,
>  				     Dwarf_Addr addr, Dwarf_Die *die_mem,
>  				     int *offset);
>  
> +/* Get the frame base information from CFA */
> +int die_get_cfa(Dwarf *dwarf, u64 pc, int *preg, int *poffset);
> +
>  #else /*  HAVE_DWARF_GETLOCATIONS_SUPPORT */
>  
>  static inline int die_get_var_range(Dwarf_Die *sp_die __maybe_unused,
> @@ -175,6 +178,12 @@ static inline Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die __maybe_unu
>  	return NULL;
>  }
>  
> +static inline int die_get_cfa(Dwarf *dwarf __maybe_unused, u64 pc __maybe_unused,
> +			      int *preg __maybe_unused, int *poffset __maybe_unused)
> +{
> +	return -1;
> +}
> +
>  #endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT */
>  
>  #endif /* _DWARF_AUX_H */
> -- 
> 2.42.0.655.g421f12c284-goog
>
  
Namhyung Kim Nov. 8, 2023, 5:28 a.m. UTC | #2
On Mon, Nov 6, 2023 at 4:50 PM Masami Hiramatsu <mhiramat@kernel.org> wrote:
>
> On Wed, 11 Oct 2023 20:50:54 -0700
> Namhyung Kim <namhyung@kernel.org> wrote:
>
> > The die_get_cfa() is to get frame base register and offset at the given
> > instruction address (pc).  This info will be used to locate stack
> > variables which have location expression using DW_OP_fbreg.
> >
>
> In the util/probe-finder.c, I added the elfutils version checker
>
> #if _ELFUTILS_PREREQ(0, 142)
>
> for CFI related code, in commit 7752f1b096e1 ("perf probe: Don't
>  compile CFI related code if elfutils is old"). Maybe we'd better to have
> a config of HAVE_DWARF_CFI_SUPPORT for this.

Sounds like a good idea.  Will add.

Thanks,
Namhyung
  

Patch

diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c
index 97d9ae56350e..796413eb4e8f 100644
--- a/tools/perf/util/dwarf-aux.c
+++ b/tools/perf/util/dwarf-aux.c
@@ -1408,6 +1408,70 @@  Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die, Dwarf_Addr pc,
 	return result;
 }
 
+static int reg_from_dwarf_op(Dwarf_Op *op)
+{
+	switch (op->atom) {
+	case DW_OP_reg0 ... DW_OP_reg31:
+		return op->atom - DW_OP_reg0;
+	case DW_OP_breg0 ... DW_OP_breg31:
+		return op->atom - DW_OP_breg0;
+	case DW_OP_regx:
+	case DW_OP_bregx:
+		return op->number;
+	default:
+		break;
+	}
+	return -1;
+}
+
+static int offset_from_dwarf_op(Dwarf_Op *op)
+{
+	switch (op->atom) {
+	case DW_OP_reg0 ... DW_OP_reg31:
+	case DW_OP_regx:
+		return 0;
+	case DW_OP_breg0 ... DW_OP_breg31:
+		return op->number;
+	case DW_OP_bregx:
+		return op->number2;
+	default:
+		break;
+	}
+	return -1;
+}
+
+/**
+ * die_get_cfa - Get frame base information
+ * @dwarf: a Dwarf info
+ * @pc: program address
+ * @preg: pointer for saved register
+ * @poffset: pointer for saved offset
+ *
+ * This function gets register and offset for CFA (Canonical Frame Address)
+ * by searching the CIE/FDE info.  The CFA usually points to the start address
+ * of the current stack frame and local variables can be located using an offset
+ * from the CFA.  The @preg and @poffset will be updated if it returns 0.
+ */
+int die_get_cfa(Dwarf *dwarf, u64 pc, int *preg, int *poffset)
+{
+	Dwarf_CFI *cfi;
+	Dwarf_Frame *frame = NULL;
+	Dwarf_Op *ops = NULL;
+	size_t nops;
+
+	cfi = dwarf_getcfi(dwarf);
+	if (cfi == NULL)
+		return -1;
+
+	if (!dwarf_cfi_addrframe(cfi, pc, &frame) &&
+	    !dwarf_frame_cfa(frame, &ops, &nops) && nops == 1) {
+		*preg = reg_from_dwarf_op(ops);
+		*poffset = offset_from_dwarf_op(ops);
+		return 0;
+	}
+	return -1;
+}
+
 #endif
 
 /*
diff --git a/tools/perf/util/dwarf-aux.h b/tools/perf/util/dwarf-aux.h
index 742098e3ee7e..29a7243b1a45 100644
--- a/tools/perf/util/dwarf-aux.h
+++ b/tools/perf/util/dwarf-aux.h
@@ -149,6 +149,9 @@  Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die, Dwarf_Addr pc,
 				     Dwarf_Addr addr, Dwarf_Die *die_mem,
 				     int *offset);
 
+/* Get the frame base information from CFA */
+int die_get_cfa(Dwarf *dwarf, u64 pc, int *preg, int *poffset);
+
 #else /*  HAVE_DWARF_GETLOCATIONS_SUPPORT */
 
 static inline int die_get_var_range(Dwarf_Die *sp_die __maybe_unused,
@@ -175,6 +178,12 @@  static inline Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die __maybe_unu
 	return NULL;
 }
 
+static inline int die_get_cfa(Dwarf *dwarf __maybe_unused, u64 pc __maybe_unused,
+			      int *preg __maybe_unused, int *poffset __maybe_unused)
+{
+	return -1;
+}
+
 #endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT */
 
 #endif /* _DWARF_AUX_H */