Ejemplo n.º 1
0
int op_write_native_code(op_agent_t hdl, char const * symbol_name,
	uint64_t vma, void const * code, unsigned int const size)
{
	struct jr_code_load rec;
	struct timeval tv;
	size_t sz_symb_name;
	char pad_bytes[7] = { 0, 0, 0, 0, 0, 0, 0 };
	size_t padding_count;
	FILE * dumpfile = (FILE *) hdl;

	if (!dumpfile) {
		errno = EINVAL;
		fprintf(stderr, "Invalid hdl argument\n");
		return -1;
	}
	sz_symb_name = strlen(symbol_name) + 1;

	rec.id = JIT_CODE_LOAD;
	rec.code_size = size;
	rec.vma = vma;
	rec.code_addr = (u64) (uintptr_t) code;
	rec.total_size = code ? sizeof(rec) + sz_symb_name + size :
			sizeof(rec) + sz_symb_name;
	/* calculate amount of padding '\0' */
	padding_count = PADDING_8ALIGNED(rec.total_size);
	rec.total_size += padding_count;
	if (gettimeofday(&tv, NULL)) {
		fprintf(stderr, "gettimeofday failed\n");
		return -1;
	}

	rec.timestamp = tv.tv_sec;

	/* locking makes sure that we continuously write this record, if
	 * we are called within a multi-threaded context */
	flockfile(dumpfile);
	/* Write record, symbol name, code (optionally), and (if necessary)
	 * additonal padding \0 bytes.
	 */
	if (fwrite_unlocked(&rec, sizeof(rec), 1, dumpfile) &&
	    fwrite_unlocked(symbol_name, sz_symb_name, 1, dumpfile)) {
		if (code)
			fwrite_unlocked(code, size, 1, dumpfile);
		if (padding_count)
			fwrite_unlocked(pad_bytes, padding_count, 1, dumpfile);
		/* Always flush to ensure conversion code to elf will see
		 * data as soon as possible */
		fflush_unlocked(dumpfile);
		funlockfile(dumpfile);
		return 0;
	}
	fflush_unlocked(dumpfile);
	funlockfile(dumpfile);
	return -1;
}
Ejemplo n.º 2
0
int op_write_native_code(op_agent_t hdl, char const * symbol_name,
	uint64_t vma, void const * code, unsigned int const size)
{
	struct jr_code_load rec;
	struct timeval tv;
	size_t sz_symb_name;
	char pad_bytes[7] = { 0, 0, 0, 0, 0, 0, 0 };
	size_t padding_count;
	FILE * dumpfile = (FILE *) hdl;

	if (!dumpfile) {
		errno = EINVAL;
		fprintf(stderr, "Invalid hdl argument\n");
		return -1;
	}
	sz_symb_name = strlen(symbol_name) + 1;

	rec.id = JIT_CODE_LOAD;
	rec.code_size = size;
	rec.vma = vma;
	rec.code_addr = (u64) (uintptr_t) code;
	rec.total_size = code ? sizeof(rec) + sz_symb_name + size :
			sizeof(rec) + sz_symb_name;
	
	padding_count = PADDING_8ALIGNED(rec.total_size);
	rec.total_size += padding_count;
	if (gettimeofday(&tv, NULL)) {
		fprintf(stderr, "gettimeofday failed\n");
		return -1;
	}

	rec.timestamp = tv.tv_sec;

	flockfile(dumpfile);
	if (fwrite_unlocked(&rec, sizeof(rec), 1, dumpfile) &&
	    fwrite_unlocked(symbol_name, sz_symb_name, 1, dumpfile)) {
		if (code)
			fwrite_unlocked(code, size, 1, dumpfile);
		if (padding_count)
			fwrite_unlocked(pad_bytes, padding_count, 1, dumpfile);
		fflush_unlocked(dumpfile);
		funlockfile(dumpfile);
		return 0;
	}
	fflush_unlocked(dumpfile);
	funlockfile(dumpfile);
	return -1;
}
Ejemplo n.º 3
0
static int parse_code_load(void const * ptr_arg, int size,
			   unsigned long long end_time)
{
	struct jitentry * entry;
	int rc = OP_JIT_CONV_OK;
	char const * ptr = ptr_arg;
	struct jr_code_load const * rec = ptr_arg;
	char const * end;
	size_t padding_count, rec_totalsize;
	end = rec->code_addr ? ptr + size : NULL;

	entry = xcalloc(1, sizeof(struct jitentry));

	
	entry->next = NULL;
	ptr += sizeof(*rec);
	
	entry->symbol_name = (char *)ptr;
	entry->sym_name_malloced = 0;
	ptr += strlen(ptr) + 1;
	entry->code = rec->code_addr ? ptr : NULL;
	entry->vma = rec->vma;
	entry->code_size = rec->code_size;
	entry->section = NULL;
	entry->life_start = rec->timestamp;
	
	// sampling run, this value may be overwritten by an unload record1
	
	entry->life_end = end_time;

	
	entry->next = jitentry_list;
	jitentry_list = entry;

	rec_totalsize = sizeof(*rec) + strlen(entry->symbol_name) + 1 + entry->code_size;
	padding_count = PADDING_8ALIGNED(rec_totalsize);

	verbprintf(debug, "record0: name=%s, vma=%llx, code_size=%i, "
		   "padding_count=%llu, life_start=%lli, life_end=%lli\n", entry->symbol_name,
		   entry->vma, entry->code_size, (unsigned long long)padding_count, entry->life_start,
		   entry->life_end);
	if (end && (ptr + entry->code_size + padding_count != end)) {
		verbprintf(debug, "record total size mismatch\n");
		rc = OP_JIT_CONV_FAIL;
	}
	return rc;
}
Ejemplo n.º 4
0
int op_write_debug_line_info(op_agent_t hdl, void const * code,
			     size_t nr_entry,
			     struct debug_line_info const * compile_map)
{
	struct jr_code_debug_info rec;
	long cur_pos, last_pos;
	struct timeval tv;
	size_t i;
	size_t padding_count;
	char padd_bytes[7] = {0, 0, 0, 0, 0, 0, 0};
	int rc = -1;
	FILE * dumpfile = (FILE *) hdl;

	if (!dumpfile) {
		errno = EINVAL;
		fprintf(stderr, "Invalid hdl argument\n");
		return -1;
	}
	
	/* write nothing if no entries are provided */
	if (nr_entry == 0)
		return 0;

	rec.id = JIT_CODE_DEBUG_INFO;
	rec.code_addr = (uint64_t)(uintptr_t)code;
	/* will be fixed after writing debug line info */
	rec.total_size = 0;
	rec.nr_entry = nr_entry;
	if (gettimeofday(&tv, NULL)) {
		fprintf(stderr, "gettimeofday failed\n");
		return -1;
	}

	rec.timestamp = tv.tv_sec;

	flockfile(dumpfile);

	if ((cur_pos = ftell(dumpfile)) == -1l)
		goto error;
	if (!fwrite_unlocked(&rec, sizeof(rec), 1, dumpfile))
		goto error;
	for (i = 0; i < nr_entry; ++i) {
		if (!fwrite_unlocked(&compile_map[i].vma,
				     sizeof(compile_map[i].vma), 1,
				     dumpfile) ||
		    !fwrite_unlocked(&compile_map[i].lineno,
				     sizeof(compile_map[i].lineno), 1,
				     dumpfile) ||
		    !fwrite_unlocked(compile_map[i].filename,
				     strlen(compile_map[i].filename) + 1, 1,
				     dumpfile))
			goto error;
	}

	if ((last_pos = ftell(dumpfile)) == -1l)
		goto error;
	rec.total_size = last_pos - cur_pos;
	padding_count = PADDING_8ALIGNED(rec.total_size);
	rec.total_size += padding_count;
	if (padding_count && !fwrite(padd_bytes, padding_count, 1, dumpfile))
		goto error;
	if (fseek(dumpfile, cur_pos, SEEK_SET) == -1l)
		goto error;
	if (!fwrite_unlocked(&rec, sizeof(rec), 1, dumpfile))
		goto error;
	if (fseek(dumpfile, last_pos + padding_count, SEEK_SET) == -1)
		goto error;

	rc = 0;
error:
	fflush_unlocked(dumpfile);
	funlockfile(dumpfile);
	return rc;
}
Ejemplo n.º 5
0
op_agent_t op_open_agent(void)
{
	char pad_bytes[7] = {0, 0, 0, 0, 0, 0, 0};
	int pad_cnt;
	char dump_path[PATH_MAX];
	char err_msg[PATH_MAX + 16];
	int rc;
	struct jitheader header;
	int fd;
	struct timeval tv;
	FILE * dumpfile = NULL;

	/* Coverity complains about 'time-of-check-time-of-use' race if we do stat() on
	 * a file (or directory) and then open or create it afterwards.  So instead,
	 * we'll try to open it and see what happens.
	 */
	int create_dir = 0;
	DIR * dir1 = opendir(TMP_OPROFILE_DIR);
	if (!dir1) {
		if (errno == ENOENT) {
			create_dir = 1;
		} else if (errno == ENOTDIR) {
			fprintf(stderr, "Error: Creation of directory %s failed. File exists where directory is expected.\n",
			        TMP_OPROFILE_DIR);
			return NULL;
		}
	} else {
		closedir(dir1);
	}
	if (create_dir) {
		create_dir = 0;
		rc = mkdir(TMP_OPROFILE_DIR, S_IRWXU | S_IRWXG | S_IRWXO);
		if (rc && (errno != EEXIST)) {
			fprintf(stderr, "Error trying to create %s dir.\n", TMP_OPROFILE_DIR);
			return NULL;
		}
	}

	dir1 = opendir(JITDUMP_DIR);
	if (!dir1) {
		if (errno == ENOENT) {
			create_dir = 1;
		} else if (errno == ENOTDIR) {
			fprintf(stderr, "Error: Creation of directory %s failed. File exists where directory is expected.\n",
			        JITDUMP_DIR);
			return NULL;
		}
	} else {
		closedir(dir1);
	}

	if (create_dir) {
		rc = mkdir(JITDUMP_DIR, S_IRWXU | S_IRWXG | S_IRWXO);
		if (rc && (errno != EEXIST)) {
			fprintf(stderr, "Error trying to create %s dir.\n", JITDUMP_DIR);
			return NULL;
		}
	}
	snprintf(dump_path, PATH_MAX, "%s/%i.dump", JITDUMP_DIR, getpid());
	snprintf(err_msg, PATH_MAX + 16, "Error opening %s\n", dump_path);
	// make the dump file only accessible for the user for security reason.
	fd = creat(dump_path, S_IRUSR|S_IWUSR);
	if (fd == -1) {
		fprintf(stderr, "%s\n", err_msg);
		return NULL;
	}
	dumpfile = fdopen(fd, "w");
	if (!dumpfile) {
		fprintf(stderr, "%s\n", err_msg);
		close(fd);
		return NULL;
	}
	if (define_bfd_vars()) {
		fclose(dumpfile);
		return NULL;
	}
	header.magic = JITHEADER_MAGIC;
	header.version = JITHEADER_VERSION;
	header.totalsize = sizeof(header) + strlen(_bfd_target_name) + 1;
	/* calculate amount of padding '\0' */
	pad_cnt = PADDING_8ALIGNED(header.totalsize);
	header.totalsize += pad_cnt;
	header.bfd_arch = _bfd_arch;
	header.bfd_mach = _bfd_mach;
	if (gettimeofday(&tv, NULL)) {
		fclose(dumpfile);
		fprintf(stderr, "gettimeofday failed\n");
		return NULL;
	}

	header.timestamp = tv.tv_sec;
	snprintf(err_msg, PATH_MAX + 16, "Error writing to %s", dump_path);
	if (!fwrite(&header, sizeof(header), 1, dumpfile)) {
		fclose(dumpfile);
		fprintf(stderr, "%s\n", err_msg);
		return NULL;
	}
	if (!fwrite(_bfd_target_name, strlen(_bfd_target_name) + 1, 1,
		    dumpfile)) {
		fclose(dumpfile);
		fprintf(stderr, "%s\n", err_msg);
		return NULL;
	}
	/* write padding '\0' if necessary */
	if (pad_cnt && !fwrite(pad_bytes, pad_cnt, 1, dumpfile)) {
		fclose(dumpfile);
		fprintf(stderr, "%s\n", err_msg);
		return NULL;
	}
	fflush(dumpfile);
	return (op_agent_t)dumpfile;
}
Ejemplo n.º 6
0
op_agent_t op_open_agent(void)
{
	char pad_bytes[7] = {0, 0, 0, 0, 0, 0, 0};
	int pad_cnt;
	char dump_path[PATH_MAX];
	char err_msg[PATH_MAX + 16];
	struct stat dirstat;
	int rc;
	struct jitheader header;
	int fd;
	struct timeval tv;
	FILE * dumpfile = NULL;

	rc = stat(AGENT_DIR, &dirstat);
	if (rc || !S_ISDIR(dirstat.st_mode)) {
		if (!rc)
			errno = ENOTDIR;
		fprintf(stderr,"libopagent: Jitdump agent directory %s "
			"missing\n", AGENT_DIR);
		fprintf(stderr,"libopagent: do opcontrol --setup or "
			"opcontrol --reset, first\n");
		return NULL;
	}
	snprintf(dump_path, PATH_MAX, "%s/%i.dump", AGENT_DIR, getpid());
	snprintf(err_msg, PATH_MAX + 16, "Error opening %s\n", dump_path);
	
	fd = creat(dump_path, S_IRUSR|S_IWUSR);
	if (fd == -1) {
		fprintf(stderr, "%s\n", err_msg);
		return NULL;
	}
	dumpfile = fdopen(fd, "w");
	if (!dumpfile) {
		fprintf(stderr, "%s\n", err_msg);
		return NULL;
	}
	if (define_bfd_vars())
		return NULL;
	header.magic = JITHEADER_MAGIC;
	header.version = JITHEADER_VERSION;
	header.totalsize = sizeof(header) + strlen(_bfd_target_name) + 1;
	
	pad_cnt = PADDING_8ALIGNED(header.totalsize);
	header.totalsize += pad_cnt;
	header.bfd_arch = _bfd_arch;
	header.bfd_mach = _bfd_mach;
	if (gettimeofday(&tv, NULL)) {
		fprintf(stderr, "gettimeofday failed\n");
		return NULL;
	}

	header.timestamp = tv.tv_sec;
	snprintf(err_msg, PATH_MAX + 16, "Error writing to %s", dump_path);
	if (!fwrite(&header, sizeof(header), 1, dumpfile)) {
		fprintf(stderr, "%s\n", err_msg);
		return NULL;
	}
	if (!fwrite(_bfd_target_name, strlen(_bfd_target_name) + 1, 1,
		    dumpfile)) {
		fprintf(stderr, "%s\n", err_msg);
		return NULL;
	}
	
	if (pad_cnt && !fwrite(pad_bytes, pad_cnt, 1, dumpfile)) {
		fprintf(stderr, "%s\n", err_msg);
		return NULL;
	}
	fflush(dumpfile);
	return (op_agent_t)dumpfile;
}
Ejemplo n.º 7
0
int
jvmti_write_debug_info(void *agent, uint64_t code, const char *file,
		       jvmti_line_info_t *li, int nr_lines)
{
	struct jr_code_debug_info rec;
	size_t sret, len, size, flen;
	size_t padding_count;
	uint64_t addr;
	const char *fn = file;
	FILE *fp = agent;
	int i;

	/*
	 * no entry to write
	 */
	if (!nr_lines)
		return 0;

	if (!fp) {
		warnx("jvmti: invalid fd in write_debug_info");
		return -1;
	}

	flen = strlen(file) + 1;

	rec.p.id        = JIT_CODE_DEBUG_INFO;
	size            = sizeof(rec);
	rec.p.timestamp = perf_get_timestamp();
	rec.code_addr   = (uint64_t)(uintptr_t)code;
	rec.nr_entry    = nr_lines;

	/*
	 * on disk source line info layout:
	 * uint64_t : addr
	 * int      : line number
	 * int      : column discriminator
	 * file[]   : source file name
	 * padding  : pad to multiple of 8 bytes
	 */
	size += nr_lines * sizeof(struct debug_entry);
	size += flen * nr_lines;
	/*
	 * pad to 8 bytes
	 */
	padding_count = PADDING_8ALIGNED(size);

	rec.p.total_size = size + padding_count;

	/*
	 * If JVM is multi-threaded, nultiple concurrent calls to agent
	 * may be possible, so protect file writes
	 */
	flockfile(fp);

	sret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
	if (sret != 1)
		goto error;

	for (i = 0; i < nr_lines; i++) {

		addr = (uint64_t)li[i].pc;
		len  = sizeof(addr);
		sret = fwrite_unlocked(&addr, len, 1, fp);
		if (sret != 1)
			goto error;

		len  = sizeof(li[0].line_number);
		sret = fwrite_unlocked(&li[i].line_number, len, 1, fp);
		if (sret != 1)
			goto error;

		len  = sizeof(li[0].discrim);
		sret = fwrite_unlocked(&li[i].discrim, len, 1, fp);
		if (sret != 1)
			goto error;

		sret = fwrite_unlocked(fn, flen, 1, fp);
		if (sret != 1)
			goto error;
	}
	if (padding_count)
		sret = fwrite_unlocked(pad_bytes, padding_count, 1, fp);
		if (sret != 1)
			goto error;

	funlockfile(fp);
	return 0;
error:
	funlockfile(fp);
	return -1;
}
Ejemplo n.º 8
0
int
jvmti_write_code(void *agent, char const *sym,
	uint64_t vma, void const *code, unsigned int const size)
{
	static int code_generation = 1;
	struct jr_code_load rec;
	size_t sym_len;
	size_t padding_count;
	FILE *fp = agent;
	int ret = -1;

	/* don't care about 0 length function, no samples */
	if (size == 0)
		return 0;

	if (!fp) {
		warnx("jvmti: invalid fd in write_native_code");
		return -1;
	}

	sym_len = strlen(sym) + 1;

	rec.p.id           = JIT_CODE_LOAD;
	rec.p.total_size   = sizeof(rec) + sym_len;
	padding_count      = PADDING_8ALIGNED(rec.p.total_size);
	rec.p. total_size += padding_count;
	rec.p.timestamp    = perf_get_timestamp();

	rec.code_size  = size;
	rec.vma        = vma;
	rec.code_addr  = vma;
	rec.pid	       = getpid();
	rec.tid	       = gettid();

	if (code)
		rec.p.total_size += size;

	/*
	 * If JVM is multi-threaded, nultiple concurrent calls to agent
	 * may be possible, so protect file writes
	 */
	flockfile(fp);

	/*
	 * get code index inside lock to avoid race condition
	 */
	rec.code_index = code_generation++;

	ret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
	fwrite_unlocked(sym, sym_len, 1, fp);

	if (padding_count)
		fwrite_unlocked(pad_bytes, padding_count, 1, fp);

	if (code)
		fwrite_unlocked(code, size, 1, fp);

	funlockfile(fp);

	ret = 0;

	return ret;
}
Ejemplo n.º 9
0
void *jvmti_open(void)
{
	int pad_cnt;
	char dump_path[PATH_MAX];
	struct jitheader header;
	int fd;
	FILE *fp;

	/*
	 * check if clockid is supported
	 */
	if (!perf_get_timestamp())
		warnx("jvmti: kernel does not support %d clock id", perf_clk_id);

	memset(&header, 0, sizeof(header));

	debug_cache_init();

	/*
	 * jitdump file name
	 */
	snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());

	fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666);
	if (fd == -1)
		return NULL;

	/*
	 * create perf.data maker for the jitdump file
	 */
	if (perf_open_marker_file(fd)) {
		warnx("jvmti: failed to create marker file");
		return NULL;
	}

	fp = fdopen(fd, "w+");
	if (!fp) {
		warn("jvmti: cannot create %s", dump_path);
		close(fd);
		goto error;
	}

	warnx("jvmti: jitdump in %s", dump_path);

	if (get_e_machine(&header)) {
		warn("get_e_machine failed\n");
		goto error;
	}

	header.magic      = JITHEADER_MAGIC;
	header.version    = JITHEADER_VERSION;
	header.total_size = sizeof(header);
	header.pid        = getpid();

	/* calculate amount of padding '\0' */
	pad_cnt = PADDING_8ALIGNED(header.total_size);
	header.total_size += pad_cnt;

	header.timestamp = perf_get_timestamp();

	if (!fwrite(&header, sizeof(header), 1, fp)) {
		warn("jvmti: cannot write dumpfile header");
		goto error;
	}

	/* write padding '\0' if necessary */
	if (pad_cnt && !fwrite(pad_bytes, pad_cnt, 1, fp)) {
		warn("jvmti: cannot write dumpfile header padding");
		goto error;
	}

	return fp;
error:
	fclose(fp);
	return NULL;
}