blob: 61d0a99f975b53268cf97391d417bad27aeedc58 [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{
Greg Clayton63094e02010-06-23 01:19:29 +000068 if (m_opaque_ptr)
69 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{
Greg Clayton63094e02010-06-23 01:19:29 +000076 if (m_opaque_ptr)
77 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();
Greg Clayton63094e02010-06-23 01:19:29 +000091 if (m_opaque_ptr)
92 {
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 {
101 result->AppendError ("SBCommandInterpreter is not valid");
102 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;
127 if (m_opaque_ptr)
128 {
129 lldb_private::StringList lldb_matches;
130 num_completions = m_opaque_ptr->HandleCompletion (current_line, cursor, last_char, match_start_point,
131 max_return_elements, lldb_matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000132
Greg Clayton63094e02010-06-23 01:19:29 +0000133 SBStringList temp_list (&lldb_matches);
134 matches.AppendList (temp_list);
135 }
Chris Lattner24943d22010-06-08 16:52:24 +0000136 return num_completions;
137}
138
Chris Lattner24943d22010-06-08 16:52:24 +0000139bool
140SBCommandInterpreter::HasCommands ()
141{
Greg Clayton63094e02010-06-23 01:19:29 +0000142 if (m_opaque_ptr)
143 return m_opaque_ptr->HasCommands();
144 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000145}
146
147bool
148SBCommandInterpreter::HasAliases ()
149{
Greg Clayton63094e02010-06-23 01:19:29 +0000150 if (m_opaque_ptr)
151 return m_opaque_ptr->HasAliases();
152 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000153}
154
155bool
Chris Lattner24943d22010-06-08 16:52:24 +0000156SBCommandInterpreter::HasAliasOptions ()
157{
Greg Clayton63094e02010-06-23 01:19:29 +0000158 if (m_opaque_ptr)
159 return m_opaque_ptr->HasAliasOptions ();
160 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000161}
162
Chris Lattner24943d22010-06-08 16:52:24 +0000163SBProcess
164SBCommandInterpreter::GetProcess ()
165{
166 SBProcess process;
Greg Clayton63094e02010-06-23 01:19:29 +0000167 if (m_opaque_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000168 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000169 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
170 if (target_sp)
171 {
172 Mutex::Locker api_locker(target_sp->GetAPIMutex());
173 process.SetProcess(target_sp->GetProcessSP());
174 }
Chris Lattner24943d22010-06-08 16:52:24 +0000175 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000176 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000177
178 if (log)
179 log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)",
180 m_opaque_ptr, process.get());
181
182
Chris Lattner24943d22010-06-08 16:52:24 +0000183 return process;
184}
185
186ssize_t
187SBCommandInterpreter::WriteToScriptInterpreter (const char *src)
188{
Greg Claytona66ba462010-10-30 04:51:46 +0000189 return WriteToScriptInterpreter (src, strlen(src));
Chris Lattner24943d22010-06-08 16:52:24 +0000190}
191
192ssize_t
193SBCommandInterpreter::WriteToScriptInterpreter (const char *src, size_t src_len)
194{
Greg Claytone005f2c2010-11-06 01:53:30 +0000195 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000196
197 ssize_t bytes_written = 0;
Greg Clayton63094e02010-06-23 01:19:29 +0000198 if (m_opaque_ptr && src && src[0])
Chris Lattner24943d22010-06-08 16:52:24 +0000199 {
Greg Clayton63094e02010-06-23 01:19:29 +0000200 ScriptInterpreter *script_interpreter = m_opaque_ptr->GetScriptInterpreter();
Chris Lattner24943d22010-06-08 16:52:24 +0000201 if (script_interpreter)
Greg Claytona66ba462010-10-30 04:51:46 +0000202 bytes_written = ::write (script_interpreter->GetMasterFileDescriptor(), src, src_len);
Chris Lattner24943d22010-06-08 16:52:24 +0000203 }
Greg Claytona66ba462010-10-30 04:51:46 +0000204 if (log)
205 log->Printf ("SBCommandInterpreter(%p)::WriteToScriptInterpreter (src=\"%s\", src_len=%zu) => %zi",
206 m_opaque_ptr, src, src_len, bytes_written);
207
208 return bytes_written;
Chris Lattner24943d22010-06-08 16:52:24 +0000209}
210
211
212CommandInterpreter *
Greg Clayton63094e02010-06-23 01:19:29 +0000213SBCommandInterpreter::get ()
Chris Lattner24943d22010-06-08 16:52:24 +0000214{
Greg Clayton63094e02010-06-23 01:19:29 +0000215 return m_opaque_ptr;
Chris Lattner24943d22010-06-08 16:52:24 +0000216}
217
218CommandInterpreter &
Greg Clayton63094e02010-06-23 01:19:29 +0000219SBCommandInterpreter::ref ()
Chris Lattner24943d22010-06-08 16:52:24 +0000220{
Greg Clayton63094e02010-06-23 01:19:29 +0000221 assert (m_opaque_ptr);
222 return *m_opaque_ptr;
223}
224
225void
226SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
227{
228 m_opaque_ptr = interpreter;
Chris Lattner24943d22010-06-08 16:52:24 +0000229}
230
231void
232SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
233{
234 result.Clear();
Greg Clayton63094e02010-06-23 01:19:29 +0000235 if (m_opaque_ptr)
236 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000237 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
238 Mutex::Locker api_locker;
239 if (target_sp)
240 api_locker.Reset(target_sp->GetAPIMutex().GetMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000241 m_opaque_ptr->SourceInitFile (false, result.ref());
242 }
243 else
244 {
245 result->AppendError ("SBCommandInterpreter is not valid");
246 result->SetStatus (eReturnStatusFailed);
247 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000248 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000249
250 if (log)
251 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))",
252 m_opaque_ptr, result.get());
253
Chris Lattner24943d22010-06-08 16:52:24 +0000254}
255
256void
257SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (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 (true, 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)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))",
277 m_opaque_ptr, result.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000278}
279
280SBBroadcaster
281SBCommandInterpreter::GetBroadcaster ()
282{
Greg Claytone005f2c2010-11-06 01:53:30 +0000283 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000284
Greg Clayton63094e02010-06-23 01:19:29 +0000285 SBBroadcaster broadcaster (m_opaque_ptr, false);
Caroline Tice7826c882010-10-26 03:11:13 +0000286
287 if (log)
Greg Claytona66ba462010-10-30 04:51:46 +0000288 log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000289 m_opaque_ptr, broadcaster.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000290
Chris Lattner24943d22010-06-08 16:52:24 +0000291 return broadcaster;
292}
293
Greg Claytonaa378b12011-02-20 02:15:07 +0000294const char *
295SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
296{
297 return CommandObject::GetArgumentTypeAsCString (arg_type);
298}
299
300const char *
301SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
302{
303 return CommandObject::GetArgumentDescriptionAsCString (arg_type);
304}
305
306
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
349
Greg Claytone86cbb92011-03-22 01:14:58 +0000350extern "C" void init_lldb(void);
351
352void
353SBCommandInterpreter::InitializeSWIG ()
354{
355 static bool g_initialized = false;
356 if (!g_initialized)
357 {
358 g_initialized = true;
359 ScriptInterpreter::InitializeInterpreter (init_lldb,
Enrico Granataf7a9b142011-07-15 02:26:42 +0000360 LLDBSwigPythonBreakpointCallbackFunction,
Enrico Granata9ae7cef2011-07-24 00:14:56 +0000361 LLDBSwigPythonCallTypeScript,
362 LLDBSwigPythonCreateSyntheticProvider,
363 LLDBSwigPython_CalculateNumChildren,
364 LLDBSwigPython_GetChildAtIndex,
365 LLDBSwigPython_GetIndexOfChildWithName,
Enrico Granata979e20d2011-07-29 19:53:35 +0000366 LLDBSWIGPython_CastPyObjectToSBValue,
Enrico Granatac2a28252011-08-16 16:49:25 +0000367 LLDBSwigPython_UpdateSynthProviderInstance,
368 LLDBSwigPythonCallCommand);
Greg Claytone86cbb92011-03-22 01:14:58 +0000369 }
370}