xref: /openbmc/qemu/trace/control-target.c (revision d40d3da0)
1 /*
2  * Interface for configuring and controlling the state of tracing events.
3  *
4  * Copyright (C) 2014-2017 Lluís Vilanova <vilanova@ac.upc.edu>
5  *
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.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "cpu.h"
12 #include "trace-root.h"
13 #include "trace/control.h"
14 #include "translate-all.h"
15 
16 
17 void trace_event_set_state_dynamic_init(TraceEvent *ev, bool state)
18 {
19     bool state_pre;
20     assert(trace_event_get_state_static(ev));
21     /*
22      * We ignore the "vcpu" property here, since no vCPUs have been created
23      * yet. Then dstate can only be 1 or 0.
24      */
25     state_pre = *ev->dstate;
26     if (state_pre != state) {
27         if (state) {
28             trace_events_enabled_count++;
29             *ev->dstate = 1;
30         } else {
31             trace_events_enabled_count--;
32             *ev->dstate = 0;
33         }
34     }
35 }
36 
37 void trace_event_set_state_dynamic(TraceEvent *ev, bool state)
38 {
39     CPUState *vcpu;
40     assert(trace_event_get_state_static(ev));
41     if (trace_event_is_vcpu(ev) && likely(first_cpu != NULL)) {
42         CPU_FOREACH(vcpu) {
43             trace_event_set_vcpu_state_dynamic(vcpu, ev, state);
44         }
45     } else {
46         /*
47          * Without the "vcpu" property, dstate can only be 1 or 0. With it, we
48          * haven't instantiated any vCPU yet, so we will set a global state
49          * instead, and trace_init_vcpu will reconcile it afterwards.
50          */
51         bool state_pre = *ev->dstate;
52         if (state_pre != state) {
53             if (state) {
54                 trace_events_enabled_count++;
55                 *ev->dstate = 1;
56             } else {
57                 trace_events_enabled_count--;
58                 *ev->dstate = 0;
59             }
60         }
61     }
62 }
63 
64 void trace_event_set_vcpu_state_dynamic(CPUState *vcpu,
65                                         TraceEvent *ev, bool state)
66 {
67     uint32_t vcpu_id;
68     bool state_pre;
69     assert(trace_event_get_state_static(ev));
70     assert(trace_event_is_vcpu(ev));
71     vcpu_id = trace_event_get_vcpu_id(ev);
72     state_pre = test_bit(vcpu_id, vcpu->trace_dstate);
73     if (state_pre != state) {
74         if (state) {
75             trace_events_enabled_count++;
76             set_bit(vcpu_id, vcpu->trace_dstate);
77             (*ev->dstate)++;
78         } else {
79             trace_events_enabled_count--;
80             clear_bit(vcpu_id, vcpu->trace_dstate);
81             (*ev->dstate)--;
82         }
83     }
84 }
85 
86 static bool adding_first_cpu1(void)
87 {
88     CPUState *cpu;
89     size_t count = 0;
90     CPU_FOREACH(cpu) {
91         count++;
92         if (count > 1) {
93             return false;
94         }
95     }
96     return true;
97 }
98 
99 static bool adding_first_cpu(void)
100 {
101     bool res;
102     cpu_list_lock();
103     res = adding_first_cpu1();
104     cpu_list_unlock();
105     return res;
106 }
107 
108 void trace_init_vcpu(CPUState *vcpu)
109 {
110     TraceEventIter iter;
111     TraceEvent *ev;
112     trace_event_iter_init(&iter, NULL);
113     while ((ev = trace_event_iter_next(&iter)) != NULL) {
114         if (trace_event_is_vcpu(ev) &&
115             trace_event_get_state_static(ev) &&
116             trace_event_get_state_dynamic(ev)) {
117             if (adding_first_cpu()) {
118                 /* check preconditions */
119                 assert(*ev->dstate == 1);
120                 /* disable early-init state ... */
121                 *ev->dstate = 0;
122                 trace_events_enabled_count--;
123                 /* ... and properly re-enable */
124                 trace_event_set_vcpu_state_dynamic(vcpu, ev, true);
125             } else {
126                 trace_event_set_vcpu_state_dynamic(vcpu, ev, true);
127             }
128         }
129     }
130     trace_guest_cpu_enter(vcpu);
131 }
132