diff options
author | Kaiyi Li <kaiyili@google.com> | 2022-01-06 15:18:18 -0800 |
---|---|---|
committer | Kaiyi Li <kaiyili@google.com> | 2022-01-12 16:57:37 -0800 |
commit | 5172a053f779cb2e0b40e05372de70af543b93de (patch) | |
tree | 9d772a48844da0eb6fd6a28f62901a62009a6520 /host-common | |
parent | 4935d31497fdec9756f278cf04fad887ee28f6c5 (diff) | |
download | vulkan-cereal-5172a053f779cb2e0b40e05372de70af543b93de.tar.gz |
Customized abort in gfxstream
Allow to set a customized abort function to be called when fatal.
Change-Id: I3b1a65798582910916d583678a44e951b02adf34
Diffstat (limited to 'host-common')
-rw-r--r-- | host-common/GfxstreamFatalError.cpp | 10 | ||||
-rw-r--r-- | host-common/GfxstreamFatalError.h | 5 | ||||
-rw-r--r-- | host-common/GfxstreamFatalError_unittest.cpp | 12 |
3 files changed, 23 insertions, 4 deletions
diff --git a/host-common/GfxstreamFatalError.cpp b/host-common/GfxstreamFatalError.cpp index 18bdd7c4..19b49732 100644 --- a/host-common/GfxstreamFatalError.cpp +++ b/host-common/GfxstreamFatalError.cpp @@ -11,10 +11,13 @@ namespace { using android::base::CreateMetricsLogger; using android::base::GfxstreamVkAbort; +std::optional<std::function<void()>> customDieFunction = std::nullopt; [[noreturn]] void die() { - // Note: we do this in this die() function instead of just calling abort() directly so that - // downstream users of gfxstream can easily substitute their own logic. - abort(); + if (customDieFunction) { + (*customDieFunction)(); + } else { + abort(); + } } } // namespace @@ -38,4 +41,5 @@ AbortMessage::~AbortMessage() { die(); } +void setDieFunction(std::optional<std::function<void()>> newDie) { customDieFunction = newDie; } } // namespace emugl diff --git a/host-common/GfxstreamFatalError.h b/host-common/GfxstreamFatalError.h index 17671f64..2d4cc841 100644 --- a/host-common/GfxstreamFatalError.h +++ b/host-common/GfxstreamFatalError.h @@ -17,6 +17,8 @@ #include <vulkan/vulkan.h> #include <cstdint> +#include <functional> +#include <optional> #include <sstream> namespace emugl { @@ -56,6 +58,9 @@ class AbortMessage { std::ostringstream mOss; }; +// A function that terminates the process should be passed in. When calling the GFXSTREAM_ABORT +// macro, the set function will be used to terminate the process instead of std::abort. +void setDieFunction(std::optional<std::function<void()>> newDie); } // namespace emugl #define GFXSTREAM_ABORT(reason) ::emugl::AbortMessage(__FILE__, __func__, __LINE__, reason).stream() diff --git a/host-common/GfxstreamFatalError_unittest.cpp b/host-common/GfxstreamFatalError_unittest.cpp index ba031d25..48d11ad1 100644 --- a/host-common/GfxstreamFatalError_unittest.cpp +++ b/host-common/GfxstreamFatalError_unittest.cpp @@ -29,5 +29,15 @@ TEST(GFXSTREAM_ABORT, WithVkResult) { R"re(err code: -1000161000: so fragmented)re"); } +TEST(GFXSTREAM_ABORT, WithCustomizedDeathFunction) { + ::testing::MockFunction<void()> customDie; + + EXPECT_CALL(customDie, Call()); + + setDieFunction(customDie.AsStdFunction()); + GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER)); + + setDieFunction(std::nullopt); +} } // namespace -} // namespace emugl
\ No newline at end of file +} // namespace emugl |