]> git.saurik.com Git - apple/xnu.git/blame - libkern/os/log.h
xnu-4903.270.47.tar.gz
[apple/xnu.git] / libkern / os / log.h
CommitLineData
39037602
A
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
49extern void *__dso_handle;
50
5ba3f43e
A
51#ifdef XNU_KERNEL_PRIVATE
52extern bool startup_serial_logging_active;
53extern uint64_t startup_serial_num_procs;
54#endif /* XNU_KERNEL_PRIVATE */
55
56OS_ALWAYS_INLINE static inline void _os_log_verify_format_str(__unused const char *msg, ...) __attribute__((format(os_log, 1, 2)));
0a7de745
A
57OS_ALWAYS_INLINE static inline void
58_os_log_verify_format_str(__unused const char *msg, ...) /* placeholder */
59{
60}
39037602
A
61
62#if OS_OBJECT_USE_OBJC
63OS_OBJECT_DECL(os_log);
64#else
65typedef struct os_log_s *os_log_t;
66#endif /* OS_OBJECT_USE_OBJC */
67
68/*!
69 * @const OS_LOG_DISABLED
70 *
71 * @discussion
72 * Use this to disable a specific log message.
73 */
74#define OS_LOG_DISABLED NULL
75
76/*!
77 * @const OS_LOG_DEFAULT
78 *
79 * @discussion
80 * Use this to log a message in accordance with current system settings.
81 */
82#define OS_LOG_DEFAULT OS_OBJECT_GLOBAL_OBJECT(os_log_t, _os_log_default)
0a7de745 83__OSX_AVAILABLE_STARTING(__MAC_10_12, __IPHONE_10_0)
39037602
A
84OS_EXPORT
85struct os_log_s _os_log_default;
86
87/*!
88 * @enum os_log_type_t
89 *
90 * @discussion
91 * Supported log message types.
92 *
93 * @constant OS_LOG_TYPE_DEFAULT
94 * Equivalent type for "os_log()" messages, i.e., default messages that are always
95 * captured to memory or disk.
96 *
97 * @constant OS_LOG_TYPE_INFO
98 * Equivalent type for "os_log_info()" messages, i.e., Additional informational messages.
99 *
100 * @constant OS_LOG_TYPE_DEBUG
101 * Equivalent type for "os_log_debug()" messages, i.e., Debug messages.
102 *
103 * @constant OS_LOG_TYPE_ERROR
104 * Equivalent type for "os_log_error()" messages, i.e., local process error messages.
105 *
106 * @constant OS_LOG_TYPE_FAULT
107 * Equivalent type for "os_log_fault()" messages, i.e., a system error that involves
108 * potentially more than one process, usually used by daemons and services.
109 */
110OS_ENUM(os_log_type, uint8_t,
0a7de745
A
111 OS_LOG_TYPE_DEFAULT = 0x00,
112 OS_LOG_TYPE_INFO = 0x01,
113 OS_LOG_TYPE_DEBUG = 0x02,
114 OS_LOG_TYPE_ERROR = 0x10,
115 OS_LOG_TYPE_FAULT = 0x11);
39037602
A
116
117/*!
118 * @function os_log_create
119 *
120 * @abstract
121 * Creates a log object to be used with other log related functions.
122 *
123 * @discussion
124 * Creates a log object to be used with other log related functions. The
125 * log object serves two purposes: (1) tag related messages by subsystem
126 * and category name for easy filtering, and (2) control logging system
127 * behavior for messages.
128 *
129 * A log object may customize logging system behavior for its messages by
0a7de745 130 * adding a configuration file in /Library/LogPreferences. Most options
39037602
A
131 * accept 3 values: "Default", "Yes" or "No" as strings, where "Default"
132 * signifies follow system behavior for the level of messages.
133 *
134 * For log:
135 *
136 * os_log_create("com.company.mysubsystem", "connections");
137 *
138 * System-provided preferences are located in /System/Library/LogPreferences/<subsystem>.plist
139 *
140 * <dict>
141 *
142 * <!-- Default options applied to message types in each category, which can be overriden. -->
143 * <key>DEFAULT-OPTIONS</key>
144 * <dict>
145 * <key>Enabled</key> <!-- Enabled state follows system defaults -->
146 * <string>Default</string>
147 * <key>Persist</key> <!-- Do not persist to disk, use memory-only buffer if enabled -->
148 * <string>No</string>
0a7de745 149 * <key>TTL</key> <!-- Follow system default behavior if persistence is enabled -->
39037602
A
150 * <string>Default</string> <!-- Can specify in days with "d" or hours "h" (e.g., "4h" = 4 hours) -->
151 * </dict>
152 *
153 * <!-- category named “connections” -->
154 * <key>connections</key>
155 * <dict>
156 *
157 * <!-- Options that control "os_log()" behavior. The "Enabled" option is ignored. -->
158 * <key>Default</key>
159 * <dict>
160 * <key>Persist</key> <!-- Always persist to disk -->
161 * <string>Yes</string>
162 * <key>TTL</key> <!-- Store default messages for maximum 4 days -->
163 * <integer>4d</integer>
164 * </dict>
165 *
166 * <!-- Subdictionary of options that control "os_log_info()" behavior -->
167 * <key>Info</key>
168 * <dict>
169 * <key>Persist</key> <!-- If enabled persist to disk -->
170 * <string>Yes</string>
0a7de745 171 * <key>TTL</key> <!-- Store Info messages for 2 days -->
39037602
A
172 * <string>2d</string>
173 * </dict>
174 *
175 * <!-- Subdictionary of options that control "os_log_debug()" behavior -->
176 * <key>Debug</key>
177 * <dict>
178 * <key>Enabled</key> <!-- Not enabled, must be enabled specifically -->
179 * <string>No</string>
180 * </dict>
181 * </dict>
182 * </dict>
183 *
184 * All other preferences and system-overrides are stored in /Library/LogPreferences/.
185 *
186 * @param subsystem
187 * The identifier of the given subsystem should be in reverse DNS form
188 * (i.e., com.company.mysubsystem). This string must be a constant string,
189 * not dynamically generated.
190 *
191 * @param category
192 * The category within the given subsystem that specifies the settings for
193 * the log object. This string must be a constant string, not dynamically
194 * generated.
195 *
196 * @result
197 * Returns an os_log_t value to be passed to other os_log API calls. This
198 * should be called once at log initialization and rely on system to detect
199 * changes to settings. This object should be released when no longer used
200 * via os_release or -[release] method.
201 *
202 * A value will always be returned to allow for dynamic enablement.
203 */
0a7de745 204__OSX_AVAILABLE_STARTING(__MAC_10_12, __IPHONE_10_0)
39037602
A
205OS_EXPORT OS_NOTHROW OS_WARN_RESULT OS_OBJECT_RETURNS_RETAINED
206os_log_t
207os_log_create(const char *subsystem, const char *category);
208
209/*!
210 * @function os_log_info_enabled
211 *
212 * @abstract
213 * Returns if development log messages are enabled for a particular log object.
214 *
215 * @discussion
216 * Returns if development log messages are enabled for a particular log object.
217 *
218 * @param log
219 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
220 *
221 * @result
222 * Returns ‘true’ if debug log messages are enabled.
223 */
224__WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0)
225OS_EXPORT OS_NOTHROW OS_WARN_RESULT
226bool
227os_log_info_enabled(os_log_t log);
228
229/*!
230 * @function os_log_debug_enabled
231 *
232 * @abstract
233 * Returns if debug log messages are enabled for a particular log object.
234 *
235 * @discussion
236 * Returns if debug log messages are enabled for a particular log object.
237 *
238 * @param log
239 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
240 *
241 * @result
242 * Returns ‘true’ if debug log messages are enabled.
243 */
244__WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0)
245OS_EXPORT OS_NOTHROW OS_WARN_RESULT
246bool
247os_log_debug_enabled(os_log_t log);
248
249/*!
250 * @function os_log
251 *
252 * @abstract
253 * Insert a log message into the Unified Logging and Tracing system.
254 *
255 * @discussion
0a7de745 256 * Insert a log message into the Unified Logging and Tracing system in
39037602
A
257 * accordance with the preferences specified by the provided log object.
258 * These messages cannot be disabled and therefore always captured either
259 * to memory or disk.
260 *
261 * When an os_activity_id_t is present, the log message will also be scoped by
262 * that identifier. Activities provide granular filtering of log messages
263 * across threads and processes.
264 *
265 * There is a physical cap of 256 bytes per entry for dynamic content,
266 * i.e., %s and %@, that can be written to the persistence store. As such,
267 * all content exceeding the limit will be truncated before written to disk.
268 * Live streams will continue to show the full content.
269 *
270 * @param log
271 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
272 *
273 * @param format
274 * A format string to generate a human-readable log message when the log
275 * line is decoded. This string must be a constant string, not dynamically
276 * generated. Supports all standard printf types and %@ (objects).
277 */
278#define os_log(log, format, ...) __extension__({ \
279 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
280 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
281 _os_log_verify_format_str(format, ##__VA_ARGS__); \
282 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_DEFAULT, _os_log_fmt, ##__VA_ARGS__); \
283 __asm__(""); /* avoid tailcall */ \
284})
285
286/*!
287 * @function os_log_info
288 *
289 * @abstract
290 * Insert a development log message into the Unified Logging and Tracing system.
291 *
292 * @discussion
0a7de745 293 * Insert a log message into the Unified Logging and Tracing system in
39037602
A
294 * accordance with the preferences specified by the provided log object.
295 *
296 * When an os_activity_id_t is present, the log message will also be scoped by
297 * that identifier. Activities provide granular filtering of log messages
298 * across threads and processes.
299 *
300 * There is a physical cap of 256 bytes per entry for dynamic content,
301 * i.e., %s and %@, that can be written to the persistence store. As such,
302 * all content exceeding the limit will be truncated before written to disk.
303 * Live streams will continue to show the full content.
304 *
305 * @param log
306 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
307 *
308 * @param format
309 * A format string to generate a human-readable log message when the log
310 * line is decoded. This string must be a constant string, not dynamically
311 * generated. Supports all standard printf types and %@ (objects).
312 */
313#define os_log_info(log, format, ...) __extension__({ \
314 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
315 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
316 _os_log_verify_format_str(format, ##__VA_ARGS__); \
317 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_INFO, _os_log_fmt, ##__VA_ARGS__); \
318 __asm__(""); /* avoid tailcall */ \
319})
320
321/*!
322 * @function os_log_debug
323 *
324 * @abstract
325 * Insert a debug log message into the Unified Logging and Tracing system.
326 *
327 * @discussion
328 * Insert a debug log message into the Unified Logging and Tracing system in
329 * accordance with the preferences specified by the provided log object.
330 *
331 * When an os_activity_id_t is present, the log message will also be scoped by
332 * that identifier. Activities provide granular filtering of log messages
333 * across threads and processes.
334 *
335 * There is a physical cap of 256 bytes per entry for dynamic content,
336 * i.e., %s and %@, that can be written to the persistence store. As such,
337 * all content exceeding the limit will be truncated before written to disk.
338 * Live streams will continue to show the full content.
339 *
340 * @param log
341 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
342 *
343 * @param format
344 * A format string to generate a human-readable log message when the log
345 * line is decoded. This string must be a constant string, not dynamically
346 * generated. Supports all standard printf types and %@ (objects).
347 */
348#define os_log_debug(log, format, ...) __extension__({ \
349 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
350 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
351 _os_log_verify_format_str(format, ##__VA_ARGS__); \
352 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_DEBUG, _os_log_fmt, ##__VA_ARGS__); \
353 __asm__(""); /* avoid tailcall */ \
354})
355
356/*!
357 * @function os_log_error
358 *
359 * @abstract
360 * Insert an error log message into the Unified Logging and Tracing system.
361 *
362 * @discussion
363 * Insert an error log message into the Unified Logging and Tracing system.
364 *
365 * When an os_activity_id_t is present, the log message will also be scoped by
366 * that identifier. Activities provide granular filtering of log messages
367 * across threads and processes.
368 *
369 * There is a physical cap of 256 bytes per entry for dynamic content,
370 * i.e., %s and %@, that can be written to the persistence store. As such,
371 * all content exceeding the limit will be truncated before written to disk.
372 * Live streams will continue to show the full content.
373 *
374 * @param log
375 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
376 *
377 * @param format
378 * A format string to generate a human-readable log message when the log
379 * line is decoded. This string must be a constant string, not dynamically
380 * generated. Supports all standard printf types and %@ (objects).
381 */
382#define os_log_error(log, format, ...) __extension__({ \
383 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
384 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
385 _os_log_verify_format_str(format, ##__VA_ARGS__); \
386 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_ERROR, _os_log_fmt, ##__VA_ARGS__); \
387 __asm__(""); /* avoid tailcall */ \
388})
389
390/*!
391 * @function os_log_fault
392 *
393 * @abstract
394 * Insert a fault log message into the Unified Logging and Tracing system.
395 *
396 * @discussion
397 * Log a fault message issue into the Unified Logging and Tracing system
398 * signifying a multi-process (i.e., system error) related issue, either
0a7de745 399 * due to interaction via IPC or some other. Faults will gather information
39037602
A
400 * from the entire process chain and record it for later inspection.
401 *
402 * When an os_activity_id_t is present, the log message will also be scoped by
403 * that identifier. Activities provide granular filtering of log messages
404 * across threads and processes.
405 *
406 * There is a physical cap of 256 bytes per entry for dynamic content,
407 * i.e., %s and %@, that can be written to the persistence store. As such,
408 * all content exceeding the limit will be truncated before written to disk.
409 * Live streams will continue to show the full content.
410 *
411 * @param log
412 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
413 *
414 * @param format
415 * A format string to generate a human-readable log message when the log
416 * line is decoded. This string must be a constant string, not dynamically
417 * generated. Supports all standard printf types and %@ (objects).
418 */
419#define os_log_fault(log, format, ...) __extension__({ \
420 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
421 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
422 _os_log_verify_format_str(format, ##__VA_ARGS__); \
423 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_FAULT, _os_log_fmt, ##__VA_ARGS__); \
424 __asm__(""); /* avoid tailcall */ \
425})
426
427/*!
428 * @function os_log_with_type
429 *
430 * @abstract
431 * Log a message using a specific type.
432 *
433 * @discussion
434 * Will log a message with the provided os_log_type_t.
435 *
436 * @param log
437 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
438 *
439 * @param type
440 * Pass a valid type from os_log_type_t.
441 *
442 * @param format
443 * A format string to generate a human-readable log message when the log
444 * line is decoded. This string must be a constant string, not dynamically
445 * generated. Supports all standard printf types and %@ (objects).
446 */
447#define os_log_with_type(log, type, format, ...) __extension__({ \
448 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
449 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
450 _os_log_verify_format_str(format, ##__VA_ARGS__); \
451 _os_log_internal(&__dso_handle, log, type, _os_log_fmt, ##__VA_ARGS__); \
452 __asm__(""); /* avoid tailcall */ \
453})
454
455/*!
456 * @function os_log_sensitive_debug
457 *
458 * @abstract
459 * Insert a debug log message containing sensitive content (i.e., personal
460 * identifying information).
461 *
462 * @discussion
463 * Insert a debug log message containing sensitive content (i.e., personal
464 * identifying information) in accordance with the preferences specified by
465 * the provided log object.
466 *
467 * All strings are considered potentially sensitive, though this call
468 * specifically signifies the message as containing sensitive content.
469 * The message will be stored separately from other messages.
470 *
471 * When an os_activity_id_t is present, the log message will also be scoped by
472 * that identifier. Activities provide granular filtering of log messages
473 * across threads and processes.
474 *
475 * There is a physical cap of 256 bytes per entry for dynamic content,
476 * i.e., %s and %@, that can be written to the persistence store. As such,
477 * all content exceeding the limit will be truncated before written to disk.
478 * Live streams will continue to show the full content.
479 *
480 * @param log
481 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
482 *
483 * @param format
484 * A format string to generate a human-readable log message when the log
485 * line is decoded. This string must be a constant string, not dynamically
486 * generated. Supports all standard printf types and %@ (objects).
487 */
488#define os_log_sensitive_debug(log, format, ...) __extension__({ \
489 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
490 __attribute__((section("__TEXT,__os_log_sens"))) static const char _os_log_fmt[] = format; \
491 _os_log_verify_format_str(format, ##__VA_ARGS__); \
492 _os_log_sensitive(&__dso_handle, log, OS_LOG_TYPE_DEBUG, _os_log_fmt, ##__VA_ARGS__); \
493 __asm__(""); /* avoid tailcall */ \
494})
495
5ba3f43e
A
496#ifdef XNU_KERNEL_PRIVATE
497#define os_log_with_startup_serial(log, format, ...) __extension__({ \
498 if (startup_serial_logging_active) { printf(format, ##__VA_ARGS__); } \
499 else { os_log(log, format, ##__VA_ARGS__); } \
500})
501#endif /* XNU_KERNEL_PRIVATE */
502
39037602
A
503/*!
504 * @function _os_log_internal
505 *
506 * @abstract
507 * Internal function used by macros.
508 */
509__WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0)
510OS_EXPORT OS_NOTHROW
511void
512_os_log_internal(void *dso, os_log_t log, os_log_type_t type, const char *message, ...);
513
514__END_DECLS
515
516#endif /* __os_log_h */