blob: 03f33c69db0e50e224a7990eed5caefcb48a8185 [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
211ssize_t
212SBCommandInterpreter::WriteToScriptInterpreter (const char *src)
213{
Greg Claytona66ba462010-10-30 04:51:46 +0000214 return WriteToScriptInterpreter (src, strlen(src));
Chris Lattner24943d22010-06-08 16:52:24 +0000215}
216
217ssize_t
218SBCommandInterpreter::WriteToScriptInterpreter (const char *src, size_t src_len)
219{
Greg Claytone005f2c2010-11-06 01:53:30 +0000220 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000221
222 ssize_t bytes_written = 0;
Greg Clayton63094e02010-06-23 01:19:29 +0000223 if (m_opaque_ptr && src && src[0])
Chris Lattner24943d22010-06-08 16:52:24 +0000224 {
Greg Clayton63094e02010-06-23 01:19:29 +0000225 ScriptInterpreter *script_interpreter = m_opaque_ptr->GetScriptInterpreter();
Chris Lattner24943d22010-06-08 16:52:24 +0000226 if (script_interpreter)
Greg Claytona66ba462010-10-30 04:51:46 +0000227 bytes_written = ::write (script_interpreter->GetMasterFileDescriptor(), src, src_len);
Chris Lattner24943d22010-06-08 16:52:24 +0000228 }
Greg Claytona66ba462010-10-30 04:51:46 +0000229 if (log)
230 log->Printf ("SBCommandInterpreter(%p)::WriteToScriptInterpreter (src=\"%s\", src_len=%zu) => %zi",
231 m_opaque_ptr, src, src_len, bytes_written);
232
233 return bytes_written;
Chris Lattner24943d22010-06-08 16:52:24 +0000234}
235
236
237CommandInterpreter *
Greg Clayton63094e02010-06-23 01:19:29 +0000238SBCommandInterpreter::get ()
Chris Lattner24943d22010-06-08 16:52:24 +0000239{
Greg Clayton63094e02010-06-23 01:19:29 +0000240 return m_opaque_ptr;
Chris Lattner24943d22010-06-08 16:52:24 +0000241}
242
243CommandInterpreter &
Greg Clayton63094e02010-06-23 01:19:29 +0000244SBCommandInterpreter::ref ()
Chris Lattner24943d22010-06-08 16:52:24 +0000245{
Greg Clayton63094e02010-06-23 01:19:29 +0000246 assert (m_opaque_ptr);
247 return *m_opaque_ptr;
248}
249
250void
251SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
252{
253 m_opaque_ptr = interpreter;
Chris Lattner24943d22010-06-08 16:52:24 +0000254}
255
256void
257SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
258{
259 result.Clear();
Greg Clayton63094e02010-06-23 01:19:29 +0000260 if (m_opaque_ptr)
261 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000262 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
263 Mutex::Locker api_locker;
264 if (target_sp)
265 api_locker.Reset(target_sp->GetAPIMutex().GetMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000266 m_opaque_ptr->SourceInitFile (false, result.ref());
267 }
268 else
269 {
270 result->AppendError ("SBCommandInterpreter is not valid");
271 result->SetStatus (eReturnStatusFailed);
272 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000273 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000274
275 if (log)
276 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))",
277 m_opaque_ptr, result.get());
278
Chris Lattner24943d22010-06-08 16:52:24 +0000279}
280
281void
282SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result)
283{
284 result.Clear();
Greg Clayton63094e02010-06-23 01:19:29 +0000285 if (m_opaque_ptr)
286 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000287 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
288 Mutex::Locker api_locker;
289 if (target_sp)
290 api_locker.Reset(target_sp->GetAPIMutex().GetMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000291 m_opaque_ptr->SourceInitFile (true, result.ref());
292 }
293 else
294 {
295 result->AppendError ("SBCommandInterpreter is not valid");
296 result->SetStatus (eReturnStatusFailed);
297 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000298 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000299
300 if (log)
301 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))",
302 m_opaque_ptr, result.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000303}
304
305SBBroadcaster
306SBCommandInterpreter::GetBroadcaster ()
307{
Greg Claytone005f2c2010-11-06 01:53:30 +0000308 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000309
Greg Clayton63094e02010-06-23 01:19:29 +0000310 SBBroadcaster broadcaster (m_opaque_ptr, false);
Caroline Tice7826c882010-10-26 03:11:13 +0000311
312 if (log)
Greg Claytona66ba462010-10-30 04:51:46 +0000313 log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000314 m_opaque_ptr, broadcaster.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000315
Chris Lattner24943d22010-06-08 16:52:24 +0000316 return broadcaster;
317}
318
Greg Claytonaa378b12011-02-20 02:15:07 +0000319const char *
320SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
321{
322 return CommandObject::GetArgumentTypeAsCString (arg_type);
323}
324
325const char *
326SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
327{
328 return CommandObject::GetArgumentDescriptionAsCString (arg_type);
329}
330
331
Greg Clayton3e4238d2011-11-04 03:34:56 +0000332#ifndef LLDB_DISABLE_PYTHON
Greg Claytone86cbb92011-03-22 01:14:58 +0000333extern "C" bool
334LLDBSwigPythonBreakpointCallbackFunction
335(
336 const char *python_function_name,
337 const char *session_dictionary_name,
338 const lldb::StackFrameSP& sb_frame,
339 const lldb::BreakpointLocationSP& sb_bp_loc
340);
Greg Claytonaa378b12011-02-20 02:15:07 +0000341
Enrico Granataf7a9b142011-07-15 02:26:42 +0000342extern "C" std::string
343LLDBSwigPythonCallTypeScript
344(
345 const char *python_function_name,
346 const char *session_dictionary_name,
347 const lldb::ValueObjectSP& valobj_sp
348);
349
Enrico Granata9ae7cef2011-07-24 00:14:56 +0000350extern "C" void*
351LLDBSwigPythonCreateSyntheticProvider
352(
353 const std::string python_class_name,
354 const char *session_dictionary_name,
355 const lldb::ValueObjectSP& valobj_sp
356);
357
358
Enrico Granata979e20d2011-07-29 19:53:35 +0000359extern "C" uint32_t LLDBSwigPython_CalculateNumChildren (void *implementor);
360extern "C" void* LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx);
361extern "C" int LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name);
Enrico Granata91544802011-09-06 19:20:51 +0000362extern "C" void* LLDBSWIGPython_CastPyObjectToSBValue (void* data);
Enrico Granata979e20d2011-07-29 19:53:35 +0000363extern "C" void LLDBSwigPython_UpdateSynthProviderInstance (void* implementor);
Enrico Granataf7a9b142011-07-15 02:26:42 +0000364
Enrico Granatac2a28252011-08-16 16:49:25 +0000365extern "C" bool LLDBSwigPythonCallCommand
366(
367 const char *python_function_name,
368 const char *session_dictionary_name,
369 lldb::DebuggerSP& debugger,
370 const char* args,
371 std::string& err_msg,
Enrico Granata3370f0c2011-08-19 23:56:34 +0000372 lldb_private::CommandReturnObject& cmd_retobj
Enrico Granatac2a28252011-08-16 16:49:25 +0000373);
374
Greg Clayton3e4238d2011-11-04 03:34:56 +0000375// Defined in the SWIG source file
376extern "C" void
377init_lldb(void);
378
Enrico Granata59df36f2011-10-17 21:45:27 +0000379extern "C" bool LLDBSwigPythonCallModuleInit
380(
381 const std::string python_module_name,
382 const char *session_dictionary_name,
383 lldb::DebuggerSP& debugger
384);
385
Greg Clayton3e4238d2011-11-04 03:34:56 +0000386#else
Enrico Granatac2a28252011-08-16 16:49:25 +0000387
Greg Claytone86cbb92011-03-22 01:14:58 +0000388extern "C" void init_lldb(void);
389
Greg Clayton3e4238d2011-11-04 03:34:56 +0000390// Usually defined in the SWIG source file, but we have sripting disabled
391extern "C" void
392init_lldb(void)
393{
394}
395
396#endif
397
Greg Claytone86cbb92011-03-22 01:14:58 +0000398void
399SBCommandInterpreter::InitializeSWIG ()
400{
401 static bool g_initialized = false;
402 if (!g_initialized)
403 {
404 g_initialized = true;
Greg Clayton3e4238d2011-11-04 03:34:56 +0000405#ifndef LLDB_DISABLE_PYTHON
Greg Claytone86cbb92011-03-22 01:14:58 +0000406 ScriptInterpreter::InitializeInterpreter (init_lldb,
Enrico Granataf7a9b142011-07-15 02:26:42 +0000407 LLDBSwigPythonBreakpointCallbackFunction,
Enrico Granata9ae7cef2011-07-24 00:14:56 +0000408 LLDBSwigPythonCallTypeScript,
409 LLDBSwigPythonCreateSyntheticProvider,
410 LLDBSwigPython_CalculateNumChildren,
411 LLDBSwigPython_GetChildAtIndex,
412 LLDBSwigPython_GetIndexOfChildWithName,
Enrico Granata979e20d2011-07-29 19:53:35 +0000413 LLDBSWIGPython_CastPyObjectToSBValue,
Enrico Granatac2a28252011-08-16 16:49:25 +0000414 LLDBSwigPython_UpdateSynthProviderInstance,
Enrico Granata59df36f2011-10-17 21:45:27 +0000415 LLDBSwigPythonCallCommand,
416 LLDBSwigPythonCallModuleInit);
Greg Clayton3e4238d2011-11-04 03:34:56 +0000417#endif
Greg Claytone86cbb92011-03-22 01:14:58 +0000418 }
419}