2 * Copyright (c) 2015 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
27 #include <os/object.h>
31 #ifndef __has_attribute
32 #define __has_attribute(x) 0
36 #define __has_builtin(x) 0
39 #if __has_attribute(not_tail_called)
40 #define OS_LOG_NOTAILCALL __attribute__((not_tail_called))
41 #define OS_LOG_NOTAILCALL_MARKER
43 #define OS_LOG_NOTAILCALL
44 #define OS_LOG_NOTAILCALL_MARKER __asm__("")
49 extern void *__dso_handle
;
51 #ifdef XNU_KERNEL_PRIVATE
52 extern bool startup_serial_logging_active
;
53 extern uint64_t startup_serial_num_procs
;
54 #endif /* XNU_KERNEL_PRIVATE */
57 #define OS_LOG_BUFFER_MAX_SIZE 256
59 #define OS_LOG_BUFFER_MAX_SIZE 1024
62 // The OS_LOG_BUFFER_MAX_SIZE limit includes the metadata that
63 // must be included in the os_log firehose buffer
64 #define OS_LOG_DATA_MAX_SIZE (OS_LOG_BUFFER_MAX_SIZE - 16)
66 OS_ALWAYS_INLINE
static inline void _os_log_verify_format_str(__unused
const char *msg
, ...) __attribute__((format(os_log
, 1, 2)));
67 OS_ALWAYS_INLINE
static inline void
68 _os_log_verify_format_str(__unused
const char *msg
, ...) /* placeholder */
72 #if OS_OBJECT_USE_OBJC
73 OS_OBJECT_DECL(os_log
);
75 typedef struct os_log_s
*os_log_t
;
76 #endif /* OS_OBJECT_USE_OBJC */
79 * @const OS_LOG_DISABLED
82 * Use this to disable a specific log message.
84 #define OS_LOG_DISABLED NULL
87 * @const OS_LOG_DEFAULT
90 * Use this to log a message in accordance with current system settings.
92 #define OS_LOG_DEFAULT OS_OBJECT_GLOBAL_OBJECT(os_log_t, _os_log_default)
93 __OSX_AVAILABLE_STARTING(__MAC_10_12
, __IPHONE_10_0
)
95 struct os_log_s _os_log_default
;
101 * Supported log message types.
103 * @constant OS_LOG_TYPE_DEFAULT
104 * Equivalent type for "os_log()" messages, i.e., default messages that are always
105 * captured to memory or disk.
107 * @constant OS_LOG_TYPE_INFO
108 * Equivalent type for "os_log_info()" messages, i.e., Additional informational messages.
110 * @constant OS_LOG_TYPE_DEBUG
111 * Equivalent type for "os_log_debug()" messages, i.e., Debug messages.
113 * @constant OS_LOG_TYPE_ERROR
114 * Equivalent type for "os_log_error()" messages, i.e., local process error messages.
116 * @constant OS_LOG_TYPE_FAULT
117 * Equivalent type for "os_log_fault()" messages, i.e., a system error that involves
118 * potentially more than one process, usually used by daemons and services.
120 OS_ENUM(os_log_type
, uint8_t,
121 OS_LOG_TYPE_DEFAULT
= 0x00,
122 OS_LOG_TYPE_INFO
= 0x01,
123 OS_LOG_TYPE_DEBUG
= 0x02,
124 OS_LOG_TYPE_ERROR
= 0x10,
125 OS_LOG_TYPE_FAULT
= 0x11);
128 * @function os_log_create
131 * Creates a log object to be used with other log related functions.
134 * Creates a log object to be used with other log related functions. The
135 * log object serves two purposes: (1) tag related messages by subsystem
136 * and category name for easy filtering, and (2) control logging system
137 * behavior for messages.
139 * A log object may customize logging system behavior for its messages by
140 * adding a configuration file in /Library/LogPreferences. Most options
141 * accept 3 values: "Default", "Yes" or "No" as strings, where "Default"
142 * signifies follow system behavior for the level of messages.
146 * os_log_create("com.company.mysubsystem", "connections");
148 * System-provided preferences are located in /System/Library/LogPreferences/<subsystem>.plist
152 * <!-- Default options applied to message types in each category, which can be overriden. -->
153 * <key>DEFAULT-OPTIONS</key>
155 * <key>Enabled</key> <!-- Enabled state follows system defaults -->
156 * <string>Default</string>
157 * <key>Persist</key> <!-- Do not persist to disk, use memory-only buffer if enabled -->
158 * <string>No</string>
159 * <key>TTL</key> <!-- Follow system default behavior if persistence is enabled -->
160 * <string>Default</string> <!-- Can specify in days with "d" or hours "h" (e.g., "4h" = 4 hours) -->
163 * <!-- category named “connections” -->
164 * <key>connections</key>
167 * <!-- Options that control "os_log()" behavior. The "Enabled" option is ignored. -->
170 * <key>Persist</key> <!-- Always persist to disk -->
171 * <string>Yes</string>
172 * <key>TTL</key> <!-- Store default messages for maximum 4 days -->
173 * <integer>4d</integer>
176 * <!-- Subdictionary of options that control "os_log_info()" behavior -->
179 * <key>Persist</key> <!-- If enabled persist to disk -->
180 * <string>Yes</string>
181 * <key>TTL</key> <!-- Store Info messages for 2 days -->
182 * <string>2d</string>
185 * <!-- Subdictionary of options that control "os_log_debug()" behavior -->
188 * <key>Enabled</key> <!-- Not enabled, must be enabled specifically -->
189 * <string>No</string>
194 * All other preferences and system-overrides are stored in /Library/LogPreferences/.
197 * The identifier of the given subsystem should be in reverse DNS form
198 * (i.e., com.company.mysubsystem). This string must be a constant string,
199 * not dynamically generated.
202 * The category within the given subsystem that specifies the settings for
203 * the log object. This string must be a constant string, not dynamically
207 * Returns an os_log_t value to be passed to other os_log API calls. This
208 * should be called once at log initialization and rely on system to detect
209 * changes to settings. This object should be released when no longer used
210 * via os_release or -[release] method.
212 * A value will always be returned to allow for dynamic enablement.
214 __OSX_AVAILABLE_STARTING(__MAC_10_12
, __IPHONE_10_0
)
215 OS_EXPORT OS_NOTHROW OS_WARN_RESULT OS_OBJECT_RETURNS_RETAINED
217 os_log_create(const char *subsystem
, const char *category
);
220 * @function os_log_info_enabled
223 * Returns if development log messages are enabled for a particular log object.
226 * Returns if development log messages are enabled for a particular log object.
229 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
232 * Returns ‘true’ if debug log messages are enabled.
234 __WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0)
235 OS_EXPORT OS_NOTHROW OS_WARN_RESULT
237 os_log_info_enabled(os_log_t log
);
240 * @function os_log_debug_enabled
243 * Returns if debug log messages are enabled for a particular log object.
246 * Returns if debug log messages are enabled for a particular log object.
249 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
252 * Returns ‘true’ if debug log messages are enabled.
254 __WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0)
255 OS_EXPORT OS_NOTHROW OS_WARN_RESULT
257 os_log_debug_enabled(os_log_t log
);
263 * Insert a log message into the Unified Logging and Tracing system.
266 * Insert a log message into the Unified Logging and Tracing system in
267 * accordance with the preferences specified by the provided log object.
268 * These messages cannot be disabled and therefore always captured either
271 * When an os_activity_id_t is present, the log message will also be scoped by
272 * that identifier. Activities provide granular filtering of log messages
273 * across threads and processes.
275 * There is a physical cap of 256 bytes per entry for dynamic content,
276 * i.e., %s and %@, that can be written to the persistence store. As such,
277 * all content exceeding the limit will be truncated before written to disk.
278 * Live streams will continue to show the full content.
281 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
284 * A format string to generate a human-readable log message when the log
285 * line is decoded. This string must be a constant string, not dynamically
286 * generated. Supports all standard printf types and %@ (objects).
288 #define os_log(log, format, ...) __extension__({ \
289 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
290 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
291 _os_log_verify_format_str(format, ##__VA_ARGS__); \
292 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_DEFAULT, _os_log_fmt, ##__VA_ARGS__); \
293 __asm__(""); /* avoid tailcall */ \
297 * @function os_log_info
300 * Insert a development log message into the Unified Logging and Tracing system.
303 * Insert a log message into the Unified Logging and Tracing system in
304 * accordance with the preferences specified by the provided log object.
306 * When an os_activity_id_t is present, the log message will also be scoped by
307 * that identifier. Activities provide granular filtering of log messages
308 * across threads and processes.
310 * There is a physical cap of 256 bytes per entry for dynamic content,
311 * i.e., %s and %@, that can be written to the persistence store. As such,
312 * all content exceeding the limit will be truncated before written to disk.
313 * Live streams will continue to show the full content.
316 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
319 * A format string to generate a human-readable log message when the log
320 * line is decoded. This string must be a constant string, not dynamically
321 * generated. Supports all standard printf types and %@ (objects).
323 #define os_log_info(log, format, ...) __extension__({ \
324 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
325 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
326 _os_log_verify_format_str(format, ##__VA_ARGS__); \
327 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_INFO, _os_log_fmt, ##__VA_ARGS__); \
328 __asm__(""); /* avoid tailcall */ \
332 * @function os_log_debug
335 * Insert a debug log message into the Unified Logging and Tracing system.
338 * Insert a debug log message into the Unified Logging and Tracing system in
339 * accordance with the preferences specified by the provided log object.
341 * When an os_activity_id_t is present, the log message will also be scoped by
342 * that identifier. Activities provide granular filtering of log messages
343 * across threads and processes.
345 * There is a physical cap of 256 bytes per entry for dynamic content,
346 * i.e., %s and %@, that can be written to the persistence store. As such,
347 * all content exceeding the limit will be truncated before written to disk.
348 * Live streams will continue to show the full content.
351 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
354 * A format string to generate a human-readable log message when the log
355 * line is decoded. This string must be a constant string, not dynamically
356 * generated. Supports all standard printf types and %@ (objects).
358 #define os_log_debug(log, format, ...) __extension__({ \
359 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
360 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
361 _os_log_verify_format_str(format, ##__VA_ARGS__); \
362 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_DEBUG, _os_log_fmt, ##__VA_ARGS__); \
363 __asm__(""); /* avoid tailcall */ \
367 * @function os_log_error
370 * Insert an error log message into the Unified Logging and Tracing system.
373 * Insert an error log message into the Unified Logging and Tracing system.
375 * When an os_activity_id_t is present, the log message will also be scoped by
376 * that identifier. Activities provide granular filtering of log messages
377 * across threads and processes.
379 * There is a physical cap of 256 bytes per entry for dynamic content,
380 * i.e., %s and %@, that can be written to the persistence store. As such,
381 * all content exceeding the limit will be truncated before written to disk.
382 * Live streams will continue to show the full content.
385 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
388 * A format string to generate a human-readable log message when the log
389 * line is decoded. This string must be a constant string, not dynamically
390 * generated. Supports all standard printf types and %@ (objects).
392 #define os_log_error(log, format, ...) __extension__({ \
393 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
394 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
395 _os_log_verify_format_str(format, ##__VA_ARGS__); \
396 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_ERROR, _os_log_fmt, ##__VA_ARGS__); \
397 __asm__(""); /* avoid tailcall */ \
401 * @function os_log_fault
404 * Insert a fault log message into the Unified Logging and Tracing system.
407 * Log a fault message issue into the Unified Logging and Tracing system
408 * signifying a multi-process (i.e., system error) related issue, either
409 * due to interaction via IPC or some other. Faults will gather information
410 * from the entire process chain and record it for later inspection.
412 * When an os_activity_id_t is present, the log message will also be scoped by
413 * that identifier. Activities provide granular filtering of log messages
414 * across threads and processes.
416 * There is a physical cap of 256 bytes per entry for dynamic content,
417 * i.e., %s and %@, that can be written to the persistence store. As such,
418 * all content exceeding the limit will be truncated before written to disk.
419 * Live streams will continue to show the full content.
422 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
425 * A format string to generate a human-readable log message when the log
426 * line is decoded. This string must be a constant string, not dynamically
427 * generated. Supports all standard printf types and %@ (objects).
429 #define os_log_fault(log, format, ...) __extension__({ \
430 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
431 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
432 _os_log_verify_format_str(format, ##__VA_ARGS__); \
433 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_FAULT, _os_log_fmt, ##__VA_ARGS__); \
434 __asm__(""); /* avoid tailcall */ \
438 * @function os_log_with_type
441 * Log a message using a specific type.
444 * Will log a message with the provided os_log_type_t.
447 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
450 * Pass a valid type from os_log_type_t.
453 * A format string to generate a human-readable log message when the log
454 * line is decoded. This string must be a constant string, not dynamically
455 * generated. Supports all standard printf types and %@ (objects).
457 #define os_log_with_type(log, type, format, ...) __extension__({ \
458 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
459 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
460 _os_log_verify_format_str(format, ##__VA_ARGS__); \
461 _os_log_internal(&__dso_handle, log, type, _os_log_fmt, ##__VA_ARGS__); \
462 __asm__(""); /* avoid tailcall */ \
466 * @function os_log_driverKit
469 * Log a message using a specific type. This variant should be called only from dexts.
472 * Will log a message with the provided os_log_type_t.
475 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
478 * Pass a valid type from os_log_type_t.
481 * A format string to generate a human-readable log message when the log
482 * line is decoded. This string must be a constant string, not dynamically
483 * generated. Supports all standard printf types and %@ (objects).
486 * Returns EPERM if the caller is not a driverKit process, 0 in case of success.
488 #define os_log_driverKit(out, log, type, format, ...) __extension__({ \
489 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
490 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
491 _os_log_verify_format_str(format, ##__VA_ARGS__); \
492 (*(out)) = _os_log_internal_driverKit(&__dso_handle, log, type, _os_log_fmt, ##__VA_ARGS__); \
493 __asm__(""); /* avoid tailcall */ \
498 * @function os_log_sensitive_debug
501 * Insert a debug log message containing sensitive content (i.e., personal
502 * identifying information).
505 * Insert a debug log message containing sensitive content (i.e., personal
506 * identifying information) in accordance with the preferences specified by
507 * the provided log object.
509 * All strings are considered potentially sensitive, though this call
510 * specifically signifies the message as containing sensitive content.
511 * The message will be stored separately from other messages.
513 * When an os_activity_id_t is present, the log message will also be scoped by
514 * that identifier. Activities provide granular filtering of log messages
515 * across threads and processes.
517 * There is a physical cap of 256 bytes per entry for dynamic content,
518 * i.e., %s and %@, that can be written to the persistence store. As such,
519 * all content exceeding the limit will be truncated before written to disk.
520 * Live streams will continue to show the full content.
523 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
526 * A format string to generate a human-readable log message when the log
527 * line is decoded. This string must be a constant string, not dynamically
528 * generated. Supports all standard printf types and %@ (objects).
530 #define os_log_sensitive_debug(log, format, ...) __extension__({ \
531 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
532 __attribute__((section("__TEXT,__os_log_sens"))) static const char _os_log_fmt[] = format; \
533 _os_log_verify_format_str(format, ##__VA_ARGS__); \
534 _os_log_sensitive(&__dso_handle, log, OS_LOG_TYPE_DEBUG, _os_log_fmt, ##__VA_ARGS__); \
535 __asm__(""); /* avoid tailcall */ \
538 #ifdef XNU_KERNEL_PRIVATE
539 #define os_log_with_startup_serial(log, format, ...) __extension__({ \
540 if (startup_serial_logging_active) { printf(format, ##__VA_ARGS__); } \
541 else { os_log(log, format, ##__VA_ARGS__); } \
543 #endif /* XNU_KERNEL_PRIVATE */
546 * @function _os_log_internal
549 * Internal function used by macros.
551 __WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0)
554 _os_log_internal(void *dso
, os_log_t log
, os_log_type_t type
, const char *message
, ...);
557 * @function _os_log_internal_driverKit
560 * Internal function used by macros.
562 __WATCHOS_AVAILABLE(6.0) __OSX_AVAILABLE(10.15) __IOS_AVAILABLE(13.0) __TVOS_AVAILABLE(13.0)
565 _os_log_internal_driverKit(void *dso
, os_log_t log
, os_log_type_t type
, const char *message
, ...);
568 #endif /* __os_log_h */