blob: 7fbf5c2c73d6b1deb1ccb8ed1d54dc4c0d0c6bcd [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- SBCommandInterpreter.cpp --------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/lldb-types.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000011#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "lldb/Core/SourceManager.h"
13#include "lldb/Core/Listener.h"
14#include "lldb/Interpreter/CommandInterpreter.h"
15#include "lldb/Interpreter/CommandReturnObject.h"
16#include "lldb/Target/Target.h"
17
Eli Friedmand6ec8aa2010-06-09 07:37:52 +000018#include "lldb/API/SBBroadcaster.h"
19#include "lldb/API/SBDebugger.h"
20#include "lldb/API/SBCommandReturnObject.h"
Eli Friedmand6ec8aa2010-06-09 07:37:52 +000021#include "lldb/API/SBCommandInterpreter.h"
22#include "lldb/API/SBProcess.h"
23#include "lldb/API/SBTarget.h"
24#include "lldb/API/SBListener.h"
Caroline Tice7826c882010-10-26 03:11:13 +000025#include "lldb/API/SBStream.h"
Eli Friedmand6ec8aa2010-06-09 07:37:52 +000026#include "lldb/API/SBStringList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027
28using namespace lldb;
29using namespace lldb_private;
30
31
Greg Clayton63094e02010-06-23 01:19:29 +000032SBCommandInterpreter::SBCommandInterpreter (CommandInterpreter *interpreter) :
33 m_opaque_ptr (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000034{
Greg Claytone005f2c2010-11-06 01:53:30 +000035 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +000036
37 if (log)
Greg Claytona66ba462010-10-30 04:51:46 +000038 log->Printf ("SBCommandInterpreter::SBCommandInterpreter (interpreter=%p)"
39 " => SBCommandInterpreter(%p)", interpreter, m_opaque_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +000040}
41
Greg Clayton538eb822010-11-05 23:17:00 +000042SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs) :
43 m_opaque_ptr (rhs.m_opaque_ptr)
44{
45}
46
47const SBCommandInterpreter &
48SBCommandInterpreter::operator = (const SBCommandInterpreter &rhs)
49{
50 m_opaque_ptr = rhs.m_opaque_ptr;
51 return *this;
52}
53
Chris Lattner24943d22010-06-08 16:52:24 +000054SBCommandInterpreter::~SBCommandInterpreter ()
55{
56}
57
58bool
Greg Clayton63094e02010-06-23 01:19:29 +000059SBCommandInterpreter::IsValid() const
60{
61 return m_opaque_ptr != NULL;
62}
63
64
65bool
Chris Lattner24943d22010-06-08 16:52:24 +000066SBCommandInterpreter::CommandExists (const char *cmd)
67{
Johnny Chenbab8cc92011-12-19 21:16:29 +000068 if (cmd && m_opaque_ptr)
Greg Clayton63094e02010-06-23 01:19:29 +000069 return m_opaque_ptr->CommandExists (cmd);
70 return false;
Chris Lattner24943d22010-06-08 16:52:24 +000071}
72
73bool
74SBCommandInterpreter::AliasExists (const char *cmd)
75{
Johnny Chenbab8cc92011-12-19 21:16:29 +000076 if (cmd && m_opaque_ptr)
Greg Clayton63094e02010-06-23 01:19:29 +000077 return m_opaque_ptr->AliasExists (cmd);
78 return false;
Chris Lattner24943d22010-06-08 16:52:24 +000079}
80
Chris Lattner24943d22010-06-08 16:52:24 +000081lldb::ReturnStatus
82SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history)
83{
Greg Claytone005f2c2010-11-06 01:53:30 +000084 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +000085
86 if (log)
Greg Clayton49ce6822010-10-31 03:01:06 +000087 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p), add_to_history=%i)",
88 m_opaque_ptr, command_line, result.get(), add_to_history);
Caroline Tice7826c882010-10-26 03:11:13 +000089
Chris Lattner24943d22010-06-08 16:52:24 +000090 result.Clear();
Johnny Chenbab8cc92011-12-19 21:16:29 +000091 if (command_line && m_opaque_ptr)
Greg Clayton63094e02010-06-23 01:19:29 +000092 {
Greg Claytonbdcda462010-12-20 20:49:23 +000093 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
94 Mutex::Locker api_locker;
95 if (target_sp)
96 api_locker.Reset(target_sp->GetAPIMutex().GetMutex());
Greg Clayton63094e02010-06-23 01:19:29 +000097 m_opaque_ptr->HandleCommand (command_line, add_to_history, result.ref());
98 }
99 else
100 {
Johnny Chenbab8cc92011-12-19 21:16:29 +0000101 result->AppendError ("SBCommandInterpreter or the command line is not valid");
Greg Clayton63094e02010-06-23 01:19:29 +0000102 result->SetStatus (eReturnStatusFailed);
103 }
Caroline Tice7826c882010-10-26 03:11:13 +0000104
Caroline Tice7de24cc2010-10-27 21:23:37 +0000105 // We need to get the value again, in case the command disabled the log!
106 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Tice7826c882010-10-26 03:11:13 +0000107 if (log)
108 {
109 SBStream sstr;
110 result.GetDescription (sstr);
Greg Clayton49ce6822010-10-31 03:01:06 +0000111 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p): %s, add_to_history=%i) => %i",
112 m_opaque_ptr, command_line, result.get(), sstr.GetData(), add_to_history, result.GetStatus());
Caroline Tice7826c882010-10-26 03:11:13 +0000113 }
114
Chris Lattner24943d22010-06-08 16:52:24 +0000115 return result.GetStatus();
116}
117
118int
119SBCommandInterpreter::HandleCompletion (const char *current_line,
120 const char *cursor,
121 const char *last_char,
122 int match_start_point,
123 int max_return_elements,
124 SBStringList &matches)
125{
Greg Clayton63094e02010-06-23 01:19:29 +0000126 int num_completions = 0;
Jim Ingham3cbf8482011-12-05 19:24:15 +0000127
128 // Sanity check the arguments that are passed in:
129 // cursor & last_char have to be within the current_line.
130 if (current_line == NULL || cursor == NULL || last_char == NULL)
131 return 0;
132
133 if (cursor < current_line || last_char < current_line)
134 return 0;
135
136 size_t current_line_size = strlen (current_line);
137 if (cursor - current_line > current_line_size || last_char - current_line > current_line_size)
138 return 0;
139
Greg Clayton63094e02010-06-23 01:19:29 +0000140 if (m_opaque_ptr)
141 {
142 lldb_private::StringList lldb_matches;
143 num_completions = m_opaque_ptr->HandleCompletion (current_line, cursor, last_char, match_start_point,
144 max_return_elements, lldb_matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000145
Greg Clayton63094e02010-06-23 01:19:29 +0000146 SBStringList temp_list (&lldb_matches);
147 matches.AppendList (temp_list);
148 }
Chris Lattner24943d22010-06-08 16:52:24 +0000149 return num_completions;
150}
151
Jim Ingham03c8ee52011-09-21 01:17:13 +0000152int
153SBCommandInterpreter::HandleCompletion (const char *current_line,
154 uint32_t cursor_pos,
155 int match_start_point,
156 int max_return_elements,
157 lldb::SBStringList &matches)
158{
159 const char *cursor = current_line + cursor_pos;
160 const char *last_char = current_line + strlen (current_line);
161 return HandleCompletion (current_line, cursor, last_char, match_start_point, max_return_elements, matches);
162}
163
Chris Lattner24943d22010-06-08 16:52:24 +0000164bool
165SBCommandInterpreter::HasCommands ()
166{
Greg Clayton63094e02010-06-23 01:19:29 +0000167 if (m_opaque_ptr)
168 return m_opaque_ptr->HasCommands();
169 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000170}
171
172bool
173SBCommandInterpreter::HasAliases ()
174{
Greg Clayton63094e02010-06-23 01:19:29 +0000175 if (m_opaque_ptr)
176 return m_opaque_ptr->HasAliases();
177 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000178}
179
180bool
Chris Lattner24943d22010-06-08 16:52:24 +0000181SBCommandInterpreter::HasAliasOptions ()
182{
Greg Clayton63094e02010-06-23 01:19:29 +0000183 if (m_opaque_ptr)
184 return m_opaque_ptr->HasAliasOptions ();
185 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000186}
187
Chris Lattner24943d22010-06-08 16:52:24 +0000188SBProcess
189SBCommandInterpreter::GetProcess ()
190{
191 SBProcess process;
Greg Clayton63094e02010-06-23 01:19:29 +0000192 if (m_opaque_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000193 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000194 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
195 if (target_sp)
196 {
197 Mutex::Locker api_locker(target_sp->GetAPIMutex());
198 process.SetProcess(target_sp->GetProcessSP());
199 }
Chris Lattner24943d22010-06-08 16:52:24 +0000200 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000201 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000202
203 if (log)
204 log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)",
205 m_opaque_ptr, process.get());
206
207
Chris Lattner24943d22010-06-08 16:52:24 +0000208 return process;
209}
210
Chris Lattner24943d22010-06-08 16:52:24 +0000211CommandInterpreter *
Greg Clayton63094e02010-06-23 01:19:29 +0000212SBCommandInterpreter::get ()
Chris Lattner24943d22010-06-08 16:52:24 +0000213{
Greg Clayton63094e02010-06-23 01:19:29 +0000214 return m_opaque_ptr;
Chris Lattner24943d22010-06-08 16:52:24 +0000215}
216
217CommandInterpreter &
Greg Clayton63094e02010-06-23 01:19:29 +0000218SBCommandInterpreter::ref ()
Chris Lattner24943d22010-06-08 16:52:24 +0000219{
Greg Clayton63094e02010-06-23 01:19:29 +0000220 assert (m_opaque_ptr);
221 return *m_opaque_ptr;
222}
223
224void
225SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
226{
227 m_opaque_ptr = interpreter;
Chris Lattner24943d22010-06-08 16:52:24 +0000228}
229
230void
231SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
232{
233 result.Clear();
Greg Clayton63094e02010-06-23 01:19:29 +0000234 if (m_opaque_ptr)
235 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000236 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
237 Mutex::Locker api_locker;
238 if (target_sp)
239 api_locker.Reset(target_sp->GetAPIMutex().GetMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000240 m_opaque_ptr->SourceInitFile (false, result.ref());
241 }
242 else
243 {
244 result->AppendError ("SBCommandInterpreter is not valid");
245 result->SetStatus (eReturnStatusFailed);
246 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000247 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000248
249 if (log)
250 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))",
251 m_opaque_ptr, result.get());
252
Chris Lattner24943d22010-06-08 16:52:24 +0000253}
254
255void
256SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result)
257{
258 result.Clear();
Greg Clayton63094e02010-06-23 01:19:29 +0000259 if (m_opaque_ptr)
260 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000261 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
262 Mutex::Locker api_locker;
263 if (target_sp)
264 api_locker.Reset(target_sp->GetAPIMutex().GetMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000265 m_opaque_ptr->SourceInitFile (true, result.ref());
266 }
267 else
268 {
269 result->AppendError ("SBCommandInterpreter is not valid");
270 result->SetStatus (eReturnStatusFailed);
271 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000272 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000273
274 if (log)
275 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))",
276 m_opaque_ptr, result.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000277}
278
279SBBroadcaster
280SBCommandInterpreter::GetBroadcaster ()
281{
Greg Claytone005f2c2010-11-06 01:53:30 +0000282 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000283
Greg Clayton63094e02010-06-23 01:19:29 +0000284 SBBroadcaster broadcaster (m_opaque_ptr, false);
Caroline Tice7826c882010-10-26 03:11:13 +0000285
286 if (log)
Greg Claytona66ba462010-10-30 04:51:46 +0000287 log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000288 m_opaque_ptr, broadcaster.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000289
Chris Lattner24943d22010-06-08 16:52:24 +0000290 return broadcaster;
291}
292
Greg Claytonaa378b12011-02-20 02:15:07 +0000293const char *
294SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
295{
296 return CommandObject::GetArgumentTypeAsCString (arg_type);
297}
298
299const char *
300SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
301{
302 return CommandObject::GetArgumentDescriptionAsCString (arg_type);
303}
304
305
Greg Clayton3e4238d2011-11-04 03:34:56 +0000306#ifndef LLDB_DISABLE_PYTHON
Greg Claytone86cbb92011-03-22 01:14:58 +0000307extern "C" bool
308LLDBSwigPythonBreakpointCallbackFunction
309(
310 const char *python_function_name,
311 const char *session_dictionary_name,
312 const lldb::StackFrameSP& sb_frame,
313 const lldb::BreakpointLocationSP& sb_bp_loc
314);
Greg Claytonaa378b12011-02-20 02:15:07 +0000315
Enrico Granataf7a9b142011-07-15 02:26:42 +0000316extern "C" std::string
317LLDBSwigPythonCallTypeScript
318(
319 const char *python_function_name,
320 const char *session_dictionary_name,
321 const lldb::ValueObjectSP& valobj_sp
322);
323
Enrico Granata9ae7cef2011-07-24 00:14:56 +0000324extern "C" void*
325LLDBSwigPythonCreateSyntheticProvider
326(
327 const std::string python_class_name,
328 const char *session_dictionary_name,
329 const lldb::ValueObjectSP& valobj_sp
330);
331
332
Enrico Granata979e20d2011-07-29 19:53:35 +0000333extern "C" uint32_t LLDBSwigPython_CalculateNumChildren (void *implementor);
334extern "C" void* LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx);
335extern "C" int LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name);
Enrico Granata91544802011-09-06 19:20:51 +0000336extern "C" void* LLDBSWIGPython_CastPyObjectToSBValue (void* data);
Enrico Granata979e20d2011-07-29 19:53:35 +0000337extern "C" void LLDBSwigPython_UpdateSynthProviderInstance (void* implementor);
Enrico Granataf7a9b142011-07-15 02:26:42 +0000338
Enrico Granatac2a28252011-08-16 16:49:25 +0000339extern "C" bool LLDBSwigPythonCallCommand
340(
341 const char *python_function_name,
342 const char *session_dictionary_name,
343 lldb::DebuggerSP& debugger,
344 const char* args,
345 std::string& err_msg,
Enrico Granata3370f0c2011-08-19 23:56:34 +0000346 lldb_private::CommandReturnObject& cmd_retobj
Enrico Granatac2a28252011-08-16 16:49:25 +0000347);
348
Greg Clayton3e4238d2011-11-04 03:34:56 +0000349// Defined in the SWIG source file
350extern "C" void
351init_lldb(void);
352
Enrico Granata59df36f2011-10-17 21:45:27 +0000353extern "C" bool LLDBSwigPythonCallModuleInit
354(
355 const std::string python_module_name,
356 const char *session_dictionary_name,
357 lldb::DebuggerSP& debugger
358);
359
Greg Clayton3e4238d2011-11-04 03:34:56 +0000360#else
Enrico Granatac2a28252011-08-16 16:49:25 +0000361
Greg Claytone86cbb92011-03-22 01:14:58 +0000362extern "C" void init_lldb(void);
363
Greg Clayton3e4238d2011-11-04 03:34:56 +0000364// Usually defined in the SWIG source file, but we have sripting disabled
365extern "C" void
366init_lldb(void)
367{
368}
369
370#endif
371
Greg Claytone86cbb92011-03-22 01:14:58 +0000372void
373SBCommandInterpreter::InitializeSWIG ()
374{
375 static bool g_initialized = false;
376 if (!g_initialized)
377 {
378 g_initialized = true;
Greg Clayton3e4238d2011-11-04 03:34:56 +0000379#ifndef LLDB_DISABLE_PYTHON
Greg Claytone86cbb92011-03-22 01:14:58 +0000380 ScriptInterpreter::InitializeInterpreter (init_lldb,
Enrico Granataf7a9b142011-07-15 02:26:42 +0000381 LLDBSwigPythonBreakpointCallbackFunction,
Enrico Granata9ae7cef2011-07-24 00:14:56 +0000382 LLDBSwigPythonCallTypeScript,
383 LLDBSwigPythonCreateSyntheticProvider,
384 LLDBSwigPython_CalculateNumChildren,
385 LLDBSwigPython_GetChildAtIndex,
386 LLDBSwigPython_GetIndexOfChildWithName,
Enrico Granata979e20d2011-07-29 19:53:35 +0000387 LLDBSWIGPython_CastPyObjectToSBValue,
Enrico Granatac2a28252011-08-16 16:49:25 +0000388 LLDBSwigPython_UpdateSynthProviderInstance,
Enrico Granata59df36f2011-10-17 21:45:27 +0000389 LLDBSwigPythonCallCommand,
390 LLDBSwigPythonCallModuleInit);
Greg Clayton3e4238d2011-11-04 03:34:56 +0000391#endif
Greg Claytone86cbb92011-03-22 01:14:58 +0000392 }
393}