From 23d15e860b33819ad76092fbb32577542fe0c44d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Llu=C3=ADs?= Date: Wed, 31 Aug 2011 20:31:31 +0200 Subject: trace: add "-trace events" argument to control initial state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The "-trace events" argument can be used to provide a file with a list of trace event names that will be enabled prior to starting execution, thus providing early tracing. This saves the user from manually toggling event states through the monitor interface or whichever backend-specific interface. Signed-off-by: Lluís Vilanova --- trace/control.c | 42 ++++++++++++++++++++++++++++++++++++++++++ trace/control.h | 14 +++++++++++--- trace/default.c | 7 ++++++- trace/simple.c | 3 ++- 4 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 trace/control.c (limited to 'trace') diff --git a/trace/control.c b/trace/control.c new file mode 100644 index 0000000000..4c5527d20a --- /dev/null +++ b/trace/control.c @@ -0,0 +1,42 @@ +/* + * Interface for configuring and controlling the state of tracing events. + * + * Copyright (C) 2011 Lluís Vilanova + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + */ + +#include "trace/control.h" + + +void trace_backend_init_events(const char *fname) +{ + if (fname == NULL) { + return; + } + + FILE *fp = fopen(fname, "r"); + if (!fp) { + fprintf(stderr, "error: could not open trace events file '%s': %s\n", + fname, strerror(errno)); + exit(1); + } + char line_buf[1024]; + while (fgets(line_buf, sizeof(line_buf), fp)) { + size_t len = strlen(line_buf); + if (len > 1) { /* skip empty lines */ + line_buf[len - 1] = '\0'; + if (!trace_event_set_state(line_buf, true)) { + fprintf(stderr, + "error: trace event '%s' does not exist\n", line_buf); + exit(1); + } + } + } + if (fclose(fp) != 0) { + fprintf(stderr, "error: closing file '%s': %s\n", + fname, strerror(errno)); + exit(1); + } +} diff --git a/trace/control.h b/trace/control.h index c99b4d50a8..2acaa4290e 100644 --- a/trace/control.h +++ b/trace/control.h @@ -24,10 +24,18 @@ bool trace_event_set_state(const char *name, bool state); /** Initialize the tracing backend. * - * @file Name of trace output file; may be NULL. - * Corresponds to commandline option "-trace file=...". + * @events Name of file with events to be enabled at startup; may be NULL. + * Corresponds to commandline option "-trace events=...". + * @file Name of trace output file; may be NULL. + * Corresponds to commandline option "-trace file=...". * @return Whether the backend could be successfully initialized. */ -bool trace_backend_init(const char *file); +bool trace_backend_init(const char *events, const char *file); + +/** Generic function to initialize the state of events. + * + * @fname Name of file with events to enable; may be NULL. + */ +void trace_backend_init_events(const char *fname); #endif /* TRACE_CONTROL_H */ diff --git a/trace/default.c b/trace/default.c index 3573d5b296..c9b27a289b 100644 --- a/trace/default.c +++ b/trace/default.c @@ -25,8 +25,13 @@ bool trace_event_set_state(const char *name, bool state) return false; } -bool trace_backend_init(const char *file) +bool trace_backend_init(const char *events, const char *file) { + if (events) { + fprintf(stderr, "error: -trace events=...: " + "option not supported by the selected tracing backend\n"); + return false; + } if (file) { fprintf(stderr, "error: -trace file=...: " "option not supported by the selected tracing backend\n"); diff --git a/trace/simple.c b/trace/simple.c index 70689e9484..a6093682dd 100644 --- a/trace/simple.c +++ b/trace/simple.c @@ -331,7 +331,7 @@ bool trace_event_set_state(const char *name, bool state) return false; } -bool trace_backend_init(const char *file) +bool trace_backend_init(const char *events, const char *file) { pthread_t thread; pthread_attr_t attr; @@ -350,6 +350,7 @@ bool trace_backend_init(const char *file) fprintf(stderr, "warning: unable to initialize simple trace backend\n"); } else { atexit(st_flush_trace_buffer); + trace_backend_init_events(events); st_set_trace_file(file); } -- cgit v1.2.1