-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·102 lines (83 loc) · 2.99 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·102 lines (83 loc) · 2.99 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
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
std::string GetSectionType(const std::string& details, const std::string& perms)
{
if(details == "[heap]") return "HEAP";
if(details == "[stack]") return "STACK";
if(details == "[vdso]") return "VDSO";
if(details == "[vvar]") return "VVAR";
if(perms[2] == 'x') return "TEXT";
if(perms[1] == 'w') return "DATA";
return "OTHER";
}
void ShowMemoryLayout(int pid)
{
std::ifstream fp("/proc/" + std::to_string(pid) + "/maps");
if(!fp)
{
std::cout << "Unable to open file\n";
return;
}
std::cout << "--------------------------------------------------------\n";
std::cout << "-------------------- Memory Layout ---------------------\n";
std::cout << "--------------------------------------------------------\n";
std::cout << "----- Header -----\n";
std::string line;
while(std::getline(fp,line))
{
std::istringstream iss(line);
std::string address, perms, offset, dev, details;
unsigned long inode = 0;
iss >> address >> perms >> offset >> dev >> inode >> details;
std::istringstream addrStream(address);
unsigned long start = 0, end = 0;
addrStream >> std::hex >> start;
addrStream.ignore(); // skip '-'
addrStream >> std::hex >> end;
unsigned long sizekb = (end - start) / 1024;
std::string section = GetSectionType(details,perms);
if(details.empty())
{
details = "Anonymous";
}
std::cout << std::hex << start << " " << end << std::dec
<< " " << sizekb << " " << perms << " " << section << " " << details << "\n";
}
std::cout << "--------------------------------------------------------\n";
}
void ShowProcessInformation(int pid)
{
std::ifstream fp("/proc/" + std::to_string(pid) + "/status");
if(!fp)
{
std::cout << "Unable to open file\n";
return;
}
std::cout << "--------------------------------------------------------\n";
std::cout << "------------------ Process Information -----------------\n";
std::cout << "--------------------------------------------------------\n";
std::string line;
while(std::getline(fp,line))
{
if(line.rfind("Name:",0) == 0 || line.rfind("Pid:",0) == 0 ||
line.rfind("State:",0) == 0 || line.rfind("Threads:",0) == 0)
{
std::cout << line << "\n";
}
}
std::cout << "--------------------------------------------------------\n";
}
int main()
{
std::cout << "-----------------------------------------------------------\n";
std::cout << "--------------- Kundan Process Inspector --------------\n";
std::cout << "-----------------------------------------------------------\n";
std::cout << "Enter PID of a process that you want to inspect\n";
int pid = 0;
std::cin >> pid;
ShowProcessInformation(pid);
ShowMemoryLayout(pid);
return 0;
}