2020-12-24 02:25:53 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-24 02:25:53 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/JsonObject.h>
|
2021-01-24 21:15:34 +03:00
|
|
|
#include <AK/QuickSort.h>
|
2020-12-24 02:25:53 +03:00
|
|
|
#include <AK/String.h>
|
|
|
|
#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
|
|
|
{
|
2021-11-28 01:26:34 +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
|
|
|
|
|
|
|
const char* pid;
|
|
|
|
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
|
|
|
|
2021-11-23 17:33:49 +03:00
|
|
|
auto file = TRY(Core::File::open(String::formatted("/proc/{}/vm", pid), Core::OpenMode::ReadOnly));
|
2020-12-24 02:25:53 +03:00
|
|
|
|
2021-05-31 17:43:25 +03:00
|
|
|
outln("{}:", pid);
|
2020-12-24 02:25:53 +03:00
|
|
|
|
2021-07-22 01:04:19 +03:00
|
|
|
#if ARCH(I386)
|
|
|
|
auto padding = "";
|
|
|
|
#else
|
|
|
|
auto padding = " ";
|
|
|
|
#endif
|
|
|
|
|
2020-12-24 02:25:53 +03:00
|
|
|
if (extended) {
|
2021-07-22 01:04:19 +03:00
|
|
|
outln("Address{} Size Resident Dirty Access VMObject Type Purgeable CoW Pages Name", padding);
|
2020-12-24 02:25:53 +03:00
|
|
|
} else {
|
2021-07-22 01:04:19 +03:00
|
|
|
outln("Address{} Size Access Name", padding);
|
2020-12-24 02:25:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
auto file_contents = file->read_all();
|
2021-11-23 17:33:49 +03:00
|
|
|
auto json = TRY(JsonValue::from_string(file_contents));
|
2021-01-24 21:15:34 +03:00
|
|
|
|
2021-11-15 03:46:51 +03:00
|
|
|
Vector<JsonValue> sorted_regions = json.as_array().values();
|
2021-01-24 21:15:34 +03:00
|
|
|
quick_sort(sorted_regions, [](auto& a, auto& b) {
|
2021-07-21 20:57:05 +03:00
|
|
|
return a.as_object().get("address").to_addr() < b.as_object().get("address").to_addr();
|
2021-01-24 21:15:34 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
for (auto& value : sorted_regions) {
|
2021-05-31 19:59:02 +03:00
|
|
|
auto& map = value.as_object();
|
2021-07-21 20:57:05 +03:00
|
|
|
auto address = map.get("address").to_addr();
|
2020-12-24 02:25:53 +03:00
|
|
|
auto size = map.get("size").to_string();
|
|
|
|
|
2021-02-02 21:58:46 +03:00
|
|
|
auto access = String::formatted("{}{}{}{}{}",
|
2021-01-29 11:33:32 +03:00
|
|
|
(map.get("readable").to_bool() ? "r" : "-"),
|
|
|
|
(map.get("writable").to_bool() ? "w" : "-"),
|
|
|
|
(map.get("executable").to_bool() ? "x" : "-"),
|
2021-02-02 21:58:46 +03:00
|
|
|
(map.get("shared").to_bool() ? "s" : "-"),
|
|
|
|
(map.get("syscall").to_bool() ? "c" : "-"));
|
2020-12-24 02:25:53 +03:00
|
|
|
|
2021-07-21 20:53:38 +03:00
|
|
|
out("{:p} ", address);
|
2021-05-31 17:43:25 +03:00
|
|
|
out("{:>10} ", size);
|
2020-12-24 02:25:53 +03:00
|
|
|
if (extended) {
|
|
|
|
auto resident = map.get("amount_resident").to_string();
|
|
|
|
auto dirty = map.get("amount_dirty").to_string();
|
|
|
|
auto vmobject = map.get("vmobject").to_string();
|
2021-01-29 13:06:09 +03:00
|
|
|
if (vmobject.ends_with("VMObject"))
|
|
|
|
vmobject = vmobject.substring(0, vmobject.length() - 8);
|
2020-12-24 02:25:53 +03:00
|
|
|
auto purgeable = map.get("purgeable").to_string();
|
|
|
|
auto cow_pages = map.get("cow_pages").to_string();
|
2021-05-31 17:43:25 +03:00
|
|
|
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 {
|
2021-05-31 17:43:25 +03:00
|
|
|
out("{:6} ", access);
|
2020-12-24 02:25:53 +03:00
|
|
|
}
|
|
|
|
auto name = map.get("name").to_string();
|
2021-05-31 17:43:25 +03:00
|
|
|
out("{:20}", name);
|
|
|
|
outln();
|
2021-01-24 21:15:34 +03:00
|
|
|
}
|
2020-12-24 02:25:53 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|