char* read_child_str(pid_t pid, long int addr) { char *tmp, *str; int i, idx = 0; int buffer_size = 256; str = sys_malloc(buffer_size); while (1) { if (idx + READ_SIZE >= buffer_size) { buffer_size *= 2; str = realloc(str, buffer_size); } tmp = read_child_data_tid(pid, READ_SIZE, addr + idx); memcpy(str + idx, tmp, READ_SIZE); sys_free((void**) &tmp); for (i = 0; i < READ_SIZE; i++) { if (str[idx + i] == '\0') { return str; } } idx += READ_SIZE; }assert(1==0); return 0; }
void* read_child_data(struct context *ctx, ssize_t size, uintptr_t addr) { void *buf = sys_malloc(size); /* if pread fails: do the following: echo 0 > /proc/sys/kernel/yama/ptrace_scope */ ssize_t read_bytes = checked_pread(ctx,buf,size,addr); if (read_bytes != size) { free(buf); buf = read_child_data_tid(ctx->child_tid,size,addr); printf("reading from: %x demanded: %u read %u event: %d\n", addr, size, read_bytes, ctx->event); perror("warning: reading from child process: "); printf("try the following: echo 0 > /proc/sys/kernel/yama/ptrace_scope\n"); sleep(5); } return buf; }
void* read_child_data(struct context *ctx, size_t size, uintptr_t addr) { size_t bytes_read; void* data = sys_malloc(size); /* if pread cannot read all data (for whatever reason) we use ptrace * primitives to get the rest. */ if ((bytes_read = pread64(ctx->child_mem_fd, data, size, addr)) < size) { assert(bytes_read >= 0); void* rest = read_child_data_tid(ctx->child_tid, size - bytes_read, addr + bytes_read); memcpy(data + bytes_read, rest, size - bytes_read); sys_free((void**) &rest); } /* make sure we no not return more than required */ return data; }