tools/perf/util/intel-pt-decoder/intel-pt-log.c

Source file repositories/reference/linux-study-clean/tools/perf/util/intel-pt-decoder/intel-pt-log.c

File Facts

System
Linux kernel
Corpus path
tools/perf/util/intel-pt-decoder/intel-pt-log.c
Extension
.c
Size
5220 bytes
Lines
268
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

struct log_buf {
	char			*buf;
	size_t			buf_sz;
	size_t			head;
	bool			wrapped;
	FILE			*backend;
};

static FILE *f;
static char log_name[MAX_LOG_NAME];
bool intel_pt_enable_logging;
static bool intel_pt_dump_log_on_error;
static unsigned int intel_pt_log_on_error_size;
static struct log_buf log_buf;

void *intel_pt_log_fp(void)
{
	return f;
}

void intel_pt_log_enable(bool dump_log_on_error, unsigned int log_on_error_size)
{
	intel_pt_enable_logging = true;
	intel_pt_dump_log_on_error = dump_log_on_error;
	intel_pt_log_on_error_size = log_on_error_size;
}

void intel_pt_log_disable(void)
{
	if (f)
		fflush(f);
	intel_pt_enable_logging = false;
}

void intel_pt_log_set_name(const char *name)
{
	strncpy(log_name, name, MAX_LOG_NAME - 5);
	strcat(log_name, ".log");
}

static void intel_pt_print_data(const unsigned char *buf, int len, uint64_t pos,
				int indent)
{
	int i;

	for (i = 0; i < indent; i++)
		fprintf(f, " ");

	fprintf(f, "  %08" PRIx64 ": ", pos);
	for (i = 0; i < len; i++)
		fprintf(f, " %02x", buf[i]);
	for (; i < 16; i++)
		fprintf(f, "   ");
	fprintf(f, " ");
}

static void intel_pt_print_no_data(uint64_t pos, int indent)
{
	int i;

	for (i = 0; i < indent; i++)
		fprintf(f, " ");

	fprintf(f, "  %08" PRIx64 ": ", pos);
	for (i = 0; i < 16; i++)
		fprintf(f, "   ");
	fprintf(f, " ");
}

static ssize_t log_buf__write(void *cookie, const char *buf, size_t size)
{
	struct log_buf *b = cookie;
	size_t sz = size;

	if (!b->buf)
		return size;

	while (sz) {
		size_t space = b->buf_sz - b->head;
		size_t n = min(space, sz);

		memcpy(b->buf + b->head, buf, n);
		sz -= n;
		buf += n;
		b->head += n;
		if (sz && b->head >= b->buf_sz) {
			b->head = 0;
			b->wrapped = true;
		}
	}

Annotation

Implementation Notes