[PATCH v2] pahole: Apply CU-level filters early in loading

Matthew Maurer
2024-08-01T18:50:57Z

Without this, even with `--lang_exclude=rust` set, running on `vmlinux`
with `CONFIG_RUST` enabled will lead to errors like:
die__process_function: tag not supported 0x2f (template_type_parameter)!
because the filtering doesn't happen until finalization, but unsupported
tags are reported during loading.

As an added bonus, this should speed up processing of large objects with
filtered CUs, as their details will no longer be walked.

Signed-off-by: Matthew Maurer <mmaurer@google.com>
---
dwarf_loader.c | 10 ++++++++++
dwarves.h | 1 +
pahole.c | 4 ++++
3 files changed, 15 insertions(+)

diff --git a/dwarf_loader.c b/dwarf_loader.c
index b832c93..02a17f4 100644
--- a/dwarf_loader.c
+++ b/dwarf_loader.c
@@ -2854,6 +2854,16 @@ static int die__process(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)

cu->language = attr_numeric(die, DW_AT_language);

+ if (conf->early_cu_filter)
+ cu = conf->early_cu_filter(cu);
+
+ /*
+ * If we filtered this CU out, we still want to keep iterating, but
+ * there's no need to walk the rest of the CU info.
+ */
+ if (cu == NULL)
+ return DWARF_CB_OK;
+
if (dwarf_child(die, &child) == 0) {
int err = die__process_unit(&child, cu, conf);
if (err)
diff --git a/dwarves.h b/dwarves.h
index f5ae79f..92d102b 100644
--- a/dwarves.h
+++ b/dwarves.h
@@ -72,6 +72,7 @@ struct conf_load {
enum load_steal_kind (*steal)(struct cu *cu,
struct conf_load *conf,
void *thr_data);
+ struct cu * (*early_cu_filter)(struct cu *cu);
int (*thread_exit)(struct conf_load *conf, void *thr_data);
void *cookie;
char *format_path;
diff --git a/pahole.c b/pahole.c
index 954498d..42a9c16 100644
--- a/pahole.c
+++ b/pahole.c
@@ -3765,6 +3765,10 @@ int main(int argc, char *argv[])
memset(tab, ' ', sizeof(tab) - 1);

conf_load.steal = pahole_stealer;
+
+ if (languages.exclude)
+ conf_load.early_cu_filter = cu__filter;
+
conf_load.thread_exit = pahole_thread_exit;

if (conf_load.reproducible_build) {
--
2.46.0.rc2.264.g509ed76dc8-goog