]> git.saurik.com Git - apple/dyld.git/blob - dyld3/Tracing.cpp
dyld-655.1.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
30 #include "Tracing.h"
31
32 namespace {
33 VIS_HIDDEN
34 static uint64_t elapsed(const time_value_t start, const time_value_t end) {
35 uint64_t duration;
36 duration = 1000000*(end.seconds - start.seconds);
37 duration += (end.microseconds - start.microseconds);
38 return duration;
39 }
40 }
41
42 namespace dyld3 {
43
44 VIS_HIDDEN
45 void kdebug_trace_dyld_image(const uint32_t code,
46 const uuid_t* uuid_bytes,
47 const fsobj_id_t fsobjid,
48 const fsid_t fsid,
49 const mach_header* load_addr)
50 {
51 #if __LP64__
52 uint64_t *uuid = (uint64_t *)uuid_bytes[0];
53 kdebug_trace(KDBG_CODE(DBG_DYLD, DBG_DYLD_UUID, code), uuid[0],
54 uuid[1], (uint64_t)load_addr,
55 (uint64_t)fsid.val[0] | ((uint64_t)fsid.val[1] << 32));
56 kdebug_trace(KDBG_CODE(DBG_DYLD, DBG_DYLD_UUID, code + 1),
57 (uint64_t)fsobjid.fid_objno |
58 ((uint64_t)fsobjid.fid_generation << 32),
59 0, 0, 0);
60 #else /* __LP64__ */
61 uint32_t *uuid = (uint32_t *)uuid_bytes[0];
62 kdebug_trace(KDBG_CODE(DBG_DYLD, DBG_DYLD_UUID, code + 2), uuid[0],
63 uuid[1], uuid[2], uuid[3]);
64 kdebug_trace(KDBG_CODE(DBG_DYLD, DBG_DYLD_UUID, code + 3),
65 (uint32_t)load_addr, fsid.val[0], fsid.val[1],
66 fsobjid.fid_objno);
67 kdebug_trace(KDBG_CODE(DBG_DYLD, DBG_DYLD_UUID, code + 4),
68 fsobjid.fid_generation, 0, 0, 0);
69 #endif /* __LP64__ */
70 }
71
72 // FIXME
73 // We get distinct copies of this in libdyld and dyld. Eventually we can fix it,
74 // for now we will just offset the values.
75
76 #if BUILDING_DYLD
77 static std::atomic<uint64_t> trace_pair_id(0);
78 #else
79 static std::atomic<uint64_t> trace_pair_id(1LL<<63);
80 #endif
81
82 VIS_HIDDEN
83 bool kdebug_trace_dyld_enabled(uint32_t code) {
84 return kdebug_is_enabled(code);
85 }
86
87 VIS_HIDDEN
88 void kdebug_trace_dyld_marker(uint32_t code, kt_arg data1, kt_arg data2, kt_arg data3, kt_arg data4) {
89 if (kdebug_is_enabled(code)) {
90 data1.prepare(code);
91 data2.prepare(code);
92 data3.prepare(code);
93 data4.prepare(code);
94 kdebug_trace(code, data1.value(), data2.value(), data3.value(), data4.value());
95 data4.destroy(code);
96 data3.destroy(code);
97 data2.destroy(code);
98 data1.destroy(code);
99 }
100 }
101
102 VIS_HIDDEN
103 uint64_t kdebug_trace_dyld_duration_start(uint32_t code, kt_arg data1, kt_arg data2, kt_arg data3) {
104 uint64_t result = 0;
105 if (kdebug_is_enabled(code)) {
106 result = ++trace_pair_id;
107 data1.prepare(code);
108 data2.prepare(code);
109 data3.prepare(code);
110 kdebug_trace(code | DBG_FUNC_START, result, data1.value(), data2.value(), data3.value());
111 data3.destroy(code);
112 data2.destroy(code);
113 data1.destroy(code);
114 }
115 return result;
116 }
117
118 VIS_HIDDEN
119 void kdebug_trace_dyld_duration_end(uint64_t trace_id, uint32_t code, kt_arg data1, kt_arg data2, kt_arg data3) {
120 if (trace_id != 0 && kdebug_is_enabled(code)) {
121 data1.prepare(code);
122 data2.prepare(code);
123 data3.prepare(code);
124 kdebug_trace(code | DBG_FUNC_END, trace_id, data1.value(), data2.value(), data3.value());
125 data3.destroy(code);
126 data2.destroy(code);
127 data1.destroy(code);
128 }
129 }
130
131 void ScopedTimer::startTimer() {
132 current_trace_id = kdebug_trace_dyld_duration_start(code, data1, data2, data3);
133 }
134
135 void ScopedTimer::endTimer() {
136 kdebug_trace_dyld_duration_end(current_trace_id, code, data4, data5, data6);
137 }
138
139 };