CrashManager.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #ifndef HERMES_PUBLIC_CRASHMANAGER_H
  8. #define HERMES_PUBLIC_CRASHMANAGER_H
  9. #include <cstddef>
  10. #include <functional>
  11. namespace hermes {
  12. namespace vm {
  13. /// A CrashManager provides functions that determine what memory and data is
  14. /// included in dumps in case of crashes.
  15. class CrashManager {
  16. public:
  17. /// CallbackKey is the type of an identifier for a callback supplied to the
  18. /// CrashManager.
  19. using CallbackKey = int;
  20. /// Type for the callback function invoked on crash. The fd supplied is a raw
  21. /// file stream an implementation should write a JSON object to.
  22. using CallbackFunc = std::function<void(int fd)>;
  23. /// Registers some memory to be included in any crash dump that occurs.
  24. /// \param mem A pointer to allocated memory. It must be unregistered
  25. /// before being freed.
  26. /// \param length The number of bytes the memory controls.
  27. virtual void registerMemory(void *mem, size_t length) = 0;
  28. /// Unregisters some memory from being included in any crash dump that occurs.
  29. virtual void unregisterMemory(void *mem) = 0;
  30. /// Registers custom data to be included in any crash dump that occurs.
  31. /// Calling \c setCustomData on the same key twice will overwrite the previous
  32. /// value.
  33. /// \param key A tag to look for in the custom data output. Distinguishes
  34. /// between multiple values.
  35. /// \param val The value to store for the given key.
  36. virtual void setCustomData(const char *key, const char *val) = 0;
  37. /// If the given \p key has an associated custom data string, remove the
  38. /// association.
  39. virtual void removeCustomData(const char *key) = 0;
  40. /// Registers a function to be called after a crash has occurred. This
  41. /// function can examine memory and serialize this to a JSON output stream.
  42. /// Implmentations decide where the stream is routed to.
  43. /// \param callback A function to called after a crash.
  44. /// \return A CallbackKey representing the function you provided. Pass this
  45. /// key into unregisterCallback when it that callback is no longer needed.
  46. virtual CallbackKey registerCallback(CallbackFunc callback) = 0;
  47. /// Unregisters a previously registered callback. After this function returns,
  48. /// the previously registered function will not be executed by this
  49. /// CrashManager during a crash.
  50. virtual void unregisterCallback(CallbackKey key) = 0;
  51. /// the heap information.
  52. struct HeapInformation {
  53. /// The amount of memory that is currently in use
  54. size_t used_{0};
  55. /// The amount of memory that can currently be allocated
  56. /// before a full GC is triggered.
  57. size_t size_{0};
  58. };
  59. /// Record the heap information.
  60. /// \param heapInfo The current heap information
  61. virtual void setHeapInfo(const HeapInformation &heapInfo) = 0;
  62. virtual ~CrashManager() {}
  63. };
  64. /// A CrashManager that does nothing.
  65. class NopCrashManager final : public CrashManager {
  66. public:
  67. void registerMemory(void *, size_t) override {}
  68. void unregisterMemory(void *) override {}
  69. void setCustomData(const char *, const char *) override {}
  70. void removeCustomData(const char *) override {}
  71. CallbackKey registerCallback(CallbackFunc /*callback*/) override {
  72. return 0;
  73. }
  74. void unregisterCallback(CallbackKey /*key*/) override {}
  75. void setHeapInfo(const HeapInformation & /*heapInfo*/) override {}
  76. ~NopCrashManager() {}
  77. };
  78. } // namespace vm
  79. } // namespace hermes
  80. #endif