objc-errors.mm 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (c) 1999-2003, 2005-2007 Apple Inc. All Rights Reserved.
  3. *
  4. * @APPLE_LICENSE_HEADER_START@
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apple Public Source License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://www.opensource.apple.com/apsl/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. *
  21. * @APPLE_LICENSE_HEADER_END@
  22. */
  23. /*
  24. * objc-errors.m
  25. * Copyright 1988-2001, NeXT Software, Inc., Apple Computer, Inc.
  26. */
  27. #include "objc-private.h"
  28. #if TARGET_OS_WIN32
  29. #include <conio.h>
  30. void _objc_inform_on_crash(const char *fmt, ...)
  31. {
  32. }
  33. void _objc_inform(const char *fmt, ...)
  34. {
  35. va_list args;
  36. va_start(args, fmt);
  37. _vcprintf(fmt, args);
  38. va_end(args);
  39. _cprintf("\n");
  40. }
  41. void _objc_fatal(const char *fmt, ...)
  42. {
  43. va_list args;
  44. va_start(args, fmt);
  45. _vcprintf(fmt, args);
  46. va_end(args);
  47. _cprintf("\n");
  48. abort();
  49. }
  50. void __objc_error(id rcv, const char *fmt, ...)
  51. {
  52. va_list args;
  53. va_start(args, fmt);
  54. _vcprintf(fmt, args);
  55. va_end(args);
  56. abort();
  57. }
  58. void _objc_error(id rcv, const char *fmt, va_list args)
  59. {
  60. _vcprintf(fmt, args);
  61. abort();
  62. }
  63. #else
  64. #include <_simple.h>
  65. // Return true if c is a UTF8 continuation byte
  66. static bool isUTF8Continuation(char c)
  67. {
  68. return (c & 0xc0) == 0x80; // continuation byte is 0b10xxxxxx
  69. }
  70. // Add "message" to any forthcoming crash log.
  71. mutex_t crashlog_lock;
  72. static void _objc_crashlog(const char *message)
  73. {
  74. char *newmsg;
  75. #if 0
  76. {
  77. // for debugging at BOOT time.
  78. extern char **_NSGetProgname(void);
  79. FILE *crashlog = fopen("/_objc_crash.log", "a");
  80. setbuf(crashlog, NULL);
  81. fprintf(crashlog, "[%s] %s\n", *_NSGetProgname(), message);
  82. fclose(crashlog);
  83. sync();
  84. }
  85. #endif
  86. mutex_locker_t lock(crashlog_lock);
  87. char *oldmsg = (char *)CRGetCrashLogMessage();
  88. size_t oldlen;
  89. const size_t limit = 8000;
  90. if (!oldmsg) {
  91. newmsg = strdup(message);
  92. } else if ((oldlen = strlen(oldmsg)) > limit) {
  93. // limit total length by dropping old contents
  94. char *truncmsg = oldmsg + oldlen - limit;
  95. // advance past partial UTF-8 bytes
  96. while (isUTF8Continuation(*truncmsg)) truncmsg++;
  97. asprintf(&newmsg, "... %s\n%s", truncmsg, message);
  98. } else {
  99. asprintf(&newmsg, "%s\n%s", oldmsg, message);
  100. }
  101. if (newmsg) {
  102. // Strip trailing newline
  103. char *c = &newmsg[strlen(newmsg)-1];
  104. if (*c == '\n') *c = '\0';
  105. if (oldmsg) free(oldmsg);
  106. CRSetCrashLogMessage(newmsg);
  107. }
  108. }
  109. // Returns true if logs should be sent to stderr as well as syslog.
  110. // Copied from CFUtilities.c
  111. static bool also_do_stderr(void)
  112. {
  113. struct stat st;
  114. int ret = fstat(STDERR_FILENO, &st);
  115. if (ret < 0) return false;
  116. mode_t m = st.st_mode & S_IFMT;
  117. if (m == S_IFREG || m == S_IFSOCK || m == S_IFIFO || m == S_IFCHR) {
  118. return true;
  119. }
  120. return false;
  121. }
  122. // Print "message" to the console.
  123. static void _objc_syslog(const char *message)
  124. {
  125. _simple_asl_log(ASL_LEVEL_ERR, nil, message);
  126. if (also_do_stderr()) {
  127. write(STDERR_FILENO, message, strlen(message));
  128. }
  129. }
  130. /*
  131. * _objc_error is the default *_error handler.
  132. */
  133. #if !__OBJC2__
  134. // used by ExceptionHandling.framework
  135. #endif
  136. __attribute__((noreturn))
  137. void _objc_error(id self, const char *fmt, va_list ap)
  138. {
  139. char *buf;
  140. vasprintf(&buf, fmt, ap);
  141. _objc_fatal("%s: %s", object_getClassName(self), buf);
  142. }
  143. /*
  144. * this routine handles errors that involve an object (or class).
  145. */
  146. void __objc_error(id rcv, const char *fmt, ...)
  147. {
  148. va_list vp;
  149. va_start(vp,fmt);
  150. #if !__OBJC2__
  151. (*_error)(rcv, fmt, vp);
  152. #endif
  153. _objc_error (rcv, fmt, vp); /* In case (*_error)() returns. */
  154. va_end(vp);
  155. }
  156. static __attribute__((noreturn))
  157. void _objc_fatalv(uint64_t reason, uint64_t flags, const char *fmt, va_list ap)
  158. {
  159. char *buf1;
  160. vasprintf(&buf1, fmt, ap);
  161. char *buf2;
  162. asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
  163. _objc_syslog(buf2);
  164. if (DebugDontCrash) {
  165. char *buf3;
  166. asprintf(&buf3, "objc[%d]: HALTED\n", getpid());
  167. _objc_syslog(buf3);
  168. _Exit(1);
  169. }
  170. else {
  171. abort_with_reason(OS_REASON_OBJC, reason, buf1, flags);
  172. }
  173. }
  174. void _objc_fatal_with_reason(uint64_t reason, uint64_t flags,
  175. const char *fmt, ...)
  176. {
  177. va_list ap;
  178. va_start(ap, fmt);
  179. _objc_fatalv(reason, flags, fmt, ap);
  180. }
  181. void _objc_fatal(const char *fmt, ...)
  182. {
  183. va_list ap;
  184. va_start(ap,fmt);
  185. _objc_fatalv(OBJC_EXIT_REASON_UNSPECIFIED,
  186. OS_REASON_FLAG_ONE_TIME_FAILURE,
  187. fmt, ap);
  188. }
  189. /*
  190. * this routine handles soft runtime errors...like not being able
  191. * add a category to a class (because it wasn't linked in).
  192. */
  193. void _objc_inform(const char *fmt, ...)
  194. {
  195. va_list ap;
  196. char *buf1;
  197. char *buf2;
  198. va_start (ap,fmt);
  199. vasprintf(&buf1, fmt, ap);
  200. va_end (ap);
  201. asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
  202. _objc_syslog(buf2);
  203. free(buf2);
  204. free(buf1);
  205. }
  206. /*
  207. * Like _objc_inform(), but prints the message only in any
  208. * forthcoming crash log, not to the console.
  209. */
  210. void _objc_inform_on_crash(const char *fmt, ...)
  211. {
  212. va_list ap;
  213. char *buf1;
  214. char *buf2;
  215. va_start (ap,fmt);
  216. vasprintf(&buf1, fmt, ap);
  217. va_end (ap);
  218. asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
  219. _objc_crashlog(buf2);
  220. free(buf2);
  221. free(buf1);
  222. }
  223. /*
  224. * Like calling both _objc_inform and _objc_inform_on_crash.
  225. */
  226. void _objc_inform_now_and_on_crash(const char *fmt, ...)
  227. {
  228. va_list ap;
  229. char *buf1;
  230. char *buf2;
  231. va_start (ap,fmt);
  232. vasprintf(&buf1, fmt, ap);
  233. va_end (ap);
  234. asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
  235. _objc_crashlog(buf2);
  236. _objc_syslog(buf2);
  237. free(buf2);
  238. free(buf1);
  239. }
  240. #endif
  241. BREAKPOINT_FUNCTION(
  242. void _objc_warn_deprecated(void)
  243. );
  244. void _objc_inform_deprecated(const char *oldf, const char *newf)
  245. {
  246. if (PrintDeprecation) {
  247. if (newf) {
  248. _objc_inform("The function %s is obsolete. Use %s instead. Set a breakpoint on _objc_warn_deprecated to find the culprit.", oldf, newf);
  249. } else {
  250. _objc_inform("The function %s is obsolete. Do not use it. Set a breakpoint on _objc_warn_deprecated to find the culprit.", oldf);
  251. }
  252. }
  253. _objc_warn_deprecated();
  254. }