Ejemplo n.º 1
0
int do_proc_op(void *t, int proc_id)
{
	struct task_struct *task;
	struct thread_struct *thread;
	int op, pid;

	task = t;
	thread = &task->thread;
	op = thread->request.op;
	switch(op){
	case OP_NONE:
	case OP_TRACE_ON:
		break;
	case OP_EXEC:
		pid = thread->request.u.exec.pid;
		do_exec(thread->mode.tt.extern_pid, pid);
		thread->mode.tt.extern_pid = pid;
		cpu_tasks[task_thread_info(task)->cpu].pid = pid;
		break;
	case OP_FORK:
		attach_process(thread->request.u.fork.pid);
		break;
	case OP_CB:
		(*thread->request.u.cb.proc)(thread->request.u.cb.arg);
		break;
	case OP_REBOOT:
	case OP_HALT:
		break;
	default:
		tracer_panic("Bad op in do_proc_op");
		break;
	}
	thread->request.op = OP_NONE;
	return(op);
}
Ejemplo n.º 2
0
int backtrace_ptrace(int pid, int *tids, int *index, int nr_tids)
{
#if !defined (NO_LIBUNWIND_PTRACE)
    int i, count, rc = 0;
    int *threads = NULL;

    count = get_threads(pid, &threads);
    if (!count || threads == NULL)
        return -1;

    if (tids != NULL) {
        if (adjust_threads(threads, count, tids, index, nr_tids) < 0)
            return -1;

        free(threads);
        count = nr_tids;
        threads = tids;
    }

    if (attach_process(pid) < 0)
        return -1;

    for (i = 0; i < count; ++i) {
        void *upt_info;

        printf("--------------------  thread %d (%d)  --------------------\n",
               (index != NULL ? index[i] : i+1), threads[i]);

        if (threads[i] != pid && attach_thread(threads[i]) < 0) {
            rc = -1;
            break;
        }

        upt_info = _UPT_create(threads[i]);

        if (backtrace_thread(&_UPT_accessors, upt_info) < 0)
            rc = -1;

        _UPT_destroy(upt_info);

        if (threads[i] != pid && detach_thread(threads[i]))
            rc = -1;
        if (rc < 0)
            break;
    }

    free(threads);

    if (detach_process(pid) < 0)
        return -1;

    return rc;

#else
    return -1;
#endif /* NO_LIBUNWIND_PTRACE */
}
Ejemplo n.º 3
0
void* process_traffic(void *arg) {
	PGW pgw;

	while(1){
		pgw.read_data();
		if(pgw.status == 0)
			continue;
		pgw.set_metadata();

		if(pgw.type == 1){	
			attach_process(pgw);
		}
		else if(pgw.type == 2){
			data_transfer(pgw);
		}
		else if(pgw.type == 3){
			detach_process(pgw);
		}
		else{
			cout << "Incorrect type - " << pgw.type << endl;
		}
	}
	return NULL;
}
Ejemplo n.º 4
0
static int padzero(struct task_struct *tsk, unsigned long bss)
{
	int ret = 0;
	unsigned long nbyte;
	struct mm_struct *mm = NULL;

	nbyte = ELF_PAGEOFFSET(bss);
	if (nbyte) {
		nbyte = ELF_MIN_ALIGN - nbyte;
		if (tsk == current) {
			if (clear_user((void __user *) bss, nbyte))
				ret = -EFAULT;
		}
		else {
			struct eprocess *process = tsk->ethread->threads_process;
			mm = attach_process(&process->pcb);
			if (clear_user((void __user *) bss, nbyte))
				ret = -EFAULT;
			detach_process(mm);
		}
	}

	return ret;
}
Ejemplo n.º 5
0
int main(int argc, char **argv)
{
	struct bt_opts opts;
	struct btproc *bt_proc;

	bt_proc = parse_args(argc, argv, &opts);

	bt_proc->pi->pi_debug = opts.debug_opt;
	if (opts.target_opt && opts.pid_opt) {
		bt_proc_destroy(bt_proc);
		printfd(2,
			FATAL " You can't choose target and pid together !\n");
		btrace_banner(*argv, 1);
	} else if (!opts.target_opt && !opts.pid_opt) {
		printfd(2, FATAL "No such target or porcess\n");
		btrace_banner(*argv, 1);
	} else {
		/* using target executable */
		if (opts.target_opt) {
			bt_proc->exec =
			    check_target_path(bt_proc->pi->pi_target,
					      bt_proc->pi->pi_perm);

			if (!bt_proc->exec) {
				bt_proc_destroy(bt_proc);
				btrace_banner(*argv, 1);
			}
			if (opts.target_has_args)
				bt_proc->args_parser((char *)bt_proc->pi->
						     pi_args, bt_proc);

			else
				bt_proc->proc_arguments[0] =
				    strdup((const char *)bt_proc->exec);

			if ((!opts.force_addr_opt && opts.off_opt) ||
			    (opts.force_addr_opt && !opts.off_opt)) {
				printfd(STDERR_FILENO,
					WARN
					"You may choose both of address and offset !\n");
				bt_proc_destroy(bt_proc);
				btrace_banner(*argv, 1);
			}
			/* if address & offset are set */
			else {
				bt_proc->pi->pi_stack->ma_map[0] =
					bt_proc->pi->pi_address;
				bt_proc->pi->pi_stack->ma_map[1] =
					bt_proc->pi->pi_address +
					bt_proc->pi->pi_offset;
				
				bt_proc->pi->pi_addr->ma_map[0] =
				    bt_proc->pi->pi_address;
				bt_proc->pi->pi_addr->ma_map[1] =
				    bt_proc->pi->pi_address +
				    bt_proc->pi->pi_offset;
			}

			exec_target(bt_proc);

			/* If force address and offset are not set we read 
			 * from profs and fetch memory base address 
			 * and write new elf binary
			 */
			if (!opts.force_addr_opt && !opts.off_opt) {
				printfd(2,
					DO "Target :" GREEN " %s " NORM
					" PID : " GREEN "%d" NORM "\n",
					bt_proc->exec, bt_proc->pi->pi_pid);
				if (read_procfs_maps(bt_proc->pi) == -1)
					die("no such process");
			}

			fetch_data(bt_proc->pi);
		}

		/* pid attach */
		if (opts.pid_opt) {
			if (attach_process(bt_proc->pi) == -1)
				die(FATAL "Can't attach process");

			get_cmdline_by_pid(bt_proc->pi);
			printfd(2,
				DO "Attach PID:" GREEN " %d" NORM "  Target :"
				GREEN " %s" NORM "\n", bt_proc->pi->pi_pid,
				bt_proc->pi->pi_perm->p_full_path);

			if ((!opts.force_addr_opt && opts.off_opt) ||
			    (opts.force_addr_opt && !opts.off_opt)) {
				printfd(STDERR_FILENO,
					WARN
					"You may choose both of address and offset !\n");
				bt_proc_destroy(bt_proc);
				btrace_banner(*argv, 1);
			}
			/* if address & offset are set */
			else {
				bt_proc->pi->pi_stack->ma_map[0] =
					bt_proc->pi->pi_address;
				bt_proc->pi->pi_stack->ma_map[1] =
					bt_proc->pi->pi_address +
					bt_proc->pi->pi_offset;
				
				bt_proc->pi->pi_addr->ma_map[0] =
				    bt_proc->pi->pi_address;
				bt_proc->pi->pi_addr->ma_map[1] =
				    bt_proc->pi->pi_address +
				    bt_proc->pi->pi_offset;

			}

			if (!opts.force_addr_opt && !opts.off_opt)
				if (read_procfs_maps(bt_proc->pi) == -1)
					die(FATAL "No such process");

			/* it shouldn't return anything 
			 * BACK TO ME 
			 */
			fetch_data(bt_proc->pi);
		}

		if (opts.raw_opt)
			raw_dump(bt_proc->pi);
		else
			dump_using_memory(bt_proc->pi);

		if(opts.elf_dump_opts) {
			
		}
		
		pinfo_destroy(bt_proc->pi);
		bt_proc_destroy(bt_proc);

	}
	return 0;

}
Ejemplo n.º 6
0
/*
 * save process' memory maps, stack contents, thread identifiers and registers
 */
struct snapshot *get_snapshot(int pid, int *tids, int *index, int nr_tids)
{
    struct snapshot *res;
    int attached_tid = 0;
    int i, n_frames;
    long page, label, rc;
    struct mem_data_chunk **stacks_cover = NULL;

    if ((page = sysconf(_SC_PAGESIZE)) < 0) {
        perror("get pagesize");
        return NULL;
    }
    --page;

    res = calloc(1, sizeof(struct snapshot));

    /*
     * create memory_map structure corresponding to process' maps
     */
    res->map = create_maps(pid);
    if (res->map == NULL)
        goto get_snapshot_fail;

    /*
     * get process' threads
     */
    res->num_threads = get_threads(pid, &res->tids);
    if (res->num_threads < 0 || res->tids == NULL)
        goto get_snapshot_fail;

    /*
     * user-provided list of threads
     */
    if (tids != NULL) {
        if (adjust_threads(res->tids, res->num_threads, tids, index, nr_tids) < 0)
            goto get_snapshot_fail;

        free(res->tids);
        res->num_threads = nr_tids;
        res->tids = tids;
    }

    res->cur_thr = 0;

    res->regs = malloc(sizeof(res->regs[0])*res->num_threads);
    if (res->regs == NULL) {
        perror("malloc");
        goto get_snapshot_fail;
    }

    /* FREEZE PROCESS */
    if (attach_process(pid) < 0)
        goto get_snapshot_fail;

    for (i = 0; i < res->num_threads; ++i) {
        struct iovec iov;

        /*
         * we have already attached to main thread. call attach_thread()
         * for other ones
         */
        attached_tid = res->tids[i];
        if (res->tids[i] != pid && attach_thread(res->tids[i]) < 0)
            goto get_snapshot_fail_attached;

        /*
         * save thread's registers
         */
        iov.iov_len = sizeof(res->regs[0]);
        iov.iov_base = &res->regs[i];
        rc = ptrace(PTRACE_GETREGSET, res->tids[i], NT_PRSTATUS, &iov);
        if (rc < 0) {
            perror("PTRACE_GETREGSET");
            goto get_snapshot_fail_attached;
        }

        /*
         * save label on memory region. it will indicate that memory contents
         * upper than this point (%rsp) will needed to unwind stacks
         */
        label = SP_REG(&res->regs[i]) & ~page;
        rc = mem_map_add_label(res->map, (void *)label, res->num_threads);

        if (rc < 0) {
            fprintf(stderr, "failed to add label 0x%lx [rsp 0x%llx thread %d]\n",
                    label, (long long unsigned int)SP_REG(&res->regs[i]), res->tids[i]);
            goto get_snapshot_fail_attached;
        }

        /*
         * detach from thread. it will still be frozen due to SIGSTOP
         */
        if (res->tids[i] != pid && detach_thread(res->tids[i]) < 0)
            goto get_snapshot_fail_attached;
    }

    /*
     * arrange data chunks to copy memory contents. in most cases the chunks
     * will start from %rsp pointing somewhere in thread's stack
     * to the end of the stack region
     */
    stacks_cover = malloc(sizeof(struct mem_data_chunk*) * res->num_threads);

    n_frames = mem_map_build_label_cover(res->map, stack_size,
            stacks_cover, page + 1);

    if (stacks_cover == NULL) {
        fprintf(stderr, "error: stacks cover == NULL, n_frames=%d\n", n_frames);
        goto get_snapshot_fail_attached;
    }

    /*
     * copy memory contents
     */
    rc = copy_memory(pid, stacks_cover, n_frames);

    if (rc < 0)
        goto get_snapshot_fail_attached;

    /* UNFREEZE PROCESS */
    if (detach_process(pid) < 0)
        goto get_snapshot_fail;

    if (opt_verbose) {
        for (i = 0; i < n_frames; ++i) {
            struct mem_data_chunk *chunk = stacks_cover[i];
            printf("chunk #%d: 0x%lx-0x%lx length: %ldK\n",
                    i, (size_t)chunk->start,
                    (size_t)chunk->start + chunk->length,
                    chunk->length >> 10);
        }
    }

    free(stacks_cover);

    return res;

get_snapshot_fail_attached:
    if (attached_tid)
        detach_thread(attached_tid);

    detach_process(pid);

get_snapshot_fail:
    if (opt_verbose) {
        fprintf(stderr, "maps of %d:\n", pid);
        print_proc_maps(pid);
    }

    free(stacks_cover);
    snapshot_destroy(res);
    return NULL;
}
Ejemplo n.º 7
0
int backtrace_ptrace(int pid, int *tids, int *index, int nr_tids)
{
#if !defined (NO_LIBUNWIND_PTRACE)
    int i, count, rc = 0;
    int *threads = NULL;

    count = get_threads(pid, &threads);
    if (!count || threads == NULL)
        return -1;

    if (tids != NULL) {
        if (adjust_threads(threads, count, tids, index, nr_tids) < 0)
            return -1;

        free(threads);
        count = nr_tids;
        threads = tids;
    }

    if (attach_process(pid) < 0)
        return -1;

    for (i = 0; i < count; ++i) {
        void *upt_info;
        int x;
        char comm[16];
        char end_pad[25] = "------------------------";

        x = get_thread_comm(threads[i], comm, sizeof(comm));

        if (x > 0 && x <= sizeof(end_pad))
        {
            end_pad[sizeof(end_pad) - x] = '\0';
            printf("-------------- thread %d (%d) (%s) %s\n", (index != NULL ? index[i] : i + 1), threads[i], comm, end_pad);
        }

        if (threads[i] != pid && attach_thread(threads[i]) < 0) {
            rc = -1;
            break;
        }

        upt_info = _UPT_create(threads[i]);

        if (backtrace_thread(&_UPT_accessors, upt_info) < 0)
            rc = -1;

        _UPT_destroy(upt_info);

        if (threads[i] != pid && detach_thread(threads[i]))
            rc = -1;
        if (rc < 0)
            break;
    }

    free(threads);

    if (detach_process(pid) < 0)
        return -1;

    return rc;

#else
    return -1;
#endif /* NO_LIBUNWIND_PTRACE */
}
Ejemplo n.º 8
0
/*
 * save process' memory maps, stack contents, thread identifiers and registers
 */
struct snapshot *get_snapshot(int pid, int *tids, int *index, int nr_tids)
{
    struct snapshot *res;
    int i, attached_tid, n_frames;
    long page, label, rc;
    struct mem_data_chunk **stacks_cover = NULL;
    int v_major, v_minor;
    int use_process_vm_readv = 0;

    if ((page = sysconf(_SC_PAGESIZE)) < 0) {
        perror("get pagesize");
        return NULL;
    }
    --page;

    res = calloc(1, sizeof(struct snapshot));

    /*
     * create memory_map structure corresponding to process' maps
     */
    res->map = create_maps(pid);
    if (res->map == NULL)
        goto get_snapshot_fail;

    /*
     * get process' threads
     */
    res->num_threads = get_threads(pid, &res->tids);
    if (res->num_threads < 0 || res->tids == NULL)
        goto get_snapshot_fail;

    /*
     * user-provided list of threads
     */
    if (tids != NULL) {
        if (adjust_threads(res->tids, res->num_threads, tids, index, nr_tids) < 0)
            goto get_snapshot_fail;

        free(res->tids);
        res->num_threads = nr_tids;
        res->tids = tids;
    }

    res->cur_thr = 0;

    res->regs = malloc(sizeof(struct user_regs_struct)*res->num_threads);
    if (res->regs == NULL) {
        perror("malloc");
        goto get_snapshot_fail;
    }

    /*
     * decide how to copy memory contents of the process. on newer kernels
     * proc_vm_readv() is used by default. on older kernels or when the option
     * --proc-mem is specified read the file /proc/<pid>/mem
     */
    if (!opt_proc_mem) {
        if (get_kernel_version(&v_major, &v_minor) < 0)
            goto get_snapshot_fail;
        if (((v_major << 16) | v_minor) >= 0x30002)
            use_process_vm_readv = 1;
    } else {
        use_process_vm_readv = 0;
    }

    /* FREEZE PROCESS */
    if (attach_process(pid) < 0)
        goto get_snapshot_fail;

    for (i = 0; i < res->num_threads; ++i) {
        /*
         * we have already attached to main thread. call attach_thread()
         * for other ones
         */
        attached_tid = res->tids[i];
        if (res->tids[i] != pid && attach_thread(res->tids[i]) < 0)
            goto get_snapshot_fail_attached;

        /*
         * save thread's registers
         */
        rc = ptrace(PTRACE_GETREGS, res->tids[i], NULL, &res->regs[i]);
        if (rc < 0) {
            perror("PTRACE_GETREGS");
            goto get_snapshot_fail_attached;
        }

        /*
         * save label on memory region. it will indicate that memory contents
         * upper than this point (%rsp) will needed to unwind stacks
         */
        label = res->regs[i].rsp & ~page;
        rc = mem_map_add_label(res->map, (void *)label, res->num_threads);

        if (rc < 0) {
            fprintf(stderr, "failed to add label 0x%lx [rsp 0x%lx thread %d]\n",
                    label, res->regs[i].rsp, res->tids[i]);
            goto get_snapshot_fail_attached;
        }

        /*
         * detach from thread. it will still be frozen due to SIGSTOP
         */
        if (res->tids[i] != pid && detach_thread(res->tids[i]) < 0)
            goto get_snapshot_fail_attached;
    }

    /*
     * arrange data chunks to copy memory contents. in most cases the chunks
     * will start from %rsp pointing somewhere in thread's stack
     * to the end of the stack region
     */
    stacks_cover = malloc(sizeof(struct mem_data_chunk*) * res->num_threads);

    n_frames = mem_map_build_label_cover(res->map, stack_size,
            stacks_cover, page + 1);

    if (stacks_cover == NULL) {
        fprintf(stderr, "error: stacks cover == NULL, n_frames=%d\n", n_frames);
        goto get_snapshot_fail_attached;
    }

    /*
     * copy memory contents
     */
    rc = use_process_vm_readv ?
        copy_memory_process_vm_readv(pid, stacks_cover, n_frames) :
        copy_memory_proc_mem(pid, stacks_cover, n_frames);

    if (rc < 0)
        goto get_snapshot_fail_attached;

    /* UNFREEZE PROCESS */
    if (detach_process(pid) < 0)
        goto get_snapshot_fail;

    if (opt_verbose) {
        for (i = 0; i < n_frames; ++i) {
            struct mem_data_chunk *chunk = stacks_cover[i];
            printf("chunk #%d: 0x%lx-0x%lx length: %ldK\n",
                    i, (size_t)chunk->start,
                    (size_t)chunk->start + chunk->length,
                    chunk->length >> 10);
        }
    }

    free(stacks_cover);

    return res;

get_snapshot_fail_attached:
    if (attached_tid)
        detach_thread(attached_tid);

    detach_process(pid);

get_snapshot_fail:
    if (opt_verbose) {
        fprintf(stderr, "maps of %d:\n", pid);
        print_proc_maps(pid);
    }

    free(stacks_cover);
    snapshot_destroy(res);
    return NULL;
}