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 custom debug printing for your asserts #15792

Merged
merged 1 commit into from
Apr 10, 2024

Conversation

rincebrain
Copy link
Contributor

@rincebrain rincebrain commented Jan 18, 2024

Motivation and Context

(Recreated because it wouldn't let me reopen #14466.)

I kept rewriting assertions into if(assert condition) { print something; trip assertion; } and went "this probably works".

So now I want to see if the CI explodes on Debian 8 or something.

(Before merge I'd probably refactor the non-f versions into being in terms of the f versions, but this way I can test a much smaller blast radius at a time...also suggestions for better naming welcome.)

(...I also flattened freebsd's debug.h and linux's after noticing they were more or less the same file, but that's mostly orthogonal and just in here because I also want to make sure that compiles everywhere.)

Description

Gives all the ASSERT/VERIFY macros an "f" suffix variant that adds "STR, ..." arguments such that you could do, say
ASSERT3Pf(hdr->b_l1hdr.b_pabd, !=, NULL, "hdr %px buf %px", hdr, buf);
and get an error trip like
VERIFY3(hdr->b_l1hdr.b_pabd != NULL) failed (0000000000000000 != 0000000000000000) hdr 0000000000000000 buf 0000000000000000

The only big caveat is I don't think you can do something like ((void) __VA_ARGS__) so the print varargs won't always get executed (because ASSERT normally grounds out in ((void) x) on non-DEBUG builds), so don't put side effect things in there you need to always run...but that's not a reason not to have it, just a warning.

How Has This Been Tested?

I mean, I compiled it once, what could go wrong, right?

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Performance enhancement (non-breaking change which improves efficiency)
  • Code cleanup (non-breaking change which makes code smaller or more readable)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Library ABI change (libzfs, libzfs_core, libnvpair, libuutil and libzfsbootenv)
  • Documentation (a change to man pages or other documentation)

Checklist:

include/os/freebsd/spl/sys/debug.h Outdated Show resolved Hide resolved
include/os/freebsd/spl/sys/debug.h Outdated Show resolved Hide resolved
@rincebrain rincebrain force-pushed the assertprintf branch 2 times, most recently from e16bc9c to a5aabbd Compare January 19, 2024 00:27
@robn
Copy link
Member

robn commented Jan 19, 2024

Oh, I love this. I write a jank variant of this nearly every day. Nice work!

If sys/debug.h is going to be "the same" on both platforms, should it just be moved to include/sys?

I'll also say that the lowercase f makes me twitch, both mixed caps and macros that aren't caps. I probably would have written VERIFYF etc. But I don't know how strongly I feel about that, and I'm not really gonna argue I think.

@rincebrain
Copy link
Contributor Author

Oh, I love this. I write a jank variant of this nearly every day. Nice work!

If sys/debug.h is going to be "the same" on both platforms, should it just be moved to include/sys?

I was a little worried it might get pulled in by libspl if I do that, and I could coalesce both of those too, but that was why I didn't, was that I mostly wanted to get feedback on the design, and figured that could be implemented later if it worked and people liked the idea.

I'll also say that the lowercase f makes me twitch, both mixed caps and macros that aren't caps. I probably would have written VERIFYF etc. But I don't know how strongly I feel about that, and if I'm not really gonna argue I think.

I don't really like it either, if I'm honest, but since all the uppercase was overloaded with pointer/unsigned/etc, lowercase seemed like the most expressive way to make it clear this wasn't just a different element type. Not that we use floating point around much, but that was why.

Suggestions on alternatives welcome.

@behlendorf behlendorf added the Status: Code Review Needed Ready for review and testing label Jan 19, 2024
@behlendorf
Copy link
Contributor

I'm all for this. Thanks for reviving this work.

For what it's worth, I'd lean slightly towards ASSERTF/VERIFYF instead of ASSERTf/VERIFYf. Lower case letters in macros and mixed case make me a bit queasy, but I could learn to live with it.

I was a little worried it might get pulled in by libspl.

It would be nice if these shared a common file, but it looks to me like that would currently be a problem. We've bumped in to this in a few other places so a generic way to handle it would be good. Maybe a new include/os/generic/spl/ directory? In the short term we could use a symlink, but that's icky too. Either way, that's the kind of thing we could leave to follow up PR.

@behlendorf
Copy link
Contributor

@rincebrain if you can rebase this and sort out the style warnings I'll go ahead and merge it. This is such a nice quality of life improvement I'd hate for it to get hung up on any ASSERTF/VERIFYF vs ASSERTf/VERIFYf minutia. Let's merge it, live with it for a bit, and tweak it if needed.

@rincebrain
Copy link
Contributor Author

Honestly, it wasn't a disagreement about the f versus F for me, I just forgot I never replied or updated this.

I'll go rebase and push.

@amotin
Copy link
Member

amotin commented Feb 5, 2024

I'd hate for it to get hung up on any ASSERTF/VERIFYF vs ASSERTf/VERIFYf minutia

I think it should be much easier to fix the capitalization now than remember about this case oddity forever after.

@rincebrain rincebrain force-pushed the assertprintf branch 2 times, most recently from cfade36 to 1a070b6 Compare February 5, 2024 21:02
@rincebrain
Copy link
Contributor Author

Looking at it quickly, I'm debating what to do about the %p/%px usage, since the x print is kind of ugly on non-Linux platforms, but I don't really want to go around plumbing in a "print less gross" PTR_PRINTF or something for this. Maybe a followup.

@behlendorf
Copy link
Contributor

It seems to me that %px is appropriate here, even if it might be ugly on non-Linux platforms. A followup PR is always an option if it becomes a real annoyance.

@behlendorf
Copy link
Contributor

@rincebrain if you can sort out the remaining build errors I think we'd all love to get a version of this integrated.

@rincebrain
Copy link
Contributor Author

...fascinatingly, my branch locally has no pending changes and even after a make distclean and configure, builds. I wonder what's going on.

@rincebrain
Copy link
Contributor Author

Not really sure what happened, but a fresh clone from the repo into a new dir and the same configure+make breaks as in the repo, now, so that was simple to debug.

@behlendorf
Copy link
Contributor

Strange, but this looks good to me as long as the CI is happy this time around.

@behlendorf behlendorf added Status: Accepted Ready to integrate (reviewed, tested) and removed Status: Code Review Needed Ready for review and testing labels Feb 16, 2024
@rincebrain
Copy link
Contributor Author

I think this is good to go and the centos 7 failure is unrelated.

I just squashed the two fix commits and pushed to see what happens.

@rincebrain
Copy link
Contributor Author

The FBSD tests seem to be the same failure described in #15934, the CentOS 9 tests seem to be bclone broken for every PR atm, and the others seem to be flaky tests, I think?

Someone check me, by all means, but they all seem to be unrelated to this change.

@behlendorf
Copy link
Contributor

@rincebrain can I talk you one to one last rebase, then we can get this merged.

Being able to print custom debug information on assert trip
seems useful.

Signed-off-by: Rich Ercolani <rincebrain@gmail.com>
@behlendorf behlendorf merged commit e5e2a5a into openzfs:master Apr 10, 2024
23 of 25 checks passed
lundman pushed a commit to openzfsonwindows/openzfs that referenced this pull request Sep 4, 2024
Being able to print custom debug information on assert trip
seems useful.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Paul Dagnelie <pcd@delphix.com>
Signed-off-by: Rich Ercolani <rincebrain@gmail.com>
Closes openzfs#15792
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Accepted Ready to integrate (reviewed, tested)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants