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