dyld-732.8.tar.gz
[apple/dyld.git] / dyld3 / Tracing.h
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 #ifndef Tracing_h
26 #define Tracing_h
27
28 #include <stdio.h>
29 #include <stdint.h>
30 #include <sys/stat.h>
31 #include <sys/param.h>
32 #include <sys/mount.h>
33 #include <uuid/uuid.h>
34 #include <mach-o/loader.h>
35 #include <System/sys/kdebug.h>
36 #include <System/sys/reason.h>
37
38
39 #define DBG_DYLD_INTERNAL_SUBCLASS (7)
40 #define DBG_DYLD_API_SUBCLASS (8)
41 #define DBG_DYLD_DEBUGGING_SUBCLASS (9)
42
43 #define DBG_DYLD_TIMING_STATIC_INITIALIZER (KDBG_CODE(DBG_DYLD, DBG_DYLD_INTERNAL_SUBCLASS, 0))
44 #define DBG_DYLD_TIMING_LAUNCH_EXECUTABLE (KDBG_CODE(DBG_DYLD, DBG_DYLD_INTERNAL_SUBCLASS, 1))
45 #define DBG_DYLD_TIMING_MAP_IMAGE (KDBG_CODE(DBG_DYLD, DBG_DYLD_INTERNAL_SUBCLASS, 2))
46 #define DBG_DYLD_TIMING_APPLY_FIXUPS (KDBG_CODE(DBG_DYLD, DBG_DYLD_INTERNAL_SUBCLASS, 3))
47 #define DBG_DYLD_TIMING_ATTACH_CODESIGNATURE (KDBG_CODE(DBG_DYLD, DBG_DYLD_INTERNAL_SUBCLASS, 4))
48 #define DBG_DYLD_TIMING_BUILD_CLOSURE (KDBG_CODE(DBG_DYLD, DBG_DYLD_INTERNAL_SUBCLASS, 5))
49 #define DBG_DYLD_TIMING_FUNC_FOR_ADD_IMAGE (KDBG_CODE(DBG_DYLD, DBG_DYLD_INTERNAL_SUBCLASS, 6))
50 #define DBG_DYLD_TIMING_FUNC_FOR_REMOVE_IMAGE (KDBG_CODE(DBG_DYLD, DBG_DYLD_INTERNAL_SUBCLASS, 7))
51 #define DBG_DYLD_TIMING_OBJC_INIT (KDBG_CODE(DBG_DYLD, DBG_DYLD_INTERNAL_SUBCLASS, 8))
52 #define DBG_DYLD_TIMING_OBJC_MAP (KDBG_CODE(DBG_DYLD, DBG_DYLD_INTERNAL_SUBCLASS, 9))
53 #define DBG_DYLD_TIMING_APPLY_INTERPOSING (KDBG_CODE(DBG_DYLD, DBG_DYLD_INTERNAL_SUBCLASS, 10))
54 #define DBG_DYLD_GDB_IMAGE_NOTIFIER (KDBG_CODE(DBG_DYLD, DBG_DYLD_INTERNAL_SUBCLASS, 11))
55 #define DBG_DYLD_REMOTE_IMAGE_NOTIFIER (KDBG_CODE(DBG_DYLD, DBG_DYLD_INTERNAL_SUBCLASS, 12))
56 #define DBG_DYLD_TIMING_BOOTSTRAP_START (KDBG_CODE(DBG_DYLD, DBG_DYLD_INTERNAL_SUBCLASS, 13))
57
58 #define DBG_DYLD_TIMING_DLOPEN (KDBG_CODE(DBG_DYLD, DBG_DYLD_API_SUBCLASS, 0))
59 #define DBG_DYLD_TIMING_DLOPEN_PREFLIGHT (KDBG_CODE(DBG_DYLD, DBG_DYLD_API_SUBCLASS, 1))
60 #define DBG_DYLD_TIMING_DLCLOSE (KDBG_CODE(DBG_DYLD, DBG_DYLD_API_SUBCLASS, 2))
61 #define DBG_DYLD_TIMING_DLSYM (KDBG_CODE(DBG_DYLD, DBG_DYLD_API_SUBCLASS, 3))
62 #define DBG_DYLD_TIMING_DLADDR (KDBG_CODE(DBG_DYLD, DBG_DYLD_API_SUBCLASS, 4))
63
64 #define DBG_DYLD_DEBUGGING_VM_REMAP (KDBG_CODE(DBG_DYLD, DBG_DYLD_DEBUGGING_SUBCLASS, 0))
65 #define DBG_DYLD_DEBUGGING_VM_UNMAP (KDBG_CODE(DBG_DYLD, DBG_DYLD_DEBUGGING_SUBCLASS, 1))
66 #define DBG_DYLD_DEBUGGING_MAP_LOOP (KDBG_CODE(DBG_DYLD, DBG_DYLD_DEBUGGING_SUBCLASS, 2))
67 #define DBG_DYLD_DEBUGGING_MARK (KDBG_CODE(DBG_DYLD, DBG_DYLD_DEBUGGING_SUBCLASS, 3))
68
69
70 #define VIS_HIDDEN __attribute__((visibility("hidden")))
71
72 namespace dyld3 {
73
74 enum class DyldTimingBuildClosure : uint64_t {
75 ClosureBuildFailure = 0,
76 LaunchClosure_Built = 1,
77 DlopenClosure_UsedSharedCacheDylib = 2,
78 DlopenClosure_UsedSharedCacheOther = 3,
79 DlopenClosure_NoLoad = 4,
80 DlopenClosure_Built = 5
81 };
82
83 struct VIS_HIDDEN kt_arg {
84 kt_arg(int value) : _value(value), _str(nullptr) {}
85 kt_arg(uint64_t value) : _value(value), _str(nullptr) {}
86 kt_arg(DyldTimingBuildClosure value) : _value((uint64_t)value), _str(nullptr) {}
87 kt_arg(const char *value) : _value(0), _str(value) {}
88 kt_arg(void *value) : _value((uint64_t)value), _str(nullptr) {}
89 uint64_t value() const { return _value; }
90 private:
91 void prepare(uint32_t code) {
92 if (_str) {
93 _value = kdebug_trace_string(code, 0, _str);
94 if (_value == (uint64_t)-1) _value = 0;
95 }
96 }
97 void destroy(uint32_t code) {
98 if (_str && _value) {
99 kdebug_trace_string(code, _value, nullptr);
100 }
101 }
102 friend class ScopedTimer;
103 friend uint64_t kdebug_trace_dyld_duration_start(uint32_t code, kt_arg data1, kt_arg data2, kt_arg data3);
104 friend void kdebug_trace_dyld_duration_end(uint64_t pair_id, uint32_t code, kt_arg data4, kt_arg data5, kt_arg data6);
105 friend void kdebug_trace_dyld_marker(uint32_t code, kt_arg data1, kt_arg data2, kt_arg data3, kt_arg data4);
106 uint64_t _value;
107 const char* _str;
108 };
109
110 class VIS_HIDDEN ScopedTimer {
111 public:
112 ScopedTimer(uint32_t code, kt_arg data1, kt_arg data2, kt_arg data3)
113 : code(code), data1(data1), data2(data2), data3(data3), data4(0), data5(0), data6(0) {
114 #if BUILDING_LIBDYLD || BUILDING_DYLD
115 startTimer();
116 #endif
117 }
118
119 ~ScopedTimer() {
120 #if BUILDING_LIBDYLD || BUILDING_DYLD
121 endTimer();
122 #endif
123 }
124
125 void setData4(kt_arg data) { data4 = data; }
126 void setData5(kt_arg data) { data5 = data; }
127 void setData6(kt_arg data) { data6 = data; }
128 private:
129 #if BUILDING_LIBDYLD || BUILDING_DYLD
130 void startTimer();
131 void endTimer();
132 #endif
133
134 uint32_t code;
135 kt_arg data1;
136 kt_arg data2;
137 kt_arg data3;
138 kt_arg data4;
139 kt_arg data5;
140 kt_arg data6;
141 uint64_t current_trace_id = 0;
142 };
143
144 VIS_HIDDEN
145 void kdebug_trace_dyld_image(const uint32_t code,
146 const char* path,
147 const uuid_t* uuid_bytes,
148 const fsobj_id_t fsobjid,
149 const fsid_t fsid,
150 const mach_header* load_addr);
151
152 VIS_HIDDEN
153 bool kdebug_trace_dyld_enabled(uint32_t code);
154
155 VIS_HIDDEN
156 void kdebug_trace_dyld_marker(uint32_t code, kt_arg data1, kt_arg data2, kt_arg data3, kt_arg data4);
157
158 VIS_HIDDEN
159 uint64_t kdebug_trace_dyld_duration_start(uint32_t code, kt_arg data1, kt_arg data2, kt_arg data3);
160
161 VIS_HIDDEN
162 void kdebug_trace_dyld_duration_end(uint64_t trace_id, uint32_t code, kt_arg data4, kt_arg data5, kt_arg data6);
163
164 VIS_HIDDEN
165 void syntheticBacktrace(const char *reason, bool enableExternally=false);
166
167 };
168 #endif /* Tracing_h */