From 6d0446e3386b7b9f704142e90a247e18cd419535 Mon Sep 17 00:00:00 2001 From: Daniel Trugman Date: Sun, 19 May 2024 09:23:38 +0100 Subject: [PATCH] Sample handles task enum exceptions better --- sample/enum_task.cpp | 79 +++++++++++++++++++++++++++++++++----------- sample/format.hpp | 3 +- 2 files changed, 62 insertions(+), 20 deletions(-) diff --git a/sample/enum_task.cpp b/sample/enum_task.cpp index edd03c9..35a19ff 100644 --- a/sample/enum_task.cpp +++ b/sample/enum_task.cpp @@ -20,74 +20,115 @@ #include "pfs/procfs.hpp" -static void enum_task(const pfs::task& task) +static void safe(const std::string& what, const std::function& func) { try { - LOG("========================================================="); - LOG("Task ID[" << task.id() << "]"); - LOG("========================================================="); + func(); + } + catch (const std::runtime_error& ex) + { + LOG("Error getting [" << what << "]:"); + LOG(TAB << ex.what()); + } +} +static void enum_task(const pfs::task& task) +{ + LOG("========================================================="); + LOG("Task ID[" << task.id() << "]"); + LOG("========================================================="); + + safe("status", [&]{ auto status = task.get_status(); print(status); + }); + bool is_kernel_thread = false; + + safe("stat", [&]{ auto stat = task.get_stat(); print(stat); + is_kernel_thread = task.is_kernel_thread(stat); + }); + + safe("statm", [&]{ auto mem_stat = task.get_statm(); print(mem_stat); + }); + safe("io", [&]{ auto io_stat = task.get_io(); print(io_stat); + }); + safe("comm", [&]{ auto comm = task.get_comm(); print(comm); + }); - if (!task.is_kernel_thread(stat)) - { + if (!is_kernel_thread) + { + safe("exe", [&]{ auto exe = task.get_exe(); print(exe); - } + }); + } + safe("cmdline", [&]{ auto cmdline = task.get_cmdline(); print(cmdline); + }); + safe("cwd", [&]{ auto cwd = task.get_cwd(); print(cwd); + }); + safe("environ", [&]{ auto environ = task.get_environ(); print(environ); + }); + + std::vector maps; - auto maps = task.get_maps(); + safe("maps", [&]{ + maps = task.get_maps(); print(maps); + }); - if (!maps.empty()) - { + if (!maps.empty()) + { + safe("mem", [&]{ static const size_t BYTES = 8; - auto mem = task.get_mem(); - auto first_map = *maps.begin(); + auto mem = task.get_mem(); + auto first_map = *maps.begin(); auto header_bytes = mem.read(first_map.start_address, BYTES); auto header = hexlify(header_bytes); print(header); - } + }); + } + safe("mountinfo", [&]{ auto mountinfo = task.get_mountinfo(); print(mountinfo); + }); + safe("cgroups", [&]{ auto cgroups = task.get_cgroups(); print(cgroups); + }); + safe("ns", [&]{ auto ns = task.get_ns(); print(ns); + }); + safe("fds", [&]{ auto fds = task.get_fds(); print(fds); - } - catch (const std::runtime_error& ex) - { - LOG("Error when printing task[" << task.id() << "]:"); - LOG(TAB << ex.what()); - } + }); } int enum_tasks(std::vector&& args) diff --git a/sample/format.hpp b/sample/format.hpp index e7ed06d..842b07e 100644 --- a/sample/format.hpp +++ b/sample/format.hpp @@ -688,7 +688,8 @@ inline std::ostream& operator<<(std::ostream& out, const pfs::zone& zone) inline std::ostream& operator<<(std::ostream& out, const pfs::fd& fd) { - out << "target[" << fd.get_target() << "] "; + std::string target = fd.get_target(); + out << "target[" << target << "] "; return out; }