// SPDX-License-Identifier: GPL-2.01/*2* trace helpers.3*4* Copyright (C) 2022 Red Hat Inc, Daniel Bristot de Oliveira <[email protected]>5*/67#include <sys/sendfile.h>8#include <tracefs.h>9#include <signal.h>10#include <stdlib.h>11#include <unistd.h>12#include <errno.h>1314#include <rv.h>15#include <trace.h>16#include <utils.h>1718/*19* create_instance - create a trace instance with *instance_name20*/21static struct tracefs_instance *create_instance(char *instance_name)22{23return tracefs_instance_create(instance_name);24}2526/*27* destroy_instance - remove a trace instance and free the data28*/29static void destroy_instance(struct tracefs_instance *inst)30{31tracefs_instance_destroy(inst);32tracefs_instance_free(inst);33}3435/**36* collect_registered_events - call the existing callback function for the event37*38* If an event has a registered callback function, call it.39* Otherwise, ignore the event.40*41* Returns 0 if the event was collected, 1 if the tool should stop collecting trace.42*/43int44collect_registered_events(struct tep_event *event, struct tep_record *record,45int cpu, void *context)46{47struct trace_instance *trace = context;48struct trace_seq *s = trace->seq;4950if (should_stop())51return 1;5253if (!event->handler)54return 0;5556event->handler(s, record, event, context);5758return 0;59}6061/**62* trace_instance_destroy - destroy and free a rv trace instance63*/64void trace_instance_destroy(struct trace_instance *trace)65{66if (trace->inst) {67destroy_instance(trace->inst);68trace->inst = NULL;69}7071if (trace->seq) {72free(trace->seq);73trace->seq = NULL;74}7576if (trace->tep) {77tep_free(trace->tep);78trace->tep = NULL;79}80}8182/**83* trace_instance_init - create a trace instance84*85* It is more than the tracefs instance, as it contains other86* things required for the tracing, such as the local events and87* a seq file.88*89* Note that the trace instance is returned disabled. This allows90* the tool to apply some other configs, like setting priority91* to the kernel threads, before starting generating trace entries.92*93* Returns 0 on success, non-zero otherwise.94*/95int trace_instance_init(struct trace_instance *trace, char *name)96{97trace->seq = calloc(1, sizeof(*trace->seq));98if (!trace->seq)99goto out_err;100101trace_seq_init(trace->seq);102103trace->inst = create_instance(name);104if (!trace->inst)105goto out_err;106107trace->tep = tracefs_local_events(NULL);108if (!trace->tep)109goto out_err;110111/*112* Let the main enable the record after setting some other113* things such as the priority of the tracer's threads.114*/115tracefs_trace_off(trace->inst);116117return 0;118119out_err:120trace_instance_destroy(trace);121return 1;122}123124/**125* trace_instance_start - start tracing a given rv instance126*127* Returns 0 on success, -1 otherwise.128*/129int trace_instance_start(struct trace_instance *trace)130{131return tracefs_trace_on(trace->inst);132}133134135