2 * Copyright (c) 2013 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Authors: Matt Horsnell
41 * @file This file describes the base components used for the probe system.
42 * There are currently 3 components:
44 * ProbePoint: an event probe point i.e. send a notify from the point
45 * at which an instruction was committed.
47 * ProbeListener: a listener provide a notify method that is called when
48 * a probe point event occurs. Multiple ProbeListeners
49 * can be added to each ProbePoint.
51 * ProbeListenerObject: a wrapper around a SimObject that can connect to another
52 * SimObject on which is will add ProbeListeners.
54 * ProbeManager: used to match up ProbeListeners and ProbePoints.
55 * At <b>simulation init</b> this is handled by regProbePoints
56 * followed by regProbeListeners being called on each
57 * SimObject in hierarchical ordering.
58 * ProbeListeners can be added/removed dynamically at runtime.
61 #ifndef __SIM_PROBE_PROBE_HH__
62 #define __SIM_PROBE_PROBE_HH__
67 #include "base/trace.hh"
68 #include "params/ProbeListenerObject.hh"
69 #include "sim/sim_object.hh"
71 /** Forward declare the ProbeManager. */
76 * Name space containing shared probe point declarations.
78 * Probe types that are shared between multiple types of SimObjects
79 * should live in this name space. This makes it possible to use a
80 * common instrumentation interface for devices such as PMUs that have
81 * different implementations in different ISAs.
83 namespace ProbePoints {
84 /* Note: This is only here for documentation purposes, new probe
85 * points should normally be declared in their own header files. See
91 * This class is a minimal wrapper around SimObject. It is used to declare
92 * a python derived object that can be added as a ProbeListener to any other
95 * It instantiates manager from a call to Parent.any.
96 * The vector of listeners is used simply to hold onto listeners until the
97 * ProbeListenerObject is destroyed.
99 class ProbeListenerObject : public SimObject
102 ProbeManager *manager;
103 std::vector<ProbeListener *> listeners;
106 ProbeListenerObject(const ProbeListenerObjectParams *params);
107 virtual ~ProbeListenerObject();
108 ProbeManager* getProbeManager() { return manager; }
112 * ProbeListener base class; here to simplify things like containers
113 * containing multiple types of ProbeListener.
115 * Note a ProbeListener is added to the ProbePoint in constructor by
116 * using the ProbeManager passed in.
121 ProbeListener(ProbeManager *manager, const std::string &name);
122 virtual ~ProbeListener();
125 ProbeManager *const manager;
126 const std::string name;
130 * ProbeListener base class; again used to simplify use of ProbePoints
131 * in containers and used as to define interface for adding removing
132 * listeners to the ProbePoint.
137 const std::string name;
139 ProbePoint(ProbeManager *manager, const std::string &name);
140 virtual ~ProbePoint() {}
142 virtual void addListener(ProbeListener *listener) = 0;
143 virtual void removeListener(ProbeListener *listener) = 0;
144 std::string getName() const { return name; }
148 * ProbeManager is a conduit class that lives on each SimObject,
149 * and is used to match up probe listeners with probe points.
154 /** Required for sensible debug messages.*/
155 const M5_CLASS_VAR_USED SimObject *object;
156 /** Vector for name look-up. */
157 std::vector<ProbePoint *> points;
160 ProbeManager(SimObject *obj)
163 virtual ~ProbeManager() {}
166 * @brief Add a ProbeListener to the ProbePoint named by pointName.
167 * If the name doesn't resolve a ProbePoint return false.
168 * @param pointName the name of the ProbePoint to add the ProbeListener to.
169 * @param listener the ProbeListener to add.
170 * @return true if added, false otherwise.
172 bool addListener(std::string pointName, ProbeListener &listener);
175 * @brief Remove a ProbeListener from the ProbePoint named by pointName.
176 * If the name doesn't resolve a ProbePoint return false.
177 * @param pointName the name of the ProbePoint to remove the ProbeListener
179 * @param listener the ProbeListener to remove.
180 * @return true if removed, false otherwise.
182 bool removeListener(std::string pointName, ProbeListener &listener);
185 * @brief Add a ProbePoint to this SimObject ProbeManager.
186 * @param point the ProbePoint to add.
188 void addPoint(ProbePoint &point);
192 * ProbeListenerArgBase is used to define the base interface to a
193 * ProbeListenerArg (i.e the notify method on specific type).
195 * It is necessary to split this out from ProbeListenerArg, as that
196 * templates off the class containing the function that notify calls.
199 class ProbeListenerArgBase : public ProbeListener
202 ProbeListenerArgBase(ProbeManager *pm, const std::string &name)
203 : ProbeListener(pm, name)
205 virtual void notify(const Arg &val) = 0;
209 * ProbeListenerArg generates a listener for the class of Arg and the
210 * class type T which is the class containing the function that notify will
213 * Note that the function is passed as a pointer on construction.
215 template <class T, class Arg>
216 class ProbeListenerArg : public ProbeListenerArgBase<Arg>
220 void (T::* function)(const Arg &);
224 * @param obj the class of type Tcontaining the method to call on notify.
225 * @param name the name of the ProbePoint to add this listener to.
226 * @param func a pointer to the function on obj (called on notify).
228 ProbeListenerArg(T *obj, const std::string &name, void (T::* func)(const Arg &))
229 : ProbeListenerArgBase<Arg>(obj->getProbeManager(), name),
235 * @brief called when the ProbePoint calls notify. This is a shim through to
236 * the function passed during construction.
237 * @param val the argument value to pass.
239 virtual void notify(const Arg &val) { (object->*function)(val); }
243 * ProbePointArg generates a point for the class of Arg. As ProbePointArgs talk
244 * directly to ProbeListenerArgs of the same type, we can store the vector of
245 * ProbeListeners as their Arg type (and not as base type).
247 * Methods are provided to addListener, removeListener and notify.
249 template <typename Arg>
250 class ProbePointArg : public ProbePoint
252 /** The attached listeners. */
253 std::vector<ProbeListenerArgBase<Arg> *> listeners;
256 ProbePointArg(ProbeManager *manager, std::string name)
257 : ProbePoint(manager, name)
262 * @brief adds a ProbeListener to this ProbePoints notify list.
263 * @param l the ProbeListener to add to the notify list.
265 void addListener(ProbeListener *l)
267 // check listener not already added
268 if (std::find(listeners.begin(), listeners.end(), l) == listeners.end()) {
269 listeners.push_back(static_cast<ProbeListenerArgBase<Arg> *>(l));
274 * @brief remove a ProbeListener from this ProbePoints notify list.
275 * @param l the ProbeListener to remove from the notify list.
277 void removeListener(ProbeListener *l)
279 listeners.erase(std::remove(listeners.begin(), listeners.end(), l),
284 * @brief called at the ProbePoint call site, passes arg to each listener.
285 * @param arg the argument to pass to each listener.
287 void notify(const Arg &arg)
289 for (auto l = listeners.begin(); l != listeners.end(); ++l) {
294 #endif//__SIM_PROBE_PROBE_HH__