Skip to content

Commit 96b6d78

Browse files
Move ElevatePrivilege back into MainThread.cpp
1 parent ccf042b commit 96b6d78

File tree

5 files changed

+28
-45
lines changed

5 files changed

+28
-45
lines changed

PresentMon/CommandLine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ static void PrintHelp()
321321
"-dont_restart_as_admin", "Don't try to elevate privilege. Elevated privilege isn't required"
322322
" to trace a process you started, but PresentMon requires elevated"
323323
" privilege in order to query processes started on another account."
324-
" Without it, these processes cannot be targetted by name and will be"
324+
" Without it, these processes cannot be targeted by name and will be"
325325
" listed as '<error>'.",
326326
"-terminate_on_proc_exit", "Terminate PresentMon when all the target processes have exited.",
327327
"-terminate_after_timed", "When using -timed, terminate PresentMon after the timed capture completes.",

PresentMon/MainThread.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,26 @@ int main(int argc, char** argv)
217217

218218
// Attempt to elevate process privilege if necessary.
219219
//
220-
// If a new process needs to be started, this will wait for the elevated
221-
// process to complete in order to report stderr and exit code, and then
222-
// abort from within ElevatePrivilege() (i.e., the rest of this function
223-
// won't run in this process).
224-
ElevatePrivilege(argc, argv);
220+
// If we are processing an ETL file we don't need elevated privilege, but
221+
// for realtime analysis we need SeDebugPrivilege in order to open handles
222+
// to processes started by other accounts (see OutputThread.cpp).
223+
//
224+
// If we can't enable SeDebugPrivilege, try to restart PresentMon as
225+
// administrator unless the user requested not to.
226+
//
227+
// RestartAsAdministrator() waits for the elevated process to complete in
228+
// order to report stderr and obtain it's exit code.
229+
if (args.mEtlFileName == nullptr && // realtime analysis
230+
!EnableDebugPrivilege()) { // failed to enable SeDebugPrivilege
231+
if (args.mTryToElevate) {
232+
return RestartAsAdministrator(argc, argv);
233+
}
234+
235+
fprintf(stderr,
236+
"warning: PresentMon requires elevated privilege in order to query processes started\n"
237+
" on another account. Without it, those processes will be listed as '<error>'\n"
238+
" and they can't be targeted by -process_name nor trigger -terminate_on_proc_exit.\n");
239+
}
225240

226241
// Create a message queue to handle the input messages.
227242
WNDCLASSEXW wndClass = { sizeof(wndClass) };

PresentMon/PresentMon.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ void StopOutputThread();
152152
void SetOutputRecordingState(bool record);
153153

154154
// Privilege.cpp:
155-
void ElevatePrivilege(int argc, char** argv);
155+
bool EnableDebugPrivilege();
156+
int RestartAsAdministrator(int argc, char** argv);
156157

157158
// TraceSession.cpp:
158159
bool StartTraceSession();

PresentMon/Privilege.cpp

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1919
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
SOFTWARE.
2121
*/
22-
23-
#include "PresentMon.hpp"
24-
25-
namespace {
22+
#define WIN32_LEAN_AND_MEAN
23+
#include <windows.h>
24+
#include <shellapi.h>
25+
#include <stdio.h>
2626

2727
bool EnableDebugPrivilege()
2828
{
@@ -142,36 +142,3 @@ int RestartAsAdministrator(
142142
return code;
143143
}
144144

145-
}
146-
147-
// Returning from this function means keep running in this process.
148-
void ElevatePrivilege(int argc, char** argv)
149-
{
150-
auto const& args = GetCommandLineArgs();
151-
152-
// If we are processing an ETL file, then we don't need elevated privilege
153-
if (args.mEtlFileName != nullptr) {
154-
return;
155-
}
156-
157-
// Try to load advapi to check and set required privilege.
158-
if (EnableDebugPrivilege()) {
159-
return;
160-
}
161-
162-
// If user requested to run anyway, warn about potential issues.
163-
if (!args.mTryToElevate) {
164-
fprintf(stderr,
165-
"warning: PresentMon requires elevated privilege in order to query processes\n"
166-
" started on another account. Without elevation, these processes can't be\n"
167-
" targetted by name and will be listed as '<error>'.\n");
168-
if (args.mTerminateOnProcExit && args.mTargetPid == 0) {
169-
fprintf(stderr, " -terminate_on_proc_exit will also not work.\n");
170-
}
171-
return;
172-
}
173-
174-
// Try to restart PresentMon with admin privileve
175-
exit(RestartAsAdministrator(argc, argv));
176-
}
177-

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Execution options:
9393
isn't required to trace a process you started, but
9494
PresentMon requires elevated privilege in order to
9595
query processes started on another account. Without
96-
it, these processes cannot be targetted by name and
96+
it, these processes cannot be targeted by name and
9797
will be listed as '<error>'.
9898
-terminate_on_proc_exit Terminate PresentMon when all the target processes
9999
have exited.

0 commit comments

Comments
 (0)