blob: bc8d6d34aa90eb02546cfe7c99fad2b05aeb6d44 [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"
23#include "lldb/API/SBProcess.h"
24#include "lldb/API/SBTarget.h"
25#include "lldb/API/SBListener.h"
Caroline Ticeceb6b132010-10-26 03:11:13 +000026#include "lldb/API/SBStream.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000027#include "lldb/API/SBStringList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028
29using namespace lldb;
30using namespace lldb_private;
31
Jim Ingham26c7bf92014-10-11 00:38:27 +000032SBCommandInterpreterRunOptions::SBCommandInterpreterRunOptions()
33{
34 m_opaque_up.reset(new CommandInterpreterRunOptions());
35}
36
37SBCommandInterpreterRunOptions::~SBCommandInterpreterRunOptions()
38{
39
40}
41
42bool
43SBCommandInterpreterRunOptions::GetStopOnContinue () const
44{
45 return m_opaque_up->GetStopOnContinue();
46}
47
48void
49SBCommandInterpreterRunOptions::SetStopOnContinue (bool stop_on_continue)
50{
51 m_opaque_up->SetStopOnContinue(stop_on_continue);
52}
53
54bool
55SBCommandInterpreterRunOptions::GetStopOnError () const
56{
57 return m_opaque_up->GetStopOnError();
58}
59
60void
61SBCommandInterpreterRunOptions::SetStopOnError (bool stop_on_error)
62{
63 m_opaque_up->SetStopOnError(stop_on_error);
64}
65
66bool
67SBCommandInterpreterRunOptions::GetStopOnCrash () const
68{
69 return m_opaque_up->GetStopOnCrash();
70}
71
72void
73SBCommandInterpreterRunOptions::SetStopOnCrash (bool stop_on_crash)
74{
75 m_opaque_up->SetStopOnCrash(stop_on_crash);
76}
77
78bool
79SBCommandInterpreterRunOptions::GetEchoCommands () const
80{
81 return m_opaque_up->GetEchoCommands();
82}
83
84void
85SBCommandInterpreterRunOptions::SetEchoCommands (bool echo_commands)
86{
87 m_opaque_up->SetEchoCommands(echo_commands);
88}
89
90bool
91SBCommandInterpreterRunOptions::GetPrintResults () const
92{
93 return m_opaque_up->GetPrintResults();
94}
95
96void
97SBCommandInterpreterRunOptions::SetPrintResults (bool print_results)
98{
99 m_opaque_up->SetPrintResults(print_results);
100}
101
102bool
103SBCommandInterpreterRunOptions::GetAddToHistory () const
104{
105 return m_opaque_up->GetAddToHistory();
106}
107
108void
109SBCommandInterpreterRunOptions::SetAddToHistory (bool add_to_history)
110{
111 m_opaque_up->SetAddToHistory(add_to_history);
112}
113
114lldb_private::CommandInterpreterRunOptions *
115SBCommandInterpreterRunOptions::get () const
116{
117 return m_opaque_up.get();
118}
119
120lldb_private::CommandInterpreterRunOptions &
121SBCommandInterpreterRunOptions::ref () const
122{
123 return *m_opaque_up.get();
124}
125
Enrico Granata21dfcd92012-09-28 23:57:51 +0000126class CommandPluginInterfaceImplementation : public CommandObjectParsed
127{
128public:
129 CommandPluginInterfaceImplementation (CommandInterpreter &interpreter,
130 const char *name,
131 lldb::SBCommandPluginInterface* backend,
132 const char *help = NULL,
133 const char *syntax = NULL,
134 uint32_t flags = 0) :
135 CommandObjectParsed (interpreter, name, help, syntax, flags),
136 m_backend(backend) {}
137
138 virtual bool
Greg Clayton3a18e312012-10-08 22:41:53 +0000139 IsRemovable() const { return true; }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000140
141protected:
142 virtual bool
143 DoExecute (Args& command, CommandReturnObject &result)
144 {
145 SBCommandReturnObject sb_return(&result);
146 SBCommandInterpreter sb_interpreter(&m_interpreter);
147 SBDebugger debugger_sb(m_interpreter.GetDebugger().shared_from_this());
148 bool ret = m_backend->DoExecute (debugger_sb,(char**)command.GetArgumentVector(), sb_return);
149 sb_return.Release();
150 return ret;
151 }
152 lldb::SBCommandPluginInterface* m_backend;
153};
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154
Greg Clayton66111032010-06-23 01:19:29 +0000155SBCommandInterpreter::SBCommandInterpreter (CommandInterpreter *interpreter) :
156 m_opaque_ptr (interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157{
Greg Clayton5160ce52013-03-27 23:08:40 +0000158 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000159
160 if (log)
Greg Clayton48381312010-10-30 04:51:46 +0000161 log->Printf ("SBCommandInterpreter::SBCommandInterpreter (interpreter=%p)"
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000162 " => SBCommandInterpreter(%p)",
163 static_cast<void*>(interpreter),
164 static_cast<void*>(m_opaque_ptr));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165}
166
Greg Claytonefabb122010-11-05 23:17:00 +0000167SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs) :
168 m_opaque_ptr (rhs.m_opaque_ptr)
169{
170}
171
172const SBCommandInterpreter &
173SBCommandInterpreter::operator = (const SBCommandInterpreter &rhs)
174{
175 m_opaque_ptr = rhs.m_opaque_ptr;
176 return *this;
177}
178
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179SBCommandInterpreter::~SBCommandInterpreter ()
180{
181}
182
183bool
Greg Clayton66111032010-06-23 01:19:29 +0000184SBCommandInterpreter::IsValid() const
185{
186 return m_opaque_ptr != NULL;
187}
188
189
190bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191SBCommandInterpreter::CommandExists (const char *cmd)
192{
Johnny Chen872e0622011-12-19 21:16:29 +0000193 if (cmd && m_opaque_ptr)
Greg Clayton66111032010-06-23 01:19:29 +0000194 return m_opaque_ptr->CommandExists (cmd);
195 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196}
197
198bool
199SBCommandInterpreter::AliasExists (const char *cmd)
200{
Johnny Chen872e0622011-12-19 21:16:29 +0000201 if (cmd && m_opaque_ptr)
Greg Clayton66111032010-06-23 01:19:29 +0000202 return m_opaque_ptr->AliasExists (cmd);
203 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204}
205
Greg Clayton44d93782014-01-27 23:43:24 +0000206bool
207SBCommandInterpreter::IsActive ()
208{
209 if (m_opaque_ptr)
210 return m_opaque_ptr->IsActive ();
211 return false;
212}
213
214const char *
215SBCommandInterpreter::GetIOHandlerControlSequence(char ch)
216{
217 if (m_opaque_ptr)
218 return m_opaque_ptr->GetDebugger().GetTopIOHandlerControlSequence (ch).GetCString();
219 return NULL;
220}
221
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222lldb::ReturnStatus
223SBCommandInterpreter::HandleCommand (const char *command_line, 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232 result.Clear();
Johnny Chen872e0622011-12-19 21:16:29 +0000233 if (command_line && m_opaque_ptr)
Greg Clayton66111032010-06-23 01:19:29 +0000234 {
Greg Clayton45a44f32014-07-15 00:25:59 +0000235 result.ref().SetInteractive(false);
Enrico Granata5f5ab602012-05-31 01:09:06 +0000236 m_opaque_ptr->HandleCommand (command_line, add_to_history ? eLazyBoolYes : eLazyBoolNo, result.ref());
Greg Clayton66111032010-06-23 01:19:29 +0000237 }
238 else
239 {
Johnny Chen872e0622011-12-19 21:16:29 +0000240 result->AppendError ("SBCommandInterpreter or the command line is not valid");
Greg Clayton66111032010-06-23 01:19:29 +0000241 result->SetStatus (eReturnStatusFailed);
242 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000243
Caroline Tice7b9da4a2010-10-27 21:23:37 +0000244 // We need to get the value again, in case the command disabled the log!
245 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000246 if (log)
247 {
248 SBStream sstr;
249 result.GetDescription (sstr);
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000250 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p): %s, add_to_history=%i) => %i",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000251 static_cast<void*>(m_opaque_ptr), command_line,
252 static_cast<void*>(result.get()), sstr.GetData(),
253 add_to_history, result.GetStatus());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000254 }
255
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256 return result.GetStatus();
257}
258
259int
260SBCommandInterpreter::HandleCompletion (const char *current_line,
261 const char *cursor,
262 const char *last_char,
263 int match_start_point,
264 int max_return_elements,
265 SBStringList &matches)
266{
Greg Clayton5160ce52013-03-27 23:08:40 +0000267 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton66111032010-06-23 01:19:29 +0000268 int num_completions = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000269
Jim Inghamac012602011-12-05 19:24:15 +0000270 // Sanity check the arguments that are passed in:
271 // cursor & last_char have to be within the current_line.
272 if (current_line == NULL || cursor == NULL || last_char == NULL)
273 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000274
Jim Inghamac012602011-12-05 19:24:15 +0000275 if (cursor < current_line || last_char < current_line)
276 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000277
Jim Inghamac012602011-12-05 19:24:15 +0000278 size_t current_line_size = strlen (current_line);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +0000279 if (cursor - current_line > static_cast<ptrdiff_t>(current_line_size) ||
280 last_char - current_line > static_cast<ptrdiff_t>(current_line_size))
Jim Inghamac012602011-12-05 19:24:15 +0000281 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000282
Jim Ingham389512d2012-06-26 01:21:59 +0000283 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000284 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 +0000285 static_cast<void*>(m_opaque_ptr), current_line,
286 static_cast<uint64_t>(cursor - current_line),
287 static_cast<uint64_t>(last_char - current_line),
288 match_start_point, max_return_elements);
289
Greg Clayton66111032010-06-23 01:19:29 +0000290 if (m_opaque_ptr)
291 {
292 lldb_private::StringList lldb_matches;
293 num_completions = m_opaque_ptr->HandleCompletion (current_line, cursor, last_char, match_start_point,
294 max_return_elements, lldb_matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295
Greg Clayton66111032010-06-23 01:19:29 +0000296 SBStringList temp_list (&lldb_matches);
297 matches.AppendList (temp_list);
298 }
Jim Ingham389512d2012-06-26 01:21:59 +0000299 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000300 log->Printf ("SBCommandInterpreter(%p)::HandleCompletion - Found %d completions.",
301 static_cast<void*>(m_opaque_ptr), num_completions);
302
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303 return num_completions;
304}
305
Jim Ingham969795f2011-09-21 01:17:13 +0000306int
307SBCommandInterpreter::HandleCompletion (const char *current_line,
308 uint32_t cursor_pos,
309 int match_start_point,
310 int max_return_elements,
311 lldb::SBStringList &matches)
312{
313 const char *cursor = current_line + cursor_pos;
314 const char *last_char = current_line + strlen (current_line);
315 return HandleCompletion (current_line, cursor, last_char, match_start_point, max_return_elements, matches);
316}
317
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000318bool
319SBCommandInterpreter::HasCommands ()
320{
Greg Clayton66111032010-06-23 01:19:29 +0000321 if (m_opaque_ptr)
322 return m_opaque_ptr->HasCommands();
323 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324}
325
326bool
327SBCommandInterpreter::HasAliases ()
328{
Greg Clayton66111032010-06-23 01:19:29 +0000329 if (m_opaque_ptr)
330 return m_opaque_ptr->HasAliases();
331 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332}
333
334bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335SBCommandInterpreter::HasAliasOptions ()
336{
Greg Clayton66111032010-06-23 01:19:29 +0000337 if (m_opaque_ptr)
338 return m_opaque_ptr->HasAliasOptions ();
339 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340}
341
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342SBProcess
343SBCommandInterpreter::GetProcess ()
344{
Greg Claytonb9556ac2012-01-30 07:41:31 +0000345 SBProcess sb_process;
346 ProcessSP process_sp;
Greg Clayton66111032010-06-23 01:19:29 +0000347 if (m_opaque_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348 {
Greg Claytonaf67cec2010-12-20 20:49:23 +0000349 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
350 if (target_sp)
351 {
352 Mutex::Locker api_locker(target_sp->GetAPIMutex());
Greg Claytonb9556ac2012-01-30 07:41:31 +0000353 process_sp = target_sp->GetProcessSP();
354 sb_process.SetSP(process_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +0000355 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000356 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000357 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000358
359 if (log)
360 log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000361 static_cast<void*>(m_opaque_ptr),
362 static_cast<void*>(process_sp.get()));
Greg Clayton48381312010-10-30 04:51:46 +0000363
Greg Claytonb9556ac2012-01-30 07:41:31 +0000364 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365}
366
Enrico Granata21dfcd92012-09-28 23:57:51 +0000367SBDebugger
368SBCommandInterpreter::GetDebugger ()
369{
370 SBDebugger sb_debugger;
371 if (m_opaque_ptr)
372 sb_debugger.reset(m_opaque_ptr->GetDebugger().shared_from_this());
Greg Clayton5160ce52013-03-27 23:08:40 +0000373 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000374
Enrico Granata21dfcd92012-09-28 23:57:51 +0000375 if (log)
376 log->Printf ("SBCommandInterpreter(%p)::GetDebugger () => SBDebugger(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000377 static_cast<void*>(m_opaque_ptr),
378 static_cast<void*>(sb_debugger.get()));
379
Enrico Granata21dfcd92012-09-28 23:57:51 +0000380 return sb_debugger;
381}
382
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383CommandInterpreter *
Greg Clayton66111032010-06-23 01:19:29 +0000384SBCommandInterpreter::get ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385{
Greg Clayton66111032010-06-23 01:19:29 +0000386 return m_opaque_ptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387}
388
389CommandInterpreter &
Greg Clayton66111032010-06-23 01:19:29 +0000390SBCommandInterpreter::ref ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391{
Greg Clayton66111032010-06-23 01:19:29 +0000392 assert (m_opaque_ptr);
393 return *m_opaque_ptr;
394}
395
396void
397SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
398{
399 m_opaque_ptr = interpreter;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400}
401
402void
403SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
404{
405 result.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000406 if (m_opaque_ptr)
407 {
Greg Claytonaf67cec2010-12-20 20:49:23 +0000408 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
409 Mutex::Locker api_locker;
410 if (target_sp)
Jim Ingham10ebffa2012-05-04 23:02:50 +0000411 api_locker.Lock(target_sp->GetAPIMutex());
Greg Clayton66111032010-06-23 01:19:29 +0000412 m_opaque_ptr->SourceInitFile (false, result.ref());
413 }
414 else
415 {
416 result->AppendError ("SBCommandInterpreter is not valid");
417 result->SetStatus (eReturnStatusFailed);
418 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000419 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000420
421 if (log)
422 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000423 static_cast<void*>(m_opaque_ptr),
424 static_cast<void*>(result.get()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425}
426
427void
428SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result)
429{
430 result.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000431 if (m_opaque_ptr)
432 {
Greg Claytonaf67cec2010-12-20 20:49:23 +0000433 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
434 Mutex::Locker api_locker;
435 if (target_sp)
Jim Ingham10ebffa2012-05-04 23:02:50 +0000436 api_locker.Lock(target_sp->GetAPIMutex());
Greg Clayton66111032010-06-23 01:19:29 +0000437 m_opaque_ptr->SourceInitFile (true, result.ref());
438 }
439 else
440 {
441 result->AppendError ("SBCommandInterpreter is not valid");
442 result->SetStatus (eReturnStatusFailed);
443 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000444 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000445
446 if (log)
447 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000448 static_cast<void*>(m_opaque_ptr),
449 static_cast<void*>(result.get()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450}
451
452SBBroadcaster
453SBCommandInterpreter::GetBroadcaster ()
454{
Greg Clayton5160ce52013-03-27 23:08:40 +0000455 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000456
Greg Clayton66111032010-06-23 01:19:29 +0000457 SBBroadcaster broadcaster (m_opaque_ptr, false);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000458
459 if (log)
Greg Clayton48381312010-10-30 04:51:46 +0000460 log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000461 static_cast<void*>(m_opaque_ptr), static_cast<void*>(broadcaster.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000462
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463 return broadcaster;
464}
465
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000466const char *
467SBCommandInterpreter::GetBroadcasterClass ()
468{
469 return Communication::GetStaticBroadcasterClass().AsCString();
470}
471
Greg Clayton9d0402b2011-02-20 02:15:07 +0000472const char *
473SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
474{
475 return CommandObject::GetArgumentTypeAsCString (arg_type);
476}
477
478const char *
479SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
480{
481 return CommandObject::GetArgumentDescriptionAsCString (arg_type);
482}
483
Greg Claytona9f7b792012-02-29 04:21:24 +0000484bool
485SBCommandInterpreter::SetCommandOverrideCallback (const char *command_name,
486 lldb::CommandOverrideCallback callback,
487 void *baton)
488{
489 if (command_name && command_name[0] && m_opaque_ptr)
490 {
Greg Claytonaf6f2752012-05-08 04:29:20 +0000491 std::string command_name_str (command_name);
492 CommandObject *cmd_obj = m_opaque_ptr->GetCommandObjectForCommand(command_name_str);
Greg Claytona9f7b792012-02-29 04:21:24 +0000493 if (cmd_obj)
494 {
Greg Claytonaf6f2752012-05-08 04:29:20 +0000495 assert(command_name_str.empty());
Greg Claytona9f7b792012-02-29 04:21:24 +0000496 cmd_obj->SetOverrideCallback (callback, baton);
497 return true;
498 }
499 }
500 return false;
501}
Greg Clayton9d0402b2011-02-20 02:15:07 +0000502
Greg Claytondce502e2011-11-04 03:34:56 +0000503#ifndef LLDB_DISABLE_PYTHON
Enrico Granatabe93a352011-08-16 16:49:25 +0000504
Greg Claytondce502e2011-11-04 03:34:56 +0000505// Defined in the SWIG source file
506extern "C" void
507init_lldb(void);
508
Greg Clayton8afa5432013-10-17 00:27:14 +0000509// these are the Pythonic implementations of the required callbacks
510// these are scripting-language specific, which is why they belong here
511// we still need to use function pointers to them instead of relying
512// on linkage-time resolution because the SWIG stuff and this file
513// get built at different times
514extern "C" bool
515LLDBSwigPythonBreakpointCallbackFunction (const char *python_function_name,
516 const char *session_dictionary_name,
Jason Molendab57e4a12013-11-04 09:33:30 +0000517 const lldb::StackFrameSP& sb_frame,
Greg Clayton8afa5432013-10-17 00:27:14 +0000518 const lldb::BreakpointLocationSP& sb_bp_loc);
Enrico Granatabe93a352011-08-16 16:49:25 +0000519
Greg Clayton8afa5432013-10-17 00:27:14 +0000520extern "C" bool
521LLDBSwigPythonWatchpointCallbackFunction (const char *python_function_name,
522 const char *session_dictionary_name,
Jason Molendab57e4a12013-11-04 09:33:30 +0000523 const lldb::StackFrameSP& sb_frame,
Greg Clayton8afa5432013-10-17 00:27:14 +0000524 const lldb::WatchpointSP& sb_wp);
Greg Claytonfc36f7912011-03-22 01:14:58 +0000525
Greg Clayton8afa5432013-10-17 00:27:14 +0000526extern "C" bool
527LLDBSwigPythonCallTypeScript (const char *python_function_name,
528 void *session_dictionary,
529 const lldb::ValueObjectSP& valobj_sp,
530 void** pyfunct_wrapper,
531 std::string& retval);
532
533extern "C" void*
534LLDBSwigPythonCreateSyntheticProvider (const char *python_class_name,
535 const char *session_dictionary_name,
536 const lldb::ValueObjectSP& valobj_sp);
537
538
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000539extern "C" void*
540LLDBSwigPythonCreateScriptedThreadPlan (const char *python_class_name,
541 const char *session_dictionary_name,
542 const lldb::ThreadPlanSP& thread_plan_sp);
543
544extern "C" bool
545LLDBSWIGPythonCallThreadPlan (void *implementor,
546 const char *method_name,
547 Event *event_sp,
548 bool &got_error);
549
Greg Clayton8afa5432013-10-17 00:27:14 +0000550extern "C" uint32_t
551LLDBSwigPython_CalculateNumChildren (void *implementor);
552
553extern "C" void *
554LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx);
555
556extern "C" int
557LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name);
558
559extern "C" void *
560LLDBSWIGPython_CastPyObjectToSBValue (void* data);
561
562extern lldb::ValueObjectSP
563LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data);
564
565extern "C" bool
566LLDBSwigPython_UpdateSynthProviderInstance (void* implementor);
567
568extern "C" bool
569LLDBSwigPython_MightHaveChildrenSynthProviderInstance (void* implementor);
570
Enrico Granatad07cfd32014-10-08 18:27:36 +0000571extern "C" void *
572LLDBSwigPython_GetValueSynthProviderInstance (void* implementor);
573
Greg Clayton8afa5432013-10-17 00:27:14 +0000574extern "C" bool
575LLDBSwigPythonCallCommand (const char *python_function_name,
576 const char *session_dictionary_name,
577 lldb::DebuggerSP& debugger,
578 const char* args,
Enrico Granata06be0592014-10-01 21:47:29 +0000579 lldb_private::CommandReturnObject &cmd_retobj,
580 lldb::ExecutionContextRefSP exe_ctx_ref_sp);
Greg Clayton8afa5432013-10-17 00:27:14 +0000581
582extern "C" bool
583LLDBSwigPythonCallModuleInit (const char *python_module_name,
584 const char *session_dictionary_name,
585 lldb::DebuggerSP& debugger);
586
587extern "C" void*
588LLDBSWIGPythonCreateOSPlugin (const char *python_class_name,
589 const char *session_dictionary_name,
590 const lldb::ProcessSP& process_sp);
591
592extern "C" bool
593LLDBSWIGPythonRunScriptKeywordProcess (const char* python_function_name,
594 const char* session_dictionary_name,
595 lldb::ProcessSP& process,
596 std::string& output);
597
598extern "C" bool
599LLDBSWIGPythonRunScriptKeywordThread (const char* python_function_name,
600 const char* session_dictionary_name,
601 lldb::ThreadSP& thread,
602 std::string& output);
603
604extern "C" bool
605LLDBSWIGPythonRunScriptKeywordTarget (const char* python_function_name,
606 const char* session_dictionary_name,
607 lldb::TargetSP& target,
608 std::string& output);
609
610extern "C" bool
611LLDBSWIGPythonRunScriptKeywordFrame (const char* python_function_name,
612 const char* session_dictionary_name,
Jason Molendab57e4a12013-11-04 09:33:30 +0000613 lldb::StackFrameSP& frame,
Greg Clayton8afa5432013-10-17 00:27:14 +0000614 std::string& output);
615
616extern "C" void*
617LLDBSWIGPython_GetDynamicSetting (void* module,
618 const char* setting,
619 const lldb::TargetSP& target_sp);
620
Greg Claytondce502e2011-11-04 03:34:56 +0000621
622#endif
623
Greg Claytonfc36f7912011-03-22 01:14:58 +0000624void
625SBCommandInterpreter::InitializeSWIG ()
626{
627 static bool g_initialized = false;
628 if (!g_initialized)
629 {
630 g_initialized = true;
Greg Claytondce502e2011-11-04 03:34:56 +0000631#ifndef LLDB_DISABLE_PYTHON
Greg Clayton8afa5432013-10-17 00:27:14 +0000632 ScriptInterpreter::InitializeInterpreter (init_lldb,
633 LLDBSwigPythonBreakpointCallbackFunction,
634 LLDBSwigPythonWatchpointCallbackFunction,
635 LLDBSwigPythonCallTypeScript,
636 LLDBSwigPythonCreateSyntheticProvider,
637 LLDBSwigPython_CalculateNumChildren,
638 LLDBSwigPython_GetChildAtIndex,
639 LLDBSwigPython_GetIndexOfChildWithName,
640 LLDBSWIGPython_CastPyObjectToSBValue,
641 LLDBSWIGPython_GetValueObjectSPFromSBValue,
642 LLDBSwigPython_UpdateSynthProviderInstance,
643 LLDBSwigPython_MightHaveChildrenSynthProviderInstance,
Enrico Granatad07cfd32014-10-08 18:27:36 +0000644 LLDBSwigPython_GetValueSynthProviderInstance,
Greg Clayton8afa5432013-10-17 00:27:14 +0000645 LLDBSwigPythonCallCommand,
646 LLDBSwigPythonCallModuleInit,
647 LLDBSWIGPythonCreateOSPlugin,
648 LLDBSWIGPythonRunScriptKeywordProcess,
649 LLDBSWIGPythonRunScriptKeywordThread,
650 LLDBSWIGPythonRunScriptKeywordTarget,
651 LLDBSWIGPythonRunScriptKeywordFrame,
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000652 LLDBSWIGPython_GetDynamicSetting,
653 LLDBSwigPythonCreateScriptedThreadPlan,
654 LLDBSWIGPythonCallThreadPlan);
Greg Claytondce502e2011-11-04 03:34:56 +0000655#endif
Greg Claytonfc36f7912011-03-22 01:14:58 +0000656 }
657}
Greg Claytona9f7b792012-02-29 04:21:24 +0000658
Enrico Granata21dfcd92012-09-28 23:57:51 +0000659lldb::SBCommand
660SBCommandInterpreter::AddMultiwordCommand (const char* name, const char* help)
661{
662 CommandObjectMultiword *new_command = new CommandObjectMultiword(*m_opaque_ptr,name,help);
663 new_command->SetRemovable (true);
664 lldb::CommandObjectSP new_command_sp(new_command);
665 if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
666 return lldb::SBCommand(new_command_sp);
667 return lldb::SBCommand();
668}
669
670lldb::SBCommand
671SBCommandInterpreter::AddCommand (const char* name, lldb::SBCommandPluginInterface* impl, const char* help)
672{
673 lldb::CommandObjectSP new_command_sp;
674 new_command_sp.reset(new CommandPluginInterfaceImplementation(*m_opaque_ptr,name,impl,help));
675
676 if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
677 return lldb::SBCommand(new_command_sp);
678 return lldb::SBCommand();
679}
680
681SBCommand::SBCommand ()
682{}
683
684SBCommand::SBCommand (lldb::CommandObjectSP cmd_sp) : m_opaque_sp (cmd_sp)
685{}
686
687bool
688SBCommand::IsValid ()
689{
690 return (bool)m_opaque_sp;
691}
692
693const char*
694SBCommand::GetName ()
695{
696 if (IsValid ())
697 return m_opaque_sp->GetCommandName ();
698 return NULL;
699}
700
701const char*
702SBCommand::GetHelp ()
703{
704 if (IsValid ())
705 return m_opaque_sp->GetHelp ();
706 return NULL;
707}
708
709lldb::SBCommand
710SBCommand::AddMultiwordCommand (const char* name, const char* help)
711{
712 if (!IsValid ())
713 return lldb::SBCommand();
714 if (m_opaque_sp->IsMultiwordObject() == false)
715 return lldb::SBCommand();
716 CommandObjectMultiword *new_command = new CommandObjectMultiword(m_opaque_sp->GetCommandInterpreter(),name,help);
717 new_command->SetRemovable (true);
718 lldb::CommandObjectSP new_command_sp(new_command);
719 if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp))
720 return lldb::SBCommand(new_command_sp);
721 return lldb::SBCommand();
722}
723
724lldb::SBCommand
725SBCommand::AddCommand (const char* name, lldb::SBCommandPluginInterface *impl, const char* help)
726{
727 if (!IsValid ())
728 return lldb::SBCommand();
729 if (m_opaque_sp->IsMultiwordObject() == false)
730 return lldb::SBCommand();
731 lldb::CommandObjectSP new_command_sp;
732 new_command_sp.reset(new CommandPluginInterfaceImplementation(m_opaque_sp->GetCommandInterpreter(),name,impl,help));
733 if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp))
734 return lldb::SBCommand(new_command_sp);
735 return lldb::SBCommand();
736}
737