SST  15.1.0
StructuralSimulationToolkit
watchPoint.h
1 // Copyright 2009-2025 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2025, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef SST_CORE_WATCHPOINT_H
13 #define SST_CORE_WATCHPOINT_H
14 
15 #include "sst/core/clock.h"
16 #include "sst/core/event.h"
17 #include "sst/core/serialization/objectMap.h"
18 #include "sst/core/stringize.h"
19 
20 namespace SST {
21 
22 
23 /**
24  Class that can attach to Clock and Event Handlers to monitor the
25  state of variables
26  */
28 {
29 public:
30  static const uint32_t VMASK = 0x10; // see simpleDebug.h::VERBOSITY_MASK
31 
32  /**
33  Base class for performing comparisons and logic operations for
34  determining when the WatchPoint triggers
35  */
36  class Logic
37  {
38  public:
39  virtual bool check() = 0;
40  virtual ~Logic() = default;
41  }; // class Logic
42 
43  /**
44  WatchPoint Action Inner Classes
45  */
46  class WPAction
47  {
48  public:
49  WPAction() {}
50  virtual ~WPAction() = default;
51  virtual std::string actionToString() = 0;
52  virtual void invokeAction(WatchPoint* wp) = 0;
53  inline void setVerbosity(uint32_t v) { verbosity = v; }
54 
55  protected:
56  uint32_t verbosity = 0;
57  inline void msg(const std::string& msg)
58  {
59  if ( WatchPoint::VMASK & verbosity ) std::cout << msg << std::endl;
60  }
61  }; // class WPAction
62 
64  {
65  public:
67  virtual ~InteractiveWPAction() = default;
68  inline std::string actionToString() override { return "interactive"; }
69  void invokeAction(WatchPoint* wp) override;
70  }; // class InteractiveWPAction
71 
73  {
74  public:
76  virtual ~PrintTraceWPAction() = default;
77  inline std::string actionToString() override { return "printTrace"; }
78  void invokeAction(WatchPoint* wp) override;
79  }; // class PrintTraceWPAction
80 
82  {
83  public:
85  virtual ~CheckpointWPAction() = default;
86  inline std::string actionToString() override { return "checkpoint"; }
87  void invokeAction(WatchPoint* wp) override;
88  }; // class CheckpointWPAction
89 
91  {
92  public:
94  virtual ~PrintStatusWPAction() = default;
95  inline std::string actionToString() override { return "printStatus"; }
96  void invokeAction(WatchPoint* wp) override;
97  }; // class PrintStatusWPAction
98 
99  class SetVarWPAction : public WPAction
100  {
101  public:
102  SetVarWPAction(std::string vname, Core::Serialization::ObjectMap* obj, std::string tval) :
103  name_(vname),
104  obj_(obj),
105  valStr_(tval)
106  {}
107  virtual ~SetVarWPAction() = default;
108  inline std::string actionToString() override { return "set " + name_ + " " + valStr_; }
109  void invokeAction(WatchPoint* wp) override;
110 
111  private:
112  std::string name_ = "";
113  Core::Serialization::ObjectMap* obj_ = nullptr;
114  std::string valStr_ = "";
115  }; // class SetVarWPAction
116 
117  class ShutdownWPAction : public WPAction
118  {
119  public:
120  ShutdownWPAction() {}
121  virtual ~ShutdownWPAction() = default;
122  inline std::string actionToString() override { return "shutdown"; }
123  void invokeAction(WatchPoint* wp) override;
124  }; // class ShutdownWPAction
125 
126  // Construction
127  WatchPoint(size_t index, const std::string& name, Core::Serialization::ObjectMapComparison* obj);
128  ~WatchPoint();
129 
130  // Inherited from both Event and Clock handler AttachPoints.
131  // WatchPoint doesn't use the key, so just return 0
132  uintptr_t registerHandler(const AttachPointMetaData& UNUSED(mdata)) override { return 0; }
133 
134  // Functions inherited from Event::HandlerBase::AttachPoint
135  void beforeHandler(uintptr_t UNUSED(key), const Event* UNUSED(ev)) override;
136  void afterHandler(uintptr_t UNUSED(key)) override;
137 
138  // Functions inherited from Clock::HandlerBase::AttachPoint
139  void beforeHandler(uintptr_t UNUSED(key), const Cycle_t& UNUSED(cycle)) override;
140  void afterHandler(uintptr_t UNUSED(key), const bool& UNUSED(ret)) override;
141 
142  // Local
143  inline std::string getName() { return name_; }
144  size_t getBufferSize();
145  void printTriggerRecord();
146  void printTrace();
147 
148  enum HANDLER : unsigned {
149  // Select which handlers do check and sample
150  NONE = 0,
151  BEFORE_CLOCK = 1,
152  AFTER_CLOCK = 2,
153  BEFORE_EVENT = 4,
154  AFTER_EVENT = 8,
155  ALL = 15
156  };
157 
158  // TODO can we avoid code duplication in WatchPoint and the WatchPointAction?
159  inline void setVerbosity(uint32_t v)
160  {
161  verbosity = v;
162  wpAction->setVerbosity(v);
163  }
164  inline void msg(const std::string& msg)
165  {
166  if ( VMASK & verbosity ) std::cout << msg << std::endl;
167  }
168  void setHandler(unsigned handlerType);
169  std::string handlerToString(HANDLER h);
170  void printHandler();
171  void printWatchpoint();
172  void resetTraceBuffer();
173  inline bool checkReset() { return reset_; }
174  void printAction();
175  void addTraceBuffer(Core::Serialization::TraceBuffer* tb);
176  void addObjectBuffer(Core::Serialization::ObjectBuffer* ob);
177  void addComparison(Core::Serialization::ObjectMapComparison* cmp);
178 
179  enum LogicOp : unsigned { // Logical Op for trigger tests
180  AND = 0,
181  OR = 1,
182  UNDEFINED = 2
183  };
184  inline void addLogicOp(LogicOp op) { logicOps_.push_back(op); }
185  inline void setAction(WPAction* action) { wpAction = action; }
186 
187 protected:
188  bool getInteractive();
189  void setEnterInteractive();
190  void setInteractiveMsg(const std::string& msg);
191  SimTime_t getCurrentSimCycle();
192  void setCheckpoint();
193  void printStatus();
194  void heartbeat();
195  void simulationShutdown();
196 
197 private:
198  size_t numCmpObj_ = 0;
199  std::vector<Core::Serialization::ObjectMapComparison*> cmpObjects_;
200  std::vector<LogicOp> logicOps_;
201  std::string name_;
202  Core::Serialization::TraceBuffer* tb_ = nullptr;
203  size_t wpIndex;
204  HANDLER handler = ALL;
205  bool trigger = false;
206  HANDLER triggerHandler = HANDLER::NONE;
207  bool reset_ = false;
208  WPAction* wpAction;
209 
210  void setBufferReset();
211  void check();
212  uint32_t verbosity = 0;
213 
214 
215 }; // class WatchPoint
216 
217 
218 } // namespace SST
219 
220 
221 #endif // SST_CORE_WATCHPOINT_H
Base class for interacting with data from ObjectMap.
Definition: objectMap.h:85
Definition: watchPoint.h:117
WatchPoint Action Inner Classes.
Definition: watchPoint.h:46
Definition: watchPoint.h:63
Definition: action.cc:18
Base class for objects created by the serializer mapping mode used to map the variables for objects...
Definition: objectMap.h:158
Definition: watchPoint.h:72
Definition: watchPoint.h:81
Base class for performing comparisons and logic operations for determining when the WatchPoint trigge...
Definition: watchPoint.h:36
Attach Point to get notified when a handler starts and stops.
Definition: ssthandler.h:117
Definition: watchPoint.h:90
Struct used as a base class for all AttachPoint metadata passed to registration functions.
Definition: sst_types.h:80
Class that can attach to Clock and Event Handlers to monitor the state of variables.
Definition: watchPoint.h:27
Base class for Events - Items sent across links to communicate between components.
Definition: event.h:40
Definition: watchPoint.h:99