Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add drgn_error_catch() - take 2 #456

Merged
merged 2 commits into from
Dec 18, 2024
Merged

Conversation

brenns10
Copy link
Contributor

I had opened a PR a while back for a new static inline, drgn_error_catch(), that makes it easier to catch all errors of a specific code, and destroy them. I tried to convert a bunch of users in that PR which made it go stale super quick. I don't really think that it's necessary to convert a bunch all in one go. So this PR just adds the helper, and does an example conversion in libdrgn/linux_kernel_helpers.c. That ensures that we can see how it's used and catch any errors, but it also won't conflict with the module API branch (I double-checked), and shouldn't go stale so quickly.

I think this would be a nice thing to have in general. It's also related to some of the preparatory commits I have for the CTF branch, and I figured it would be nice to keep closer to upstream by adding this now.

@osandov
Copy link
Owner

osandov commented Dec 17, 2024

Looks good to me conceptually, although there's a build error (which apparently makes the CI action get stuck. Oops.)

@brenns10
Copy link
Contributor Author

How embarrassing, I swear I built and ran this, but I must not have had the linux_kernel_helpers.c changes applied yet. I've fixed the build error and now actually tested loading a vmcore and /proc/kcore with this, as well as running vmtests against 6.12.

Copy link
Owner

@osandov osandov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I just noticed that the new function is in drgn.h. Do you need it to be public? If not, let's move it to be internal in error.h. Other than that, LGTM.

@brenns10
Copy link
Contributor Author

Nope, I don't need a public API (we don't currently even release drgn-devel). I'll move it!

Catching errors is a relatively common practice. But to do it correctly,
users should actually look at the error code and may need to use
drgn_error_destroy(). For some kinds of errors, (e.g. lookup errors,
which commonly use &drgn_not_found), it feels more convenient to
directly compare error pointers, but that may leave some errors
unhandled.

Introduce drgn_error_catch(), which makes it more convenient to catch
errors correctly. Its intended use is to convert constructs like (1)
and (2) below, to (3):

  // (1)
  err = some_function();
  if (err->code == DRGN_ERROR_LOOKUP) {
    drgn_error_destroy(err);
    err = NULL;
    // do something else
  }
  // (2)
  err = some_function();
  if (err == &drgn_not_found) {
    err = NULL;
    // do something else
  }
  // (3)
  err = some_function();
  if (drgn_error_catch(&err, DRGN_ERROR_LOOKUP)) {
    // do something else
  }

Signed-off-by: Stephen Brennan <[email protected]>
As an example of the functionality, refactor the lookup error handling
here to use drgn_error_catch().

Signed-off-by: Stephen Brennan <[email protected]>
@osandov osandov merged commit 7ad4022 into osandov:main Dec 18, 2024
27 of 34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants