2 * Copyright (c) 2013-2016 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 __FIREHOSE_ACTIVITY__
22 #define __FIREHOSE_ACTIVITY__
24 #include <machine/cpu_capabilities.h>
25 #include <mach/mach_time.h>
28 #include <atm/atm_internal.h>
30 #if __has_include(<os/atomic_private.h>)
31 #include <os/atomic_private.h>
33 #include <os/internal/internal_shared.h>
35 #include "firehose_types_private.h"
37 OS_ASSUME_NONNULL_BEGIN
40 * @typedef firehose_tracepoint_id_u
43 * Broken down tracepoint identifier.
47 firehose_tracepoint_namespace_t _namespace
;
48 firehose_tracepoint_type_t _type
;
49 firehose_tracepoint_flags_t _flags
;
52 firehose_tracepoint_id_t ftid_value
;
53 os_atomic(firehose_tracepoint_id_t
) ftid_atomic_value
;
54 } firehose_tracepoint_id_u
;
56 #define FIREHOSE_STAMP_SLOP (1ULL << 36) // ~1minute
59 * @typedef firehose_trace_uuid_info_t
62 * Info needed by logd when kexts are loaded or unloaded
65 typedef struct firehose_trace_uuid_info_s
{
66 uuid_t ftui_uuid
; /* uuid of binary */
67 uint64_t ftui_address
; /* load address */
68 uint64_t ftui_size
; /* load size */
69 char ftui_path
[]; /* full path of binary - Unused in the kernel*/
70 } *firehose_trace_uuid_info_t
;
73 * @typedef firehose_tracepoint_t
75 typedef struct firehose_tracepoint_s
{
76 firehose_tracepoint_id_u ft_id
;
80 uint64_t ft_timestamp_delta
: 48;
81 uint64_t ft_length
: 16;
83 uint64_t ft_stamp_and_length
;
84 os_atomic(uint64_t) ft_atomic_stamp_and_length
;
87 } *firehose_tracepoint_t
;
89 #define FIREHOSE_TRACE_ID_MAKE(ns, type, flags, code) \
90 (((firehose_tracepoint_id_u){ .ftid = { \
97 #define FIREHOSE_TRACE_ID_SET_NS(tid, ns) \
98 ((tid).ftid._namespace = firehose_tracepoint_namespace_##ns)
100 #define FIREHOSE_TRACE_ID_SET_TYPE(tid, ns, type) \
101 ((tid).ftid._type = _firehose_tracepoint_type_##ns##_##type)
103 #define FIREHOSE_TRACE_ID_PC_STYLE(tid) \
104 ((tid).ftid._flags & _firehose_tracepoint_flags_pc_style_mask)
106 #define FIREHOSE_TRACE_ID_SET_PC_STYLE(tid, flag) ({ \
107 firehose_tracepoint_id_u _tmp_tid = (tid); \
108 _tmp_tid.ftid._flags &= ~_firehose_tracepoint_flags_pc_style_mask; \
109 _tmp_tid.ftid._flags |= _firehose_tracepoint_flags_pc_style_##flag; \
112 #define FIREHOSE_TRACE_ID_HAS_FLAG(tid, ns, flag) \
113 ((tid).ftid._flags & _firehose_tracepoint_flags_##ns##_##flag)
114 #define FIREHOSE_TRACE_ID_SET_FLAG(tid, ns, flag) \
115 ((void)((tid).ftid._flags |= _firehose_tracepoint_flags_##ns##_##flag))
116 #define FIREHOSE_TRACE_ID_CLEAR_FLAG(tid, ns, flag) \
117 ((void)((tid).ftid._flags &= ~_firehose_tracepoint_flags_##ns##_##flag))
119 #define FIREHOSE_TRACE_ID_SET_CODE(tid, code) \
120 ((tid).ftid._code = code)
123 * @typedef firehose_loss_payload_s
126 * The payload for tracepoints in the loss namespace, generated by the firehose
127 * itself when unreliable tracepoints are lost.
129 typedef struct firehose_loss_payload_s
{
130 uint64_t start_stamp
; /* may (rarely!) disagree with the tracepoint stamp */
132 #define FIREHOSE_LOSS_COUNT_WIDTH 6 /* as many bits as can be spared */
133 #define FIREHOSE_LOSS_COUNT_MAX ((1u << FIREHOSE_LOSS_COUNT_WIDTH) - 1)
135 } firehose_loss_payload_s
, *firehose_loss_payload_t
;
139 #if __has_feature(address_sanitizer)
140 __attribute__((no_sanitize("address")))
142 __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0)
143 __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0)
146 firehose_precise_timestamps_enabled(void)
149 return (atm_get_diagnostic_config() & 0x80) == 0;
151 return (*((volatile uint32_t *)_COMM_PAGE_ATM_DIAGNOSTIC_CONFIG
) & 0x80) == 0;
155 #if __has_feature(address_sanitizer)
156 __attribute__((no_sanitize("address")))
158 __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0)
159 __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0)
161 static inline uint64_t
162 firehose_tracepoint_time(firehose_activity_flags_t flags
)
164 if (firehose_precise_timestamps_enabled() ||
165 (flags
& firehose_activity_flags_precise_timestamp
)) {
166 return mach_continuous_time();
168 return mach_continuous_approximate_time();
173 __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0)
174 __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0)
176 firehose_trace_metadata(firehose_stream_t stream
, firehose_tracepoint_id_u ftid
,
177 uint64_t stamp
, const void* pubdata
, size_t publen
);
181 OS_ASSUME_NONNULL_END
183 #endif // __FIREHOSE_FIREHOSE__