1 // SPDX-License-Identifier: GPL-2.0
3 #include <sys/sendfile.h>
14 * enable_tracer_by_name - enable a tracer on the given instance
16 int enable_tracer_by_name(struct tracefs_instance *inst, const char *tracer_name)
18 enum tracefs_tracers tracer;
21 tracer = TRACEFS_TRACER_CUSTOM;
23 debug_msg("Enabling %s tracer\n", tracer_name);
25 retval = tracefs_tracer_set(inst, tracer, tracer_name);
28 err_msg("Tracer %s not found!\n", tracer_name);
30 err_msg("Failed to enable the %s tracer\n", tracer_name);
38 * disable_tracer - set nop tracer to the insta
40 void disable_tracer(struct tracefs_instance *inst)
42 enum tracefs_tracers t = TRACEFS_TRACER_NOP;
45 retval = tracefs_tracer_set(inst, t);
47 err_msg("Oops, error disabling tracer\n");
51 * create_instance - create a trace instance with *instance_name
53 struct tracefs_instance *create_instance(char *instance_name)
55 return tracefs_instance_create(instance_name);
59 * destroy_instance - remove a trace instance and free the data
61 void destroy_instance(struct tracefs_instance *inst)
63 tracefs_instance_destroy(inst);
64 tracefs_instance_free(inst);
68 * save_trace_to_file - save the trace output of the instance to the file
70 int save_trace_to_file(struct tracefs_instance *inst, const char *filename)
72 const char *file = "trace";
78 in_fd = tracefs_instance_file_open(inst, file, O_RDONLY);
80 err_msg("Failed to open trace file\n");
84 out_fd = creat(filename, mode);
86 err_msg("Failed to create output file %s\n", filename);
91 retval = read(in_fd, buffer, sizeof(buffer));
95 retval = write(out_fd, buffer, retval);
109 * collect_registered_events - call the existing callback function for the event
111 * If an event has a registered callback function, call it.
112 * Otherwise, ignore the event.
115 collect_registered_events(struct tep_event *event, struct tep_record *record,
116 int cpu, void *context)
118 struct trace_instance *trace = context;
119 struct trace_seq *s = trace->seq;
124 event->handler(s, record, event, context);
130 * trace_instance_destroy - destroy and free a rtla trace instance
132 void trace_instance_destroy(struct trace_instance *trace)
135 disable_tracer(trace->inst);
136 destroy_instance(trace->inst);
143 tep_free(trace->tep);
147 * trace_instance_init - create an rtla trace instance
149 * It is more than the tracefs instance, as it contains other
150 * things required for the tracing, such as the local events and
153 * Note that the trace instance is returned disabled. This allows
154 * the tool to apply some other configs, like setting priority
155 * to the kernel threads, before starting generating trace entries.
157 int trace_instance_init(struct trace_instance *trace, char *tool_name)
159 trace->seq = calloc(1, sizeof(*trace->seq));
163 trace_seq_init(trace->seq);
165 trace->inst = create_instance(tool_name);
169 trace->tep = tracefs_local_events(NULL);
174 * Let the main enable the record after setting some other
175 * things such as the priority of the tracer's threads.
177 tracefs_trace_off(trace->inst);
182 trace_instance_destroy(trace);
187 * trace_instance_start - start tracing a given rtla instance
189 int trace_instance_start(struct trace_instance *trace)
191 return tracefs_trace_on(trace->inst);