From 7ad40224efdaa8f0138cf22399849e3795f5748c Mon Sep 17 00:00:00 2001 From: Stephen Brennan Date: Mon, 16 Dec 2024 16:10:41 -0800 Subject: [PATCH] linux_kernel_helpers: use drgn_error_catch() As an example of the functionality, refactor the lookup error handling here to use drgn_error_catch(). Signed-off-by: Stephen Brennan --- libdrgn/linux_kernel_helpers.c | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/libdrgn/linux_kernel_helpers.c b/libdrgn/linux_kernel_helpers.c index e6a3a3989..7a8ae0ac5 100644 --- a/libdrgn/linux_kernel_helpers.c +++ b/libdrgn/linux_kernel_helpers.c @@ -95,10 +95,9 @@ struct drgn_error *linux_helper_direct_mapping_offset(struct drgn_program *prog, uint64_t virt_addr; if (!err) { err = drgn_object_read_unsigned(&tmp, &virt_addr); - } else if (err->code == DRGN_ERROR_LOOKUP) { + } else if (drgn_error_catch(&err, DRGN_ERROR_LOOKUP)) { // Avoid a confusing error message with our arbitrary variable // name. - drgn_error_destroy(err); err = drgn_error_create(DRGN_ERROR_OTHER, "could not find variable in direct mapping"); } @@ -248,8 +247,7 @@ struct drgn_error *linux_helper_per_cpu_ptr(struct drgn_object *res, drgn_object_qualified_type(ptr), ptr_value + per_cpu_offset.uvalue, 0); - } else if (err->code == DRGN_ERROR_LOOKUP) { - drgn_error_destroy(err); + } else if (drgn_error_catch(&err, DRGN_ERROR_LOOKUP)) { return drgn_object_copy(res, ptr); } else { return err; @@ -299,9 +297,8 @@ struct drgn_error *linux_helper_task_thread_info(struct drgn_object *res, if (!err) { // CONFIG_THREAD_INFO_IN_TASK=y return drgn_object_address_of(res, &tmp); - } else if (err->code == DRGN_ERROR_LOOKUP) { + } else if (drgn_error_catch(&err, DRGN_ERROR_LOOKUP)) { // CONFIG_THREAD_INFO_IN_TASK=n - drgn_error_destroy(err); err = drgn_object_member_dereference(&tmp, task, "stack"); if (err) return err; @@ -340,8 +337,7 @@ struct drgn_error *linux_helper_task_cpu(const struct drgn_object *task, if (err) return err; err = drgn_object_member_dereference(&tmp, &tmp, "cpu"); - if (err && err->code == DRGN_ERROR_LOOKUP) { - drgn_error_destroy(err); + if (drgn_error_catch(&err, DRGN_ERROR_LOOKUP)) { err = drgn_object_member_dereference(&tmp, task, "cpu"); } if (!err) { @@ -349,11 +345,9 @@ struct drgn_error *linux_helper_task_cpu(const struct drgn_object *task, err = drgn_object_read_integer(&tmp, &value); if (!err) *ret = value.uvalue; - } else if (err->code == DRGN_ERROR_LOOKUP) { + } else if (drgn_error_catch(&err, DRGN_ERROR_LOOKUP)) { // CONFIG_SMP=n - drgn_error_destroy(err); *ret = 0; - err = NULL; } return err; } @@ -387,8 +381,7 @@ linux_helper_xa_load(struct drgn_object *res, return err; internal_flag = 2; node_min = 4097; - } else if (err->code == DRGN_ERROR_LOOKUP) { - drgn_error_destroy(err); + } else if (drgn_error_catch(&err, DRGN_ERROR_LOOKUP)) { // entry = (void *)xa->rnode err = drgn_object_member_dereference(&entry, xa, "rnode"); if (err) @@ -587,10 +580,8 @@ struct drgn_error *linux_helper_idr_find(struct drgn_object *res, if (err) return err; id -= idr_base.uvalue; - } else if (err->code == DRGN_ERROR_LOOKUP) { + } else if (!drgn_error_catch(&err, DRGN_ERROR_LOOKUP)) { /* idr_base was added in v4.16. */ - drgn_error_destroy(err); - } else { return err; } @@ -787,8 +778,7 @@ struct drgn_error *linux_helper_find_pid(struct drgn_object *res, if (err) return err; return drgn_object_cast(res, qualified_type, &tmp); - } else if (err->code == DRGN_ERROR_LOOKUP) { - drgn_error_destroy(err); + } else if (drgn_error_catch(&err, DRGN_ERROR_LOOKUP)) { err = drgn_program_find_object(drgn_object_program(res), "pid_hash", NULL, DRGN_FIND_OBJECT_ANY, &tmp); @@ -851,8 +841,7 @@ struct drgn_error *linux_helper_pid_task(struct drgn_object *res, + 1]; snprintf(member, sizeof(member), PID_LINKS_FORMAT, pid_type); err = drgn_object_container_of(res, &first, task_struct_type, member); - if (err && err->code == DRGN_ERROR_LOOKUP) { - drgn_error_destroy(err); + if (drgn_error_catch(&err, DRGN_ERROR_LOOKUP)) { /* container_of(first, struct task_struct, pids[pid_type].node) */ snprintf(member, sizeof(member), PIDS_NODE_FORMAT, pid_type); #undef PID_LINKS_FORMAT