1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/apple/mach_logging.h"
6
7 #include <iomanip>
8 #include <string>
9
10 #include "base/immediate_crash.h"
11 #include "base/strings/stringprintf.h"
12 #include "build/build_config.h"
13
14 #if BUILDFLAG(USE_BLINK)
15 #if BUILDFLAG(IS_IOS)
16 #include "base/ios/sim_header_shims.h"
17 #else
18 #include <servers/bootstrap.h>
19 #endif // BUILDFLAG(IS_IOS)
20 #endif // BUILDFLAG(USE_BLINK)
21
22 namespace {
23
FormatMachErrorNumber(mach_error_t mach_err)24 std::string FormatMachErrorNumber(mach_error_t mach_err) {
25 // For the os/kern subsystem, give the error number in decimal as in
26 // <mach/kern_return.h>. Otherwise, give it in hexadecimal to make it easier
27 // to visualize the various bits. See <mach/error.h>.
28 if (mach_err >= 0 && mach_err < KERN_RETURN_MAX) {
29 return base::StringPrintf(" (%d)", mach_err);
30 }
31 return base::StringPrintf(" (0x%08x)", mach_err);
32 }
33
34 } // namespace
35
36 namespace logging {
37
MachLogMessage(const char * file_path,int line,LogSeverity severity,mach_error_t mach_err)38 MachLogMessage::MachLogMessage(const char* file_path,
39 int line,
40 LogSeverity severity,
41 mach_error_t mach_err)
42 : LogMessage(file_path, line, severity), mach_err_(mach_err) {}
43
~MachLogMessage()44 MachLogMessage::~MachLogMessage() {
45 AppendError();
46 }
47
AppendError()48 void MachLogMessage::AppendError() {
49 stream() << ": " << mach_error_string(mach_err_)
50 << FormatMachErrorNumber(mach_err_);
51 }
52
~MachLogMessageFatal()53 MachLogMessageFatal::~MachLogMessageFatal() {
54 AppendError();
55 Flush();
56 base::ImmediateCrash();
57 }
58
59 #if BUILDFLAG(USE_BLINK)
60
BootstrapLogMessage(const char * file_path,int line,LogSeverity severity,kern_return_t bootstrap_err)61 BootstrapLogMessage::BootstrapLogMessage(const char* file_path,
62 int line,
63 LogSeverity severity,
64 kern_return_t bootstrap_err)
65 : LogMessage(file_path, line, severity), bootstrap_err_(bootstrap_err) {}
66
~BootstrapLogMessage()67 BootstrapLogMessage::~BootstrapLogMessage() {
68 AppendError();
69 }
70
AppendError()71 void BootstrapLogMessage::AppendError() {
72 stream() << ": " << bootstrap_strerror(bootstrap_err_);
73
74 switch (bootstrap_err_) {
75 case BOOTSTRAP_SUCCESS:
76 case BOOTSTRAP_NOT_PRIVILEGED:
77 case BOOTSTRAP_NAME_IN_USE:
78 case BOOTSTRAP_UNKNOWN_SERVICE:
79 case BOOTSTRAP_SERVICE_ACTIVE:
80 case BOOTSTRAP_BAD_COUNT:
81 case BOOTSTRAP_NO_MEMORY:
82 case BOOTSTRAP_NO_CHILDREN: {
83 // Show known bootstrap errors in decimal because that's how they're
84 // defined in <servers/bootstrap.h>.
85 stream() << " (" << bootstrap_err_ << ")";
86 break;
87 }
88
89 default: {
90 // bootstrap_strerror passes unknown errors to mach_error_string, so
91 // format them as they would be if they were handled by
92 // MachErrorMessage.
93 stream() << FormatMachErrorNumber(bootstrap_err_);
94 break;
95 }
96 }
97 }
98
~BootstrapLogMessageFatal()99 BootstrapLogMessageFatal::~BootstrapLogMessageFatal() {
100 AppendError();
101 Flush();
102 base::ImmediateCrash();
103 }
104
105 #endif // BUILDFLAG(USE_BLINK)
106
107 } // namespace logging
108