Beispiel #1
0
int
main (int argc, char **argv)
{
    int opt;
    bool dry_run = false;
    bool disable_ioctl = false;

#define LIB_GL_OPT              (CHAR_MAX + 1)
#define LIB_EGL_OPT             (CHAR_MAX + 2)
#define DEBUG_CTX_OPT           (CHAR_MAX + 3)
#define NCURSES_OPT             (CHAR_MAX + 4)
#define DRY_RUN_OPT             (CHAR_MAX + 5)
#define DISABLE_IOCTL_OPT       (CHAR_MAX + 6)
#define FAKE_OPT                (CHAR_MAX + 7)
#define GPUTOP_SCISSOR_TEST     (CHAR_MAX + 8)
#define PORT_OPT                (CHAR_MAX + 9)

    /* The initial '+' means that getopt will stop looking for
     * options after the first non-option argument. */
    const char *short_options="+h";
    const struct option long_options[] = {
        {"help",            no_argument,        0, 'h'},
        {"dry-run",         no_argument,        0, DRY_RUN_OPT},
        {"fake",            no_argument,        0, FAKE_OPT},
        {"disable-ioctl-intercept", optional_argument,  0, DISABLE_IOCTL_OPT},
#ifdef SUPPORT_GL
        {"libgl",                   optional_argument,  0, LIB_GL_OPT},
        {"libegl",                  optional_argument,  0, LIB_EGL_OPT},
        {"debug-gl-context",        no_argument,        0, DEBUG_CTX_OPT},
        {"enable-gl-scissor-test",  optional_argument,  0, GPUTOP_SCISSOR_TEST},
#endif
        {"ncurses",         no_argument,        0, NCURSES_OPT},
        {"port",            required_argument,  0, PORT_OPT},
        {0, 0, 0, 0}
    };
    char *ld_preload_path;
    char *gputop_system_args[] = {
        get_gputop_system_path(),
        NULL
    };
    char **args = argv;
    int err;
    int i;


    while ((opt = getopt_long(argc, argv, short_options, long_options, NULL))
           != -1)
    {
        switch (opt) {
            case 'h':
                usage();
                return 0;
            case DRY_RUN_OPT:
                dry_run = true;
                break;
            case FAKE_OPT:
                setenv("GPUTOP_FAKE_MODE", "1", true);
                break;
            case DISABLE_IOCTL_OPT:
                disable_ioctl = true;
                break;
#ifdef SUPPORT_GL
            case LIB_GL_OPT:
                setenv("GPUTOP_GL_LIBRARY", optarg, true);
                break;
            case LIB_EGL_OPT:
                setenv("GPUTOP_EGL_LIBRARY", optarg, true);
                break;
            case DEBUG_CTX_OPT:
                setenv("GPUTOP_GL_DEBUG_CONTEXT", "1", true);
                break;
            case GPUTOP_SCISSOR_TEST:
                setenv("GPUTOP_GL_SCISSOR_TEST", "1", true);
                break;
#endif
            case NCURSES_OPT:
                setenv("GPUTOP_MODE", "ncurses", true);
                break;
            case PORT_OPT:
                setenv("GPUTOP_PORT", optarg, true);
                break;
            default:
                fprintf(stderr, "Internal error: "
                        "unexpected getopt value: %d\n", opt);
                exit (1);
        }
    }

    if (optind >= argc) {
        /* If no program is given then launch a dummy "test
         * application" so gputop can also be used for analysing
         * system wide metrics. */
        args = gputop_system_args;
        optind = 0;
        argc = 1;
    }

    if (!disable_ioctl)
        env_resolve_append_lib_path("LD_PRELOAD", "libgputop.so");

#ifdef SUPPORT_GL
    env_resolve_append_lib_path("LD_PRELOAD", "libfakeGL.so");

    if (!getenv("GPUTOP_GL_LIBRARY")) {
        bool found = resolve_lib_path_for_env("libGL.so.1", "glClear", "GPUTOP_GL_LIBRARY");
        if (!found) {
            fprintf(stderr, "Could not resolve a path for the system libGL.so library\n");
            exit(0);
        }
    }

    if (!getenv("GPUTOP_EGL_LIBRARY"))
        resolve_lib_path_for_env("libEGL.so.1", "eglGetDisplay", "GPUTOP_EGL_LIBRARY");
#endif

    setup_web_root_env();

    /*
     * Print out the environment that we are setting up...
     */

    if (dry_run)
        fprintf(stderr, "\nWould run:\n\n");
    else
        fprintf(stderr, "Running:\n\n");

    ld_preload_path = getenv("LD_PRELOAD");
    if (ld_preload_path)
        fprintf(stderr, "LD_PRELOAD=%s \\\n", ld_preload_path);

    print_gputop_env_vars();

    for (i = optind; i < argc; i++)
        fprintf(stderr, "%s ", args[i]);
    fprintf(stderr, "\n\n");

    fprintf(stderr, "The LD_PRELOAD makes running gdb awkward. Either attach with\n");
    fprintf(stderr, "gdb --pid=`pidof %s` or run like:\n\n", args[optind]);

    print_gputop_env_vars();
    if (ld_preload_path)
        fprintf(stderr, "gdb -ex \"set exec-wrapper env 'LD_PRELOAD=%s'\" --args \\\n", ld_preload_path);
    else
        fprintf(stderr, "gdb --args \\\n");

    for (i = optind; i < argc; i++)
        fprintf(stderr, "%s ", args[i]);
    fprintf(stderr, "\n\n");


    fprintf(stderr, "Valgrind can be run like:\n\n");

    if (ld_preload_path)
        fprintf(stderr, "LD_PRELOAD=%s \\\n", ld_preload_path);
    print_gputop_env_vars();
    fprintf(stderr, "valgrind --log-file=valgrind-log.txt \\\n");

    for (i = optind; i < argc; i++)
        fprintf(stderr, "%s ", args[i]);
    fprintf(stderr, "\n\n");


    if (!dry_run)
    {
        execvp(args[optind], &args[optind]);
        err = errno;

        fprintf(stderr, "gputop: Failed to run GL application: \n\n"
               "  ");
        for (i = optind; i < argc; i++)
               fprintf(stderr, "%s ", args[i]);
        fprintf(stderr, "\n\n%s\n", strerror(err));
    }

    return 0;
}
Beispiel #2
0
int
main (int argc, char **argv)
{
    int opt;
    bool dry_run = false;
    bool disable_ioctl = false;

#define LIB_GL_OPT		(CHAR_MAX + 1)
#define LIB_EGL_OPT		(CHAR_MAX + 2)
#define DEBUG_CTX_OPT		(CHAR_MAX + 3)
#define REMOTE_OPT		(CHAR_MAX + 4)
#define DRY_RUN_OPT		(CHAR_MAX + 5)
#define DISABLE_IOCTL_OPT	(CHAR_MAX + 6)
#define FAKE_OPT	        (CHAR_MAX + 7)

    /* The initial '+' means that getopt will stop looking for
     * options after the first non-option argument. */
    const char *short_options="+h";
    const struct option long_options[] = {
        {"help",	    no_argument,	0, 'h'},
        {"dry-run",         no_argument,        0, DRY_RUN_OPT},
        {"fake",            no_argument,        0, FAKE_OPT},
#ifdef SUPPORT_GL
        {"libgl",	    optional_argument,	0, LIB_GL_OPT},
        {"libegl",	    optional_argument,	0, LIB_EGL_OPT},
        {"disable-ioctl-intercept",   optional_argument,	0, DISABLE_IOCTL_OPT},
        {"debug-context",   no_argument,	0, DEBUG_CTX_OPT},
#endif
#ifdef SUPPORT_WEBUI
        {"remote",	    no_argument,	0, REMOTE_OPT},
#endif
        {0, 0, 0, 0}
    };
    char *ld_preload_path;
    char *ld_preload_path_ioctl = "";
    char *prev_ld_preload_path;
    char *gputop_system_args[] = {
        "gputop-system",
        NULL
    };
    char **args = argv;
    int err;
    int i;


    while ((opt = getopt_long(argc, argv, short_options, long_options, NULL))
            != -1)
    {
        switch (opt) {
        case 'h':
            usage();
            return 0;
        case LIB_GL_OPT:
            setenv("GPUTOP_GL_LIBRARY", optarg, true);
            break;
        case LIB_EGL_OPT:
            setenv("GPUTOP_EGL_LIBRARY", optarg, true);
            break;
        case DEBUG_CTX_OPT:
            setenv("GPUTOP_FORCE_DEBUG_CONTEXT", "1", true);
            break;
        case REMOTE_OPT:
            setenv("GPUTOP_MODE", "remote", true);
            break;
        case DRY_RUN_OPT:
            dry_run = true;
            break;
        case DISABLE_IOCTL_OPT:
            disable_ioctl = true;
            break;
        case FAKE_OPT:
            setenv("GPUTOP_FAKE_MODE", "1", true);
            break;
        default:
            fprintf (stderr, "Internal error: "
                     "unexpected getopt value: %d\n", opt);
            exit (1);
        }
    }

    if (optind >= argc) {
        /* If no program is given then launch a dummy "test
         * application" so gputop can also be used for analysing
         * system wide metrics. */
        args = gputop_system_args;
        optind = 0;
    }

    prev_ld_preload_path = getenv("LD_PRELOAD");
    if (!prev_ld_preload_path)
        prev_ld_preload_path= "";

    if (!disable_ioctl)
        asprintf(&ld_preload_path_ioctl, "%s", "libgputop.so");

    asprintf(&ld_preload_path, "%s/libfakeGL.so:%s:%s", GPUTOP_WRAPPER_DIR,
             ld_preload_path_ioctl, prev_ld_preload_path);

    if (!getenv("GPUTOP_GL_LIBRARY"))
        resolve_lib_path_for_env("libGL.so.1", "glClear", "GPUTOP_GL_LIBRARY");

    if (!getenv("GPUTOP_EGL_LIBRARY"))
        resolve_lib_path_for_env("libEGL.so.1", "eglGetDisplay", "GPUTOP_EGL_LIBRARY");

    fprintf(stderr, "LD_PRELOAD=%s \\\n", ld_preload_path);
    if (getenv("GPUTOP_GL_LIBRARY"))
        fprintf(stderr, "GPUTOP_GL_LIBRARY=%s \\\n", getenv("GPUTOP_GL_LIBRARY"));

    if (getenv("GPUTOP_EGL_LIBRARY"))
        fprintf(stderr, "GPUTOP_EGL_LIBRARY=%s \\\n", getenv("GPUTOP_EGL_LIBRARY"));

    if (getenv("GPUTOP_FORCE_DEBUG_CONTEXT"))
        fprintf(stderr, "GPUTOP_FORCE_DEBUG_CONTEXT=%s \\\n", getenv("GPUTOP_FORCE_DEBUG_CONTEXT"));

    if (getenv("GPUTOP_MODE"))
        fprintf(stderr, "GPUTOP_MODE=%s \\\n", getenv("GPUTOP_MODE"));

    if (getenv("GPUTOP_FAKE_MODE"))
        fprintf(stderr, "GPUTOP_FAKE_MODE=%s \\\n", getenv("GPUTOP_FAKE_MODE"));

    fprintf(stderr, "%s\n", args[optind]);

    setenv("LD_PRELOAD", ld_preload_path, true);
    free(ld_preload_path);

    if (!dry_run)
    {
        execvp(args[optind], &args[optind]);
        err = errno;

        fprintf(stderr, "gputop: Failed to run GL application: \n\n"
                "  ");
        for (i = optind; i < argc; i++)
            fprintf(stderr, "%s ", args[i]);
        fprintf(stderr, "\n\n%s\n", strerror(err));
    }

    return 0;
}