2 * Copyright (c) 2017 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@
28 #include <mach/mach.h>
29 #include <kern/kcdata.h>
30 #include <mach-o/dyld_priv.h>
35 // Workaround for header issues in rdar://49073930
36 // #include <System/os/reason_private.h>
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
));
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
,
50 const mach_header
* load_addr
)
52 uint64_t id
= kdebug_trace_string(KDBG_CODE(DBG_DYLD
, DBG_DYLD_UUID
, code
), 0, imagePath
);
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],
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),
71 #endif /* !__ARM_ARCH_7K__ */
72 kdebug_trace_string(KDBG_CODE(DBG_DYLD
, DBG_DYLD_UUID
, code
), id
, nullptr);
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.
80 static std::atomic
<uint64_t> trace_pair_id(0);
82 static std::atomic
<uint64_t> trace_pair_id(1LL<<63);
86 bool kdebug_trace_dyld_enabled(uint32_t code
) {
87 return kdebug_is_enabled(code
);
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
)) {
97 kdebug_trace(code
, data1
.value(), data2
.value(), data3
.value(), data4
.value());
106 uint64_t kdebug_trace_dyld_duration_start(uint32_t code
, kt_arg data1
, kt_arg data2
, kt_arg data3
) {
108 if (kdebug_is_enabled(code
)) {
109 result
= ++trace_pair_id
;
113 kdebug_trace(code
| DBG_FUNC_START
, result
, data1
.value(), data2
.value(), data3
.value());
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
)) {
127 kdebug_trace(code
| DBG_FUNC_END
, trace_id
, data1
.value(), data2
.value(), data3
.value());
134 void ScopedTimer::startTimer() {
135 current_trace_id
= kdebug_trace_dyld_duration_start(code
, data1
, data2
, data3
);
138 void ScopedTimer::endTimer() {
139 kdebug_trace_dyld_duration_end(current_trace_id
, code
, data4
, data5
, data6
);
142 void syntheticBacktrace(const char *reason
, bool enableExternally
) {
143 if (!enableExternally
&& !internalInstall()) { return; }
145 char payloadBuffer
[EXIT_REASON_PAYLOAD_MAX_LEN
];
146 dyld_abort_payload
* payload
= (dyld_abort_payload
*)payloadBuffer
;
147 payload
->version
= 1;
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);