LLDB  mainline
CommandObjectSession.cpp
Go to the documentation of this file.
1 #include "CommandObjectSession.h"
12 
13 using namespace lldb;
14 using namespace lldb_private;
15 
17 public:
19  : CommandObjectParsed(interpreter, "session save",
20  "Save the current session transcripts to a file.\n"
21  "If no file if specified, transcripts will be "
22  "saved to a temporary file.",
23  "session save [file]") {
25  arg1.emplace_back(eArgTypePath, eArgRepeatOptional);
26  m_arguments.push_back(arg1);
27  }
28 
29  ~CommandObjectSessionSave() override = default;
30 
31  void
33  OptionElementVector &opt_element_vector) override {
34  CommandCompletions::InvokeCommonCompletionCallbacks(
35  GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
36  request, nullptr);
37  }
38 
39 protected:
40  bool DoExecute(Args &args, CommandReturnObject &result) override {
41  llvm::StringRef file_path;
42 
43  if (!args.empty())
44  file_path = args[0].ref();
45 
46  if (m_interpreter.SaveTranscript(result, file_path.str()))
48  else
50  return result.Succeeded();
51  }
52 };
53 
54 #define LLDB_OPTIONS_history
55 #include "CommandOptions.inc"
56 
58 public:
60  : CommandObjectParsed(interpreter, "session history",
61  "Dump the history of commands in this session.\n"
62  "Commands in the history list can be run again "
63  "using \"!<INDEX>\". \"!-<OFFSET>\" will re-run "
64  "the command that is <OFFSET> commands from the end"
65  " of the list (counting the current command).",
66  nullptr) {}
67 
68  ~CommandObjectSessionHistory() override = default;
69 
70  Options *GetOptions() override { return &m_options; }
71 
72 protected:
73  class CommandOptions : public Options {
74  public:
76  : m_start_idx(0), m_stop_idx(0), m_count(0), m_clear(false) {}
77 
78  ~CommandOptions() override = default;
79 
80  Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
81  ExecutionContext *execution_context) override {
82  Status error;
83  const int short_option = m_getopt_table[option_idx].val;
84 
85  switch (short_option) {
86  case 'c':
87  error = m_count.SetValueFromString(option_arg, eVarSetOperationAssign);
88  break;
89  case 's':
90  if (option_arg == "end") {
91  m_start_idx.SetCurrentValue(UINT64_MAX);
92  m_start_idx.SetOptionWasSet();
93  } else
94  error = m_start_idx.SetValueFromString(option_arg,
96  break;
97  case 'e':
98  error =
99  m_stop_idx.SetValueFromString(option_arg, eVarSetOperationAssign);
100  break;
101  case 'C':
102  m_clear.SetCurrentValue(true);
103  m_clear.SetOptionWasSet();
104  break;
105  default:
106  llvm_unreachable("Unimplemented option");
107  }
108 
109  return error;
110  }
111 
112  void OptionParsingStarting(ExecutionContext *execution_context) override {
113  m_start_idx.Clear();
114  m_stop_idx.Clear();
115  m_count.Clear();
116  m_clear.Clear();
117  }
118 
119  llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
120  return llvm::makeArrayRef(g_history_options);
121  }
122 
123  // Instance variables to hold the values for command options.
124 
129  };
130 
131  bool DoExecute(Args &command, CommandReturnObject &result) override {
132  if (m_options.m_clear.GetCurrentValue() &&
133  m_options.m_clear.OptionWasSet()) {
134  m_interpreter.GetCommandHistory().Clear();
136  } else {
137  if (m_options.m_start_idx.OptionWasSet() &&
138  m_options.m_stop_idx.OptionWasSet() &&
139  m_options.m_count.OptionWasSet()) {
140  result.AppendError("--count, --start-index and --end-index cannot be "
141  "all specified in the same invocation");
143  } else {
144  std::pair<bool, uint64_t> start_idx(
145  m_options.m_start_idx.OptionWasSet(),
146  m_options.m_start_idx.GetCurrentValue());
147  std::pair<bool, uint64_t> stop_idx(
148  m_options.m_stop_idx.OptionWasSet(),
149  m_options.m_stop_idx.GetCurrentValue());
150  std::pair<bool, uint64_t> count(m_options.m_count.OptionWasSet(),
151  m_options.m_count.GetCurrentValue());
152 
153  const CommandHistory &history(m_interpreter.GetCommandHistory());
154 
155  if (start_idx.first && start_idx.second == UINT64_MAX) {
156  if (count.first) {
157  start_idx.second = history.GetSize() - count.second;
158  stop_idx.second = history.GetSize() - 1;
159  } else if (stop_idx.first) {
160  start_idx.second = stop_idx.second;
161  stop_idx.second = history.GetSize() - 1;
162  } else {
163  start_idx.second = 0;
164  stop_idx.second = history.GetSize() - 1;
165  }
166  } else {
167  if (!start_idx.first && !stop_idx.first && !count.first) {
168  start_idx.second = 0;
169  stop_idx.second = history.GetSize() - 1;
170  } else if (start_idx.first) {
171  if (count.first) {
172  stop_idx.second = start_idx.second + count.second - 1;
173  } else if (!stop_idx.first) {
174  stop_idx.second = history.GetSize() - 1;
175  }
176  } else if (stop_idx.first) {
177  if (count.first) {
178  if (stop_idx.second >= count.second)
179  start_idx.second = stop_idx.second - count.second + 1;
180  else
181  start_idx.second = 0;
182  }
183  } else /* if (count.first) */
184  {
185  start_idx.second = 0;
186  stop_idx.second = count.second - 1;
187  }
188  }
189  history.Dump(result.GetOutputStream(), start_idx.second,
190  stop_idx.second);
191  }
192  }
193  return result.Succeeded();
194  }
195 
197 };
198 
199 CommandObjectSession::CommandObjectSession(CommandInterpreter &interpreter)
200  : CommandObjectMultiword(interpreter, "session",
201  "Commands controlling LLDB session.",
202  "session <subcommand> [<command-options>]") {
203  LoadSubCommand("save",
204  CommandObjectSP(new CommandObjectSessionSave(interpreter)));
205  LoadSubCommand("history",
206  CommandObjectSP(new CommandObjectSessionHistory(interpreter)));
207 }
lldb_private::eArgRepeatOptional
@ eArgRepeatOptional
Definition: lldb-private-enumerations.h:96
lldb_private::CommandObjectParsed
Definition: CommandObject.h:389
CommandObjectSessionHistory::m_options
CommandOptions m_options
Definition: CommandObjectSession.cpp:196
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
lldb_private::CommandHistory::GetSize
size_t GetSize() const
Definition: CommandHistory.cpp:16
CommandObjectSessionHistory::CommandOptions::SetOptionValue
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
Definition: CommandObjectSession.cpp:80
CommandObjectSessionHistory::CommandOptions::m_stop_idx
OptionValueUInt64 m_stop_idx
Definition: CommandObjectSession.cpp:126
lldb_private::OptionValueBoolean
Definition: OptionValueBoolean.h:16
OptionValueUInt64.h
OptionArgParser.h
lldb_private::CommandHistory::Dump
void Dump(Stream &stream, size_t start_idx=0, size_t stop_idx=SIZE_MAX) const
Definition: CommandHistory.cpp:94
CommandObjectSessionHistory::CommandOptions::CommandOptions
CommandOptions()
Definition: CommandObjectSession.cpp:75
CommandObjectSessionHistory::GetOptions
Options * GetOptions() override
Definition: CommandObjectSession.cpp:70
lldb_private::Args
Definition: Args.h:33
lldb_private::CommandHistory
Definition: CommandHistory.h:21
lldb_private::CommandReturnObject::Succeeded
bool Succeeded() const
Definition: CommandReturnObject.cpp:131
CommandReturnObject.h
lldb::eArgTypePath
@ eArgTypePath
Definition: lldb-enumerations.h:558
lldb::eReturnStatusSuccessFinishNoResult
@ eReturnStatusSuccessFinishNoResult
Definition: lldb-enumerations.h:260
OptionValue.h
lldb_private::CommandObjectMultiword
Definition: CommandObjectMultiword.h:19
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
Options.h
CommandObjectSessionHistory::CommandObjectSessionHistory
CommandObjectSessionHistory(CommandInterpreter &interpreter)
Definition: CommandObjectSession.cpp:59
CommandObjectSessionSave::CommandObjectSessionSave
CommandObjectSessionSave(CommandInterpreter &interpreter)
Definition: CommandObjectSession.cpp:18
lldb_private::Options
Definition: Options.h:57
CommandObjectSessionHistory::CommandOptions::GetDefinitions
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
Definition: CommandObjectSession.cpp:119
lldb_private::CommandObjectMultiword::LoadSubCommand
bool LoadSubCommand(llvm::StringRef cmd_name, const lldb::CommandObjectSP &command_obj) override
Definition: CommandObjectMultiword.cpp:80
lldb_private::CommandReturnObject::SetStatus
void SetStatus(lldb::ReturnStatus status)
Definition: CommandReturnObject.cpp:127
CommandObjectSessionSave::DoExecute
bool DoExecute(Args &args, CommandReturnObject &result) override
Definition: CommandObjectSession.cpp:40
lldb_private::CommandReturnObject::GetOutputStream
Stream & GetOutputStream()
Definition: CommandReturnObject.h:46
CommandObjectSessionHistory::CommandOptions::OptionParsingStarting
void OptionParsingStarting(ExecutionContext *execution_context) override
Definition: CommandObjectSession.cpp:112
lldb_private::CommandInterpreter
Definition: CommandInterpreter.h:214
lldb::eReturnStatusFailed
@ eReturnStatusFailed
Definition: lldb-enumerations.h:265
lldb_private::OptionElementVector
std::vector< OptionArgElement > OptionElementVector
Definition: Options.h:42
CommandObjectSessionSave
Definition: CommandObjectSession.cpp:16
OptionValueString.h
CommandObjectSessionHistory::CommandOptions
Definition: CommandObjectSession.cpp:73
CommandObjectSession.h
lldb_private::Status
Definition: Status.h:44
lldb_private::CommandReturnObject
Definition: CommandReturnObject.h:26
uint32_t
CommandObjectSessionHistory::CommandOptions::m_start_idx
OptionValueUInt64 m_start_idx
Definition: CommandObjectSession.cpp:125
CommandObjectSessionHistory
Definition: CommandObjectSession.cpp:57
OptionParser.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::OptionValueUInt64
Definition: OptionValueUInt64.h:17
CommandObjectSessionHistory::CommandOptions::m_clear
OptionValueBoolean m_clear
Definition: CommandObjectSession.cpp:128
CommandInterpreter.h
CommandObjectSessionSave::HandleArgumentCompletion
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The input array contains a parsed version of the line.
Definition: CommandObjectSession.cpp:32
lldb_private::Args::empty
bool empty() const
Definition: Args.h:120
CommandOptionArgumentTable.h
CommandObjectSessionHistory::DoExecute
bool DoExecute(Args &command, CommandReturnObject &result) override
Definition: CommandObjectSession.cpp:131
OptionValueBoolean.h
lldb_private::CommandObject::CommandArgumentEntry
std::vector< CommandArgumentData > CommandArgumentEntry
Definition: CommandObject.h:106
lldb_private::CommandReturnObject::AppendError
void void AppendError(llvm::StringRef in_string)
Definition: CommandReturnObject.cpp:100
lldb
Definition: SBAddress.h:15
CommandObjectSessionHistory::CommandOptions::m_count
OptionValueUInt64 m_count
Definition: CommandObjectSession.cpp:127
UINT64_MAX
#define UINT64_MAX
Definition: lldb-defines.h:23
lldb_private::CompletionRequest
Definition: CompletionRequest.h:100
lldb_private::eVarSetOperationAssign
@ eVarSetOperationAssign
Definition: lldb-private-enumerations.h:90