|
20 | 20 |
|
21 | 21 | #include "pfs/procfs.hpp"
|
22 | 22 |
|
23 |
| -static void enum_task(const pfs::task& task) |
| 23 | +static void safe(const std::string& what, const std::function<void(void)>& func) |
24 | 24 | {
|
25 | 25 | try
|
26 | 26 | {
|
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 | +} |
30 | 35 |
|
| 36 | +static void enum_task(const pfs::task& task) |
| 37 | +{ |
| 38 | + LOG("========================================================="); |
| 39 | + LOG("Task ID[" << task.id() << "]"); |
| 40 | + LOG("========================================================="); |
| 41 | + |
| 42 | + safe("status", [&]{ |
31 | 43 | auto status = task.get_status();
|
32 | 44 | print(status);
|
| 45 | + }); |
33 | 46 |
|
| 47 | + bool is_kernel_thread = false; |
| 48 | + |
| 49 | + safe("stat", [&]{ |
34 | 50 | auto stat = task.get_stat();
|
35 | 51 | print(stat);
|
36 | 52 |
|
| 53 | + is_kernel_thread = task.is_kernel_thread(stat); |
| 54 | + }); |
| 55 | + |
| 56 | + safe("statm", [&]{ |
37 | 57 | auto mem_stat = task.get_statm();
|
38 | 58 | print(mem_stat);
|
| 59 | + }); |
39 | 60 |
|
| 61 | + safe("io", [&]{ |
40 | 62 | auto io_stat = task.get_io();
|
41 | 63 | print(io_stat);
|
| 64 | + }); |
42 | 65 |
|
| 66 | + safe("comm", [&]{ |
43 | 67 | auto comm = task.get_comm();
|
44 | 68 | print(comm);
|
| 69 | + }); |
45 | 70 |
|
46 |
| - if (!task.is_kernel_thread(stat)) |
47 |
| - { |
| 71 | + if (!is_kernel_thread) |
| 72 | + { |
| 73 | + safe("exe", [&]{ |
48 | 74 | auto exe = task.get_exe();
|
49 | 75 | print(exe);
|
50 |
| - } |
| 76 | + }); |
| 77 | + } |
51 | 78 |
|
| 79 | + safe("cmdline", [&]{ |
52 | 80 | auto cmdline = task.get_cmdline();
|
53 | 81 | print(cmdline);
|
| 82 | + }); |
54 | 83 |
|
| 84 | + safe("cwd", [&]{ |
55 | 85 | auto cwd = task.get_cwd();
|
56 | 86 | print(cwd);
|
| 87 | + }); |
57 | 88 |
|
| 89 | + safe("environ", [&]{ |
58 | 90 | auto environ = task.get_environ();
|
59 | 91 | print(environ);
|
| 92 | + }); |
| 93 | + |
| 94 | + std::vector<pfs::mem_region> maps; |
60 | 95 |
|
61 |
| - auto maps = task.get_maps(); |
| 96 | + safe("maps", [&]{ |
| 97 | + maps = task.get_maps(); |
62 | 98 | print(maps);
|
| 99 | + }); |
63 | 100 |
|
64 |
| - if (!maps.empty()) |
65 |
| - { |
| 101 | + if (!maps.empty()) |
| 102 | + { |
| 103 | + safe("mem", [&]{ |
66 | 104 | 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(); |
69 | 107 | auto header_bytes = mem.read(first_map.start_address, BYTES);
|
70 | 108 | auto header = hexlify(header_bytes);
|
71 | 109 | print(header);
|
72 |
| - } |
| 110 | + }); |
| 111 | + } |
73 | 112 |
|
| 113 | + safe("mountinfo", [&]{ |
74 | 114 | auto mountinfo = task.get_mountinfo();
|
75 | 115 | print(mountinfo);
|
| 116 | + }); |
76 | 117 |
|
| 118 | + safe("cgroups", [&]{ |
77 | 119 | auto cgroups = task.get_cgroups();
|
78 | 120 | print(cgroups);
|
| 121 | + }); |
79 | 122 |
|
| 123 | + safe("ns", [&]{ |
80 | 124 | auto ns = task.get_ns();
|
81 | 125 | print(ns);
|
| 126 | + }); |
82 | 127 |
|
| 128 | + safe("fds", [&]{ |
83 | 129 | auto fds = task.get_fds();
|
84 | 130 | 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 | + }); |
91 | 132 | }
|
92 | 133 |
|
93 | 134 | int enum_tasks(std::vector<std::string>&& args)
|
|
0 commit comments