提交 0d4e995c 编写于 作者: D Daniel P. Berrange 提交者: Stefan Hajnoczi

trace: convert code to use event iterators

This converts the HMP/QMP monitor API implementations
and some internal trace control methods to use the new
trace event iterator APIs.
Reviewed-by: NStefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: NDaniel P. Berrange <berrange@redhat.com>
Reviewed-by: NLluís Vilanova <vilanova@ac.upc.edu>
Message-id: 1475588159-30598-5-git-send-email-berrange@redhat.com
Signed-off-by: NStefan Hajnoczi <stefanha@redhat.com>
上级 6a1b0f3a
...@@ -3330,13 +3330,14 @@ void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *st ...@@ -3330,13 +3330,14 @@ void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *st
len = strlen(str); len = strlen(str);
readline_set_completion_index(rs, len); readline_set_completion_index(rs, len);
if (nb_args == 2) { if (nb_args == 2) {
TraceEventID id; TraceEventIter iter;
for (id = 0; id < trace_event_count(); id++) { TraceEvent *ev;
const char *event_name = trace_event_get_name(trace_event_id(id)); char *pattern = g_strdup_printf("%s*", str);
if (!strncmp(str, event_name, len)) { trace_event_iter_init(&iter, pattern);
readline_add_completion(rs, event_name); while ((ev = trace_event_iter_next(&iter)) != NULL) {
} readline_add_completion(rs, trace_event_get_name(ev));
} }
g_free(pattern);
} }
} }
...@@ -3347,13 +3348,14 @@ void trace_event_completion(ReadLineState *rs, int nb_args, const char *str) ...@@ -3347,13 +3348,14 @@ void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
len = strlen(str); len = strlen(str);
readline_set_completion_index(rs, len); readline_set_completion_index(rs, len);
if (nb_args == 2) { if (nb_args == 2) {
TraceEventID id; TraceEventIter iter;
for (id = 0; id < trace_event_count(); id++) { TraceEvent *ev;
const char *event_name = trace_event_get_name(trace_event_id(id)); char *pattern = g_strdup_printf("%s*", str);
if (!strncmp(str, event_name, len)) { trace_event_iter_init(&iter, pattern);
readline_add_completion(rs, event_name); while ((ev = trace_event_iter_next(&iter)) != NULL) {
} readline_add_completion(rs, trace_event_get_name(ev));
} }
g_free(pattern);
} else if (nb_args == 3) { } else if (nb_args == 3) {
add_completion_option(rs, str, "on"); add_completion_option(rs, str, "on");
add_completion_option(rs, str, "off"); add_completion_option(rs, str, "off");
......
...@@ -98,9 +98,10 @@ static bool adding_first_cpu(void) ...@@ -98,9 +98,10 @@ static bool adding_first_cpu(void)
void trace_init_vcpu(CPUState *vcpu) void trace_init_vcpu(CPUState *vcpu)
{ {
TraceEvent *ev = NULL; TraceEventIter iter;
TraceEvent *ev;
while ((ev = trace_event_pattern("*", ev)) != NULL) { trace_event_iter_init(&iter, NULL);
while ((ev = trace_event_iter_next(&iter)) != NULL) {
if (trace_event_is_vcpu(ev) && if (trace_event_is_vcpu(ev) &&
trace_event_get_state_static(ev) && trace_event_get_state_static(ev) &&
trace_event_get_state_dynamic(ev)) { trace_event_get_state_dynamic(ev)) {
...@@ -118,6 +119,5 @@ void trace_init_vcpu(CPUState *vcpu) ...@@ -118,6 +119,5 @@ void trace_init_vcpu(CPUState *vcpu)
} }
} }
} }
trace_guest_cpu_enter(vcpu); trace_guest_cpu_enter(vcpu);
} }
...@@ -60,9 +60,10 @@ TraceEvent *trace_event_name(const char *name) ...@@ -60,9 +60,10 @@ TraceEvent *trace_event_name(const char *name)
{ {
assert(name != NULL); assert(name != NULL);
TraceEventID i; TraceEventIter iter;
for (i = 0; i < trace_event_count(); i++) { TraceEvent *ev;
TraceEvent *ev = trace_event_id(i); trace_event_iter_init(&iter, NULL);
while ((ev = trace_event_iter_next(&iter)) != NULL) {
if (strcmp(trace_event_get_name(ev), name) == 0) { if (strcmp(trace_event_get_name(ev), name) == 0) {
return ev; return ev;
} }
...@@ -105,21 +106,18 @@ TraceEvent *trace_event_pattern(const char *pat, TraceEvent *ev) ...@@ -105,21 +106,18 @@ TraceEvent *trace_event_pattern(const char *pat, TraceEvent *ev)
{ {
assert(pat != NULL); assert(pat != NULL);
TraceEventID i; bool matched = ev ? false : true;
TraceEventIter iter;
if (ev == NULL) { TraceEvent *thisev;
i = -1; trace_event_iter_init(&iter, pat);
} else { while ((thisev = trace_event_iter_next(&iter)) != NULL) {
i = trace_event_get_id(ev); if (matched) {
} return thisev;
i++; } else {
if (ev == thisev) {
while (i < trace_event_count()) { matched = true;
TraceEvent *res = trace_event_id(i); }
if (pattern_glob(pat, trace_event_get_name(res))) {
return res;
} }
i++;
} }
return NULL; return NULL;
...@@ -148,10 +146,11 @@ TraceEvent *trace_event_iter_next(TraceEventIter *iter) ...@@ -148,10 +146,11 @@ TraceEvent *trace_event_iter_next(TraceEventIter *iter)
void trace_list_events(void) void trace_list_events(void)
{ {
int i; TraceEventIter iter;
for (i = 0; i < trace_event_count(); i++) { TraceEvent *ev;
TraceEvent *res = trace_event_id(i); trace_event_iter_init(&iter, NULL);
fprintf(stderr, "%s\n", trace_event_get_name(res)); while ((ev = trace_event_iter_next(&iter)) != NULL) {
fprintf(stderr, "%s\n", trace_event_get_name(ev));
} }
} }
...@@ -159,26 +158,32 @@ static void do_trace_enable_events(const char *line_buf) ...@@ -159,26 +158,32 @@ static void do_trace_enable_events(const char *line_buf)
{ {
const bool enable = ('-' != line_buf[0]); const bool enable = ('-' != line_buf[0]);
const char *line_ptr = enable ? line_buf : line_buf + 1; const char *line_ptr = enable ? line_buf : line_buf + 1;
TraceEventIter iter;
TraceEvent *ev;
bool is_pattern = trace_event_is_pattern(line_ptr);
if (trace_event_is_pattern(line_ptr)) { trace_event_iter_init(&iter, line_ptr);
TraceEvent *ev = NULL; while ((ev = trace_event_iter_next(&iter)) != NULL) {
while ((ev = trace_event_pattern(line_ptr, ev)) != NULL) { if (!trace_event_get_state_static(ev)) {
if (trace_event_get_state_static(ev)) { if (!is_pattern) {
trace_event_set_state_dynamic_init(ev, enable); error_report("WARNING: trace event '%s' is not traceable",
line_ptr);
return;
} }
continue;
} }
} else {
TraceEvent *ev = trace_event_name(line_ptr); /* start tracing */
if (ev == NULL) { trace_event_set_state_dynamic(ev, enable);
error_report("WARNING: trace event '%s' does not exist", if (!is_pattern) {
line_ptr); return;
} else if (!trace_event_get_state_static(ev)) {
error_report("WARNING: trace event '%s' is not traceable",
line_ptr);
} else {
trace_event_set_state_dynamic_init(ev, enable);
} }
} }
if (!is_pattern) {
error_report("WARNING: trace event '%s' does not exist",
line_ptr);
}
} }
void trace_enable_events(const char *line_buf) void trace_enable_events(const char *line_buf)
......
...@@ -52,8 +52,10 @@ static bool check_events(bool has_vcpu, bool ignore_unavailable, bool is_pattern ...@@ -52,8 +52,10 @@ static bool check_events(bool has_vcpu, bool ignore_unavailable, bool is_pattern
return true; return true;
} else { } else {
/* error for unavailable events */ /* error for unavailable events */
TraceEvent *ev = NULL; TraceEventIter iter;
while ((ev = trace_event_pattern(name, ev)) != NULL) { TraceEvent *ev;
trace_event_iter_init(&iter, name);
while ((ev = trace_event_iter_next(&iter)) != NULL) {
if (!ignore_unavailable && !trace_event_get_state_static(ev)) { if (!ignore_unavailable && !trace_event_get_state_static(ev)) {
error_setg(errp, "event \"%s\" is disabled", trace_event_get_name(ev)); error_setg(errp, "event \"%s\" is disabled", trace_event_get_name(ev));
return false; return false;
...@@ -69,6 +71,7 @@ TraceEventInfoList *qmp_trace_event_get_state(const char *name, ...@@ -69,6 +71,7 @@ TraceEventInfoList *qmp_trace_event_get_state(const char *name,
{ {
Error *err = NULL; Error *err = NULL;
TraceEventInfoList *events = NULL; TraceEventInfoList *events = NULL;
TraceEventIter iter;
TraceEvent *ev; TraceEvent *ev;
bool is_pattern = trace_event_is_pattern(name); bool is_pattern = trace_event_is_pattern(name);
CPUState *cpu; CPUState *cpu;
...@@ -86,8 +89,8 @@ TraceEventInfoList *qmp_trace_event_get_state(const char *name, ...@@ -86,8 +89,8 @@ TraceEventInfoList *qmp_trace_event_get_state(const char *name,
} }
/* Get states (all errors checked above) */ /* Get states (all errors checked above) */
ev = NULL; trace_event_iter_init(&iter, name);
while ((ev = trace_event_pattern(name, ev)) != NULL) { while ((ev = trace_event_iter_next(&iter)) != NULL) {
TraceEventInfoList *elem; TraceEventInfoList *elem;
bool is_vcpu = trace_event_is_vcpu(ev); bool is_vcpu = trace_event_is_vcpu(ev);
if (has_vcpu && !is_vcpu) { if (has_vcpu && !is_vcpu) {
...@@ -132,6 +135,7 @@ void qmp_trace_event_set_state(const char *name, bool enable, ...@@ -132,6 +135,7 @@ void qmp_trace_event_set_state(const char *name, bool enable,
Error **errp) Error **errp)
{ {
Error *err = NULL; Error *err = NULL;
TraceEventIter iter;
TraceEvent *ev; TraceEvent *ev;
bool is_pattern = trace_event_is_pattern(name); bool is_pattern = trace_event_is_pattern(name);
CPUState *cpu; CPUState *cpu;
...@@ -150,8 +154,8 @@ void qmp_trace_event_set_state(const char *name, bool enable, ...@@ -150,8 +154,8 @@ void qmp_trace_event_set_state(const char *name, bool enable,
} }
/* Apply changes (all errors checked above) */ /* Apply changes (all errors checked above) */
ev = NULL; trace_event_iter_init(&iter, name);
while ((ev = trace_event_pattern(name, ev)) != NULL) { while ((ev = trace_event_iter_next(&iter)) != NULL) {
if (!trace_event_get_state_static(ev) || if (!trace_event_get_state_static(ev) ||
(has_vcpu && !trace_event_is_vcpu(ev))) { (has_vcpu && !trace_event_is_vcpu(ev))) {
continue; continue;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册