1359575Sdim%header %{
2359575Sdim
3359575Sdimtemplate <typename T>
4359575SdimPyObject *
5359575SdimSBTypeToSWIGWrapper (T* item);
6359575Sdim
7359575Sdimclass PyErr_Cleaner
8359575Sdim{
9359575Sdimpublic:
10359575Sdim    PyErr_Cleaner(bool print=false) :
11359575Sdim    m_print(print)
12359575Sdim    {
13359575Sdim    }
14359575Sdim
15359575Sdim    ~PyErr_Cleaner()
16359575Sdim    {
17359575Sdim        if (PyErr_Occurred())
18359575Sdim        {
19359575Sdim            if(m_print && !PyErr_ExceptionMatches(PyExc_SystemExit))
20359575Sdim                PyErr_Print();
21359575Sdim            PyErr_Clear();
22359575Sdim        }
23359575Sdim    }
24359575Sdim
25359575Sdimprivate:
26359575Sdim    bool m_print;
27359575Sdim};
28359575Sdim
29359575Sdim%}
30359575Sdim
31359575Sdim%wrapper %{
32359575Sdim
33359575Sdim// resolve a dotted Python name in the form
34359575Sdim// foo.bar.baz.Foobar to an actual Python object
35359575Sdim// if pmodule is NULL, the __main__ module will be used
36359575Sdim// as the starting point for the search
37359575Sdim
38359575Sdim
39359575Sdim// This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...)
40359575Sdim// and is used when a script command is attached to a breakpoint for execution.
41359575Sdim
42359575SdimSWIGEXPORT llvm::Expected<bool>
43359575SdimLLDBSwigPythonBreakpointCallbackFunction
44359575Sdim(
45359575Sdim    const char *python_function_name,
46359575Sdim    const char *session_dictionary_name,
47359575Sdim    const lldb::StackFrameSP& frame_sp,
48359575Sdim    const lldb::BreakpointLocationSP& bp_loc_sp,
49359575Sdim    lldb_private::StructuredDataImpl *args_impl
50359575Sdim)
51359575Sdim{
52359575Sdim    using namespace llvm;
53359575Sdim
54359575Sdim    lldb::SBFrame sb_frame (frame_sp);
55359575Sdim    lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
56359575Sdim
57359575Sdim    PyErr_Cleaner py_err_cleaner(true);
58359575Sdim    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
59359575Sdim    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
60359575Sdim
61359575Sdim    unsigned max_positional_args;
62359575Sdim    if (auto arg_info = pfunc.GetArgInfo())
63359575Sdim        max_positional_args = arg_info.get().max_positional_args;
64359575Sdim    else
65359575Sdim        return arg_info.takeError();
66359575Sdim
67359575Sdim    PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame));
68359575Sdim    PythonObject bp_loc_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_bp_loc));
69359575Sdim
70359575Sdim    auto result = [&] () -> Expected<PythonObject> {
71359575Sdim        // If the called function doesn't take extra_args, drop them here:
72359575Sdim        if (max_positional_args < 4) {
73359575Sdim            return pfunc.Call(frame_arg, bp_loc_arg, dict);
74359575Sdim        } else {
75359575Sdim            lldb::SBStructuredData *args_value = new lldb::SBStructuredData(args_impl);
76359575Sdim            PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(args_value));
77359575Sdim            return pfunc.Call(frame_arg, bp_loc_arg, args_arg, dict);
78359575Sdim        }
79359575Sdim    } ();
80359575Sdim
81359575Sdim    if (!result)
82359575Sdim        return result.takeError();
83359575Sdim
84359575Sdim    // Only False counts as false!
85359575Sdim    return result.get().get() != Py_False;
86359575Sdim}
87359575Sdim
88359575Sdim// This function is called by lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...)
89359575Sdim// and is used when a script command is attached to a watchpoint for execution.
90359575Sdim
91359575SdimSWIGEXPORT bool
92359575SdimLLDBSwigPythonWatchpointCallbackFunction
93359575Sdim(
94359575Sdim    const char *python_function_name,
95359575Sdim    const char *session_dictionary_name,
96359575Sdim    const lldb::StackFrameSP& frame_sp,
97359575Sdim    const lldb::WatchpointSP& wp_sp
98359575Sdim)
99359575Sdim{
100359575Sdim    lldb::SBFrame sb_frame (frame_sp);
101359575Sdim    lldb::SBWatchpoint sb_wp(wp_sp);
102359575Sdim
103359575Sdim    bool stop_at_watchpoint = true;
104359575Sdim
105359575Sdim    PyErr_Cleaner py_err_cleaner(true);
106359575Sdim
107359575Sdim    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
108359575Sdim    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
109359575Sdim
110359575Sdim    if (!pfunc.IsAllocated())
111359575Sdim        return stop_at_watchpoint;
112359575Sdim
113359575Sdim    PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame));
114359575Sdim    PythonObject wp_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_wp));
115359575Sdim    PythonObject result = pfunc(frame_arg, wp_arg, dict);
116359575Sdim
117359575Sdim    if (result.get() == Py_False)
118359575Sdim        stop_at_watchpoint = false;
119359575Sdim
120359575Sdim    return stop_at_watchpoint;
121359575Sdim}
122359575Sdim
123359575SdimSWIGEXPORT bool
124359575SdimLLDBSwigPythonCallTypeScript
125359575Sdim(
126359575Sdim    const char *python_function_name,
127359575Sdim    const void *session_dictionary,
128359575Sdim    const lldb::ValueObjectSP& valobj_sp,
129359575Sdim    void** pyfunct_wrapper,
130359575Sdim    const lldb::TypeSummaryOptionsSP& options_sp,
131359575Sdim    std::string& retval
132359575Sdim)
133359575Sdim{
134359575Sdim    lldb::SBValue sb_value (valobj_sp);
135359575Sdim    lldb::SBTypeSummaryOptions sb_options(options_sp.get());
136359575Sdim
137359575Sdim    retval.clear();
138359575Sdim
139359575Sdim    if (!python_function_name || !session_dictionary)
140359575Sdim        return false;
141359575Sdim
142359575Sdim    PyObject *pfunc_impl = nullptr;
143359575Sdim
144359575Sdim    if (pyfunct_wrapper && *pyfunct_wrapper && PyFunction_Check (*pyfunct_wrapper))
145359575Sdim    {
146359575Sdim        pfunc_impl = (PyObject*)(*pyfunct_wrapper);
147359575Sdim        if (pfunc_impl->ob_refcnt == 1)
148359575Sdim        {
149359575Sdim            Py_XDECREF(pfunc_impl);
150359575Sdim            pfunc_impl = NULL;
151359575Sdim        }
152359575Sdim    }
153359575Sdim
154359575Sdim    PyObject *py_dict = (PyObject*)session_dictionary;
155359575Sdim    if (!PythonDictionary::Check(py_dict))
156359575Sdim        return true;
157359575Sdim
158359575Sdim    PythonDictionary dict(PyRefType::Borrowed, py_dict);
159359575Sdim
160359575Sdim    PyErr_Cleaner pyerr_cleanup(true);  // show Python errors
161359575Sdim
162359575Sdim    PythonCallable pfunc(PyRefType::Borrowed, pfunc_impl);
163359575Sdim
164359575Sdim    if (!pfunc.IsAllocated())
165359575Sdim    {
166359575Sdim        pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
167359575Sdim        if (!pfunc.IsAllocated())
168359575Sdim            return false;
169359575Sdim
170359575Sdim        if (pyfunct_wrapper)
171359575Sdim        {
172359575Sdim            *pyfunct_wrapper = pfunc.get();
173359575Sdim            Py_XINCREF(pfunc.get());
174359575Sdim        }
175359575Sdim    }
176359575Sdim
177359575Sdim    PythonObject result;
178359575Sdim    auto argc = pfunc.GetArgInfo();
179359575Sdim    if (!argc) {
180359575Sdim        llvm::consumeError(argc.takeError());
181359575Sdim        return false;
182359575Sdim    }
183359575Sdim
184359575Sdim    PythonObject value_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_value));
185359575Sdim    PythonObject options_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_options));
186359575Sdim
187359575Sdim    if (argc.get().max_positional_args < 3)
188359575Sdim        result = pfunc(value_arg,dict);
189359575Sdim    else
190359575Sdim        result = pfunc(value_arg,dict,options_arg);
191359575Sdim
192359575Sdim    retval = result.Str().GetString().str();
193359575Sdim
194359575Sdim    return true;
195359575Sdim}
196359575Sdim
197359575SdimSWIGEXPORT void*
198359575SdimLLDBSwigPythonCreateSyntheticProvider
199359575Sdim(
200359575Sdim    const char *python_class_name,
201359575Sdim    const char *session_dictionary_name,
202359575Sdim    const lldb::ValueObjectSP& valobj_sp
203359575Sdim)
204359575Sdim{
205359575Sdim    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
206359575Sdim        Py_RETURN_NONE;
207359575Sdim
208359575Sdim    PyErr_Cleaner py_err_cleaner(true);
209359575Sdim
210359575Sdim    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
211359575Sdim    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name,dict);
212359575Sdim
213359575Sdim    if (!pfunc.IsAllocated())
214359575Sdim        Py_RETURN_NONE;
215359575Sdim
216359575Sdim    // I do not want the SBValue to be deallocated when going out of scope because python
217359575Sdim    // has ownership of it and will manage memory for this object by itself
218359575Sdim    lldb::SBValue *sb_value = new lldb::SBValue(valobj_sp);
219359575Sdim    sb_value->SetPreferSyntheticValue(false);
220359575Sdim
221359575Sdim    PythonObject val_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_value));
222359575Sdim    if (!val_arg.IsAllocated())
223359575Sdim        Py_RETURN_NONE;
224359575Sdim
225359575Sdim    PythonObject result = pfunc(val_arg, dict);
226359575Sdim
227359575Sdim    if (result.IsAllocated())
228359575Sdim        return result.release();
229359575Sdim
230359575Sdim    Py_RETURN_NONE;
231359575Sdim}
232359575Sdim
233359575SdimSWIGEXPORT void*
234359575SdimLLDBSwigPythonCreateCommandObject
235359575Sdim(
236359575Sdim    const char *python_class_name,
237359575Sdim    const char *session_dictionary_name,
238359575Sdim    const lldb::DebuggerSP debugger_sp
239359575Sdim)
240359575Sdim{
241359575Sdim    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
242359575Sdim        Py_RETURN_NONE;
243359575Sdim
244359575Sdim    PyErr_Cleaner py_err_cleaner(true);
245359575Sdim    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
246359575Sdim    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
247359575Sdim
248359575Sdim    if (!pfunc.IsAllocated())
249359575Sdim        return nullptr;
250359575Sdim
251359575Sdim    lldb::SBDebugger debugger_sb(debugger_sp);
252359575Sdim    PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
253359575Sdim    PythonObject result = pfunc(debugger_arg, dict);
254359575Sdim
255359575Sdim    if (result.IsAllocated())
256359575Sdim        return result.release();
257359575Sdim
258359575Sdim    Py_RETURN_NONE;
259359575Sdim}
260359575Sdim
261359575SdimSWIGEXPORT void*
262359575SdimLLDBSwigPythonCreateScriptedThreadPlan
263359575Sdim(
264359575Sdim    const char *python_class_name,
265359575Sdim    const char *session_dictionary_name,
266359575Sdim    lldb_private::StructuredDataImpl *args_impl,
267359575Sdim    std::string &error_string,
268359575Sdim    const lldb::ThreadPlanSP& thread_plan_sp
269359575Sdim)
270359575Sdim{
271359575Sdim    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
272359575Sdim        Py_RETURN_NONE;
273359575Sdim
274359575Sdim    // I do not want the SBThreadPlan to be deallocated when going out of scope because python
275359575Sdim    // has ownership of it and will manage memory for this object by itself
276359575Sdim    lldb::SBThreadPlan *tp_value = new lldb::SBThreadPlan(thread_plan_sp);
277359575Sdim
278359575Sdim    PyErr_Cleaner py_err_cleaner(true);
279359575Sdim
280359575Sdim    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
281359575Sdim    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
282359575Sdim
283359575Sdim    if (!pfunc.IsAllocated()) {
284359575Sdim        error_string.append("could not find script class: ");
285359575Sdim        error_string.append(python_class_name);
286359575Sdim        return nullptr;
287359575Sdim    }
288359575Sdim
289359575Sdim    PythonObject tp_arg(PyRefType::Owned, SBTypeToSWIGWrapper(tp_value));
290359575Sdim
291359575Sdim    if (!tp_arg.IsAllocated())
292359575Sdim        Py_RETURN_NONE;
293359575Sdim
294359575Sdim    llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
295359575Sdim    if (!arg_info) {
296359575Sdim        llvm::handleAllErrors(
297359575Sdim            arg_info.takeError(),
298359575Sdim            [&](PythonException &E) {
299359575Sdim                error_string.append(E.ReadBacktrace());
300359575Sdim            },
301359575Sdim            [&](const llvm::ErrorInfoBase &E) {
302359575Sdim                error_string.append(E.message());
303359575Sdim            });
304359575Sdim        Py_RETURN_NONE;
305359575Sdim    }
306359575Sdim
307359575Sdim    PythonObject result = {};
308359575Sdim    if (arg_info.get().max_positional_args == 2) {
309359575Sdim        if (args_impl != nullptr) {
310359575Sdim           error_string.assign("args passed, but __init__ does not take an args dictionary");
311359575Sdim           Py_RETURN_NONE;
312359575Sdim        }
313359575Sdim        result = pfunc(tp_arg, dict);
314359575Sdim    } else if (arg_info.get().max_positional_args >= 3) {
315359575Sdim        lldb::SBStructuredData *args_value = new lldb::SBStructuredData(args_impl);
316359575Sdim        PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(args_value));
317359575Sdim        result = pfunc(tp_arg, args_arg, dict);
318359575Sdim    } else {
319359575Sdim        error_string.assign("wrong number of arguments in __init__, should be 2 or 3 (not including self)");
320359575Sdim        Py_RETURN_NONE;
321359575Sdim    }
322359575Sdim
323359575Sdim    // FIXME: At this point we should check that the class we found supports all the methods
324359575Sdim    // that we need.
325359575Sdim
326359575Sdim    if (result.IsAllocated())
327359575Sdim        return result.release();
328359575Sdim    Py_RETURN_NONE;
329359575Sdim}
330359575Sdim
331359575SdimSWIGEXPORT bool
332359575SdimLLDBSWIGPythonCallThreadPlan
333359575Sdim(
334359575Sdim    void *implementor,
335359575Sdim    const char *method_name,
336359575Sdim    lldb_private::Event *event,
337359575Sdim    bool &got_error
338359575Sdim)
339359575Sdim{
340359575Sdim    got_error = false;
341359575Sdim
342359575Sdim    PyErr_Cleaner py_err_cleaner(false);
343359575Sdim    PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor));
344359575Sdim    auto pfunc = self.ResolveName<PythonCallable>(method_name);
345359575Sdim
346359575Sdim    if (!pfunc.IsAllocated())
347359575Sdim        return false;
348359575Sdim
349359575Sdim    PythonObject result;
350359575Sdim    if (event != nullptr)
351359575Sdim    {
352359575Sdim        lldb::SBEvent sb_event(event);
353359575Sdim        PythonObject event_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_event));
354359575Sdim        result = pfunc(event_arg);
355359575Sdim    }
356359575Sdim    else
357359575Sdim        result = pfunc();
358359575Sdim
359359575Sdim    if (PyErr_Occurred())
360359575Sdim    {
361359575Sdim        got_error = true;
362359575Sdim        printf ("Return value was neither false nor true for call to %s.\n", method_name);
363359575Sdim        PyErr_Print();
364359575Sdim        return false;
365359575Sdim    }
366359575Sdim
367359575Sdim    if (result.get() == Py_True)
368359575Sdim        return true;
369359575Sdim    else if (result.get() == Py_False)
370359575Sdim        return false;
371359575Sdim
372359575Sdim    // Somebody returned the wrong thing...
373359575Sdim    got_error = true;
374359575Sdim    printf ("Wrong return value type for call to %s.\n", method_name);
375359575Sdim    return false;
376359575Sdim}
377359575Sdim
378359575SdimSWIGEXPORT void *
379359575SdimLLDBSwigPythonCreateScriptedBreakpointResolver
380359575Sdim(
381359575Sdim    const char *python_class_name,
382359575Sdim    const char *session_dictionary_name,
383359575Sdim    lldb_private::StructuredDataImpl *args_impl,
384359575Sdim    lldb::BreakpointSP &breakpoint_sp
385359575Sdim)
386359575Sdim{
387359575Sdim    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
388359575Sdim        Py_RETURN_NONE;
389359575Sdim
390359575Sdim    PyErr_Cleaner py_err_cleaner(true);
391359575Sdim
392359575Sdim    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
393359575Sdim    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
394359575Sdim
395359575Sdim    if (!pfunc.IsAllocated())
396359575Sdim        return nullptr;
397359575Sdim
398359575Sdim    lldb::SBBreakpoint *bkpt_value = new lldb::SBBreakpoint(breakpoint_sp);
399359575Sdim
400359575Sdim    PythonObject bkpt_arg(PyRefType::Owned, SBTypeToSWIGWrapper(bkpt_value));
401359575Sdim
402359575Sdim    lldb::SBStructuredData *args_value = new lldb::SBStructuredData(args_impl);
403359575Sdim    PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(args_value));
404359575Sdim
405359575Sdim    PythonObject result = pfunc(bkpt_arg, args_arg, dict);
406359575Sdim    // FIXME: At this point we should check that the class we found supports all the methods
407359575Sdim    // that we need.
408359575Sdim
409359575Sdim    if (result.IsAllocated())
410359575Sdim    {
411359575Sdim        // Check that __callback__ is defined:
412359575Sdim        auto callback_func = result.ResolveName<PythonCallable>("__callback__");
413359575Sdim        if (callback_func.IsAllocated())
414359575Sdim            return result.release();
415359575Sdim        else
416359575Sdim            result.release();
417359575Sdim    }
418359575Sdim    Py_RETURN_NONE;
419359575Sdim}
420359575Sdim
421359575SdimSWIGEXPORT unsigned int
422359575SdimLLDBSwigPythonCallBreakpointResolver
423359575Sdim(
424359575Sdim    void *implementor,
425359575Sdim    const char *method_name,
426359575Sdim    lldb_private::SymbolContext *sym_ctx
427359575Sdim)
428359575Sdim{
429359575Sdim    PyErr_Cleaner py_err_cleaner(false);
430359575Sdim    PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor));
431359575Sdim    auto pfunc = self.ResolveName<PythonCallable>(method_name);
432359575Sdim
433359575Sdim    if (!pfunc.IsAllocated())
434359575Sdim        return 0;
435359575Sdim
436359575Sdim    PythonObject result;
437359575Sdim    if (sym_ctx != nullptr) {
438359575Sdim      lldb::SBSymbolContext sb_sym_ctx(sym_ctx);
439359575Sdim      PythonObject sym_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_sym_ctx));
440359575Sdim      result = pfunc(sym_ctx_arg);
441359575Sdim    } else
442359575Sdim      result = pfunc();
443359575Sdim
444359575Sdim    if (PyErr_Occurred())
445359575Sdim    {
446359575Sdim        PyErr_Print();
447359575Sdim        return 0;
448359575Sdim    }
449359575Sdim
450359575Sdim    // The callback will return a bool, but we're need to also return ints
451359575Sdim    // so we're squirrelling the bool through as an int...  And if you return
452359575Sdim    // nothing, we'll continue.
453359575Sdim    if (strcmp(method_name, "__callback__") == 0) {
454359575Sdim        if (result.get() == Py_False)
455359575Sdim          return 0;
456359575Sdim        else
457359575Sdim          return 1;
458359575Sdim    }
459359575Sdim
460359575Sdim    PythonInteger int_result = result.AsType<PythonInteger>();
461359575Sdim    if (!int_result.IsAllocated())
462359575Sdim        return 0;
463359575Sdim
464359575Sdim    unsigned int ret_val = int_result.GetInteger();
465359575Sdim
466359575Sdim    return ret_val;
467359575Sdim}
468359575Sdim
469359575Sdim// wrapper that calls an optional instance member of an object taking no arguments
470359575Sdimstatic PyObject*
471359575SdimLLDBSwigPython_CallOptionalMember
472359575Sdim(
473359575Sdim    PyObject* implementor,
474359575Sdim    char* callee_name,
475359575Sdim    PyObject* ret_if_not_found = Py_None,
476359575Sdim    bool* was_found = NULL
477359575Sdim)
478359575Sdim{
479359575Sdim    PyErr_Cleaner py_err_cleaner(false);
480359575Sdim
481359575Sdim    PythonObject self(PyRefType::Borrowed, static_cast<PyObject*>(implementor));
482359575Sdim    auto pfunc = self.ResolveName<PythonCallable>(callee_name);
483359575Sdim
484359575Sdim    if (!pfunc.IsAllocated())
485359575Sdim    {
486359575Sdim        if (was_found)
487359575Sdim            *was_found = false;
488359575Sdim        Py_XINCREF(ret_if_not_found);
489359575Sdim        return ret_if_not_found;
490359575Sdim    }
491359575Sdim
492359575Sdim    if (was_found)
493359575Sdim        *was_found = true;
494359575Sdim
495359575Sdim    PythonObject result = pfunc();
496359575Sdim    return result.release();
497359575Sdim}
498359575Sdim
499359575SdimSWIGEXPORT size_t
500359575SdimLLDBSwigPython_CalculateNumChildren
501359575Sdim(
502359575Sdim    PyObject *implementor,
503359575Sdim    uint32_t max
504359575Sdim)
505359575Sdim{
506359575Sdim    PythonObject self(PyRefType::Borrowed, implementor);
507359575Sdim    auto pfunc = self.ResolveName<PythonCallable>("num_children");
508359575Sdim
509359575Sdim    if (!pfunc.IsAllocated())
510359575Sdim        return 0;
511359575Sdim
512359575Sdim    auto arg_info = pfunc.GetArgInfo();
513359575Sdim    if (!arg_info) {
514359575Sdim        llvm::consumeError(arg_info.takeError());
515359575Sdim        return 0;
516359575Sdim    }
517359575Sdim
518359575Sdim    PythonObject result;
519359575Sdim
520359575Sdim    if (arg_info.get().max_positional_args < 1)
521359575Sdim        result = pfunc();
522359575Sdim    else
523359575Sdim        result = pfunc(PythonInteger(max));
524359575Sdim
525359575Sdim    if (!result.IsAllocated())
526359575Sdim        return 0;
527359575Sdim
528359575Sdim    PythonInteger int_result = result.AsType<PythonInteger>();
529359575Sdim    if (!int_result.IsAllocated())
530359575Sdim        return 0;
531359575Sdim
532359575Sdim    size_t ret_val = int_result.GetInteger();
533359575Sdim
534359575Sdim    if (PyErr_Occurred()) //FIXME use Expected to catch python exceptions
535359575Sdim    {
536359575Sdim        PyErr_Print();
537359575Sdim        PyErr_Clear();
538359575Sdim    }
539359575Sdim
540359575Sdim    if (arg_info.get().max_positional_args < 1)
541359575Sdim        ret_val = std::min(ret_val, static_cast<size_t>(max));
542359575Sdim
543359575Sdim    return ret_val;
544359575Sdim}
545359575Sdim
546359575SdimSWIGEXPORT PyObject*
547359575SdimLLDBSwigPython_GetChildAtIndex
548359575Sdim(
549359575Sdim    PyObject *implementor,
550359575Sdim    uint32_t idx
551359575Sdim)
552359575Sdim{
553359575Sdim    PyErr_Cleaner py_err_cleaner(true);
554359575Sdim
555359575Sdim    PythonObject self(PyRefType::Borrowed, implementor);
556359575Sdim    auto pfunc = self.ResolveName<PythonCallable>("get_child_at_index");
557359575Sdim
558359575Sdim    if (!pfunc.IsAllocated())
559359575Sdim        return nullptr;
560359575Sdim
561359575Sdim    PythonObject result = pfunc(PythonInteger(idx));
562359575Sdim
563359575Sdim    if (!result.IsAllocated())
564359575Sdim        return nullptr;
565359575Sdim
566359575Sdim    lldb::SBValue* sbvalue_ptr = nullptr;
567359575Sdim    if (SWIG_ConvertPtr(result.get(), (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
568359575Sdim        return nullptr;
569359575Sdim
570359575Sdim    if (sbvalue_ptr == nullptr)
571359575Sdim        return nullptr;
572359575Sdim
573359575Sdim    return result.release();
574359575Sdim}
575359575Sdim
576359575SdimSWIGEXPORT int
577359575SdimLLDBSwigPython_GetIndexOfChildWithName
578359575Sdim(
579359575Sdim    PyObject *implementor,
580359575Sdim    const char* child_name
581359575Sdim)
582359575Sdim{
583359575Sdim    PyErr_Cleaner py_err_cleaner(true);
584359575Sdim
585359575Sdim    PythonObject self(PyRefType::Borrowed, implementor);
586359575Sdim    auto pfunc = self.ResolveName<PythonCallable>("get_child_index");
587359575Sdim
588359575Sdim    if (!pfunc.IsAllocated())
589359575Sdim        return UINT32_MAX;
590359575Sdim
591359575Sdim    PythonObject result = pfunc(PythonString(child_name));
592359575Sdim
593359575Sdim    if (!result.IsAllocated())
594359575Sdim        return UINT32_MAX;
595359575Sdim
596359575Sdim    PythonInteger int_result = result.AsType<PythonInteger>();
597359575Sdim    if (!int_result.IsAllocated())
598359575Sdim        return UINT32_MAX;
599359575Sdim
600359575Sdim    int64_t retval = int_result.GetInteger();
601359575Sdim    if (retval >= 0)
602359575Sdim        return (uint32_t)retval;
603359575Sdim
604359575Sdim    return UINT32_MAX;
605359575Sdim}
606359575Sdim
607359575SdimSWIGEXPORT bool
608359575SdimLLDBSwigPython_UpdateSynthProviderInstance
609359575Sdim(
610359575Sdim    PyObject *implementor
611359575Sdim)
612359575Sdim{
613359575Sdim    bool ret_val = false;
614359575Sdim
615359575Sdim    static char callee_name[] = "update";
616359575Sdim
617359575Sdim    PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name);
618359575Sdim
619359575Sdim    if (py_return == Py_True)
620359575Sdim        ret_val = true;
621359575Sdim
622359575Sdim    Py_XDECREF(py_return);
623359575Sdim
624359575Sdim    return ret_val;
625359575Sdim}
626359575Sdim
627359575SdimSWIGEXPORT bool
628359575SdimLLDBSwigPython_MightHaveChildrenSynthProviderInstance
629359575Sdim(
630359575Sdim    PyObject *implementor
631359575Sdim)
632359575Sdim{
633359575Sdim    bool ret_val = false;
634359575Sdim
635359575Sdim    static char callee_name[] = "has_children";
636359575Sdim
637359575Sdim    PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True);
638359575Sdim
639359575Sdim    if (py_return == Py_True)
640359575Sdim        ret_val = true;
641359575Sdim
642359575Sdim    Py_XDECREF(py_return);
643359575Sdim
644359575Sdim    return ret_val;
645359575Sdim}
646359575Sdim
647359575SdimSWIGEXPORT PyObject*
648359575SdimLLDBSwigPython_GetValueSynthProviderInstance
649359575Sdim(
650359575Sdim    PyObject *implementor
651359575Sdim)
652359575Sdim{
653359575Sdim    PyObject* ret_val = nullptr;
654359575Sdim
655359575Sdim    static char callee_name[] = "get_value";
656359575Sdim
657359575Sdim    PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_None);
658359575Sdim
659359575Sdim    if (py_return == Py_None || py_return == nullptr)
660359575Sdim        ret_val = nullptr;
661359575Sdim
662359575Sdim    lldb::SBValue* sbvalue_ptr = NULL;
663359575Sdim
664359575Sdim    if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
665359575Sdim        ret_val = nullptr;
666359575Sdim    else if (sbvalue_ptr == NULL)
667359575Sdim        ret_val = nullptr;
668359575Sdim    else
669359575Sdim        ret_val = py_return;
670359575Sdim
671359575Sdim    Py_XDECREF(py_return);
672359575Sdim    return ret_val;
673359575Sdim}
674359575Sdim
675359575SdimSWIGEXPORT void*
676359575SdimLLDBSWIGPython_CastPyObjectToSBValue
677359575Sdim(
678359575Sdim    PyObject* data
679359575Sdim)
680359575Sdim{
681359575Sdim    lldb::SBValue* sb_ptr = NULL;
682359575Sdim
683359575Sdim    int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
684359575Sdim
685359575Sdim    if (valid_cast == -1)
686359575Sdim        return NULL;
687359575Sdim
688359575Sdim    return sb_ptr;
689359575Sdim}
690359575Sdim
691359575SdimSWIGEXPORT bool
692359575SdimLLDBSwigPythonCallCommand
693359575Sdim(
694359575Sdim    const char *python_function_name,
695359575Sdim    const char *session_dictionary_name,
696359575Sdim    lldb::DebuggerSP& debugger,
697359575Sdim    const char* args,
698359575Sdim    lldb_private::CommandReturnObject& cmd_retobj,
699359575Sdim    lldb::ExecutionContextRefSP exe_ctx_ref_sp
700359575Sdim)
701359575Sdim{
702359575Sdim    lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj);
703359575Sdim    lldb::SBDebugger debugger_sb(debugger);
704359575Sdim    lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
705359575Sdim
706359575Sdim    PyErr_Cleaner py_err_cleaner(true);
707359575Sdim    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
708359575Sdim    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
709359575Sdim
710359575Sdim    if (!pfunc.IsAllocated())
711359575Sdim        return false;
712359575Sdim
713359575Sdim    // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you
714359575Sdim    // see comment above for SBCommandReturnObjectReleaser for further details
715359575Sdim    auto argc = pfunc.GetArgInfo();
716359575Sdim    if (!argc) {
717359575Sdim        llvm::consumeError(argc.takeError());
718359575Sdim        return false;
719359575Sdim    }
720359575Sdim    PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
721359575Sdim    PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb));
722359575Sdim    PythonObject cmd_retobj_arg(PyRefType::Owned, SBTypeToSWIGWrapper(&cmd_retobj_sb));
723359575Sdim
724359575Sdim    if (argc.get().max_positional_args < 5u)
725359575Sdim        pfunc(debugger_arg, PythonString(args), cmd_retobj_arg, dict);
726359575Sdim    else
727359575Sdim        pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg, dict);
728359575Sdim
729359575Sdim    return true;
730359575Sdim}
731359575Sdim
732359575SdimSWIGEXPORT bool
733359575SdimLLDBSwigPythonCallCommandObject
734359575Sdim(
735359575Sdim    PyObject *implementor,
736359575Sdim    lldb::DebuggerSP& debugger,
737359575Sdim    const char* args,
738359575Sdim    lldb_private::CommandReturnObject& cmd_retobj,
739359575Sdim    lldb::ExecutionContextRefSP exe_ctx_ref_sp
740359575Sdim)
741359575Sdim{
742359575Sdim    lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj);
743359575Sdim    lldb::SBDebugger debugger_sb(debugger);
744359575Sdim    lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
745359575Sdim
746359575Sdim    PyErr_Cleaner py_err_cleaner(true);
747359575Sdim
748359575Sdim    PythonObject self(PyRefType::Borrowed, implementor);
749359575Sdim    auto pfunc = self.ResolveName<PythonCallable>("__call__");
750359575Sdim
751359575Sdim    if (!pfunc.IsAllocated())
752359575Sdim        return false;
753359575Sdim
754359575Sdim    // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you
755359575Sdim    // see comment above for SBCommandReturnObjectReleaser for further details
756359575Sdim    PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
757359575Sdim    PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb));
758359575Sdim    PythonObject cmd_retobj_arg(PyRefType::Owned, SBTypeToSWIGWrapper(&cmd_retobj_sb));
759359575Sdim
760359575Sdim    pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg);
761359575Sdim
762359575Sdim    return true;
763359575Sdim}
764359575Sdim
765359575SdimSWIGEXPORT void*
766359575SdimLLDBSWIGPythonCreateOSPlugin
767359575Sdim(
768359575Sdim    const char *python_class_name,
769359575Sdim    const char *session_dictionary_name,
770359575Sdim    const lldb::ProcessSP& process_sp
771359575Sdim)
772359575Sdim{
773359575Sdim    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
774359575Sdim        Py_RETURN_NONE;
775359575Sdim
776359575Sdim    PyErr_Cleaner py_err_cleaner(true);
777359575Sdim
778359575Sdim    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
779359575Sdim    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
780359575Sdim
781359575Sdim    if (!pfunc.IsAllocated())
782359575Sdim        Py_RETURN_NONE;
783359575Sdim
784359575Sdim    // I do not want the SBProcess to be deallocated when going out of scope because python
785359575Sdim    // has ownership of it and will manage memory for this object by itself
786359575Sdim    lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp);
787359575Sdim    PythonObject process_arg(PyRefType::Owned, SBTypeToSWIGWrapper(process_sb));
788359575Sdim    if (!process_arg.IsAllocated())
789359575Sdim        Py_RETURN_NONE;
790359575Sdim
791359575Sdim    auto result = pfunc(process_arg);
792359575Sdim
793359575Sdim    if (result.IsAllocated())
794359575Sdim        return result.release();
795359575Sdim
796359575Sdim    Py_RETURN_NONE;
797359575Sdim}
798359575Sdim
799359575SdimSWIGEXPORT void*
800359575SdimLLDBSWIGPython_CreateFrameRecognizer
801359575Sdim(
802359575Sdim    const char *python_class_name,
803359575Sdim    const char *session_dictionary_name
804359575Sdim)
805359575Sdim{
806359575Sdim    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
807359575Sdim        Py_RETURN_NONE;
808359575Sdim
809359575Sdim    PyErr_Cleaner py_err_cleaner(true);
810359575Sdim
811359575Sdim    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
812359575Sdim    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_class_name, dict);
813359575Sdim
814359575Sdim    if (!pfunc.IsAllocated())
815359575Sdim        Py_RETURN_NONE;
816359575Sdim
817359575Sdim    auto result = pfunc();
818359575Sdim
819359575Sdim    if (result.IsAllocated())
820359575Sdim        return result.release();
821359575Sdim
822359575Sdim    Py_RETURN_NONE;
823359575Sdim}
824359575Sdim
825359575SdimSWIGEXPORT PyObject*
826359575SdimLLDBSwigPython_GetRecognizedArguments
827359575Sdim(
828359575Sdim    PyObject *implementor,
829359575Sdim    const lldb::StackFrameSP& frame_sp
830359575Sdim)
831359575Sdim{
832359575Sdim    static char callee_name[] = "get_recognized_arguments";
833359575Sdim
834359575Sdim    lldb::SBFrame frame_sb(frame_sp);
835359575Sdim    PyObject *arg = SBTypeToSWIGWrapper(frame_sb);
836359575Sdim
837359575Sdim    PythonString str(callee_name);
838359575Sdim    PyObject* result = PyObject_CallMethodObjArgs(implementor, str.get(), arg,
839359575Sdim                                                  NULL);
840359575Sdim    return result;
841359575Sdim}
842359575Sdim
843359575SdimSWIGEXPORT void*
844359575SdimLLDBSWIGPython_GetDynamicSetting (void* module, const char* setting, const lldb::TargetSP& target_sp)
845359575Sdim{
846359575Sdim    if (!module || !setting)
847359575Sdim        Py_RETURN_NONE;
848359575Sdim
849359575Sdim    PyErr_Cleaner py_err_cleaner(true);
850359575Sdim    PythonObject py_module(PyRefType::Borrowed, (PyObject *)module);
851359575Sdim    auto pfunc = py_module.ResolveName<PythonCallable>("get_dynamic_setting");
852359575Sdim
853359575Sdim    if (!pfunc.IsAllocated())
854359575Sdim        Py_RETURN_NONE;
855359575Sdim
856359575Sdim    lldb::SBTarget target_sb(target_sp);
857359575Sdim    PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(target_sb));
858359575Sdim    auto result = pfunc(target_arg, PythonString(setting));
859359575Sdim
860359575Sdim    return result.release();
861359575Sdim}
862359575Sdim
863359575SdimSWIGEXPORT bool
864359575SdimLLDBSWIGPythonRunScriptKeywordProcess
865359575Sdim(const char* python_function_name,
866359575Sdimconst char* session_dictionary_name,
867359575Sdimlldb::ProcessSP& process,
868359575Sdimstd::string& output)
869359575Sdim
870359575Sdim{
871359575Sdim    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
872359575Sdim        return false;
873359575Sdim
874359575Sdim    PyErr_Cleaner py_err_cleaner(true);
875359575Sdim
876359575Sdim    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
877359575Sdim    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
878359575Sdim
879359575Sdim    if (!pfunc.IsAllocated())
880359575Sdim        return false;
881359575Sdim
882359575Sdim    lldb::SBProcess process_sb(process);
883359575Sdim    PythonObject process_arg(PyRefType::Owned, SBTypeToSWIGWrapper(process_sb));
884359575Sdim    auto result = pfunc(process_arg, dict);
885359575Sdim
886359575Sdim    output = result.Str().GetString().str();
887359575Sdim
888359575Sdim    return true;
889359575Sdim}
890359575Sdim
891359575SdimSWIGEXPORT bool
892359575SdimLLDBSWIGPythonRunScriptKeywordThread
893359575Sdim(const char* python_function_name,
894359575Sdimconst char* session_dictionary_name,
895359575Sdimlldb::ThreadSP& thread,
896359575Sdimstd::string& output)
897359575Sdim
898359575Sdim{
899359575Sdim    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
900359575Sdim        return false;
901359575Sdim
902359575Sdim    PyErr_Cleaner py_err_cleaner(true);
903359575Sdim
904359575Sdim    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
905359575Sdim    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
906359575Sdim
907359575Sdim    if (!pfunc.IsAllocated())
908359575Sdim        return false;
909359575Sdim
910359575Sdim    lldb::SBThread thread_sb(thread);
911359575Sdim    PythonObject thread_arg(PyRefType::Owned, SBTypeToSWIGWrapper(thread_sb));
912359575Sdim    auto result = pfunc(thread_arg, dict);
913359575Sdim
914359575Sdim    output = result.Str().GetString().str();
915359575Sdim
916359575Sdim    return true;
917359575Sdim}
918359575Sdim
919359575SdimSWIGEXPORT bool
920359575SdimLLDBSWIGPythonRunScriptKeywordTarget
921359575Sdim(const char* python_function_name,
922359575Sdimconst char* session_dictionary_name,
923359575Sdimlldb::TargetSP& target,
924359575Sdimstd::string& output)
925359575Sdim
926359575Sdim{
927359575Sdim    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
928359575Sdim        return false;
929359575Sdim
930359575Sdim    PyErr_Cleaner py_err_cleaner(true);
931359575Sdim
932359575Sdim    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
933359575Sdim    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name,dict);
934359575Sdim
935359575Sdim    if (!pfunc.IsAllocated())
936359575Sdim        return false;
937359575Sdim
938359575Sdim    lldb::SBTarget target_sb(target);
939359575Sdim    PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(target_sb));
940359575Sdim    auto result = pfunc(target_arg, dict);
941359575Sdim
942359575Sdim    output = result.Str().GetString().str();
943359575Sdim
944359575Sdim    return true;
945359575Sdim}
946359575Sdim
947359575SdimSWIGEXPORT bool
948359575SdimLLDBSWIGPythonRunScriptKeywordFrame
949359575Sdim(const char* python_function_name,
950359575Sdimconst char* session_dictionary_name,
951359575Sdimlldb::StackFrameSP& frame,
952359575Sdimstd::string& output)
953359575Sdim
954359575Sdim{
955359575Sdim    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
956359575Sdim        return false;
957359575Sdim
958359575Sdim    PyErr_Cleaner py_err_cleaner(true);
959359575Sdim
960359575Sdim    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
961359575Sdim    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name,dict);
962359575Sdim
963359575Sdim    if (!pfunc.IsAllocated())
964359575Sdim        return false;
965359575Sdim
966359575Sdim    lldb::SBFrame frame_sb(frame);
967359575Sdim    PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(frame_sb));
968359575Sdim    auto result = pfunc(frame_arg, dict);
969359575Sdim
970359575Sdim    output = result.Str().GetString().str();
971359575Sdim
972359575Sdim    return true;
973359575Sdim}
974359575Sdim
975359575SdimSWIGEXPORT bool
976359575SdimLLDBSWIGPythonRunScriptKeywordValue
977359575Sdim(const char* python_function_name,
978359575Sdimconst char* session_dictionary_name,
979359575Sdimlldb::ValueObjectSP& value,
980359575Sdimstd::string& output)
981359575Sdim
982359575Sdim{
983359575Sdim    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
984359575Sdim        return false;
985359575Sdim
986359575Sdim    PyErr_Cleaner py_err_cleaner(true);
987359575Sdim
988359575Sdim    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
989359575Sdim    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
990359575Sdim
991359575Sdim    if (!pfunc.IsAllocated())
992359575Sdim        return false;
993359575Sdim
994359575Sdim    lldb::SBValue value_sb(value);
995359575Sdim    PythonObject value_arg(PyRefType::Owned, SBTypeToSWIGWrapper(value_sb));
996359575Sdim    auto result = pfunc(value_arg, dict);
997359575Sdim
998359575Sdim    output = result.Str().GetString().str();
999359575Sdim
1000359575Sdim    return true;
1001359575Sdim}
1002359575Sdim
1003359575SdimSWIGEXPORT bool
1004359575SdimLLDBSwigPythonCallModuleInit
1005359575Sdim(
1006359575Sdim    const char *python_module_name,
1007359575Sdim    const char *session_dictionary_name,
1008359575Sdim    lldb::DebuggerSP& debugger
1009359575Sdim)
1010359575Sdim{
1011359575Sdim    std::string python_function_name_string = python_module_name;
1012359575Sdim    python_function_name_string += ".__lldb_init_module";
1013359575Sdim    const char* python_function_name = python_function_name_string.c_str();
1014359575Sdim
1015359575Sdim    PyErr_Cleaner py_err_cleaner(true);
1016359575Sdim
1017359575Sdim    auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(session_dictionary_name);
1018359575Sdim    auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(python_function_name, dict);
1019359575Sdim
1020359575Sdim    // This method is optional and need not exist.  So if we don't find it,
1021359575Sdim    // it's actually a success, not a failure.
1022359575Sdim    if (!pfunc.IsAllocated())
1023359575Sdim        return true;
1024359575Sdim
1025359575Sdim    lldb::SBDebugger debugger_sb(debugger);
1026359575Sdim    PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
1027359575Sdim    pfunc(debugger_arg, dict);
1028359575Sdim
1029359575Sdim    return true;
1030359575Sdim}
1031359575Sdim%}
1032359575Sdim
1033359575Sdim
1034359575Sdim%runtime %{
1035359575Sdim// Forward declaration to be inserted at the start of LLDBWrapPython.h
1036359575Sdim#include "lldb/API/SBDebugger.h"
1037359575Sdim#include "lldb/API/SBValue.h"
1038359575Sdim
1039359575SdimSWIGEXPORT lldb::ValueObjectSP
1040359575SdimLLDBSWIGPython_GetValueObjectSPFromSBValue (void* data)
1041359575Sdim{
1042359575Sdim    lldb::ValueObjectSP valobj_sp;
1043359575Sdim    if (data)
1044359575Sdim    {
1045359575Sdim        lldb::SBValue* sb_ptr = (lldb::SBValue *)data;
1046359575Sdim        valobj_sp = sb_ptr->GetSP();
1047359575Sdim    }
1048359575Sdim    return valobj_sp;
1049359575Sdim}
1050359575Sdim
1051359575Sdim#ifdef __cplusplus
1052359575Sdimextern "C" {
1053359575Sdim#endif
1054359575Sdim
1055359575Sdimvoid LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton);
1056359575Sdim
1057359575Sdim#ifdef __cplusplus
1058359575Sdim}
1059359575Sdim#endif
1060359575Sdim%}
1061359575Sdim
1062359575Sdim%wrapper %{
1063359575Sdim
1064359575Sdim
1065359575Sdim// For the LogOutputCallback functions
1066359575Sdimvoid LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) {
1067359575Sdim    if (baton != Py_None) {
1068359575Sdim      SWIG_PYTHON_THREAD_BEGIN_BLOCK;
1069359575Sdim      PyObject *result = PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str);
1070359575Sdim	  Py_XDECREF(result);
1071359575Sdim      SWIG_PYTHON_THREAD_END_BLOCK;
1072359575Sdim    }
1073359575Sdim}
1074359575Sdim%}
1075