-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpstree.cpp
More file actions
262 lines (238 loc) · 9.66 KB
/
pstree.cpp
File metadata and controls
262 lines (238 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// pstree - display a tree of processes (Windows port)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tlhelp32.h>
#include <psapi.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
#pragma comment(lib, "psapi.lib")
#pragma comment(lib, "advapi32.lib")
static void usage() {
fprintf(stderr,
"Usage: pstree [-acGhlnpsSuUZ] [ -H PID ] [ -T ] [ PID | USER ]\n"
" or: pstree -V\n\n"
" -a show command line arguments\n"
" -A use ASCII line drawing characters\n"
" -c don't compact identical subtrees\n"
" -h highlight current process and its ancestors\n"
" -H PID highlight this process and its ancestors\n"
" -l don't truncate long lines\n"
" -n sort output by process ID\n"
" -p show PIDs; implies -c\n"
" -s show parent process of specified process\n"
" -T hide threads\n"
" -u show uid transitions\n"
" -U use UTF-8 (Unicode) line drawing characters\n"
" -V display version information\n"
);
}
struct Proc {
DWORD pid, ppid;
std::string name, user;
};
static std::string get_user(DWORD pid) {
HANDLE hProc = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
if (!hProc) return "";
HANDLE hToken = NULL;
if (!OpenProcessToken(hProc, TOKEN_QUERY, &hToken)) { CloseHandle(hProc); return ""; }
CloseHandle(hProc);
DWORD sz = 0;
GetTokenInformation(hToken, TokenUser, NULL, 0, &sz);
if (!sz) { CloseHandle(hToken); return ""; }
std::vector<char> buf(sz);
if (!GetTokenInformation(hToken, TokenUser, buf.data(), sz, &sz)) { CloseHandle(hToken); return ""; }
CloseHandle(hToken);
TOKEN_USER* tu = (TOKEN_USER*)buf.data();
char name[256]={}, domain[256]={};
DWORD nl=sizeof(name), dl=sizeof(domain);
SID_NAME_USE use;
if (LookupAccountSidA(NULL, tu->User.Sid, name, &nl, domain, &dl, &use)) return name;
return "";
}
int main(int argc, char* argv[]) {
// Set console to UTF-8 so box-drawing characters render correctly
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
// Enable VT processing for proper rendering
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mode = 0;
if (GetConsoleMode(hOut, &mode))
SetConsoleMode(hOut, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
bool show_pids = false;
bool sort_by_pid = false;
bool use_ascii = false;
bool show_uid = false;
bool compact = true; // -c disables compaction
DWORD highlight_pid = 0;
DWORD root_pid = 0;
std::string root_user;
for (int i = 1; i < argc; i++) {
char* a = argv[i];
if (strcmp(a, "--help") == 0) { usage(); return 0; }
if (strcmp(a, "-V") == 0 || strcmp(a, "--version") == 0) {
printf("pstree (Windows port) v1.0\n"); return 0;
}
if (a[0] == '-') {
for (int j = 1; a[j]; j++) {
switch(a[j]) {
case 'p': show_pids = true; compact = false; break;
case 'n': sort_by_pid = true; break;
case 'A': use_ascii = true; break;
case 'U': use_ascii = false; break;
case 'u': show_uid = true; break;
case 'h': highlight_pid = GetCurrentProcessId(); break;
case 'H':
if (a[j+1]) { highlight_pid = (DWORD)atoi(a+j+1); j=(int)strlen(a)-1; }
else if (i+1 < argc) highlight_pid = (DWORD)atoi(argv[++i]);
break;
case 'c': compact = false; break;
case 'a': case 'l': case 'T':
case 's': case 'S': case 'Z': case 'N':
case 'C': case 'G': case 'g': case 't':
break; // accepted, ignored
}
}
} else {
if (isdigit((unsigned char)a[0])) root_pid = (DWORD)atoi(a);
else root_user = a;
}
}
// Collect processes
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap == INVALID_HANDLE_VALUE) {
fprintf(stderr, "pstree: cannot list processes\n"); return 1;
}
std::map<DWORD, Proc> procs;
PROCESSENTRY32 pe = {};
pe.dwSize = sizeof(pe);
if (Process32First(hSnap, &pe)) {
do {
Proc p;
p.pid = pe.th32ProcessID;
p.ppid = pe.th32ParentProcessID;
p.name = pe.szExeFile;
// Remove .exe
if (p.name.size() > 4 &&
_stricmp(p.name.c_str() + p.name.size() - 4, ".exe") == 0)
p.name = p.name.substr(0, p.name.size() - 4);
if (show_uid) p.user = get_user(p.pid);
procs[p.pid] = p;
} while (Process32Next(hSnap, &pe));
}
CloseHandle(hSnap);
// Build children map
std::map<DWORD, std::vector<DWORD>> children;
std::set<DWORD> has_parent;
for (auto& kv : procs) {
DWORD pid = kv.first;
DWORD ppid = kv.second.ppid;
if (ppid != pid && procs.count(ppid)) {
children[ppid].push_back(pid);
has_parent.insert(pid);
}
}
// Sort children lists
for (auto& kv : children) {
auto& vec = kv.second;
if (sort_by_pid)
std::sort(vec.begin(), vec.end());
else
std::sort(vec.begin(), vec.end(), [&](DWORD a, DWORD b) {
return procs[a].name < procs[b].name;
});
}
// Line drawing characters
const char* branch_mid = use_ascii ? "|-" : "\xe2\x94\x9c\xe2\x94\x80";
const char* branch_end = use_ascii ? "`-" : "\xe2\x94\x94\xe2\x94\x80";
const char* vert_bar = use_ascii ? "|" : "\xe2\x94\x82";
// VT processing already enabled at startup
// Recursive tree printer
// own_prefix: prefix printed on the same line as this node's label
// child_prefix: prefix for this node's children's lines
std::function<void(DWORD, const std::string&, const std::string&)> ptree =
[&](DWORD pid, const std::string& own_prefix, const std::string& child_prefix) {
auto it = procs.find(pid);
if (it == procs.end()) return;
const Proc& p = it->second;
std::string label = p.name;
if (show_uid && !p.user.empty())
label += "(" + p.user + ")";
if (show_pids)
label += "(" + std::to_string(p.pid) + ")";
bool do_hl = (highlight_pid != 0 && pid == highlight_pid);
if (do_hl) printf("\033[1;31m");
printf("%s%s\n", own_prefix.c_str(), label.c_str());
if (do_hl) printf("\033[0m");
auto cit = children.find(pid);
if (cit == children.end()) return;
const auto& kids = cit->second;
// Compact duplicate leaf names: group consecutive same-name leafs
// as "3*[name]" like Linux pstree does (when -c not specified)
// We'll process kids with compaction
std::vector<std::pair<std::string,std::vector<DWORD>>> groups; // (name, pids)
for (DWORD kid : kids) {
auto kit = procs.find(kid);
if (kit == procs.end()) continue;
const std::string& kname = kit->second.name;
bool is_leaf = (children.find(kid) == children.end() || children.at(kid).empty());
// Only compact if: leaf node, compact mode on, and no pid/uid display
bool do_compact = is_leaf && compact && !show_pids && !show_uid;
if (do_compact) {
if (!groups.empty() && groups.back().first == kname)
groups.back().second.push_back(kid);
else
groups.push_back({kname, {kid}});
} else {
groups.push_back({kname, {kid}});
}
}
size_t ng = groups.size();
for (size_t gi = 0; gi < ng; gi++) {
bool last = (gi == ng - 1);
std::string kid_own = child_prefix + (last ? branch_end : branch_mid);
std::string kid_child = child_prefix + (last ? " " : (std::string(vert_bar) + " "));
auto& grp = groups[gi];
if (grp.second.size() > 1) {
// Print as N*[name]
bool do_hl2 = false;
for (DWORD kpid : grp.second)
if (highlight_pid && kpid == highlight_pid) do_hl2 = true;
if (do_hl2) printf("\033[1;31m");
printf("%s%zu*[%s]\n", kid_own.c_str(), grp.second.size(), grp.first.c_str());
if (do_hl2) printf("\033[0m");
} else {
ptree(grp.second[0], kid_own, kid_child);
}
}
};
// Determine root nodes
std::vector<DWORD> roots;
if (root_pid != 0) {
roots.push_back(root_pid);
} else if (!root_user.empty()) {
for (auto& kv : procs)
if (_stricmp(kv.second.user.c_str(), root_user.c_str()) == 0 &&
!has_parent.count(kv.first))
roots.push_back(kv.first);
} else {
for (auto& kv : procs)
if (!has_parent.count(kv.first))
roots.push_back(kv.first);
}
if (sort_by_pid)
std::sort(roots.begin(), roots.end());
else
std::sort(roots.begin(), roots.end(), [&](DWORD a, DWORD b) {
return procs[a].name < procs[b].name;
});
for (DWORD r : roots)
ptree(r, "", "");
return 0;
}