6#include <unordered_map>
11#define PANO_IMAGE_KEEP_MS 60000
15#define PANO_IMAGES_PER_PID 2
19#define PANO_IMAGE_TIME_SLACK (10000000ULL)
27static std::mutex g_Lock;
28static std::unordered_map<DWORD, std::vector<ProcessImageEntry>> g_Images;
30static std::deque<std::pair<ULONGLONG, DWORD>> g_Exits;
32static ULONGLONG ToNumber(
const FILETIME& time)
34 return ((ULONGLONG)time.dwHighDateTime << 32) | time.dwLowDateTime;
37static bool StartsWithIgnoreCase(
const std::string& text,
const char* prefix)
39 size_t length = strlen(prefix);
40 return text.size() >= length && _strnicmp(text.c_str(), prefix, length) == 0;
44static std::string ToWin32Path(
const std::string& image)
46 if (StartsWithIgnoreCase(image,
"\\??\\")) {
47 return image.substr(4);
49 if (StartsWithIgnoreCase(image,
"\\SystemRoot\\")) {
51 UINT length = GetWindowsDirectoryW(windows,
MAX_PATH);
52 if (length > 0 && length <
MAX_PATH) {
53 return ToString(std::wstring(windows, length)) + image.substr(strlen(
"\\SystemRoot"));
60static void Prune(ULONGLONG now)
63 DWORD processId = g_Exits.front().second;
66 auto found = g_Images.find(processId);
67 if (found == g_Images.end()) {
70 auto& entries = found->second;
71 entries.erase(std::remove_if(entries.begin(), entries.end(), [now](
const ProcessImageEntry& entry) {
72 return entry.ExitTick != 0 && now - entry.ExitTick >= PANO_IMAGE_KEEP_MS;
74 if (entries.empty()) {
75 g_Images.erase(found);
82 std::lock_guard<std::mutex> guard(g_Lock);
83 Prune(GetTickCount64());
85 auto& entries = g_Images[processId];
87 entry.
Image = ToWin32Path(image);
88 entry.
Start = ToNumber(time);
89 entries.push_back(std::move(entry));
91 entries.erase(entries.begin());
97 ULONGLONG now = GetTickCount64();
98 std::lock_guard<std::mutex> guard(g_Lock);
101 auto found = g_Images.find(processId);
102 if (found == g_Images.end()) {
106 auto& newest = found->second.back();
107 if (newest.ExitTick == 0) {
108 newest.ExitTick = now;
109 g_Exits.emplace_back(now, processId);
115 ULONGLONG at = ToNumber(time);
116 std::lock_guard<std::mutex> guard(g_Lock);
117 auto found = g_Images.find(processId);
118 if (found == g_Images.end()) {
122 const auto& entries = found->second;
123 for (
auto entry = entries.rbegin(); entry != entries.rend(); ++entry) {
133#define PANO_SYSTEM_PROCESS_ID 4
137#define PANO_SYSTEM_PROCESS_ID_INFORMATION_CLASS 0x58
151static std::string KernelProcessImage(DWORD processId)
158 GetModuleHandleW(L
"ntdll.dll"),
"NtQuerySystemInformation");
159 if (query ==
nullptr) {
164 info.
ProcessId = (HANDLE)(ULONG_PTR)processId;
176 if (!image.empty()) {
180 HANDLE process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processId);
181 if (process != NULL) {
183 DWORD length = ARRAYSIZE(path);
184 if (QueryFullProcessImageNameW(process, 0, path, &length)) {
185 image =
ToString(std::wstring(path, length));
187 CloseHandle(process);
190 image = KernelProcessImage(processId);
void RecordProcessExit(DWORD processId)
Records a process exit (ProcessTerminated). Its entry is forgotten a minute later.
struct _PANO_SYSTEM_PROCESS_ID_INFORMATION PANO_SYSTEM_PROCESS_ID_INFORMATION
LONG(NTAPI * PanoNtQuerySystemInformation)(ULONG, PVOID, ULONG, PULONG)
#define PANO_SYSTEM_PROCESS_ID
void RecordProcessStart(DWORD processId, const std::string &image, const FILETIME &time)
Records a process start (ProcessCreated).
#define PANO_IMAGE_KEEP_MS
#define PANO_SYSTEM_PROCESS_ID_INFORMATION_CLASS
#define PANO_IMAGES_PER_PID
std::string LookupProcessImage(DWORD processId, const FILETIME &time)
The image of the process that had processId at the given time, from the recorded starts....
#define PANO_IMAGE_TIME_SLACK
std::string ResolveProcessImage(DWORD processId, const FILETIME &time)
LookupProcessImage, falling back to asking Windows (QueryFullProcessImageName) for processes the serv...
std::string ToString(const std::wstring &wstr)