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