probe.hh revision 10365:e2c43045a81b
1/*
2 * Copyright (c) 2013 ARM Limited
3 * All rights reserved
4 *
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.
13 *
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.
24 *
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.
36 *
37 * Authors: Matt Horsnell
38  */
39
40/**
41 * @file This file describes the base components used for the probe system.
42 * There are currently 3 components:
43 *
44 * ProbePoint:          an event probe point i.e. send a notify from the point
45 *                      at which an instruction was committed.
46 *
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.
50 *
51 * ProbeListenerObject: a wrapper around a SimObject that can connect to another
52 *                      SimObject on which is will add ProbeListeners.
53 *
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.
59 */
60
61#ifndef __SIM_PROBE_PROBE_HH__
62#define __SIM_PROBE_PROBE_HH__
63
64#include <string>
65#include <vector>
66
67#include "base/trace.hh"
68#include "params/ProbeListenerObject.hh"
69#include "sim/sim_object.hh"
70
71/** Forward declare the ProbeManager. */
72class ProbeManager;
73class ProbeListener;
74
75/**
76 * This class is a minimal wrapper around SimObject. It is used to declare
77 * a python derived object that can be added as a ProbeListener to any other
78 * SimObject.
79 *
80 * It instantiates manager from a call to Parent.any.
81 * The vector of listeners is used simply to hold onto listeners until the
82 * ProbeListenerObject is destroyed.
83 */
84class ProbeListenerObject : public SimObject
85{
86  protected:
87    ProbeManager *manager;
88    std::vector<ProbeListener *> listeners;
89
90  public:
91    ProbeListenerObject(const ProbeListenerObjectParams *params);
92    virtual ~ProbeListenerObject();
93    ProbeManager* getProbeManager() { return manager; }
94};
95
96/**
97 * ProbeListener base class; here to simplify things like containers
98 * containing multiple types of ProbeListener.
99 *
100 * Note a ProbeListener is added to the ProbePoint in constructor by
101 * using the ProbeManager passed in.
102 */
103class ProbeListener
104{
105  public:
106    ProbeListener(ProbeManager *manager, const std::string &name);
107    virtual ~ProbeListener();
108
109  protected:
110    ProbeManager *const manager;
111    const std::string name;
112};
113
114/**
115 * ProbeListener base class; again used to simplify use of ProbePoints
116 * in containers and used as to define interface for adding removing
117 * listeners to the ProbePoint.
118 */
119class ProbePoint
120{
121  protected:
122    const std::string name;
123  public:
124    ProbePoint(ProbeManager *manager, const std::string &name);
125    virtual ~ProbePoint() {}
126
127    virtual void addListener(ProbeListener *listener) = 0;
128    virtual void removeListener(ProbeListener *listener) = 0;
129    std::string getName() const { return name; }
130};
131
132/**
133 * ProbeManager is a conduit class that lives on each SimObject,
134 *  and is used to match up probe listeners with probe points.
135 */
136class ProbeManager
137{
138  private:
139    /** Required for sensible debug messages.*/
140    const M5_CLASS_VAR_USED SimObject *object;
141    /** Vector for name look-up. */
142    std::vector<ProbePoint *> points;
143
144  public:
145    ProbeManager(SimObject *obj)
146        : object(obj)
147    {}
148    virtual ~ProbeManager() {}
149
150    /**
151     * @brief Add a ProbeListener to the ProbePoint named by pointName.
152     *        If the name doesn't resolve a ProbePoint return false.
153     * @param pointName the name of the ProbePoint to add the ProbeListener to.
154     * @param listener the ProbeListener to add.
155     * @return true if added, false otherwise.
156     */
157    bool addListener(std::string pointName, ProbeListener &listener);
158
159    /**
160     * @brief Remove a ProbeListener from the ProbePoint named by pointName.
161     *        If the name doesn't resolve a ProbePoint return false.
162     * @param pointName the name of the ProbePoint to remove the ProbeListener
163     *        from.
164     * @param listener the ProbeListener to remove.
165     * @return true if removed, false otherwise.
166     */
167    bool removeListener(std::string pointName, ProbeListener &listener);
168
169    /**
170     * @brief Add a ProbePoint to this SimObject ProbeManager.
171     * @param point the ProbePoint to add.
172     */
173    void addPoint(ProbePoint &point);
174};
175
176/**
177 * ProbeListenerArgBase is used to define the base interface to a
178 * ProbeListenerArg (i.e the notify method on specific type).
179 *
180 * It is necessary to split this out from ProbeListenerArg, as that
181 * templates off the class containing the function that notify calls.
182 */
183template <class Arg>
184class ProbeListenerArgBase : public ProbeListener
185{
186  public:
187    ProbeListenerArgBase(ProbeManager *pm, const std::string &name)
188        : ProbeListener(pm, name)
189    {}
190    virtual void notify(const Arg &val) = 0;
191};
192
193/**
194 * ProbeListenerArg generates a listener for the class of Arg and the
195 * class type T which is the class containing the function that notify will
196 * call.
197 *
198 * Note that the function is passed as a pointer on construction.
199 */
200template <class T, class Arg>
201class ProbeListenerArg : public ProbeListenerArgBase<Arg>
202{
203  private:
204    T *object;
205    void (T::* function)(const Arg &);
206
207  public:
208    /**
209     * @param obj the class of type Tcontaining the method to call on notify.
210     * @param name the name of the ProbePoint to add this listener to.
211     * @param func a pointer to the function on obj (called on notify).
212     */
213    ProbeListenerArg(T *obj, const std::string &name, void (T::* func)(const Arg &))
214        : ProbeListenerArgBase<Arg>(obj->getProbeManager(), name),
215          object(obj),
216          function(func)
217    {}
218
219    /**
220     * @brief called when the ProbePoint calls notify. This is a shim through to
221     *        the function passed during construction.
222     * @param val the argument value to pass.
223     */
224    virtual void notify(const Arg &val) { (object->*function)(val); }
225};
226
227/**
228 * ProbePointArg generates a point for the class of Arg. As ProbePointArgs talk
229 * directly to ProbeListenerArgs of the same type, we can store the vector of
230 * ProbeListeners as their Arg type (and not as base type).
231 *
232 * Methods are provided to addListener, removeListener and notify.
233 */
234template <typename Arg>
235class ProbePointArg : public ProbePoint
236{
237    /** The attached listeners. */
238    std::vector<ProbeListenerArgBase<Arg> *> listeners;
239
240  public:
241    ProbePointArg(ProbeManager *manager, std::string name)
242        : ProbePoint(manager, name)
243    {
244    }
245
246    /**
247     * @brief adds a ProbeListener to this ProbePoints notify list.
248     * @param l the ProbeListener to add to the notify list.
249     */
250    void addListener(ProbeListener *l)
251    {
252        // check listener not already added
253        if (std::find(listeners.begin(), listeners.end(), l) == listeners.end()) {
254            listeners.push_back(static_cast<ProbeListenerArgBase<Arg> *>(l));
255        }
256    }
257
258    /**
259     * @brief remove a ProbeListener from this ProbePoints notify list.
260     * @param l the ProbeListener to remove from the notify list.
261     */
262    void removeListener(ProbeListener *l)
263    {
264        listeners.erase(std::remove(listeners.begin(), listeners.end(), l),
265                        listeners.end());
266    }
267
268    /**
269     * @brief called at the ProbePoint call site, passes arg to each listener.
270     * @param arg the argument to pass to each listener.
271     */
272    void notify(const Arg &arg)
273    {
274        for (auto l = listeners.begin(); l != listeners.end(); ++l) {
275            (*l)->notify(arg);
276        }
277    }
278};
279#endif//__SIM_PROBE_PROBE_HH__
280