Skip to content

Commit 6d0446e

Browse files
committed
Sample handles task enum exceptions better
1 parent 3571af3 commit 6d0446e

File tree

2 files changed

+62
-20
lines changed

2 files changed

+62
-20
lines changed

sample/enum_task.cpp

+60-19
Original file line numberDiff line numberDiff line change
@@ -20,74 +20,115 @@
2020

2121
#include "pfs/procfs.hpp"
2222

23-
static void enum_task(const pfs::task& task)
23+
static void safe(const std::string& what, const std::function<void(void)>& func)
2424
{
2525
try
2626
{
27-
LOG("=========================================================");
28-
LOG("Task ID[" << task.id() << "]");
29-
LOG("=========================================================");
27+
func();
28+
}
29+
catch (const std::runtime_error& ex)
30+
{
31+
LOG("Error getting [" << what << "]:");
32+
LOG(TAB << ex.what());
33+
}
34+
}
3035

36+
static void enum_task(const pfs::task& task)
37+
{
38+
LOG("=========================================================");
39+
LOG("Task ID[" << task.id() << "]");
40+
LOG("=========================================================");
41+
42+
safe("status", [&]{
3143
auto status = task.get_status();
3244
print(status);
45+
});
3346

47+
bool is_kernel_thread = false;
48+
49+
safe("stat", [&]{
3450
auto stat = task.get_stat();
3551
print(stat);
3652

53+
is_kernel_thread = task.is_kernel_thread(stat);
54+
});
55+
56+
safe("statm", [&]{
3757
auto mem_stat = task.get_statm();
3858
print(mem_stat);
59+
});
3960

61+
safe("io", [&]{
4062
auto io_stat = task.get_io();
4163
print(io_stat);
64+
});
4265

66+
safe("comm", [&]{
4367
auto comm = task.get_comm();
4468
print(comm);
69+
});
4570

46-
if (!task.is_kernel_thread(stat))
47-
{
71+
if (!is_kernel_thread)
72+
{
73+
safe("exe", [&]{
4874
auto exe = task.get_exe();
4975
print(exe);
50-
}
76+
});
77+
}
5178

79+
safe("cmdline", [&]{
5280
auto cmdline = task.get_cmdline();
5381
print(cmdline);
82+
});
5483

84+
safe("cwd", [&]{
5585
auto cwd = task.get_cwd();
5686
print(cwd);
87+
});
5788

89+
safe("environ", [&]{
5890
auto environ = task.get_environ();
5991
print(environ);
92+
});
93+
94+
std::vector<pfs::mem_region> maps;
6095

61-
auto maps = task.get_maps();
96+
safe("maps", [&]{
97+
maps = task.get_maps();
6298
print(maps);
99+
});
63100

64-
if (!maps.empty())
65-
{
101+
if (!maps.empty())
102+
{
103+
safe("mem", [&]{
66104
static const size_t BYTES = 8;
67-
auto mem = task.get_mem();
68-
auto first_map = *maps.begin();
105+
auto mem = task.get_mem();
106+
auto first_map = *maps.begin();
69107
auto header_bytes = mem.read(first_map.start_address, BYTES);
70108
auto header = hexlify(header_bytes);
71109
print(header);
72-
}
110+
});
111+
}
73112

113+
safe("mountinfo", [&]{
74114
auto mountinfo = task.get_mountinfo();
75115
print(mountinfo);
116+
});
76117

118+
safe("cgroups", [&]{
77119
auto cgroups = task.get_cgroups();
78120
print(cgroups);
121+
});
79122

123+
safe("ns", [&]{
80124
auto ns = task.get_ns();
81125
print(ns);
126+
});
82127

128+
safe("fds", [&]{
83129
auto fds = task.get_fds();
84130
print(fds);
85-
}
86-
catch (const std::runtime_error& ex)
87-
{
88-
LOG("Error when printing task[" << task.id() << "]:");
89-
LOG(TAB << ex.what());
90-
}
131+
});
91132
}
92133

93134
int enum_tasks(std::vector<std::string>&& args)

sample/format.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,8 @@ inline std::ostream& operator<<(std::ostream& out, const pfs::zone& zone)
688688

689689
inline std::ostream& operator<<(std::ostream& out, const pfs::fd& fd)
690690
{
691-
out << "target[" << fd.get_target() << "] ";
691+
std::string target = fd.get_target();
692+
out << "target[" << target << "] ";
692693
return out;
693694
}
694695

0 commit comments

Comments
 (0)