2014-10-09 15:31:29 -04:00
|
|
|
|
// 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 "minidump/minidump_thread_writer.h"
|
|
|
|
|
|
2014-10-23 18:47:27 -04:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
2015-12-09 17:36:32 -05:00
|
|
|
|
#include <utility>
|
|
|
|
|
|
2014-10-09 15:31:29 -04:00
|
|
|
|
#include "base/logging.h"
|
|
|
|
|
#include "minidump/minidump_context_writer.h"
|
|
|
|
|
#include "minidump/minidump_memory_writer.h"
|
2014-11-04 12:36:29 -05:00
|
|
|
|
#include "snapshot/memory_snapshot.h"
|
|
|
|
|
#include "snapshot/thread_snapshot.h"
|
2014-10-23 18:47:27 -04:00
|
|
|
|
#include "util/file/file_writer.h"
|
2014-10-09 15:31:29 -04:00
|
|
|
|
#include "util/numeric/safe_assignment.h"
|
|
|
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
|
|
|
|
|
|
MinidumpThreadWriter::MinidumpThreadWriter()
|
2014-10-14 11:10:45 -04:00
|
|
|
|
: MinidumpWritable(), thread_(), stack_(nullptr), context_(nullptr) {
|
2014-10-09 15:31:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-27 15:01:39 -04:00
|
|
|
|
MinidumpThreadWriter::~MinidumpThreadWriter() {
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-04 12:36:29 -05:00
|
|
|
|
void MinidumpThreadWriter::InitializeFromSnapshot(
|
|
|
|
|
const ThreadSnapshot* thread_snapshot,
|
|
|
|
|
const MinidumpThreadIDMap* thread_id_map) {
|
|
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
|
DCHECK(!stack_);
|
|
|
|
|
DCHECK(!context_);
|
|
|
|
|
|
|
|
|
|
auto thread_id_it = thread_id_map->find(thread_snapshot->ThreadID());
|
|
|
|
|
DCHECK(thread_id_it != thread_id_map->end());
|
|
|
|
|
SetThreadID(thread_id_it->second);
|
|
|
|
|
|
|
|
|
|
SetSuspendCount(thread_snapshot->SuspendCount());
|
|
|
|
|
SetPriority(thread_snapshot->Priority());
|
|
|
|
|
SetTEB(thread_snapshot->ThreadSpecificDataAddress());
|
|
|
|
|
|
|
|
|
|
const MemorySnapshot* stack_snapshot = thread_snapshot->Stack();
|
|
|
|
|
if (stack_snapshot && stack_snapshot->Size() > 0) {
|
|
|
|
|
scoped_ptr<MinidumpMemoryWriter> stack =
|
|
|
|
|
MinidumpMemoryWriter::CreateFromSnapshot(stack_snapshot);
|
2015-12-09 17:36:32 -05:00
|
|
|
|
SetStack(std::move(stack));
|
2014-11-04 12:36:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scoped_ptr<MinidumpContextWriter> context =
|
|
|
|
|
MinidumpContextWriter::CreateFromSnapshot(thread_snapshot->Context());
|
2015-12-09 17:36:32 -05:00
|
|
|
|
SetContext(std::move(context));
|
2014-11-04 12:36:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-09 15:31:29 -04:00
|
|
|
|
const MINIDUMP_THREAD* MinidumpThreadWriter::MinidumpThread() const {
|
|
|
|
|
DCHECK_EQ(state(), kStateWritable);
|
|
|
|
|
|
|
|
|
|
return &thread_;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-27 15:01:39 -04:00
|
|
|
|
void MinidumpThreadWriter::SetStack(scoped_ptr<MinidumpMemoryWriter> stack) {
|
2014-10-09 15:31:29 -04:00
|
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
|
|
2015-12-09 17:36:32 -05:00
|
|
|
|
stack_ = std::move(stack);
|
2014-10-09 15:31:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-27 15:01:39 -04:00
|
|
|
|
void MinidumpThreadWriter::SetContext(
|
|
|
|
|
scoped_ptr<MinidumpContextWriter> context) {
|
2014-10-09 15:31:29 -04:00
|
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
|
|
2015-12-09 17:36:32 -05:00
|
|
|
|
context_ = std::move(context);
|
2014-10-09 15:31:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MinidumpThreadWriter::Freeze() {
|
|
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
|
CHECK(context_);
|
|
|
|
|
|
|
|
|
|
if (!MinidumpWritable::Freeze()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stack_) {
|
|
|
|
|
stack_->RegisterMemoryDescriptor(&thread_.Stack);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context_->RegisterLocationDescriptor(&thread_.ThreadContext);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t MinidumpThreadWriter::SizeOfObject() {
|
|
|
|
|
DCHECK_GE(state(), kStateFrozen);
|
|
|
|
|
|
|
|
|
|
// This object doesn’t directly write anything itself. Its MINIDUMP_THREAD is
|
|
|
|
|
// written by its parent as part of a MINIDUMP_THREAD_LIST, and its children
|
|
|
|
|
// are responsible for writing themselves.
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<internal::MinidumpWritable*> MinidumpThreadWriter::Children() {
|
|
|
|
|
DCHECK_GE(state(), kStateFrozen);
|
|
|
|
|
DCHECK(context_);
|
|
|
|
|
|
|
|
|
|
std::vector<MinidumpWritable*> children;
|
|
|
|
|
if (stack_) {
|
2014-10-27 15:01:39 -04:00
|
|
|
|
children.push_back(stack_.get());
|
2014-10-09 15:31:29 -04:00
|
|
|
|
}
|
2014-10-27 15:01:39 -04:00
|
|
|
|
children.push_back(context_.get());
|
2014-10-09 15:31:29 -04:00
|
|
|
|
|
|
|
|
|
return children;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MinidumpThreadWriter::WriteObject(FileWriterInterface* file_writer) {
|
|
|
|
|
DCHECK_EQ(state(), kStateWritable);
|
|
|
|
|
|
|
|
|
|
// This object doesn’t directly write anything itself. Its MINIDUMP_THREAD is
|
|
|
|
|
// written by its parent as part of a MINIDUMP_THREAD_LIST, and its children
|
|
|
|
|
// are responsible for writing themselves.
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MinidumpThreadListWriter::MinidumpThreadListWriter()
|
|
|
|
|
: MinidumpStreamWriter(),
|
|
|
|
|
threads_(),
|
2015-02-04 17:34:43 -08:00
|
|
|
|
memory_list_writer_(nullptr),
|
|
|
|
|
thread_list_base_() {
|
2014-10-09 15:31:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MinidumpThreadListWriter::~MinidumpThreadListWriter() {
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-04 12:36:29 -05:00
|
|
|
|
void MinidumpThreadListWriter::InitializeFromSnapshot(
|
|
|
|
|
const std::vector<const ThreadSnapshot*>& thread_snapshots,
|
|
|
|
|
MinidumpThreadIDMap* thread_id_map) {
|
|
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
|
DCHECK(threads_.empty());
|
|
|
|
|
|
|
|
|
|
BuildMinidumpThreadIDMap(thread_snapshots, thread_id_map);
|
|
|
|
|
|
|
|
|
|
for (const ThreadSnapshot* thread_snapshot : thread_snapshots) {
|
|
|
|
|
auto thread = make_scoped_ptr(new MinidumpThreadWriter());
|
|
|
|
|
thread->InitializeFromSnapshot(thread_snapshot, thread_id_map);
|
2015-12-09 17:36:32 -05:00
|
|
|
|
AddThread(std::move(thread));
|
2014-11-04 12:36:29 -05:00
|
|
|
|
}
|
2015-10-01 14:04:49 -07:00
|
|
|
|
|
|
|
|
|
// Do this in a separate loop to keep the thread stacks earlier in the dump,
|
|
|
|
|
// and together.
|
|
|
|
|
for (const ThreadSnapshot* thread_snapshot : thread_snapshots)
|
|
|
|
|
memory_list_writer_->AddFromSnapshot(thread_snapshot->ExtraMemory());
|
2014-11-04 12:36:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-09 15:31:29 -04:00
|
|
|
|
void MinidumpThreadListWriter::SetMemoryListWriter(
|
|
|
|
|
MinidumpMemoryListWriter* memory_list_writer) {
|
|
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
|
DCHECK(threads_.empty());
|
|
|
|
|
|
|
|
|
|
memory_list_writer_ = memory_list_writer;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-27 15:01:39 -04:00
|
|
|
|
void MinidumpThreadListWriter::AddThread(
|
|
|
|
|
scoped_ptr<MinidumpThreadWriter> thread) {
|
2014-10-09 15:31:29 -04:00
|
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
|
|
|
|
|
|
if (memory_list_writer_) {
|
|
|
|
|
MinidumpMemoryWriter* stack = thread->Stack();
|
|
|
|
|
if (stack) {
|
|
|
|
|
memory_list_writer_->AddExtraMemory(stack);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-27 15:01:39 -04:00
|
|
|
|
|
|
|
|
|
threads_.push_back(thread.release());
|
2014-10-09 15:31:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MinidumpThreadListWriter::Freeze() {
|
|
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
|
|
|
|
|
|
if (!MinidumpStreamWriter::Freeze()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t thread_count = threads_.size();
|
|
|
|
|
if (!AssignIfInRange(&thread_list_base_.NumberOfThreads, thread_count)) {
|
|
|
|
|
LOG(ERROR) << "thread_count " << thread_count << " out of range";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t MinidumpThreadListWriter::SizeOfObject() {
|
|
|
|
|
DCHECK_GE(state(), kStateFrozen);
|
|
|
|
|
|
|
|
|
|
return sizeof(thread_list_base_) + threads_.size() * sizeof(MINIDUMP_THREAD);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<internal::MinidumpWritable*> MinidumpThreadListWriter::Children() {
|
|
|
|
|
DCHECK_GE(state(), kStateFrozen);
|
|
|
|
|
|
|
|
|
|
std::vector<MinidumpWritable*> children;
|
|
|
|
|
for (MinidumpThreadWriter* thread : threads_) {
|
|
|
|
|
children.push_back(thread);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return children;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MinidumpThreadListWriter::WriteObject(FileWriterInterface* file_writer) {
|
|
|
|
|
DCHECK_EQ(state(), kStateWritable);
|
|
|
|
|
|
|
|
|
|
WritableIoVec iov;
|
|
|
|
|
iov.iov_base = &thread_list_base_;
|
|
|
|
|
iov.iov_len = sizeof(thread_list_base_);
|
|
|
|
|
std::vector<WritableIoVec> iovecs(1, iov);
|
|
|
|
|
|
|
|
|
|
for (const MinidumpThreadWriter* thread : threads_) {
|
|
|
|
|
iov.iov_base = thread->MinidumpThread();
|
2014-10-23 17:25:20 -04:00
|
|
|
|
iov.iov_len = sizeof(MINIDUMP_THREAD);
|
2014-10-09 15:31:29 -04:00
|
|
|
|
iovecs.push_back(iov);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return file_writer->WriteIoVec(&iovecs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MinidumpStreamType MinidumpThreadListWriter::StreamType() const {
|
|
|
|
|
return kMinidumpStreamTypeThreadList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace crashpad
|