xref: /openbmc/qemu/trace/trace-hmp-cmds.c (revision 21063bce)
1 /*
2  * HMP commands related to tracing
3  *
4  * Copyright (c) 2003-2004 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 #include "monitor/hmp.h"
27 #include "monitor/monitor.h"
28 #include "qapi/error.h"
29 #include "qapi/qapi-commands-trace.h"
30 #include "qapi/qmp/qdict.h"
31 #include "trace/control.h"
32 #ifdef CONFIG_TRACE_SIMPLE
33 #include "trace/simple.h"
34 #endif
35 
36 void hmp_trace_event(Monitor *mon, const QDict *qdict)
37 {
38     const char *tp_name = qdict_get_str(qdict, "name");
39     bool new_state = qdict_get_bool(qdict, "option");
40     bool has_vcpu = qdict_haskey(qdict, "vcpu");
41     int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
42     Error *local_err = NULL;
43 
44     if (vcpu < 0) {
45         monitor_printf(mon, "argument vcpu must be positive");
46         return;
47     }
48 
49     qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
50     if (local_err) {
51         error_report_err(local_err);
52     }
53 }
54 
55 #ifdef CONFIG_TRACE_SIMPLE
56 void hmp_trace_file(Monitor *mon, const QDict *qdict)
57 {
58     const char *op = qdict_get_try_str(qdict, "op");
59     const char *arg = qdict_get_try_str(qdict, "arg");
60 
61     if (!op) {
62         st_print_trace_file_status();
63     } else if (!strcmp(op, "on")) {
64         st_set_trace_file_enabled(true);
65     } else if (!strcmp(op, "off")) {
66         st_set_trace_file_enabled(false);
67     } else if (!strcmp(op, "flush")) {
68         st_flush_trace_buffer();
69     } else if (!strcmp(op, "set")) {
70         if (arg) {
71             st_set_trace_file(arg);
72         }
73     } else {
74         monitor_printf(mon, "unexpected argument \"%s\"\n", op);
75         hmp_help_cmd(mon, "trace-file");
76     }
77 }
78 #endif
79 
80 void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
81 {
82     const char *name = qdict_get_try_str(qdict, "name");
83     bool has_vcpu = qdict_haskey(qdict, "vcpu");
84     int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
85     TraceEventInfoList *events;
86     TraceEventInfoList *elem;
87     Error *local_err = NULL;
88 
89     if (name == NULL) {
90         name = "*";
91     }
92     if (vcpu < 0) {
93         monitor_printf(mon, "argument vcpu must be positive");
94         return;
95     }
96 
97     events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
98     if (local_err) {
99         error_report_err(local_err);
100         return;
101     }
102 
103     for (elem = events; elem != NULL; elem = elem->next) {
104         monitor_printf(mon, "%s : state %u\n",
105                        elem->value->name,
106                        elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
107     }
108     qapi_free_TraceEventInfoList(events);
109 }
110 
111 void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
112 {
113     size_t len;
114 
115     len = strlen(str);
116     readline_set_completion_index(rs, len);
117     if (nb_args == 2) {
118         TraceEventIter iter;
119         TraceEvent *ev;
120         char *pattern = g_strdup_printf("%s*", str);
121         trace_event_iter_init_pattern(&iter, pattern);
122         while ((ev = trace_event_iter_next(&iter)) != NULL) {
123             readline_add_completion(rs, trace_event_get_name(ev));
124         }
125         g_free(pattern);
126     }
127 }
128 
129 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
130 {
131     size_t len;
132 
133     len = strlen(str);
134     readline_set_completion_index(rs, len);
135     if (nb_args == 2) {
136         TraceEventIter iter;
137         TraceEvent *ev;
138         char *pattern = g_strdup_printf("%s*", str);
139         trace_event_iter_init_pattern(&iter, pattern);
140         while ((ev = trace_event_iter_next(&iter)) != NULL) {
141             readline_add_completion(rs, trace_event_get_name(ev));
142         }
143         g_free(pattern);
144     } else if (nb_args == 3) {
145         readline_add_completion_of(rs, str, "on");
146         readline_add_completion_of(rs, str, "off");
147     }
148 }
149