blob: 5fda8d4fa016bf5ea11470a8907ce749aa3df92f [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBCommandInterpreter.cpp --------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/lldb-types.h"
Zachary Turner2c1f46d2015-07-30 20:28:07 +000011
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/Listener.h"
13#include "lldb/Interpreter/CommandInterpreter.h"
Enrico Granata21dfcd92012-09-28 23:57:51 +000014#include "lldb/Interpreter/CommandObjectMultiword.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Interpreter/CommandReturnObject.h"
16#include "lldb/Target/Target.h"
17
Eli Friedmanca93cc12010-06-09 07:37:52 +000018#include "lldb/API/SBBroadcaster.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000019#include "lldb/API/SBCommandInterpreter.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000020#include "lldb/API/SBCommandReturnObject.h"
Ilia Kaf10e1c2015-03-21 10:53:37 +000021#include "lldb/API/SBEvent.h"
Jim Inghamffc9f1d2014-10-14 01:20:07 +000022#include "lldb/API/SBExecutionContext.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000023#include "lldb/API/SBListener.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000024#include "lldb/API/SBProcess.h"
Caroline Ticeceb6b132010-10-26 03:11:13 +000025#include "lldb/API/SBStream.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000026#include "lldb/API/SBStringList.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000027#include "lldb/API/SBTarget.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028
29using namespace lldb;
30using namespace lldb_private;
31
Kate Stoneb9c1b512016-09-06 20:57:50 +000032SBCommandInterpreterRunOptions::SBCommandInterpreterRunOptions() {
33 m_opaque_up.reset(new CommandInterpreterRunOptions());
Jim Ingham26c7bf92014-10-11 00:38:27 +000034}
35
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +000036SBCommandInterpreterRunOptions::~SBCommandInterpreterRunOptions() = default;
Jim Ingham26c7bf92014-10-11 00:38:27 +000037
Kate Stoneb9c1b512016-09-06 20:57:50 +000038bool SBCommandInterpreterRunOptions::GetStopOnContinue() const {
39 return m_opaque_up->GetStopOnContinue();
Jim Ingham26c7bf92014-10-11 00:38:27 +000040}
41
Kate Stoneb9c1b512016-09-06 20:57:50 +000042void SBCommandInterpreterRunOptions::SetStopOnContinue(bool stop_on_continue) {
43 m_opaque_up->SetStopOnContinue(stop_on_continue);
Jim Ingham26c7bf92014-10-11 00:38:27 +000044}
45
Kate Stoneb9c1b512016-09-06 20:57:50 +000046bool SBCommandInterpreterRunOptions::GetStopOnError() const {
47 return m_opaque_up->GetStopOnError();
Jim Ingham26c7bf92014-10-11 00:38:27 +000048}
49
Kate Stoneb9c1b512016-09-06 20:57:50 +000050void SBCommandInterpreterRunOptions::SetStopOnError(bool stop_on_error) {
51 m_opaque_up->SetStopOnError(stop_on_error);
Jim Ingham26c7bf92014-10-11 00:38:27 +000052}
53
Kate Stoneb9c1b512016-09-06 20:57:50 +000054bool SBCommandInterpreterRunOptions::GetStopOnCrash() const {
55 return m_opaque_up->GetStopOnCrash();
Jim Ingham26c7bf92014-10-11 00:38:27 +000056}
57
Kate Stoneb9c1b512016-09-06 20:57:50 +000058void SBCommandInterpreterRunOptions::SetStopOnCrash(bool stop_on_crash) {
59 m_opaque_up->SetStopOnCrash(stop_on_crash);
Jim Ingham26c7bf92014-10-11 00:38:27 +000060}
61
Kate Stoneb9c1b512016-09-06 20:57:50 +000062bool SBCommandInterpreterRunOptions::GetEchoCommands() const {
63 return m_opaque_up->GetEchoCommands();
Jim Ingham26c7bf92014-10-11 00:38:27 +000064}
65
Kate Stoneb9c1b512016-09-06 20:57:50 +000066void SBCommandInterpreterRunOptions::SetEchoCommands(bool echo_commands) {
67 m_opaque_up->SetEchoCommands(echo_commands);
Jim Ingham26c7bf92014-10-11 00:38:27 +000068}
69
Stefan Granitzc678ed72018-10-05 16:49:47 +000070bool SBCommandInterpreterRunOptions::GetEchoCommentCommands() const {
71 return m_opaque_up->GetEchoCommentCommands();
72}
73
74void SBCommandInterpreterRunOptions::SetEchoCommentCommands(bool echo) {
75 m_opaque_up->SetEchoCommentCommands(echo);
76}
77
Kate Stoneb9c1b512016-09-06 20:57:50 +000078bool SBCommandInterpreterRunOptions::GetPrintResults() const {
79 return m_opaque_up->GetPrintResults();
Jim Ingham26c7bf92014-10-11 00:38:27 +000080}
81
Kate Stoneb9c1b512016-09-06 20:57:50 +000082void SBCommandInterpreterRunOptions::SetPrintResults(bool print_results) {
83 m_opaque_up->SetPrintResults(print_results);
Jim Ingham26c7bf92014-10-11 00:38:27 +000084}
85
Kate Stoneb9c1b512016-09-06 20:57:50 +000086bool SBCommandInterpreterRunOptions::GetAddToHistory() const {
87 return m_opaque_up->GetAddToHistory();
Jim Ingham26c7bf92014-10-11 00:38:27 +000088}
89
Kate Stoneb9c1b512016-09-06 20:57:50 +000090void SBCommandInterpreterRunOptions::SetAddToHistory(bool add_to_history) {
91 m_opaque_up->SetAddToHistory(add_to_history);
Jim Ingham26c7bf92014-10-11 00:38:27 +000092}
93
94lldb_private::CommandInterpreterRunOptions *
Kate Stoneb9c1b512016-09-06 20:57:50 +000095SBCommandInterpreterRunOptions::get() const {
96 return m_opaque_up.get();
Jim Ingham26c7bf92014-10-11 00:38:27 +000097}
98
99lldb_private::CommandInterpreterRunOptions &
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100SBCommandInterpreterRunOptions::ref() const {
101 return *m_opaque_up.get();
Jim Ingham26c7bf92014-10-11 00:38:27 +0000102}
103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104class CommandPluginInterfaceImplementation : public CommandObjectParsed {
Enrico Granata21dfcd92012-09-28 23:57:51 +0000105public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 CommandPluginInterfaceImplementation(CommandInterpreter &interpreter,
107 const char *name,
108 lldb::SBCommandPluginInterface *backend,
109 const char *help = nullptr,
110 const char *syntax = nullptr,
111 uint32_t flags = 0)
112 : CommandObjectParsed(interpreter, name, help, syntax, flags),
113 m_backend(backend) {}
114
115 bool IsRemovable() const override { return true; }
116
Enrico Granata21dfcd92012-09-28 23:57:51 +0000117protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 bool DoExecute(Args &command, CommandReturnObject &result) override {
119 SBCommandReturnObject sb_return(&result);
120 SBCommandInterpreter sb_interpreter(&m_interpreter);
121 SBDebugger debugger_sb(m_interpreter.GetDebugger().shared_from_this());
122 bool ret = m_backend->DoExecute(
123 debugger_sb, (char **)command.GetArgumentVector(), sb_return);
124 sb_return.Release();
125 return ret;
126 }
127 std::shared_ptr<lldb::SBCommandPluginInterface> m_backend;
Enrico Granata21dfcd92012-09-28 23:57:51 +0000128};
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130SBCommandInterpreter::SBCommandInterpreter(CommandInterpreter *interpreter)
131 : m_opaque_ptr(interpreter) {
132 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 if (log)
135 log->Printf("SBCommandInterpreter::SBCommandInterpreter (interpreter=%p)"
136 " => SBCommandInterpreter(%p)",
137 static_cast<void *>(interpreter),
138 static_cast<void *>(m_opaque_ptr));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139}
140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs)
142 : m_opaque_ptr(rhs.m_opaque_ptr) {}
Greg Claytonefabb122010-11-05 23:17:00 +0000143
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000144SBCommandInterpreter::~SBCommandInterpreter() = default;
145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146const SBCommandInterpreter &SBCommandInterpreter::
147operator=(const SBCommandInterpreter &rhs) {
148 m_opaque_ptr = rhs.m_opaque_ptr;
149 return *this;
Greg Claytonefabb122010-11-05 23:17:00 +0000150}
151
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152bool SBCommandInterpreter::IsValid() const { return m_opaque_ptr != nullptr; }
153
154bool SBCommandInterpreter::CommandExists(const char *cmd) {
155 return (((cmd != nullptr) && IsValid()) ? m_opaque_ptr->CommandExists(cmd)
156 : false);
Greg Clayton66111032010-06-23 01:19:29 +0000157}
158
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159bool SBCommandInterpreter::AliasExists(const char *cmd) {
160 return (((cmd != nullptr) && IsValid()) ? m_opaque_ptr->AliasExists(cmd)
161 : false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162}
163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164bool SBCommandInterpreter::IsActive() {
165 return (IsValid() ? m_opaque_ptr->IsActive() : false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166}
167
Leonard Mosescu17ffd392017-10-05 23:41:28 +0000168bool SBCommandInterpreter::WasInterrupted() const {
169 return (IsValid() ? m_opaque_ptr->WasInterrupted() : false);
170}
171
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172const char *SBCommandInterpreter::GetIOHandlerControlSequence(char ch) {
173 return (IsValid()
174 ? m_opaque_ptr->GetDebugger()
175 .GetTopIOHandlerControlSequence(ch)
176 .GetCString()
177 : nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +0000178}
179
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180lldb::ReturnStatus
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181SBCommandInterpreter::HandleCommand(const char *command_line,
182 SBCommandReturnObject &result,
183 bool add_to_history) {
184 SBExecutionContext sb_exe_ctx;
185 return HandleCommand(command_line, sb_exe_ctx, result, add_to_history);
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000186}
187
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188lldb::ReturnStatus SBCommandInterpreter::HandleCommand(
189 const char *command_line, SBExecutionContext &override_context,
190 SBCommandReturnObject &result, bool add_to_history) {
191 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000192
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 if (log)
194 log->Printf("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", "
195 "SBCommandReturnObject(%p), add_to_history=%i)",
196 static_cast<void *>(m_opaque_ptr), command_line,
197 static_cast<void *>(result.get()), add_to_history);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 ExecutionContext ctx, *ctx_ptr;
200 if (override_context.get()) {
201 ctx = override_context.get()->Lock(true);
202 ctx_ptr = &ctx;
203 } else
204 ctx_ptr = nullptr;
205
206 result.Clear();
207 if (command_line && IsValid()) {
208 result.ref().SetInteractive(false);
209 m_opaque_ptr->HandleCommand(command_line,
210 add_to_history ? eLazyBoolYes : eLazyBoolNo,
211 result.ref(), ctx_ptr);
212 } else {
213 result->AppendError(
214 "SBCommandInterpreter or the command line is not valid");
215 result->SetStatus(eReturnStatusFailed);
216 }
217
218 // We need to get the value again, in case the command disabled the log!
219 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API);
220 if (log) {
221 SBStream sstr;
222 result.GetDescription(sstr);
223 log->Printf("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", "
224 "SBCommandReturnObject(%p): %s, add_to_history=%i) => %i",
225 static_cast<void *>(m_opaque_ptr), command_line,
226 static_cast<void *>(result.get()), sstr.GetData(),
227 add_to_history, result.GetStatus());
228 }
229
230 return result.GetStatus();
231}
232
233void SBCommandInterpreter::HandleCommandsFromFile(
234 lldb::SBFileSpec &file, lldb::SBExecutionContext &override_context,
235 lldb::SBCommandInterpreterRunOptions &options,
236 lldb::SBCommandReturnObject result) {
237 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
238
239 if (log) {
240 SBStream s;
241 file.GetDescription(s);
242 log->Printf("SBCommandInterpreter(%p)::HandleCommandsFromFile "
243 "(file=\"%s\", SBCommandReturnObject(%p))",
244 static_cast<void *>(m_opaque_ptr), s.GetData(),
245 static_cast<void *>(result.get()));
246 }
247
248 if (!IsValid()) {
249 result->AppendError("SBCommandInterpreter is not valid.");
250 result->SetStatus(eReturnStatusFailed);
251 return;
252 }
253
254 if (!file.IsValid()) {
255 SBStream s;
256 file.GetDescription(s);
257 result->AppendErrorWithFormat("File is not valid: %s.", s.GetData());
258 result->SetStatus(eReturnStatusFailed);
259 }
260
261 FileSpec tmp_spec = file.ref();
262 ExecutionContext ctx, *ctx_ptr;
263 if (override_context.get()) {
264 ctx = override_context.get()->Lock(true);
265 ctx_ptr = &ctx;
266 } else
267 ctx_ptr = nullptr;
268
269 m_opaque_ptr->HandleCommandsFromFile(tmp_spec, ctx_ptr, options.ref(),
270 result.ref());
271}
272
273int SBCommandInterpreter::HandleCompletion(
274 const char *current_line, const char *cursor, const char *last_char,
275 int match_start_point, int max_return_elements, SBStringList &matches) {
Raphael Isemann7f888292018-09-13 21:26:00 +0000276 SBStringList dummy_descriptions;
277 return HandleCompletionWithDescriptions(
278 current_line, cursor, last_char, match_start_point, max_return_elements,
279 matches, dummy_descriptions);
280}
281
282int SBCommandInterpreter::HandleCompletionWithDescriptions(
283 const char *current_line, const char *cursor, const char *last_char,
284 int match_start_point, int max_return_elements, SBStringList &matches,
285 SBStringList &descriptions) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
287 int num_completions = 0;
288
Adrian Prantl05097242018-04-30 16:49:04 +0000289 // Sanity check the arguments that are passed in: cursor & last_char have to
290 // be within the current_line.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 if (current_line == nullptr || cursor == nullptr || last_char == nullptr)
292 return 0;
293
294 if (cursor < current_line || last_char < current_line)
295 return 0;
296
297 size_t current_line_size = strlen(current_line);
298 if (cursor - current_line > static_cast<ptrdiff_t>(current_line_size) ||
299 last_char - current_line > static_cast<ptrdiff_t>(current_line_size))
300 return 0;
301
302 if (log)
303 log->Printf("SBCommandInterpreter(%p)::HandleCompletion "
304 "(current_line=\"%s\", cursor at: %" PRId64
305 ", last char at: %" PRId64
306 ", match_start_point: %d, max_return_elements: %d)",
307 static_cast<void *>(m_opaque_ptr), current_line,
308 static_cast<uint64_t>(cursor - current_line),
309 static_cast<uint64_t>(last_char - current_line),
310 match_start_point, max_return_elements);
311
312 if (IsValid()) {
Raphael Isemann7f888292018-09-13 21:26:00 +0000313 lldb_private::StringList lldb_matches, lldb_descriptions;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314 num_completions = m_opaque_ptr->HandleCompletion(
315 current_line, cursor, last_char, match_start_point, max_return_elements,
Raphael Isemann7f888292018-09-13 21:26:00 +0000316 lldb_matches, lldb_descriptions);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317
Raphael Isemann7f888292018-09-13 21:26:00 +0000318 SBStringList temp_matches_list(&lldb_matches);
319 matches.AppendList(temp_matches_list);
320 SBStringList temp_descriptions_list(&lldb_descriptions);
321 descriptions.AppendList(temp_descriptions_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322 }
323 if (log)
324 log->Printf(
325 "SBCommandInterpreter(%p)::HandleCompletion - Found %d completions.",
326 static_cast<void *>(m_opaque_ptr), num_completions);
327
328 return num_completions;
329}
330
Raphael Isemann7f888292018-09-13 21:26:00 +0000331int SBCommandInterpreter::HandleCompletionWithDescriptions(
332 const char *current_line, uint32_t cursor_pos, int match_start_point,
333 int max_return_elements, SBStringList &matches,
334 SBStringList &descriptions) {
335 const char *cursor = current_line + cursor_pos;
336 const char *last_char = current_line + strlen(current_line);
337 return HandleCompletionWithDescriptions(
338 current_line, cursor, last_char, match_start_point, max_return_elements,
339 matches, descriptions);
340}
341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342int SBCommandInterpreter::HandleCompletion(const char *current_line,
343 uint32_t cursor_pos,
344 int match_start_point,
345 int max_return_elements,
346 lldb::SBStringList &matches) {
347 const char *cursor = current_line + cursor_pos;
348 const char *last_char = current_line + strlen(current_line);
349 return HandleCompletion(current_line, cursor, last_char, match_start_point,
350 max_return_elements, matches);
351}
352
353bool SBCommandInterpreter::HasCommands() {
354 return (IsValid() ? m_opaque_ptr->HasCommands() : false);
355}
356
357bool SBCommandInterpreter::HasAliases() {
358 return (IsValid() ? m_opaque_ptr->HasAliases() : false);
359}
360
361bool SBCommandInterpreter::HasAliasOptions() {
362 return (IsValid() ? m_opaque_ptr->HasAliasOptions() : false);
363}
364
365SBProcess SBCommandInterpreter::GetProcess() {
366 SBProcess sb_process;
367 ProcessSP process_sp;
368 if (IsValid()) {
369 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
370 if (target_sp) {
371 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
372 process_sp = target_sp->GetProcessSP();
373 sb_process.SetSP(process_sp);
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000374 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375 }
376 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000377
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 if (log)
379 log->Printf("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)",
380 static_cast<void *>(m_opaque_ptr),
381 static_cast<void *>(process_sp.get()));
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000382
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383 return sb_process;
384}
385
386SBDebugger SBCommandInterpreter::GetDebugger() {
387 SBDebugger sb_debugger;
388 if (IsValid())
389 sb_debugger.reset(m_opaque_ptr->GetDebugger().shared_from_this());
390 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
391
392 if (log)
393 log->Printf("SBCommandInterpreter(%p)::GetDebugger () => SBDebugger(%p)",
394 static_cast<void *>(m_opaque_ptr),
395 static_cast<void *>(sb_debugger.get()));
396
397 return sb_debugger;
398}
399
400bool SBCommandInterpreter::GetPromptOnQuit() {
401 return (IsValid() ? m_opaque_ptr->GetPromptOnQuit() : false);
402}
403
404void SBCommandInterpreter::SetPromptOnQuit(bool b) {
405 if (IsValid())
406 m_opaque_ptr->SetPromptOnQuit(b);
407}
408
Raphael Isemannc094d232018-07-11 17:18:01 +0000409void SBCommandInterpreter::AllowExitCodeOnQuit(bool allow) {
410 if (m_opaque_ptr)
411 m_opaque_ptr->AllowExitCodeOnQuit(allow);
412}
413
414bool SBCommandInterpreter::HasCustomQuitExitCode() {
415 bool exited = false;
416 if (m_opaque_ptr)
417 m_opaque_ptr->GetQuitExitCode(exited);
418 return exited;
419}
420
421int SBCommandInterpreter::GetQuitStatus() {
422 bool exited = false;
423 return (m_opaque_ptr ? m_opaque_ptr->GetQuitExitCode(exited) : 0);
424}
425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426void SBCommandInterpreter::ResolveCommand(const char *command_line,
427 SBCommandReturnObject &result) {
428 result.Clear();
429 if (command_line && IsValid()) {
430 m_opaque_ptr->ResolveCommand(command_line, result.ref());
431 } else {
432 result->AppendError(
433 "SBCommandInterpreter or the command line is not valid");
434 result->SetStatus(eReturnStatusFailed);
435 }
436}
437
438CommandInterpreter *SBCommandInterpreter::get() { return m_opaque_ptr; }
439
440CommandInterpreter &SBCommandInterpreter::ref() {
441 assert(m_opaque_ptr);
442 return *m_opaque_ptr;
443}
444
445void SBCommandInterpreter::reset(
446 lldb_private::CommandInterpreter *interpreter) {
447 m_opaque_ptr = interpreter;
448}
449
450void SBCommandInterpreter::SourceInitFileInHomeDirectory(
451 SBCommandReturnObject &result) {
452 result.Clear();
453 if (IsValid()) {
454 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
455 std::unique_lock<std::recursive_mutex> lock;
456 if (target_sp)
457 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
458 m_opaque_ptr->SourceInitFile(false, result.ref());
459 } else {
460 result->AppendError("SBCommandInterpreter is not valid");
461 result->SetStatus(eReturnStatusFailed);
462 }
463 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
464
465 if (log)
466 log->Printf("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory "
467 "(&SBCommandReturnObject(%p))",
468 static_cast<void *>(m_opaque_ptr),
469 static_cast<void *>(result.get()));
470}
471
472void SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory(
473 SBCommandReturnObject &result) {
474 result.Clear();
475 if (IsValid()) {
476 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
477 std::unique_lock<std::recursive_mutex> lock;
478 if (target_sp)
479 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
480 m_opaque_ptr->SourceInitFile(true, result.ref());
481 } else {
482 result->AppendError("SBCommandInterpreter is not valid");
483 result->SetStatus(eReturnStatusFailed);
484 }
485 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
486
487 if (log)
488 log->Printf(
489 "SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory "
490 "(&SBCommandReturnObject(%p))",
491 static_cast<void *>(m_opaque_ptr), static_cast<void *>(result.get()));
492}
493
494SBBroadcaster SBCommandInterpreter::GetBroadcaster() {
495 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
496
497 SBBroadcaster broadcaster(m_opaque_ptr, false);
498
499 if (log)
500 log->Printf(
501 "SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
502 static_cast<void *>(m_opaque_ptr),
503 static_cast<void *>(broadcaster.get()));
504
505 return broadcaster;
506}
507
508const char *SBCommandInterpreter::GetBroadcasterClass() {
509 return CommandInterpreter::GetStaticBroadcasterClass().AsCString();
510}
511
512const char *SBCommandInterpreter::GetArgumentTypeAsCString(
513 const lldb::CommandArgumentType arg_type) {
514 return CommandObject::GetArgumentTypeAsCString(arg_type);
515}
516
517const char *SBCommandInterpreter::GetArgumentDescriptionAsCString(
518 const lldb::CommandArgumentType arg_type) {
519 return CommandObject::GetArgumentDescriptionAsCString(arg_type);
520}
521
522bool SBCommandInterpreter::EventIsCommandInterpreterEvent(
523 const lldb::SBEvent &event) {
524 return event.GetBroadcasterClass() ==
525 SBCommandInterpreter::GetBroadcasterClass();
526}
527
528bool SBCommandInterpreter::SetCommandOverrideCallback(
529 const char *command_name, lldb::CommandOverrideCallback callback,
530 void *baton) {
531 if (command_name && command_name[0] && IsValid()) {
Zachary Turnera01bccd2016-10-05 21:14:56 +0000532 llvm::StringRef command_name_str = command_name;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000533 CommandObject *cmd_obj =
534 m_opaque_ptr->GetCommandObjectForCommand(command_name_str);
535 if (cmd_obj) {
536 assert(command_name_str.empty());
537 cmd_obj->SetOverrideCallback(callback, baton);
538 return true;
Greg Clayton66111032010-06-23 01:19:29 +0000539 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000540 }
541 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542}
543
Kate Stoneb9c1b512016-09-06 20:57:50 +0000544lldb::SBCommand SBCommandInterpreter::AddMultiwordCommand(const char *name,
545 const char *help) {
546 CommandObjectMultiword *new_command =
547 new CommandObjectMultiword(*m_opaque_ptr, name, help);
548 new_command->SetRemovable(true);
549 lldb::CommandObjectSP new_command_sp(new_command);
550 if (new_command_sp &&
551 m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
552 return lldb::SBCommand(new_command_sp);
553 return lldb::SBCommand();
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000554}
555
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556lldb::SBCommand SBCommandInterpreter::AddCommand(
557 const char *name, lldb::SBCommandPluginInterface *impl, const char *help) {
558 lldb::CommandObjectSP new_command_sp;
559 new_command_sp.reset(new CommandPluginInterfaceImplementation(
560 *m_opaque_ptr, name, impl, help));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000561
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562 if (new_command_sp &&
563 m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
564 return lldb::SBCommand(new_command_sp);
565 return lldb::SBCommand();
Greg Claytona9f7b792012-02-29 04:21:24 +0000566}
Greg Clayton9d0402b2011-02-20 02:15:07 +0000567
Enrico Granata21dfcd92012-09-28 23:57:51 +0000568lldb::SBCommand
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569SBCommandInterpreter::AddCommand(const char *name,
570 lldb::SBCommandPluginInterface *impl,
571 const char *help, const char *syntax) {
572 lldb::CommandObjectSP new_command_sp;
573 new_command_sp.reset(new CommandPluginInterfaceImplementation(
574 *m_opaque_ptr, name, impl, help, syntax));
Enrico Granata21dfcd92012-09-28 23:57:51 +0000575
Kate Stoneb9c1b512016-09-06 20:57:50 +0000576 if (new_command_sp &&
577 m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
578 return lldb::SBCommand(new_command_sp);
579 return lldb::SBCommand();
Abhishek Aggarwalf605c192016-07-29 07:46:32 +0000580}
581
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000582SBCommand::SBCommand() = default;
Enrico Granata21dfcd92012-09-28 23:57:51 +0000583
Kate Stoneb9c1b512016-09-06 20:57:50 +0000584SBCommand::SBCommand(lldb::CommandObjectSP cmd_sp) : m_opaque_sp(cmd_sp) {}
Enrico Granata21dfcd92012-09-28 23:57:51 +0000585
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586bool SBCommand::IsValid() { return m_opaque_sp.get() != nullptr; }
587
588const char *SBCommand::GetName() {
Zachary Turnera4496982016-10-05 21:14:38 +0000589 return (IsValid() ? ConstString(m_opaque_sp->GetCommandName()).AsCString() : nullptr);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000590}
591
Kate Stoneb9c1b512016-09-06 20:57:50 +0000592const char *SBCommand::GetHelp() {
Zachary Turner442f6532016-11-12 20:41:02 +0000593 return (IsValid() ? ConstString(m_opaque_sp->GetHelp()).AsCString()
594 : nullptr);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000595}
596
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597const char *SBCommand::GetHelpLong() {
Zachary Turner442f6532016-11-12 20:41:02 +0000598 return (IsValid() ? ConstString(m_opaque_sp->GetHelpLong()).AsCString()
599 : nullptr);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000600}
601
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602void SBCommand::SetHelp(const char *help) {
603 if (IsValid())
604 m_opaque_sp->SetHelp(help);
Enrico Granatacc342da2015-03-13 22:32:11 +0000605}
606
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607void SBCommand::SetHelpLong(const char *help) {
608 if (IsValid())
609 m_opaque_sp->SetHelpLong(help);
Enrico Granatacc342da2015-03-13 22:32:11 +0000610}
611
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612lldb::SBCommand SBCommand::AddMultiwordCommand(const char *name,
613 const char *help) {
614 if (!IsValid())
Enrico Granata21dfcd92012-09-28 23:57:51 +0000615 return lldb::SBCommand();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616 if (!m_opaque_sp->IsMultiwordObject())
Enrico Granata21dfcd92012-09-28 23:57:51 +0000617 return lldb::SBCommand();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000618 CommandObjectMultiword *new_command = new CommandObjectMultiword(
619 m_opaque_sp->GetCommandInterpreter(), name, help);
620 new_command->SetRemovable(true);
621 lldb::CommandObjectSP new_command_sp(new_command);
622 if (new_command_sp && m_opaque_sp->LoadSubCommand(name, new_command_sp))
623 return lldb::SBCommand(new_command_sp);
624 return lldb::SBCommand();
Enrico Granata21dfcd92012-09-28 23:57:51 +0000625}
Enrico Granatae87764f2015-05-27 05:04:35 +0000626
Kate Stoneb9c1b512016-09-06 20:57:50 +0000627lldb::SBCommand SBCommand::AddCommand(const char *name,
628 lldb::SBCommandPluginInterface *impl,
629 const char *help) {
630 if (!IsValid())
Abhishek Aggarwalf605c192016-07-29 07:46:32 +0000631 return lldb::SBCommand();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000632 if (!m_opaque_sp->IsMultiwordObject())
633 return lldb::SBCommand();
634 lldb::CommandObjectSP new_command_sp;
635 new_command_sp.reset(new CommandPluginInterfaceImplementation(
636 m_opaque_sp->GetCommandInterpreter(), name, impl, help));
637 if (new_command_sp && m_opaque_sp->LoadSubCommand(name, new_command_sp))
638 return lldb::SBCommand(new_command_sp);
639 return lldb::SBCommand();
Abhishek Aggarwalf605c192016-07-29 07:46:32 +0000640}
641
Kate Stoneb9c1b512016-09-06 20:57:50 +0000642lldb::SBCommand SBCommand::AddCommand(const char *name,
643 lldb::SBCommandPluginInterface *impl,
644 const char *help, const char *syntax) {
645 if (!IsValid())
646 return lldb::SBCommand();
647 if (!m_opaque_sp->IsMultiwordObject())
648 return lldb::SBCommand();
649 lldb::CommandObjectSP new_command_sp;
650 new_command_sp.reset(new CommandPluginInterfaceImplementation(
651 m_opaque_sp->GetCommandInterpreter(), name, impl, help, syntax));
652 if (new_command_sp && m_opaque_sp->LoadSubCommand(name, new_command_sp))
653 return lldb::SBCommand(new_command_sp);
654 return lldb::SBCommand();
Enrico Granatae87764f2015-05-27 05:04:35 +0000655}
656
Kate Stoneb9c1b512016-09-06 20:57:50 +0000657uint32_t SBCommand::GetFlags() {
658 return (IsValid() ? m_opaque_sp->GetFlags().Get() : 0);
659}
660
661void SBCommand::SetFlags(uint32_t flags) {
662 if (IsValid())
663 m_opaque_sp->GetFlags().Set(flags);
Enrico Granatae87764f2015-05-27 05:04:35 +0000664}