perf dwarf-aux: Add die_get_cfa()
authorNamhyung Kim <namhyung@kernel.org>
Wed, 17 Jan 2024 06:26:55 +0000 (22:26 -0800)
committerNamhyung Kim <namhyung@kernel.org>
Mon, 22 Jan 2024 20:08:20 +0000 (12:08 -0800)
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.

Reviewed-by: Ian Rogers <irogers@google.com>
Cc: Stephane Eranian <eranian@google.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Link: https://lore.kernel.org/r/20240117062657.985479-8-namhyung@kernel.org
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/util/dwarf-aux.c
tools/perf/util/dwarf-aux.h

index 7aa5fee0da1906a073ac9423305bf26deb569761..3d42a8613869897ce5e0cbdab91310067a31d47a 100644 (file)
@@ -1407,7 +1407,73 @@ Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die, Dwarf_Addr pc,
                *offset = data.offset;
        return result;
 }
-#endif
+#endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT */
+
+#ifdef HAVE_DWARF_CFI_SUPPORT
+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 /* HAVE_DWARF_CFI_SUPPORT */
 
 /*
  * die_has_loclist - Check if DW_AT_location of @vr_die is a location list
index 4e64caac6df83ea5ba292225894206b450d63222..f209f91629088bfc3073867453c065034a9a0e5b 100644 (file)
@@ -177,4 +177,19 @@ static inline Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die __maybe_unu
 
 #endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT */
 
+#ifdef HAVE_DWARF_CFI_SUPPORT
+
+/* Get the frame base information from CFA */
+int die_get_cfa(Dwarf *dwarf, u64 pc, int *preg, int *poffset);
+
+#else /* HAVE_DWARF_CFI_SUPPORT */
+
+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_CFI_SUPPORT */
+
 #endif /* _DWARF_AUX_H */