KSCrashC.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // KSCrashC.h
  3. //
  4. // Created by Karl Stenerud on 2012-01-28.
  5. //
  6. // Copyright (c) 2012 Karl Stenerud. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall remain in place
  16. // in this source code.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. /* Primary C entry point into the crash reporting system.
  27. */
  28. #ifndef HDR_KSCrashC_h
  29. #define HDR_KSCrashC_h
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #include "KSCrashMonitorType.h"
  34. #include "KSCrashReportWriter.h"
  35. #include <stdbool.h>
  36. /** Install the crash reporter. The reporter will record the next crash and then
  37. * terminate the program.
  38. *
  39. * @param installPath Directory to install to.
  40. *
  41. * @return The crash types that are being handled.
  42. */
  43. KSCrashMonitorType kscrash_install(const char* appName, const char* const installPath);
  44. /** Set the crash types that will be handled.
  45. * Some crash types may not be enabled depending on circumstances (e.g. running
  46. * in a debugger).
  47. *
  48. * @param monitors The monitors to install.
  49. *
  50. * @return The monitors that were installed. If KSCrash has been
  51. * installed, the return value represents the monitors that were
  52. * successfully installed. Otherwise it represents which monitors it
  53. * will attempt to activate when KSCrash installs.
  54. */
  55. KSCrashMonitorType kscrash_setMonitoring(KSCrashMonitorType monitors);
  56. /** Set the user-supplied data in JSON format.
  57. *
  58. * @param userInfoJSON Pre-baked JSON containing user-supplied information.
  59. * NULL = delete.
  60. */
  61. void kscrash_setUserInfoJSON(const char* const userInfoJSON);
  62. /** Set the maximum time to allow the main thread to run without returning.
  63. * If a task occupies the main thread for longer than this interval, the
  64. * watchdog will consider the queue deadlocked and shut down the app and write a
  65. * crash report.
  66. *
  67. * Warning: Make SURE that nothing in your app that runs on the main thread takes
  68. * longer to complete than this value or it WILL get shut down! This includes
  69. * your app startup process, so you may need to push app initialization to
  70. * another thread, or perhaps set this to a higher value until your application
  71. * has been fully initialized.
  72. *
  73. * 0 = Disabled.
  74. *
  75. * Default: 0
  76. */
  77. void kscrash_setDeadlockWatchdogInterval(double deadlockWatchdogInterval);
  78. /** If true, attempt to fetch dispatch queue names for each running thread.
  79. *
  80. * WARNING: There is a chance that this will crash on a ksthread_getQueueName() call!
  81. *
  82. * Enable at your own risk.
  83. *
  84. * Default: false
  85. */
  86. void kscrash_setSearchQueueNames(bool searchQueueNames);
  87. /** If true, introspect memory contents during a crash.
  88. * Any Objective-C objects or C strings near the stack pointer or referenced by
  89. * cpu registers or exceptions will be recorded in the crash report, along with
  90. * their contents.
  91. *
  92. * Default: false
  93. */
  94. void kscrash_setIntrospectMemory(bool introspectMemory);
  95. /** List of Objective-C classes that should never be introspected.
  96. * Whenever a class in this list is encountered, only the class name will be recorded.
  97. * This can be useful for information security concerns.
  98. *
  99. * Default: NULL
  100. */
  101. void kscrash_setDoNotIntrospectClasses(const char** doNotIntrospectClasses, int length);
  102. /** Set the callback to invoke upon a crash.
  103. *
  104. * WARNING: Only call async-safe functions from this function! DO NOT call
  105. * Objective-C methods!!!
  106. *
  107. * @param onCrashNotify Function to call during a crash report to give the
  108. * callee an opportunity to add to the report.
  109. * NULL = ignore.
  110. *
  111. * Default: NULL
  112. */
  113. void kscrash_setCrashNotifyCallback(const KSReportWriteCallback onCrashNotify);
  114. typedef void (*KSReportWrittenCallback)(int64_t reportID);
  115. /** Set the callback to invoke upon finishing writing a crash report.
  116. *
  117. * WARNING: Only call async-safe functions from this function! DO NOT call
  118. * Objective-C methods!!!
  119. *
  120. * @param onReportWrittenNotify Function to call after writing a crash report to
  121. * give the callee an opportunity to react to the report.
  122. * NULL = ignore.
  123. *
  124. * Default: NULL
  125. */
  126. void kscrash_setReportWrittenCallback(const KSReportWrittenCallback onReportWrittenNotify);
  127. /** Set if KSLOG console messages should be appended to the report.
  128. *
  129. * @param shouldAddConsoleLogToReport If true, add the log to the report.
  130. */
  131. void kscrash_setAddConsoleLogToReport(bool shouldAddConsoleLogToReport);
  132. /** Set if KSCrash should print the previous log to the console on startup.
  133. * This is for debugging purposes.
  134. */
  135. void kscrash_setPrintPreviousLog(bool shouldPrintPreviousLog);
  136. /** Set the maximum number of reports allowed on disk before old ones get deleted.
  137. *
  138. * @param maxReportCount The maximum number of reports.
  139. */
  140. void kscrash_setMaxReportCount(int maxReportCount);
  141. /** Report a custom, user defined exception.
  142. * This can be useful when dealing with scripting languages.
  143. *
  144. * If terminateProgram is true, all sentries will be uninstalled and the application will
  145. * terminate with an abort().
  146. *
  147. * @param name The exception name (for namespacing exception types).
  148. *
  149. * @param reason A description of why the exception occurred.
  150. *
  151. * @param language A unique language identifier.
  152. *
  153. * @param lineOfCode A copy of the offending line of code (NULL = ignore).
  154. *
  155. * @param stackTrace JSON encoded array containing stack trace information (one frame per array entry).
  156. * The frame structure can be anything you want, including bare strings.
  157. *
  158. * @param logAllThreads If true, suspend all threads and log their state. Note that this incurs a
  159. * performance penalty, so it's best to use only on fatal errors.
  160. *
  161. * @param terminateProgram If true, do not return from this function call. Terminate the program instead.
  162. */
  163. void kscrash_reportUserException(const char* name,
  164. const char* reason,
  165. const char* language,
  166. const char* lineOfCode,
  167. const char* stackTrace,
  168. bool logAllThreads,
  169. bool terminateProgram);
  170. /** Experimental feature. Works like LD_PRELOAD. Enable C++ exceptions catching with __cxa_throw swap,
  171. * by updating pointers in the indirect symbol table, which is located in the __LINKEDIT segment.
  172. * It supports getting a true stackstace even in dynamically linked libraries.
  173. * Also allows a user to override original __cxa_throw with his implementation.
  174. */
  175. void enableSwapCxaThrow(void);
  176. #pragma mark -- Notifications --
  177. /** Notify the crash reporter of KSCrash being added to Objective-C runtime system.
  178. */
  179. void kscrash_notifyObjCLoad(void);
  180. /** Notify the crash reporter of the application active state.
  181. *
  182. * @param isActive true if the application is active, otherwise false.
  183. */
  184. void kscrash_notifyAppActive(bool isActive);
  185. /** Notify the crash reporter of the application foreground/background state.
  186. *
  187. * @param isInForeground true if the application is in the foreground, false if
  188. * it is in the background.
  189. */
  190. void kscrash_notifyAppInForeground(bool isInForeground);
  191. /** Notify the crash reporter that the application is terminating.
  192. */
  193. void kscrash_notifyAppTerminate(void);
  194. /** Notify the crash reporter that the application has crashed.
  195. */
  196. void kscrash_notifyAppCrash(void);
  197. #pragma mark -- Reporting --
  198. /** Get the number of reports on disk.
  199. */
  200. int kscrash_getReportCount(void);
  201. /** Get a list of IDs for all reports on disk.
  202. *
  203. * @param reportIDs An array big enough to hold all report IDs.
  204. * @param count How many reports the array can hold.
  205. *
  206. * @return The number of report IDs that were placed in the array.
  207. */
  208. int kscrash_getReportIDs(int64_t* reportIDs, int count);
  209. /** Read a report.
  210. *
  211. * @param reportID The report's ID.
  212. *
  213. * @return The NULL terminated report, or NULL if not found.
  214. * MEMORY MANAGEMENT WARNING: User is responsible for calling free() on the returned value.
  215. */
  216. char* kscrash_readReport(int64_t reportID);
  217. /** Add a custom report to the store.
  218. *
  219. * @param report The report's contents (must be JSON encoded).
  220. * @param reportLength The length of the report in bytes.
  221. *
  222. * @return the new report's ID.
  223. */
  224. int64_t kscrash_addUserReport(const char* report, int reportLength);
  225. /** Delete all reports on disk.
  226. */
  227. void kscrash_deleteAllReports(void);
  228. /** Delete report.
  229. *
  230. * @param reportID An ID of report to delete.
  231. */
  232. void kscrash_deleteReportWithID(int64_t reportID);
  233. #ifdef __cplusplus
  234. }
  235. #endif
  236. #endif // HDR_KSCrashC_h