#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/kernel.h>
#include <linux/err.h>
#include <traceevent/event-parse.h>
#include <api/fs/tracing_path.h>
#include <api/fs/fs.h>
#include "trace-event.h"
#include "machine.h"
#include "util.h"

/*
 * global trace_event object used by trace_event__tp_format
 *
 * TODO There's no cleanup call for this. Add some sort of
 * __exit function support and call trace_event__cleanup
 * there.
 */
static struct trace_event tevent;
static bool tevent_initialized;

int trace_event__init(struct trace_event *t)
{
	struct pevent *pevent = pevent_alloc();

	if (pevent) {
		t->plugin_list = traceevent_load_plugins(pevent);
		t->pevent  = pevent;
	}

	return pevent ? 0 : -1;
}

static int trace_event__init2(void)
{
	int be = traceevent_host_bigendian();
	struct pevent *pevent;

	if (trace_event__init(&tevent))
		return -1;

	pevent = tevent.pevent;
	pevent_set_flag(pevent, PEVENT_NSEC_OUTPUT);
	pevent_set_file_bigendian(pevent, be);
	pevent_set_host_bigendian(pevent, be);
	tevent_initialized = true;
	return 0;
}

int trace_event__register_resolver(struct machine *machine,
				   pevent_func_resolver_t *func)
{
	if (!tevent_initialized && trace_event__init2())
		return -1;

	return pevent_set_function_resolver(tevent.pevent, func, machine);
}

void trace_event__cleanup(struct trace_event *t)
{
	traceevent_unload_plugins(t->plugin_list, t->pevent);
	pevent_free(t->pevent);
}

/*
 * Returns pointer with encoded error via <linux/err.h> interface.
 */
static struct event_format*
tp_format(const char *sys, const char *name)
{
	struct pevent *pevent = tevent.pevent;
	struct event_format *event = NULL;
	char path[PATH_MAX];
	size_t size;
	char *data;
	int err;

	scnprintf(path, PATH_MAX, "%s/%s/%s/format",
		  tracing_events_path, sys, name);

	err = filename__read_str(path, &data, &size);
	if (err)
		return ERR_PTR(err);

	pevent_parse_format(pevent, &event, data, size, sys);

	free(data);
	return event;
}

/*
 * Returns pointer with encoded error via <linux/err.h> interface.
 */
struct event_format*
trace_event__tp_format(const char *sys, const char *name)
{
	if (!tevent_initialized && trace_event__init2())
		return ERR_PTR(-ENOMEM);

	return tp_format(sys, name);
}

struct event_format *trace_event__tp_format_id(int id)
{
	if (!tevent_initialized && trace_event__init2())
		return ERR_PTR(-ENOMEM);

	return pevent_find_event(tevent.pevent, id);
}
sg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/cgit.cgi/linux/net-next.git/diff/?id=214767faa2f31285f92754393c036f13b55474a6&amp;id2=4ea33ef0f9e95b69db9131d7afd98563713e81b0'>root</a>/<a href='/cgit.cgi/linux/net-next.git/diff/fs?id=214767faa2f31285f92754393c036f13b55474a6&amp;id2=4ea33ef0f9e95b69db9131d7afd98563713e81b0'>fs</a>/<a href='/cgit.cgi/linux/net-next.git/diff/fs/xfs?id=214767faa2f31285f92754393c036f13b55474a6&amp;id2=4ea33ef0f9e95b69db9131d7afd98563713e81b0'>xfs</a>/<a href='/cgit.cgi/linux/net-next.git/diff/fs/xfs/libxfs?id=214767faa2f31285f92754393c036f13b55474a6&amp;id2=4ea33ef0f9e95b69db9131d7afd98563713e81b0'>libxfs</a></div><div class='content'><div class='cgit-panel'><b>diff options</b><form method='get'><input type='hidden' name='id' value='214767faa2f31285f92754393c036f13b55474a6'/><input type='hidden' name='id2' value='4ea33ef0f9e95b69db9131d7afd98563713e81b0'/><table><tr><td colspan='2'/></tr><tr><td class='label'>context:</td><td class='ctrl'><select name='context' onchange='this.form.submit();'><option value='1'>1</option><option value='2'>2</option><option value='3' selected='selected'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option><option value='15'>15</option><option value='20'>20</option><option value='25'>25</option><option value='30'>30</option><option value='35'>35</option><option value='40'>40</option></select></td></tr><tr><td class='label'>space:</td><td class='ctrl'><select name='ignorews' onchange='this.form.submit();'><option value='0' selected='selected'>include</option><option value='1'>ignore</option></select></td></tr><tr><td class='label'>mode:</td><td class='ctrl'><select name='dt' onchange='this.form.submit();'><option value='0' selected='selected'>unified</option><option value='1'>ssdiff</option><option value='2'>stat only</option></select></td></tr><tr><td/><td class='ctrl'><noscript><input type='submit' value='reload'/></noscript></td></tr></table></form></div><div class='diffstat-header'><a href='/cgit.cgi/linux/net-next.git/diff/?id=214767faa2f31285f92754393c036f13b55474a6&amp;id2=4ea33ef0f9e95b69db9131d7afd98563713e81b0'>Diffstat</a> (limited to 'fs/xfs/libxfs')</div><table summary='diffstat' class='diffstat'>