]> git.saurik.com Git - apple/xnu.git/blame - libkern/firehose/tracepoint_private.h
xnu-7195.81.3.tar.gz
[apple/xnu.git] / libkern / firehose / tracepoint_private.h
CommitLineData
39037602
A
1/*
2 * Copyright (c) 2013-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20
21#ifndef __FIREHOSE_ACTIVITY__
22#define __FIREHOSE_ACTIVITY__
23
24#include <machine/cpu_capabilities.h>
25#include <mach/mach_time.h>
26#include <os/base.h>
27#if KERNEL
28#include <atm/atm_internal.h>
29#endif
f427ee49 30#include <os/atomic_private.h>
39037602
A
31#include "firehose_types_private.h"
32
33OS_ASSUME_NONNULL_BEGIN
34
35/*!
36 * @typedef firehose_tracepoint_id_u
37 *
38 * @abstract
39 * Broken down tracepoint identifier.
40 */
41typedef union {
42 struct {
43 firehose_tracepoint_namespace_t _namespace;
44 firehose_tracepoint_type_t _type;
45 firehose_tracepoint_flags_t _flags;
46 uint32_t _code;
47 } ftid;
48 firehose_tracepoint_id_t ftid_value;
f427ee49 49 os_atomic(firehose_tracepoint_id_t) ftid_atomic_value;
39037602
A
50} firehose_tracepoint_id_u;
51
52#define FIREHOSE_STAMP_SLOP (1ULL << 36) // ~1minute
53
54/*!
55 * @typedef firehose_trace_uuid_info_t
56 *
57 * @abstract
58 * Info needed by logd when kexts are loaded or unloaded
59 *
60 */
61typedef struct firehose_trace_uuid_info_s {
62 uuid_t ftui_uuid; /* uuid of binary */
63 uint64_t ftui_address; /* load address */
64 uint64_t ftui_size; /* load size */
65 char ftui_path[]; /* full path of binary - Unused in the kernel*/
66} *firehose_trace_uuid_info_t;
67
68/*!
69 * @typedef firehose_tracepoint_t
70 */
71typedef struct firehose_tracepoint_s {
72 firehose_tracepoint_id_u ft_id;
73 uint64_t ft_thread;
74 union {
75 struct {
76 uint64_t ft_timestamp_delta : 48;
77 uint64_t ft_length : 16;
78 };
79 uint64_t ft_stamp_and_length;
f427ee49 80 os_atomic(uint64_t) ft_atomic_stamp_and_length;
39037602
A
81 };
82 uint8_t ft_data[];
83} *firehose_tracepoint_t;
84
85#define FIREHOSE_TRACE_ID_MAKE(ns, type, flags, code) \
86 (((firehose_tracepoint_id_u){ .ftid = { \
0a7de745
A
87 ._namespace = ns, \
88 ._type = type, \
89 ._flags = flags, \
90 ._code = code, \
39037602
A
91 } }).ftid_value)
92
93#define FIREHOSE_TRACE_ID_SET_NS(tid, ns) \
94 ((tid).ftid._namespace = firehose_tracepoint_namespace_##ns)
95
96#define FIREHOSE_TRACE_ID_SET_TYPE(tid, ns, type) \
97 ((tid).ftid._type = _firehose_tracepoint_type_##ns##_##type)
98
813fb2f6 99#define FIREHOSE_TRACE_ID_PC_STYLE(tid) \
0a7de745 100 ((tid).ftid._flags & _firehose_tracepoint_flags_pc_style_mask)
813fb2f6
A
101
102#define FIREHOSE_TRACE_ID_SET_PC_STYLE(tid, flag) ({ \
0a7de745
A
103 firehose_tracepoint_id_u _tmp_tid = (tid); \
104 _tmp_tid.ftid._flags &= ~_firehose_tracepoint_flags_pc_style_mask; \
105 _tmp_tid.ftid._flags |= _firehose_tracepoint_flags_pc_style_##flag; \
813fb2f6
A
106})
107
39037602
A
108#define FIREHOSE_TRACE_ID_HAS_FLAG(tid, ns, flag) \
109 ((tid).ftid._flags & _firehose_tracepoint_flags_##ns##_##flag)
110#define FIREHOSE_TRACE_ID_SET_FLAG(tid, ns, flag) \
111 ((void)((tid).ftid._flags |= _firehose_tracepoint_flags_##ns##_##flag))
112#define FIREHOSE_TRACE_ID_CLEAR_FLAG(tid, ns, flag) \
113 ((void)((tid).ftid._flags &= ~_firehose_tracepoint_flags_##ns##_##flag))
114
115#define FIREHOSE_TRACE_ID_SET_CODE(tid, code) \
116 ((tid).ftid._code = code)
117
d9a64523
A
118/*!
119 * @typedef firehose_loss_payload_s
120 *
121 * @abstract
122 * The payload for tracepoints in the loss namespace, generated by the firehose
123 * itself when unreliable tracepoints are lost.
124 */
125typedef struct firehose_loss_payload_s {
126 uint64_t start_stamp; /* may (rarely!) disagree with the tracepoint stamp */
127 uint64_t end_stamp;
128#define FIREHOSE_LOSS_COUNT_WIDTH 6 /* as many bits as can be spared */
129#define FIREHOSE_LOSS_COUNT_MAX ((1u << FIREHOSE_LOSS_COUNT_WIDTH) - 1)
130 uint32_t count;
131} firehose_loss_payload_s, *firehose_loss_payload_t;
132
39037602
A
133__BEGIN_DECLS
134
135#if __has_feature(address_sanitizer)
136__attribute__((no_sanitize("address")))
137#endif
138__OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0)
139__TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0)
140OS_ALWAYS_INLINE
141static inline bool
142firehose_precise_timestamps_enabled(void)
143{
144#if KERNEL
145 return (atm_get_diagnostic_config() & 0x80) == 0;
146#else
147 return (*((volatile uint32_t *)_COMM_PAGE_ATM_DIAGNOSTIC_CONFIG) & 0x80) == 0;
148#endif
149}
150
151#if __has_feature(address_sanitizer)
152__attribute__((no_sanitize("address")))
153#endif
154__OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0)
155__TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0)
156OS_ALWAYS_INLINE
157static inline uint64_t
158firehose_tracepoint_time(firehose_activity_flags_t flags)
159{
160 if (firehose_precise_timestamps_enabled() ||
0a7de745 161 (flags & firehose_activity_flags_precise_timestamp)) {
39037602
A
162 return mach_continuous_time();
163 } else {
164 return mach_continuous_approximate_time();
165 }
166}
167
168#ifdef KERNEL
169__OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0)
170__TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0)
171void
172firehose_trace_metadata(firehose_stream_t stream, firehose_tracepoint_id_u ftid,
0a7de745 173 uint64_t stamp, const void* pubdata, size_t publen);
39037602
A
174#endif
175__END_DECLS
176
0a7de745 177 OS_ASSUME_NONNULL_END
39037602
A
178
179#endif // __FIREHOSE_FIREHOSE__