mirror of
https://github.com/chromium/crashpad.git
synced 2025-01-14 09:17:57 +08:00
Add SymbolicConstantsMach and its test.
TEST=util_test SymbolicConstantsMach.* R=rsesek@chromium.org Review URL: https://codereview.chromium.org/563383002
This commit is contained in:
parent
1e7cdb30a0
commit
e49510ab7c
@ -14,8 +14,11 @@
|
||||
|
||||
#include "util/mach/mach_extensions.h"
|
||||
|
||||
#include <AvailabilityMacros.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "util/mac/mac_util.h"
|
||||
|
||||
namespace crashpad {
|
||||
|
||||
mach_port_t MachThreadSelf() {
|
||||
@ -24,4 +27,53 @@ mach_port_t MachThreadSelf() {
|
||||
return pthread_mach_thread_np(pthread_self());
|
||||
}
|
||||
|
||||
exception_mask_t ExcMaskAll() {
|
||||
// This is necessary because of the way that the kernel validates
|
||||
// exception_mask_t arguments to
|
||||
// {host,task,thread}_{get,set,swap}_exception_ports(). It is strict,
|
||||
// rejecting attempts to operate on any bits that it does not recognize. See
|
||||
// 10.9.4 xnu-2422.110.17/osfmk/mach/ipc_host.c and
|
||||
// xnu-2422.110.17/osfmk/mach/ipc_tt.c.
|
||||
|
||||
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_9
|
||||
int mac_os_x_minor_version = MacOSXMinorVersion();
|
||||
#endif
|
||||
|
||||
// See 10.6.8 xnu-1504.15.3/osfmk/mach/exception_types.h. 10.7 uses the same
|
||||
// definition as 10.6. See 10.7.5 xnu-1699.32.7/osfmk/mach/exception_types.h
|
||||
//
|
||||
// The 10.5 SDK actually defined EXC_MASK_ALL as including EXC_MASK_CRASH.
|
||||
// Later SDKs removed EXC_MASK_CRASH from EXC_MASK_ALL, but placed it into a
|
||||
// new constant, EXC_MASK_VALID. For consistent behavior, don’t include
|
||||
// EXC_MASK_CRASH in the 10.5 EXC_MASK_ALL. Consumers that want EXC_MASK_ALL
|
||||
// along with EXC_MASK_CRASH must use ExcMaskAll() | EXC_MASK_CRASH
|
||||
// explicitly. 10.5 otherwise behaves identically to 10.6. See 10.5.8
|
||||
// xnu-1228.15.4/osfmk/mach/exception_types.h.
|
||||
const exception_mask_t kExcMaskAll_10_6 =
|
||||
EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION | EXC_MASK_ARITHMETIC |
|
||||
EXC_MASK_EMULATION | EXC_MASK_SOFTWARE | EXC_MASK_BREAKPOINT |
|
||||
EXC_MASK_SYSCALL | EXC_MASK_MACH_SYSCALL | EXC_MASK_RPC_ALERT |
|
||||
EXC_MASK_MACHINE;
|
||||
#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_7
|
||||
if (mac_os_x_minor_version <= 7) {
|
||||
return kExcMaskAll_10_6;
|
||||
}
|
||||
#endif
|
||||
|
||||
// 10.8 added EXC_MASK_RESOURCE. See 10.8.5
|
||||
// xnu-2050.48.11/osfmk/mach/exception_types.h.
|
||||
const exception_mask_t kExcMaskAll_10_8 =
|
||||
kExcMaskAll_10_6 | EXC_MASK_RESOURCE;
|
||||
#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_8
|
||||
if (mac_os_x_minor_version <= 8) {
|
||||
return kExcMaskAll_10_8;
|
||||
}
|
||||
#endif
|
||||
|
||||
// 10.9 added EXC_MASK_GUARD. See 10.9.4
|
||||
// xnu-2422.110.17/osfmk/mach/exception_types.h.
|
||||
const exception_mask_t kExcMaskAll_10_9 = kExcMaskAll_10_8 | EXC_MASK_GUARD;
|
||||
return kExcMaskAll_10_9;
|
||||
}
|
||||
|
||||
} // namespace crashpad
|
||||
|
@ -67,6 +67,18 @@ const exception_type_t kMachExceptionSimulated = 'CPsx';
|
||||
//! thread continues to exist as a `pthread_t`.
|
||||
mach_port_t MachThreadSelf();
|
||||
|
||||
//! \brief The value for `EXC_MASK_ALL` appropriate for the operating system at
|
||||
//! run time.
|
||||
//!
|
||||
//! The SDK’s definition of `EXC_MASK_ALL` has changed over time, with later
|
||||
//! versions containing more bits set than earlier versions. However, older
|
||||
//! kernels will reject exception masks that contain bits set that they don’t
|
||||
//! recognize. Calling this function will return a value for `EXC_MASK_ALL`
|
||||
//! appropriate for the system at run time.
|
||||
//!
|
||||
//! \note `EXC_MASK_ALL` does not include the value of `EXC_MASK_CRASH`.
|
||||
exception_mask_t ExcMaskAll();
|
||||
|
||||
} // namespace crashpad
|
||||
|
||||
#endif // CRASHPAD_UTIL_MACH_MACH_EXTENSIONS_H_
|
||||
|
545
util/mach/symbolic_constants_mach.cc
Normal file
545
util/mach/symbolic_constants_mach.cc
Normal file
@ -0,0 +1,545 @@
|
||||
// Copyright 2014 The Crashpad Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "util/mach/symbolic_constants_mach.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "base/strings/stringprintf.h"
|
||||
#include "util/mach/mach_extensions.h"
|
||||
#include "util/stdlib/string_number_conversion.h"
|
||||
|
||||
namespace {
|
||||
|
||||
const char* kExceptionNames[] = {
|
||||
NULL,
|
||||
|
||||
// sed -Ene 's/^#define[[:space:]]EXC_([[:graph:]]+)[[:space:]]+[[:digit:]]{1,2}([[:space:]]|$).*/ "\1",/p'
|
||||
// /usr/include/mach/exception_types.h
|
||||
"BAD_ACCESS",
|
||||
"BAD_INSTRUCTION",
|
||||
"ARITHMETIC",
|
||||
"EMULATION",
|
||||
"SOFTWARE",
|
||||
"BREAKPOINT",
|
||||
"SYSCALL",
|
||||
"MACH_SYSCALL",
|
||||
"RPC_ALERT",
|
||||
"CRASH",
|
||||
"RESOURCE",
|
||||
"GUARD",
|
||||
};
|
||||
COMPILE_ASSERT(arraysize(kExceptionNames) == EXC_TYPES_COUNT,
|
||||
kExceptionNames_length);
|
||||
|
||||
const char kExcPrefix[] = "EXC_";
|
||||
const char kExcMaskPrefix[] = "EXC_MASK_";
|
||||
|
||||
const char* kBehaviorNames[] = {
|
||||
NULL,
|
||||
|
||||
// sed -Ene 's/^# define[[:space:]]EXCEPTION_([[:graph:]]+)[[:space:]]+[[:digit:]]{1,2}([[:space:]]|$).*/ "\1",/p'
|
||||
// /usr/include/mach/exception_types.h
|
||||
"DEFAULT",
|
||||
"STATE",
|
||||
"STATE_IDENTITY",
|
||||
};
|
||||
|
||||
const char kBehaviorPrefix[] = "EXCEPTION_";
|
||||
const char kMachExceptionCodesFull[] = "MACH_EXCEPTION_CODES";
|
||||
const char kMachExceptionCodesShort[] = "MACH";
|
||||
|
||||
const char* kFlavorNames[] = {
|
||||
"THREAD_STATE_FLAVOR_LIST",
|
||||
|
||||
#if defined(__i386__) || defined(__x86_64__)
|
||||
// sed -Ene 's/^#define ((x86|THREAD)_[[:graph:]]+)[[:space:]]+[[:digit:]]{1,2}.*$/ "\1",/p'
|
||||
// /usr/include/mach/i386/thread_status.h
|
||||
// and then fix up by adding x86_SAVED_STATE32 and x86_SAVED_STATE64.
|
||||
"x86_THREAD_STATE32",
|
||||
"x86_FLOAT_STATE32",
|
||||
"x86_EXCEPTION_STATE32",
|
||||
"x86_THREAD_STATE64",
|
||||
"x86_FLOAT_STATE64",
|
||||
"x86_EXCEPTION_STATE64",
|
||||
"x86_THREAD_STATE",
|
||||
"x86_FLOAT_STATE",
|
||||
"x86_EXCEPTION_STATE",
|
||||
"x86_DEBUG_STATE32",
|
||||
"x86_DEBUG_STATE64",
|
||||
"x86_DEBUG_STATE",
|
||||
"THREAD_STATE_NONE",
|
||||
"x86_SAVED_STATE32",
|
||||
"x86_SAVED_STATE64",
|
||||
"x86_AVX_STATE32",
|
||||
"x86_AVX_STATE64",
|
||||
"x86_AVX_STATE",
|
||||
#elif defined(__ppc__) || defined(__ppc64__)
|
||||
// sed -Ene 's/^#define ((PPC|THREAD)_[[:graph:]]+)[[:space:]]+[[:digit:]]{1,2}.*$/ "\1",/p'
|
||||
// usr/include/mach/ppc/thread_status.h
|
||||
// (Mac OS X 10.6 SDK)
|
||||
"PPC_THREAD_STATE",
|
||||
"PPC_FLOAT_STATE",
|
||||
"PPC_EXCEPTION_STATE",
|
||||
"PPC_VECTOR_STATE",
|
||||
"PPC_THREAD_STATE64",
|
||||
"PPC_EXCEPTION_STATE64",
|
||||
"THREAD_STATE_NONE",
|
||||
#elif defined(__arm__) || defined(__arm64__)
|
||||
// sed -Ene 's/^#define ((ARM|THREAD)_[[:graph:]]+)[[:space:]]+[[:digit:]]{1,2}.*$/ "\1",/p'
|
||||
// usr/include/mach/arm/thread_status.h
|
||||
// (iOS 7 SDK)
|
||||
// and then fix up by making the list sparse as appropriate.
|
||||
"ARM_THREAD_STATE",
|
||||
"ARM_VFP_STATE",
|
||||
"ARM_EXCEPTION_STATE",
|
||||
"ARM_DEBUG_STATE",
|
||||
"THREAD_STATE_NONE",
|
||||
"ARM_THREAD_STATE64",
|
||||
"ARM_EXCEPTION_STATE64",
|
||||
NULL,
|
||||
"ARM_THREAD_STATE32",
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
"ARM_DEBUG_STATE32",
|
||||
"ARM_DEBUG_STATE64",
|
||||
"ARM_NEON_STATE",
|
||||
"ARM_NEON_STATE64",
|
||||
#endif
|
||||
};
|
||||
|
||||
// Certain generic flavors have high constants not contiguous with the flavors
|
||||
// above. List them separately alongside their constants.
|
||||
const struct {
|
||||
thread_state_flavor_t flavor;
|
||||
const char* name;
|
||||
} kGenericFlavorNames[] = {
|
||||
{THREAD_STATE_FLAVOR_LIST_NEW, "THREAD_STATE_FLAVOR_LIST_NEW"},
|
||||
{THREAD_STATE_FLAVOR_LIST_10_9, "THREAD_STATE_FLAVOR_LIST_10_9"},
|
||||
};
|
||||
|
||||
// Returns the short name for a flavor name, given its full flavor name.
|
||||
std::string ThreadStateFlavorFullToShort(const base::StringPiece& flavor) {
|
||||
// For generic flavors like THREAD_STATE_NONE and THREAD_STATE_FLAVOR_LIST_*.
|
||||
const char kThreadState[] = "THREAD_STATE_";
|
||||
size_t prefix_len = strlen(kThreadState);
|
||||
const char* flavor_data = flavor.data();
|
||||
size_t flavor_len = flavor.size();
|
||||
if (flavor_len >= prefix_len &&
|
||||
strncmp(flavor_data, kThreadState, prefix_len) == 0) {
|
||||
return std::string(flavor_data + prefix_len, flavor_len - prefix_len);
|
||||
}
|
||||
|
||||
// For architecture-specific flavors.
|
||||
#if defined(__i386__) || defined(__x86_64__)
|
||||
const char kArchPrefix[] = "x86_";
|
||||
#elif defined(__ppc__) || defined(__ppc64__)
|
||||
const char kArchPrefix[] = "PPC_";
|
||||
#elif defined(__arm__) || defined(__arm64__)
|
||||
const char kArchPrefix[] = "ARM_"
|
||||
#endif
|
||||
prefix_len = strlen(kArchPrefix);
|
||||
if (flavor_len >= prefix_len &&
|
||||
strncmp(flavor_data, kArchPrefix, prefix_len) == 0) {
|
||||
// Shorten the suffix by removing _STATE. If the suffix contains a
|
||||
// significant designation like 32 or 64, keep it, so that a full name like
|
||||
// x86_THREAD_STATE64 becomes a short name like THREAD64.
|
||||
const struct {
|
||||
const char* orig;
|
||||
const char* repl;
|
||||
} kStateSuffixes[] = {
|
||||
{"_STATE", ""},
|
||||
{"_STATE32", "32"},
|
||||
{"_STATE64", "64"},
|
||||
};
|
||||
for (size_t suffix_index = 0;
|
||||
suffix_index < arraysize(kStateSuffixes);
|
||||
++suffix_index) {
|
||||
const char* suffix = kStateSuffixes[suffix_index].orig;
|
||||
size_t suffix_len = strlen(suffix);
|
||||
if (flavor_len >= suffix_len &&
|
||||
strncmp(flavor_data + flavor_len - suffix_len, suffix, suffix_len) ==
|
||||
0) {
|
||||
std::string s(flavor_data + prefix_len,
|
||||
flavor_len - prefix_len - suffix_len);
|
||||
return s.append(kStateSuffixes[suffix_index].repl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return std::string(flavor_data, flavor_len);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace crashpad {
|
||||
|
||||
std::string ExceptionToString(exception_type_t exception,
|
||||
SymbolicConstantToStringOptions options) {
|
||||
const char* exception_name =
|
||||
static_cast<size_t>(exception) < arraysize(kExceptionNames)
|
||||
? kExceptionNames[exception]
|
||||
: NULL;
|
||||
if (!exception_name) {
|
||||
if (options & kUnknownIsNumeric) {
|
||||
return base::StringPrintf("%d", exception);
|
||||
}
|
||||
return std::string();
|
||||
}
|
||||
|
||||
if (options & kUseShortName) {
|
||||
return std::string(exception_name);
|
||||
}
|
||||
return base::StringPrintf("%s%s", kExcPrefix, exception_name);
|
||||
}
|
||||
|
||||
bool StringToException(const base::StringPiece& string,
|
||||
StringToSymbolicConstantOptions options,
|
||||
exception_type_t* exception) {
|
||||
if ((options & kAllowFullName) || (options & kAllowShortName)) {
|
||||
bool can_match_full =
|
||||
(options & kAllowFullName) &&
|
||||
string.substr(0, strlen(kExcPrefix)).compare(kExcPrefix) == 0;
|
||||
base::StringPiece short_string =
|
||||
can_match_full ? string.substr(strlen(kExcPrefix)) : string;
|
||||
for (exception_type_t index = 0;
|
||||
index < static_cast<exception_type_t>(arraysize(kExceptionNames));
|
||||
++index) {
|
||||
const char* exception_name = kExceptionNames[index];
|
||||
if (!exception_name) {
|
||||
continue;
|
||||
}
|
||||
if (can_match_full && short_string.compare(exception_name) == 0) {
|
||||
*exception = index;
|
||||
return true;
|
||||
}
|
||||
if ((options & kAllowShortName) && string.compare(exception_name) == 0) {
|
||||
*exception = index;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (options & kAllowNumber) {
|
||||
return StringToNumber(string, reinterpret_cast<unsigned int*>(exception));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string ExceptionMaskToString(exception_mask_t exception_mask,
|
||||
SymbolicConstantToStringOptions options) {
|
||||
exception_mask_t local_exception_mask = exception_mask;
|
||||
std::string mask_string;
|
||||
bool has_forbidden_or = false;
|
||||
for (size_t exception = 0;
|
||||
exception < arraysize(kExceptionNames);
|
||||
++exception) {
|
||||
const char* exception_name = kExceptionNames[exception];
|
||||
exception_mask_t exception_mask_value = 1 << exception;
|
||||
if (exception_name && (local_exception_mask & exception_mask_value)) {
|
||||
if (!mask_string.empty()) {
|
||||
if (!(options & kUseOr)) {
|
||||
has_forbidden_or = true;
|
||||
break;
|
||||
}
|
||||
mask_string.append("|");
|
||||
}
|
||||
if (!(options & kUseShortName)) {
|
||||
mask_string.append(kExcMaskPrefix);
|
||||
}
|
||||
mask_string.append(exception_name);
|
||||
local_exception_mask &= ~exception_mask_value;
|
||||
}
|
||||
}
|
||||
|
||||
if (has_forbidden_or) {
|
||||
local_exception_mask = exception_mask;
|
||||
mask_string.clear();
|
||||
}
|
||||
|
||||
// Deal with any remainder.
|
||||
if (local_exception_mask) {
|
||||
if (!(options & kUnknownIsNumeric)) {
|
||||
return std::string();
|
||||
}
|
||||
if (!mask_string.empty()) {
|
||||
mask_string.append("|");
|
||||
}
|
||||
mask_string.append(base::StringPrintf("%#x", local_exception_mask));
|
||||
}
|
||||
|
||||
return mask_string;
|
||||
}
|
||||
|
||||
bool StringToExceptionMask(const base::StringPiece& string,
|
||||
StringToSymbolicConstantOptions options,
|
||||
exception_mask_t* exception_mask) {
|
||||
if (options & kAllowOr) {
|
||||
options &= ~kAllowOr;
|
||||
exception_mask_t build_mask = 0;
|
||||
size_t pos = -1;
|
||||
do {
|
||||
++pos;
|
||||
const char* substring_begin = string.begin() + pos;
|
||||
pos = string.find('|', pos);
|
||||
const char* substring_end = (pos == base::StringPiece::npos)
|
||||
? string.end()
|
||||
: (string.begin() + pos);
|
||||
base::StringPiece substring = string.substr(
|
||||
substring_begin - string.begin(), substring_end - substring_begin);
|
||||
|
||||
exception_mask_t temp_mask;
|
||||
if (!StringToExceptionMask(substring, options, &temp_mask)) {
|
||||
return false;
|
||||
}
|
||||
build_mask |= temp_mask;
|
||||
} while (pos != base::StringPiece::npos);
|
||||
|
||||
*exception_mask = build_mask;
|
||||
return true;
|
||||
}
|
||||
|
||||
if ((options & kAllowFullName) || (options & kAllowShortName)) {
|
||||
bool can_match_full =
|
||||
(options & kAllowFullName) &&
|
||||
string.substr(0, strlen(kExcMaskPrefix)).compare(kExcMaskPrefix) == 0;
|
||||
base::StringPiece short_string =
|
||||
can_match_full ? string.substr(strlen(kExcMaskPrefix)) : string;
|
||||
for (exception_type_t index = 0;
|
||||
index < static_cast<exception_type_t>(arraysize(kExceptionNames));
|
||||
++index) {
|
||||
const char* exception_name = kExceptionNames[index];
|
||||
if (!exception_name) {
|
||||
continue;
|
||||
}
|
||||
if (can_match_full && short_string.compare(exception_name) == 0) {
|
||||
*exception_mask = 1 << index;
|
||||
return true;
|
||||
}
|
||||
if ((options & kAllowShortName) && string.compare(exception_name) == 0) {
|
||||
*exception_mask = 1 << index;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// EXC_MASK_ALL is a special case: it is not in kExceptionNames as it exists
|
||||
// only as a mask value.
|
||||
const char kExcMaskAll[] = "ALL";
|
||||
if ((can_match_full && short_string.compare(kExcMaskAll) == 0) ||
|
||||
((options & kAllowShortName) && string.compare(kExcMaskAll) == 0)) {
|
||||
*exception_mask = ExcMaskAll();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (options & kAllowNumber) {
|
||||
return StringToNumber(string,
|
||||
reinterpret_cast<unsigned int*>(exception_mask));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string ExceptionBehaviorToString(exception_behavior_t behavior,
|
||||
SymbolicConstantToStringOptions options) {
|
||||
bool mach_exception_codes = behavior & MACH_EXCEPTION_CODES;
|
||||
exception_behavior_t basic_behavior = behavior & ~MACH_EXCEPTION_CODES;
|
||||
|
||||
const char* behavior_name =
|
||||
static_cast<size_t>(basic_behavior) < arraysize(kBehaviorNames)
|
||||
? kBehaviorNames[basic_behavior]
|
||||
: NULL;
|
||||
if (!behavior_name) {
|
||||
if (options & kUnknownIsNumeric) {
|
||||
return base::StringPrintf("%#x", behavior);
|
||||
}
|
||||
return std::string();
|
||||
}
|
||||
|
||||
std::string behavior_string;
|
||||
if (options & kUseShortName) {
|
||||
behavior_string.assign(behavior_name);
|
||||
} else {
|
||||
behavior_string.assign(base::StringPrintf(
|
||||
"%s%s", kBehaviorPrefix, behavior_name));
|
||||
}
|
||||
|
||||
if (mach_exception_codes) {
|
||||
behavior_string.append("|");
|
||||
behavior_string.append((options & kUseShortName) ? kMachExceptionCodesShort
|
||||
: kMachExceptionCodesFull);
|
||||
}
|
||||
|
||||
return behavior_string;
|
||||
}
|
||||
|
||||
bool StringToExceptionBehavior(const base::StringPiece& string,
|
||||
StringToSymbolicConstantOptions options,
|
||||
exception_behavior_t* behavior) {
|
||||
base::StringPiece sp = string;
|
||||
exception_behavior_t build_behavior = 0;
|
||||
size_t pos = sp.find('|', 0);
|
||||
if (pos != base::StringPiece::npos) {
|
||||
base::StringPiece left = sp.substr(0, pos);
|
||||
base::StringPiece right = sp.substr(pos + 1, sp.length() - pos - 1);
|
||||
if (options & kAllowFullName) {
|
||||
if (left.compare(kMachExceptionCodesFull) == 0) {
|
||||
build_behavior |= MACH_EXCEPTION_CODES;
|
||||
sp = right;
|
||||
} else if (right.compare(kMachExceptionCodesFull) == 0) {
|
||||
build_behavior |= MACH_EXCEPTION_CODES;
|
||||
sp = left;
|
||||
}
|
||||
}
|
||||
if (!(build_behavior & MACH_EXCEPTION_CODES) &&
|
||||
(options & kAllowShortName)) {
|
||||
if (left.compare(kMachExceptionCodesShort) == 0) {
|
||||
build_behavior |= MACH_EXCEPTION_CODES;
|
||||
sp = right;
|
||||
} else if (right.compare(kMachExceptionCodesShort) == 0) {
|
||||
build_behavior |= MACH_EXCEPTION_CODES;
|
||||
sp = left;
|
||||
}
|
||||
}
|
||||
if (!(build_behavior & MACH_EXCEPTION_CODES)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if ((options & kAllowFullName) || (options & kAllowShortName)) {
|
||||
bool can_match_full =
|
||||
(options & kAllowFullName) &&
|
||||
sp.substr(0, strlen(kBehaviorPrefix)).compare(kBehaviorPrefix) == 0;
|
||||
base::StringPiece short_string =
|
||||
can_match_full ? sp.substr(strlen(kBehaviorPrefix)) : sp;
|
||||
for (exception_behavior_t index = 0;
|
||||
index < static_cast<exception_behavior_t>(arraysize(kBehaviorNames));
|
||||
++index) {
|
||||
const char* behavior_name = kBehaviorNames[index];
|
||||
if (!behavior_name) {
|
||||
continue;
|
||||
}
|
||||
if (can_match_full && short_string.compare(behavior_name) == 0) {
|
||||
build_behavior |= index;
|
||||
*behavior = build_behavior;
|
||||
return true;
|
||||
}
|
||||
if ((options & kAllowShortName) && sp.compare(behavior_name) == 0) {
|
||||
build_behavior |= index;
|
||||
*behavior = build_behavior;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (options & kAllowNumber) {
|
||||
exception_behavior_t temp_behavior;
|
||||
if (!StringToNumber(sp, reinterpret_cast<unsigned int*>(&temp_behavior))) {
|
||||
return false;
|
||||
}
|
||||
build_behavior |= temp_behavior;
|
||||
*behavior = build_behavior;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string ThreadStateFlavorToString(thread_state_flavor_t flavor,
|
||||
SymbolicConstantToStringOptions options) {
|
||||
const char* flavor_name =
|
||||
static_cast<size_t>(flavor) < arraysize(kFlavorNames)
|
||||
? kFlavorNames[flavor]
|
||||
: NULL;
|
||||
|
||||
if (!flavor_name) {
|
||||
for (size_t generic_flavor_index = 0;
|
||||
generic_flavor_index < arraysize(kGenericFlavorNames);
|
||||
++generic_flavor_index) {
|
||||
if (flavor == kGenericFlavorNames[generic_flavor_index].flavor) {
|
||||
flavor_name = kGenericFlavorNames[generic_flavor_index].name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!flavor_name) {
|
||||
if (options & kUnknownIsNumeric) {
|
||||
return base::StringPrintf("%d", flavor);
|
||||
}
|
||||
return std::string();
|
||||
}
|
||||
|
||||
if (options & kUseShortName) {
|
||||
return ThreadStateFlavorFullToShort(flavor_name);
|
||||
}
|
||||
return std::string(flavor_name);
|
||||
}
|
||||
|
||||
bool StringToThreadStateFlavor(const base::StringPiece& string,
|
||||
StringToSymbolicConstantOptions options,
|
||||
thread_state_flavor_t* flavor) {
|
||||
if ((options & kAllowFullName) || (options & kAllowShortName)) {
|
||||
for (thread_state_flavor_t index = 0;
|
||||
index < static_cast<thread_state_flavor_t>(arraysize(kFlavorNames));
|
||||
++index) {
|
||||
const char* flavor_name = kFlavorNames[index];
|
||||
if (!flavor_name) {
|
||||
continue;
|
||||
}
|
||||
if ((options & kAllowFullName) && string.compare(flavor_name) == 0) {
|
||||
*flavor = index;
|
||||
return true;
|
||||
}
|
||||
if (options & kAllowShortName) {
|
||||
std::string short_name = ThreadStateFlavorFullToShort(flavor_name);
|
||||
if (string.compare(short_name) == 0) {
|
||||
*flavor = index;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t generic_flavor_index = 0;
|
||||
generic_flavor_index < arraysize(kGenericFlavorNames);
|
||||
++generic_flavor_index) {
|
||||
const char* flavor_name = kGenericFlavorNames[generic_flavor_index].name;
|
||||
thread_state_flavor_t flavor_number =
|
||||
kGenericFlavorNames[generic_flavor_index].flavor;
|
||||
if ((options & kAllowFullName) && string.compare(flavor_name) == 0) {
|
||||
*flavor = flavor_number;
|
||||
return true;
|
||||
}
|
||||
if (options & kAllowShortName) {
|
||||
std::string short_name = ThreadStateFlavorFullToShort(flavor_name);
|
||||
if (string.compare(short_name) == 0) {
|
||||
*flavor = flavor_number;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (options & kAllowNumber) {
|
||||
return StringToNumber(string, reinterpret_cast<unsigned int*>(flavor));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace crashpad
|
120
util/mach/symbolic_constants_mach.h
Normal file
120
util/mach/symbolic_constants_mach.h
Normal file
@ -0,0 +1,120 @@
|
||||
// Copyright 2014 The Crashpad Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef CRASHPAD_UTIL_MACH_SYMBOLIC_CONSTANTS_MACH_H_
|
||||
#define CRASHPAD_UTIL_MACH_SYMBOLIC_CONSTANTS_MACH_H_
|
||||
|
||||
#include <mach/mach.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "base/strings/string_piece.h"
|
||||
#include "util/misc/symbolic_constants_common.h"
|
||||
|
||||
namespace crashpad {
|
||||
|
||||
//! \brief Converts a Mach exception value to a textual representation.
|
||||
//!
|
||||
//! \param[in] exception The Mach exception value to convert.
|
||||
//! \param[in] options Options affecting the conversion. ::kUseOr is ignored.
|
||||
//! For ::kUnknownIsNumeric, the format is `"%d"`.
|
||||
//!
|
||||
//! \return The converted string.
|
||||
std::string ExceptionToString(exception_type_t exception,
|
||||
SymbolicConstantToStringOptions options);
|
||||
|
||||
//! \brief Converts a string to its corresponding Mach exception value.
|
||||
//!
|
||||
//! \param[in] string The string to convert.
|
||||
//! \param[in] options Options affecting the conversion. ::kAllowOr is ignored.
|
||||
//! \param[out] exception The converted Mach exception value.
|
||||
//!
|
||||
//! \return `true` on success, `false` if \a string could not be converted as
|
||||
//! requested.
|
||||
bool StringToException(const base::StringPiece& string,
|
||||
StringToSymbolicConstantOptions options,
|
||||
exception_type_t* exception);
|
||||
|
||||
//! \brief Converts a Mach exception mask value to a textual representation.
|
||||
//!
|
||||
//! \param[in] exception_mask The Mach exception mask value to convert.
|
||||
//! \param[in] options Options affecting the conversion. ::kUseOr is honored.
|
||||
//! For ::kUnknownIsNumeric, the format is `"%#x"`.
|
||||
//!
|
||||
//! \return The converted string.
|
||||
std::string ExceptionMaskToString(exception_mask_t exception_mask,
|
||||
SymbolicConstantToStringOptions options);
|
||||
|
||||
//! \brief Converts a string to its corresponding Mach exception mask value.
|
||||
//!
|
||||
//! \param[in] string The string to convert.
|
||||
//! \param[in] options Options affecting the conversion. ::kAllowOr is honored.
|
||||
//! \param[out] exception_mask The converted Mach exception mask value.
|
||||
//!
|
||||
//! \return `true` on success, `false` if \a string could not be converted as
|
||||
//! requested.
|
||||
bool StringToExceptionMask(const base::StringPiece& string,
|
||||
StringToSymbolicConstantOptions options,
|
||||
exception_mask_t* exception_mask);
|
||||
|
||||
//! \brief Converts a Mach exception behavior value to a textual representation.
|
||||
//!
|
||||
//! \param[in] behavior The Mach exception behavior value to convert.
|
||||
//! \param[in] options Options affecting the conversion. ::kUseOr is ignored.
|
||||
//! `MACH_EXCEPTION_CODES` can always be ORed in, but no other values can be
|
||||
//! ORed with each other. For ::kUnknownIsNumeric, the format is `"%#x"`.
|
||||
//!
|
||||
//! \return The converted string.
|
||||
std::string ExceptionBehaviorToString(exception_behavior_t behavior,
|
||||
SymbolicConstantToStringOptions options);
|
||||
|
||||
//! \brief Converts a string to its corresponding Mach exception behavior value.
|
||||
//!
|
||||
//! \param[in] string The string to convert.
|
||||
//! \param[in] options Options affecting the conversion. ::kAllowOr is ignored.
|
||||
//! `MACH_EXCEPTION_CODES` can always be ORed in, but no other values can be
|
||||
//! ORed with each other.
|
||||
//! \param[out] behavior The converted Mach exception behavior value.
|
||||
//!
|
||||
//! \return `true` on success, `false` if \a string could not be converted as
|
||||
//! requested.
|
||||
bool StringToExceptionBehavior(const base::StringPiece& string,
|
||||
StringToSymbolicConstantOptions options,
|
||||
exception_behavior_t* behavior);
|
||||
|
||||
//! \brief Converts a thread state flavor value to a textual representation.
|
||||
//!
|
||||
//! \param[in] flavor The thread state flavor value to convert.
|
||||
//! \param[in] options Options affecting the conversion. ::kUseOr is ignored.
|
||||
//! For ::kUnknownIsNumeric, the format is `"%d"`.
|
||||
//!
|
||||
//! \return The converted string.
|
||||
std::string ThreadStateFlavorToString(thread_state_flavor_t flavor,
|
||||
SymbolicConstantToStringOptions options);
|
||||
|
||||
//! \brief Converts a string to its corresponding thread state flavor value.
|
||||
//!
|
||||
//! \param[in] string The string to convert.
|
||||
//! \param[in] options Options affecting the conversion. ::kAllowOr is ignored.
|
||||
//! \param[out] flavor The converted thread state flavor value.
|
||||
//!
|
||||
//! \return `true` on success, `false` if \a string could not be converted as
|
||||
//! requested.
|
||||
bool StringToThreadStateFlavor(const base::StringPiece& string,
|
||||
StringToSymbolicConstantOptions options,
|
||||
thread_state_flavor_t* flavor);
|
||||
|
||||
} // namespace crashpad
|
||||
|
||||
#endif // CRASHPAD_UTIL_MACH_SYMBOLIC_CONSTANTS_MACH_H_
|
1064
util/mach/symbolic_constants_mach_test.cc
Normal file
1064
util/mach/symbolic_constants_mach_test.cc
Normal file
File diff suppressed because it is too large
Load Diff
@ -21,6 +21,8 @@
|
||||
#include "base/strings/stringprintf.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#define NUL_TEST_DATA(string) { string, arraysize(string) - 1 }
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace crashpad;
|
||||
@ -67,10 +69,10 @@ const struct {
|
||||
#endif
|
||||
};
|
||||
|
||||
// If expect is NULL, the conversion is expected to fail. If expect is empty,
|
||||
// the conversion is expected to succeed, but the precise returned string value
|
||||
// is unknown. Otherwise, the conversion is expected to succeed, and expect
|
||||
// contains the precise expected string value to be returned.
|
||||
// If |expect| is NULL, the conversion is expected to fail. If |expect| is
|
||||
// empty, the conversion is expected to succeed, but the precise returned string
|
||||
// value is unknown. Otherwise, the conversion is expected to succeed, and
|
||||
// |expect| contains the precise expected string value to be returned.
|
||||
//
|
||||
// Only set kUseFullName or kUseShortName when calling this. Other options are
|
||||
// exercised directly by this function.
|
||||
@ -164,7 +166,8 @@ TEST(SymbolicConstantsPOSIX, StringToSignal) {
|
||||
kAllowFullName | kAllowShortName | kAllowNumber,
|
||||
};
|
||||
|
||||
for (size_t option_index = 0; option_index < arraysize(kOptions);
|
||||
for (size_t option_index = 0;
|
||||
option_index < arraysize(kOptions);
|
||||
++option_index) {
|
||||
SCOPED_TRACE(base::StringPrintf("option_index %zu", option_index));
|
||||
StringToSymbolicConstantOptions options = kOptions[option_index];
|
||||
@ -215,7 +218,6 @@ TEST(SymbolicConstantsPOSIX, StringToSignal) {
|
||||
const char* string;
|
||||
size_t length;
|
||||
} kNULTestData[] = {
|
||||
#define NUL_TEST_DATA(string) { string, arraysize(string) - 1 }
|
||||
NUL_TEST_DATA("\0SIGBUS"),
|
||||
NUL_TEST_DATA("SIG\0BUS"),
|
||||
NUL_TEST_DATA("SIGB\0US"),
|
||||
@ -226,7 +228,6 @@ TEST(SymbolicConstantsPOSIX, StringToSignal) {
|
||||
NUL_TEST_DATA("\0002"),
|
||||
NUL_TEST_DATA("2\0"),
|
||||
NUL_TEST_DATA("1\0002"),
|
||||
#undef NUL_TEST_DATA
|
||||
};
|
||||
|
||||
for (size_t index = 0; index < arraysize(kNULTestData); ++index) {
|
||||
|
@ -69,6 +69,8 @@
|
||||
'mach/mach_extensions.h',
|
||||
'mach/mach_message_server.cc',
|
||||
'mach/mach_message_server.h',
|
||||
'mach/symbolic_constants_mach.cc',
|
||||
'mach/symbolic_constants_mach.h',
|
||||
'mach/task_memory.cc',
|
||||
'mach/task_memory.h',
|
||||
'misc/initialization_state.h',
|
||||
@ -199,6 +201,7 @@
|
||||
'mach/exc_server_variants_test.cc',
|
||||
'mach/mach_extensions_test.cc',
|
||||
'mach/mach_message_server_test.cc',
|
||||
'mach/symbolic_constants_mach_test.cc',
|
||||
'mach/task_memory_test.cc',
|
||||
'misc/initialization_state_dcheck_test.cc',
|
||||
'misc/initialization_state_test.cc',
|
||||
|
Loading…
x
Reference in New Issue
Block a user