Signals.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Support/Signals.h - Signal Handling support ----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines some helpful functions for dealing with the possibility of
  15. // unix signals occurring while your program is running.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_SUPPORT_SIGNALS_H
  19. #define LLVM_SUPPORT_SIGNALS_H
  20. #include <cstdint>
  21. #include <string>
  22. namespace llvm {
  23. class StringRef;
  24. class raw_ostream;
  25. namespace sys {
  26. /// This function runs all the registered interrupt handlers, including the
  27. /// removal of files registered by RemoveFileOnSignal.
  28. void RunInterruptHandlers();
  29. /// This function registers signal handlers to ensure that if a signal gets
  30. /// delivered that the named file is removed.
  31. /// Remove a file if a fatal signal occurs.
  32. bool RemoveFileOnSignal(StringRef Filename, std::string* ErrMsg = nullptr);
  33. /// This function removes a file from the list of files to be removed on
  34. /// signal delivery.
  35. void DontRemoveFileOnSignal(StringRef Filename);
  36. /// When an error signal (such as SIGABRT or SIGSEGV) is delivered to the
  37. /// process, print a stack trace and then exit.
  38. /// Print a stack trace if a fatal signal occurs.
  39. /// \param Argv0 the current binary name, used to find the symbolizer
  40. /// relative to the current binary before searching $PATH; can be
  41. /// StringRef(), in which case we will only search $PATH.
  42. /// \param DisableCrashReporting if \c true, disable the normal crash
  43. /// reporting mechanisms on the underlying operating system.
  44. void PrintStackTraceOnErrorSignal(StringRef Argv0,
  45. bool DisableCrashReporting = false);
  46. /// Disable all system dialog boxes that appear when the process crashes.
  47. void DisableSystemDialogsOnCrash();
  48. /// Print the stack trace using the given \c raw_ostream object.
  49. /// \param Depth refers to the number of stackframes to print. If not
  50. /// specified, the entire frame is printed.
  51. void PrintStackTrace(raw_ostream &OS, int Depth = 0);
  52. // Run all registered signal handlers.
  53. void RunSignalHandlers();
  54. using SignalHandlerCallback = void (*)(void *);
  55. /// Add a function to be called when an abort/kill signal is delivered to the
  56. /// process. The handler can have a cookie passed to it to identify what
  57. /// instance of the handler it is.
  58. void AddSignalHandler(SignalHandlerCallback FnPtr, void *Cookie);
  59. /// This function registers a function to be called when the user "interrupts"
  60. /// the program (typically by pressing ctrl-c). When the user interrupts the
  61. /// program, the specified interrupt function is called instead of the program
  62. /// being killed, and the interrupt function automatically disabled.
  63. ///
  64. /// Note that interrupt functions are not allowed to call any non-reentrant
  65. /// functions. An null interrupt function pointer disables the current
  66. /// installed function. Note also that the handler may be executed on a
  67. /// different thread on some platforms.
  68. void SetInterruptFunction(void (*IF)());
  69. /// Registers a function to be called when an "info" signal is delivered to
  70. /// the process.
  71. ///
  72. /// On POSIX systems, this will be SIGUSR1; on systems that have it, SIGINFO
  73. /// will also be used (typically ctrl-t).
  74. ///
  75. /// Note that signal handlers are not allowed to call any non-reentrant
  76. /// functions. An null function pointer disables the current installed
  77. /// function. Note also that the handler may be executed on a different
  78. /// thread on some platforms.
  79. void SetInfoSignalFunction(void (*Handler)());
  80. /// Registers a function to be called in a "one-shot" manner when a pipe
  81. /// signal is delivered to the process (i.e., on a failed write to a pipe).
  82. /// After the pipe signal is handled once, the handler is unregistered.
  83. ///
  84. /// The LLVM signal handling code will not install any handler for the pipe
  85. /// signal unless one is provided with this API (see \ref
  86. /// DefaultOneShotPipeSignalHandler). This handler must be provided before
  87. /// any other LLVM signal handlers are installed: the \ref InitLLVM
  88. /// constructor has a flag that can simplify this setup.
  89. ///
  90. /// Note that the handler is not allowed to call any non-reentrant
  91. /// functions. A null handler pointer disables the current installed
  92. /// function. Note also that the handler may be executed on a
  93. /// different thread on some platforms.
  94. ///
  95. /// This is a no-op on Windows.
  96. void SetOneShotPipeSignalFunction(void (*Handler)());
  97. /// On Unix systems, this function exits with an "IO error" exit code.
  98. /// This is a no-op on Windows.
  99. void DefaultOneShotPipeSignalHandler();
  100. /// This function does the following:
  101. /// - clean up any temporary files registered with RemoveFileOnSignal()
  102. /// - dump the callstack from the exception context
  103. /// - call any relevant interrupt/signal handlers
  104. /// - create a core/mini dump of the exception context whenever possible
  105. /// Context is a system-specific failure context: it is the signal type on
  106. /// Unix; the ExceptionContext on Windows.
  107. void CleanupOnSignal(uintptr_t Context);
  108. void unregisterHandlers();
  109. } // End sys namespace
  110. } // End llvm namespace
  111. #endif
  112. #ifdef __GNUC__
  113. #pragma GCC diagnostic pop
  114. #endif