ladybird/Userland/Utilities/pmap.cpp

87 lines
3.0 KiB
C++
Raw Normal View History

2020-12-24 02:25:53 +03:00
/*
2022-09-14 19:01:17 +03:00
* Copyright (c) 2020-2022, the SerenityOS developers.
2020-12-24 02:25:53 +03:00
*
* SPDX-License-Identifier: BSD-2-Clause
2020-12-24 02:25:53 +03:00
*/
#include <AK/ByteString.h>
2020-12-24 02:25:53 +03:00
#include <AK/JsonObject.h>
#include <AK/QuickSort.h>
2020-12-24 02:25:53 +03:00
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
2021-11-23 17:33:49 +03:00
#include <LibCore/System.h>
#include <LibMain/Main.h>
2020-12-24 02:25:53 +03:00
2021-11-23 17:33:49 +03:00
ErrorOr<int> serenity_main(Main::Arguments arguments)
2020-12-24 02:25:53 +03:00
{
TRY(Core::System::pledge("stdio rpath"));
2021-11-23 17:33:49 +03:00
TRY(Core::System::unveil("/proc", "r"));
TRY(Core::System::unveil(nullptr, nullptr));
2020-12-24 02:25:53 +03:00
StringView pid;
2020-12-24 02:25:53 +03:00
static bool extended = false;
Core::ArgsParser args_parser;
args_parser.add_option(extended, "Extended output", nullptr, 'x');
args_parser.add_positional_argument(pid, "PID", "PID", Core::ArgsParser::Required::Yes);
2021-11-23 17:33:49 +03:00
args_parser.parse(arguments);
2020-12-24 02:25:53 +03:00
auto file = TRY(Core::File::open(ByteString::formatted("/proc/{}/vm", pid), Core::File::OpenMode::Read));
2020-12-24 02:25:53 +03:00
outln("{}:", pid);
2020-12-24 02:25:53 +03:00
auto padding = " ";
2020-12-24 02:25:53 +03:00
if (extended) {
outln("Address{} Size Resident Dirty Access VMObject Type Purgeable CoW Pages Name", padding);
2020-12-24 02:25:53 +03:00
} else {
outln("Address{} Size Access Name", padding);
2020-12-24 02:25:53 +03:00
}
auto file_contents = TRY(file->read_until_eof());
2021-11-23 17:33:49 +03:00
auto json = TRY(JsonValue::from_string(file_contents));
Vector<JsonValue> sorted_regions = json.as_array().values();
quick_sort(sorted_regions, [](auto& a, auto& b) {
return a.as_object().get_addr("address"sv).value_or(0) < b.as_object().get_addr("address"sv).value_or(0);
});
for (auto& value : sorted_regions) {
auto& map = value.as_object();
auto address = map.get_addr("address"sv).value_or(0);
auto size = map.get_u64("size"sv).value();
2020-12-24 02:25:53 +03:00
auto access = ByteString::formatted("{}{}{}{}{}",
(map.get_bool("readable"sv).value_or(false) ? "r" : "-"),
(map.get_bool("writable"sv).value_or(false) ? "w" : "-"),
(map.get_bool("executable"sv).value_or(false) ? "x" : "-"),
(map.get_bool("shared"sv).value_or(false) ? "s" : "-"),
(map.get_bool("syscall"sv).value_or(false) ? "c" : "-"));
2020-12-24 02:25:53 +03:00
out("{:p} ", address);
out("{:>10} ", size);
2020-12-24 02:25:53 +03:00
if (extended) {
auto resident = map.get_u64("amount_resident"sv).value();
auto dirty = map.get_u64("amount_dirty"sv).value();
auto vmobject = map.get_byte_string("vmobject"sv).value();
if (vmobject.ends_with("VMObject"sv))
vmobject = vmobject.substring(0, vmobject.length() - 8);
auto purgeable = map.get_u64("purgeable"sv).value();
auto cow_pages = map.get_u64("cow_pages"sv).value();
out("{:>10} ", resident);
out("{:>10} ", dirty);
out("{:6} ", access);
out("{:14} ", vmobject);
out("{:10} ", purgeable);
out("{:>10} ", cow_pages);
2020-12-24 02:25:53 +03:00
} else {
out("{:6} ", access);
2020-12-24 02:25:53 +03:00
}
auto name = map.get_byte_string("name"sv).value_or({});
out("{:20}", name);
outln();
}
2020-12-24 02:25:53 +03:00
return 0;
}