ScriptInterpreter.h revision 36da2aa6dc5ad9994b638ed09eb81c44cc05540b
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    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 ScriptInterpreter
66{
67public:
68
69    typedef void (*SWIGInitCallback) (void);
70
71    typedef bool (*SWIGBreakpointCallbackFunction) (const char *python_function_name,
72                                                    const char *session_dictionary_name,
73                                                    const lldb::StackFrameSP& frame_sp,
74                                                    const lldb::BreakpointLocationSP &bp_loc_sp);
75
76    typedef bool (*SWIGWatchpointCallbackFunction) (const char *python_function_name,
77                                                    const char *session_dictionary_name,
78                                                    const lldb::StackFrameSP& frame_sp,
79                                                    const lldb::WatchpointSP &wp_sp);
80
81    typedef bool (*SWIGPythonTypeScriptCallbackFunction) (const char *python_function_name,
82                                                          void *session_dictionary,
83                                                          const lldb::ValueObjectSP& valobj_sp,
84                                                          void** pyfunct_wrapper,
85                                                          std::string& retval);
86
87    typedef void* (*SWIGPythonCreateSyntheticProvider) (const char *python_class_name,
88                                                        const char *session_dictionary_name,
89                                                        const lldb::ValueObjectSP& valobj_sp);
90
91    typedef void* (*SWIGPythonCreateOSPlugin) (const char *python_class_name,
92                                               const char *session_dictionary_name,
93                                               const lldb::ProcessSP& process_sp);
94
95    typedef uint32_t       (*SWIGPythonCalculateNumChildren)                   (void *implementor);
96    typedef void*          (*SWIGPythonGetChildAtIndex)                        (void *implementor, uint32_t idx);
97    typedef int            (*SWIGPythonGetIndexOfChildWithName)                (void *implementor, const char* child_name);
98    typedef void*          (*SWIGPythonCastPyObjectToSBValue)                  (void* data);
99    typedef bool           (*SWIGPythonUpdateSynthProviderInstance)            (void* data);
100    typedef bool           (*SWIGPythonMightHaveChildrenSynthProviderInstance) (void* data);
101
102
103    typedef bool           (*SWIGPythonCallCommand)                 (const char *python_function_name,
104                                                                     const char *session_dictionary_name,
105                                                                     lldb::DebuggerSP& debugger,
106                                                                     const char* args,
107                                                                     std::string& err_msg,
108                                                                     lldb_private::CommandReturnObject& cmd_retobj);
109
110    typedef bool           (*SWIGPythonCallModuleInit)              (const char *python_module_name,
111                                                                     const char *session_dictionary_name,
112                                                                     lldb::DebuggerSP& debugger);
113
114    typedef enum
115    {
116        eScriptReturnTypeCharPtr,
117        eScriptReturnTypeBool,
118        eScriptReturnTypeShortInt,
119        eScriptReturnTypeShortIntUnsigned,
120        eScriptReturnTypeInt,
121        eScriptReturnTypeIntUnsigned,
122        eScriptReturnTypeLongInt,
123        eScriptReturnTypeLongIntUnsigned,
124        eScriptReturnTypeLongLong,
125        eScriptReturnTypeLongLongUnsigned,
126        eScriptReturnTypeFloat,
127        eScriptReturnTypeDouble,
128        eScriptReturnTypeChar,
129        eScriptReturnTypeCharStrOrNone
130    } ScriptReturnType;
131
132    ScriptInterpreter (CommandInterpreter &interpreter, lldb::ScriptLanguage script_lang);
133
134    virtual ~ScriptInterpreter ();
135
136    struct ExecuteScriptOptions
137    {
138    public:
139        ExecuteScriptOptions () :
140            m_enable_io(true),
141            m_set_lldb_globals(true),
142            m_maskout_errors(true)
143        {
144        }
145
146        bool
147        GetEnableIO () const
148        {
149            return m_enable_io;
150        }
151
152        bool
153        GetSetLLDBGlobals () const
154        {
155            return m_set_lldb_globals;
156        }
157
158        bool
159        GetMaskoutErrors () const
160        {
161            return m_maskout_errors;
162        }
163
164        ExecuteScriptOptions&
165        SetEnableIO (bool enable)
166        {
167            m_enable_io = enable;
168            return *this;
169        }
170
171        ExecuteScriptOptions&
172        SetSetLLDBGlobals (bool set)
173        {
174            m_set_lldb_globals = set;
175            return *this;
176        }
177
178        ExecuteScriptOptions&
179        SetMaskoutErrors (bool maskout)
180        {
181            m_maskout_errors = maskout;
182            return *this;
183        }
184
185    private:
186        bool m_enable_io;
187        bool m_set_lldb_globals;
188        bool m_maskout_errors;
189    };
190
191    virtual bool
192    ExecuteOneLine (const char *command,
193                    CommandReturnObject *result,
194                    const ExecuteScriptOptions &options = ExecuteScriptOptions()) = 0;
195
196    virtual void
197    ExecuteInterpreterLoop () = 0;
198
199    virtual bool
200    ExecuteOneLineWithReturn (const char *in_string,
201                              ScriptReturnType return_type,
202                              void *ret_value,
203                              const ExecuteScriptOptions &options = ExecuteScriptOptions())
204    {
205        return true;
206    }
207
208    virtual bool
209    ExecuteMultipleLines (const char *in_string,
210                          const ExecuteScriptOptions &options = ExecuteScriptOptions())
211    {
212        return true;
213    }
214
215    virtual bool
216    ExportFunctionDefinitionToInterpreter (StringList &function_def)
217    {
218        return false;
219    }
220
221    virtual bool
222    GenerateBreakpointCommandCallbackData (StringList &input, std::string& output)
223    {
224        return false;
225    }
226
227    virtual bool
228    GenerateWatchpointCommandCallbackData (StringList &input, std::string& output)
229    {
230        return false;
231    }
232
233    virtual bool
234    GenerateTypeScriptFunction (const char* oneliner, std::string& output, void* name_token = NULL)
235    {
236        return false;
237    }
238
239    virtual bool
240    GenerateTypeScriptFunction (StringList &input, std::string& output, void* name_token = NULL)
241    {
242        return false;
243    }
244
245    virtual bool
246    GenerateScriptAliasFunction (StringList &input, std::string& output)
247    {
248        return false;
249    }
250
251    virtual bool
252    GenerateTypeSynthClass (StringList &input, std::string& output, void* name_token = NULL)
253    {
254        return false;
255    }
256
257    virtual bool
258    GenerateTypeSynthClass (const char* oneliner, std::string& output, void* name_token = NULL)
259    {
260        return false;
261    }
262
263    virtual lldb::ScriptInterpreterObjectSP
264    CreateSyntheticScriptedProvider (const char *class_name,
265                                     lldb::ValueObjectSP valobj)
266    {
267        return lldb::ScriptInterpreterObjectSP();
268    }
269
270    virtual lldb::ScriptInterpreterObjectSP
271    OSPlugin_CreatePluginObject (const char *class_name,
272                                 lldb::ProcessSP process_sp)
273    {
274        return lldb::ScriptInterpreterObjectSP();
275    }
276
277    virtual lldb::ScriptInterpreterObjectSP
278    OSPlugin_RegisterInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp)
279    {
280        return lldb::ScriptInterpreterObjectSP();
281    }
282
283    virtual lldb::ScriptInterpreterObjectSP
284    OSPlugin_ThreadsInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp)
285    {
286        return lldb::ScriptInterpreterObjectSP();
287    }
288
289    virtual lldb::ScriptInterpreterObjectSP
290    OSPlugin_RegisterContextData (lldb::ScriptInterpreterObjectSP os_plugin_object_sp,
291                                  lldb::tid_t thread_id)
292    {
293        return lldb::ScriptInterpreterObjectSP();
294    }
295
296    virtual lldb::ScriptInterpreterObjectSP
297    OSPlugin_CreateThread (lldb::ScriptInterpreterObjectSP os_plugin_object_sp,
298                           lldb::tid_t tid,
299                           lldb::addr_t context)
300    {
301        return lldb::ScriptInterpreterObjectSP();
302    }
303
304    virtual bool
305    GenerateFunction(const char *signature, const StringList &input)
306    {
307        return false;
308    }
309
310    virtual void
311    CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options,
312                                             CommandReturnObject &result);
313
314    virtual void
315    CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options,
316                                             CommandReturnObject &result);
317
318    /// Set a one-liner as the callback for the breakpoint.
319    virtual void
320    SetBreakpointCommandCallback (BreakpointOptions *bp_options,
321                                  const char *oneliner)
322    {
323        return;
324    }
325
326    /// Set a one-liner as the callback for the watchpoint.
327    virtual void
328    SetWatchpointCommandCallback (WatchpointOptions *wp_options,
329                                  const char *oneliner)
330    {
331        return;
332    }
333
334    virtual bool
335    GetScriptedSummary (const char *function_name,
336                        lldb::ValueObjectSP valobj,
337                        lldb::ScriptInterpreterObjectSP& callee_wrapper_sp,
338                        std::string& retval)
339    {
340        return false;
341    }
342
343    virtual size_t
344    CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor)
345    {
346        return 0;
347    }
348
349    virtual lldb::ValueObjectSP
350    GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor, uint32_t idx)
351    {
352        return lldb::ValueObjectSP();
353    }
354
355    virtual int
356    GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor, const char* child_name)
357    {
358        return UINT32_MAX;
359    }
360
361    virtual bool
362    UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor)
363    {
364        return false;
365    }
366
367    virtual bool
368    MightHaveChildrenSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor)
369    {
370        return true;
371    }
372
373    virtual bool
374    RunScriptBasedCommand (const char* impl_function,
375                           const char* args,
376                           ScriptedCommandSynchronicity synchronicity,
377                           lldb_private::CommandReturnObject& cmd_retobj,
378                           Error& error)
379    {
380        return false;
381    }
382
383    virtual bool
384    GetDocumentationForItem (const char* item, std::string& dest)
385    {
386		dest.clear();
387        return false;
388    }
389
390    virtual bool
391    CheckObjectExists (const char* name)
392    {
393        return false;
394    }
395
396    virtual bool
397    LoadScriptingModule (const char* filename,
398                         bool can_reload,
399                         bool init_session,
400                         lldb_private::Error& error)
401    {
402        error.SetErrorString("loading unimplemented");
403        return false;
404    }
405
406    virtual lldb::ScriptInterpreterObjectSP
407    MakeScriptObject (void* object)
408    {
409        return lldb::ScriptInterpreterObjectSP(new ScriptInterpreterObject(object));
410    }
411
412    const char *
413    GetScriptInterpreterPtyName ();
414
415    int
416    GetMasterFileDescriptor ();
417
418	CommandInterpreter &
419	GetCommandInterpreter ();
420
421    static std::string
422    LanguageToString (lldb::ScriptLanguage language);
423
424    static void
425    InitializeInterpreter (SWIGInitCallback python_swig_init_callback);
426
427    static void
428    TerminateInterpreter ();
429
430    virtual void
431    ResetOutputFileHandle (FILE *new_fh) { } //By default, do nothing.
432
433protected:
434    CommandInterpreter &m_interpreter;
435    lldb::ScriptLanguage m_script_lang;
436};
437
438} // namespace lldb_private
439
440#endif // #ifndef liblldb_ScriptInterpreter_h_
441