]> git.saurik.com Git - apple/dyld.git/blob - dyld3/Tracing.cpp
dyld-551.4.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 VIS_HIDDEN
73 void kdebug_trace_dyld_signpost(const uint32_t code, uint64_t data1, uint64_t data2) {
74 if (kdebug_is_enabled(KDBG_CODE(DBG_DYLD, DBG_DYLD_SIGNPOST, code))) {
75 task_thread_times_info info;
76 mach_msg_type_number_t infoSize = sizeof(task_thread_times_info);
77 (void)task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t)&info, &infoSize);
78 uint64_t user_duration = elapsed({0,0}, info.user_time);
79 uint64_t system_duration = elapsed({0,0}, info.system_time);
80 kdebug_trace(KDBG_CODE(DBG_DYLD, DBG_DYLD_SIGNPOST, code), user_duration, system_duration, data1, data2);
81 }
82 }
83
84 static std::atomic<uint64_t> trace_pair_id(0);
85
86 VIS_HIDDEN
87 void kdebug_trace_dyld_duration(const uint32_t code, uint64_t data1, uint64_t data2, void (^block)()) {
88 //FIXME: We should assert here, but it is verified on our current platforms
89 //Re-enabled when we move to C++17 and can use constexpr is_lock_always_free()
90 //assert(std::atomic<uint64_t>{}.is_lock_free());
91 if (kdebug_is_enabled(KDBG_CODE(DBG_DYLD, DBG_DYLD_TIMING, code))) {
92 uint64_t current_trace_id = trace_pair_id++;
93 kdebug_trace(KDBG_CODE(DBG_DYLD, DBG_DYLD_TIMING, code) | DBG_FUNC_START, current_trace_id, 0, data1, data2);
94 block();
95 kdebug_trace(KDBG_CODE(DBG_DYLD, DBG_DYLD_TIMING, code) | DBG_FUNC_END, current_trace_id, 0, data1, data2);
96 } else {
97 block();
98 }
99 }
100
101 void kdebug_trace_print(const uint32_t code, const char *string) {
102 if (kdebug_is_enabled(KDBG_CODE(DBG_DYLD, DBG_DYLD_PRINT, code))) {
103 kdebug_trace_string(KDBG_CODE(DBG_DYLD, DBG_DYLD_PRINT, code), 0, string);
104 }
105 }
106 };