Last active
June 21, 2022 19:57
-
-
Save lsem/1416b88d7b013aec65b551715ef7c2d9 to your computer and use it in GitHub Desktop.
parse_linux_mmap.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cassert> | |
#include <iomanip> | |
#include <iostream> | |
#include <optional> | |
#include <string> | |
#include <string_view> | |
#include <tuple> | |
using namespace std; | |
std::optional<pair<string_view, string_view>> split_two(string_view s, | |
string_view tok) { | |
// we need to find first occurence of tok in s. | |
size_t tok_idx = s.find(tok); | |
if (tok_idx != string_view::npos) { | |
auto head = s; | |
head.remove_suffix(s.size() - tok_idx); | |
auto tail = s; | |
tail.remove_prefix(tok_idx + tok.size()); | |
return pair{head, tail}; | |
} else { | |
return std::nullopt; | |
} | |
} | |
int main() { | |
uint64_t total_size = 0; | |
while (!std::cin.eof()) { | |
string line; | |
std::getline(std::cin, line); | |
// std::cout << "line: " << line << "\n"; | |
std::string_view tail = line; | |
std::string_view addr1, addr2; | |
if (auto toks = split_two(tail, "-")) { | |
addr1 = std::get<0>(*toks); | |
tail = std::get<1>(*toks); | |
} else { | |
std::cerr << "failed parsing addr1\n"; | |
continue; | |
} | |
if (auto toks = split_two(tail, " ")) { | |
addr2 = std::get<0>(*toks); | |
tail = std::get<1>(*toks); | |
} else { | |
std::cerr << "failed parsing addr2\n"; | |
continue; | |
} | |
if (auto toks = split_two(tail, " ")) { | |
tail = std::get<1>(*toks); | |
} else { | |
std::cerr << "failed ignore 1\n"; | |
continue; | |
} | |
if (auto toks = split_two(tail, " ")) { | |
tail = std::get<1>(*toks); | |
} else { | |
std::cerr << "failed ignore 2\n"; | |
continue; | |
} | |
if (auto toks = split_two(tail, " ")) { | |
tail = std::get<1>(*toks); | |
} else { | |
std::cerr << "failed ignore 3\n"; | |
continue; | |
} | |
if (auto toks = split_two(tail, " ")) { | |
tail = std::get<1>(*toks); | |
} else { | |
std::cerr << "failed ignore 4\n"; | |
continue; | |
} | |
std::string module_name; | |
size_t module_start_pos = tail.find_first_not_of(" "); | |
if (module_start_pos != std::string_view::npos) { | |
module_name = tail.substr(module_start_pos); | |
// std::cout << "module_name: " << module_name << "\n"; | |
} | |
// std::cout << "addr1: " << add1 << ", addr2: " << addr2 << "\n"; | |
auto address1 = stol(std::string(addr1), 0, 16); | |
auto address2 = stol(std::string(addr2), 0, 16); | |
std::cout << std::hex; | |
// std::cout << "addr1: " << address1 << ", addr2: " << address2 << "\t" << | |
// module_name << "\n"; | |
std::cout << std::setw(8); | |
std::cout << std::hex << address1 << "-" << address2 << " " << std::dec | |
<< (address2 - address1) / 1024 << "\t" << module_name << "\n"; | |
total_size += address2 - address1; | |
} | |
std::cout << "total size in Mb: " << total_size / 1024 / 1024 << "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment