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