2 * Copyright (c) 2013-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@
24 #ifndef __OS_TRACE_H__
25 #define __OS_TRACE_H__
27 #include <Availability.h>
29 #include <sys/types.h>
33 #if __has_include(<xpc/xpc.h>)
36 typedef void *xpc_object_t
;
40 #error "must be GNU C compatible"
45 extern void *__dso_handle
;
49 _os_trace_verify_printf(const char *msg
, ...)
50 __attribute__((format(printf
, 1, 2)))
55 #if !defined OS_COUNT_ARGS
56 #define OS_COUNT_ARGS(...) OS_COUNT_ARGS1(, ##__VA_ARGS__, _8, _7, _6, _5, _4, _3, _2, _1, _0)
57 #define OS_COUNT_ARGS1(z, a, b, c, d, e, f, g, h, cnt, ...) cnt
60 /* use old macros for anything less than iOS 10 and MacOS 10.12 */
61 #if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0) \
62 || (defined(__WATCH_OS_VERSION_MIN_REQUIRED) && __WATCH_OS_VERSION_MIN_REQUIRED < __WATCHOS_3_0) \
63 || (defined(__TV_OS_VERSION_MIN_REQUIRED) && __TV_OS_VERSION_MIN_REQUIRED < __TVOS_10_0) \
64 || (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_12)
66 #define _os_trace_0(_l, _m, _t) __extension__({ \
67 _os_trace_verify_printf(_l); \
68 _os_trace_with_buffer(&__dso_handle, _m, _t, NULL, 0, NULL); \
69 __asm__(""); /* avoid tailcall */ \
72 #define _os_trace_1(_l, _m, _t, _1) __extension__({ \
73 _Pragma("clang diagnostic push") \
74 _Pragma("clang diagnostic ignored \"-Wpacked\"") \
75 const __typeof__(_1) _c1 = _1; \
76 _os_trace_verify_printf(_l, _c1); \
77 const struct __attribute__((packed)) { \
78 __typeof__(_c1) _f1; \
82 ._f1 = _c1, ._s[0] = sizeof(_c1), \
85 _os_trace_with_buffer(&__dso_handle, _m, _t, &_buf, sizeof(_buf), NULL); \
86 __asm__(""); /* avoid tailcall */ \
87 _Pragma("clang diagnostic pop") \
90 #define _os_trace_2(_l, _m, _t, _1, _2) __extension__({ \
91 _Pragma("clang diagnostic push") \
92 _Pragma("clang diagnostic ignored \"-Wpacked\"") \
93 const __typeof__(_1) _c1 = _1; \
94 const __typeof__(_2) _c2 = _2; \
95 _os_trace_verify_printf(_l, _c1, _c2); \
96 const struct __attribute__((packed)) { \
97 __typeof__(_c1) _f1; \
98 __typeof__(_c2) _f2; \
102 ._f1 = _c1, ._s[0] = sizeof(_c1), \
103 ._f2 = _c2, ._s[1] = sizeof(_c2), \
106 _os_trace_with_buffer(&__dso_handle, _m, _t, &_buf, sizeof(_buf), NULL); \
107 __asm__(""); /* avoid tailcall */ \
108 _Pragma("clang diagnostic pop") \
111 #define _os_trace_3(_l, _m, _t, _1, _2, _3) __extension__({ \
112 _Pragma("clang diagnostic push") \
113 _Pragma("clang diagnostic ignored \"-Wpacked\"") \
114 const __typeof__(_1) _c1 = _1; \
115 const __typeof__(_2) _c2 = _2; \
116 const __typeof__(_3) _c3 = _3; \
117 _os_trace_verify_printf(_l, _c1, _c2, _c3); \
118 const struct __attribute__((packed)) { \
119 __typeof__(_c1) _f1; \
120 __typeof__(_c2) _f2; \
121 __typeof__(_c3) _f3; \
125 ._f1 = _c1, ._s[0] = sizeof(_c1), \
126 ._f2 = _c2, ._s[1] = sizeof(_c2), \
127 ._f3 = _c3, ._s[2] = sizeof(_c3), \
130 _os_trace_with_buffer(&__dso_handle, _m, _t, &_buf, sizeof(_buf), NULL); \
131 __asm__(""); /* avoid tailcall */ \
132 _Pragma("clang diagnostic pop") \
135 #define _os_trace_4(_l, _m, _t, _1, _2, _3, _4) __extension__({ \
136 _Pragma("clang diagnostic push") \
137 _Pragma("clang diagnostic ignored \"-Wpacked\"") \
138 const __typeof__(_1) _c1 = _1; \
139 const __typeof__(_2) _c2 = _2; \
140 const __typeof__(_3) _c3 = _3; \
141 const __typeof__(_4) _c4 = _4; \
142 _os_trace_verify_printf(_l, _c1, _c2, _c3, _c4); \
143 const struct __attribute__((packed)) { \
144 __typeof__(_c1) _f1; \
145 __typeof__(_c2) _f2; \
146 __typeof__(_c3) _f3; \
147 __typeof__(_c4) _f4; \
151 ._f1 = _c1, ._s[0] = sizeof(_c1), \
152 ._f2 = _c2, ._s[1] = sizeof(_c2), \
153 ._f3 = _c3, ._s[2] = sizeof(_c3), \
154 ._f4 = _c4, ._s[3] = sizeof(_c4), \
157 _os_trace_with_buffer(&__dso_handle, _m, _t, &_buf, sizeof(_buf), NULL); \
158 __asm__(""); /* avoid tailcall */ \
159 _Pragma("clang diagnostic pop") \
162 #define _os_trace_5(_l, _m, _t, _1, _2, _3, _4, _5) __extension__({ \
163 _Pragma("clang diagnostic push") \
164 _Pragma("clang diagnostic ignored \"-Wpacked\"") \
165 const __typeof__(_1) _c1 = _1; \
166 const __typeof__(_2) _c2 = _2; \
167 const __typeof__(_3) _c3 = _3; \
168 const __typeof__(_4) _c4 = _4; \
169 const __typeof__(_5) _c5 = _5; \
170 _os_trace_verify_printf(_l, _c1, _c2, _c3, _c4, _c5); \
171 const struct __attribute__((packed)) { \
172 __typeof__(_c1) _f1; \
173 __typeof__(_c2) _f2; \
174 __typeof__(_c3) _f3; \
175 __typeof__(_c4) _f4; \
176 __typeof__(_c5) _f5; \
180 ._f1 = _c1, ._s[0] = sizeof(_c1), \
181 ._f2 = _c2, ._s[1] = sizeof(_c2), \
182 ._f3 = _c3, ._s[2] = sizeof(_c3), \
183 ._f4 = _c4, ._s[3] = sizeof(_c4), \
184 ._f5 = _c5, ._s[4] = sizeof(_c5), \
187 _os_trace_with_buffer(&__dso_handle, _m, _t, &_buf, sizeof(_buf), NULL); \
188 __asm__(""); /* avoid tailcall */ \
189 _Pragma("clang diagnostic pop") \
192 #define _os_trace_6(_l, _m, _t, _1, _2, _3, _4, _5, _6) __extension__({ \
193 _Pragma("clang diagnostic push") \
194 _Pragma("clang diagnostic ignored \"-Wpacked\"") \
195 const __typeof__(_1) _c1 = _1; \
196 const __typeof__(_2) _c2 = _2; \
197 const __typeof__(_3) _c3 = _3; \
198 const __typeof__(_4) _c4 = _4; \
199 const __typeof__(_5) _c5 = _5; \
200 const __typeof__(_6) _c6 = _6; \
201 _os_trace_verify_printf(_l, _c1, _c2, _c3, _c4, _c5, _c6); \
202 const struct __attribute__((packed)) { \
203 __typeof__(_c1) _f1; \
204 __typeof__(_c2) _f2; \
205 __typeof__(_c3) _f3; \
206 __typeof__(_c4) _f4; \
207 __typeof__(_c5) _f5; \
208 __typeof__(_c6) _f6; \
212 ._f1 = _c1, ._s[0] = sizeof(_c1), \
213 ._f2 = _c2, ._s[1] = sizeof(_c2), \
214 ._f3 = _c3, ._s[2] = sizeof(_c3), \
215 ._f4 = _c4, ._s[3] = sizeof(_c4), \
216 ._f5 = _c5, ._s[4] = sizeof(_c5), \
217 ._f6 = _c6, ._s[5] = sizeof(_c6), \
220 _os_trace_with_buffer(&__dso_handle, _m, _t, &_buf, sizeof(_buf), NULL); \
221 __asm__(""); /* avoid tailcall */ \
222 _Pragma("clang diagnostic pop") \
225 #define _os_trace_7(_l, _m, _t, _1, _2, _3, _4, _5, _6, _7) __extension__({ \
226 _Pragma("clang diagnostic push") \
227 _Pragma("clang diagnostic ignored \"-Wpacked\"") \
228 const __typeof__(_1) _c1 = _1; \
229 const __typeof__(_2) _c2 = _2; \
230 const __typeof__(_3) _c3 = _3; \
231 const __typeof__(_4) _c4 = _4; \
232 const __typeof__(_5) _c5 = _5; \
233 const __typeof__(_6) _c6 = _6; \
234 const __typeof__(_7) _c7 = _7; \
235 _os_trace_verify_printf(_l, _c1, _c2, _c3, _c4, _c5, _c6, _c7); \
236 const struct __attribute__((packed)) { \
237 __typeof__(_c1) _f1; \
238 __typeof__(_c2) _f2; \
239 __typeof__(_c3) _f3; \
240 __typeof__(_c4) _f4; \
241 __typeof__(_c5) _f5; \
242 __typeof__(_c6) _f6; \
243 __typeof__(_c7) _f7; \
247 ._f1 = _c1, ._s[0] = sizeof(_c1), \
248 ._f2 = _c2, ._s[1] = sizeof(_c2), \
249 ._f3 = _c3, ._s[2] = sizeof(_c3), \
250 ._f4 = _c4, ._s[3] = sizeof(_c4), \
251 ._f5 = _c5, ._s[4] = sizeof(_c5), \
252 ._f6 = _c6, ._s[5] = sizeof(_c6), \
253 ._f7 = _c7, ._s[6] = sizeof(_c7), \
256 _os_trace_with_buffer(&__dso_handle, _m, _t, &_buf, sizeof(_buf), NULL); \
257 __asm__(""); /* avoid tailcall */ \
258 _Pragma("clang diagnostic pop") \
261 #define _os_trace_with_payload_1(_l, _m, _t, _payload) __extension__({ \
262 _os_trace_verify_printf(_l); \
263 _os_trace_with_buffer(&__dso_handle, _m, _t, NULL, 0, _payload); \
264 __asm__(""); /* avoid tailcall */ \
267 #define _os_trace_with_payload_2(_l, _m, _t, _1, _payload) __extension__({ \
268 _Pragma("clang diagnostic push") \
269 _Pragma("clang diagnostic ignored \"-Wpacked\"") \
270 const __typeof__(_1) _c1 = _1; \
271 _os_trace_verify_printf(_l, _c1); \
272 const struct __attribute__((packed)) { \
273 __typeof__(_c1) _f1; \
277 ._f1 = _c1, ._s[0] = sizeof(_c1), \
280 _os_trace_with_buffer(&__dso_handle, _m, _t, &_buf, sizeof(_buf), _payload); \
281 __asm__(""); /* avoid tailcall */ \
282 _Pragma("clang diagnostic pop") \
285 #define _os_trace_with_payload_3(_l, _m, _t, _1, _2, _payload) __extension__({ \
286 _Pragma("clang diagnostic push") \
287 _Pragma("clang diagnostic ignored \"-Wpacked\"") \
288 const __typeof__(_1) _c1 = _1; \
289 const __typeof__(_2) _c2 = _2; \
290 _os_trace_verify_printf(_l, _c1, _c2); \
291 const struct __attribute__((packed)) { \
292 __typeof__(_c1) _f1; \
293 __typeof__(_c2) _f2; \
297 ._f1 = _c1, ._s[0] = sizeof(_c1), \
298 ._f2 = _c2, ._s[1] = sizeof(_c2), \
301 _os_trace_with_buffer(&__dso_handle, _m, _t, &_buf, sizeof(_buf), _payload); \
302 __asm__(""); /* avoid tailcall */ \
303 _Pragma("clang diagnostic pop") \
306 #define _os_trace_with_payload_4(_l, _m, _t, _1, _2, _3, _payload) __extension__({ \
307 _Pragma("clang diagnostic push") \
308 _Pragma("clang diagnostic ignored \"-Wpacked\"") \
309 const __typeof__(_1) _c1 = _1; \
310 const __typeof__(_2) _c2 = _2; \
311 const __typeof__(_3) _c3 = _3; \
312 _os_trace_verify_printf(_l, _c1, _c2, _c3); \
313 const struct __attribute__((packed)) { \
314 __typeof__(_c1) _f1; \
315 __typeof__(_c2) _f2; \
316 __typeof__(_c3) _f3; \
320 ._f1 = _c1, ._s[0] = sizeof(_c1), \
321 ._f2 = _c2, ._s[1] = sizeof(_c2), \
322 ._f3 = _c3, ._s[2] = sizeof(_c3), \
325 _os_trace_with_buffer(&__dso_handle, _m, _t, &_buf, sizeof(_buf), _payload); \
326 __asm__(""); /* avoid tailcall */ \
327 _Pragma("clang diagnostic pop") \
330 #define _os_trace_with_payload_5(_l, _m, _t, _1, _2, _3, _4, _payload) __extension__({ \
331 _Pragma("clang diagnostic push") \
332 _Pragma("clang diagnostic ignored \"-Wpacked\"") \
333 const __typeof__(_1) _c1 = _1; \
334 const __typeof__(_2) _c2 = _2; \
335 const __typeof__(_3) _c3 = _3; \
336 const __typeof__(_4) _c4 = _4; \
337 _os_trace_verify_printf(_l, _c1, _c2, _c3, _c4); \
338 const struct __attribute__((packed)) { \
339 __typeof__(_c1) _f1; \
340 __typeof__(_c2) _f2; \
341 __typeof__(_c3) _f3; \
342 __typeof__(_c4) _f4; \
346 ._f1 = _c1, ._s[0] = sizeof(_c1), \
347 ._f2 = _c2, ._s[1] = sizeof(_c2), \
348 ._f3 = _c3, ._s[2] = sizeof(_c3), \
349 ._f4 = _c4, ._s[3] = sizeof(_c4), \
352 _os_trace_with_buffer(&__dso_handle, _m, _t, &_buf, sizeof(_buf), _payload); \
353 __asm__(""); /* avoid tailcall */ \
354 _Pragma("clang diagnostic pop") \
357 #define _os_trace_with_payload_6(_l, _m, _t, _1, _2, _3, _4, _5, _payload) __extension__({ \
358 _Pragma("clang diagnostic push") \
359 _Pragma("clang diagnostic ignored \"-Wpacked\"") \
360 const __typeof__(_1) _c1 = _1; \
361 const __typeof__(_2) _c2 = _2; \
362 const __typeof__(_3) _c3 = _3; \
363 const __typeof__(_4) _c4 = _4; \
364 const __typeof__(_4) _c5 = _5; \
365 _os_trace_verify_printf(_l, _c1, _c2, _c3, _c4, _c5); \
366 const struct __attribute__((packed)) { \
367 __typeof__(_c1) _f1; \
368 __typeof__(_c2) _f2; \
369 __typeof__(_c3) _f3; \
370 __typeof__(_c4) _f4; \
371 __typeof__(_c5) _f5; \
375 ._f1 = _c1, ._s[0] = sizeof(_c1), \
376 ._f2 = _c2, ._s[1] = sizeof(_c2), \
377 ._f3 = _c3, ._s[2] = sizeof(_c3), \
378 ._f4 = _c4, ._s[3] = sizeof(_c4), \
379 ._f5 = _c5, ._s[4] = sizeof(_c5), \
382 _os_trace_with_buffer(&__dso_handle, _m, _t, &_buf, sizeof(_buf), _payload); \
383 __asm__(""); /* avoid tailcall */ \
384 _Pragma("clang diagnostic pop") \
387 #define _os_trace_with_payload_7(_l, _m, _t, _1, _2, _3, _4, _5, _6, _payload) __extension__({ \
388 _Pragma("clang diagnostic push") \
389 _Pragma("clang diagnostic ignored \"-Wpacked\"") \
390 const __typeof__(_1) _c1 = _1; \
391 const __typeof__(_2) _c2 = _2; \
392 const __typeof__(_3) _c3 = _3; \
393 const __typeof__(_4) _c4 = _4; \
394 const __typeof__(_5) _c5 = _5; \
395 const __typeof__(_6) _c6 = _6; \
396 _os_trace_verify_printf(_l, _c1, _c2, _c3, _c4, _c5, _c6); \
397 const struct __attribute__((packed)) { \
398 __typeof__(_c1) _f1; \
399 __typeof__(_c2) _f2; \
400 __typeof__(_c3) _f3; \
401 __typeof__(_c4) _f4; \
402 __typeof__(_c5) _f5; \
403 __typeof__(_c6) _f6; \
407 ._f1 = _c1, ._s[0] = sizeof(_c1), \
408 ._f2 = _c2, ._s[1] = sizeof(_c2), \
409 ._f3 = _c3, ._s[2] = sizeof(_c3), \
410 ._f4 = _c4, ._s[3] = sizeof(_c4), \
411 ._f5 = _c5, ._s[4] = sizeof(_c5), \
412 ._f6 = _c6, ._s[5] = sizeof(_c6), \
415 _os_trace_with_buffer(&__dso_handle, _m, _t, &_buf, sizeof(_buf), _payload); \
416 __asm__(""); /* avoid tailcall */ \
417 _Pragma("clang diagnostic pop") \
420 #define _os_trace_with_payload_8(_l, _m, _t, _1, _2, _3, _4, _5, _6, _7, _payload) __extension__({ \
421 _Pragma("clang diagnostic push") \
422 _Pragma("clang diagnostic ignored \"-Wpacked\"") \
423 const __typeof__(_1) _c1 = _1; \
424 const __typeof__(_2) _c2 = _2; \
425 const __typeof__(_3) _c3 = _3; \
426 const __typeof__(_4) _c4 = _4; \
427 const __typeof__(_5) _c5 = _5; \
428 const __typeof__(_6) _c6 = _6; \
429 const __typeof__(_7) _c7 = _7; \
430 _os_trace_verify_printf(_l, _c1, _c2, _c3, _c4, _c5, _c6, _c7); \
431 const struct __attribute__((packed)) { \
432 __typeof__(_c1) _f1; \
433 __typeof__(_c2) _f2; \
434 __typeof__(_c3) _f3; \
435 __typeof__(_c4) _f4; \
436 __typeof__(_c5) _f5; \
437 __typeof__(_c6) _f6; \
438 __typeof__(_c7) _f7; \
442 ._f1 = _c1, ._s[0] = sizeof(_c1), \
443 ._f2 = _c2, ._s[1] = sizeof(_c2), \
444 ._f3 = _c3, ._s[2] = sizeof(_c3), \
445 ._f4 = _c4, ._s[3] = sizeof(_c4), \
446 ._f5 = _c5, ._s[4] = sizeof(_c5), \
447 ._f6 = _c6, ._s[5] = sizeof(_c6), \
448 ._f7 = _c7, ._s[6] = sizeof(_c7), \
451 _os_trace_with_buffer(&__dso_handle, _m, _t, &_buf, sizeof(_buf), _payload); \
452 __asm__(""); /* avoid tailcall */ \
453 _Pragma("clang diagnostic pop") \
456 #define OS_TRACE_CALL(format, _m, _t, ...) __extension__({ \
457 _Pragma("clang diagnostic push") \
458 _Pragma("clang diagnostic ignored \"-Wc++98-compat-pedantic\"") \
459 OS_CONCAT(_os_trace, OS_COUNT_ARGS(__VA_ARGS__))(format, _m, _t, ##__VA_ARGS__); \
460 _Pragma("clang diagnostic pop") \
465 // Use a new layout in Mac OS 10.12+ and iOS 10.0+
466 #define OS_TRACE_CALL(_l, _m, _t, ...) __extension__({ \
468 _os_trace_verify_printf(_l, ##__VA_ARGS__); \
469 size_t buf_size = _os_trace_encode(buf, sizeof(buf), _m, ##__VA_ARGS__); \
470 _os_trace_internal(&__dso_handle, _t, _m, buf, buf_size, NULL); \
471 __asm__(""); /* avoid tailcall */ \
474 #define _os_trace_with_payload_1(_l, _m, _t, _payload) __extension__({ \
475 _os_trace_verify_printf(_l); \
476 _os_trace_internal(&__dso_handle, _t, _m, NULL, 0, _payload); \
477 __asm__(""); /* avoid tailcall */ \
480 #define _os_trace_with_payload_2(_l, _m, _t, _1, _payload) __extension__({ \
481 _os_trace_verify_printf(_l, _1); \
483 size_t buf_size = _os_trace_encode(buf, sizeof(buf), _m, _1); \
484 _os_trace_internal(&__dso_handle, _t, _m, buf, buf_size, _payload); \
485 __asm__(""); /* avoid tailcall */ \
488 #define _os_trace_with_payload_3(_l, _m, _t, _1, _2, _payload) __extension__({ \
489 _os_trace_verify_printf(_l, _1, _2); \
491 size_t buf_size = _os_trace_encode(buf, sizeof(buf), _m, _1, _2); \
492 _os_trace_internal(&__dso_handle, _t, _m, buf, buf_size, _payload); \
493 __asm__(""); /* avoid tailcall */ \
496 #define _os_trace_with_payload_4(_l, _m, _t, _1, _2, _3, _payload) __extension__({ \
497 _os_trace_verify_printf(_l, _1, _2, _3); \
499 size_t buf_size = _os_trace_encode(buf, sizeof(buf), _m, _1, _2, _3); \
500 _os_trace_internal(&__dso_handle, _t, _m, buf, buf_size, _payload); \
501 __asm__(""); /* avoid tailcall */ \
504 #define _os_trace_with_payload_5(_l, _m, _t, _1, _2, _3, _4, _payload) __extension__({ \
505 _os_trace_verify_printf(_l, _1, _2, _3, _4); \
507 size_t buf_size = _os_trace_encode(buf, sizeof(buf), _m, _1, _2, _3, _4); \
508 _os_trace_internal(&__dso_handle, _t, _m, buf, buf_size, _payload); \
509 __asm__(""); /* avoid tailcall */ \
512 #define _os_trace_with_payload_6(_l, _m, _t, _1, _2, _3, _4, _5, _payload) __extension__({ \
513 _os_trace_verify_printf(_l, _1, _2, _3, _4, _5); \
515 size_t buf_size = _os_trace_encode(buf, sizeof(buf), _m, _1, _2, _3, _4, _5); \
516 _os_trace_internal(&__dso_handle, _t, _m, buf, buf_size, _payload); \
517 __asm__(""); /* avoid tailcall */ \
520 #define _os_trace_with_payload_7(_l, _m, _t, _1, _2, _3, _4, _5, _6, _payload) __extension__({ \
521 _os_trace_verify_printf(_l, _1, _2, _3, _4, _5, _6); \
523 size_t buf_size = _os_trace_encode(buf, sizeof(buf), _m, _1, _2, _3, _4, _5, _6); \
524 _os_trace_internal(&__dso_handle, _t, _m, buf, buf_size, _payload); \
525 __asm__(""); /* avoid tailcall */ \
528 #define _os_trace_with_payload_8(_l, _m, _t, _1, _2, _3, _4, _5, _6, _7, _payload) __extension__({ \
529 _os_trace_verify_printf(_l, _1, _2, _3, _4, _5, _6, _7); \
531 size_t buf_size = _os_trace_encode(buf, sizeof(buf), _m, _1, _2, _3, _4, _5, _6, _7); \
532 _os_trace_internal(&__dso_handle, _t, _m, buf, buf_size, _payload); \
533 __asm__(""); /* avoid tailcall */ \
536 #endif /* if Mac OS >= 10.12 or iPhone OS >= 10.0 */
541 * Hashtags in trace messages
544 * Developers are encouraged to include hashtags in log messages, regardless of what API you use.
545 * A hashtag is composed of a hash (#) symbol, followed by at least three non-whitespace characters,
546 * terminated by whitespace or the end of the message. Hashtags may not begin with a number.
548 * Below is the list of predefined tags:
549 * #System - Message in the context of a system process.
550 * #User - Message in the context of a user process.
551 * #Developer - Message in the context of software development. For example, deprecated APIs and debugging messages.
552 * #Attention - Message that should be investigated by a system administrator, because it may be a sign of a larger issue.
553 * For example, errors from a hard drive controller that typically occur when the drive is about to fail.
554 * #Critical - Message in the context of a critical event or critical failure.
555 * #Error - Message that is a noncritical error.
556 * #Comment - Message that is a comment.
557 * #Marker - Message that marks a change to divide the messages around it into those before and those after the change.
558 * #Clue - Message containing extra key/value pairs with additional information to help reconstruct the context.
559 * #Security - Message related to security concerns.
560 * #Filesystem - Message describing a file system related event.
561 * #Network - Message describing a network-related event.
562 * #Hardware - Message describing a hardware-related event.
563 * #CPU - Message describing CPU related event, e.g., initiating heavy work load
564 * #State - Message describing state changed, e.g., global state, preference, etc.
565 * #Graphics - Message describing significant graphics event
566 * #Disk - Message describing disk activity
570 #pragma mark - Other defines
573 * @define OS_TRACE_TYPE_RELEASE
574 * Trace messages to be captured on a typical user install. These should be
575 * limited to things which improve diagnosis of a failure/crash/hang. Trace
576 * buffers are generally smaller on a production system.
578 #define OS_TRACE_TYPE_RELEASE (1u << 0)
581 * @define OS_TRACE_TYPE_DEBUG
582 * Trace messages to be captured while debugger or other development tool is
583 * attached to the originator.
585 #define OS_TRACE_TYPE_DEBUG (1u << 1)
588 * @define OS_TRACE_TYPE_INFO
589 * Trace messages that are captured when a debugger is attached, system or
590 * Application mode has been increased to include additional information.
592 #define OS_TRACE_TYPE_INFO (1u << 2)
595 * @define OS_TRACE_TYPE_ERROR
596 * Trace the message as an error and force a collection as a failure may be
599 #define OS_TRACE_TYPE_ERROR ((1u << 6) | (1u << 0))
602 * @define OS_TRACE_TYPE_FAULT
603 * Trace the message as a fatal error which forces a collection and a diagnostic
606 #define OS_TRACE_TYPE_FAULT ((1u << 7) | (1u << 6) | (1u << 0))
609 * @typedef os_trace_payload_t
610 * A block that populates an xpc_object_t of type XPC_TYPE_DICTIONARY to represent
611 * complex data. This block will only be invoked under conditions where tools
612 * have attached to the process. The payload can be used to send arbitrary data
613 * via the trace call. Tools may use the data to validate state for integration
614 * tests or provide other introspection services. No assumptions are made about
615 * the format or structure of the data.
617 typedef void (^os_trace_payload_t
)(xpc_object_t xdict
);
619 #pragma mark - function declarations
625 * Always inserts a trace message into a buffer pool for later decoding.
628 * Trace message that will be recorded on a typical user install. These should
629 * be limited to things which help diagnose a failure during postmortem
630 * analysis. Trace buffers are generally smaller on a production system.
633 * A printf-style format string to generate a human-readable log message when
634 * the trace line is decoded. Only scalar types are supported, attempts
635 * to pass arbitrary strings will store a pointer that is unresolvable and
636 * will generate an error during decode.
638 * os_trace("network event: %ld, last seen: %ld, avg: %g", event_id, last_seen, avg);
640 #define os_trace(format, ...) __extension__({ \
641 _Static_assert(__builtin_constant_p(format), "format must be a constant string"); \
642 __attribute__((section("__TEXT,__os_trace"))) static const char _m[] = format; \
643 OS_TRACE_CALL(format, _m, OS_TRACE_TYPE_RELEASE, ##__VA_ARGS__); \
647 #if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_10_0) \
648 || (defined(__WATCH_OS_VERSION_MIN_REQUIRED) && __WATCH_OS_VERSION_MIN_REQUIRED >= __WATCHOS_3_0) \
649 || (defined(__TV_OS_VERSION_MIN_REQUIRED) && __TV_OS_VERSION_MIN_REQUIRED >= __TVOS_10_0) \
650 || (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_12)
653 * @function os_trace_info
656 * Optionally inserts a trace message containing additional information into a
657 * buffer pool for later decoding.
660 * Trace messages that will be captured when additional information is needed
661 * and are not captured by default. They will only be captured if the
662 * system/process/activity mode has been increased or if a Development tool has
663 * been attached to the process.
666 * A printf-style format string that represents a human-readable message when
667 * the trace line is decoded. Only scalar types are supported, attempts
668 * to pass arbitrary strings will store a pointer that is unresolvable and
669 * will generate an error during decode.
671 * os_trace_info("network interface status %ld", status);
673 #define os_trace_info(format, ...) __extension__({ \
674 _Static_assert(__builtin_constant_p(format), "format must be a constant string"); \
675 __attribute__((section("__TEXT,__os_trace"))) static const char _m[] = format; \
676 OS_TRACE_CALL(format, _m, OS_TRACE_TYPE_INFO, ##__VA_ARGS__); \
682 * @function os_trace_debug
685 * Insert debug trace message into a buffer pool for later decoding.
688 * Debug trace message to be recorded while debugger or other development tool is
689 * attached to the originator. This is transported interprocess to help
690 * diagnose the entire call chain including external helpers.
693 * A printf-style format string that represents a human-readable message when
694 * the trace line is decoded. Only scalar types are supported, attempts
695 * to pass arbitrary strings will store a pointer that is unresolvable and
696 * will generate an error during decode.
698 * os_trace_debug("network interface status %ld", status);
700 #define os_trace_debug(format, ...) __extension__({ \
701 _Static_assert(__builtin_constant_p(format), "format must be a constant string"); \
702 __attribute__((section("__TEXT,__os_trace"))) static const char _m[] = format; \
703 OS_TRACE_CALL(format, _m, OS_TRACE_TYPE_DEBUG, ##__VA_ARGS__); \
707 * @function os_trace_info_enabled
710 * Avoid unnecessary work for a trace point by checking if additional information
714 * Avoid unnecessary work for a trace point by checking if additional information
715 * is enabled. Generally trace points should not involve expensive operations, but some
716 * circumstances warrant it. Use this function to avoid doing the work unless
717 * debug level trace messages are requested.
719 * if (os_trace_info_enabled()) {
720 * os_trace_info("value = %d, average = %d",
721 * [[dict objectForKey: @"myKey"] intValue],
722 * (int) [self getAverage: dict]);
726 * Returns true if development mode is enabled.
728 __OSX_AVAILABLE_STARTING(__MAC_10_12
, __IPHONE_10_0
)
729 OS_EXPORT OS_NOTHROW OS_WARN_RESULT
731 os_trace_info_enabled(void);
734 * @function os_trace_debug_enabled
737 * Avoid unnecessary work for a trace point by checking if debug level is enabled.
740 * Avoid unnecessary work for a trace point by checking if debug level is enabled.
741 * Generally trace points should not involve expensive operations, but some
742 * circumstances warrant it. Use this function to avoid doing the work unless
743 * debug level trace messages are requested.
745 * if (os_trace_debug_enabled()) {
746 * os_trace_debug("value = %d, average = %d",
747 * [[dict objectForKey: @"myKey"] intValue],
748 * (int) [self getAverage: dict]);
752 * Returns true if debug mode is enabled.
754 __OSX_AVAILABLE(10.10) __IOS_AVAILABLE(8.0) __WATCHOS_AVAILABLE(1.0) __TVOS_AVAILABLE(9.0)
755 OS_EXPORT OS_NOTHROW OS_WARN_RESULT
757 os_trace_debug_enabled(void);
760 * @function os_trace_error
763 * Trace the message as an error and force a collection of the trace buffer as a
764 * failure may be imminent.
767 * Trace the message as an error and force a collection of the trace buffer as a
768 * failure may be imminent.
771 * A printf-style format string to generate a human-readable log message when
772 * the trace line is decoded. Only scalar types are supported, attempts
773 * to pass arbitrary strings will store a pointer that is unresolvable and
774 * will generate an error during decode.
776 * os_trace_error("socket %d connection timeout %ld", fd, secs);
778 #define os_trace_error(format, ...) __extension__({ \
779 _Static_assert(__builtin_constant_p(format), "format must be a constant string"); \
780 __attribute__((section("__TEXT,__os_trace"))) static const char _m[] = format; \
781 OS_TRACE_CALL(format, _m, OS_TRACE_TYPE_ERROR, ##__VA_ARGS__); \
785 * @function os_trace_fault
788 * Trace the message as a fault which forces a collection of the trace buffer
789 * and diagnostic of the activity.
792 * Trace the message as a fault which forces a collection of the trace buffer
793 * and diagnostic of the activity.
796 * A printf-style format string to generate a human-readable log message when
797 * the trace line is decoded. Only scalar types are supported, attempts
798 * to pass arbitrary strings will store a pointer that is unresolvable and
799 * will generate an error during decode.
801 * os_trace_fault("failed to lookup uid %d - aborting", uid);
803 #define os_trace_fault(format, ...) __extension__({ \
804 _Static_assert(__builtin_constant_p(format), "format must be a constant string"); \
805 __attribute__((section("__TEXT,__os_trace"))) static const char _m[] = format; \
806 OS_TRACE_CALL(format, _m, OS_TRACE_TYPE_FAULT, ##__VA_ARGS__); \
809 #if __has_include(<xpc/xpc.h>)
811 * @function os_trace_with_payload
814 * Add a trace entry containing the provided values and call the block if
818 * Will insert a trace entry into a limited ring buffer for an activity or
819 * process. Trace points are for recording interesting data that would improve
820 * diagnosis of unexpected crashes, failures and hangs. The block will only be
821 * called under the required conditions.
824 * A printf-style format string to generate a human-readable log message when
825 * the trace line is decoded. Only scalar types are supported. Attempts
826 * to pass arbitrary strings will store a pointer that is unresolvable and
827 * will generate an error during decode.
829 * The final parameter must be a block of type os_trace_payload_t.
831 * os_trace_with_payload("network event %ld", event, ^(xpc_object_t xdict) {
833 * // validate the network interface and address where what was expected
834 * xpc_dictionary_set_string(xdict, "network", ifp->ifa_name);
835 * xpc_dictionary_set_string(xdict, "ip_address", _get_address(ifp));
838 #define os_trace_with_payload(format, ...) __extension__({ \
839 _Static_assert(__builtin_constant_p(format), "format must be a constant string"); \
840 __attribute__((section("__TEXT,__os_trace"))) static const char _m[] = format; \
841 OS_CONCAT(_os_trace_with_payload, OS_COUNT_ARGS(__VA_ARGS__))(format, _m, OS_TRACE_TYPE_RELEASE, ##__VA_ARGS__); \
844 #define os_trace_info_with_payload(format, ...) __extension__({ \
845 _Static_assert(__builtin_constant_p(format), "format must be a constant string"); \
846 __attribute__((section("__TEXT,__os_trace"))) static const char _m[] = format; \
847 OS_CONCAT(_os_trace_with_payload, OS_COUNT_ARGS(__VA_ARGS__))(format, _m, OS_TRACE_TYPE_INFO, ##__VA_ARGS__); \
850 #define os_trace_debug_with_payload(format, ...) __extension__({ \
851 _Static_assert(__builtin_constant_p(format), "format must be a constant string"); \
852 __attribute__((section("__TEXT,__os_trace"))) static const char _m[] = format; \
853 OS_CONCAT(_os_trace_with_payload, OS_COUNT_ARGS(__VA_ARGS__))(format, _m, OS_TRACE_TYPE_DEBUG, ##__VA_ARGS__); \
856 #define os_trace_error_with_payload(format, ...) __extension__({ \
857 _Static_assert(__builtin_constant_p(format), "format must be a constant string"); \
858 __attribute__((section("__TEXT,__os_trace"))) static const char _m[] = format; \
859 OS_CONCAT(_os_trace_with_payload, OS_COUNT_ARGS(__VA_ARGS__))(format, _m, OS_TRACE_TYPE_ERROR, ##__VA_ARGS__); \
862 #define os_trace_fault_with_payload(format, ...) __extension__({ \
863 _Static_assert(__builtin_constant_p(format), "format must be a constant string"); \
864 __attribute__((section("__TEXT,__os_trace"))) static const char _m[] = format; \
865 OS_CONCAT(_os_trace_with_payload, OS_COUNT_ARGS(__VA_ARGS__))(format, _m, OS_TRACE_TYPE_FAULT, ##__VA_ARGS__); \
868 #endif // __has_include(<xpc/xpc.h>)
870 // TODO: change this once we have compiler support
871 __OSX_AVAILABLE_STARTING(__MAC_10_12
, __IPHONE_10_0
)
874 _os_trace_encode(uint8_t *buf
, size_t buf_size
, const char *format
, ...);
876 __OSX_AVAILABLE_STARTING(__MAC_10_12
, __IPHONE_10_0
)
879 _os_trace_internal(void *dso
, uint8_t type
, const char *format
, const uint8_t *buf
, size_t buf_size
, os_trace_payload_t payload
);
882 * @function _os_trace_with_buffer
885 * Internal function to support pre-encoded buffer.
887 __OSX_AVAILABLE(10.10) __IOS_AVAILABLE(8.0) __WATCHOS_AVAILABLE(1.0) __TVOS_AVAILABLE(9.0)
890 _os_trace_with_buffer(void *dso
, const char *message
, uint8_t type
, const void *buffer
, size_t buffer_size
, os_trace_payload_t payload
);
894 #endif // __OS_TRACE_H__