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
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-24 02:25:53 +03:00
|
|
|
*/
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
#include <AK/ByteString.h>
|
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 <LibCore/ArgsParser.h>
|
2023-02-09 05:02:46 +03:00
|
|
|
#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
|
|
|
|
2023-02-28 23:41:43 +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
|
|
|
|
2023-12-16 17:19:34 +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
|
|
|
|
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
|
|
|
auto padding = " ";
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-12-11 19:49:00 +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));
|
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) {
|
2022-12-22 17:28:38 +03:00
|
|
|
return a.as_object().get_addr("address"sv).value_or(0) < b.as_object().get_addr("address"sv).value_or(0);
|
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();
|
2022-12-22 17:28:38 +03:00
|
|
|
auto address = map.get_addr("address"sv).value_or(0);
|
2024-01-06 23:49:17 +03:00
|
|
|
auto size = map.get_u64("size"sv).value();
|
2020-12-24 02:25:53 +03:00
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
auto access = ByteString::formatted("{}{}{}{}{}",
|
2022-12-22 17:28:38 +03:00
|
|
|
(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
|
|
|
|
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) {
|
2024-01-06 23:49:17 +03:00
|
|
|
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();
|
2022-07-11 20:32:29 +03:00
|
|
|
if (vmobject.ends_with("VMObject"sv))
|
2021-01-29 13:06:09 +03:00
|
|
|
vmobject = vmobject.substring(0, vmobject.length() - 8);
|
2024-01-06 23:49:17 +03:00
|
|
|
auto purgeable = map.get_u64("purgeable"sv).value();
|
|
|
|
auto cow_pages = map.get_u64("cow_pages"sv).value();
|
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
|
|
|
}
|
2023-12-16 17:19:34 +03:00
|
|
|
auto name = map.get_byte_string("name"sv).value_or({});
|
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;
|
|
|
|
}
|