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
)
66 Diagnostics::Diagnostics(const std::string
& prefix
, bool verbose
)
73 Diagnostics::~Diagnostics()
78 void Diagnostics::error(const char* format
, ...)
80 _buffer
= _simple_salloc();
82 va_start(list
, format
);
83 _simple_vsprintf(_buffer
, format
, list
);
91 va_start(list
, format
);
92 vasprintf(&output_string
, format
, list
);
95 if (_prefix
.empty()) {
96 fprintf(stderr
, "%s", output_string
);
98 fprintf(stderr
, "[%s] %s", _prefix
.c_str(), output_string
);
103 bool Diagnostics::hasError() const
105 return _buffer
!= nullptr;
108 bool Diagnostics::noError() const
110 return _buffer
== nullptr;
113 void Diagnostics::clearError()
116 _simple_sfree(_buffer
);
120 void Diagnostics::assertNoError() const
122 if ( _buffer
!= nullptr )
123 abort_report_np("%s", _simple_string(_buffer
));
127 const char* Diagnostics::errorMessage() const
129 return _simple_string(_buffer
);
133 void Diagnostics::warning(const char* format
, ...)
135 _SIMPLE_STRING tmp
= _simple_salloc();
137 va_start(list
, format
);
138 _simple_vsprintf(tmp
, format
, list
);
140 #if BUILDING_CACHE_BUILDER
141 dispatch_sync(sWarningQueue
, ^{
142 _warnings
.insert(_simple_string(tmp
));
145 _warnings
.insert(_simple_string(tmp
));
150 void Diagnostics::verbose(const char* format
, ...)
157 va_start(list
, format
);
158 vasprintf(&output_string
, format
, list
);
161 if (_prefix
.empty()) {
162 fprintf(stderr
, "%s", output_string
);
164 fprintf(stderr
, "[%s] %s", _prefix
.c_str(), output_string
);
168 const std::string
Diagnostics::prefix() const
173 void Diagnostics::copy(const Diagnostics
& other
)
175 if ( other
.hasError() )
176 error("%s", other
.errorMessage().c_str());
177 for (const std::string
& warn
: other
.warnings())
178 warning("%s", warn
.c_str());
181 std::string
Diagnostics::errorMessage() const
183 if ( _buffer
!= nullptr )
184 return _simple_string(_buffer
);
186 return std::string();
189 const std::set
<std::string
> Diagnostics::warnings() const
191 #if BUILDING_CACHE_BUILDER
192 __block
std::set
<std::string
> retval
;
193 dispatch_sync(sWarningQueue
, ^{
202 void Diagnostics::clearWarnings()
204 #if BUILDING_CACHE_BUILDER
205 dispatch_sync(sWarningQueue
, ^{