blob: c93db506c5aa1a9c441fbd6d2a585c622ee44491 [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
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/lldb-types.h"
Zachary Turner2c1f46d2015-07-30 20:28:07 +000015
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Listener.h"
17#include "lldb/Interpreter/CommandInterpreter.h"
Enrico Granata21dfcd92012-09-28 23:57:51 +000018#include "lldb/Interpreter/CommandObjectMultiword.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Interpreter/CommandReturnObject.h"
20#include "lldb/Target/Target.h"
21
Eli Friedmanca93cc12010-06-09 07:37:52 +000022#include "lldb/API/SBBroadcaster.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000023#include "lldb/API/SBCommandReturnObject.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000024#include "lldb/API/SBCommandInterpreter.h"
Ilia Kaf10e1c2015-03-21 10:53:37 +000025#include "lldb/API/SBEvent.h"
Jim Inghamffc9f1d2014-10-14 01:20:07 +000026#include "lldb/API/SBExecutionContext.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000027#include "lldb/API/SBProcess.h"
28#include "lldb/API/SBTarget.h"
29#include "lldb/API/SBListener.h"
Caroline Ticeceb6b132010-10-26 03:11:13 +000030#include "lldb/API/SBStream.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000031#include "lldb/API/SBStringList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
33using namespace lldb;
34using namespace lldb_private;
35
Jim Ingham26c7bf92014-10-11 00:38:27 +000036SBCommandInterpreterRunOptions::SBCommandInterpreterRunOptions()
37{
38 m_opaque_up.reset(new CommandInterpreterRunOptions());
39}
40
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +000041SBCommandInterpreterRunOptions::~SBCommandInterpreterRunOptions() = default;
Jim Ingham26c7bf92014-10-11 00:38:27 +000042
43bool
44SBCommandInterpreterRunOptions::GetStopOnContinue () const
45{
46 return m_opaque_up->GetStopOnContinue();
47}
48
49void
50SBCommandInterpreterRunOptions::SetStopOnContinue (bool stop_on_continue)
51{
52 m_opaque_up->SetStopOnContinue(stop_on_continue);
53}
54
55bool
56SBCommandInterpreterRunOptions::GetStopOnError () const
57{
58 return m_opaque_up->GetStopOnError();
59}
60
61void
62SBCommandInterpreterRunOptions::SetStopOnError (bool stop_on_error)
63{
64 m_opaque_up->SetStopOnError(stop_on_error);
65}
66
67bool
68SBCommandInterpreterRunOptions::GetStopOnCrash () const
69{
70 return m_opaque_up->GetStopOnCrash();
71}
72
73void
74SBCommandInterpreterRunOptions::SetStopOnCrash (bool stop_on_crash)
75{
76 m_opaque_up->SetStopOnCrash(stop_on_crash);
77}
78
79bool
80SBCommandInterpreterRunOptions::GetEchoCommands () const
81{
82 return m_opaque_up->GetEchoCommands();
83}
84
85void
86SBCommandInterpreterRunOptions::SetEchoCommands (bool echo_commands)
87{
88 m_opaque_up->SetEchoCommands(echo_commands);
89}
90
91bool
92SBCommandInterpreterRunOptions::GetPrintResults () const
93{
94 return m_opaque_up->GetPrintResults();
95}
96
97void
98SBCommandInterpreterRunOptions::SetPrintResults (bool print_results)
99{
100 m_opaque_up->SetPrintResults(print_results);
101}
102
103bool
104SBCommandInterpreterRunOptions::GetAddToHistory () const
105{
106 return m_opaque_up->GetAddToHistory();
107}
108
109void
110SBCommandInterpreterRunOptions::SetAddToHistory (bool add_to_history)
111{
112 m_opaque_up->SetAddToHistory(add_to_history);
113}
114
115lldb_private::CommandInterpreterRunOptions *
116SBCommandInterpreterRunOptions::get () const
117{
118 return m_opaque_up.get();
119}
120
121lldb_private::CommandInterpreterRunOptions &
122SBCommandInterpreterRunOptions::ref () const
123{
124 return *m_opaque_up.get();
125}
126
Enrico Granata21dfcd92012-09-28 23:57:51 +0000127class CommandPluginInterfaceImplementation : public CommandObjectParsed
128{
129public:
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000130 CommandPluginInterfaceImplementation(CommandInterpreter &interpreter,
131 const char *name,
132 lldb::SBCommandPluginInterface* backend,
133 const char *help = nullptr,
134 const char *syntax = nullptr,
135 uint32_t flags = 0) :
Enrico Granata21dfcd92012-09-28 23:57:51 +0000136 CommandObjectParsed (interpreter, name, help, syntax, flags),
137 m_backend(backend) {}
138
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000139 bool
140 IsRemovable() const override
141 {
142 return true;
143 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000144
145protected:
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000146 bool
147 DoExecute(Args& command, CommandReturnObject &result) override
Enrico Granata21dfcd92012-09-28 23:57:51 +0000148 {
149 SBCommandReturnObject sb_return(&result);
150 SBCommandInterpreter sb_interpreter(&m_interpreter);
151 SBDebugger debugger_sb(m_interpreter.GetDebugger().shared_from_this());
152 bool ret = m_backend->DoExecute (debugger_sb,(char**)command.GetArgumentVector(), sb_return);
153 sb_return.Release();
154 return ret;
155 }
Abhishek Aggarwalf605c192016-07-29 07:46:32 +0000156 std::shared_ptr<lldb::SBCommandPluginInterface> m_backend;
Enrico Granata21dfcd92012-09-28 23:57:51 +0000157};
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158
Greg Clayton66111032010-06-23 01:19:29 +0000159SBCommandInterpreter::SBCommandInterpreter (CommandInterpreter *interpreter) :
160 m_opaque_ptr (interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161{
Greg Clayton5160ce52013-03-27 23:08:40 +0000162 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000163
164 if (log)
Greg Clayton48381312010-10-30 04:51:46 +0000165 log->Printf ("SBCommandInterpreter::SBCommandInterpreter (interpreter=%p)"
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000166 " => SBCommandInterpreter(%p)",
167 static_cast<void*>(interpreter),
168 static_cast<void*>(m_opaque_ptr));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169}
170
Greg Claytonefabb122010-11-05 23:17:00 +0000171SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs) :
172 m_opaque_ptr (rhs.m_opaque_ptr)
173{
174}
175
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000176SBCommandInterpreter::~SBCommandInterpreter() = default;
177
Greg Claytonefabb122010-11-05 23:17:00 +0000178const SBCommandInterpreter &
179SBCommandInterpreter::operator = (const SBCommandInterpreter &rhs)
180{
181 m_opaque_ptr = rhs.m_opaque_ptr;
182 return *this;
183}
184
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185bool
Greg Clayton66111032010-06-23 01:19:29 +0000186SBCommandInterpreter::IsValid() const
187{
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000188 return m_opaque_ptr != nullptr;
Greg Clayton66111032010-06-23 01:19:29 +0000189}
190
Greg Clayton66111032010-06-23 01:19:29 +0000191bool
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000192SBCommandInterpreter::CommandExists(const char *cmd)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193{
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000194 return (((cmd != nullptr) && IsValid()) ? m_opaque_ptr->CommandExists(cmd) : false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195}
196
197bool
198SBCommandInterpreter::AliasExists (const char *cmd)
199{
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000200 return (((cmd != nullptr) && IsValid()) ? m_opaque_ptr->AliasExists(cmd) : false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201}
202
Greg Clayton44d93782014-01-27 23:43:24 +0000203bool
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000204SBCommandInterpreter::IsActive()
Greg Clayton44d93782014-01-27 23:43:24 +0000205{
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000206 return (IsValid() ? m_opaque_ptr->IsActive() : false);
Greg Clayton44d93782014-01-27 23:43:24 +0000207}
208
209const char *
210SBCommandInterpreter::GetIOHandlerControlSequence(char ch)
211{
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000212 return (IsValid() ? m_opaque_ptr->GetDebugger().GetTopIOHandlerControlSequence(ch).GetCString() : nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +0000213}
214
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215lldb::ReturnStatus
216SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history)
217{
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000218 SBExecutionContext sb_exe_ctx;
219 return HandleCommand (command_line, sb_exe_ctx, result, add_to_history);
220}
221
222lldb::ReturnStatus
223SBCommandInterpreter::HandleCommand (const char *command_line, SBExecutionContext &override_context, SBCommandReturnObject &result, bool add_to_history)
224{
Greg Clayton5160ce52013-03-27 23:08:40 +0000225 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000226
227 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000228 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p), add_to_history=%i)",
229 static_cast<void*>(m_opaque_ptr), command_line,
230 static_cast<void*>(result.get()), add_to_history);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000231
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000232 ExecutionContext ctx, *ctx_ptr;
233 if (override_context.get())
234 {
235 ctx = override_context.get()->Lock(true);
236 ctx_ptr = &ctx;
237 }
238 else
239 ctx_ptr = nullptr;
240
241
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242 result.Clear();
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000243 if (command_line && IsValid())
Greg Clayton66111032010-06-23 01:19:29 +0000244 {
Greg Clayton45a44f32014-07-15 00:25:59 +0000245 result.ref().SetInteractive(false);
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000246 m_opaque_ptr->HandleCommand (command_line, add_to_history ? eLazyBoolYes : eLazyBoolNo, result.ref(), ctx_ptr);
Greg Clayton66111032010-06-23 01:19:29 +0000247 }
248 else
249 {
Johnny Chen872e0622011-12-19 21:16:29 +0000250 result->AppendError ("SBCommandInterpreter or the command line is not valid");
Greg Clayton66111032010-06-23 01:19:29 +0000251 result->SetStatus (eReturnStatusFailed);
252 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000253
Caroline Tice7b9da4a2010-10-27 21:23:37 +0000254 // We need to get the value again, in case the command disabled the log!
255 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000256 if (log)
257 {
258 SBStream sstr;
259 result.GetDescription (sstr);
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000260 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p): %s, add_to_history=%i) => %i",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000261 static_cast<void*>(m_opaque_ptr), command_line,
262 static_cast<void*>(result.get()), sstr.GetData(),
263 add_to_history, result.GetStatus());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000264 }
265
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000266 return result.GetStatus();
267}
268
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000269void
270SBCommandInterpreter::HandleCommandsFromFile (lldb::SBFileSpec &file,
271 lldb::SBExecutionContext &override_context,
272 lldb::SBCommandInterpreterRunOptions &options,
273 lldb::SBCommandReturnObject result)
274{
275 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
276
277 if (log)
278 {
279 SBStream s;
280 file.GetDescription (s);
281 log->Printf ("SBCommandInterpreter(%p)::HandleCommandsFromFile (file=\"%s\", SBCommandReturnObject(%p))",
282 static_cast<void*>(m_opaque_ptr), s.GetData(),
283 static_cast<void*>(result.get()));
284 }
285
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000286 if (!IsValid())
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000287 {
288 result->AppendError ("SBCommandInterpreter is not valid.");
289 result->SetStatus (eReturnStatusFailed);
290 return;
291 }
292
293 if (!file.IsValid())
294 {
295 SBStream s;
296 file.GetDescription (s);
297 result->AppendErrorWithFormat ("File is not valid: %s.", s.GetData());
298 result->SetStatus (eReturnStatusFailed);
299 }
300
301 FileSpec tmp_spec = file.ref();
302 ExecutionContext ctx, *ctx_ptr;
303 if (override_context.get())
304 {
305 ctx = override_context.get()->Lock(true);
306 ctx_ptr = &ctx;
307 }
308 else
309 ctx_ptr = nullptr;
310
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000311 m_opaque_ptr->HandleCommandsFromFile (tmp_spec, ctx_ptr, options.ref(), result.ref());
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000312}
313
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314int
315SBCommandInterpreter::HandleCompletion (const char *current_line,
316 const char *cursor,
317 const char *last_char,
318 int match_start_point,
319 int max_return_elements,
320 SBStringList &matches)
321{
Greg Clayton5160ce52013-03-27 23:08:40 +0000322 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton66111032010-06-23 01:19:29 +0000323 int num_completions = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000324
Jim Inghamac012602011-12-05 19:24:15 +0000325 // Sanity check the arguments that are passed in:
326 // cursor & last_char have to be within the current_line.
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000327 if (current_line == nullptr || cursor == nullptr || last_char == nullptr)
Jim Inghamac012602011-12-05 19:24:15 +0000328 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000329
Jim Inghamac012602011-12-05 19:24:15 +0000330 if (cursor < current_line || last_char < current_line)
331 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000332
Jim Inghamac012602011-12-05 19:24:15 +0000333 size_t current_line_size = strlen (current_line);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +0000334 if (cursor - current_line > static_cast<ptrdiff_t>(current_line_size) ||
335 last_char - current_line > static_cast<ptrdiff_t>(current_line_size))
Jim Inghamac012602011-12-05 19:24:15 +0000336 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000337
Jim Ingham389512d2012-06-26 01:21:59 +0000338 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000339 log->Printf ("SBCommandInterpreter(%p)::HandleCompletion (current_line=\"%s\", cursor at: %" PRId64 ", last char at: %" PRId64 ", match_start_point: %d, max_return_elements: %d)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000340 static_cast<void*>(m_opaque_ptr), current_line,
341 static_cast<uint64_t>(cursor - current_line),
342 static_cast<uint64_t>(last_char - current_line),
343 match_start_point, max_return_elements);
344
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000345 if (IsValid())
Greg Clayton66111032010-06-23 01:19:29 +0000346 {
347 lldb_private::StringList lldb_matches;
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000348 num_completions = m_opaque_ptr->HandleCompletion(current_line, cursor, last_char, match_start_point,
349 max_return_elements, lldb_matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350
Greg Clayton66111032010-06-23 01:19:29 +0000351 SBStringList temp_list (&lldb_matches);
352 matches.AppendList (temp_list);
353 }
Jim Ingham389512d2012-06-26 01:21:59 +0000354 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000355 log->Printf ("SBCommandInterpreter(%p)::HandleCompletion - Found %d completions.",
356 static_cast<void*>(m_opaque_ptr), num_completions);
357
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358 return num_completions;
359}
360
Jim Ingham969795f2011-09-21 01:17:13 +0000361int
362SBCommandInterpreter::HandleCompletion (const char *current_line,
363 uint32_t cursor_pos,
364 int match_start_point,
365 int max_return_elements,
366 lldb::SBStringList &matches)
367{
368 const char *cursor = current_line + cursor_pos;
369 const char *last_char = current_line + strlen (current_line);
370 return HandleCompletion (current_line, cursor, last_char, match_start_point, max_return_elements, matches);
371}
372
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373bool
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000374SBCommandInterpreter::HasCommands()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375{
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000376 return (IsValid() ? m_opaque_ptr->HasCommands() : false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377}
378
379bool
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000380SBCommandInterpreter::HasAliases()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381{
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000382 return (IsValid() ? m_opaque_ptr->HasAliases() : false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383}
384
385bool
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000386SBCommandInterpreter::HasAliasOptions()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387{
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000388 return (IsValid() ? m_opaque_ptr->HasAliasOptions() : false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389}
390
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391SBProcess
392SBCommandInterpreter::GetProcess ()
393{
Greg Claytonb9556ac2012-01-30 07:41:31 +0000394 SBProcess sb_process;
395 ProcessSP process_sp;
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000396 if (IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397 {
Greg Claytonaf67cec2010-12-20 20:49:23 +0000398 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
399 if (target_sp)
400 {
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000401 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Greg Claytonb9556ac2012-01-30 07:41:31 +0000402 process_sp = target_sp->GetProcessSP();
403 sb_process.SetSP(process_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +0000404 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000406 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000407
408 if (log)
409 log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000410 static_cast<void*>(m_opaque_ptr),
411 static_cast<void*>(process_sp.get()));
Greg Clayton48381312010-10-30 04:51:46 +0000412
Greg Claytonb9556ac2012-01-30 07:41:31 +0000413 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414}
415
Enrico Granata21dfcd92012-09-28 23:57:51 +0000416SBDebugger
417SBCommandInterpreter::GetDebugger ()
418{
419 SBDebugger sb_debugger;
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000420 if (IsValid())
Enrico Granata21dfcd92012-09-28 23:57:51 +0000421 sb_debugger.reset(m_opaque_ptr->GetDebugger().shared_from_this());
Greg Clayton5160ce52013-03-27 23:08:40 +0000422 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000423
Enrico Granata21dfcd92012-09-28 23:57:51 +0000424 if (log)
425 log->Printf ("SBCommandInterpreter(%p)::GetDebugger () => SBDebugger(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000426 static_cast<void*>(m_opaque_ptr),
427 static_cast<void*>(sb_debugger.get()));
428
Enrico Granata21dfcd92012-09-28 23:57:51 +0000429 return sb_debugger;
430}
431
Ilia Kacf28be2015-03-23 22:45:13 +0000432bool
433SBCommandInterpreter::GetPromptOnQuit()
434{
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000435 return (IsValid() ? m_opaque_ptr->GetPromptOnQuit() : false);
Ilia Kacf28be2015-03-23 22:45:13 +0000436}
437
438void
439SBCommandInterpreter::SetPromptOnQuit (bool b)
440{
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000441 if (IsValid())
Ilia Kacf28be2015-03-23 22:45:13 +0000442 m_opaque_ptr->SetPromptOnQuit(b);
443}
444
Adrian McCarthy2304b6f2015-04-23 20:00:25 +0000445void
446SBCommandInterpreter::ResolveCommand(const char *command_line, SBCommandReturnObject &result)
447{
448 result.Clear();
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000449 if (command_line && IsValid())
Adrian McCarthy2304b6f2015-04-23 20:00:25 +0000450 {
451 m_opaque_ptr->ResolveCommand(command_line, result.ref());
452 }
453 else
454 {
455 result->AppendError("SBCommandInterpreter or the command line is not valid");
456 result->SetStatus(eReturnStatusFailed);
457 }
458}
459
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460CommandInterpreter *
Greg Clayton66111032010-06-23 01:19:29 +0000461SBCommandInterpreter::get ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462{
Greg Clayton66111032010-06-23 01:19:29 +0000463 return m_opaque_ptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464}
465
466CommandInterpreter &
Greg Clayton66111032010-06-23 01:19:29 +0000467SBCommandInterpreter::ref ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468{
Greg Clayton66111032010-06-23 01:19:29 +0000469 assert (m_opaque_ptr);
470 return *m_opaque_ptr;
471}
472
473void
474SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
475{
476 m_opaque_ptr = interpreter;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477}
478
479void
480SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
481{
482 result.Clear();
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000483 if (IsValid())
Greg Clayton66111032010-06-23 01:19:29 +0000484 {
Greg Claytonaf67cec2010-12-20 20:49:23 +0000485 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000486 std::unique_lock<std::recursive_mutex> lock;
Greg Claytonaf67cec2010-12-20 20:49:23 +0000487 if (target_sp)
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000488 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
Greg Clayton66111032010-06-23 01:19:29 +0000489 m_opaque_ptr->SourceInitFile (false, result.ref());
490 }
491 else
492 {
493 result->AppendError ("SBCommandInterpreter is not valid");
494 result->SetStatus (eReturnStatusFailed);
495 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000496 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000497
498 if (log)
499 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000500 static_cast<void*>(m_opaque_ptr),
501 static_cast<void*>(result.get()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502}
503
504void
505SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result)
506{
507 result.Clear();
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000508 if (IsValid())
Greg Clayton66111032010-06-23 01:19:29 +0000509 {
Greg Claytonaf67cec2010-12-20 20:49:23 +0000510 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000511 std::unique_lock<std::recursive_mutex> lock;
Greg Claytonaf67cec2010-12-20 20:49:23 +0000512 if (target_sp)
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000513 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
Greg Clayton66111032010-06-23 01:19:29 +0000514 m_opaque_ptr->SourceInitFile (true, result.ref());
515 }
516 else
517 {
518 result->AppendError ("SBCommandInterpreter is not valid");
519 result->SetStatus (eReturnStatusFailed);
520 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000521 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000522
523 if (log)
524 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000525 static_cast<void*>(m_opaque_ptr),
526 static_cast<void*>(result.get()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527}
528
529SBBroadcaster
530SBCommandInterpreter::GetBroadcaster ()
531{
Greg Clayton5160ce52013-03-27 23:08:40 +0000532 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000533
Greg Clayton66111032010-06-23 01:19:29 +0000534 SBBroadcaster broadcaster (m_opaque_ptr, false);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000535
536 if (log)
Greg Clayton48381312010-10-30 04:51:46 +0000537 log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000538 static_cast<void*>(m_opaque_ptr), static_cast<void*>(broadcaster.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000539
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540 return broadcaster;
541}
542
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000543const char *
544SBCommandInterpreter::GetBroadcasterClass ()
545{
Ilia K8a00a562015-03-17 16:54:52 +0000546 return CommandInterpreter::GetStaticBroadcasterClass().AsCString();
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000547}
548
Greg Clayton9d0402b2011-02-20 02:15:07 +0000549const char *
550SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
551{
552 return CommandObject::GetArgumentTypeAsCString (arg_type);
553}
554
555const char *
556SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
557{
558 return CommandObject::GetArgumentDescriptionAsCString (arg_type);
559}
560
Greg Claytona9f7b792012-02-29 04:21:24 +0000561bool
Ilia Kaf10e1c2015-03-21 10:53:37 +0000562SBCommandInterpreter::EventIsCommandInterpreterEvent (const lldb::SBEvent &event)
563{
Ilia Kf9e5dc12015-03-21 11:11:07 +0000564 return event.GetBroadcasterClass() == SBCommandInterpreter::GetBroadcasterClass();
Ilia Kaf10e1c2015-03-21 10:53:37 +0000565}
566
567bool
Greg Claytona9f7b792012-02-29 04:21:24 +0000568SBCommandInterpreter::SetCommandOverrideCallback (const char *command_name,
569 lldb::CommandOverrideCallback callback,
570 void *baton)
571{
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000572 if (command_name && command_name[0] && IsValid())
Greg Claytona9f7b792012-02-29 04:21:24 +0000573 {
Greg Claytonaf6f2752012-05-08 04:29:20 +0000574 std::string command_name_str (command_name);
575 CommandObject *cmd_obj = m_opaque_ptr->GetCommandObjectForCommand(command_name_str);
Greg Claytona9f7b792012-02-29 04:21:24 +0000576 if (cmd_obj)
577 {
Greg Claytonaf6f2752012-05-08 04:29:20 +0000578 assert(command_name_str.empty());
Greg Claytona9f7b792012-02-29 04:21:24 +0000579 cmd_obj->SetOverrideCallback (callback, baton);
580 return true;
581 }
582 }
583 return false;
584}
Greg Clayton9d0402b2011-02-20 02:15:07 +0000585
Enrico Granata21dfcd92012-09-28 23:57:51 +0000586lldb::SBCommand
587SBCommandInterpreter::AddMultiwordCommand (const char* name, const char* help)
588{
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000589 CommandObjectMultiword *new_command = new CommandObjectMultiword(*m_opaque_ptr, name, help);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000590 new_command->SetRemovable (true);
591 lldb::CommandObjectSP new_command_sp(new_command);
592 if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
593 return lldb::SBCommand(new_command_sp);
594 return lldb::SBCommand();
595}
596
597lldb::SBCommand
598SBCommandInterpreter::AddCommand (const char* name, lldb::SBCommandPluginInterface* impl, const char* help)
599{
600 lldb::CommandObjectSP new_command_sp;
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000601 new_command_sp.reset(new CommandPluginInterfaceImplementation(*m_opaque_ptr,name, impl, help));
Enrico Granata21dfcd92012-09-28 23:57:51 +0000602
603 if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
604 return lldb::SBCommand(new_command_sp);
605 return lldb::SBCommand();
606}
607
Abhishek Aggarwalf605c192016-07-29 07:46:32 +0000608lldb::SBCommand
609SBCommandInterpreter::AddCommand (const char* name, lldb::SBCommandPluginInterface* impl, const char* help, const char* syntax)
610{
611 lldb::CommandObjectSP new_command_sp;
612 new_command_sp.reset(new CommandPluginInterfaceImplementation(*m_opaque_ptr,name, impl, help, syntax));
613
614 if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
615 return lldb::SBCommand(new_command_sp);
616 return lldb::SBCommand();
617}
618
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000619SBCommand::SBCommand() = default;
Enrico Granata21dfcd92012-09-28 23:57:51 +0000620
621SBCommand::SBCommand (lldb::CommandObjectSP cmd_sp) : m_opaque_sp (cmd_sp)
622{}
623
624bool
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000625SBCommand::IsValid()
Enrico Granata21dfcd92012-09-28 23:57:51 +0000626{
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000627 return m_opaque_sp.get() != nullptr;
Enrico Granata21dfcd92012-09-28 23:57:51 +0000628}
629
630const char*
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000631SBCommand::GetName()
Enrico Granata21dfcd92012-09-28 23:57:51 +0000632{
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000633 return (IsValid() ? m_opaque_sp->GetCommandName() : nullptr);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000634}
635
636const char*
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000637SBCommand::GetHelp()
Enrico Granata21dfcd92012-09-28 23:57:51 +0000638{
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000639 return (IsValid() ? m_opaque_sp->GetHelp() : nullptr);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000640}
641
Enrico Granatacc342da2015-03-13 22:32:11 +0000642const char*
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000643SBCommand::GetHelpLong()
Enrico Granatacc342da2015-03-13 22:32:11 +0000644{
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000645 return (IsValid() ? m_opaque_sp->GetHelpLong() : nullptr);
Enrico Granatacc342da2015-03-13 22:32:11 +0000646}
647
648void
649SBCommand::SetHelp (const char* help)
650{
651 if (IsValid())
652 m_opaque_sp->SetHelp(help);
653}
654
655void
656SBCommand::SetHelpLong (const char* help)
657{
658 if (IsValid())
659 m_opaque_sp->SetHelpLong(help);
660}
661
Enrico Granata21dfcd92012-09-28 23:57:51 +0000662lldb::SBCommand
663SBCommand::AddMultiwordCommand (const char* name, const char* help)
664{
665 if (!IsValid ())
666 return lldb::SBCommand();
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000667 if (!m_opaque_sp->IsMultiwordObject())
Enrico Granata21dfcd92012-09-28 23:57:51 +0000668 return lldb::SBCommand();
669 CommandObjectMultiword *new_command = new CommandObjectMultiword(m_opaque_sp->GetCommandInterpreter(),name,help);
670 new_command->SetRemovable (true);
671 lldb::CommandObjectSP new_command_sp(new_command);
672 if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp))
673 return lldb::SBCommand(new_command_sp);
674 return lldb::SBCommand();
675}
676
677lldb::SBCommand
678SBCommand::AddCommand (const char* name, lldb::SBCommandPluginInterface *impl, const char* help)
679{
680 if (!IsValid ())
681 return lldb::SBCommand();
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000682 if (!m_opaque_sp->IsMultiwordObject())
Enrico Granata21dfcd92012-09-28 23:57:51 +0000683 return lldb::SBCommand();
684 lldb::CommandObjectSP new_command_sp;
685 new_command_sp.reset(new CommandPluginInterfaceImplementation(m_opaque_sp->GetCommandInterpreter(),name,impl,help));
686 if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp))
687 return lldb::SBCommand(new_command_sp);
688 return lldb::SBCommand();
689}
Enrico Granatae87764f2015-05-27 05:04:35 +0000690
Abhishek Aggarwalf605c192016-07-29 07:46:32 +0000691lldb::SBCommand
692SBCommand::AddCommand (const char* name, lldb::SBCommandPluginInterface *impl, const char* help, const char* syntax)
693{
694 if (!IsValid ())
695 return lldb::SBCommand();
696 if (!m_opaque_sp->IsMultiwordObject())
697 return lldb::SBCommand();
698 lldb::CommandObjectSP new_command_sp;
699 new_command_sp.reset(new CommandPluginInterfaceImplementation(m_opaque_sp->GetCommandInterpreter(),name,impl,help, syntax));
700 if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp))
701 return lldb::SBCommand(new_command_sp);
702 return lldb::SBCommand();
703}
704
705
Enrico Granatae87764f2015-05-27 05:04:35 +0000706uint32_t
707SBCommand::GetFlags ()
708{
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +0000709 return (IsValid() ? m_opaque_sp->GetFlags().Get() : 0);
Enrico Granatae87764f2015-05-27 05:04:35 +0000710}
711
712void
713SBCommand::SetFlags (uint32_t flags)
714{
715 if (IsValid())
716 m_opaque_sp->GetFlags().Set(flags);
717}