2 * Copyright (c) 2013-2014 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
21 #ifndef __OS_VOUCHER_ACTIVITY_PRIVATE__
22 #define __OS_VOUCHER_ACTIVITY_PRIVATE__
25 #include <os/object.h>
26 #if !defined(__DISPATCH_BUILDING_DISPATCH__)
27 #include <os/voucher_private.h>
30 #define OS_VOUCHER_ACTIVITY_SPI_VERSION 20140708
32 #if OS_VOUCHER_WEAK_IMPORT
33 #define OS_VOUCHER_EXPORT OS_EXPORT OS_WEAK_IMPORT
35 #define OS_VOUCHER_EXPORT OS_EXPORT
40 #if OS_VOUCHER_ACTIVITY_SPI
43 * @group Voucher Activity SPI
44 * SPI intended for libtrace only
48 * @typedef voucher_activity_id_t
51 * Opaque activity identifier.
54 * Scalar value type, not reference counted.
56 typedef uint64_t voucher_activity_id_t
;
59 * @enum voucher_activity_tracepoint_type_t
62 * Types of tracepoints.
64 OS_ENUM(voucher_activity_tracepoint_type
, uint8_t,
65 voucher_activity_tracepoint_type_release
= (1u << 0),
66 voucher_activity_tracepoint_type_debug
= (1u << 1),
67 voucher_activity_tracepoint_type_error
= (1u << 6) | (1u << 0),
68 voucher_activity_tracepoint_type_fault
= (1u << 7) | (1u << 6) | (1u << 0),
72 * @enum voucher_activity_flag_t
75 * Flags to pass to voucher_activity_start/voucher_activity_start_with_location
77 OS_ENUM(voucher_activity_flag
, unsigned long,
78 voucher_activity_flag_default
= 0,
79 voucher_activity_flag_force
= 0x1,
83 * @typedef voucher_activity_trace_id_t
86 * Opaque tracepoint identifier.
88 typedef uint64_t voucher_activity_trace_id_t
;
89 static const uint8_t _voucher_activity_trace_id_type_shift
= 40;
90 static const uint8_t _voucher_activity_trace_id_code_namespace_shift
= 32;
93 * @function voucher_activity_trace_id
96 * Return tracepoint identifier for specified arguments.
99 * Tracepoint type from voucher_activity_tracepoint_type_t.
101 * @param code_namespace
102 * Namespace of 'code' argument.
108 * Tracepoint identifier.
110 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
111 OS_INLINE OS_ALWAYS_INLINE
112 voucher_activity_trace_id_t
113 voucher_activity_trace_id(uint8_t type
, uint8_t code_namespace
, uint32_t code
)
115 return ((voucher_activity_trace_id_t
)type
<<
116 _voucher_activity_trace_id_type_shift
) |
117 ((voucher_activity_trace_id_t
)code_namespace
<<
118 _voucher_activity_trace_id_code_namespace_shift
) |
119 (voucher_activity_trace_id_t
)code
;
123 * @function voucher_activity_start
126 * Creates a new activity identifier and marks the current thread as
127 * participating in the activity.
130 * As part of voucher transport, activities are automatically propagated by the
131 * system to other threads and processes (across IPC).
133 * Activities persist as long as any threads in any process are marked as
134 * participating. There may be many calls to voucher_activity_end()
135 * corresponding to one call to voucher_activity_start().
138 * Tracepoint identifier returned by voucher_activity_trace_id(), intended for
139 * identification of the automatic tracepoint generated as part of creating the
143 * Pass voucher_activity_flag_force to indicate that existing activities
144 * on the current thread should not be inherited and that a new toplevel
145 * activity should be created.
148 * A new activity identifier.
150 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
151 OS_VOUCHER_EXPORT OS_WARN_RESULT OS_NOTHROW
152 voucher_activity_id_t
153 voucher_activity_start(voucher_activity_trace_id_t trace_id
,
154 voucher_activity_flag_t flags
);
157 * @function voucher_activity_start_with_location
160 * Creates a new activity identifier and marks the current thread as
161 * participating in the activity.
164 * As part of voucher transport, activities are automatically propagated by the
165 * system to other threads and processes (across IPC).
167 * Activities persist as long as any threads in any process are marked as
168 * participating. There may be many calls to voucher_activity_end()
169 * corresponding to one call to voucher_activity_start_with_location().
172 * Tracepoint identifier returned by voucher_activity_trace_id(), intended for
173 * identification of the automatic tracepoint generated as part of creating the
177 * Location identifier for the automatic tracepoint generated as part of
178 * creating the new activity.
181 * Pass voucher_activity_flag_force to indicate that existing activities
182 * on the current thread should not be inherited and that a new toplevel
183 * activity should be created.
186 * A new activity identifier.
188 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
189 OS_VOUCHER_EXPORT OS_WARN_RESULT OS_NOTHROW
190 voucher_activity_id_t
191 voucher_activity_start_with_location(voucher_activity_trace_id_t trace_id
,
192 uint64_t location
, voucher_activity_flag_t flags
);
195 * @function voucher_activity_end
198 * Unmarks the current thread if it is marked as particpating in the activity
199 * with the specified identifier.
202 * Activities persist as long as any threads in any process are marked as
203 * participating. There may be many calls to voucher_activity_end()
204 * corresponding to one call to voucher_activity_start() or
205 * voucher_activity_start_with_location().
207 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
208 OS_VOUCHER_EXPORT OS_NOTHROW
210 voucher_activity_end(voucher_activity_id_t activity_id
);
213 * @function voucher_get_activities
216 * Returns the list of activity identifiers that the current thread is marked
220 * Pointer to an array of activity identifiers to be filled in.
223 * Pointer to the requested number of activity identifiers.
224 * On output will be filled with the number of activities that are available.
227 * Number of activity identifiers written to 'entries'
229 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
230 OS_VOUCHER_EXPORT OS_NOTHROW
232 voucher_get_activities(voucher_activity_id_t
*entries
, unsigned int *count
);
235 * @group Voucher Activity Trace SPI
236 * SPI intended for libtrace only
240 * @function voucher_activity_get_namespace
243 * Returns the namespace of the current activity.
246 * The namespace of the current activity (if any).
248 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
249 OS_VOUCHER_EXPORT OS_NOTHROW
251 voucher_activity_get_namespace(void);
254 * @function voucher_activity_trace
257 * Add a tracepoint to trace buffer of the current activity.
260 * Tracepoint identifier returned by voucher_activity_trace_id()
263 * Tracepoint location.
266 * Pointer to packed buffer of tracepoint data.
269 * Length of data at 'buffer'.
272 * Timestamp recorded in tracepoint or 0 if no tracepoint was recorded.
274 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
275 OS_VOUCHER_EXPORT OS_NOTHROW
277 voucher_activity_trace(voucher_activity_trace_id_t trace_id
, uint64_t location
,
278 void *buffer
, size_t length
);
281 * @function voucher_activity_trace_args
284 * Add a tracepoint to trace buffer of the current activity, recording
285 * specified arguments passed in registers.
288 * Tracepoint identifier returned by voucher_activity_trace_id()
291 * Tracepoint location.
294 * Argument to be recorded in tracepoint data.
297 * Argument to be recorded in tracepoint data.
300 * Argument to be recorded in tracepoint data.
303 * Argument to be recorded in tracepoint data.
306 * Timestamp recorded in tracepoint or 0 if no tracepoint was recorded.
308 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
309 OS_VOUCHER_EXPORT OS_NOTHROW
311 voucher_activity_trace_args(voucher_activity_trace_id_t trace_id
,
312 uint64_t location
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
,
316 * @group Voucher Activity Mode SPI
317 * SPI intended for libtrace only
321 * @enum voucher_activity_mode_t
324 * Voucher activity mode.
327 * Configure at process start by setting the OS_ACTIVITY_MODE environment
330 OS_ENUM(voucher_activity_mode
, unsigned long,
331 voucher_activity_mode_disable
= 0,
332 voucher_activity_mode_release
= (1u << 0),
333 voucher_activity_mode_debug
= (1u << 1),
334 voucher_activity_mode_stream
= (1u << 2),
338 * @function voucher_activity_get_mode
341 * Return current mode of voucher activity subsystem.
344 * Value from voucher_activity_mode_t enum.
346 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
347 OS_VOUCHER_EXPORT OS_WARN_RESULT OS_NOTHROW
348 voucher_activity_mode_t
349 voucher_activity_get_mode(void);
352 * @function voucher_activity_set_mode_4libtrace(void)
355 * Set the current mode of voucher activity subsystem.
360 * Note that the new mode will take effect soon, but not immediately.
362 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
363 OS_VOUCHER_EXPORT OS_NOTHROW
365 voucher_activity_set_mode_4libtrace(voucher_activity_mode_t mode
);
368 * @group Voucher Activity Metadata SPI
369 * SPI intended for libtrace only
373 * @function voucher_activity_get_metadata_buffer
376 * Return address and length of buffer in the process trace memory area
377 * reserved for libtrace metadata.
380 * Pointer to size_t variable, filled with length of metadata buffer.
383 * Address of metadata buffer.
385 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
386 OS_VOUCHER_EXPORT OS_WARN_RESULT OS_NOTHROW OS_NONNULL_ALL
388 voucher_activity_get_metadata_buffer(size_t *length
);
390 #endif // OS_VOUCHER_ACTIVITY_SPI
392 #if OS_VOUCHER_ACTIVITY_BUFFER_SPI
395 * @group Voucher Activity Tracepoint SPI
396 * SPI intended for diagnosticd only
399 OS_ENUM(_voucher_activity_tracepoint_flag
, uint16_t,
400 _voucher_activity_trace_flag_buffer_empty
= 0,
401 _voucher_activity_trace_flag_tracepoint
= (1u << 0),
402 _voucher_activity_trace_flag_tracepoint_args
= (1u << 1),
403 _voucher_activity_trace_flag_wide_first
= (1u << 6),
404 _voucher_activity_trace_flag_wide_second
= (1u << 6) | (1u << 7),
405 _voucher_activity_trace_flag_start
= (1u << 8),
406 _voucher_activity_trace_flag_end
= (1u << 8) | (1u << 9),
407 _voucher_activity_trace_flag_libdispatch
= (1u << 13),
408 _voucher_activity_trace_flag_activity
= (1u << 14),
409 _voucher_activity_trace_flag_buffer_header
= (1u << 15),
412 // for tracepoints with _voucher_activity_trace_flag_libdispatch
413 OS_ENUM(_voucher_activity_tracepoint_namespace
, uint8_t,
414 _voucher_activity_tracepoint_namespace_ipc
= 0x1
416 OS_ENUM(_voucher_activity_tracepoint_code
, uint32_t,
417 _voucher_activity_tracepoint_namespace_ipc_send
= 0x1,
418 _voucher_activity_tracepoint_namespace_ipc_receive
= 0x2,
421 typedef struct _voucher_activity_tracepoint_s
{
422 uint16_t vat_flags
; // voucher_activity_tracepoint_flag_t
423 uint8_t vat_type
; // voucher_activity_tracepoint_type_t
424 uint8_t vat_namespace
; // namespace for tracepoint code
425 uint32_t vat_code
; // tracepoint code
426 uint64_t vat_thread
; // pthread_t
427 uint64_t vat_timestamp
; // absolute time
428 uint64_t vat_location
; // tracepoint PC
429 uint64_t vat_data
[4]; // trace data
430 } *_voucher_activity_tracepoint_t
;
433 * @group Voucher Activity Buffer Internals
434 * SPI intended for diagnosticd only
435 * Layout of structs is subject to change without notice
438 #include <sys/queue.h>
439 #include <atm/atm_types.h>
440 #include <os/lock_private.h>
442 static const atm_subaid32_t _voucher_default_activity_subid
=
445 static const size_t _voucher_activity_buffer_size
= 4096;
446 static const size_t _voucher_activity_tracepoints_per_buffer
=
447 _voucher_activity_buffer_size
/
448 sizeof(struct _voucher_activity_tracepoint_s
);
449 typedef uint8_t _voucher_activity_buffer_t
[_voucher_activity_buffer_size
];
451 struct _voucher_activity_self_metadata_s
{
452 struct _voucher_activity_metadata_opaque_s
*vasm_baseaddr
;
454 typedef struct _voucher_activity_metadata_opaque_s
{
455 _voucher_activity_buffer_t vam_kernel_metadata
;
456 _voucher_activity_buffer_t vam_client_metadata
;
458 struct _voucher_activity_self_metadata_s vam_self_metadata
;
459 _voucher_activity_buffer_t vam_self_metadata_opaque
;
461 } *_voucher_activity_metadata_opaque_t
;
463 typedef os_lock_handoff_s _voucher_activity_lock_s
;
465 typedef struct _voucher_atm_s
{
466 int32_t volatile vatm_refcnt
;
467 mach_voucher_t vatm_kvoucher
;
469 atm_mailbox_offset_t vatm_mailbox_offset
;
470 TAILQ_ENTRY(_voucher_atm_s
) vatm_list
;
472 uintptr_t vatm_pad
[3];
475 _voucher_activity_lock_s vatm_activities_lock
;
476 TAILQ_HEAD(_voucher_atm_activities_s
, _voucher_activity_s
) vatm_activities
;
477 TAILQ_HEAD(, _voucher_activity_s
) vatm_used_activities
;
480 // must match layout of _voucher_activity_tracepoint_s
481 typedef struct _voucher_activity_buffer_header_s
{
482 uint16_t vabh_flags
; // _voucher_activity_trace_flag_buffer_header
483 uint8_t vabh_unused
[6];
484 uint64_t vabh_thread
;
485 uint64_t vabh_timestamp
;
486 uint32_t volatile vabh_next_tracepoint_idx
;
487 uint32_t vabh_sequence_no
;
488 voucher_activity_id_t vabh_activity_id
;
489 uint64_t vabh_reserved
;
490 TAILQ_ENTRY(_voucher_activity_buffer_header_s
) vabh_list
;
491 } *_voucher_activity_buffer_header_t
;
493 // must match layout of _voucher_activity_buffer_header_s
494 typedef struct _voucher_activity_s
{
495 // first tracepoint entry
496 // must match layout of _voucher_activity_tracepoint_s
497 uint16_t va_flags
; // _voucher_activity_trace_flag_buffer_header |
498 // _voucher_activity_trace_flag_activity |
499 // _voucher_activity_trace_flag_start |
500 // _voucher_activity_trace_flag_wide_first
502 uint8_t va_namespace
;
505 uint64_t va_timestamp
;
506 uint32_t volatile vabh_next_tracepoint_idx
;
507 uint32_t volatile va_max_sequence_no
;
508 voucher_activity_id_t va_id
;
509 int32_t volatile va_use_count
;
510 uint32_t va_buffer_limit
;
511 TAILQ_HEAD(_voucher_activity_buffer_list_s
,
512 _voucher_activity_buffer_header_s
) va_buffers
;
517 // second tracepoint entry
518 // must match layout of _voucher_activity_tracepoint_s
520 uint8_t va_unused2
[2];
521 int32_t volatile va_refcnt
;
522 uint64_t va_location
;
523 _voucher_activity_buffer_header_t
volatile va_current_buffer
;
524 _voucher_atm_t va_atm
;
525 _voucher_activity_lock_s va_buffers_lock
;
526 uintptr_t va_pad2
[2];
529 // third tracepoint entry
530 // must match layout of _voucher_activity_tracepoint_s
532 uint8_t va_unused3
[6];
535 TAILQ_ENTRY(_voucher_activity_s
) va_list;
536 TAILQ_ENTRY(_voucher_activity_s
) va_atm_list
;
537 TAILQ_ENTRY(_voucher_activity_s
) va_atm_used_list
;
538 } *_voucher_activity_t
;
540 #endif // OS_VOUCHER_ACTIVITY_BUFFER_SPI
544 #endif // __OS_VOUCHER_ACTIVITY_PRIVATE__