]> git.saurik.com Git - apple/dyld.git/blob - dyld3/Tracing.cpp
dyld-732.8.tar.gz
[apple/dyld.git] / dyld3 / Tracing.cpp
1 /*
2 * Copyright (c) 2017 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
25 #include <atomic>
26
27 #include <assert.h>
28 #include <mach/mach.h>
29 #include <kern/kcdata.h>
30 #include <mach-o/dyld_priv.h>
31
32 #include "Loading.h"
33 #include "Tracing.h"
34
35 // Workaround for header issues in rdar://49073930
36 // #include <System/os/reason_private.h>
37 extern "C" int
38 os_fault_with_payload(uint32_t reason_namespace, uint64_t reason_code,
39 void *payload, uint32_t payload_size, const char *reason_string,
40 uint64_t reason_flags) __attribute__((cold));
41
42 namespace dyld3 {
43
44 VIS_HIDDEN
45 void kdebug_trace_dyld_image(const uint32_t code,
46 const char* imagePath,
47 const uuid_t* uuid_bytes,
48 const fsobj_id_t fsobjid,
49 const fsid_t fsid,
50 const mach_header* load_addr)
51 {
52 uint64_t id = kdebug_trace_string(code, 0, imagePath);
53 #if __ARM_ARCH_7K__
54 uint32_t *uuid = (uint32_t *)uuid_bytes;
55 kdebug_trace(KDBG_CODE(DBG_DYLD, DBG_DYLD_UUID, code + 2), uuid[0],
56 uuid[1], uuid[2], uuid[3]);
57 kdebug_trace(KDBG_CODE(DBG_DYLD, DBG_DYLD_UUID, code + 3),
58 (uint32_t)load_addr, fsid.val[0], fsid.val[1],
59 fsobjid.fid_objno);
60 kdebug_trace(KDBG_CODE(DBG_DYLD, DBG_DYLD_UUID, code + 4),
61 fsobjid.fid_generation, id, 0, 0);
62 #else /* __ARM_ARCH_7K__ */
63 uint64_t *uuid = (uint64_t *)uuid_bytes;
64 kdebug_trace(KDBG_CODE(DBG_DYLD, DBG_DYLD_UUID, code), uuid[0],
65 uuid[1], (uint64_t)load_addr,
66 (uint64_t)fsid.val[0] | ((uint64_t)fsid.val[1] << 32));
67 kdebug_trace(KDBG_CODE(DBG_DYLD, DBG_DYLD_UUID, code + 1),
68 (uint64_t)fsobjid.fid_objno |
69 ((uint64_t)fsobjid.fid_generation << 32),
70 id, 0, 0);
71 #endif /* !__ARM_ARCH_7K__ */
72 kdebug_trace_string(code, id, nullptr);
73 }
74
75 // FIXME
76 // We get distinct copies of this in libdyld and dyld. Eventually we can fix it,
77 // for now we will just offset the values.
78
79 #if BUILDING_DYLD
80 static std::atomic<uint64_t> trace_pair_id(0);
81 #else
82 static std::atomic<uint64_t> trace_pair_id(1LL<<63);
83 #endif
84
85 VIS_HIDDEN
86 bool kdebug_trace_dyld_enabled(uint32_t code) {
87 return kdebug_is_enabled(code);
88 }
89
90 VIS_HIDDEN
91 void kdebug_trace_dyld_marker(uint32_t code, kt_arg data1, kt_arg data2, kt_arg data3, kt_arg data4) {
92 if (kdebug_is_enabled(code)) {
93 data1.prepare(code);
94 data2.prepare(code);
95 data3.prepare(code);
96 data4.prepare(code);
97 kdebug_trace(code, data1.value(), data2.value(), data3.value(), data4.value());
98 data4.destroy(code);
99 data3.destroy(code);
100 data2.destroy(code);
101 data1.destroy(code);
102 }
103 }
104
105 VIS_HIDDEN
106 uint64_t kdebug_trace_dyld_duration_start(uint32_t code, kt_arg data1, kt_arg data2, kt_arg data3) {
107 uint64_t result = 0;
108 if (kdebug_is_enabled(code)) {
109 result = ++trace_pair_id;
110 data1.prepare(code);
111 data2.prepare(code);
112 data3.prepare(code);
113 kdebug_trace(code | DBG_FUNC_START, result, data1.value(), data2.value(), data3.value());
114 data3.destroy(code);
115 data2.destroy(code);
116 data1.destroy(code);
117 }
118 return result;
119 }
120
121 VIS_HIDDEN
122 void kdebug_trace_dyld_duration_end(uint64_t trace_id, uint32_t code, kt_arg data1, kt_arg data2, kt_arg data3) {
123 if (trace_id != 0 && kdebug_is_enabled(code)) {
124 data1.prepare(code);
125 data2.prepare(code);
126 data3.prepare(code);
127 kdebug_trace(code | DBG_FUNC_END, trace_id, data1.value(), data2.value(), data3.value());
128 data3.destroy(code);
129 data2.destroy(code);
130 data1.destroy(code);
131 }
132 }
133
134 void ScopedTimer::startTimer() {
135 current_trace_id = kdebug_trace_dyld_duration_start(code, data1, data2, data3);
136 }
137
138 void ScopedTimer::endTimer() {
139 kdebug_trace_dyld_duration_end(current_trace_id, code, data4, data5, data6);
140 }
141
142 void syntheticBacktrace(const char *reason, bool enableExternally) {
143 if (!enableExternally && !internalInstall()) { return; }
144
145 char payloadBuffer[EXIT_REASON_PAYLOAD_MAX_LEN];
146 dyld_abort_payload* payload = (dyld_abort_payload*)payloadBuffer;
147 payload->version = 1;
148 payload->flags = 0;
149 payload->targetDylibPathOffset = 0;
150 payload->clientPathOffset = 0;
151 payload->symbolOffset = 0;
152 int payloadSize = sizeof(dyld_abort_payload);
153 char truncMessage[EXIT_REASON_USER_DESC_MAX_LEN];
154 strlcpy(truncMessage, reason, EXIT_REASON_USER_DESC_MAX_LEN);
155 os_fault_with_payload(OS_REASON_DYLD, DYLD_EXIT_REASON_OTHER, payloadBuffer, payloadSize, truncMessage, 0);
156 }
157
158 };