X-Git-Url: https://git.saurik.com/apple/libc.git/blobdiff_plain/6465356a983ac139f81d3b7913cdb548477c346c..c13ac0be4e632012a2344baecc513dfb99811ccd:/os/assumes.h diff --git a/os/assumes.h b/os/assumes.h index 028b828..c120f4e 100644 --- a/os/assumes.h +++ b/os/assumes.h @@ -34,8 +34,31 @@ __BEGIN_DECLS #include #include #include <_simple.h> -#include #include +#include + +#if __GNUC__ +#define os_constant(x) __builtin_constant_p((x)) +#define os_hardware_trap() __asm__ __volatile__ (""); __builtin_trap() +#define __OS_COMPILETIME_ASSERT__(e) __extension__({ \ + char __compile_time_assert__[(e) ? 1 : -1]; \ + (void)__compile_time_assert__; \ +}) +#else /* __GNUC__ */ +#define os_constant(x) ((long)0) +#define os_hardware_trap() abort() +#define __OS_COMPILETIME_ASSERT__(e) (e) +#endif /* __GNUC__ */ + + +/* os_crash() is like os_hardware_trap(), except you get to pass in a crash + * message, and it can be redirected to a callback function using + * os_set_crash_callback() */ +#define os_crash(msg) \ + ({ \ + _os_crash(msg); \ + os_hardware_trap(); \ + }) /* This is useful for clients who wish for the messages generated by assumes() * failures to go somewhere other than (or in addition to) the system log. If @@ -58,18 +81,30 @@ struct _os_redirect_assumes_s { .redirect = &func, \ }; + +typedef void (*os_crash_callback_t) (const char *); + +/* private. use os_get_crash_callback and os_set_crash_callback */ +extern os_crash_callback_t _os_crash_callback; + +static inline os_crash_callback_t +os_get_crash_callback() { + return _os_crash_callback; +} + +static inline void +os_set_crash_callback(os_crash_callback_t callback) { + _os_crash_callback = callback; +} + /* The asl_message argument is a _SIMPLE_STRING that, when given to _simple_asl_send(), will * direct the message to the MessageTracer diagnostic messages store rather than * the default system log store. */ typedef bool (*os_log_callout_t)(_SIMPLE_STRING asl_message, void *ctx, const char *); -#if TARGET_OS_IPHONE -#define os_set_crash_message(arg) /* no-op */ -#else #include -#define os_set_crash_message(arg) _crc_make_setter(message, (arg)) -#endif +#define os_set_crash_message(arg) CRSetCrashLogMessage(arg) #define os_assumes(e) __extension__({ \ __typeof__(e) _e = os_fastpath(e); \ @@ -87,7 +122,7 @@ typedef bool (*os_log_callout_t)(_SIMPLE_STRING asl_message, void *ctx, const ch __typeof__(e) _e = os_slowpath(e); \ if (_e) { \ if (os_constant(e)) { \ - __OS_COMPILETIME_ASSERT__(!e); \ + __OS_COMPILETIME_ASSERT__(!(e)); \ } \ _os_assumes_log((uint64_t)(uintptr_t)_e); \ _os_avoid_tail_call(); \ @@ -121,8 +156,7 @@ typedef bool (*os_log_callout_t)(_SIMPLE_STRING asl_message, void *ctx, const ch } \ \ char *_fail_message = _os_assert_log((uint64_t)(uintptr_t)_e); \ - os_set_crash_message(_fail_message); \ - os_hardware_trap(); \ + os_crash(_fail_message); \ free(_fail_message); \ } \ }) @@ -131,12 +165,11 @@ typedef bool (*os_log_callout_t)(_SIMPLE_STRING asl_message, void *ctx, const ch __typeof__(e) _e = os_slowpath(e); \ if (_e) { \ if (os_constant(e)) { \ - __OS_COMPILETIME_ASSERT__(!e); \ + __OS_COMPILETIME_ASSERT__(!(e)); \ } \ \ char *_fail_message = _os_assert_log((uint64_t)(uintptr_t)_e); \ - os_set_crash_message(_fail_message); \ - os_hardware_trap(); \ + os_crash(_fail_message); \ free(_fail_message); \ } \ }) @@ -145,8 +178,7 @@ typedef bool (*os_log_callout_t)(_SIMPLE_STRING asl_message, void *ctx, const ch __typeof__(e) _e = os_slowpath(e); \ if (_e == (__typeof__(e))-1) { \ char *_fail_message = _os_assert_log((uint64_t)(uintptr_t)errno); \ - os_set_crash_message(_fail_message); \ - os_hardware_trap(); \ + os_crash(_fail_message); \ free(_fail_message); \ } \ }) @@ -171,7 +203,7 @@ typedef bool (*os_log_callout_t)(_SIMPLE_STRING asl_message, void *ctx, const ch __typeof__(e) _e = os_slowpath(e); \ if (_e) { \ if (os_constant(e)) { \ - __OS_COMPILETIME_ASSERT__(!e); \ + __OS_COMPILETIME_ASSERT__(!(e)); \ } \ _os_assumes_log_ctx((f), (ctx), (uintptr_t)_e); \ _os_avoid_tail_call(); \ @@ -194,10 +226,9 @@ typedef bool (*os_log_callout_t)(_SIMPLE_STRING asl_message, void *ctx, const ch if (os_constant(e)) { \ __OS_COMPILETIME_ASSERT__(e); \ } \ -\ + \ char *_fail_message = _os_assert_log_ctx((f), (ctx), (uint64_t)(uintptr_t)_e); \ - os_set_crash_message(_fail_message); \ - os_hardware_trap(); \ + os_crash(_fail_message); \ free(_fail_message); \ } \ }) @@ -206,12 +237,11 @@ typedef bool (*os_log_callout_t)(_SIMPLE_STRING asl_message, void *ctx, const ch __typeof__(e) _e = os_slowpath(e); \ if (_e) { \ if (os_constant(e)) { \ - __OS_COMPILETIME_ASSERT__(!e); \ + __OS_COMPILETIME_ASSERT__(!(e)); \ } \ \ char *_fail_message = _os_assert_log_ctx((f), (ctx), (uint64_t)(uintptr_t)_e); \ - os_set_crash_message(_fail_message); \ - os_hardware_trap(); \ + os_crash(_fail_message); \ free(_fail_message); \ } \ }) @@ -220,12 +250,15 @@ typedef bool (*os_log_callout_t)(_SIMPLE_STRING asl_message, void *ctx, const ch __typeof__(e) _e = os_slowpath(e); \ if (_e == (__typeof__(e))-1) { \ char *_fail_message = _os_assert_log_ctx((f), (ctx), (uint64_t)(uintptr_t)errno); \ - os_set_crash_message(_fail_message); \ - os_hardware_trap(); \ + os_crash(_fail_message); \ free(_fail_message); \ } \ }) +__OSX_AVAILABLE_STARTING(__MAC_10_11, __IPHONE_9_0) +extern void +_os_crash(const char *); + __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_6_0) extern void _os_assumes_log(uint64_t code);