Ejemplo n.º 1
0
int main(int argc, char **argv)
{
    char **newargs;

    /* form our new arguments */
    newargs = alloca((argc + 2) * sizeof(char*));
    memcpy(newargs + 1, argv, (argc + 1) * sizeof(char*));
    newargs[0] = PREFIX "/bin/gelfload";

    /* make the program name absolute */
    if (argv[0][0] != '/') {
        char *dir, *fil;
        if (whereAmI(argv[0], &dir, &fil)) {
            char *newargv1 = alloca(strlen(dir) + strlen(fil) + 2);
            sprintf(newargv1, "%s/%s", dir, fil);
            newargs[1] = newargv1;
        }
    }

    /* then call */
    execv(newargs[0], newargs);
    fprintf(stderr, "Failed to call %s\n", newargs[0]);
    return 0;
}
Ejemplo n.º 2
0
int main(int argc, char **argv, char **envp)
{
    struct ELF_File *f;
    void **newstack;
    int i, envc, progarg;
    char *dir, *fil;
    int maybe = 0;

    progarg = -1;
    for (i = 1; i < argc; i++) {
        if (argv[i][0] == '-') {
            if (!strcmp(argv[i], "-m")) {
                maybe = 1;
            } else if (!strcmp(argv[i], "--")) {
                progarg = i + 1;
                break;
            }
        } else {
            progarg = i;
            break;
        }
    }
    if (progarg == -1) {
        fprintf(stderr, "Use: elfload [-m] <elf file> [arguments]\n");
        return 1;
    }

    whereAmI(argv[0], &dir, &fil);
    elfload_dlinstdir = dir;

    /* load them all in */
    f = loadELF(argv[progarg], dir, maybe);

    if (!f) {
        /* try just execing it */
        execv(argv[progarg], argv + progarg);
        fprintf(stderr, "Failed to load %s.\n", argv[progarg]);
        return 1;
    }

    /* relocate them */
    relocateELFs();

    /* initialize .so files */
    initELF(f);

    /* make its stack */
    for (envc = 0; envp[envc]; envc++);
    newstack = (void**)
        alloca((argc + envc + 2) * sizeof(void*));
    newstack[0] = (void*) (size_t) (argc - progarg);
    for (i = progarg; i < argc; i++) {
        newstack[i - progarg + 1] = (void*) argv[i];
    }
    newstack[i - progarg + 1] = NULL;

    for (i = 0; i < envc; i++) {
        newstack[i-progarg+argc+2] = (void*) envp[i];
    }
    newstack[i-progarg+argc+2] = NULL;

    /* and call it */
    WITHSTACK_JMP(newstack, f->ehdr->e_entry + f->offset);
}
Ejemplo n.º 3
0
Archivo: psli.c Proyecto: GregorR/plof3
int main(int argc, char **argv)
{
    FILE *pslf;
    unsigned char *psl;
    size_t len, rd, i;
    size_t slen, stype, stl;
    struct PlofObject *context;
    struct PlofReturn ret;
    char *wdir, *wfil;

    GC_INIT();

    if (argc != 2) {
        fprintf(stderr, "Use: psli <file>\n");
        return 1;
    }

    /* get our search path */
    if (whereAmI(argv[0], &wdir, &wfil)) {
        plofIncludePaths = GC_MALLOC(3 * sizeof(unsigned char *));

        /* /../share/plof_include/ */
        plofIncludePaths[0] = GC_MALLOC_ATOMIC(strlen(wdir) + 24);
        sprintf((char *) plofIncludePaths[0], "%s/../share/plof_include/", wdir);

        /* /../../plof_include/ (for running from src/) */
        plofIncludePaths[1] = GC_MALLOC_ATOMIC(strlen(wdir) + 21);
        sprintf((char *) plofIncludePaths[1], "%s/../../plof_include/", wdir);

        plofIncludePaths[2] = NULL;

        /* FIXME: should support -I eventually */

    } else {
        plofIncludePaths[0] = NULL;

    }

    /* open the file */
    pslf = fopen(argv[1], "rb");
    if (pslf == NULL) {
        perror(argv[1]);
        return 1;
    }

    /* preallocate the buffer */
    psl = GC_MALLOC_ATOMIC(BUFSTEP);
    len = 0;

    /* now start reading */
    while ((rd = fread(psl + len, 1, BUFSTEP, pslf))) {
        len += rd;
        if (rd < BUFSTEP) break;

        /* allocate more */
        psl = GC_REALLOC(psl, len + BUFSTEP);
    }
    fclose(pslf);

    /* FIXME: bounds checking */

    /* Go section-by-section */
    for (i = 8; i < len;) {
        /* section length */
        i += pslBignumToInt(psl + i, (size_t *) &slen);

        /* section type */
        stl = pslBignumToInt(psl + i, (size_t *) &stype);

        /* if it's program data, we found it */
        if (stype == 0) {
            i += stl;
            break;

        } else {
            /* skip this section */
            i += slen;
        }
    }

    /* make sure we found it */
    if (i >= len) {
        fprintf(stderr, "No program data!\n");
        return 1;
    }

    /* Initialize null and global */
    plofNull = newPlofObject();
    plofNull->parent = plofNull;
    plofGlobal = newPlofObject();
    plofGlobal->parent = plofGlobal;

    /* And the context */
    context = newPlofObject();
    context->parent = plofNull;

    /* Now interp */
    interpretPSL(context, plofNull, NULL, slen - stl, psl + i, 0, 1);
    ret = interpretPSL(context, plofNull, NULL, slen - stl, psl + i, 0, 0);

    if (ret.isThrown) {
        fprintf(stderr, "PSL threw up!\n");
        return 1;
    }

    return 0;
}
void debugger(){
	int hasStarted = 0;
	int hasContinued = 0;
	while(1){
		//wait for a signal
		wait(&status);
		//if the program exited, just return
		if (WIFEXITED( status )){
			printf("\nthe program terminated\n");
			return;
	 	}

		// Quick overview of what's going on here:
		// **Variable 'done': Done will keep us within this while loop. If it is
		//   set to 1 that means we issued the "continue" command.
		//
		// **Variable 'hasContinued': In the event that we stop at a breakpoint
		//   and issue a command other than "continue", we will end up calling
		//   fixBreakpoint a second time. hasContinued will tell the loop
		//   to pump the breaks, we haven't moved on yet!
		int done = 0;
	 	while(!done){
	    		//get user input, what should we do?
			if((breakpointCount > 0) && hasStarted && hasContinued){
				printf("breakpoint %d hit\n",findBreakpoint(AFTER_BREAKPOINT));
				fixBreakpoint(0);	// Restore our opcode and registers
				hasContinued = 0;
			}

	    		user_command * cmd = debugger_interface_get_cmd();
	    		if (cmd){
				//now handle the input
				switch(cmd->command_type){
				case CMD_TYPE_BREAKPOINT:
					if(breakpoint(cmd->value.line_number)){
						printf("Could not set breakpoint at line %d.\n",cmd->value.line_number);
					}
					break;
				case CMD_TYPE_STACKTRACE:
					stacktrace();
					break;
				case CMD_TYPE_PRINT:
					printVariable(cmd->value.var.format,cmd->value.var.name,0);
					break;
				case CMD_TYPE_PRINT_GEN:
					printVariable(cmd->value.var.format,cmd->value.var.name,1);
					break;
				case CMD_TYPE_CONTINUE:
					if(!hasStarted){
						// If we are here, that means we are just starting to run the
						// program, so nothing special.
						hasStarted = 1;
					}
					else{
						// However, if we are here that means we were "interuptted"
						// by a break. We fixed the breakpoint and altered our registers
						// At the beginning of the parent while loop, so now we want
						// to use SINGLE_STEP and then reset the breakpoint that we just
						// fixed.
						fixBreakpoint(1);
					}
					done = 1;
					hasContinued = 1;
					break;
				case CMD_TYPE_WHERE:
					whereAmI();
					break;
				}
			}
		}
		ptrace( PTRACE_CONT, child, NULL, NULL );
	}
}