1 //===-- 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"
11 #include "lldb/Interpreter/Args.h"
12 #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 
18 #include "lldb/API/SBBroadcaster.h"
19 #include "lldb/API/SBDebugger.h"
20 #include "lldb/API/SBCommandReturnObject.h"
21 #include "lldb/API/SBCommandInterpreter.h"
22 #include "lldb/API/SBProcess.h"
23 #include "lldb/API/SBTarget.h"
24 #include "lldb/API/SBListener.h"
25 #include "lldb/API/SBStream.h"
26 #include "lldb/API/SBStringList.h"
27 
28 using namespace lldb;
29 using namespace lldb_private;
30 
31 
32 SBCommandInterpreter::SBCommandInterpreter (CommandInterpreter *interpreter) :
33     m_opaque_ptr (interpreter)
34 {
35     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
36 
37     if (log)
38         log->Printf ("SBCommandInterpreter::SBCommandInterpreter (interpreter=%p)"
39                      " => SBCommandInterpreter(%p)", interpreter, m_opaque_ptr);
40 }
41 
42 SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs) :
43     m_opaque_ptr (rhs.m_opaque_ptr)
44 {
45 }
46 
47 const SBCommandInterpreter &
48 SBCommandInterpreter::operator = (const SBCommandInterpreter &rhs)
49 {
50     m_opaque_ptr = rhs.m_opaque_ptr;
51     return *this;
52 }
53 
54 SBCommandInterpreter::~SBCommandInterpreter ()
55 {
56 }
57 
58 bool
59 SBCommandInterpreter::IsValid() const
60 {
61     return m_opaque_ptr != NULL;
62 }
63 
64 
65 bool
66 SBCommandInterpreter::CommandExists (const char *cmd)
67 {
68     if (cmd && m_opaque_ptr)
69         return m_opaque_ptr->CommandExists (cmd);
70     return false;
71 }
72 
73 bool
74 SBCommandInterpreter::AliasExists (const char *cmd)
75 {
76     if (cmd && m_opaque_ptr)
77         return m_opaque_ptr->AliasExists (cmd);
78     return false;
79 }
80 
81 lldb::ReturnStatus
82 SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history)
83 {
84     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
85 
86     if (log)
87         log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p), add_to_history=%i)",
88                      m_opaque_ptr, command_line, result.get(), add_to_history);
89 
90     result.Clear();
91     if (command_line && m_opaque_ptr)
92     {
93         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());
97         m_opaque_ptr->HandleCommand (command_line, add_to_history, result.ref());
98     }
99     else
100     {
101         result->AppendError ("SBCommandInterpreter or the command line is not valid");
102         result->SetStatus (eReturnStatusFailed);
103     }
104 
105     // We need to get the value again, in case the command disabled the log!
106     log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
107     if (log)
108     {
109         SBStream sstr;
110         result.GetDescription (sstr);
111         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());
113     }
114 
115     return result.GetStatus();
116 }
117 
118 int
119 SBCommandInterpreter::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 {
126     int num_completions = 0;
127 
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 
140     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);
145 
146         SBStringList temp_list (&lldb_matches);
147         matches.AppendList (temp_list);
148     }
149     return num_completions;
150 }
151 
152 int
153 SBCommandInterpreter::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 
164 bool
165 SBCommandInterpreter::HasCommands ()
166 {
167     if (m_opaque_ptr)
168         return m_opaque_ptr->HasCommands();
169     return false;
170 }
171 
172 bool
173 SBCommandInterpreter::HasAliases ()
174 {
175     if (m_opaque_ptr)
176         return m_opaque_ptr->HasAliases();
177     return false;
178 }
179 
180 bool
181 SBCommandInterpreter::HasAliasOptions ()
182 {
183     if (m_opaque_ptr)
184         return m_opaque_ptr->HasAliasOptions ();
185     return false;
186 }
187 
188 SBProcess
189 SBCommandInterpreter::GetProcess ()
190 {
191     SBProcess process;
192     if (m_opaque_ptr)
193     {
194         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         }
200     }
201     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
202 
203     if (log)
204         log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)",
205                      m_opaque_ptr, process.get());
206 
207 
208     return process;
209 }
210 
211 ssize_t
212 SBCommandInterpreter::WriteToScriptInterpreter (const char *src)
213 {
214     return WriteToScriptInterpreter (src, strlen(src));
215 }
216 
217 ssize_t
218 SBCommandInterpreter::WriteToScriptInterpreter (const char *src, size_t src_len)
219 {
220     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
221 
222     ssize_t bytes_written = 0;
223     if (m_opaque_ptr && src && src[0])
224     {
225         ScriptInterpreter *script_interpreter = m_opaque_ptr->GetScriptInterpreter();
226         if (script_interpreter)
227             bytes_written = ::write (script_interpreter->GetMasterFileDescriptor(), src, src_len);
228     }
229     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;
234 }
235 
236 
237 CommandInterpreter *
238 SBCommandInterpreter::get ()
239 {
240     return m_opaque_ptr;
241 }
242 
243 CommandInterpreter &
244 SBCommandInterpreter::ref ()
245 {
246     assert (m_opaque_ptr);
247     return *m_opaque_ptr;
248 }
249 
250 void
251 SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
252 {
253     m_opaque_ptr = interpreter;
254 }
255 
256 void
257 SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
258 {
259     result.Clear();
260     if (m_opaque_ptr)
261     {
262         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());
266         m_opaque_ptr->SourceInitFile (false, result.ref());
267     }
268     else
269     {
270         result->AppendError ("SBCommandInterpreter is not valid");
271         result->SetStatus (eReturnStatusFailed);
272     }
273     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
274 
275     if (log)
276         log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))",
277                      m_opaque_ptr, result.get());
278 
279 }
280 
281 void
282 SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result)
283 {
284     result.Clear();
285     if (m_opaque_ptr)
286     {
287         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());
291         m_opaque_ptr->SourceInitFile (true, result.ref());
292     }
293     else
294     {
295         result->AppendError ("SBCommandInterpreter is not valid");
296         result->SetStatus (eReturnStatusFailed);
297     }
298     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
299 
300     if (log)
301         log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))",
302                      m_opaque_ptr, result.get());
303 }
304 
305 SBBroadcaster
306 SBCommandInterpreter::GetBroadcaster ()
307 {
308     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
309 
310     SBBroadcaster broadcaster (m_opaque_ptr, false);
311 
312     if (log)
313         log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
314                      m_opaque_ptr, broadcaster.get());
315 
316     return broadcaster;
317 }
318 
319 const char *
320 SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
321 {
322     return CommandObject::GetArgumentTypeAsCString (arg_type);
323 }
324 
325 const char *
326 SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
327 {
328     return CommandObject::GetArgumentDescriptionAsCString (arg_type);
329 }
330 
331 
332 #ifndef LLDB_DISABLE_PYTHON
333 extern "C" bool
334 LLDBSwigPythonBreakpointCallbackFunction
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 );
341 
342 extern "C" std::string
343 LLDBSwigPythonCallTypeScript
344 (
345     const char *python_function_name,
346     const char *session_dictionary_name,
347     const lldb::ValueObjectSP& valobj_sp
348 );
349 
350 extern "C" void*
351 LLDBSwigPythonCreateSyntheticProvider
352 (
353     const std::string python_class_name,
354     const char *session_dictionary_name,
355     const lldb::ValueObjectSP& valobj_sp
356 );
357 
358 
359 extern "C" uint32_t       LLDBSwigPython_CalculateNumChildren        (void *implementor);
360 extern "C" void*          LLDBSwigPython_GetChildAtIndex             (void *implementor, uint32_t idx);
361 extern "C" int            LLDBSwigPython_GetIndexOfChildWithName     (void *implementor, const char* child_name);
362 extern "C" void*          LLDBSWIGPython_CastPyObjectToSBValue       (void* data);
363 extern "C" void           LLDBSwigPython_UpdateSynthProviderInstance (void* implementor);
364 
365 extern "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,
372     lldb_private::CommandReturnObject& cmd_retobj
373 );
374 
375 // Defined in the SWIG source file
376 extern "C" void
377 init_lldb(void);
378 
379 extern "C" bool           LLDBSwigPythonCallModuleInit
380 (
381     const std::string python_module_name,
382     const char *session_dictionary_name,
383     lldb::DebuggerSP& debugger
384 );
385 
386 #else
387 
388 extern "C" void init_lldb(void);
389 
390 // Usually defined in the SWIG source file, but we have sripting disabled
391 extern "C" void
392 init_lldb(void)
393 {
394 }
395 
396 #endif
397 
398 void
399 SBCommandInterpreter::InitializeSWIG ()
400 {
401     static bool g_initialized = false;
402     if (!g_initialized)
403     {
404         g_initialized = true;
405 #ifndef LLDB_DISABLE_PYTHON
406         ScriptInterpreter::InitializeInterpreter (init_lldb,
407                                                   LLDBSwigPythonBreakpointCallbackFunction,
408                                                   LLDBSwigPythonCallTypeScript,
409                                                   LLDBSwigPythonCreateSyntheticProvider,
410                                                   LLDBSwigPython_CalculateNumChildren,
411                                                   LLDBSwigPython_GetChildAtIndex,
412                                                   LLDBSwigPython_GetIndexOfChildWithName,
413                                                   LLDBSWIGPython_CastPyObjectToSBValue,
414                                                   LLDBSwigPython_UpdateSynthProviderInstance,
415                                                   LLDBSwigPythonCallCommand,
416                                                   LLDBSwigPythonCallModuleInit);
417 #endif
418     }
419 }
420