blob: 8a15423c3245e2eb7a46abc0e3985c17fa860bb7 [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
Jim Ingham03c8ee52011-09-21 01:17:13 +0000139int
140SBCommandInterpreter::HandleCompletion (const char *current_line,
141 uint32_t cursor_pos,
142 int match_start_point,
143 int max_return_elements,
144 lldb::SBStringList &matches)
145{
146 const char *cursor = current_line + cursor_pos;
147 const char *last_char = current_line + strlen (current_line);
148 return HandleCompletion (current_line, cursor, last_char, match_start_point, max_return_elements, matches);
149}
150
Chris Lattner24943d22010-06-08 16:52:24 +0000151bool
152SBCommandInterpreter::HasCommands ()
153{
Greg Clayton63094e02010-06-23 01:19:29 +0000154 if (m_opaque_ptr)
155 return m_opaque_ptr->HasCommands();
156 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000157}
158
159bool
160SBCommandInterpreter::HasAliases ()
161{
Greg Clayton63094e02010-06-23 01:19:29 +0000162 if (m_opaque_ptr)
163 return m_opaque_ptr->HasAliases();
164 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000165}
166
167bool
Chris Lattner24943d22010-06-08 16:52:24 +0000168SBCommandInterpreter::HasAliasOptions ()
169{
Greg Clayton63094e02010-06-23 01:19:29 +0000170 if (m_opaque_ptr)
171 return m_opaque_ptr->HasAliasOptions ();
172 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000173}
174
Chris Lattner24943d22010-06-08 16:52:24 +0000175SBProcess
176SBCommandInterpreter::GetProcess ()
177{
178 SBProcess process;
Greg Clayton63094e02010-06-23 01:19:29 +0000179 if (m_opaque_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000180 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000181 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
182 if (target_sp)
183 {
184 Mutex::Locker api_locker(target_sp->GetAPIMutex());
185 process.SetProcess(target_sp->GetProcessSP());
186 }
Chris Lattner24943d22010-06-08 16:52:24 +0000187 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000188 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000189
190 if (log)
191 log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)",
192 m_opaque_ptr, process.get());
193
194
Chris Lattner24943d22010-06-08 16:52:24 +0000195 return process;
196}
197
198ssize_t
199SBCommandInterpreter::WriteToScriptInterpreter (const char *src)
200{
Greg Claytona66ba462010-10-30 04:51:46 +0000201 return WriteToScriptInterpreter (src, strlen(src));
Chris Lattner24943d22010-06-08 16:52:24 +0000202}
203
204ssize_t
205SBCommandInterpreter::WriteToScriptInterpreter (const char *src, size_t src_len)
206{
Greg Claytone005f2c2010-11-06 01:53:30 +0000207 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000208
209 ssize_t bytes_written = 0;
Greg Clayton63094e02010-06-23 01:19:29 +0000210 if (m_opaque_ptr && src && src[0])
Chris Lattner24943d22010-06-08 16:52:24 +0000211 {
Greg Clayton63094e02010-06-23 01:19:29 +0000212 ScriptInterpreter *script_interpreter = m_opaque_ptr->GetScriptInterpreter();
Chris Lattner24943d22010-06-08 16:52:24 +0000213 if (script_interpreter)
Greg Claytona66ba462010-10-30 04:51:46 +0000214 bytes_written = ::write (script_interpreter->GetMasterFileDescriptor(), src, src_len);
Chris Lattner24943d22010-06-08 16:52:24 +0000215 }
Greg Claytona66ba462010-10-30 04:51:46 +0000216 if (log)
217 log->Printf ("SBCommandInterpreter(%p)::WriteToScriptInterpreter (src=\"%s\", src_len=%zu) => %zi",
218 m_opaque_ptr, src, src_len, bytes_written);
219
220 return bytes_written;
Chris Lattner24943d22010-06-08 16:52:24 +0000221}
222
223
224CommandInterpreter *
Greg Clayton63094e02010-06-23 01:19:29 +0000225SBCommandInterpreter::get ()
Chris Lattner24943d22010-06-08 16:52:24 +0000226{
Greg Clayton63094e02010-06-23 01:19:29 +0000227 return m_opaque_ptr;
Chris Lattner24943d22010-06-08 16:52:24 +0000228}
229
230CommandInterpreter &
Greg Clayton63094e02010-06-23 01:19:29 +0000231SBCommandInterpreter::ref ()
Chris Lattner24943d22010-06-08 16:52:24 +0000232{
Greg Clayton63094e02010-06-23 01:19:29 +0000233 assert (m_opaque_ptr);
234 return *m_opaque_ptr;
235}
236
237void
238SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
239{
240 m_opaque_ptr = interpreter;
Chris Lattner24943d22010-06-08 16:52:24 +0000241}
242
243void
244SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
245{
246 result.Clear();
Greg Clayton63094e02010-06-23 01:19:29 +0000247 if (m_opaque_ptr)
248 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000249 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
250 Mutex::Locker api_locker;
251 if (target_sp)
252 api_locker.Reset(target_sp->GetAPIMutex().GetMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000253 m_opaque_ptr->SourceInitFile (false, result.ref());
254 }
255 else
256 {
257 result->AppendError ("SBCommandInterpreter is not valid");
258 result->SetStatus (eReturnStatusFailed);
259 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000260 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000261
262 if (log)
263 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))",
264 m_opaque_ptr, result.get());
265
Chris Lattner24943d22010-06-08 16:52:24 +0000266}
267
268void
269SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result)
270{
271 result.Clear();
Greg Clayton63094e02010-06-23 01:19:29 +0000272 if (m_opaque_ptr)
273 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000274 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
275 Mutex::Locker api_locker;
276 if (target_sp)
277 api_locker.Reset(target_sp->GetAPIMutex().GetMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000278 m_opaque_ptr->SourceInitFile (true, result.ref());
279 }
280 else
281 {
282 result->AppendError ("SBCommandInterpreter is not valid");
283 result->SetStatus (eReturnStatusFailed);
284 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000285 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000286
287 if (log)
288 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))",
289 m_opaque_ptr, result.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000290}
291
292SBBroadcaster
293SBCommandInterpreter::GetBroadcaster ()
294{
Greg Claytone005f2c2010-11-06 01:53:30 +0000295 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000296
Greg Clayton63094e02010-06-23 01:19:29 +0000297 SBBroadcaster broadcaster (m_opaque_ptr, false);
Caroline Tice7826c882010-10-26 03:11:13 +0000298
299 if (log)
Greg Claytona66ba462010-10-30 04:51:46 +0000300 log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000301 m_opaque_ptr, broadcaster.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000302
Chris Lattner24943d22010-06-08 16:52:24 +0000303 return broadcaster;
304}
305
Greg Claytonaa378b12011-02-20 02:15:07 +0000306const char *
307SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
308{
309 return CommandObject::GetArgumentTypeAsCString (arg_type);
310}
311
312const char *
313SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
314{
315 return CommandObject::GetArgumentDescriptionAsCString (arg_type);
316}
317
318
Greg Claytone86cbb92011-03-22 01:14:58 +0000319extern "C" bool
320LLDBSwigPythonBreakpointCallbackFunction
321(
322 const char *python_function_name,
323 const char *session_dictionary_name,
324 const lldb::StackFrameSP& sb_frame,
325 const lldb::BreakpointLocationSP& sb_bp_loc
326);
Greg Claytonaa378b12011-02-20 02:15:07 +0000327
Enrico Granataf7a9b142011-07-15 02:26:42 +0000328extern "C" std::string
329LLDBSwigPythonCallTypeScript
330(
331 const char *python_function_name,
332 const char *session_dictionary_name,
333 const lldb::ValueObjectSP& valobj_sp
334);
335
Enrico Granata9ae7cef2011-07-24 00:14:56 +0000336extern "C" void*
337LLDBSwigPythonCreateSyntheticProvider
338(
339 const std::string python_class_name,
340 const char *session_dictionary_name,
341 const lldb::ValueObjectSP& valobj_sp
342);
343
344
Enrico Granata979e20d2011-07-29 19:53:35 +0000345extern "C" uint32_t LLDBSwigPython_CalculateNumChildren (void *implementor);
346extern "C" void* LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx);
347extern "C" int LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name);
Enrico Granata91544802011-09-06 19:20:51 +0000348extern "C" void* LLDBSWIGPython_CastPyObjectToSBValue (void* data);
Enrico Granata979e20d2011-07-29 19:53:35 +0000349extern "C" void LLDBSwigPython_UpdateSynthProviderInstance (void* implementor);
Enrico Granataf7a9b142011-07-15 02:26:42 +0000350
Enrico Granatac2a28252011-08-16 16:49:25 +0000351extern "C" bool LLDBSwigPythonCallCommand
352(
353 const char *python_function_name,
354 const char *session_dictionary_name,
355 lldb::DebuggerSP& debugger,
356 const char* args,
357 std::string& err_msg,
Enrico Granata3370f0c2011-08-19 23:56:34 +0000358 lldb_private::CommandReturnObject& cmd_retobj
Enrico Granatac2a28252011-08-16 16:49:25 +0000359);
360
361
Greg Claytone86cbb92011-03-22 01:14:58 +0000362extern "C" void init_lldb(void);
363
364void
365SBCommandInterpreter::InitializeSWIG ()
366{
367 static bool g_initialized = false;
368 if (!g_initialized)
369 {
370 g_initialized = true;
371 ScriptInterpreter::InitializeInterpreter (init_lldb,
Enrico Granataf7a9b142011-07-15 02:26:42 +0000372 LLDBSwigPythonBreakpointCallbackFunction,
Enrico Granata9ae7cef2011-07-24 00:14:56 +0000373 LLDBSwigPythonCallTypeScript,
374 LLDBSwigPythonCreateSyntheticProvider,
375 LLDBSwigPython_CalculateNumChildren,
376 LLDBSwigPython_GetChildAtIndex,
377 LLDBSwigPython_GetIndexOfChildWithName,
Enrico Granata979e20d2011-07-29 19:53:35 +0000378 LLDBSWIGPython_CastPyObjectToSBValue,
Enrico Granatac2a28252011-08-16 16:49:25 +0000379 LLDBSwigPython_UpdateSynthProviderInstance,
380 LLDBSwigPythonCallCommand);
Greg Claytone86cbb92011-03-22 01:14:58 +0000381 }
382}