SST  15.1.0
StructuralSimulationToolkit
simpleDebug.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_IMPL_INTERACTIVE_SIMPLEDEBUG_H
13 #define SST_CORE_IMPL_INTERACTIVE_SIMPLEDEBUG_H
14 
15 // clang-format off
16 #include "sst/core/eli/elementinfo.h"
17 #include <cstdint>
18 #include <ostream>
19 #include <sstream>
20 #include <sst/core/interactiveConsole.h>
21 #include "sst/core/serialization/objectMapDeferred.h"
22 #include "sst/core/impl/interactive/cmdLineEditor.h"
23 #include <sst/core/watchPoint.h>
24 
25 #include <fstream>
26 #include <functional>
27 #include <map>
28 #include <string>
29 #include <vector>
30 // clang-format on
31 
32 namespace SST::IMPL::Interactive {
33 
34 enum class ConsoleCommandGroup { GENERAL, NAVIGATION, STATE, WATCH, SIMULATION, LOGGING, MISC };
35 
36 const std::map<ConsoleCommandGroup, std::string> GroupText {
37  { ConsoleCommandGroup::GENERAL, "General" },
38  { ConsoleCommandGroup::NAVIGATION, "Navigation" },
39  { ConsoleCommandGroup::STATE, "State" },
40  { ConsoleCommandGroup::WATCH, "Watch/Trace" },
41  { ConsoleCommandGroup::SIMULATION, "Simulation" },
42  { ConsoleCommandGroup::LOGGING, "Logging" },
43  { ConsoleCommandGroup::MISC, "Misc" },
44 };
45 
46 enum class VERBOSITY_MASK : uint32_t {
47  WATCHPOINTS = 0b0001'0000 // 0x10
48 };
49 
50 // Encapsulate a console command.
51 class ConsoleCommand
52 {
53 public:
54  ConsoleCommand(std::string str_long, std::string str_short, std::string str_help, ConsoleCommandGroup group,
55  std::function<void(std::vector<std::string>& tokens)> func) :
56  str_long_(str_long),
57  str_short_(str_short),
58  str_help_(str_help),
59  group_(group),
60  func_(func)
61  {}
62  const std::string& str_long() const { return str_long_; }
63  const std::string& str_short() const { return str_short_; }
64  const std::string& str_help() const { return str_help_; }
65  const ConsoleCommandGroup& group() const { return group_; }
66  void exec(std::vector<std::string>& tokens) { return func_(tokens); }
67  bool match(const std::string& token)
68  {
69  std::string lctoken = toLower(token);
70  if ( lctoken.size() == str_long_.size() && lctoken == toLower(str_long_) ) return true;
71  if ( lctoken.size() == str_short_.size() && lctoken == toLower(str_short_) ) return true;
72 
73  return false;
74  }
75  friend std::ostream& operator<<(std::ostream& os, const ConsoleCommand c)
76  {
77  os << c.str_long_ << " (" << c.str_short_ << ") " << c.str_help_;
78  return os;
79  }
80 
81 private:
82  std::string str_long_;
83  std::string str_short_;
84  std::string str_help_;
85  ConsoleCommandGroup group_;
86  std::function<void(std::vector<std::string>& tokens)> func_;
87  std::string toLower(std::string s)
88  {
89  std::transform(s.begin(), s.end(), s.begin(), ::tolower);
90  return s;
91  }
92 };
93 
94 class CommandHistoryBuffer
95 {
96 public:
97  const int MAX_CMDS = 200;
98  CommandHistoryBuffer() { buf_.resize(MAX_CMDS); }
99  void append(std::string s);
100  void print(int num);
101  std::vector<std::string>& getBuffer();
102  enum BANG_RC { INVALID, ECHO_ONLY, EXEC, NOP };
103  BANG_RC bang(const std::string& token, std::string& newcmd);
104 
105 private:
106  int cur_ = 0;
107  int nxt_ = 0;
108  int sz_ = 0;
109  int count_ = 0;
110  std::vector<std::pair<std::size_t, std::string>> buf_; // actual history with index number
111  std::vector<std::string> stringBuffer_; // copy of history strings provided to command line editor
112  // support for ! history retrieval
113  bool findEvent(const std::string& s, std::string& newcmd);
114  bool findOffset(const std::string& s, std::string& newcmd);
115  bool searchFirst(const std::string& s, std::string& newcmd);
116  bool searchAny(const std::string& s, std::string& newcmd);
117 };
118 
119 class SimpleDebugger : public SST::InteractiveConsole
120 {
121 
122 public:
123  SST_ELI_REGISTER_INTERACTIVE_CONSOLE(
124  SimpleDebugger, // class
125  "sst", // library
126  "interactive.simpledebug", // name
127  SST_ELI_ELEMENT_VERSION(1, 0, 0),
128  "{EXPERIMENTAL} Interactive console debug probe")
129 
130  SST_ELI_DOCUMENT_PARAMS(
131  {"replayFile", "script for playback upon entering interactive debug console", ""}
132  )
133 
134  /**
135  Creates a new self partition scheme.
136  */
137  explicit SimpleDebugger(Params& params);
138  ~SimpleDebugger();
139 
140  void execute(const std::string& msg) override;
141 
142  // Callbacks from command line completions
143  void get_listing_strings(std::list<std::string>&);
144 
145 private:
146  // This is the stack of where we are in the class hierarchy. This
147  // is needed because when we advance time, we'll need to delete
148  // any ObjectMap because they could change during execution.
149  // After running, this will allow us to recreate the working
150  // directory as far as we can.
151  std::vector<std::string> name_stack;
152 
153  SST::Core::Serialization::ObjectMap* obj_ = nullptr;
154  bool done = false;
155 
156  bool autoCompleteEnable = true;
157 
158  // gdb/lldb thread spin support
159  uint64_t spinner = 1;
160 
161  // logging support
162  std::ofstream loggingFile;
163  std::ifstream replayFile;
164  std::string loggingFilePath = "sst-console.out";
165  std::string replayFilePath = "sst-console.in";
166  bool enLogging = false;
167 
168  // command injection
169  std::stringstream injectedCommand; // TODO use ConsoleCommand object
170 
171  // Keep a pointer to the ObjectMap for the top level Component
173 
174  // Keep track of all the WatchPoints
175  std::vector<std::pair<WatchPoint*, BaseComponent*>> watch_points_;
176  bool clear_watchlist();
177  bool confirm = true; // Ask for confirmation to clear watchlist
178 
179  std::vector<std::string> tokenize(std::vector<std::string>& tokens, const std::string& input);
180 
181  // Navigation
182  void cmd_help(std::vector<std::string>& UNUSED(tokens));
183  void cmd_verbose(std::vector<std::string>&(tokens));
184  void cmd_pwd(std::vector<std::string>& UNUSED(tokens));
185  void cmd_ls(std::vector<std::string>& UNUSED(tokens));
186  void cmd_cd(std::vector<std::string>& tokens);
187 
188  // Variable Access
189  void cmd_print(std::vector<std::string>& tokens);
190  void cmd_set(std::vector<std::string>& tokens);
191  void cmd_time(std::vector<std::string>& tokens);
192  void cmd_watch(std::vector<std::string>& tokens);
193  void cmd_unwatch(std::vector<std::string>& tokens);
194 
195  // Simulation Control
196  void cmd_run(std::vector<std::string>& tokens);
197  void cmd_shutdown(std::vector<std::string>& tokens);
198  void cmd_exit(std::vector<std::string>& UNUSED(tokens));
199 
200  // Watch/Trace
201  void cmd_watchlist(std::vector<std::string>& tokens);
202  void cmd_trace(std::vector<std::string>& tokens);
203  void cmd_setHandler(std::vector<std::string>& tokens);
204  void cmd_addTraceVar(std::vector<std::string>& tokens);
205  void cmd_resetTraceBuffer(std::vector<std::string>& tokens);
206  void cmd_printTrace(std::vector<std::string>& tokens);
207  void cmd_printWatchpoint(std::vector<std::string>& tokens);
208  void cmd_setConfirm(std::vector<std::string>& tokens);
209 
210  // Logging/Replay
211  void cmd_logging(std::vector<std::string>& tokens);
212  void cmd_replay(std::vector<std::string>& tokens);
213  void cmd_history(std::vector<std::string>& tokens);
214 
215  // Auto-completion toggle
216  void cmd_autoComplete(std::vector<std::string>& UNUSED(tokens));
217 
218  // Reset terminal
219  void cmd_clear(std::vector<std::string>& UNUSED(tokens));
220 
221  // LLDB/GDB helper
222  void cmd_spinThread(std::vector<std::string>& tokens);
223 
224  void dispatch_cmd(std::string& cmd);
225 
226  // Command Registry
227  std::vector<ConsoleCommand> cmdRegistry;
228 
229  // Detailed Command Help
230  std::map<std::string, std::string> cmdHelp;
231 
232  // Command History
233  CommandHistoryBuffer cmdHistoryBuf;
234 
235  // Command Line Editor
236  CmdLineEditor cmdLineEditor;
237 
238  // Verbosity controlled console printing
239  uint32_t verbosity = 0;
240  void msg(VERBOSITY_MASK mask, std::string message);
241 };
242 
243 } // namespace SST::IMPL::Interactive
244 
245 #endif
ObjectMap version that will delay building the internal data structures until the object is "selected...
Definition: objectMapDeferred.h:29
Base class for objects created by the serializer mapping mode used to map the variables for objects...
Definition: objectMap.h:158
The command line editor uses termios to detect key presses and perform auto-completions.
Definition: cmdLineEditor.h:43
Definition: simpleDebug.cc:33