Example #1
0
int main(int argc, char ** argv)
{
    --argc;
    ++argv;

    if (argc == 0) {
        printHelp();
        return 0;
    }

    int waitpid = -1;
    PresentMonArgs args;
    std::string title_string = "PresentMon";

    args.mTargetProcessName = "*";

    for (int i = 0; i < argc; ++i)
    {
        // 2-component arguments
        if (i + 1 < argc)
        {
            if (!strcmp(argv[i], "-waitpid"))
            {
                waitpid = atoi(argv[++i]);
                continue;
            }
            else if (!strcmp(argv[i], "-process_name"))
            {
                args.mTargetProcessName = argv[++i];
            }
            else if (!strcmp(argv[i], "-process_id"))
            {
                args.mTargetPid = atoi(argv[++i]);
            }
            else if (!strcmp(argv[i], "-output_file"))
            {
                args.mOutputFileName = argv[++i];
            }
            else if (!strcmp(argv[i], "-etl_file"))
            {
                args.mEtlFileName = argv[++i];
            }
            else if (!strcmp(argv[i], "-delay"))
            {
                args.mDelay = atoi(argv[++i]);
            }
            else if (!strcmp(argv[i], "-timed"))
            {
                args.mTimer = atoi(argv[++i]);
            }
        }
        // 1-component args
        {
            if (!strcmp(argv[i], "-no_csv"))
            {
                args.mOutputFileName = "*";
            }
            else if (!strcmp(argv[i], "-exclude_dropped"))
            {
                args.mExcludeDropped = true;
            }
            else if (!strcmp(argv[i], "-scroll_toggle"))
            {
                args.mScrollLockToggle = true;
            }
            else if (!strcmp(argv[i], "-?") || !strcmp(argv[i], "-help"))
            {
                printHelp();
                return 0;
            }
        }

        title_string += ' ';
        title_string += argv[i];
    }

    if (waitpid >= 0) {
        WaitForProcess(waitpid);
        if (!HaveAdministratorPrivileges()) {
            printf("Elevation process failed. Aborting.\n");
            return 0;
        }
    }

    if (!args.mEtlFileName && !HaveAdministratorPrivileges()) {
        printf("Process is not running as admin. Attempting to elevate.\n");
        RestartAsAdministrator(argc, argv);
        return 0;
    }

    SetConsoleCtrlHandler(HandlerRoutine, TRUE);
    SetConsoleTitleA(title_string.c_str());

    std::mutex exit_mutex;
    g_ExitMutex = &exit_mutex;

    // Run PM in a separate thread so we can join it in the CtrlHandler (can't join the main thread)
    std::thread pm(PresentMonEtw, args);
    g_PresentMonThread = &pm;
    while (!g_Quit)
    {
        Sleep(100);
    }

    // Wait for tracing to finish, to ensure the PM thread closes the session correctly
    // Prevent races on joining the PM thread between the control handler and the main thread
    std::lock_guard<std::mutex> lock(exit_mutex);
    if (g_PresentMonThread->joinable()) {
        g_PresentMonThread->join();
    }
    return 0;
}
Example #2
0
int main(int argc, char ** argv)
{
    --argc;
    ++argv;

    if (argc == 0) {
        printf(
            "command line options:\n"
            " -captureall: record ALL processes (default).\n"
            " -process_name [exe name]: record specific process.\n"
            " -process_id [integer]: record specific process ID.\n"
            " -output_file [path]: override the default output path.\n"
            " -no_csv: do not create any output file.\n"
            );
        return 0;
    }

    int waitpid = -1;
    PresentMonArgs args;

    args.mTargetProcessName = "*";

    for (int i = 0; i < argc; ++i)
    {
        // 2-component arguments
        if (i + 1 < argc)
        {
            if (!strcmp(argv[i], "-waitpid"))
            {
                waitpid = atoi(argv[++i]);
            }
            else if (!strcmp(argv[i], "-process_name"))
            {
                args.mTargetProcessName = argv[++i];
            }
            else if (!strcmp(argv[i], "-process_id"))
            {
                args.mTargetPid = atoi(argv[++i]);
            }
            else if (!strcmp(argv[i], "-output_file"))
            {
                args.mOutputFileName = argv[++i];
            }
        }
        // 1-component args
        else
        {
            if (!strcmp(argv[i], "-no_csv"))
            {
                args.mOutputFileName = "*";
            }
        }
    }

    if (waitpid >= 0) {
        WaitForProcess(waitpid);
        if (!HaveAdministratorPrivilidges()) {
            printf("Elevation process failed. Aborting.\n");
            return 0;
        }
    }

    if (!HaveAdministratorPrivilidges()) {
        printf("Process is not running as admin. Attempting to elevate.\n");
        RestartAsAdministrator(argc, argv);
        return 0;
    }

    SetConsoleCtrlHandler(HandlerRoutine, TRUE);

    // Run PM in a separate thread so we can join it in the CtrlHandler (can't join the main thread)
    std::thread pm(PresentMonEtw, args);
    g_PresentMonThread = &pm;
    Sleep(INFINITE);
}