blob: 193d06e4d9201da88efcedb16a616ff745b99f1f [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"
Jim Inghamffc9f1d2014-10-14 01:20:07 +000023#include "lldb/API/SBExecutionContext.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000024#include "lldb/API/SBProcess.h"
25#include "lldb/API/SBTarget.h"
26#include "lldb/API/SBListener.h"
Caroline Ticeceb6b132010-10-26 03:11:13 +000027#include "lldb/API/SBStream.h"
Eli Friedmanca93cc12010-06-09 07:37:52 +000028#include "lldb/API/SBStringList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
30using namespace lldb;
31using namespace lldb_private;
32
Jim Ingham26c7bf92014-10-11 00:38:27 +000033SBCommandInterpreterRunOptions::SBCommandInterpreterRunOptions()
34{
35 m_opaque_up.reset(new CommandInterpreterRunOptions());
36}
37
38SBCommandInterpreterRunOptions::~SBCommandInterpreterRunOptions()
39{
40
41}
42
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:
130 CommandPluginInterfaceImplementation (CommandInterpreter &interpreter,
131 const char *name,
132 lldb::SBCommandPluginInterface* backend,
133 const char *help = NULL,
134 const char *syntax = NULL,
135 uint32_t flags = 0) :
136 CommandObjectParsed (interpreter, name, help, syntax, flags),
137 m_backend(backend) {}
138
139 virtual bool
Greg Clayton3a18e312012-10-08 22:41:53 +0000140 IsRemovable() const { return true; }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000141
142protected:
143 virtual bool
144 DoExecute (Args& command, CommandReturnObject &result)
145 {
146 SBCommandReturnObject sb_return(&result);
147 SBCommandInterpreter sb_interpreter(&m_interpreter);
148 SBDebugger debugger_sb(m_interpreter.GetDebugger().shared_from_this());
149 bool ret = m_backend->DoExecute (debugger_sb,(char**)command.GetArgumentVector(), sb_return);
150 sb_return.Release();
151 return ret;
152 }
153 lldb::SBCommandPluginInterface* m_backend;
154};
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155
Greg Clayton66111032010-06-23 01:19:29 +0000156SBCommandInterpreter::SBCommandInterpreter (CommandInterpreter *interpreter) :
157 m_opaque_ptr (interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158{
Greg Clayton5160ce52013-03-27 23:08:40 +0000159 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000160
161 if (log)
Greg Clayton48381312010-10-30 04:51:46 +0000162 log->Printf ("SBCommandInterpreter::SBCommandInterpreter (interpreter=%p)"
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000163 " => SBCommandInterpreter(%p)",
164 static_cast<void*>(interpreter),
165 static_cast<void*>(m_opaque_ptr));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166}
167
Greg Claytonefabb122010-11-05 23:17:00 +0000168SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs) :
169 m_opaque_ptr (rhs.m_opaque_ptr)
170{
171}
172
173const SBCommandInterpreter &
174SBCommandInterpreter::operator = (const SBCommandInterpreter &rhs)
175{
176 m_opaque_ptr = rhs.m_opaque_ptr;
177 return *this;
178}
179
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180SBCommandInterpreter::~SBCommandInterpreter ()
181{
182}
183
184bool
Greg Clayton66111032010-06-23 01:19:29 +0000185SBCommandInterpreter::IsValid() const
186{
187 return m_opaque_ptr != NULL;
188}
189
190
191bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192SBCommandInterpreter::CommandExists (const char *cmd)
193{
Johnny Chen872e0622011-12-19 21:16:29 +0000194 if (cmd && m_opaque_ptr)
Greg Clayton66111032010-06-23 01:19:29 +0000195 return m_opaque_ptr->CommandExists (cmd);
196 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000197}
198
199bool
200SBCommandInterpreter::AliasExists (const char *cmd)
201{
Johnny Chen872e0622011-12-19 21:16:29 +0000202 if (cmd && m_opaque_ptr)
Greg Clayton66111032010-06-23 01:19:29 +0000203 return m_opaque_ptr->AliasExists (cmd);
204 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205}
206
Greg Clayton44d93782014-01-27 23:43:24 +0000207bool
208SBCommandInterpreter::IsActive ()
209{
210 if (m_opaque_ptr)
211 return m_opaque_ptr->IsActive ();
212 return false;
213}
214
215const char *
216SBCommandInterpreter::GetIOHandlerControlSequence(char ch)
217{
218 if (m_opaque_ptr)
219 return m_opaque_ptr->GetDebugger().GetTopIOHandlerControlSequence (ch).GetCString();
220 return NULL;
221}
222
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223lldb::ReturnStatus
224SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history)
225{
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000226 SBExecutionContext sb_exe_ctx;
227 return HandleCommand (command_line, sb_exe_ctx, result, add_to_history);
228}
229
230lldb::ReturnStatus
231SBCommandInterpreter::HandleCommand (const char *command_line, SBExecutionContext &override_context, SBCommandReturnObject &result, bool add_to_history)
232{
Greg Clayton5160ce52013-03-27 23:08:40 +0000233 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000234
235 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000236 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p), add_to_history=%i)",
237 static_cast<void*>(m_opaque_ptr), command_line,
238 static_cast<void*>(result.get()), add_to_history);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000239
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000240 ExecutionContext ctx, *ctx_ptr;
241 if (override_context.get())
242 {
243 ctx = override_context.get()->Lock(true);
244 ctx_ptr = &ctx;
245 }
246 else
247 ctx_ptr = nullptr;
248
249
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250 result.Clear();
Johnny Chen872e0622011-12-19 21:16:29 +0000251 if (command_line && m_opaque_ptr)
Greg Clayton66111032010-06-23 01:19:29 +0000252 {
Greg Clayton45a44f32014-07-15 00:25:59 +0000253 result.ref().SetInteractive(false);
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000254 m_opaque_ptr->HandleCommand (command_line, add_to_history ? eLazyBoolYes : eLazyBoolNo, result.ref(), ctx_ptr);
Greg Clayton66111032010-06-23 01:19:29 +0000255 }
256 else
257 {
Johnny Chen872e0622011-12-19 21:16:29 +0000258 result->AppendError ("SBCommandInterpreter or the command line is not valid");
Greg Clayton66111032010-06-23 01:19:29 +0000259 result->SetStatus (eReturnStatusFailed);
260 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000261
Caroline Tice7b9da4a2010-10-27 21:23:37 +0000262 // We need to get the value again, in case the command disabled the log!
263 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000264 if (log)
265 {
266 SBStream sstr;
267 result.GetDescription (sstr);
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000268 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p): %s, add_to_history=%i) => %i",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000269 static_cast<void*>(m_opaque_ptr), command_line,
270 static_cast<void*>(result.get()), sstr.GetData(),
271 add_to_history, result.GetStatus());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000272 }
273
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000274 return result.GetStatus();
275}
276
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000277void
278SBCommandInterpreter::HandleCommandsFromFile (lldb::SBFileSpec &file,
279 lldb::SBExecutionContext &override_context,
280 lldb::SBCommandInterpreterRunOptions &options,
281 lldb::SBCommandReturnObject result)
282{
283 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
284
285 if (log)
286 {
287 SBStream s;
288 file.GetDescription (s);
289 log->Printf ("SBCommandInterpreter(%p)::HandleCommandsFromFile (file=\"%s\", SBCommandReturnObject(%p))",
290 static_cast<void*>(m_opaque_ptr), s.GetData(),
291 static_cast<void*>(result.get()));
292 }
293
294 if (!m_opaque_ptr)
295 {
296 result->AppendError ("SBCommandInterpreter is not valid.");
297 result->SetStatus (eReturnStatusFailed);
298 return;
299 }
300
301 if (!file.IsValid())
302 {
303 SBStream s;
304 file.GetDescription (s);
305 result->AppendErrorWithFormat ("File is not valid: %s.", s.GetData());
306 result->SetStatus (eReturnStatusFailed);
307 }
308
309 FileSpec tmp_spec = file.ref();
310 ExecutionContext ctx, *ctx_ptr;
311 if (override_context.get())
312 {
313 ctx = override_context.get()->Lock(true);
314 ctx_ptr = &ctx;
315 }
316 else
317 ctx_ptr = nullptr;
318
319
320 m_opaque_ptr->HandleCommandsFromFile (tmp_spec, ctx_ptr, options.ref(), result.ref());
321
322}
323
324
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325int
326SBCommandInterpreter::HandleCompletion (const char *current_line,
327 const char *cursor,
328 const char *last_char,
329 int match_start_point,
330 int max_return_elements,
331 SBStringList &matches)
332{
Greg Clayton5160ce52013-03-27 23:08:40 +0000333 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton66111032010-06-23 01:19:29 +0000334 int num_completions = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000335
Jim Inghamac012602011-12-05 19:24:15 +0000336 // Sanity check the arguments that are passed in:
337 // cursor & last_char have to be within the current_line.
338 if (current_line == NULL || cursor == NULL || last_char == NULL)
339 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000340
Jim Inghamac012602011-12-05 19:24:15 +0000341 if (cursor < current_line || last_char < current_line)
342 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000343
Jim Inghamac012602011-12-05 19:24:15 +0000344 size_t current_line_size = strlen (current_line);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +0000345 if (cursor - current_line > static_cast<ptrdiff_t>(current_line_size) ||
346 last_char - current_line > static_cast<ptrdiff_t>(current_line_size))
Jim Inghamac012602011-12-05 19:24:15 +0000347 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000348
Jim Ingham389512d2012-06-26 01:21:59 +0000349 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000350 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 +0000351 static_cast<void*>(m_opaque_ptr), current_line,
352 static_cast<uint64_t>(cursor - current_line),
353 static_cast<uint64_t>(last_char - current_line),
354 match_start_point, max_return_elements);
355
Greg Clayton66111032010-06-23 01:19:29 +0000356 if (m_opaque_ptr)
357 {
358 lldb_private::StringList lldb_matches;
359 num_completions = m_opaque_ptr->HandleCompletion (current_line, cursor, last_char, match_start_point,
360 max_return_elements, lldb_matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361
Greg Clayton66111032010-06-23 01:19:29 +0000362 SBStringList temp_list (&lldb_matches);
363 matches.AppendList (temp_list);
364 }
Jim Ingham389512d2012-06-26 01:21:59 +0000365 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000366 log->Printf ("SBCommandInterpreter(%p)::HandleCompletion - Found %d completions.",
367 static_cast<void*>(m_opaque_ptr), num_completions);
368
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369 return num_completions;
370}
371
Jim Ingham969795f2011-09-21 01:17:13 +0000372int
373SBCommandInterpreter::HandleCompletion (const char *current_line,
374 uint32_t cursor_pos,
375 int match_start_point,
376 int max_return_elements,
377 lldb::SBStringList &matches)
378{
379 const char *cursor = current_line + cursor_pos;
380 const char *last_char = current_line + strlen (current_line);
381 return HandleCompletion (current_line, cursor, last_char, match_start_point, max_return_elements, matches);
382}
383
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384bool
385SBCommandInterpreter::HasCommands ()
386{
Greg Clayton66111032010-06-23 01:19:29 +0000387 if (m_opaque_ptr)
388 return m_opaque_ptr->HasCommands();
389 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390}
391
392bool
393SBCommandInterpreter::HasAliases ()
394{
Greg Clayton66111032010-06-23 01:19:29 +0000395 if (m_opaque_ptr)
396 return m_opaque_ptr->HasAliases();
397 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398}
399
400bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000401SBCommandInterpreter::HasAliasOptions ()
402{
Greg Clayton66111032010-06-23 01:19:29 +0000403 if (m_opaque_ptr)
404 return m_opaque_ptr->HasAliasOptions ();
405 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406}
407
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408SBProcess
409SBCommandInterpreter::GetProcess ()
410{
Greg Claytonb9556ac2012-01-30 07:41:31 +0000411 SBProcess sb_process;
412 ProcessSP process_sp;
Greg Clayton66111032010-06-23 01:19:29 +0000413 if (m_opaque_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414 {
Greg Claytonaf67cec2010-12-20 20:49:23 +0000415 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
416 if (target_sp)
417 {
418 Mutex::Locker api_locker(target_sp->GetAPIMutex());
Greg Claytonb9556ac2012-01-30 07:41:31 +0000419 process_sp = target_sp->GetProcessSP();
420 sb_process.SetSP(process_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +0000421 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000423 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000424
425 if (log)
426 log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000427 static_cast<void*>(m_opaque_ptr),
428 static_cast<void*>(process_sp.get()));
Greg Clayton48381312010-10-30 04:51:46 +0000429
Greg Claytonb9556ac2012-01-30 07:41:31 +0000430 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431}
432
Enrico Granata21dfcd92012-09-28 23:57:51 +0000433SBDebugger
434SBCommandInterpreter::GetDebugger ()
435{
436 SBDebugger sb_debugger;
437 if (m_opaque_ptr)
438 sb_debugger.reset(m_opaque_ptr->GetDebugger().shared_from_this());
Greg Clayton5160ce52013-03-27 23:08:40 +0000439 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000440
Enrico Granata21dfcd92012-09-28 23:57:51 +0000441 if (log)
442 log->Printf ("SBCommandInterpreter(%p)::GetDebugger () => SBDebugger(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000443 static_cast<void*>(m_opaque_ptr),
444 static_cast<void*>(sb_debugger.get()));
445
Enrico Granata21dfcd92012-09-28 23:57:51 +0000446 return sb_debugger;
447}
448
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449CommandInterpreter *
Greg Clayton66111032010-06-23 01:19:29 +0000450SBCommandInterpreter::get ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451{
Greg Clayton66111032010-06-23 01:19:29 +0000452 return m_opaque_ptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453}
454
455CommandInterpreter &
Greg Clayton66111032010-06-23 01:19:29 +0000456SBCommandInterpreter::ref ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457{
Greg Clayton66111032010-06-23 01:19:29 +0000458 assert (m_opaque_ptr);
459 return *m_opaque_ptr;
460}
461
462void
463SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
464{
465 m_opaque_ptr = interpreter;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466}
467
468void
469SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
470{
471 result.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000472 if (m_opaque_ptr)
473 {
Greg Claytonaf67cec2010-12-20 20:49:23 +0000474 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
475 Mutex::Locker api_locker;
476 if (target_sp)
Jim Ingham10ebffa2012-05-04 23:02:50 +0000477 api_locker.Lock(target_sp->GetAPIMutex());
Greg Clayton66111032010-06-23 01:19:29 +0000478 m_opaque_ptr->SourceInitFile (false, result.ref());
479 }
480 else
481 {
482 result->AppendError ("SBCommandInterpreter is not valid");
483 result->SetStatus (eReturnStatusFailed);
484 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000485 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000486
487 if (log)
488 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000489 static_cast<void*>(m_opaque_ptr),
490 static_cast<void*>(result.get()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491}
492
493void
494SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result)
495{
496 result.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000497 if (m_opaque_ptr)
498 {
Greg Claytonaf67cec2010-12-20 20:49:23 +0000499 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
500 Mutex::Locker api_locker;
501 if (target_sp)
Jim Ingham10ebffa2012-05-04 23:02:50 +0000502 api_locker.Lock(target_sp->GetAPIMutex());
Greg Clayton66111032010-06-23 01:19:29 +0000503 m_opaque_ptr->SourceInitFile (true, result.ref());
504 }
505 else
506 {
507 result->AppendError ("SBCommandInterpreter is not valid");
508 result->SetStatus (eReturnStatusFailed);
509 }
Greg Clayton5160ce52013-03-27 23:08:40 +0000510 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000511
512 if (log)
513 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000514 static_cast<void*>(m_opaque_ptr),
515 static_cast<void*>(result.get()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516}
517
518SBBroadcaster
519SBCommandInterpreter::GetBroadcaster ()
520{
Greg Clayton5160ce52013-03-27 23:08:40 +0000521 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000522
Greg Clayton66111032010-06-23 01:19:29 +0000523 SBBroadcaster broadcaster (m_opaque_ptr, false);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000524
525 if (log)
Greg Clayton48381312010-10-30 04:51:46 +0000526 log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000527 static_cast<void*>(m_opaque_ptr), static_cast<void*>(broadcaster.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000528
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000529 return broadcaster;
530}
531
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000532const char *
533SBCommandInterpreter::GetBroadcasterClass ()
534{
535 return Communication::GetStaticBroadcasterClass().AsCString();
536}
537
Greg Clayton9d0402b2011-02-20 02:15:07 +0000538const char *
539SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
540{
541 return CommandObject::GetArgumentTypeAsCString (arg_type);
542}
543
544const char *
545SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
546{
547 return CommandObject::GetArgumentDescriptionAsCString (arg_type);
548}
549
Greg Claytona9f7b792012-02-29 04:21:24 +0000550bool
551SBCommandInterpreter::SetCommandOverrideCallback (const char *command_name,
552 lldb::CommandOverrideCallback callback,
553 void *baton)
554{
555 if (command_name && command_name[0] && m_opaque_ptr)
556 {
Greg Claytonaf6f2752012-05-08 04:29:20 +0000557 std::string command_name_str (command_name);
558 CommandObject *cmd_obj = m_opaque_ptr->GetCommandObjectForCommand(command_name_str);
Greg Claytona9f7b792012-02-29 04:21:24 +0000559 if (cmd_obj)
560 {
Greg Claytonaf6f2752012-05-08 04:29:20 +0000561 assert(command_name_str.empty());
Greg Claytona9f7b792012-02-29 04:21:24 +0000562 cmd_obj->SetOverrideCallback (callback, baton);
563 return true;
564 }
565 }
566 return false;
567}
Greg Clayton9d0402b2011-02-20 02:15:07 +0000568
Greg Claytondce502e2011-11-04 03:34:56 +0000569#ifndef LLDB_DISABLE_PYTHON
Enrico Granatabe93a352011-08-16 16:49:25 +0000570
Greg Claytondce502e2011-11-04 03:34:56 +0000571// Defined in the SWIG source file
572extern "C" void
573init_lldb(void);
574
Greg Clayton8afa5432013-10-17 00:27:14 +0000575// these are the Pythonic implementations of the required callbacks
576// these are scripting-language specific, which is why they belong here
577// we still need to use function pointers to them instead of relying
578// on linkage-time resolution because the SWIG stuff and this file
579// get built at different times
580extern "C" bool
581LLDBSwigPythonBreakpointCallbackFunction (const char *python_function_name,
582 const char *session_dictionary_name,
Jason Molendab57e4a12013-11-04 09:33:30 +0000583 const lldb::StackFrameSP& sb_frame,
Greg Clayton8afa5432013-10-17 00:27:14 +0000584 const lldb::BreakpointLocationSP& sb_bp_loc);
Enrico Granatabe93a352011-08-16 16:49:25 +0000585
Greg Clayton8afa5432013-10-17 00:27:14 +0000586extern "C" bool
587LLDBSwigPythonWatchpointCallbackFunction (const char *python_function_name,
588 const char *session_dictionary_name,
Jason Molendab57e4a12013-11-04 09:33:30 +0000589 const lldb::StackFrameSP& sb_frame,
Greg Clayton8afa5432013-10-17 00:27:14 +0000590 const lldb::WatchpointSP& sb_wp);
Greg Claytonfc36f7912011-03-22 01:14:58 +0000591
Greg Clayton8afa5432013-10-17 00:27:14 +0000592extern "C" bool
593LLDBSwigPythonCallTypeScript (const char *python_function_name,
594 void *session_dictionary,
595 const lldb::ValueObjectSP& valobj_sp,
596 void** pyfunct_wrapper,
Enrico Granata7e4df562014-11-22 00:02:47 +0000597 const lldb::TypeSummaryOptionsSP& options_sp,
Greg Clayton8afa5432013-10-17 00:27:14 +0000598 std::string& retval);
599
600extern "C" void*
601LLDBSwigPythonCreateSyntheticProvider (const char *python_class_name,
602 const char *session_dictionary_name,
603 const lldb::ValueObjectSP& valobj_sp);
604
605
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000606extern "C" void*
607LLDBSwigPythonCreateScriptedThreadPlan (const char *python_class_name,
608 const char *session_dictionary_name,
609 const lldb::ThreadPlanSP& thread_plan_sp);
610
611extern "C" bool
612LLDBSWIGPythonCallThreadPlan (void *implementor,
613 const char *method_name,
614 Event *event_sp,
615 bool &got_error);
616
Greg Clayton8afa5432013-10-17 00:27:14 +0000617extern "C" uint32_t
618LLDBSwigPython_CalculateNumChildren (void *implementor);
619
620extern "C" void *
621LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx);
622
623extern "C" int
624LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name);
625
626extern "C" void *
627LLDBSWIGPython_CastPyObjectToSBValue (void* data);
628
629extern lldb::ValueObjectSP
630LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data);
631
632extern "C" bool
633LLDBSwigPython_UpdateSynthProviderInstance (void* implementor);
634
635extern "C" bool
636LLDBSwigPython_MightHaveChildrenSynthProviderInstance (void* implementor);
637
Enrico Granatad07cfd32014-10-08 18:27:36 +0000638extern "C" void *
639LLDBSwigPython_GetValueSynthProviderInstance (void* implementor);
640
Greg Clayton8afa5432013-10-17 00:27:14 +0000641extern "C" bool
642LLDBSwigPythonCallCommand (const char *python_function_name,
643 const char *session_dictionary_name,
644 lldb::DebuggerSP& debugger,
645 const char* args,
Enrico Granata06be0592014-10-01 21:47:29 +0000646 lldb_private::CommandReturnObject &cmd_retobj,
647 lldb::ExecutionContextRefSP exe_ctx_ref_sp);
Greg Clayton8afa5432013-10-17 00:27:14 +0000648
649extern "C" bool
650LLDBSwigPythonCallModuleInit (const char *python_module_name,
651 const char *session_dictionary_name,
652 lldb::DebuggerSP& debugger);
653
654extern "C" void*
655LLDBSWIGPythonCreateOSPlugin (const char *python_class_name,
656 const char *session_dictionary_name,
657 const lldb::ProcessSP& process_sp);
658
659extern "C" bool
660LLDBSWIGPythonRunScriptKeywordProcess (const char* python_function_name,
661 const char* session_dictionary_name,
662 lldb::ProcessSP& process,
663 std::string& output);
664
665extern "C" bool
666LLDBSWIGPythonRunScriptKeywordThread (const char* python_function_name,
667 const char* session_dictionary_name,
668 lldb::ThreadSP& thread,
669 std::string& output);
670
671extern "C" bool
672LLDBSWIGPythonRunScriptKeywordTarget (const char* python_function_name,
673 const char* session_dictionary_name,
674 lldb::TargetSP& target,
675 std::string& output);
676
677extern "C" bool
678LLDBSWIGPythonRunScriptKeywordFrame (const char* python_function_name,
679 const char* session_dictionary_name,
Jason Molendab57e4a12013-11-04 09:33:30 +0000680 lldb::StackFrameSP& frame,
Greg Clayton8afa5432013-10-17 00:27:14 +0000681 std::string& output);
682
Enrico Granata88282c62014-10-28 21:07:00 +0000683extern "C" bool
684LLDBSWIGPythonRunScriptKeywordValue (const char* python_function_name,
685 const char* session_dictionary_name,
686 lldb::ValueObjectSP& value,
687 std::string& output);
688
Greg Clayton8afa5432013-10-17 00:27:14 +0000689extern "C" void*
690LLDBSWIGPython_GetDynamicSetting (void* module,
691 const char* setting,
692 const lldb::TargetSP& target_sp);
693
Greg Claytondce502e2011-11-04 03:34:56 +0000694
695#endif
696
Greg Claytonfc36f7912011-03-22 01:14:58 +0000697void
698SBCommandInterpreter::InitializeSWIG ()
699{
700 static bool g_initialized = false;
701 if (!g_initialized)
702 {
703 g_initialized = true;
Greg Claytondce502e2011-11-04 03:34:56 +0000704#ifndef LLDB_DISABLE_PYTHON
Greg Clayton8afa5432013-10-17 00:27:14 +0000705 ScriptInterpreter::InitializeInterpreter (init_lldb,
706 LLDBSwigPythonBreakpointCallbackFunction,
707 LLDBSwigPythonWatchpointCallbackFunction,
708 LLDBSwigPythonCallTypeScript,
709 LLDBSwigPythonCreateSyntheticProvider,
710 LLDBSwigPython_CalculateNumChildren,
711 LLDBSwigPython_GetChildAtIndex,
712 LLDBSwigPython_GetIndexOfChildWithName,
713 LLDBSWIGPython_CastPyObjectToSBValue,
714 LLDBSWIGPython_GetValueObjectSPFromSBValue,
715 LLDBSwigPython_UpdateSynthProviderInstance,
716 LLDBSwigPython_MightHaveChildrenSynthProviderInstance,
Enrico Granatad07cfd32014-10-08 18:27:36 +0000717 LLDBSwigPython_GetValueSynthProviderInstance,
Greg Clayton8afa5432013-10-17 00:27:14 +0000718 LLDBSwigPythonCallCommand,
719 LLDBSwigPythonCallModuleInit,
720 LLDBSWIGPythonCreateOSPlugin,
721 LLDBSWIGPythonRunScriptKeywordProcess,
722 LLDBSWIGPythonRunScriptKeywordThread,
723 LLDBSWIGPythonRunScriptKeywordTarget,
724 LLDBSWIGPythonRunScriptKeywordFrame,
Enrico Granata88282c62014-10-28 21:07:00 +0000725 LLDBSWIGPythonRunScriptKeywordValue,
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000726 LLDBSWIGPython_GetDynamicSetting,
727 LLDBSwigPythonCreateScriptedThreadPlan,
728 LLDBSWIGPythonCallThreadPlan);
Greg Claytondce502e2011-11-04 03:34:56 +0000729#endif
Greg Claytonfc36f7912011-03-22 01:14:58 +0000730 }
731}
Greg Claytona9f7b792012-02-29 04:21:24 +0000732
Enrico Granata21dfcd92012-09-28 23:57:51 +0000733lldb::SBCommand
734SBCommandInterpreter::AddMultiwordCommand (const char* name, const char* help)
735{
736 CommandObjectMultiword *new_command = new CommandObjectMultiword(*m_opaque_ptr,name,help);
737 new_command->SetRemovable (true);
738 lldb::CommandObjectSP new_command_sp(new_command);
739 if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
740 return lldb::SBCommand(new_command_sp);
741 return lldb::SBCommand();
742}
743
744lldb::SBCommand
745SBCommandInterpreter::AddCommand (const char* name, lldb::SBCommandPluginInterface* impl, const char* help)
746{
747 lldb::CommandObjectSP new_command_sp;
748 new_command_sp.reset(new CommandPluginInterfaceImplementation(*m_opaque_ptr,name,impl,help));
749
750 if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
751 return lldb::SBCommand(new_command_sp);
752 return lldb::SBCommand();
753}
754
755SBCommand::SBCommand ()
756{}
757
758SBCommand::SBCommand (lldb::CommandObjectSP cmd_sp) : m_opaque_sp (cmd_sp)
759{}
760
761bool
762SBCommand::IsValid ()
763{
764 return (bool)m_opaque_sp;
765}
766
767const char*
768SBCommand::GetName ()
769{
770 if (IsValid ())
771 return m_opaque_sp->GetCommandName ();
772 return NULL;
773}
774
775const char*
776SBCommand::GetHelp ()
777{
778 if (IsValid ())
779 return m_opaque_sp->GetHelp ();
780 return NULL;
781}
782
783lldb::SBCommand
784SBCommand::AddMultiwordCommand (const char* name, const char* help)
785{
786 if (!IsValid ())
787 return lldb::SBCommand();
788 if (m_opaque_sp->IsMultiwordObject() == false)
789 return lldb::SBCommand();
790 CommandObjectMultiword *new_command = new CommandObjectMultiword(m_opaque_sp->GetCommandInterpreter(),name,help);
791 new_command->SetRemovable (true);
792 lldb::CommandObjectSP new_command_sp(new_command);
793 if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp))
794 return lldb::SBCommand(new_command_sp);
795 return lldb::SBCommand();
796}
797
798lldb::SBCommand
799SBCommand::AddCommand (const char* name, lldb::SBCommandPluginInterface *impl, const char* help)
800{
801 if (!IsValid ())
802 return lldb::SBCommand();
803 if (m_opaque_sp->IsMultiwordObject() == false)
804 return lldb::SBCommand();
805 lldb::CommandObjectSP new_command_sp;
806 new_command_sp.reset(new CommandPluginInterfaceImplementation(m_opaque_sp->GetCommandInterpreter(),name,impl,help));
807 if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp))
808 return lldb::SBCommand(new_command_sp);
809 return lldb::SBCommand();
810}
811