0
0
mirror of https://github.com/yse/easy_profiler.git synced 2024-12-27 08:41:02 +08:00

(ETW) setDebugPrivelege() rename

This commit is contained in:
Victor Zarubkin 2016-12-11 03:31:22 +03:00
parent 0dc944a877
commit 5e4193cb13
2 changed files with 35 additions and 14 deletions

View File

@ -246,15 +246,14 @@ namespace profiler {
m_lowPriority.store(_value, ::std::memory_order_release); m_lowPriority.store(_value, ::std::memory_order_release);
} }
bool EasyEventTracer::setDebugPrivilege() bool setPrivilege(HANDLE hToken, LPCSTR _privelegeName)
{ {
bool success = false; bool success = false;
HANDLE hToken = nullptr; if (hToken)
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{ {
LUID privilegyId; LUID privilegyId;
if (LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &privilegyId)) if (LookupPrivilegeValue(NULL, _privelegeName, &privilegyId))
{ {
TOKEN_PRIVILEGES tokenPrivilege; TOKEN_PRIVILEGES tokenPrivilege;
tokenPrivilege.PrivilegeCount = 1; tokenPrivilege.PrivilegeCount = 1;
@ -262,18 +261,45 @@ namespace profiler {
tokenPrivilege.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; tokenPrivilege.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
success = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivilege, sizeof(TOKEN_PRIVILEGES), NULL, NULL) != FALSE; success = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivilege, sizeof(TOKEN_PRIVILEGES), NULL, NULL) != FALSE;
} }
CloseHandle(hToken);
} }
#if EASY_LOG_ENABLED != 0 #if EASY_LOG_ENABLED != 0
if (!success) if (!success)
::std::cerr << "Warning: EasyProfiler failed to set Debug privelege for the application. Some context switch events could not get process name.\n"; ::std::cerr << "Warning: EasyProfiler failed to set " << _privelegeName << " privelege for the application.\n";
#endif #endif
return success; return success;
} }
void EasyEventTracer::setProcessPrivileges()
{
static bool alreadySet = false;
if (alreadySet)
return;
alreadySet = true;
HANDLE hToken = nullptr;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
#if EASY_LOG_ENABLED != 0
const bool success = setPrivilege(hToken, SE_DEBUG_NAME);
if (!success)
::std::cerr << "Warning: Some context switch events could not get process name.\n";
#else
setPrivilege(hToken, SE_DEBUG_NAME);
#endif
CloseHandle(hToken);
}
#if EASY_LOG_ENABLED != 0
else
{
::std::cerr << "Warning: EasyProfiler failed to open process to adjust priveleges.\n";
}
#endif
}
::profiler::EventTracingEnableStatus EasyEventTracer::startTrace(bool _force, int _step) ::profiler::EventTracingEnableStatus EasyEventTracer::startTrace(bool _force, int _step)
{ {
auto startTraceResult = StartTrace(&m_sessionHandle, KERNEL_LOGGER_NAME, props()); auto startTraceResult = StartTrace(&m_sessionHandle, KERNEL_LOGGER_NAME, props());
@ -355,12 +381,8 @@ namespace profiler {
/* /*
Trying to set debug privilege for current process Trying to set debug privilege for current process
to be able to get other process information (process name). to be able to get other process information (process name).
Also it seems that debug privelege lets you to launch
event tracing without Administrator access rights.
*/ */
if (!m_bPrivilegeSet) EasyEventTracer::setProcessPrivileges();
m_bPrivilegeSet = setDebugPrivilege();
// Clear properties // Clear properties
memset(&m_properties, 0, sizeof(m_properties)); memset(&m_properties, 0, sizeof(m_properties));

View File

@ -85,7 +85,6 @@ namespace profiler {
TRACEHANDLE m_sessionHandle = INVALID_PROCESSTRACE_HANDLE; TRACEHANDLE m_sessionHandle = INVALID_PROCESSTRACE_HANDLE;
TRACEHANDLE m_openedHandle = INVALID_PROCESSTRACE_HANDLE; TRACEHANDLE m_openedHandle = INVALID_PROCESSTRACE_HANDLE;
bool m_bEnabled = false; bool m_bEnabled = false;
bool m_bPrivilegeSet = false;
public: public:
@ -97,6 +96,7 @@ namespace profiler {
::profiler::EventTracingEnableStatus enable(bool _force = false); ::profiler::EventTracingEnableStatus enable(bool _force = false);
void disable(); void disable();
void setLowPriority(bool _value); void setLowPriority(bool _value);
static void setProcessPrivileges();
private: private:
@ -108,7 +108,6 @@ namespace profiler {
} }
::profiler::EventTracingEnableStatus startTrace(bool _force, int _step = 0); ::profiler::EventTracingEnableStatus startTrace(bool _force, int _step = 0);
bool setDebugPrivilege();
}; // END of class EasyEventTracer. }; // END of class EasyEventTracer.