ScriptInterpreter.h revision 640d5b7744750f7d37d69c6b6012549e54a711da
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    CreateOSPlugin (const char *class_name,
272                    lldb::ProcessSP process_sp)
273    {
274        return lldb::ScriptInterpreterObjectSP();
275    }
276
277    virtual lldb::ScriptInterpreterObjectSP
278    OSPlugin_QueryForRegisterInfo (lldb::ScriptInterpreterObjectSP object)
279    {
280        return lldb::ScriptInterpreterObjectSP();
281    }
282
283    virtual lldb::ScriptInterpreterObjectSP
284    OSPlugin_QueryForThreadsInfo (lldb::ScriptInterpreterObjectSP object)
285    {
286        return lldb::ScriptInterpreterObjectSP();
287    }
288
289    virtual lldb::ScriptInterpreterObjectSP
290    OSPlugin_QueryForRegisterContextData (lldb::ScriptInterpreterObjectSP object,
291                                          lldb::tid_t thread_id)
292    {
293        return lldb::ScriptInterpreterObjectSP();
294    }
295
296    virtual bool
297    GenerateFunction(const char *signature, const StringList &input)
298    {
299        return false;
300    }
301
302    virtual void
303    CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options,
304                                             CommandReturnObject &result);
305
306    virtual void
307    CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options,
308                                             CommandReturnObject &result);
309
310    /// Set a one-liner as the callback for the breakpoint.
311    virtual void
312    SetBreakpointCommandCallback (BreakpointOptions *bp_options,
313                                  const char *oneliner)
314    {
315        return;
316    }
317
318    /// Set a one-liner as the callback for the watchpoint.
319    virtual void
320    SetWatchpointCommandCallback (WatchpointOptions *wp_options,
321                                  const char *oneliner)
322    {
323        return;
324    }
325
326    virtual bool
327    GetScriptedSummary (const char *function_name,
328                        lldb::ValueObjectSP valobj,
329                        lldb::ScriptInterpreterObjectSP& callee_wrapper_sp,
330                        std::string& retval)
331    {
332        return false;
333    }
334
335    virtual uint32_t
336    CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor)
337    {
338        return 0;
339    }
340
341    virtual lldb::ValueObjectSP
342    GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor, uint32_t idx)
343    {
344        return lldb::ValueObjectSP();
345    }
346
347    virtual int
348    GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor, const char* child_name)
349    {
350        return UINT32_MAX;
351    }
352
353    virtual bool
354    UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor)
355    {
356        return false;
357    }
358
359    virtual bool
360    MightHaveChildrenSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor)
361    {
362        return true;
363    }
364
365    virtual bool
366    RunScriptBasedCommand (const char* impl_function,
367                           const char* args,
368                           ScriptedCommandSynchronicity synchronicity,
369                           lldb_private::CommandReturnObject& cmd_retobj,
370                           Error& error)
371    {
372        return false;
373    }
374
375    virtual bool
376    GetDocumentationForItem (const char* item, std::string& dest)
377    {
378		dest.clear();
379        return false;
380    }
381
382    virtual bool
383    CheckObjectExists (const char* name)
384    {
385        return false;
386    }
387
388    virtual bool
389    LoadScriptingModule (const char* filename,
390                         bool can_reload,
391                         bool init_session,
392                         lldb_private::Error& error)
393    {
394        error.SetErrorString("loading unimplemented");
395        return false;
396    }
397
398    virtual lldb::ScriptInterpreterObjectSP
399    MakeScriptObject (void* object)
400    {
401        return lldb::ScriptInterpreterObjectSP(new ScriptInterpreterObject(object));
402    }
403
404    const char *
405    GetScriptInterpreterPtyName ();
406
407    int
408    GetMasterFileDescriptor ();
409
410	CommandInterpreter &
411	GetCommandInterpreter ();
412
413    static std::string
414    LanguageToString (lldb::ScriptLanguage language);
415
416    static void
417    InitializeInterpreter (SWIGInitCallback python_swig_init_callback);
418
419    static void
420    TerminateInterpreter ();
421
422    virtual void
423    ResetOutputFileHandle (FILE *new_fh) { } //By default, do nothing.
424
425protected:
426    CommandInterpreter &m_interpreter;
427    lldb::ScriptLanguage m_script_lang;
428};
429
430} // namespace lldb_private
431
432#endif // #ifndef liblldb_ScriptInterpreter_h_
433