-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Define ETW profile as an INI file
- Loading branch information
1 parent
a78c740
commit 0b11af8
Showing
6 changed files
with
246 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
#include "etw_settings.h" | ||
|
||
#include <string> | ||
#include <vector> | ||
#include <sstream> | ||
#include <fstream> | ||
|
||
#define WATCH_PROCESS_START "WATCH_PROCESS_START" | ||
#define WATCH_IMG_LOAD "WATCH_IMG_LOAD" | ||
#define WATCH_ALLOCATION "WATCH_ALLOCATION" | ||
#define WATCH_TCP_IP "WATCH_TCP_IP" | ||
#define WATCH_OBJ_MGR "WATCH_OBJ_MGR" | ||
|
||
namespace util { | ||
|
||
static inline void ltrim(std::string& s) | ||
{ | ||
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { | ||
return !std::isspace(ch); | ||
})); | ||
} | ||
|
||
static inline void rtrim(std::string& s) | ||
{ | ||
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { | ||
return !std::isspace(ch); | ||
}).base(), s.end()); | ||
} | ||
|
||
void trim(std::string& s) | ||
{ | ||
ltrim(s); | ||
rtrim(s); | ||
} | ||
|
||
bool iequals(const std::string& a, const std::string& b) | ||
{ | ||
size_t aLen = a.size(); | ||
if (b.size() != aLen) return false; | ||
|
||
for (size_t i = 0; i < aLen; ++i) { | ||
if (tolower(a[i]) != tolower(b[i])) return false; | ||
} | ||
return true; | ||
} | ||
|
||
size_t splitList(const std::string& sline, const char delimiter, std::vector<std::string>& args) | ||
{ | ||
std::istringstream f(sline); | ||
std::string s; | ||
while (getline(f, s, delimiter)) { | ||
args.push_back(s); | ||
} | ||
return args.size(); | ||
} | ||
|
||
|
||
int loadInt(const std::string& str, bool as_hex=false) | ||
{ | ||
int intVal = 0; | ||
|
||
std::stringstream ss; | ||
ss << (as_hex ? std::hex : std::dec) << str; | ||
ss >> intVal; | ||
|
||
return intVal; | ||
} | ||
|
||
bool loadBoolean(const std::string& str, bool defaultVal) | ||
{ | ||
if (util::iequals(str, "True") || util::iequals(str, "on") || util::iequals(str, "yes")) { | ||
return true; | ||
} | ||
if (util::iequals(str, "False") || util::iequals(str, "off") || util::iequals(str, "no")) { | ||
return false; | ||
} | ||
const int val = loadInt(str); | ||
if (val == 0) return false; | ||
return true; | ||
} | ||
|
||
std::string booleanToStr(bool val) | ||
{ | ||
return (val) ? "True": "False"; | ||
} | ||
|
||
|
||
}; // util | ||
|
||
//--- | ||
const char ETWProfile::DELIM = '='; | ||
|
||
void ETWProfile::stripComments(std::string& str) | ||
{ | ||
size_t found = str.find_first_of(";#"); | ||
if (found != std::string::npos) { | ||
str.resize(found); | ||
} | ||
} | ||
|
||
bool ETWProfile::fillSettings(std::string line) | ||
{ | ||
using namespace util; | ||
|
||
std::vector<std::string> args; | ||
util::splitList(line, DELIM, args); | ||
|
||
if (args.size() < 2) { | ||
return false; | ||
} | ||
bool isFilled = false; | ||
std::string valName = args[0]; | ||
std::string valStr = args[1]; | ||
util::trim(valName); | ||
util::trim(valStr); | ||
|
||
if (util::iequals(valName, WATCH_PROCESS_START)) { | ||
this->process_start = loadBoolean(valStr, this->process_start); | ||
isFilled = true; | ||
} | ||
if (util::iequals(valName, WATCH_IMG_LOAD)) { | ||
this->img_load = loadBoolean(valStr, this->img_load); | ||
isFilled = true; | ||
} | ||
if (util::iequals(valName, WATCH_ALLOCATION)) { | ||
this->allocation = loadBoolean(valStr, this->allocation); | ||
isFilled = true; | ||
} | ||
if (util::iequals(valName, WATCH_TCP_IP)) { | ||
this->tcpip = loadBoolean(valStr, this->tcpip); | ||
isFilled = true; | ||
} | ||
if (util::iequals(valName, WATCH_OBJ_MGR)) { | ||
this->obj_mgr = loadBoolean(valStr, this->obj_mgr); | ||
isFilled = true; | ||
} | ||
return isFilled; | ||
} | ||
|
||
bool ETWProfile::loadIni(const std::string& filename) | ||
{ | ||
std::ifstream myfile(filename.c_str()); | ||
if (!myfile.is_open()) { | ||
return false; | ||
} | ||
const size_t MAX_LINE = 300; | ||
char line[MAX_LINE] = { 0 }; | ||
bool filledAny = false; | ||
|
||
while (!myfile.eof()) { | ||
myfile.getline(line, MAX_LINE); | ||
std::string lineStr = line; | ||
stripComments(lineStr); | ||
|
||
if (fillSettings(lineStr)) { | ||
filledAny = true; | ||
} | ||
} | ||
myfile.close(); | ||
return filledAny; | ||
} | ||
|
||
bool ETWProfile::saveIni(const std::string& filename) | ||
{ | ||
using namespace util; | ||
std::ofstream myfile(filename.c_str()); | ||
if (!myfile.is_open()) { | ||
return false; | ||
} | ||
myfile << WATCH_PROCESS_START << DELIM << booleanToStr(this->process_start) << "\n"; | ||
myfile << WATCH_IMG_LOAD << DELIM << booleanToStr(this->img_load) << "\n"; | ||
myfile << WATCH_ALLOCATION << DELIM << booleanToStr(this->allocation) << "\n"; | ||
myfile << WATCH_TCP_IP << DELIM << booleanToStr(this->tcpip) << "\n"; | ||
myfile << WATCH_OBJ_MGR << DELIM << booleanToStr(this->obj_mgr) << "\n"; | ||
myfile.close(); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#pragma once | ||
|
||
#include <iostream> | ||
|
||
struct ETWProfile { | ||
public: | ||
bool process_start; | ||
bool img_load; | ||
bool allocation; | ||
bool tcpip; | ||
bool obj_mgr; | ||
|
||
ETWProfile(bool _process_start = false, bool _img_load = false, bool _allocation = false, bool _tcpip = false, bool _obj_mgr = false) | ||
: process_start(_process_start), img_load(_img_load), allocation(_allocation), tcpip(_tcpip), obj_mgr(_obj_mgr) | ||
{ | ||
} | ||
|
||
bool initProfile(const std::string& fileName) | ||
{ | ||
bool isOk = loadIni(fileName); | ||
if (!isOk) { | ||
setAll(); | ||
isOk = saveIni(fileName); | ||
} | ||
return isOk; | ||
} | ||
|
||
bool loadIni(const std::string& fileName); | ||
bool saveIni(const std::string& fileName); | ||
|
||
void setAll() | ||
{ | ||
this->process_start = true; | ||
this->img_load = true; | ||
this->allocation = true; | ||
this->tcpip = true; | ||
this->obj_mgr = true; | ||
} | ||
|
||
bool isEnabled() | ||
{ | ||
if (this->process_start | ||
|| this->img_load | ||
|| this->allocation | ||
|| this->tcpip | ||
|| this->obj_mgr | ||
) | ||
{ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
protected: | ||
static const char DELIM; | ||
|
||
bool fillSettings(std::string line); | ||
void stripComments(std::string& str); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters