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@
29 #include <uuid/uuid.h>
35 #include <sys/param.h>
36 #include <sys/sysctl.h>
37 #include <sys/resource.h>
38 #include <sys/types.h>
42 #include <mach/mach.h>
43 #include <mach/machine.h>
44 #include <mach-o/loader.h>
45 #include <mach-o/nlist.h>
46 #include <mach-o/fat.h>
48 #include <libc_private.h>
50 #include "Diagnostics.h"
52 #if BUILDING_CACHE_BUILDER
53 #include <dispatch/dispatch.h>
54 dispatch_queue_t sWarningQueue
= dispatch_queue_create("com.apple.dyld.cache-builder.warnings", NULL
);
57 Diagnostics::Diagnostics(bool verbose
)
58 #if BUILDING_CACHE_BUILDER
65 #if BUILDING_CACHE_BUILDER
66 Diagnostics::Diagnostics(const std::string
& prefix
, bool verbose
)
73 Diagnostics::~Diagnostics()
78 void Diagnostics::error(const char* format
, ...)
81 va_start(list
, format
);
86 void Diagnostics::error(const char* format
, va_list list
)
88 //FIXME: this should be assertNoError(), but we currently overwrite some errors
90 _buffer
= _simple_salloc();
91 _simple_vsprintf(_buffer
, format
, list
);
93 #if BUILDING_CACHE_BUILDER
97 if (_prefix
.empty()) {
98 fprintf(stderr
, "%s", _simple_string(_buffer
));
100 fprintf(stderr
, "[%s] %s", _prefix
.c_str(), _simple_string(_buffer
));
105 bool Diagnostics::hasError() const
107 return _buffer
!= nullptr;
110 bool Diagnostics::noError() const
112 return _buffer
== nullptr;
115 void Diagnostics::clearError()
118 _simple_sfree(_buffer
);
122 void Diagnostics::assertNoError() const
124 if ( _buffer
!= nullptr )
125 abort_report_np("%s", _simple_string(_buffer
));
128 #if !BUILDING_CACHE_BUILDER
129 const char* Diagnostics::errorMessage() const
131 return _simple_string(_buffer
);
135 void Diagnostics::warning(const char* format
, ...)
137 _SIMPLE_STRING tmp
= _simple_salloc();
139 va_start(list
, format
);
140 _simple_vsprintf(tmp
, format
, list
);
142 #if BUILDING_CACHE_BUILDER
143 dispatch_sync(sWarningQueue
, ^{
144 _warnings
.insert(_simple_string(tmp
));
147 _warnings
.insert(_simple_string(tmp
));
152 void Diagnostics::verbose(const char* format
, ...)
159 va_start(list
, format
);
160 vasprintf(&output_string
, format
, list
);
163 if (_prefix
.empty()) {
164 fprintf(stderr
, "%s", output_string
);
166 fprintf(stderr
, "[%s] %s", _prefix
.c_str(), output_string
);
171 const std::string
Diagnostics::prefix() const
176 void Diagnostics::copy(const Diagnostics
& other
)
178 if ( other
.hasError() )
179 error("%s", other
.errorMessage().c_str());
180 for (const std::string
& warn
: other
.warnings())
181 warning("%s", warn
.c_str());
184 std::string
Diagnostics::errorMessage() const
186 if ( _buffer
!= nullptr )
187 return _simple_string(_buffer
);
192 const std::set
<std::string
> Diagnostics::warnings() const
194 #if BUILDING_CACHE_BUILDER
195 __block
std::set
<std::string
> retval
;
196 dispatch_sync(sWarningQueue
, ^{
205 void Diagnostics::clearWarnings()
207 #if BUILDING_CACHE_BUILDER
208 dispatch_sync(sWarningQueue
, ^{