2 * Interface for configuring and controlling the state of tracing events.
4 * Copyright (C) 2011-2012 LluĂs Vilanova <vilanova@ac.upc.edu>
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
10 #include "trace/control.h"
13 TraceEvent *trace_event_name(const char *name)
18 for (i = 0; i < trace_event_count(); i++) {
19 TraceEvent *ev = trace_event_id(i);
20 if (strcmp(trace_event_get_name(ev), name) == 0) {
27 static bool pattern_glob(const char *pat, const char *ev)
29 while (*pat != '\0' && *ev != '\0') {
34 else if (*pat == '*') {
35 if (pattern_glob(pat, ev+1)) {
37 } else if (pattern_glob(pat+1, ev)) {
51 if (*pat == '\0' && *ev == '\0') {
58 TraceEvent *trace_event_pattern(const char *pat, TraceEvent *ev)
67 i = trace_event_get_id(ev);
71 while (i < trace_event_count()) {
72 TraceEvent *res = trace_event_id(i);
73 if (pattern_glob(pat, trace_event_get_name(res))) {
82 void trace_backend_init_events(const char *fname)
88 FILE *fp = fopen(fname, "r");
90 fprintf(stderr, "error: could not open trace events file '%s': %s\n",
91 fname, strerror(errno));
95 while (fgets(line_buf, sizeof(line_buf), fp)) {
96 size_t len = strlen(line_buf);
97 if (len > 1) { /* skip empty lines */
98 line_buf[len - 1] = '\0';
99 if ('#' == line_buf[0]) { /* skip commented lines */
102 const bool enable = ('-' != line_buf[0]);
103 char *line_ptr = enable ? line_buf : line_buf + 1;
104 if (trace_event_is_pattern(line_ptr)) {
105 TraceEvent *ev = NULL;
106 while ((ev = trace_event_pattern(line_ptr, ev)) != NULL) {
107 if (trace_event_get_state_static(ev)) {
108 trace_event_set_state_dynamic(ev, enable);
112 TraceEvent *ev = trace_event_name(line_ptr);
115 "error: trace event '%s' does not exist\n", line_ptr);
118 if (!trace_event_get_state_static(ev)) {
120 "error: trace event '%s' is not traceable\n", line_ptr);
123 trace_event_set_state_dynamic(ev, enable);
127 if (fclose(fp) != 0) {
128 fprintf(stderr, "error: closing file '%s': %s\n",
129 fname, strerror(errno));