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