From 59776f1a0e5baf23664efb62bfdeef9608b8aac6 Mon Sep 17 00:00:00 2001 From: Nav Date: Sat, 24 Dec 2022 02:40:00 +0000 Subject: [PATCH] Memory snapshot data structure --- src/Insight/CMakeLists.txt | 1 + .../MemorySnapshot.cpp | 97 +++++++++++++++++++ .../MemorySnapshot.hpp | 56 +++++++++++ 3 files changed, 154 insertions(+) create mode 100644 src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/MemorySnapshot.cpp create mode 100644 src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/MemorySnapshot.hpp diff --git a/src/Insight/CMakeLists.txt b/src/Insight/CMakeLists.txt index da37f587..efbad715 100755 --- a/src/Insight/CMakeLists.txt +++ b/src/Insight/CMakeLists.txt @@ -79,6 +79,7 @@ target_sources( ${CMAKE_CURRENT_SOURCE_DIR}/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/MemoryRegion.cpp ${CMAKE_CURRENT_SOURCE_DIR}/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/FocusedMemoryRegion.cpp ${CMAKE_CURRENT_SOURCE_DIR}/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/ExcludedMemoryRegion.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/MemorySnapshot.cpp # Memory region manager window ${CMAKE_CURRENT_SOURCE_DIR}/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/MemoryRegionManager/MemoryRegionManagerWindow.cpp diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/MemorySnapshot.cpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/MemorySnapshot.cpp new file mode 100644 index 00000000..521e20c4 --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/MemorySnapshot.cpp @@ -0,0 +1,97 @@ +#include "MemorySnapshot.hpp" + +#include +#include + +#include "src/Helpers/EnumToStringMappings.hpp" +#include "src/Exceptions/Exception.hpp" + +namespace Bloom +{ + MemorySnapshot::MemorySnapshot( + const QString& name, + const QString& description, + Targets::TargetMemoryType memoryType, + const Targets::TargetMemoryBuffer& data, + Targets::TargetProgramCounter programCounter, + const std::vector& focusedRegions, + const std::vector& excludedRegions + ) + : name(name) + , description(description) + , memoryType(memoryType) + , data(data) + , programCounter(programCounter) + , focusedRegions(focusedRegions) + , excludedRegions(excludedRegions) + {} + + MemorySnapshot::MemorySnapshot(const QJsonObject& jsonObject) { + using Exceptions::Exception; + + if ( + !jsonObject.contains("id") + || !jsonObject.contains("name") + || !jsonObject.contains("description") + || !jsonObject.contains("memoryType") + || !jsonObject.contains("hexData") + || !jsonObject.contains("programCounter") + || !jsonObject.contains("createdTimestamp") + || !jsonObject.contains("focusedRegions") + || !jsonObject.contains("excludedRegions") + ) { + throw Exception("Missing data"); + } + + this->id = jsonObject.find("id")->toString(); + this->name = jsonObject.find("name")->toString(); + this->description = jsonObject.find("description")->toString(); + this->memoryType = EnumToStringMappings::targetMemoryTypes.at(jsonObject.find("memoryType")->toString()); + this->programCounter = static_cast(jsonObject.find("programCounter")->toInteger()); + this->createdDate.setSecsSinceEpoch(jsonObject.find("createdTimestamp")->toInteger()); + + const auto hexData = QByteArray::fromHex(jsonObject.find("hexData")->toString().toUtf8()); + this->data = Targets::TargetMemoryBuffer(hexData.begin(), hexData.end()); + + // TODO: Memory regions + } + + QJsonObject MemorySnapshot::toJson() const { + auto focusedRegions = QJsonArray(); + for (const auto& focusedRegion : this->focusedRegions) { + focusedRegions.push_back(focusedRegion.toJson()); + } + + auto excludedRegions = QJsonArray(); + for (const auto& excludedRegion : this->excludedRegions) { + excludedRegions.push_back(excludedRegion.toJson()); + } + + return QJsonObject({ + {"id", this->id}, + {"name", this->name}, + {"description", this->description}, + {"memoryType", EnumToStringMappings::targetMemoryTypes.at(this->memoryType)}, + {"hexData", QString(QByteArray( + reinterpret_cast(this->data.data()), + static_cast(this->data.size()) + ).toHex())}, + {"programCounter", static_cast(this->programCounter)}, + {"createdTimestamp", this->createdDate.toSecsSinceEpoch()}, + {"focusedRegions", focusedRegions}, + {"excludedRegions", excludedRegions}, + }); + } + + bool MemorySnapshot::isCompatible(const Targets::TargetMemoryDescriptor& memoryDescriptor) const { + if (this->memoryType != memoryDescriptor.type) { + return false; + } + + if (this->data.size() != memoryDescriptor.size()) { + return false; + } + + return true; + } +} diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/MemorySnapshot.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/MemorySnapshot.hpp new file mode 100644 index 00000000..07f9c2af --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/MemorySnapshot.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include "src/Targets/TargetMemory.hpp" +#include "src/Helpers/DateTime.hpp" + +#include "FocusedMemoryRegion.hpp" +#include "ExcludedMemoryRegion.hpp" + +namespace Bloom +{ + struct MemorySnapshot + { + public: + QString id = QUuid::createUuid().toString(QUuid::StringFormat::WithoutBraces); + QString name; + QString description; + Targets::TargetMemoryType memoryType; + Targets::TargetMemoryBuffer data; + Targets::TargetProgramCounter programCounter; + QDateTime createdDate = DateTime::currentDateTime(); + + std::vector focusedRegions; + std::vector excludedRegions; + + MemorySnapshot( + const QString& name, + const QString& description, + Targets::TargetMemoryType memoryType, + const Targets::TargetMemoryBuffer& data, + Targets::TargetProgramCounter programCounter, + const std::vector& focusedRegions, + const std::vector& excludedRegions + ); + + MemorySnapshot(const QJsonObject& jsonObject); + + QJsonObject toJson() const; + + bool isCompatible(const Targets::TargetMemoryDescriptor& memoryDescriptor) const; + + virtual ~MemorySnapshot() = default; + + MemorySnapshot(const MemorySnapshot& other) = default; + MemorySnapshot(MemorySnapshot&& other) = default; + + MemorySnapshot& operator = (const MemorySnapshot& other) = default; + MemorySnapshot& operator = (MemorySnapshot&& other) = default; + }; +}