2022-09-06 19:14:07 -04:00
|
|
|
|
// Copyright 2014 The Crashpad Authors
|
2014-08-12 10:26:40 -07: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.
|
|
|
|
|
|
|
|
|
|
#ifndef CRASHPAD_MINIDUMP_MINIDUMP_MEMORY_WRITER_H_
|
|
|
|
|
#define CRASHPAD_MINIDUMP_MINIDUMP_MEMORY_WRITER_H_
|
|
|
|
|
|
2015-02-04 17:30:03 -08:00
|
|
|
|
#include <windows.h>
|
2014-08-12 10:26:40 -07:00
|
|
|
|
#include <dbghelp.h>
|
2014-10-23 18:47:27 -04:00
|
|
|
|
#include <stdint.h>
|
2014-08-12 10:26:40 -07:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
|
#include <memory>
|
2014-08-12 10:26:40 -07:00
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "minidump/minidump_stream_writer.h"
|
|
|
|
|
#include "minidump/minidump_writable.h"
|
2016-08-25 15:01:27 -07:00
|
|
|
|
#include "snapshot/memory_snapshot.h"
|
2015-03-06 16:05:34 -08:00
|
|
|
|
#include "util/file/file_io.h"
|
2014-08-12 10:26:40 -07:00
|
|
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
|
|
|
|
|
|
//! \brief The base class for writers of memory ranges pointed to by
|
|
|
|
|
//! MINIDUMP_MEMORY_DESCRIPTOR objects in a minidump file.
|
2016-08-25 15:01:27 -07:00
|
|
|
|
class SnapshotMinidumpMemoryWriter : public internal::MinidumpWritable,
|
|
|
|
|
public MemorySnapshot::Delegate {
|
2014-08-12 10:26:40 -07:00
|
|
|
|
public:
|
2016-08-25 15:01:27 -07:00
|
|
|
|
explicit SnapshotMinidumpMemoryWriter(const MemorySnapshot* memory_snapshot);
|
2021-09-20 12:55:12 -07:00
|
|
|
|
|
|
|
|
|
SnapshotMinidumpMemoryWriter(const SnapshotMinidumpMemoryWriter&) = delete;
|
|
|
|
|
SnapshotMinidumpMemoryWriter& operator=(const SnapshotMinidumpMemoryWriter&) =
|
|
|
|
|
delete;
|
|
|
|
|
|
2016-08-25 15:01:27 -07:00
|
|
|
|
~SnapshotMinidumpMemoryWriter() override;
|
2014-10-30 17:15:49 -04:00
|
|
|
|
|
2014-08-12 10:26:40 -07:00
|
|
|
|
//! \brief Returns a MINIDUMP_MEMORY_DESCRIPTOR referencing the data that this
|
|
|
|
|
//! object writes.
|
|
|
|
|
//!
|
|
|
|
|
//! This method is expected to be called by a MinidumpMemoryListWriter in
|
|
|
|
|
//! order to obtain a MINIDUMP_MEMORY_DESCRIPTOR to include in its list.
|
|
|
|
|
//!
|
|
|
|
|
//! \note Valid in #kStateWritable.
|
|
|
|
|
const MINIDUMP_MEMORY_DESCRIPTOR* MinidumpMemoryDescriptor() const;
|
|
|
|
|
|
|
|
|
|
//! \brief Registers a memory descriptor as one that should point to the
|
|
|
|
|
//! object on which this method is called.
|
|
|
|
|
//!
|
|
|
|
|
//! This method is expected to be called by objects of other classes, when
|
|
|
|
|
//! those other classes have their own memory descriptors that need to point
|
|
|
|
|
//! to memory ranges within a minidump file. MinidumpThreadWriter is one such
|
|
|
|
|
//! class. This method is public for this reason, otherwise it would suffice
|
|
|
|
|
//! to be private.
|
|
|
|
|
//!
|
|
|
|
|
//! \note Valid in #kStateFrozen or any preceding state.
|
|
|
|
|
void RegisterMemoryDescriptor(MINIDUMP_MEMORY_DESCRIPTOR* memory_descriptor);
|
|
|
|
|
|
2018-02-01 15:54:56 -08:00
|
|
|
|
//! \brief Sets the underlying memory snapshot. Does not take ownership of \a
|
|
|
|
|
//! memory_snapshot.
|
|
|
|
|
void SetSnapshot(const MemorySnapshot* memory_snapshot) {
|
|
|
|
|
memory_snapshot_ = memory_snapshot;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-25 15:01:27 -07:00
|
|
|
|
private:
|
2018-02-01 15:54:56 -08:00
|
|
|
|
friend class MinidumpMemoryListWriter;
|
|
|
|
|
|
2016-08-25 15:01:27 -07:00
|
|
|
|
// MemorySnapshot::Delegate:
|
|
|
|
|
bool MemorySnapshotDelegateRead(void* data, size_t size) override;
|
2014-08-12 10:26:40 -07:00
|
|
|
|
|
|
|
|
|
// MinidumpWritable:
|
2014-10-14 11:11:57 -04:00
|
|
|
|
bool Freeze() override;
|
|
|
|
|
size_t SizeOfObject() final;
|
2016-08-25 15:01:27 -07:00
|
|
|
|
bool WriteObject(FileWriterInterface* file_writer) override;
|
2014-08-12 10:26:40 -07:00
|
|
|
|
|
|
|
|
|
//! \brief Returns the object’s desired byte-boundary alignment.
|
|
|
|
|
//!
|
|
|
|
|
//! Memory regions are aligned to a 16-byte boundary. The actual alignment
|
|
|
|
|
//! requirements of any data within the memory region are unknown, and may be
|
|
|
|
|
//! more or less strict than this depending on the platform.
|
|
|
|
|
//!
|
|
|
|
|
//! \return `16`.
|
|
|
|
|
//!
|
|
|
|
|
//! \note Valid in #kStateFrozen or any subsequent state.
|
2014-10-14 11:11:57 -04:00
|
|
|
|
size_t Alignment() override;
|
2014-08-12 10:26:40 -07:00
|
|
|
|
|
2015-03-06 16:05:34 -08:00
|
|
|
|
bool WillWriteAtOffsetImpl(FileOffset offset) override;
|
2014-08-12 10:26:40 -07:00
|
|
|
|
|
|
|
|
|
//! \brief Returns the object’s desired write phase.
|
|
|
|
|
//!
|
|
|
|
|
//! Memory regions are written at the end of minidump files, because it is
|
|
|
|
|
//! expected that unlike most other data in a minidump file, the contents of
|
|
|
|
|
//! memory regions will be accessed sparsely.
|
|
|
|
|
//!
|
|
|
|
|
//! \return #kPhaseLate.
|
|
|
|
|
//!
|
|
|
|
|
//! \note Valid in any state.
|
2014-10-14 11:11:57 -04:00
|
|
|
|
Phase WritePhase() final;
|
2014-08-12 10:26:40 -07:00
|
|
|
|
|
2016-08-25 15:01:27 -07:00
|
|
|
|
//! \brief Gets the underlying memory snapshot that the memory writer will
|
|
|
|
|
//! write to the minidump.
|
2018-02-01 15:54:56 -08:00
|
|
|
|
const MemorySnapshot* UnderlyingSnapshot() const { return memory_snapshot_; }
|
2016-08-25 15:01:27 -07:00
|
|
|
|
|
2014-08-12 10:26:40 -07:00
|
|
|
|
MINIDUMP_MEMORY_DESCRIPTOR memory_descriptor_;
|
|
|
|
|
|
|
|
|
|
// weak
|
|
|
|
|
std::vector<MINIDUMP_MEMORY_DESCRIPTOR*> registered_memory_descriptors_;
|
2016-08-25 15:01:27 -07:00
|
|
|
|
const MemorySnapshot* memory_snapshot_;
|
|
|
|
|
FileWriterInterface* file_writer_;
|
2014-08-12 10:26:40 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//! \brief The writer for a MINIDUMP_MEMORY_LIST stream in a minidump file,
|
|
|
|
|
//! containing a list of MINIDUMP_MEMORY_DESCRIPTOR objects.
|
|
|
|
|
class MinidumpMemoryListWriter final : public internal::MinidumpStreamWriter {
|
|
|
|
|
public:
|
|
|
|
|
MinidumpMemoryListWriter();
|
2021-09-20 12:55:12 -07:00
|
|
|
|
|
|
|
|
|
MinidumpMemoryListWriter(const MinidumpMemoryListWriter&) = delete;
|
|
|
|
|
MinidumpMemoryListWriter& operator=(const MinidumpMemoryListWriter&) = delete;
|
|
|
|
|
|
2014-10-27 15:01:39 -04:00
|
|
|
|
~MinidumpMemoryListWriter() override;
|
2014-08-12 10:26:40 -07:00
|
|
|
|
|
2016-08-25 15:01:27 -07:00
|
|
|
|
//! \brief Adds a concrete initialized SnapshotMinidumpMemoryWriter for each
|
|
|
|
|
//! memory snapshot in \a memory_snapshots to the MINIDUMP_MEMORY_LIST.
|
2014-10-30 17:15:49 -04:00
|
|
|
|
//!
|
|
|
|
|
//! Memory snapshots are added in the fashion of AddMemory().
|
|
|
|
|
//!
|
|
|
|
|
//! \param[in] memory_snapshots The memory snapshots to use as source data.
|
|
|
|
|
//!
|
|
|
|
|
//! \note Valid in #kStateMutable.
|
|
|
|
|
void AddFromSnapshot(
|
|
|
|
|
const std::vector<const MemorySnapshot*>& memory_snapshots);
|
|
|
|
|
|
2016-08-25 15:01:27 -07:00
|
|
|
|
//! \brief Adds a SnapshotMinidumpMemoryWriter to the MINIDUMP_MEMORY_LIST.
|
2014-08-12 10:26:40 -07:00
|
|
|
|
//!
|
2014-10-27 15:01:39 -04:00
|
|
|
|
//! This object takes ownership of \a memory_writer and becomes its parent in
|
|
|
|
|
//! the overall tree of internal::MinidumpWritable objects.
|
2014-08-12 10:26:40 -07:00
|
|
|
|
//!
|
|
|
|
|
//! \note Valid in #kStateMutable.
|
2016-08-25 15:01:27 -07:00
|
|
|
|
void AddMemory(std::unique_ptr<SnapshotMinidumpMemoryWriter> memory_writer);
|
2014-08-12 10:26:40 -07:00
|
|
|
|
|
2016-08-25 15:01:27 -07:00
|
|
|
|
//! \brief Adds a SnapshotMinidumpMemoryWriter that’s a child of another
|
2014-08-12 10:26:40 -07:00
|
|
|
|
//! internal::MinidumpWritable object to the MINIDUMP_MEMORY_LIST.
|
|
|
|
|
//!
|
|
|
|
|
//! \a memory_writer does not become a child of this object, but the
|
|
|
|
|
//! MINIDUMP_MEMORY_LIST will still contain a MINIDUMP_MEMORY_DESCRIPTOR for
|
|
|
|
|
//! it. \a memory_writer must be a child of another object in the
|
|
|
|
|
//! internal::MinidumpWritable tree.
|
|
|
|
|
//!
|
|
|
|
|
//! This method exists to be called by objects that have their own
|
2016-08-25 15:01:27 -07:00
|
|
|
|
//! SnapshotMinidumpMemoryWriter children but wish for them to also appear in
|
|
|
|
|
//! the minidump file’s MINIDUMP_MEMORY_LIST. MinidumpThreadWriter, which has
|
|
|
|
|
//! a SnapshotMinidumpMemoryWriter for thread stack memory, is an example.
|
2014-08-12 10:26:40 -07:00
|
|
|
|
//!
|
|
|
|
|
//! \note Valid in #kStateMutable.
|
2018-02-01 15:54:56 -08:00
|
|
|
|
void AddNonOwnedMemory(SnapshotMinidumpMemoryWriter* memory_writer);
|
2014-08-12 10:26:40 -07:00
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
// MinidumpWritable:
|
2014-10-14 11:11:57 -04:00
|
|
|
|
bool Freeze() override;
|
|
|
|
|
size_t SizeOfObject() override;
|
|
|
|
|
std::vector<MinidumpWritable*> Children() override;
|
|
|
|
|
bool WriteObject(FileWriterInterface* file_writer) override;
|
2014-08-12 10:26:40 -07:00
|
|
|
|
|
|
|
|
|
// MinidumpStreamWriter:
|
2014-10-14 11:11:57 -04:00
|
|
|
|
MinidumpStreamType StreamType() const override;
|
2014-08-12 10:26:40 -07:00
|
|
|
|
|
2018-02-01 15:54:56 -08:00
|
|
|
|
//! \brief Merges any overlapping and abutting memory ranges that were added
|
|
|
|
|
//! via AddFromSnapshot() and AddMemory() into single entries.
|
|
|
|
|
//!
|
|
|
|
|
//! This is expected to be called once just before writing, generally from
|
|
|
|
|
//! Freeze().
|
|
|
|
|
//!
|
|
|
|
|
//! This function has the side-effect of merging owned ranges, dropping any
|
|
|
|
|
//! owned ranges that overlap with non-owned ranges, removing empty ranges,
|
|
|
|
|
//! and sorting all ranges by address.
|
|
|
|
|
//!
|
|
|
|
|
//! Per its name, this coalesces owned memory, however, this is not a complete
|
|
|
|
|
//! solution for ensuring that no overlapping memory ranges are emitted in the
|
|
|
|
|
//! minidump. In particular, if AddNonOwnedMemory() is used to add multiple
|
|
|
|
|
//! overlapping ranges, then overlapping ranges will still be emitted to the
|
|
|
|
|
//! minidump. Currently, AddNonOwnedMemory() is used only for adding thread
|
|
|
|
|
//! stacks, so overlapping shouldn't be a problem in practice. For more
|
|
|
|
|
//! details see https://crashpad.chromium.org/bug/61 and
|
|
|
|
|
//! https://crrev.com/c/374539.
|
|
|
|
|
void CoalesceOwnedMemory();
|
|
|
|
|
|
2014-08-12 10:26:40 -07:00
|
|
|
|
private:
|
2018-02-01 15:54:56 -08:00
|
|
|
|
//! \brief Drops children_ ranges that overlap non_owned_memory_writers_.
|
|
|
|
|
void DropRangesThatOverlapNonOwned();
|
|
|
|
|
|
|
|
|
|
std::vector<SnapshotMinidumpMemoryWriter*> non_owned_memory_writers_; // weak
|
2017-10-19 00:26:38 -04:00
|
|
|
|
std::vector<std::unique_ptr<SnapshotMinidumpMemoryWriter>> children_;
|
2018-02-01 15:54:56 -08:00
|
|
|
|
std::vector<std::unique_ptr<const MemorySnapshot>>
|
|
|
|
|
snapshots_created_during_merge_;
|
|
|
|
|
std::vector<SnapshotMinidumpMemoryWriter*> all_memory_writers_; // weak
|
2015-02-04 17:34:43 -08:00
|
|
|
|
MINIDUMP_MEMORY_LIST memory_list_base_;
|
2014-08-12 10:26:40 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace crashpad
|
|
|
|
|
|
|
|
|
|
#endif // CRASHPAD_MINIDUMP_MINIDUMP_MEMORY_WRITER_H_
|