2022-09-06 19:14:07 -04:00
|
|
|
// Copyright 2014 The Crashpad Authors
|
2014-10-09 15:13:13 -04:00
|
|
|
//
|
|
|
|
// 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 "minidump/minidump_exception_writer.h"
|
|
|
|
|
2015-12-09 17:36:32 -05:00
|
|
|
#include <utility>
|
|
|
|
|
2020-06-22 11:14:31 +02:00
|
|
|
#include "base/check_op.h"
|
2015-03-06 16:05:34 -08:00
|
|
|
#include "base/numerics/safe_conversions.h"
|
2014-10-23 18:47:27 -04:00
|
|
|
#include "minidump/minidump_context_writer.h"
|
2014-11-04 12:41:01 -05:00
|
|
|
#include "snapshot/exception_snapshot.h"
|
2014-10-23 18:47:27 -04:00
|
|
|
#include "util/file/file_writer.h"
|
2019-01-03 13:36:02 -05:00
|
|
|
#include "util/misc/arraysize.h"
|
2014-10-09 15:13:13 -04:00
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
|
|
|
|
MinidumpExceptionWriter::MinidumpExceptionWriter()
|
2014-10-14 11:10:45 -04:00
|
|
|
: MinidumpStreamWriter(), exception_(), context_(nullptr) {
|
2014-10-09 15:13:13 -04:00
|
|
|
}
|
|
|
|
|
2014-10-27 15:01:39 -04:00
|
|
|
MinidumpExceptionWriter::~MinidumpExceptionWriter() {
|
|
|
|
}
|
|
|
|
|
2014-11-04 12:41:01 -05:00
|
|
|
void MinidumpExceptionWriter::InitializeFromSnapshot(
|
|
|
|
const ExceptionSnapshot* exception_snapshot,
|
2024-01-19 13:41:26 -05:00
|
|
|
const MinidumpThreadIDMap& thread_id_map,
|
|
|
|
bool allow_missing_thread_id_from_map) {
|
2014-11-04 12:41:01 -05:00
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
DCHECK(!context_);
|
|
|
|
|
2014-11-07 14:47:08 -05:00
|
|
|
auto thread_id_it = thread_id_map.find(exception_snapshot->ThreadID());
|
2024-01-19 13:41:26 -05:00
|
|
|
bool thread_id_missing = thread_id_it == thread_id_map.end();
|
|
|
|
if (allow_missing_thread_id_from_map && thread_id_missing) {
|
|
|
|
SetThreadID(static_cast<uint32_t>(exception_snapshot->ThreadID()));
|
|
|
|
} else {
|
|
|
|
DCHECK(!thread_id_missing);
|
|
|
|
SetThreadID(thread_id_it->second);
|
|
|
|
}
|
2014-11-04 12:41:01 -05:00
|
|
|
|
|
|
|
SetExceptionCode(exception_snapshot->Exception());
|
|
|
|
SetExceptionFlags(exception_snapshot->ExceptionInfo());
|
|
|
|
SetExceptionAddress(exception_snapshot->ExceptionAddress());
|
|
|
|
SetExceptionInformation(exception_snapshot->Codes());
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
std::unique_ptr<MinidumpContextWriter> context =
|
2014-11-04 12:41:01 -05:00
|
|
|
MinidumpContextWriter::CreateFromSnapshot(exception_snapshot->Context());
|
2015-12-09 17:36:32 -05:00
|
|
|
SetContext(std::move(context));
|
2014-11-04 12:41:01 -05:00
|
|
|
}
|
|
|
|
|
2014-10-27 15:01:39 -04:00
|
|
|
void MinidumpExceptionWriter::SetContext(
|
2016-04-25 12:13:07 -07:00
|
|
|
std::unique_ptr<MinidumpContextWriter> context) {
|
2014-10-09 15:13:13 -04:00
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
|
2015-12-09 17:36:32 -05:00
|
|
|
context_ = std::move(context);
|
2014-10-09 15:13:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MinidumpExceptionWriter::SetExceptionInformation(
|
|
|
|
const std::vector<uint64_t>& exception_information) {
|
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
|
|
|
|
const size_t parameters = exception_information.size();
|
2017-07-25 19:15:48 -04:00
|
|
|
constexpr size_t kMaxParameters =
|
2019-01-03 13:36:02 -05:00
|
|
|
ArraySize(exception_.ExceptionRecord.ExceptionInformation);
|
2014-10-09 15:13:13 -04:00
|
|
|
CHECK_LE(parameters, kMaxParameters);
|
|
|
|
|
2015-03-06 16:05:34 -08:00
|
|
|
exception_.ExceptionRecord.NumberParameters =
|
|
|
|
base::checked_cast<uint32_t>(parameters);
|
2014-10-09 15:13:13 -04:00
|
|
|
size_t parameter = 0;
|
|
|
|
for (; parameter < parameters; ++parameter) {
|
|
|
|
exception_.ExceptionRecord.ExceptionInformation[parameter] =
|
|
|
|
exception_information[parameter];
|
|
|
|
}
|
|
|
|
for (; parameter < kMaxParameters; ++parameter) {
|
|
|
|
exception_.ExceptionRecord.ExceptionInformation[parameter] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MinidumpExceptionWriter::Freeze() {
|
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
CHECK(context_);
|
|
|
|
|
|
|
|
if (!MinidumpStreamWriter::Freeze()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
context_->RegisterLocationDescriptor(&exception_.ThreadContext);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t MinidumpExceptionWriter::SizeOfObject() {
|
|
|
|
DCHECK_GE(state(), kStateFrozen);
|
|
|
|
|
|
|
|
return sizeof(exception_);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<internal::MinidumpWritable*> MinidumpExceptionWriter::Children() {
|
|
|
|
DCHECK_GE(state(), kStateFrozen);
|
|
|
|
DCHECK(context_);
|
|
|
|
|
|
|
|
std::vector<MinidumpWritable*> children;
|
2014-10-27 15:01:39 -04:00
|
|
|
children.push_back(context_.get());
|
2014-10-09 15:13:13 -04:00
|
|
|
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MinidumpExceptionWriter::WriteObject(FileWriterInterface* file_writer) {
|
|
|
|
DCHECK_EQ(state(), kStateWritable);
|
|
|
|
|
|
|
|
return file_writer->Write(&exception_, sizeof(exception_));
|
|
|
|
}
|
|
|
|
|
|
|
|
MinidumpStreamType MinidumpExceptionWriter::StreamType() const {
|
|
|
|
return kMinidumpStreamTypeException;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace crashpad
|