1 //===-- ScriptInterpreter.h -------------------------------------*- 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 #ifndef liblldb_ScriptInterpreter_h_
11 #define liblldb_ScriptInterpreter_h_
12 
13 #include "lldb/lldb-private.h"
14 
15 #include "lldb/Core/Broadcaster.h"
16 #include "lldb/Core/Error.h"
17 
18 #include "lldb/Utility/PseudoTerminal.h"
19 
20 
21 namespace lldb_private {
22 
23 class ScriptInterpreterObject
24 {
25 public:
ScriptInterpreterObject()26     ScriptInterpreterObject() :
27     m_object(NULL)
28     {}
29 
ScriptInterpreterObject(void * obj)30     ScriptInterpreterObject(void* obj) :
31     m_object(obj)
32     {}
33 
ScriptInterpreterObject(const ScriptInterpreterObject & rhs)34     ScriptInterpreterObject(const ScriptInterpreterObject& rhs)
35     : m_object(rhs.m_object)
36     {}
37 
38     virtual void*
GetObject()39     GetObject()
40     {
41         return m_object;
42     }
43 
44     operator bool ()
45     {
46         return m_object != NULL;
47     }
48 
49     ScriptInterpreterObject&
50     operator = (const ScriptInterpreterObject& rhs)
51     {
52         if (this != &rhs)
53             m_object = rhs.m_object;
54         return *this;
55     }
56 
57     virtual
~ScriptInterpreterObject()58     ~ScriptInterpreterObject()
59     {}
60 
61 protected:
62     void* m_object;
63 };
64 
65 class ScriptInterpreterLocker
66 {
67 public:
68 
ScriptInterpreterLocker()69     ScriptInterpreterLocker ()
70     {
71     }
72 
~ScriptInterpreterLocker()73     virtual ~ScriptInterpreterLocker ()
74     {
75     }
76 private:
77     DISALLOW_COPY_AND_ASSIGN (ScriptInterpreterLocker);
78 };
79 
80 
81 class ScriptInterpreter
82 {
83 public:
84 
85     typedef void (*SWIGInitCallback) (void);
86 
87     typedef bool (*SWIGBreakpointCallbackFunction) (const char *python_function_name,
88                                                     const char *session_dictionary_name,
89                                                     const lldb::StackFrameSP& frame_sp,
90                                                     const lldb::BreakpointLocationSP &bp_loc_sp);
91 
92     typedef bool (*SWIGWatchpointCallbackFunction) (const char *python_function_name,
93                                                     const char *session_dictionary_name,
94                                                     const lldb::StackFrameSP& frame_sp,
95                                                     const lldb::WatchpointSP &wp_sp);
96 
97     typedef bool (*SWIGPythonTypeScriptCallbackFunction) (const char *python_function_name,
98                                                           void *session_dictionary,
99                                                           const lldb::ValueObjectSP& valobj_sp,
100                                                           void** pyfunct_wrapper,
101                                                           std::string& retval);
102 
103     typedef void* (*SWIGPythonCreateSyntheticProvider) (const char *python_class_name,
104                                                         const char *session_dictionary_name,
105                                                         const lldb::ValueObjectSP& valobj_sp);
106 
107     typedef void* (*SWIGPythonCreateOSPlugin) (const char *python_class_name,
108                                                const char *session_dictionary_name,
109                                                const lldb::ProcessSP& process_sp);
110 
111     typedef uint32_t       (*SWIGPythonCalculateNumChildren)                   (void *implementor);
112     typedef void*          (*SWIGPythonGetChildAtIndex)                        (void *implementor, uint32_t idx);
113     typedef int            (*SWIGPythonGetIndexOfChildWithName)                (void *implementor, const char* child_name);
114     typedef void*          (*SWIGPythonCastPyObjectToSBValue)                  (void* data);
115     typedef bool           (*SWIGPythonUpdateSynthProviderInstance)            (void* data);
116     typedef bool           (*SWIGPythonMightHaveChildrenSynthProviderInstance) (void* data);
117 
118 
119     typedef bool           (*SWIGPythonCallCommand)                 (const char *python_function_name,
120                                                                      const char *session_dictionary_name,
121                                                                      lldb::DebuggerSP& debugger,
122                                                                      const char* args,
123                                                                      lldb_private::CommandReturnObject& cmd_retobj);
124 
125     typedef bool           (*SWIGPythonCallModuleInit)              (const char *python_module_name,
126                                                                      const char *session_dictionary_name,
127                                                                      lldb::DebuggerSP& debugger);
128 
129     typedef bool            (*SWIGPythonScriptKeyword_Process)      (const char* python_function_name,
130                                                                      const char* session_dictionary_name,
131                                                                      lldb::ProcessSP& process,
132                                                                      std::string& output);
133     typedef bool            (*SWIGPythonScriptKeyword_Thread)      (const char* python_function_name,
134                                                                     const char* session_dictionary_name,
135                                                                     lldb::ThreadSP& thread,
136                                                                     std::string& output);
137 
138     typedef bool            (*SWIGPythonScriptKeyword_Target)      (const char* python_function_name,
139                                                                     const char* session_dictionary_name,
140                                                                     lldb::TargetSP& target,
141                                                                     std::string& output);
142 
143     typedef bool            (*SWIGPythonScriptKeyword_Frame)      (const char* python_function_name,
144                                                                     const char* session_dictionary_name,
145                                                                     lldb::StackFrameSP& frame,
146                                                                     std::string& output);
147 
148 
149 
150     typedef enum
151     {
152         eScriptReturnTypeCharPtr,
153         eScriptReturnTypeBool,
154         eScriptReturnTypeShortInt,
155         eScriptReturnTypeShortIntUnsigned,
156         eScriptReturnTypeInt,
157         eScriptReturnTypeIntUnsigned,
158         eScriptReturnTypeLongInt,
159         eScriptReturnTypeLongIntUnsigned,
160         eScriptReturnTypeLongLong,
161         eScriptReturnTypeLongLongUnsigned,
162         eScriptReturnTypeFloat,
163         eScriptReturnTypeDouble,
164         eScriptReturnTypeChar,
165         eScriptReturnTypeCharStrOrNone
166     } ScriptReturnType;
167 
168     ScriptInterpreter (CommandInterpreter &interpreter, lldb::ScriptLanguage script_lang);
169 
170     virtual ~ScriptInterpreter ();
171 
172     struct ExecuteScriptOptions
173     {
174     public:
ExecuteScriptOptionsExecuteScriptOptions175         ExecuteScriptOptions () :
176             m_enable_io(true),
177             m_set_lldb_globals(true),
178             m_maskout_errors(true)
179         {
180         }
181 
182         bool
GetEnableIOExecuteScriptOptions183         GetEnableIO () const
184         {
185             return m_enable_io;
186         }
187 
188         bool
GetSetLLDBGlobalsExecuteScriptOptions189         GetSetLLDBGlobals () const
190         {
191             return m_set_lldb_globals;
192         }
193 
194         bool
GetMaskoutErrorsExecuteScriptOptions195         GetMaskoutErrors () const
196         {
197             return m_maskout_errors;
198         }
199 
200         ExecuteScriptOptions&
SetEnableIOExecuteScriptOptions201         SetEnableIO (bool enable)
202         {
203             m_enable_io = enable;
204             return *this;
205         }
206 
207         ExecuteScriptOptions&
SetSetLLDBGlobalsExecuteScriptOptions208         SetSetLLDBGlobals (bool set)
209         {
210             m_set_lldb_globals = set;
211             return *this;
212         }
213 
214         ExecuteScriptOptions&
SetMaskoutErrorsExecuteScriptOptions215         SetMaskoutErrors (bool maskout)
216         {
217             m_maskout_errors = maskout;
218             return *this;
219         }
220 
221     private:
222         bool m_enable_io;
223         bool m_set_lldb_globals;
224         bool m_maskout_errors;
225     };
226 
227     virtual bool
228     ExecuteOneLine (const char *command,
229                     CommandReturnObject *result,
230                     const ExecuteScriptOptions &options = ExecuteScriptOptions()) = 0;
231 
232     virtual void
233     ExecuteInterpreterLoop () = 0;
234 
235     virtual bool
236     ExecuteOneLineWithReturn (const char *in_string,
237                               ScriptReturnType return_type,
238                               void *ret_value,
239                               const ExecuteScriptOptions &options = ExecuteScriptOptions())
240     {
241         return true;
242     }
243 
244     virtual bool
245     ExecuteMultipleLines (const char *in_string,
246                           const ExecuteScriptOptions &options = ExecuteScriptOptions())
247     {
248         return true;
249     }
250 
251     virtual bool
ExportFunctionDefinitionToInterpreter(StringList & function_def)252     ExportFunctionDefinitionToInterpreter (StringList &function_def)
253     {
254         return false;
255     }
256 
257     virtual bool
GenerateBreakpointCommandCallbackData(StringList & input,std::string & output)258     GenerateBreakpointCommandCallbackData (StringList &input, std::string& output)
259     {
260         return false;
261     }
262 
263     virtual bool
GenerateWatchpointCommandCallbackData(StringList & input,std::string & output)264     GenerateWatchpointCommandCallbackData (StringList &input, std::string& output)
265     {
266         return false;
267     }
268 
269     virtual bool
270     GenerateTypeScriptFunction (const char* oneliner, std::string& output, void* name_token = NULL)
271     {
272         return false;
273     }
274 
275     virtual bool
276     GenerateTypeScriptFunction (StringList &input, std::string& output, void* name_token = NULL)
277     {
278         return false;
279     }
280 
281     virtual bool
GenerateScriptAliasFunction(StringList & input,std::string & output)282     GenerateScriptAliasFunction (StringList &input, std::string& output)
283     {
284         return false;
285     }
286 
287     virtual bool
288     GenerateTypeSynthClass (StringList &input, std::string& output, void* name_token = NULL)
289     {
290         return false;
291     }
292 
293     virtual bool
294     GenerateTypeSynthClass (const char* oneliner, std::string& output, void* name_token = NULL)
295     {
296         return false;
297     }
298 
299     virtual lldb::ScriptInterpreterObjectSP
CreateSyntheticScriptedProvider(const char * class_name,lldb::ValueObjectSP valobj)300     CreateSyntheticScriptedProvider (const char *class_name,
301                                      lldb::ValueObjectSP valobj)
302     {
303         return lldb::ScriptInterpreterObjectSP();
304     }
305 
306     virtual lldb::ScriptInterpreterObjectSP
OSPlugin_CreatePluginObject(const char * class_name,lldb::ProcessSP process_sp)307     OSPlugin_CreatePluginObject (const char *class_name,
308                                  lldb::ProcessSP process_sp)
309     {
310         return lldb::ScriptInterpreterObjectSP();
311     }
312 
313     virtual lldb::ScriptInterpreterObjectSP
OSPlugin_RegisterInfo(lldb::ScriptInterpreterObjectSP os_plugin_object_sp)314     OSPlugin_RegisterInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp)
315     {
316         return lldb::ScriptInterpreterObjectSP();
317     }
318 
319     virtual lldb::ScriptInterpreterObjectSP
OSPlugin_ThreadsInfo(lldb::ScriptInterpreterObjectSP os_plugin_object_sp)320     OSPlugin_ThreadsInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp)
321     {
322         return lldb::ScriptInterpreterObjectSP();
323     }
324 
325     virtual lldb::ScriptInterpreterObjectSP
OSPlugin_RegisterContextData(lldb::ScriptInterpreterObjectSP os_plugin_object_sp,lldb::tid_t thread_id)326     OSPlugin_RegisterContextData (lldb::ScriptInterpreterObjectSP os_plugin_object_sp,
327                                   lldb::tid_t thread_id)
328     {
329         return lldb::ScriptInterpreterObjectSP();
330     }
331 
332     virtual lldb::ScriptInterpreterObjectSP
OSPlugin_CreateThread(lldb::ScriptInterpreterObjectSP os_plugin_object_sp,lldb::tid_t tid,lldb::addr_t context)333     OSPlugin_CreateThread (lldb::ScriptInterpreterObjectSP os_plugin_object_sp,
334                            lldb::tid_t tid,
335                            lldb::addr_t context)
336     {
337         return lldb::ScriptInterpreterObjectSP();
338     }
339 
340     virtual bool
GenerateFunction(const char * signature,const StringList & input)341     GenerateFunction(const char *signature, const StringList &input)
342     {
343         return false;
344     }
345 
346     virtual void
347     CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options,
348                                              CommandReturnObject &result);
349 
350     virtual void
351     CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options,
352                                              CommandReturnObject &result);
353 
354     /// Set a one-liner as the callback for the breakpoint.
355     virtual void
SetBreakpointCommandCallback(BreakpointOptions * bp_options,const char * oneliner)356     SetBreakpointCommandCallback (BreakpointOptions *bp_options,
357                                   const char *oneliner)
358     {
359         return;
360     }
361 
362     /// Set a one-liner as the callback for the watchpoint.
363     virtual void
SetWatchpointCommandCallback(WatchpointOptions * wp_options,const char * oneliner)364     SetWatchpointCommandCallback (WatchpointOptions *wp_options,
365                                   const char *oneliner)
366     {
367         return;
368     }
369 
370     virtual bool
GetScriptedSummary(const char * function_name,lldb::ValueObjectSP valobj,lldb::ScriptInterpreterObjectSP & callee_wrapper_sp,std::string & retval)371     GetScriptedSummary (const char *function_name,
372                         lldb::ValueObjectSP valobj,
373                         lldb::ScriptInterpreterObjectSP& callee_wrapper_sp,
374                         std::string& retval)
375     {
376         return false;
377     }
378 
379     virtual size_t
CalculateNumChildren(const lldb::ScriptInterpreterObjectSP & implementor)380     CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor)
381     {
382         return 0;
383     }
384 
385     virtual lldb::ValueObjectSP
GetChildAtIndex(const lldb::ScriptInterpreterObjectSP & implementor,uint32_t idx)386     GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor, uint32_t idx)
387     {
388         return lldb::ValueObjectSP();
389     }
390 
391     virtual int
GetIndexOfChildWithName(const lldb::ScriptInterpreterObjectSP & implementor,const char * child_name)392     GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor, const char* child_name)
393     {
394         return UINT32_MAX;
395     }
396 
397     virtual bool
UpdateSynthProviderInstance(const lldb::ScriptInterpreterObjectSP & implementor)398     UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor)
399     {
400         return false;
401     }
402 
403     virtual bool
MightHaveChildrenSynthProviderInstance(const lldb::ScriptInterpreterObjectSP & implementor)404     MightHaveChildrenSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor)
405     {
406         return true;
407     }
408 
409     virtual bool
RunScriptBasedCommand(const char * impl_function,const char * args,ScriptedCommandSynchronicity synchronicity,lldb_private::CommandReturnObject & cmd_retobj,Error & error)410     RunScriptBasedCommand (const char* impl_function,
411                            const char* args,
412                            ScriptedCommandSynchronicity synchronicity,
413                            lldb_private::CommandReturnObject& cmd_retobj,
414                            Error& error)
415     {
416         return false;
417     }
418 
419     virtual bool
RunScriptFormatKeyword(const char * impl_function,Process * process,std::string & output,Error & error)420     RunScriptFormatKeyword (const char* impl_function,
421                             Process* process,
422                             std::string& output,
423                             Error& error)
424     {
425         error.SetErrorString("unimplemented");
426         return false;
427     }
428 
429     virtual bool
RunScriptFormatKeyword(const char * impl_function,Thread * thread,std::string & output,Error & error)430     RunScriptFormatKeyword (const char* impl_function,
431                             Thread* thread,
432                             std::string& output,
433                             Error& error)
434     {
435         error.SetErrorString("unimplemented");
436         return false;
437     }
438 
439     virtual bool
RunScriptFormatKeyword(const char * impl_function,Target * target,std::string & output,Error & error)440     RunScriptFormatKeyword (const char* impl_function,
441                             Target* target,
442                             std::string& output,
443                             Error& error)
444     {
445         error.SetErrorString("unimplemented");
446         return false;
447     }
448 
449     virtual bool
RunScriptFormatKeyword(const char * impl_function,StackFrame * frame,std::string & output,Error & error)450     RunScriptFormatKeyword (const char* impl_function,
451                             StackFrame* frame,
452                             std::string& output,
453                             Error& error)
454     {
455         error.SetErrorString("unimplemented");
456         return false;
457     }
458 
459     virtual bool
GetDocumentationForItem(const char * item,std::string & dest)460     GetDocumentationForItem (const char* item, std::string& dest)
461     {
462 		dest.clear();
463         return false;
464     }
465 
466     virtual bool
CheckObjectExists(const char * name)467     CheckObjectExists (const char* name)
468     {
469         return false;
470     }
471 
472     virtual bool
LoadScriptingModule(const char * filename,bool can_reload,bool init_session,lldb_private::Error & error)473     LoadScriptingModule (const char* filename,
474                          bool can_reload,
475                          bool init_session,
476                          lldb_private::Error& error)
477     {
478         error.SetErrorString("loading unimplemented");
479         return false;
480     }
481 
482     virtual lldb::ScriptInterpreterObjectSP
MakeScriptObject(void * object)483     MakeScriptObject (void* object)
484     {
485         return lldb::ScriptInterpreterObjectSP(new ScriptInterpreterObject(object));
486     }
487 
488     virtual std::unique_ptr<ScriptInterpreterLocker>
489     AcquireInterpreterLock ();
490 
491     const char *
492     GetScriptInterpreterPtyName ();
493 
494     int
495     GetMasterFileDescriptor ();
496 
497 	CommandInterpreter &
498 	GetCommandInterpreter ();
499 
500     static std::string
501     LanguageToString (lldb::ScriptLanguage language);
502 
503     static void
504     InitializeInterpreter (SWIGInitCallback python_swig_init_callback);
505 
506     static void
507     TerminateInterpreter ();
508 
509     virtual void
ResetOutputFileHandle(FILE * new_fh)510     ResetOutputFileHandle (FILE *new_fh) { } //By default, do nothing.
511 
512 protected:
513     CommandInterpreter &m_interpreter;
514     lldb::ScriptLanguage m_script_lang;
515 };
516 
517 } // namespace lldb_private
518 
519 #endif // #ifndef liblldb_ScriptInterpreter_h_
520