]> git.saurik.com Git - apple/dyld.git/blob - dyld3/Logging.cpp
dyld-551.4.tar.gz
[apple/dyld.git] / dyld3 / Logging.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 <string.h>
26 #include <stdint.h>
27 #include <_simple.h>
28
29 #include "Logging.h"
30
31
32 namespace dyld3 {
33
34
35 static bool sVerboseLoading = false;
36 static bool sVerboseInitializers = false;
37 static bool sVerboseSegments = false;
38 static bool sVerboseAPIs = false;
39 static bool sVerboseNotifications = false;
40 static bool sVerboseFixups = false;
41 static bool sVerboseDOFs = false;
42
43 static void vlog_default(const char* format, va_list list)
44 {
45 _simple_vdprintf(2, format, list);
46 }
47
48 static void (*sLogFunction)(const char* format, va_list list) = &vlog_default;
49 static void (*sHaltFunction)(const char* message) __attribute__((noreturn)) = nullptr;
50
51 void halt(const char* message)
52 {
53 (*sHaltFunction)(message);
54 }
55
56 static void vlog(const char* format, va_list list)
57 {
58 (*sLogFunction)(format, list);
59 }
60
61 bool log_loads(const char* format, ...)
62 {
63 if ( !sVerboseLoading )
64 return false;
65 va_list list;
66 va_start(list, format);
67 vlog(format, list);
68 va_end(list);
69 return true;
70 }
71
72 bool log_segments(const char* format, ...)
73 {
74 if ( !sVerboseSegments )
75 return false;
76 va_list list;
77 va_start(list, format);
78 vlog(format, list);
79 va_end(list);
80 return true;
81 }
82
83 bool log_fixups(const char* format, ...)
84 {
85 if ( !sVerboseFixups )
86 return false;
87 va_list list;
88 va_start(list, format);
89 vlog(format, list);
90 va_end(list);
91 return true;
92 }
93
94 bool log_initializers(const char* format, ...)
95 {
96 if ( !sVerboseInitializers )
97 return false;
98 va_list list;
99 va_start(list, format);
100 vlog(format, list);
101 va_end(list);
102 return true;
103 }
104
105 bool log_apis(const char* format, ...)
106 {
107 if ( !sVerboseAPIs )
108 return false;
109 va_list list;
110 va_start(list, format);
111 vlog(format, list);
112 va_end(list);
113 return true;
114 }
115
116 bool log_notifications(const char* format, ...)
117 {
118 if ( !sVerboseNotifications )
119 return false;
120 va_list list;
121 va_start(list, format);
122 vlog(format, list);
123 va_end(list);
124 return true;
125 }
126
127 bool log_dofs(const char* format, ...)
128 {
129 if ( !sVerboseDOFs )
130 return false;
131 va_list list;
132 va_start(list, format);
133 vlog(format, list);
134 va_end(list);
135 return true;
136 }
137
138
139
140 void setLoggingFromEnvs(const char* envp[])
141 {
142 for(const char** p = envp; *p != NULL; p++) {
143 const char* keyEqualsValue = *p;
144 const char* equals = strchr(keyEqualsValue, '=');
145 if ( equals != NULL ) {
146 //const char* value = &equals[1];
147 const size_t keyLen = equals-keyEqualsValue;
148 char key[keyLen+1];
149 strncpy(key, keyEqualsValue, keyLen);
150 key[keyLen] = '\0';
151 if ( strcmp(key, "DYLD_PRINT_LIBRARIES") == 0 ) {
152 sVerboseLoading = true;
153 }
154 else if ( strcmp(key, "DYLD_PRINT_SEGMENTS") == 0 ) {
155 sVerboseSegments = true;
156 }
157 else if ( strcmp(key, "DYLD_PRINT_INITIALIZERS") == 0 ) {
158 sVerboseInitializers = true;
159 }
160 else if ( strcmp(key, "DYLD_PRINT_APIS") == 0 ) {
161 sVerboseAPIs = true;
162 }
163 else if ( strcmp(key, "DYLD_PRINT_NOTIFICATIONS") == 0 ) {
164 sVerboseNotifications = true;
165 }
166 else if ( strcmp(key, "DYLD_PRINT_BINDINGS") == 0 ) {
167 sVerboseFixups = true;
168 }
169 else if ( strcmp(key, "DYLD_PRINT_DOFS") == 0 ) {
170 sVerboseDOFs = true;
171 }
172 }
173 }
174 }
175
176 void setLoggingFunction(void (*func)(const char* format, va_list list))
177 {
178 sLogFunction = func;
179 }
180
181 void setHaltFunction(void (*func)(const char* message) __attribute__((noreturn)))
182 {
183 sHaltFunction = func;
184 }
185
186
187 } // namespace dyld3
188