ScriptInterpreter.h revision e89ab7b58d2542f4f42e923fa65ef8984840a90c
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/API/SBValue.h"
14
15#include "lldb/lldb-private.h"
16#include "lldb/Core/Broadcaster.h"
17#include "lldb/Utility/PseudoTerminal.h"
18
19
20namespace lldb_private {
21
22class ScriptInterpreter
23{
24public:
25
26    typedef void (*SWIGInitCallback) (void);
27
28    typedef bool (*SWIGBreakpointCallbackFunction) (const char *python_function_name,
29                                                    const char *session_dictionary_name,
30                                                    const lldb::StackFrameSP& frame_sp,
31                                                    const lldb::BreakpointLocationSP &bp_loc_sp);
32
33    typedef std::string (*SWIGPythonTypeScriptCallbackFunction) (const char *python_function_name,
34                                                                 const char *session_dictionary_name,
35                                                                 const lldb::ValueObjectSP& valobj_sp);
36
37    typedef void* (*SWIGPythonCreateSyntheticProvider) (const std::string python_class_name,
38                                                        const char *session_dictionary_name,
39                                                        const lldb::ValueObjectSP& valobj_sp);
40
41    typedef uint32_t       (*SWIGPythonCalculateNumChildren)    (void *implementor);
42    typedef void*          (*SWIGPythonGetChildAtIndex)         (void *implementor, uint32_t idx);
43    typedef int            (*SWIGPythonGetIndexOfChildWithName) (void *implementor, const char* child_name);
44    typedef lldb::SBValue* (*SWIGPythonCastPyObjectToSBValue)   (void* data);
45
46    typedef enum
47    {
48        eCharPtr,
49        eBool,
50        eShortInt,
51        eShortIntUnsigned,
52        eInt,
53        eIntUnsigned,
54        eLongInt,
55        eLongIntUnsigned,
56        eLongLong,
57        eLongLongUnsigned,
58        eFloat,
59        eDouble,
60        eChar
61    } ReturnType;
62
63
64    ScriptInterpreter (CommandInterpreter &interpreter, lldb::ScriptLanguage script_lang);
65
66    virtual ~ScriptInterpreter ();
67
68    virtual bool
69    ExecuteOneLine (const char *command, CommandReturnObject *result) = 0;
70
71    virtual void
72    ExecuteInterpreterLoop () = 0;
73
74    virtual bool
75    ExecuteOneLineWithReturn (const char *in_string, ReturnType return_type, void *ret_value)
76    {
77        return true;
78    }
79
80    virtual bool
81    ExecuteMultipleLines (const char *in_string)
82    {
83        return true;
84    }
85
86    virtual bool
87    ExportFunctionDefinitionToInterpreter (StringList &function_def)
88    {
89        return false;
90    }
91
92    virtual bool
93    GenerateBreakpointCommandCallbackData (StringList &input, StringList &output)
94    {
95        return false;
96    }
97
98    virtual bool
99    GenerateTypeScriptFunction (StringList &input, StringList &output)
100    {
101        return false;
102    }
103
104    virtual bool
105    GenerateTypeSynthClass (StringList &input, StringList &output)
106    {
107        return false;
108    }
109
110    virtual void*
111    CreateSyntheticScriptedProvider (std::string class_name,
112                                     lldb::ValueObjectSP valobj)
113    {
114        return NULL;
115    }
116
117    // use this if the function code is just a one-liner script
118    virtual bool
119    GenerateTypeScriptFunction (const char* oneliner, StringList &output)
120    {
121        return false;
122    }
123
124    virtual bool
125    GenerateFunction(std::string& signature, StringList &input, StringList &output)
126    {
127        return false;
128    }
129
130    virtual void
131    CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options,
132                                             CommandReturnObject &result);
133
134    /// Set a one-liner as the callback for the breakpoint.
135    virtual void
136    SetBreakpointCommandCallback (BreakpointOptions *bp_options,
137                                  const char *oneliner)
138    {
139        return;
140    }
141
142    virtual uint32_t
143    CalculateNumChildren (void *implementor)
144    {
145        return 0;
146    }
147
148    virtual void*
149    GetChildAtIndex (void *implementor, uint32_t idx)
150    {
151        return NULL;
152    }
153
154    virtual int
155    GetIndexOfChildWithName (void *implementor, const char* child_name)
156    {
157        return UINT32_MAX;
158    }
159
160    virtual lldb::SBValue*
161    CastPyObjectToSBValue (void* data)
162    {
163        return NULL;
164    }
165
166    const char *
167    GetScriptInterpreterPtyName ();
168
169    int
170    GetMasterFileDescriptor ();
171
172	CommandInterpreter &
173	GetCommandInterpreter ();
174
175     static std::string
176    LanguageToString (lldb::ScriptLanguage language);
177
178    static void
179    InitializeInterpreter (SWIGInitCallback python_swig_init_callback,
180                           SWIGBreakpointCallbackFunction python_swig_breakpoint_callback,
181                           SWIGPythonTypeScriptCallbackFunction python_swig_typescript_callback,
182                           SWIGPythonCreateSyntheticProvider python_swig_synthetic_script,
183                           SWIGPythonCalculateNumChildren python_swig_calc_children,
184                           SWIGPythonGetChildAtIndex python_swig_get_child_index,
185                           SWIGPythonGetIndexOfChildWithName python_swig_get_index_child,
186                           SWIGPythonCastPyObjectToSBValue python_swig_cast_to_sbvalue);
187
188    static void
189    TerminateInterpreter ();
190
191    virtual void
192    ResetOutputFileHandle (FILE *new_fh) { } //By default, do nothing.
193
194protected:
195    CommandInterpreter &m_interpreter;
196    lldb::ScriptLanguage m_script_lang;
197
198    // Scripting languages may need to use stdin for their interactive loops;
199    // however we don't want them to grab the real system stdin because that
200    // resource needs to be shared among the debugger UI, the inferior process and these
201    // embedded scripting loops.  Therefore we need to set up a pseudoterminal and use that
202    // as stdin for the script interpreter interactive loops/prompts.
203
204    lldb_utility::PseudoTerminal m_interpreter_pty; // m_session_pty
205    std::string m_pty_slave_name;                   //m_session_pty_slave_name
206
207private:
208
209};
210
211} // namespace lldb_private
212
213#endif // #ifndef liblldb_ScriptInterpreter_h_
214