RFR: 8367149: Add ergonomic construction for creating ad-hoc VMErrorCallback [v2]
Axel Boldt-Christmas
aboldtch at openjdk.org
Thu Sep 11 05:37:20 UTC 2025
On Wed, 10 Sep 2025 14:17:42 GMT, Axel Boldt-Christmas <aboldtch at openjdk.org> wrote:
>> Add a class OnVMError which uses the VMErrorCallback mechanism which is an ergonomic construction for creating ad-hoc VMErrorCallback which automatically calls the provided invocable f if a VM crash occurs within its lifetime. Can be used to instrument a build for more detailed contextual information gathering. Especially useful when hunting down intermittent bugs, or issues only reproducible in environments where access to a debugger is not readily available. Example use:
>> ```C++
>> {
>> // Note the lambda is invoked after an error occurs within this thread,
>> // and during on_error's lifetime. If state prior to the crash is required,
>> // capture a copy of it first.
>> auto important_value = get_the_value();
>>
>> OnVMError on_error([&](outputStream* st) {
>> // Dump the important bits.
>> st->print("Prior value: ");
>> important_value.print_on(st);
>> st->print("During crash: ")
>> get_the_value().print_on(st);
>> // Dump whole the whole state.
>> this->print_on(st);
>> });
>>
>> // Sometimes doing a thing will crash the VM.
>> do_a_thing();
>> }
>>
>>
>> C++17 class template argument deduction finally makes these sort of constructions ergonomic to use without the need for auto and helper construction methods.
>
> Axel Boldt-Christmas has updated the pull request incrementally with three additional commits since the last revision:
>
> - Add a comment explaining the deduction rules
> - Skip multiple inheritance and allow more than lambda like callables.
> - Update doc example
I initially intended for this to just be allowed to take a lambda like temporary and just use that. But after looking at it some more and using it a bit I think it is fine to extend this to anything which behaves like a callable (via operator()(outputStream*)) and can be move/copy constructed. So you can now also use a lvalue lambda or function pointer.
Felt like it would be the least surprising outcome when trying to use this class. Even though using it as `OnVMError(<lambda type>&&)` is the expected use case (as presented in the example).
-------------
PR Comment: https://git.openjdk.org/jdk/pull/27159#issuecomment-3277907074
More information about the hotspot-dev
mailing list