Skip to content

Commit

Permalink
Sample handles task enum exceptions better
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrugman committed May 19, 2024
1 parent 3571af3 commit 6d0446e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 20 deletions.
79 changes: 60 additions & 19 deletions sample/enum_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(void)>& 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<pfs::mem_region> 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<std::string>&& args)
Expand Down
3 changes: 2 additions & 1 deletion sample/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 6d0446e

Please sign in to comment.