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 (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 (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 (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 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     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);
132 
133         SBStringList temp_list (&lldb_matches);
134         matches.AppendList (temp_list);
135     }
136     return num_completions;
137 }
138 
139 bool
140 SBCommandInterpreter::HasCommands ()
141 {
142     if (m_opaque_ptr)
143         return m_opaque_ptr->HasCommands();
144     return false;
145 }
146 
147 bool
148 SBCommandInterpreter::HasAliases ()
149 {
150     if (m_opaque_ptr)
151         return m_opaque_ptr->HasAliases();
152     return false;
153 }
154 
155 bool
156 SBCommandInterpreter::HasAliasOptions ()
157 {
158     if (m_opaque_ptr)
159         return m_opaque_ptr->HasAliasOptions ();
160     return false;
161 }
162 
163 SBProcess
164 SBCommandInterpreter::GetProcess ()
165 {
166     SBProcess process;
167     if (m_opaque_ptr)
168     {
169         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         }
175     }
176     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
177 
178     if (log)
179         log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)",
180                      m_opaque_ptr, process.get());
181 
182 
183     return process;
184 }
185 
186 ssize_t
187 SBCommandInterpreter::WriteToScriptInterpreter (const char *src)
188 {
189     return WriteToScriptInterpreter (src, strlen(src));
190 }
191 
192 ssize_t
193 SBCommandInterpreter::WriteToScriptInterpreter (const char *src, size_t src_len)
194 {
195     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
196 
197     ssize_t bytes_written = 0;
198     if (m_opaque_ptr && src && src[0])
199     {
200         ScriptInterpreter *script_interpreter = m_opaque_ptr->GetScriptInterpreter();
201         if (script_interpreter)
202             bytes_written = ::write (script_interpreter->GetMasterFileDescriptor(), src, src_len);
203     }
204     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;
209 }
210 
211 
212 CommandInterpreter *
213 SBCommandInterpreter::get ()
214 {
215     return m_opaque_ptr;
216 }
217 
218 CommandInterpreter &
219 SBCommandInterpreter::ref ()
220 {
221     assert (m_opaque_ptr);
222     return *m_opaque_ptr;
223 }
224 
225 void
226 SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
227 {
228     m_opaque_ptr = interpreter;
229 }
230 
231 void
232 SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
233 {
234     result.Clear();
235     if (m_opaque_ptr)
236     {
237         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());
241         m_opaque_ptr->SourceInitFile (false, result.ref());
242     }
243     else
244     {
245         result->AppendError ("SBCommandInterpreter is not valid");
246         result->SetStatus (eReturnStatusFailed);
247     }
248     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
249 
250     if (log)
251         log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))",
252                      m_opaque_ptr, result.get());
253 
254 }
255 
256 void
257 SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (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 (true, 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)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))",
277                      m_opaque_ptr, result.get());
278 }
279 
280 SBBroadcaster
281 SBCommandInterpreter::GetBroadcaster ()
282 {
283     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
284 
285     SBBroadcaster broadcaster (m_opaque_ptr, false);
286 
287     if (log)
288         log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
289                      m_opaque_ptr, broadcaster.get());
290 
291     return broadcaster;
292 }
293 
294 const char *
295 SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
296 {
297     return CommandObject::GetArgumentTypeAsCString (arg_type);
298 }
299 
300 const char *
301 SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
302 {
303     return CommandObject::GetArgumentDescriptionAsCString (arg_type);
304 }
305 
306 
307 extern "C" bool
308 LLDBSwigPythonBreakpointCallbackFunction
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 );
315 
316 extern "C" std::string
317 LLDBSwigPythonCallTypeScript
318 (
319     const char *python_function_name,
320     const char *session_dictionary_name,
321     const lldb::ValueObjectSP& valobj_sp
322 );
323 
324 extern "C" void*
325 LLDBSwigPythonCreateSyntheticProvider
326 (
327     const std::string python_class_name,
328     const char *session_dictionary_name,
329     const lldb::ValueObjectSP& valobj_sp
330 );
331 
332 
333 extern "C" uint32_t       LLDBSwigPython_CalculateNumChildren        (void *implementor);
334 extern "C" void*          LLDBSwigPython_GetChildAtIndex             (void *implementor, uint32_t idx);
335 extern "C" int            LLDBSwigPython_GetIndexOfChildWithName     (void *implementor, const char* child_name);
336 extern "C" void*          LLDBSWIGPython_CastPyObjectToSBValue       (void* data);
337 extern "C" void           LLDBSwigPython_UpdateSynthProviderInstance (void* implementor);
338 
339 extern "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,
346     lldb_private::CommandReturnObject& cmd_retobj
347 );
348 
349 
350 extern "C" void init_lldb(void);
351 
352 void
353 SBCommandInterpreter::InitializeSWIG ()
354 {
355     static bool g_initialized = false;
356     if (!g_initialized)
357     {
358         g_initialized = true;
359         ScriptInterpreter::InitializeInterpreter (init_lldb,
360                                                   LLDBSwigPythonBreakpointCallbackFunction,
361                                                   LLDBSwigPythonCallTypeScript,
362                                                   LLDBSwigPythonCreateSyntheticProvider,
363                                                   LLDBSwigPython_CalculateNumChildren,
364                                                   LLDBSwigPython_GetChildAtIndex,
365                                                   LLDBSwigPython_GetIndexOfChildWithName,
366                                                   LLDBSWIGPython_CastPyObjectToSBValue,
367                                                   LLDBSwigPython_UpdateSynthProviderInstance,
368                                                   LLDBSwigPythonCallCommand);
369     }
370 }
371