blob: 7a1ef1a77839549eeaf30e5e2a1cff04842e696e [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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/lldb-types.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "lldb/Core/SourceManager.h"
14#include "lldb/Core/Listener.h"
15#include "lldb/Interpreter/CommandInterpreter.h"
Enrico Granata21dfcd92012-09-28 23:57:51 +000016#include "lldb/Interpreter/CommandObjectMultiword.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Interpreter/CommandReturnObject.h"
18#include "lldb/Target/Target.h"
19
Eli Friedmanca93cc12010-06-09 07:37:52 +000020#include "lldb/API/SBBroadcaster.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000021#include "lldb/API/SBCommandReturnObject.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000022#include "lldb/API/SBCommandInterpreter.h"
Ilia Kaf10e1c2015-03-21 10:53:37 +000023#include "lldb/API/SBEvent.h"
Jim Inghamffc9f1d2014-10-14 01:20:07 +000024#include "lldb/API/SBExecutionContext.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000025#include "lldb/API/SBProcess.h"
26#include "lldb/API/SBTarget.h"
27#include "lldb/API/SBListener.h"
Caroline Ticeceb6b132010-10-26 03:11:13 +000028#include "lldb/API/SBStream.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000029#include "lldb/API/SBStringList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
31using namespace lldb;
32using namespace lldb_private;
33
Jim Ingham26c7bf92014-10-11 00:38:27 +000034SBCommandInterpreterRunOptions::SBCommandInterpreterRunOptions()
35{
36 m_opaque_up.reset(new CommandInterpreterRunOptions());
37}
38
39SBCommandInterpreterRunOptions::~SBCommandInterpreterRunOptions()
40{
41
42}
43
44bool
45SBCommandInterpreterRunOptions::GetStopOnContinue () const
46{
47 return m_opaque_up->GetStopOnContinue();
48}
49
50void
51SBCommandInterpreterRunOptions::SetStopOnContinue (bool stop_on_continue)
52{
53 m_opaque_up->SetStopOnContinue(stop_on_continue);
54}
55
56bool
57SBCommandInterpreterRunOptions::GetStopOnError () const
58{
59 return m_opaque_up->GetStopOnError();
60}
61
62void
63SBCommandInterpreterRunOptions::SetStopOnError (bool stop_on_error)
64{
65 m_opaque_up->SetStopOnError(stop_on_error);
66}
67
68bool
69SBCommandInterpreterRunOptions::GetStopOnCrash () const
70{
71 return m_opaque_up->GetStopOnCrash();
72}
73
74void
75SBCommandInterpreterRunOptions::SetStopOnCrash (bool stop_on_crash)
76{
77 m_opaque_up->SetStopOnCrash(stop_on_crash);
78}
79
80bool
81SBCommandInterpreterRunOptions::GetEchoCommands () const
82{
83 return m_opaque_up->GetEchoCommands();
84}
85
86void
87SBCommandInterpreterRunOptions::SetEchoCommands (bool echo_commands)
88{
89 m_opaque_up->SetEchoCommands(echo_commands);
90}
91
92bool
93SBCommandInterpreterRunOptions::GetPrintResults () const
94{
95 return m_opaque_up->GetPrintResults();
96}
97
98void
99SBCommandInterpreterRunOptions::SetPrintResults (bool print_results)
100{
101 m_opaque_up->SetPrintResults(print_results);
102}
103
104bool
105SBCommandInterpreterRunOptions::GetAddToHistory () const
106{
107 return m_opaque_up->GetAddToHistory();
108}
109
110void
111SBCommandInterpreterRunOptions::SetAddToHistory (bool add_to_history)
112{
113 m_opaque_up->SetAddToHistory(add_to_history);
114}
115
116lldb_private::CommandInterpreterRunOptions *
117SBCommandInterpreterRunOptions::get () const
118{
119 return m_opaque_up.get();
120}
121
122lldb_private::CommandInterpreterRunOptions &
123SBCommandInterpreterRunOptions::ref () const
124{
125 return *m_opaque_up.get();
126}
127
Enrico Granata21dfcd92012-09-28 23:57:51 +0000128class CommandPluginInterfaceImplementation : public CommandObjectParsed
129{
130public:
131 CommandPluginInterfaceImplementation (CommandInterpreter &interpreter,
132 const char *name,
133 lldb::SBCommandPluginInterface* backend,
134 const char *help = NULL,
135 const char *syntax = NULL,
136 uint32_t flags = 0) :
137 CommandObjectParsed (interpreter, name, help, syntax, flags),
138 m_backend(backend) {}
139
140 virtual bool
Greg Clayton3a18e312012-10-08 22:41:53 +0000141 IsRemovable() const { return true; }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000142
143protected:
144 virtual bool
145 DoExecute (Args& command, CommandReturnObject &result)
146 {
147 SBCommandReturnObject sb_return(&result);
148 SBCommandInterpreter sb_interpreter(&m_interpreter);
149 SBDebugger debugger_sb(m_interpreter.GetDebugger().shared_from_this());
150 bool ret = m_backend->DoExecute (debugger_sb,(char**)command.GetArgumentVector(), sb_return);
151 sb_return.Release();
152 return ret;
153 }
154 lldb::SBCommandPluginInterface* m_backend;
155};
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156
Greg Clayton66111032010-06-23 01:19:29 +0000157SBCommandInterpreter::SBCommandInterpreter (CommandInterpreter *interpreter) :
158 m_opaque_ptr (interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159{
Greg Clayton5160ce52013-03-27 23:08:40 +0000160 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000161
162 if (log)
Greg Clayton48381312010-10-30 04:51:46 +0000163 log->Printf ("SBCommandInterpreter::SBCommandInterpreter (interpreter=%p)"
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000164 " => SBCommandInterpreter(%p)",
165 static_cast<void*>(interpreter),
166 static_cast<void*>(m_opaque_ptr));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167}
168
Greg Claytonefabb122010-11-05 23:17:00 +0000169SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs) :
170 m_opaque_ptr (rhs.m_opaque_ptr)
171{
172}
173
174const SBCommandInterpreter &
175SBCommandInterpreter::operator = (const SBCommandInterpreter &rhs)
176{
177 m_opaque_ptr = rhs.m_opaque_ptr;
178 return *this;
179}
180
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181SBCommandInterpreter::~SBCommandInterpreter ()
182{
183}
184
185bool
Greg Clayton66111032010-06-23 01:19:29 +0000186SBCommandInterpreter::IsValid() const
187{
188 return m_opaque_ptr != NULL;
189}
190
191
192bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193SBCommandInterpreter::CommandExists (const char *cmd)
194{
Johnny Chen872e0622011-12-19 21:16:29 +0000195 if (cmd && m_opaque_ptr)
Greg Clayton66111032010-06-23 01:19:29 +0000196 return m_opaque_ptr->CommandExists (cmd);
197 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198}
199
200bool
201SBCommandInterpreter::AliasExists (const char *cmd)
202{
Johnny Chen872e0622011-12-19 21:16:29 +0000203 if (cmd && m_opaque_ptr)
Greg Clayton66111032010-06-23 01:19:29 +0000204 return m_opaque_ptr->AliasExists (cmd);
205 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206}
207
Greg Clayton44d93782014-01-27 23:43:24 +0000208bool
209SBCommandInterpreter::IsActive ()
210{
211 if (m_opaque_ptr)
212 return m_opaque_ptr->IsActive ();
213 return false;
214}
215
216const char *
217SBCommandInterpreter::GetIOHandlerControlSequence(char ch)
218{
219 if (m_opaque_ptr)
220 return m_opaque_ptr->GetDebugger().GetTopIOHandlerControlSequence (ch).GetCString();
221 return NULL;
222}
223
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224lldb::ReturnStatus
225SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history)
226{
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000227 SBExecutionContext sb_exe_ctx;
228 return HandleCommand (command_line, sb_exe_ctx, result, add_to_history);
229}
230
231lldb::ReturnStatus
232SBCommandInterpreter::HandleCommand (const char *command_line, SBExecutionContext &override_context, SBCommandReturnObject &result, bool add_to_history)
233{
Greg Clayton5160ce52013-03-27 23:08:40 +0000234 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000235
236 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000237 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p), add_to_history=%i)",
238 static_cast<void*>(m_opaque_ptr), command_line,
239 static_cast<void*>(result.get()), add_to_history);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000240
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000241 ExecutionContext ctx, *ctx_ptr;
242 if (override_context.get())
243 {
244 ctx = override_context.get()->Lock(true);
245 ctx_ptr = &ctx;
246 }
247 else
248 ctx_ptr = nullptr;
249
250
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251 result.Clear();
Johnny Chen872e0622011-12-19 21:16:29 +0000252 if (command_line && m_opaque_ptr)
Greg Clayton66111032010-06-23 01:19:29 +0000253 {
Greg Clayton45a44f32014-07-15 00:25:59 +0000254 result.ref().SetInteractive(false);
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000255 m_opaque_ptr->HandleCommand (command_line, add_to_history ? eLazyBoolYes : eLazyBoolNo, result.ref(), ctx_ptr);
Greg Clayton66111032010-06-23 01:19:29 +0000256 }
257 else
258 {
Johnny Chen872e0622011-12-19 21:16:29 +0000259 result->AppendError ("SBCommandInterpreter or the command line is not valid");
Greg Clayton66111032010-06-23 01:19:29 +0000260 result->SetStatus (eReturnStatusFailed);
261 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000262
Caroline Tice7b9da4a2010-10-27 21:23:37 +0000263 // We need to get the value again, in case the command disabled the log!
264 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000265 if (log)
266 {
267 SBStream sstr;
268 result.GetDescription (sstr);
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000269 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p): %s, add_to_history=%i) => %i",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000270 static_cast<void*>(m_opaque_ptr), command_line,
271 static_cast<void*>(result.get()), sstr.GetData(),
272 add_to_history, result.GetStatus());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000273 }
274
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275 return result.GetStatus();
276}
277
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000278void
279SBCommandInterpreter::HandleCommandsFromFile (lldb::SBFileSpec &file,
280 lldb::SBExecutionContext &override_context,
281 lldb::SBCommandInterpreterRunOptions &options,
282 lldb::SBCommandReturnObject result)
283{
284 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
285
286 if (log)
287 {
288 SBStream s;
289 file.GetDescription (s);
290 log->Printf ("SBCommandInterpreter(%p)::HandleCommandsFromFile (file=\"%s\", SBCommandReturnObject(%p))",
291 static_cast<void*>(m_opaque_ptr), s.GetData(),
292 static_cast<void*>(result.get()));
293 }
294
295 if (!m_opaque_ptr)
296 {
297 result->AppendError ("SBCommandInterpreter is not valid.");
298 result->SetStatus (eReturnStatusFailed);
299 return;
300 }
301
302 if (!file.IsValid())
303 {
304 SBStream s;
305 file.GetDescription (s);
306 result->AppendErrorWithFormat ("File is not valid: %s.", s.GetData());
307 result->SetStatus (eReturnStatusFailed);
308 }
309
310 FileSpec tmp_spec = file.ref();
311 ExecutionContext ctx, *ctx_ptr;
312 if (override_context.get())
313 {
314 ctx = override_context.get()->Lock(true);
315 ctx_ptr = &ctx;
316 }
317 else
318 ctx_ptr = nullptr;
319
320
321 m_opaque_ptr->HandleCommandsFromFile (tmp_spec, ctx_ptr, options.ref(), result.ref());
322
323}
324
325
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326int
327SBCommandInterpreter::HandleCompletion (const char *current_line,
328 const char *cursor,
329 const char *last_char,
330 int match_start_point,
331 int max_return_elements,
332 SBStringList &matches)
333{
Greg Clayton5160ce52013-03-27 23:08:40 +0000334 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton66111032010-06-23 01:19:29 +0000335 int num_completions = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000336
Jim Inghamac012602011-12-05 19:24:15 +0000337 // Sanity check the arguments that are passed in:
338 // cursor & last_char have to be within the current_line.
339 if (current_line == NULL || cursor == NULL || last_char == NULL)
340 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000341
Jim Inghamac012602011-12-05 19:24:15 +0000342 if (cursor < current_line || last_char < current_line)
343 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000344
Jim Inghamac012602011-12-05 19:24:15 +0000345 size_t current_line_size = strlen (current_line);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +0000346 if (cursor - current_line > static_cast<ptrdiff_t>(current_line_size) ||
347 last_char - current_line > static_cast<ptrdiff_t>(current_line_size))
Jim Inghamac012602011-12-05 19:24:15 +0000348 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000349
Jim Ingham389512d2012-06-26 01:21:59 +0000350 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000351 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 +0000352 static_cast<void*>(m_opaque_ptr), current_line,
353 static_cast<uint64_t>(cursor - current_line),
354 static_cast<uint64_t>(last_char - current_line),
355 match_start_point, max_return_elements);
356
Greg Clayton66111032010-06-23 01:19:29 +0000357 if (m_opaque_ptr)
358 {
359 lldb_private::StringList lldb_matches;
360 num_completions = m_opaque_ptr->HandleCompletion (current_line, cursor, last_char, match_start_point,
361 max_return_elements, lldb_matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362
Greg Clayton66111032010-06-23 01:19:29 +0000363 SBStringList temp_list (&lldb_matches);
364 matches.AppendList (temp_list);
365 }
Jim Ingham389512d2012-06-26 01:21:59 +0000366 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000367 log->Printf ("SBCommandInterpreter(%p)::HandleCompletion - Found %d completions.",
368 static_cast<void*>(m_opaque_ptr), num_completions);
369
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370 return num_completions;
371}
372
Jim Ingham969795f2011-09-21 01:17:13 +0000373int
374SBCommandInterpreter::HandleCompletion (const char *current_line,
375 uint32_t cursor_pos,
376 int match_start_point,
377 int max_return_elements,
378 lldb::SBStringList &matches)
379{
380 const char *cursor = current_line + cursor_pos;
381 const char *last_char = current_line + strlen (current_line);
382 return HandleCompletion (current_line, cursor, last_char, match_start_point, max_return_elements, matches);
383}
384
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385bool
386SBCommandInterpreter::HasCommands ()
387{
Greg Clayton66111032010-06-23 01:19:29 +0000388 if (m_opaque_ptr)
389 return m_opaque_ptr->HasCommands();
390 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391}
392
393bool
394SBCommandInterpreter::HasAliases ()
395{
Greg Clayton66111032010-06-23 01:19:29 +0000396 if (m_opaque_ptr)
397 return m_opaque_ptr->HasAliases();
398 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399}
400
401bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402SBCommandInterpreter::HasAliasOptions ()
403{
Greg Clayton66111032010-06-23 01:19:29 +0000404 if (m_opaque_ptr)
405 return m_opaque_ptr->HasAliasOptions ();
406 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407}
408
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409SBProcess
410SBCommandInterpreter::GetProcess ()
411{
Greg Claytonb9556ac2012-01-30 07:41:31 +0000412 SBProcess sb_process;
413 ProcessSP process_sp;
Greg Clayton66111032010-06-23 01:19:29 +0000414 if (m_opaque_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415 {
Greg Claytonaf67cec2010-12-20 20:49:23 +0000416 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
417 if (target_sp)
418 {
419 Mutex::Locker api_locker(target_sp->GetAPIMutex());
Greg Claytonb9556ac2012-01-30 07:41:31 +0000420 process_sp = target_sp->GetProcessSP();
421 sb_process.SetSP(process_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +0000422 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000424 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000425
426 if (log)
427 log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000428 static_cast<void*>(m_opaque_ptr),
429 static_cast<void*>(process_sp.get()));
Greg Clayton48381312010-10-30 04:51:46 +0000430
Greg Claytonb9556ac2012-01-30 07:41:31 +0000431 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432}
433
Enrico Granata21dfcd92012-09-28 23:57:51 +0000434SBDebugger
435SBCommandInterpreter::GetDebugger ()
436{
437 SBDebugger sb_debugger;
438 if (m_opaque_ptr)
439 sb_debugger.reset(m_opaque_ptr->GetDebugger().shared_from_this());
Greg Clayton5160ce52013-03-27 23:08:40 +0000440 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000441
Enrico Granata21dfcd92012-09-28 23:57:51 +0000442 if (log)
443 log->Printf ("SBCommandInterpreter(%p)::GetDebugger () => SBDebugger(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000444 static_cast<void*>(m_opaque_ptr),
445 static_cast<void*>(sb_debugger.get()));
446
Enrico Granata21dfcd92012-09-28 23:57:51 +0000447 return sb_debugger;
448}
449
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450CommandInterpreter *
Greg Clayton66111032010-06-23 01:19:29 +0000451SBCommandInterpreter::get ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452{
Greg Clayton66111032010-06-23 01:19:29 +0000453 return m_opaque_ptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454}
455
456CommandInterpreter &
Greg Clayton66111032010-06-23 01:19:29 +0000457SBCommandInterpreter::ref ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458{
Greg Clayton66111032010-06-23 01:19:29 +0000459 assert (m_opaque_ptr);
460 return *m_opaque_ptr;
461}
462
463void
464SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
465{
466 m_opaque_ptr = interpreter;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467}
468
469void
470SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
471{
472 result.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000473 if (m_opaque_ptr)
474 {
Greg Claytonaf67cec2010-12-20 20:49:23 +0000475 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
476 Mutex::Locker api_locker;
477 if (target_sp)
Jim Ingham10ebffa2012-05-04 23:02:50 +0000478 api_locker.Lock(target_sp->GetAPIMutex());
Greg Clayton66111032010-06-23 01:19:29 +0000479 m_opaque_ptr->SourceInitFile (false, result.ref());
480 }
481 else
482 {
483 result->AppendError ("SBCommandInterpreter is not valid");
484 result->SetStatus (eReturnStatusFailed);
485 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000486 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000487
488 if (log)
489 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000490 static_cast<void*>(m_opaque_ptr),
491 static_cast<void*>(result.get()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492}
493
494void
495SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result)
496{
497 result.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000498 if (m_opaque_ptr)
499 {
Greg Claytonaf67cec2010-12-20 20:49:23 +0000500 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
501 Mutex::Locker api_locker;
502 if (target_sp)
Jim Ingham10ebffa2012-05-04 23:02:50 +0000503 api_locker.Lock(target_sp->GetAPIMutex());
Greg Clayton66111032010-06-23 01:19:29 +0000504 m_opaque_ptr->SourceInitFile (true, result.ref());
505 }
506 else
507 {
508 result->AppendError ("SBCommandInterpreter is not valid");
509 result->SetStatus (eReturnStatusFailed);
510 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000511 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000512
513 if (log)
514 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000515 static_cast<void*>(m_opaque_ptr),
516 static_cast<void*>(result.get()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000517}
518
519SBBroadcaster
520SBCommandInterpreter::GetBroadcaster ()
521{
Greg Clayton5160ce52013-03-27 23:08:40 +0000522 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000523
Greg Clayton66111032010-06-23 01:19:29 +0000524 SBBroadcaster broadcaster (m_opaque_ptr, false);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000525
526 if (log)
Greg Clayton48381312010-10-30 04:51:46 +0000527 log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000528 static_cast<void*>(m_opaque_ptr), static_cast<void*>(broadcaster.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000529
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530 return broadcaster;
531}
532
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000533const char *
534SBCommandInterpreter::GetBroadcasterClass ()
535{
Ilia K8a00a562015-03-17 16:54:52 +0000536 return CommandInterpreter::GetStaticBroadcasterClass().AsCString();
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000537}
538
Greg Clayton9d0402b2011-02-20 02:15:07 +0000539const char *
540SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
541{
542 return CommandObject::GetArgumentTypeAsCString (arg_type);
543}
544
545const char *
546SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
547{
548 return CommandObject::GetArgumentDescriptionAsCString (arg_type);
549}
550
Greg Claytona9f7b792012-02-29 04:21:24 +0000551bool
Ilia Kaf10e1c2015-03-21 10:53:37 +0000552SBCommandInterpreter::EventIsCommandInterpreterEvent (const lldb::SBEvent &event)
553{
554 return strcmp (event.GetBroadcasterClass(), SBCommandInterpreter::GetBroadcasterClass()) == 0;
555}
556
557bool
Greg Claytona9f7b792012-02-29 04:21:24 +0000558SBCommandInterpreter::SetCommandOverrideCallback (const char *command_name,
559 lldb::CommandOverrideCallback callback,
560 void *baton)
561{
562 if (command_name && command_name[0] && m_opaque_ptr)
563 {
Greg Claytonaf6f2752012-05-08 04:29:20 +0000564 std::string command_name_str (command_name);
565 CommandObject *cmd_obj = m_opaque_ptr->GetCommandObjectForCommand(command_name_str);
Greg Claytona9f7b792012-02-29 04:21:24 +0000566 if (cmd_obj)
567 {
Greg Claytonaf6f2752012-05-08 04:29:20 +0000568 assert(command_name_str.empty());
Greg Claytona9f7b792012-02-29 04:21:24 +0000569 cmd_obj->SetOverrideCallback (callback, baton);
570 return true;
571 }
572 }
573 return false;
574}
Greg Clayton9d0402b2011-02-20 02:15:07 +0000575
Greg Claytondce502e2011-11-04 03:34:56 +0000576#ifndef LLDB_DISABLE_PYTHON
Enrico Granatabe93a352011-08-16 16:49:25 +0000577
Greg Claytondce502e2011-11-04 03:34:56 +0000578// Defined in the SWIG source file
579extern "C" void
580init_lldb(void);
581
Greg Clayton8afa5432013-10-17 00:27:14 +0000582// these are the Pythonic implementations of the required callbacks
583// these are scripting-language specific, which is why they belong here
584// we still need to use function pointers to them instead of relying
585// on linkage-time resolution because the SWIG stuff and this file
586// get built at different times
587extern "C" bool
588LLDBSwigPythonBreakpointCallbackFunction (const char *python_function_name,
589 const char *session_dictionary_name,
Jason Molendab57e4a12013-11-04 09:33:30 +0000590 const lldb::StackFrameSP& sb_frame,
Greg Clayton8afa5432013-10-17 00:27:14 +0000591 const lldb::BreakpointLocationSP& sb_bp_loc);
Enrico Granatabe93a352011-08-16 16:49:25 +0000592
Greg Clayton8afa5432013-10-17 00:27:14 +0000593extern "C" bool
594LLDBSwigPythonWatchpointCallbackFunction (const char *python_function_name,
595 const char *session_dictionary_name,
Jason Molendab57e4a12013-11-04 09:33:30 +0000596 const lldb::StackFrameSP& sb_frame,
Greg Clayton8afa5432013-10-17 00:27:14 +0000597 const lldb::WatchpointSP& sb_wp);
Greg Claytonfc36f7912011-03-22 01:14:58 +0000598
Greg Clayton8afa5432013-10-17 00:27:14 +0000599extern "C" bool
600LLDBSwigPythonCallTypeScript (const char *python_function_name,
601 void *session_dictionary,
602 const lldb::ValueObjectSP& valobj_sp,
603 void** pyfunct_wrapper,
Enrico Granata7e4df562014-11-22 00:02:47 +0000604 const lldb::TypeSummaryOptionsSP& options_sp,
Greg Clayton8afa5432013-10-17 00:27:14 +0000605 std::string& retval);
606
607extern "C" void*
608LLDBSwigPythonCreateSyntheticProvider (const char *python_class_name,
609 const char *session_dictionary_name,
610 const lldb::ValueObjectSP& valobj_sp);
611
Enrico Granata9fe00e52015-03-13 02:20:41 +0000612extern "C" void*
613LLDBSwigPythonCreateCommandObject (const char *python_class_name,
614 const char *session_dictionary_name,
615 const lldb::DebuggerSP debugger_sp);
Greg Clayton8afa5432013-10-17 00:27:14 +0000616
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000617extern "C" void*
618LLDBSwigPythonCreateScriptedThreadPlan (const char *python_class_name,
619 const char *session_dictionary_name,
620 const lldb::ThreadPlanSP& thread_plan_sp);
621
622extern "C" bool
623LLDBSWIGPythonCallThreadPlan (void *implementor,
624 const char *method_name,
625 Event *event_sp,
626 bool &got_error);
627
Siva Chandra870602d2015-03-16 19:01:33 +0000628extern "C" size_t
Greg Clayton8afa5432013-10-17 00:27:14 +0000629LLDBSwigPython_CalculateNumChildren (void *implementor);
630
631extern "C" void *
632LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx);
633
634extern "C" int
635LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name);
636
637extern "C" void *
638LLDBSWIGPython_CastPyObjectToSBValue (void* data);
639
640extern lldb::ValueObjectSP
641LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data);
642
643extern "C" bool
644LLDBSwigPython_UpdateSynthProviderInstance (void* implementor);
645
646extern "C" bool
647LLDBSwigPython_MightHaveChildrenSynthProviderInstance (void* implementor);
648
Enrico Granatad07cfd32014-10-08 18:27:36 +0000649extern "C" void *
650LLDBSwigPython_GetValueSynthProviderInstance (void* implementor);
651
Greg Clayton8afa5432013-10-17 00:27:14 +0000652extern "C" bool
653LLDBSwigPythonCallCommand (const char *python_function_name,
654 const char *session_dictionary_name,
655 lldb::DebuggerSP& debugger,
656 const char* args,
Enrico Granata06be0592014-10-01 21:47:29 +0000657 lldb_private::CommandReturnObject &cmd_retobj,
658 lldb::ExecutionContextRefSP exe_ctx_ref_sp);
Greg Clayton8afa5432013-10-17 00:27:14 +0000659
660extern "C" bool
Enrico Granata9fe00e52015-03-13 02:20:41 +0000661LLDBSwigPythonCallCommandObject (void *implementor,
662 lldb::DebuggerSP& debugger,
663 const char* args,
664 lldb_private::CommandReturnObject& cmd_retobj,
665 lldb::ExecutionContextRefSP exe_ctx_ref_sp);
666
667extern "C" bool
Greg Clayton8afa5432013-10-17 00:27:14 +0000668LLDBSwigPythonCallModuleInit (const char *python_module_name,
669 const char *session_dictionary_name,
670 lldb::DebuggerSP& debugger);
671
672extern "C" void*
673LLDBSWIGPythonCreateOSPlugin (const char *python_class_name,
674 const char *session_dictionary_name,
675 const lldb::ProcessSP& process_sp);
676
677extern "C" bool
678LLDBSWIGPythonRunScriptKeywordProcess (const char* python_function_name,
679 const char* session_dictionary_name,
680 lldb::ProcessSP& process,
681 std::string& output);
682
683extern "C" bool
684LLDBSWIGPythonRunScriptKeywordThread (const char* python_function_name,
685 const char* session_dictionary_name,
686 lldb::ThreadSP& thread,
687 std::string& output);
688
689extern "C" bool
690LLDBSWIGPythonRunScriptKeywordTarget (const char* python_function_name,
691 const char* session_dictionary_name,
692 lldb::TargetSP& target,
693 std::string& output);
694
695extern "C" bool
696LLDBSWIGPythonRunScriptKeywordFrame (const char* python_function_name,
697 const char* session_dictionary_name,
Jason Molendab57e4a12013-11-04 09:33:30 +0000698 lldb::StackFrameSP& frame,
Greg Clayton8afa5432013-10-17 00:27:14 +0000699 std::string& output);
700
Enrico Granata88282c62014-10-28 21:07:00 +0000701extern "C" bool
702LLDBSWIGPythonRunScriptKeywordValue (const char* python_function_name,
703 const char* session_dictionary_name,
704 lldb::ValueObjectSP& value,
705 std::string& output);
706
Greg Clayton8afa5432013-10-17 00:27:14 +0000707extern "C" void*
708LLDBSWIGPython_GetDynamicSetting (void* module,
709 const char* setting,
710 const lldb::TargetSP& target_sp);
711
Greg Claytondce502e2011-11-04 03:34:56 +0000712
713#endif
714
Greg Claytonfc36f7912011-03-22 01:14:58 +0000715void
716SBCommandInterpreter::InitializeSWIG ()
717{
718 static bool g_initialized = false;
719 if (!g_initialized)
720 {
721 g_initialized = true;
Greg Claytondce502e2011-11-04 03:34:56 +0000722#ifndef LLDB_DISABLE_PYTHON
Greg Clayton8afa5432013-10-17 00:27:14 +0000723 ScriptInterpreter::InitializeInterpreter (init_lldb,
724 LLDBSwigPythonBreakpointCallbackFunction,
725 LLDBSwigPythonWatchpointCallbackFunction,
726 LLDBSwigPythonCallTypeScript,
727 LLDBSwigPythonCreateSyntheticProvider,
Enrico Granata9fe00e52015-03-13 02:20:41 +0000728 LLDBSwigPythonCreateCommandObject,
Greg Clayton8afa5432013-10-17 00:27:14 +0000729 LLDBSwigPython_CalculateNumChildren,
730 LLDBSwigPython_GetChildAtIndex,
731 LLDBSwigPython_GetIndexOfChildWithName,
732 LLDBSWIGPython_CastPyObjectToSBValue,
733 LLDBSWIGPython_GetValueObjectSPFromSBValue,
734 LLDBSwigPython_UpdateSynthProviderInstance,
735 LLDBSwigPython_MightHaveChildrenSynthProviderInstance,
Enrico Granatad07cfd32014-10-08 18:27:36 +0000736 LLDBSwigPython_GetValueSynthProviderInstance,
Greg Clayton8afa5432013-10-17 00:27:14 +0000737 LLDBSwigPythonCallCommand,
Enrico Granata9fe00e52015-03-13 02:20:41 +0000738 LLDBSwigPythonCallCommandObject,
Greg Clayton8afa5432013-10-17 00:27:14 +0000739 LLDBSwigPythonCallModuleInit,
740 LLDBSWIGPythonCreateOSPlugin,
741 LLDBSWIGPythonRunScriptKeywordProcess,
742 LLDBSWIGPythonRunScriptKeywordThread,
743 LLDBSWIGPythonRunScriptKeywordTarget,
744 LLDBSWIGPythonRunScriptKeywordFrame,
Enrico Granata88282c62014-10-28 21:07:00 +0000745 LLDBSWIGPythonRunScriptKeywordValue,
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000746 LLDBSWIGPython_GetDynamicSetting,
747 LLDBSwigPythonCreateScriptedThreadPlan,
748 LLDBSWIGPythonCallThreadPlan);
Greg Claytondce502e2011-11-04 03:34:56 +0000749#endif
Greg Claytonfc36f7912011-03-22 01:14:58 +0000750 }
751}
Greg Claytona9f7b792012-02-29 04:21:24 +0000752
Enrico Granata21dfcd92012-09-28 23:57:51 +0000753lldb::SBCommand
754SBCommandInterpreter::AddMultiwordCommand (const char* name, const char* help)
755{
756 CommandObjectMultiword *new_command = new CommandObjectMultiword(*m_opaque_ptr,name,help);
757 new_command->SetRemovable (true);
758 lldb::CommandObjectSP new_command_sp(new_command);
759 if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
760 return lldb::SBCommand(new_command_sp);
761 return lldb::SBCommand();
762}
763
764lldb::SBCommand
765SBCommandInterpreter::AddCommand (const char* name, lldb::SBCommandPluginInterface* impl, const char* help)
766{
767 lldb::CommandObjectSP new_command_sp;
768 new_command_sp.reset(new CommandPluginInterfaceImplementation(*m_opaque_ptr,name,impl,help));
769
770 if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
771 return lldb::SBCommand(new_command_sp);
772 return lldb::SBCommand();
773}
774
775SBCommand::SBCommand ()
776{}
777
778SBCommand::SBCommand (lldb::CommandObjectSP cmd_sp) : m_opaque_sp (cmd_sp)
779{}
780
781bool
782SBCommand::IsValid ()
783{
784 return (bool)m_opaque_sp;
785}
786
787const char*
788SBCommand::GetName ()
789{
790 if (IsValid ())
791 return m_opaque_sp->GetCommandName ();
792 return NULL;
793}
794
795const char*
796SBCommand::GetHelp ()
797{
798 if (IsValid ())
799 return m_opaque_sp->GetHelp ();
800 return NULL;
801}
802
Enrico Granatacc342da2015-03-13 22:32:11 +0000803const char*
804SBCommand::GetHelpLong ()
805{
806 if (IsValid ())
807 return m_opaque_sp->GetHelpLong ();
808 return NULL;
809}
810
811void
812SBCommand::SetHelp (const char* help)
813{
814 if (IsValid())
815 m_opaque_sp->SetHelp(help);
816}
817
818void
819SBCommand::SetHelpLong (const char* help)
820{
821 if (IsValid())
822 m_opaque_sp->SetHelpLong(help);
823}
824
Enrico Granata21dfcd92012-09-28 23:57:51 +0000825lldb::SBCommand
826SBCommand::AddMultiwordCommand (const char* name, const char* help)
827{
828 if (!IsValid ())
829 return lldb::SBCommand();
830 if (m_opaque_sp->IsMultiwordObject() == false)
831 return lldb::SBCommand();
832 CommandObjectMultiword *new_command = new CommandObjectMultiword(m_opaque_sp->GetCommandInterpreter(),name,help);
833 new_command->SetRemovable (true);
834 lldb::CommandObjectSP new_command_sp(new_command);
835 if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp))
836 return lldb::SBCommand(new_command_sp);
837 return lldb::SBCommand();
838}
839
840lldb::SBCommand
841SBCommand::AddCommand (const char* name, lldb::SBCommandPluginInterface *impl, const char* help)
842{
843 if (!IsValid ())
844 return lldb::SBCommand();
845 if (m_opaque_sp->IsMultiwordObject() == false)
846 return lldb::SBCommand();
847 lldb::CommandObjectSP new_command_sp;
848 new_command_sp.reset(new CommandPluginInterfaceImplementation(m_opaque_sp->GetCommandInterpreter(),name,impl,help));
849 if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp))
850 return lldb::SBCommand(new_command_sp);
851 return lldb::SBCommand();
852}
853