From 4d03b7ce5c44d733ad78d32bb8d363674f899326 Mon Sep 17 00:00:00 2001 From: Horst Schirmeier Date: Sat, 6 Apr 2013 22:05:51 +0200 Subject: [PATCH] core/util: MemoryMap learns to materialize in files Change-Id: I003168d3718491524db91b9f7d855d6b3428961c --- src/core/util/CMakeLists.txt | 1 + src/core/util/MemoryMap.cc | 45 ++++++++++++++++++++++++++++++++++++ src/core/util/MemoryMap.hpp | 18 +++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 src/core/util/MemoryMap.cc diff --git a/src/core/util/CMakeLists.txt b/src/core/util/CMakeLists.txt index bfaf0765..a2bec4a3 100644 --- a/src/core/util/CMakeLists.txt +++ b/src/core/util/CMakeLists.txt @@ -17,6 +17,7 @@ set(SRCS gzstream/gzstream.h Logger.cc Logger.hpp + MemoryMap.cc MemoryMap.hpp ProtoStream.cc ProtoStream.hpp diff --git a/src/core/util/MemoryMap.cc b/src/core/util/MemoryMap.cc new file mode 100644 index 00000000..90db5c26 --- /dev/null +++ b/src/core/util/MemoryMap.cc @@ -0,0 +1,45 @@ +#include +#include +#include +#include "MemoryMap.hpp" + +namespace fail { + +bool MemoryMap::readFromFile(char const * const filename) +{ + std::ifstream file(filename); + if (!file.is_open()) { + return false; + } + + std::string buf; + unsigned guest_addr, guest_len; + unsigned count = 0; + + while (getline(file, buf)) { + std::stringstream ss(buf, std::ios::in); + ss >> guest_addr >> guest_len; + add(guest_addr, guest_len); + count++; + } + // assertion kept from original code; usually something's fishy if the file + // contains no entries + assert(count > 0); + return true; +} + +bool MemoryMap::writeToFile(char const * const filename) +{ + std::ofstream file(filename); + if (!file.is_open()) { + return false; + } + + for (iterator it = begin(); it != end(); ++it) { + file << *it << "\t1\n"; + } + + return true; +} + +} diff --git a/src/core/util/MemoryMap.hpp b/src/core/util/MemoryMap.hpp index 15a6cbbe..3aa06118 100644 --- a/src/core/util/MemoryMap.hpp +++ b/src/core/util/MemoryMap.hpp @@ -73,6 +73,24 @@ public: * structure. */ iterator end() { return as.end(); } + /** + * Loads a memory map from a file and merges it with the current state. + * + * File format (addresses and sizes in decimal): + * \code + * address1size1 + * address2size2 + * ... + * \endcode + */ + bool readFromFile(char const * const filename); + /** + * Saves the map to a file. + * + * Currently all access size information is lost; the map is flattened out + * to a long list of single-byte addresses. + */ + bool writeToFile(char const * const filename); }; } // end-of-namespace: fail