tools/bpf/bpftool/tracelog.c

Source file repositories/reference/linux-study-clean/tools/bpf/bpftool/tracelog.c

File Facts

System
Linux kernel
Corpus path
tools/bpf/bpftool/tracelog.c
Extension
.c
Size
3242 bytes
Lines
160
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (strcmp(type, fstype) == 0) {
			found = true;
			break;
		}
	fclose(fp);

	/* The string from fscanf() might be truncated, check mnt is valid */
	if (found && validate_tracefs_mnt(mnt, TRACEFS_MAGIC))
		goto exit_found;

	if (block_mount)
		return false;

	p_info("could not find tracefs, attempting to mount it now");
	strcpy(mnt, known_mnts[0]);
	if (mount_tracefs(mnt))
		return false;

exit_found:
	strcat(mnt, pipe_name);
	return true;
}

static void exit_tracelog(int signum)
{
	fclose(trace_pipe_fd);
	free(buff);

	if (json_output) {
		jsonw_end_array(json_wtr);
		jsonw_destroy(&json_wtr);
	}

	exit(0);
}

int do_tracelog(int argc, char **argv)
{
	const struct sigaction act = {
		.sa_handler = exit_tracelog
	};
	char trace_pipe[PATH_MAX];
	size_t buff_len = 0;

	if (json_output)
		jsonw_start_array(json_wtr);

	if (!get_tracefs_pipe(trace_pipe))
		return -1;

	trace_pipe_fd = fopen(trace_pipe, "r");
	if (!trace_pipe_fd) {
		p_err("could not open trace pipe: %s", strerror(errno));
		return -1;
	}

	sigaction(SIGHUP, &act, NULL);
	sigaction(SIGINT, &act, NULL);
	sigaction(SIGTERM, &act, NULL);
	while (1) {
		ssize_t ret;

		ret = getline(&buff, &buff_len, trace_pipe_fd);
		if (ret <= 0) {
			p_err("failed to read content from trace pipe: %s",
			      strerror(errno));
			break;
		}
		if (json_output)
			jsonw_string(json_wtr, buff);
		else
			printf("%s", buff);
	}

	fclose(trace_pipe_fd);
	free(buff);
	return -1;
}

Annotation

Implementation Notes