]> git.saurik.com Git - apple/xnu.git/blob - libkern/os/log.h
xnu-6153.141.1.tar.gz
[apple/xnu.git] / libkern / os / log.h
1 /*
2 * Copyright (c) 2015 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 #ifndef __os_log_h
25 #define __os_log_h
26
27 #include <os/object.h>
28 #include <stdint.h>
29 #include <stdbool.h>
30
31 #ifndef __has_attribute
32 #define __has_attribute(x) 0
33 #endif
34
35 #ifndef __has_builtin
36 #define __has_builtin(x) 0
37 #endif
38
39 #if __has_attribute(not_tail_called)
40 #define OS_LOG_NOTAILCALL __attribute__((not_tail_called))
41 #define OS_LOG_NOTAILCALL_MARKER
42 #else
43 #define OS_LOG_NOTAILCALL
44 #define OS_LOG_NOTAILCALL_MARKER __asm__("")
45 #endif
46
47 __BEGIN_DECLS
48
49 extern void *__dso_handle;
50
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 */
55
56 #ifdef KERNEL
57 #define OS_LOG_BUFFER_MAX_SIZE 256
58 #else
59 #define OS_LOG_BUFFER_MAX_SIZE 1024
60 #endif
61
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)
65
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 */
69 {
70 }
71
72 #if OS_OBJECT_USE_OBJC
73 OS_OBJECT_DECL(os_log);
74 #else
75 typedef struct os_log_s *os_log_t;
76 #endif /* OS_OBJECT_USE_OBJC */
77
78 /*!
79 * @const OS_LOG_DISABLED
80 *
81 * @discussion
82 * Use this to disable a specific log message.
83 */
84 #define OS_LOG_DISABLED NULL
85
86 /*!
87 * @const OS_LOG_DEFAULT
88 *
89 * @discussion
90 * Use this to log a message in accordance with current system settings.
91 */
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)
94 OS_EXPORT
95 struct os_log_s _os_log_default;
96
97 /*!
98 * @enum os_log_type_t
99 *
100 * @discussion
101 * Supported log message types.
102 *
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.
106 *
107 * @constant OS_LOG_TYPE_INFO
108 * Equivalent type for "os_log_info()" messages, i.e., Additional informational messages.
109 *
110 * @constant OS_LOG_TYPE_DEBUG
111 * Equivalent type for "os_log_debug()" messages, i.e., Debug messages.
112 *
113 * @constant OS_LOG_TYPE_ERROR
114 * Equivalent type for "os_log_error()" messages, i.e., local process error messages.
115 *
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.
119 */
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);
126
127 /*!
128 * @function os_log_create
129 *
130 * @abstract
131 * Creates a log object to be used with other log related functions.
132 *
133 * @discussion
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.
138 *
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.
143 *
144 * For log:
145 *
146 * os_log_create("com.company.mysubsystem", "connections");
147 *
148 * System-provided preferences are located in /System/Library/LogPreferences/<subsystem>.plist
149 *
150 * <dict>
151 *
152 * <!-- Default options applied to message types in each category, which can be overriden. -->
153 * <key>DEFAULT-OPTIONS</key>
154 * <dict>
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) -->
161 * </dict>
162 *
163 * <!-- category named “connections” -->
164 * <key>connections</key>
165 * <dict>
166 *
167 * <!-- Options that control "os_log()" behavior. The "Enabled" option is ignored. -->
168 * <key>Default</key>
169 * <dict>
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>
174 * </dict>
175 *
176 * <!-- Subdictionary of options that control "os_log_info()" behavior -->
177 * <key>Info</key>
178 * <dict>
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>
183 * </dict>
184 *
185 * <!-- Subdictionary of options that control "os_log_debug()" behavior -->
186 * <key>Debug</key>
187 * <dict>
188 * <key>Enabled</key> <!-- Not enabled, must be enabled specifically -->
189 * <string>No</string>
190 * </dict>
191 * </dict>
192 * </dict>
193 *
194 * All other preferences and system-overrides are stored in /Library/LogPreferences/.
195 *
196 * @param subsystem
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.
200 *
201 * @param category
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
204 * generated.
205 *
206 * @result
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.
211 *
212 * A value will always be returned to allow for dynamic enablement.
213 */
214 __OSX_AVAILABLE_STARTING(__MAC_10_12, __IPHONE_10_0)
215 OS_EXPORT OS_NOTHROW OS_WARN_RESULT OS_OBJECT_RETURNS_RETAINED
216 os_log_t
217 os_log_create(const char *subsystem, const char *category);
218
219 /*!
220 * @function os_log_info_enabled
221 *
222 * @abstract
223 * Returns if development log messages are enabled for a particular log object.
224 *
225 * @discussion
226 * Returns if development log messages are enabled for a particular log object.
227 *
228 * @param log
229 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
230 *
231 * @result
232 * Returns ‘true’ if debug log messages are enabled.
233 */
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
236 bool
237 os_log_info_enabled(os_log_t log);
238
239 /*!
240 * @function os_log_debug_enabled
241 *
242 * @abstract
243 * Returns if debug log messages are enabled for a particular log object.
244 *
245 * @discussion
246 * Returns if debug log messages are enabled for a particular log object.
247 *
248 * @param log
249 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
250 *
251 * @result
252 * Returns ‘true’ if debug log messages are enabled.
253 */
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
256 bool
257 os_log_debug_enabled(os_log_t log);
258
259 /*!
260 * @function os_log
261 *
262 * @abstract
263 * Insert a log message into the Unified Logging and Tracing system.
264 *
265 * @discussion
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
269 * to memory or disk.
270 *
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.
274 *
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.
279 *
280 * @param log
281 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
282 *
283 * @param format
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).
287 */
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 */ \
294 })
295
296 /*!
297 * @function os_log_info
298 *
299 * @abstract
300 * Insert a development log message into the Unified Logging and Tracing system.
301 *
302 * @discussion
303 * Insert a log message into the Unified Logging and Tracing system in
304 * accordance with the preferences specified by the provided log object.
305 *
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.
309 *
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.
314 *
315 * @param log
316 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
317 *
318 * @param format
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).
322 */
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 */ \
329 })
330
331 /*!
332 * @function os_log_debug
333 *
334 * @abstract
335 * Insert a debug log message into the Unified Logging and Tracing system.
336 *
337 * @discussion
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.
340 *
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.
344 *
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.
349 *
350 * @param log
351 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
352 *
353 * @param format
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).
357 */
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 */ \
364 })
365
366 /*!
367 * @function os_log_error
368 *
369 * @abstract
370 * Insert an error log message into the Unified Logging and Tracing system.
371 *
372 * @discussion
373 * Insert an error log message into the Unified Logging and Tracing system.
374 *
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.
378 *
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.
383 *
384 * @param log
385 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
386 *
387 * @param format
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).
391 */
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 */ \
398 })
399
400 /*!
401 * @function os_log_fault
402 *
403 * @abstract
404 * Insert a fault log message into the Unified Logging and Tracing system.
405 *
406 * @discussion
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.
411 *
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.
415 *
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.
420 *
421 * @param log
422 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
423 *
424 * @param format
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).
428 */
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 */ \
435 })
436
437 /*!
438 * @function os_log_with_type
439 *
440 * @abstract
441 * Log a message using a specific type.
442 *
443 * @discussion
444 * Will log a message with the provided os_log_type_t.
445 *
446 * @param log
447 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
448 *
449 * @param type
450 * Pass a valid type from os_log_type_t.
451 *
452 * @param format
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).
456 */
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 */ \
463 })
464
465 /*!
466 * @function os_log_driverKit
467 *
468 * @abstract
469 * Log a message using a specific type. This variant should be called only from dexts.
470 *
471 * @discussion
472 * Will log a message with the provided os_log_type_t.
473 *
474 * @param log
475 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
476 *
477 * @param type
478 * Pass a valid type from os_log_type_t.
479 *
480 * @param format
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).
484 *
485 * @result
486 * Returns EPERM if the caller is not a driverKit process, 0 in case of success.
487 */
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 */ \
494 })
495
496
497 /*!
498 * @function os_log_sensitive_debug
499 *
500 * @abstract
501 * Insert a debug log message containing sensitive content (i.e., personal
502 * identifying information).
503 *
504 * @discussion
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.
508 *
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.
512 *
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.
516 *
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.
521 *
522 * @param log
523 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
524 *
525 * @param format
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).
529 */
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 */ \
536 })
537
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__); } \
542 })
543 #endif /* XNU_KERNEL_PRIVATE */
544
545 /*!
546 * @function _os_log_internal
547 *
548 * @abstract
549 * Internal function used by macros.
550 */
551 __WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0)
552 OS_EXPORT OS_NOTHROW
553 void
554 _os_log_internal(void *dso, os_log_t log, os_log_type_t type, const char *message, ...);
555
556 /*!
557 * @function _os_log_internal_driverKit
558 *
559 * @abstract
560 * Internal function used by macros.
561 */
562 __WATCHOS_AVAILABLE(6.0) __OSX_AVAILABLE(10.15) __IOS_AVAILABLE(13.0) __TVOS_AVAILABLE(13.0)
563 OS_EXPORT OS_NOTHROW
564 int
565 _os_log_internal_driverKit(void *dso, os_log_t log, os_log_type_t type, const char *message, ...);
566 __END_DECLS
567
568 #endif /* __os_log_h */