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>
34 static uint64_t elapsed(const time_value_t start
, const time_value_t end
) {
36 duration
= 1000000*(end
.seconds
- start
.seconds
);
37 duration
+= (end
.microseconds
- start
.microseconds
);
45 void kdebug_trace_dyld_image(const uint32_t code
,
46 const uuid_t
* uuid_bytes
,
47 const fsobj_id_t fsobjid
,
49 const mach_header
* load_addr
)
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),
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],
67 kdebug_trace(KDBG_CODE(DBG_DYLD
, DBG_DYLD_UUID
, code
+ 4),
68 fsobjid
.fid_generation
, 0, 0, 0);
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
);
84 static std::atomic
<uint64_t> trace_pair_id(0);
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
);
95 kdebug_trace(KDBG_CODE(DBG_DYLD
, DBG_DYLD_TIMING
, code
) | DBG_FUNC_END
, current_trace_id
, 0, data1
, data2
);
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
);