[LLDBSwigPythonCallTypeScript] Remove redundant call to type summary func.
[lldb.git] / lldb / scripts / Python / python-wrapper.swig
1 %header %{
2
3 template <typename T>
4 PyObject *
5 SBTypeToSWIGWrapper (T* item);
6
7 class PyErr_Cleaner
8 {
9 public:
10     PyErr_Cleaner(bool print=false) :
11     m_print(print)
12     {
13     }
14
15     ~PyErr_Cleaner()
16     {
17         if (PyErr_Occurred())
18         {
19             if(m_print && !PyErr_ExceptionMatches(PyExc_SystemExit))
20                 PyErr_Print();
21             PyErr_Clear();
22         }
23     }
24
25 private:
26     bool m_print;
27 };
28
29 static PyObject*
30 ResolvePythonName(const char* name,
31                   PyObject* pmodule)
32 {
33     if (!name)
34         return pmodule;
35
36     PyErr_Cleaner pyerr_cleanup(true);  // show Python errors
37
38     PyObject* main_dict;
39
40     if (!pmodule)
41     {
42         pmodule = PyImport_AddModule ("__main__");
43         if (!pmodule)
44             return NULL;
45     }
46
47     if (PyType_Check(pmodule))
48     {
49         main_dict = ((PyTypeObject*)pmodule)->tp_dict;
50         if (!main_dict)
51             return NULL;
52     }
53     else if (!PyDict_Check(pmodule))
54     {
55         main_dict = PyModule_GetDict (pmodule);
56         if (!main_dict)
57             return NULL;
58     }
59     else
60         main_dict = pmodule;
61
62     const char* dot_pos = ::strchr(name, '.');
63
64     PyObject *dest_object;
65     PyObject *key, *value;
66     Py_ssize_t pos = 0;
67
68     if (!dot_pos)
69     {
70         dest_object = NULL;
71         while (PyDict_Next (main_dict, &pos, &key, &value))
72         {
73             // We have stolen references to the key and value objects in the dictionary; we need to increment
74             // them now so that Python's garbage collector doesn't collect them out from under us.
75             Py_INCREF (key);
76             Py_INCREF (value);
77             if (strcmp (PyString_AsString (key), name) == 0)
78             {
79                 dest_object = value;
80                 break;
81             }
82         }
83         if (!dest_object || dest_object == Py_None)
84             return NULL;
85         return dest_object;
86     }
87     else
88     {
89         size_t len = dot_pos - name;
90         std::string piece(name,len);
91         pmodule = ResolvePythonName(piece.c_str(), main_dict);
92         if (!pmodule)
93             return NULL;
94         return ResolvePythonName(dot_pos+1,pmodule); // tail recursion.. should be optimized by the compiler
95     }
96 }
97
98 static PyObject*
99 FindSessionDictionary(const char *session_dictionary_name)
100 {
101     return ResolvePythonName(session_dictionary_name, NULL);
102 }
103
104 class PyCallable
105 {
106 public:
107     struct argc {
108         size_t num_args;
109         bool varargs : 1;
110         bool kwargs : 1;
111     };
112
113     argc
114     GetNumArguments ()
115     {
116         if (m_callable && PyFunction_Check(m_callable))
117         {
118             PyCodeObject* code = (PyCodeObject*)PyFunction_GET_CODE(m_callable);
119             if (code)
120             {
121                 size_t args = code->co_argcount;
122                 bool va=false,kw=false;
123                 if ((code->co_flags & 4) == 4)
124                     va = true;
125                 if ((code->co_flags & 8) == 8)
126                     kw = true;
127                 return {args,va,kw};
128             }
129         }
130         return {SIZE_MAX,false,false};
131     }
132
133     operator
134     bool ()
135     {
136         return m_callable != NULL;
137     }
138
139     template<typename ...Args>
140     PyObject*
141     operator () (Args... args)
142     {
143         return (*this)({SBTypeToSWIGWrapper(args)...});
144     }
145
146     PyObject*
147     operator () (std::initializer_list<PyObject*> args)
148     {
149         PyObject* retval = NULL;
150         PyObject* pargs = PyTuple_New (args.size());
151         if (pargs == NULL)
152         {
153             if (PyErr_Occurred())
154                 PyErr_Clear();
155             return retval;
156         }
157         size_t idx = 0;
158         for (auto arg : args)
159         {
160             if (!arg)
161                 return retval;
162             Py_INCREF(arg); // _SetItem steals a reference
163             PyTuple_SetItem(pargs,idx,arg);
164             idx++;
165         }
166         retval = PyObject_CallObject (m_callable, pargs);
167         Py_XDECREF (pargs);
168         return retval;
169     }
170
171     static PyCallable
172     FindWithPythonObject (PyObject* pfunc)
173     {
174         return PyCallable(pfunc);
175     }
176
177     static PyCallable
178     FindWithFunctionName (const char *python_function_name,
179                           const char *session_dictionary_name)
180     {
181         if (!python_function_name || !session_dictionary_name)
182             return PyCallable();
183         if ( (python_function_name[0] == 0) || (session_dictionary_name[0] == 0) )
184             return PyCallable();
185         return FindWithFunctionName(python_function_name,FindSessionDictionary (session_dictionary_name));
186     }
187
188     static PyCallable
189     FindWithFunctionName (const char *python_function_name,
190                           PyObject *session_dict)
191     {
192         if (!python_function_name || !session_dict)
193             return PyCallable();
194         if ( (python_function_name[0] == 0))
195             return PyCallable();
196         return PyCallable(ResolvePythonName (python_function_name, session_dict));
197     }
198
199     static PyCallable
200     FindWithMemberFunction (PyObject *self,
201                             const char *python_function_name)
202     {
203         if (self == NULL || self == Py_None)
204             return PyCallable();
205         if (!python_function_name || (python_function_name[0] == 0))
206             return PyCallable();
207         return PyCallable(PyObject_GetAttrString(self, python_function_name));
208     }
209
210 private:
211     PyObject* m_callable;
212
213     PyCallable (PyObject *callable = NULL) :
214     m_callable(callable)
215     {
216         if (m_callable && PyCallable_Check(m_callable) == false)
217             m_callable = NULL;
218     }
219 };
220
221 %}
222
223 %wrapper %{
224
225 // resolve a dotted Python name in the form
226 // foo.bar.baz.Foobar to an actual Python object
227 // if pmodule is NULL, the __main__ module will be used
228 // as the starting point for the search
229
230
231 // This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...)
232 // and is used when a script command is attached to a breakpoint for execution.
233
234 SWIGEXPORT bool
235 LLDBSwigPythonBreakpointCallbackFunction
236 (
237     const char *python_function_name,
238     const char *session_dictionary_name,
239     const lldb::StackFrameSP& frame_sp,
240     const lldb::BreakpointLocationSP& bp_loc_sp
241 )
242 {
243     lldb::SBFrame sb_frame (frame_sp);
244     lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
245
246     bool stop_at_breakpoint = true;
247
248     {
249         PyErr_Cleaner py_err_cleaner(true);
250
251         PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
252
253         if (!pfunc)
254             return stop_at_breakpoint;
255
256         PyObject* session_dict = NULL;
257         PyObject* pvalue = NULL;
258         pvalue = pfunc(sb_frame, sb_bp_loc, session_dict = FindSessionDictionary(session_dictionary_name));
259
260         Py_XINCREF (session_dict);
261
262         if (pvalue == Py_False)
263             stop_at_breakpoint = false;
264
265         Py_XDECREF (pvalue);
266     }
267
268     return stop_at_breakpoint;
269 }
270
271 // This function is called by lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...)
272 // and is used when a script command is attached to a watchpoint for execution.
273
274 SWIGEXPORT bool
275 LLDBSwigPythonWatchpointCallbackFunction
276 (
277     const char *python_function_name,
278     const char *session_dictionary_name,
279     const lldb::StackFrameSP& frame_sp,
280     const lldb::WatchpointSP& wp_sp
281 )
282 {
283     lldb::SBFrame sb_frame (frame_sp);
284     lldb::SBWatchpoint sb_wp(wp_sp);
285
286     bool stop_at_watchpoint = true;
287
288     {
289         PyErr_Cleaner py_err_cleaner(true);
290
291         PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
292
293         if (!pfunc)
294             return stop_at_watchpoint;
295
296         PyObject* session_dict = NULL;
297         PyObject* pvalue = NULL;
298         pvalue = pfunc(sb_frame, sb_wp, session_dict = FindSessionDictionary(session_dictionary_name));
299
300         Py_XINCREF (session_dict);
301
302         if (pvalue == Py_False)
303             stop_at_watchpoint = false;
304
305         Py_XDECREF (pvalue);
306     }
307
308     return stop_at_watchpoint;
309 }
310
311 bool
312 PyObjectToString (PyObject* object,
313                   std::string& retval)
314 {
315     retval.clear();
316     bool was_ok = false;
317     if (object != NULL && object != Py_None)
318     {
319         if (PyString_Check(object))
320         {
321             retval.assign(PyString_AsString(object));
322             was_ok = true;
323         }
324         else
325         {
326             PyObject* value_as_string = PyObject_Str(object);
327             if (value_as_string && value_as_string != Py_None && PyString_Check(value_as_string))
328             {
329                 retval.assign(PyString_AsString(value_as_string));
330                 was_ok = true;
331             }
332             Py_XDECREF(value_as_string);
333         }
334     }
335     return was_ok;
336 }
337
338 SWIGEXPORT bool
339 LLDBSwigPythonCallTypeScript
340 (
341     const char *python_function_name,
342     const void *session_dictionary,
343     const lldb::ValueObjectSP& valobj_sp,
344     void** pyfunct_wrapper,
345     const lldb::TypeSummaryOptionsSP& options_sp,
346     std::string& retval
347 )
348 {
349     lldb::SBValue sb_value (valobj_sp);
350     lldb::SBTypeSummaryOptions sb_options(options_sp.get());
351
352     retval.clear();
353
354     if (!python_function_name || !session_dictionary)
355         return false;
356
357     PyObject *session_dict = (PyObject*)session_dictionary, *pfunc_impl = NULL, *pvalue = NULL;
358
359     if (pyfunct_wrapper && *pyfunct_wrapper && PyFunction_Check (*pyfunct_wrapper))
360     {
361         pfunc_impl = (PyObject*)(*pyfunct_wrapper);
362         if (pfunc_impl->ob_refcnt == 1)
363         {
364             Py_XDECREF(pfunc_impl);
365             pfunc_impl = NULL;
366         }
367     }
368
369     if (PyDict_Check(session_dict))
370     {
371         PyErr_Cleaner pyerr_cleanup(true);  // show Python errors
372
373         if (!pfunc_impl)
374         {
375             pfunc_impl = ResolvePythonName (python_function_name, session_dict);
376             if (!pfunc_impl || !PyCallable_Check (pfunc_impl))
377                 return false;
378             else
379             {
380                 if (pyfunct_wrapper)
381                     *pyfunct_wrapper = pfunc_impl;
382             }
383         }
384
385         PyCallable pfunc = PyCallable::FindWithPythonObject(pfunc_impl);
386
387         if (!pfunc)
388             return false;
389
390         // if the third argument is supported, or varargs are allowed
391         PyCallable::argc argc = pfunc.GetNumArguments();
392         if (argc.num_args == 3 || argc.varargs == true)
393             pvalue = pfunc(sb_value,session_dict,sb_options);
394         else
395             pvalue = pfunc(sb_value,session_dict);
396
397         Py_INCREF (session_dict);
398
399         PyObjectToString(pvalue,retval);
400
401         Py_XDECREF (pvalue);
402     }
403     return true;
404 }
405
406 SWIGEXPORT void*
407 LLDBSwigPythonCreateSyntheticProvider
408 (
409     const char *python_class_name,
410     const char *session_dictionary_name,
411     const lldb::ValueObjectSP& valobj_sp
412 )
413 {
414     PyObject* retval = NULL;
415
416     if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
417         Py_RETURN_NONE;
418
419     // I do not want the SBValue to be deallocated when going out of scope because python
420     // has ownership of it and will manage memory for this object by itself
421     lldb::SBValue *sb_value = new lldb::SBValue(valobj_sp);
422     sb_value->SetPreferSyntheticValue(false);
423     PyObject *ValObj_PyObj = SBTypeToSWIGWrapper(sb_value);
424
425     if (ValObj_PyObj == NULL)
426         Py_RETURN_NONE;
427
428     {
429         PyErr_Cleaner py_err_cleaner(true);
430
431         PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name,session_dictionary_name);
432
433         if (!pfunc)
434             return retval;
435
436         Py_INCREF(ValObj_PyObj);
437
438         PyObject* session_dict = NULL;
439         session_dict = FindSessionDictionary(session_dictionary_name);
440         retval = pfunc(sb_value, session_dict);
441
442         Py_XINCREF (session_dict);
443
444         Py_XINCREF(retval);
445     }
446
447     if (retval)
448         return retval;
449     else
450         Py_RETURN_NONE;
451 }
452
453 SWIGEXPORT void*
454 LLDBSwigPythonCreateCommandObject
455 (
456     const char *python_class_name,
457     const char *session_dictionary_name,
458     const lldb::DebuggerSP debugger_sp
459 )
460 {
461     PyObject* retval = NULL;
462
463     if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
464         Py_RETURN_NONE;
465
466     lldb::SBDebugger debugger_sb(debugger_sp);
467
468     {
469         PyErr_Cleaner py_err_cleaner(true);
470
471         PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name,session_dictionary_name);
472
473         if (!pfunc)
474             return retval;
475
476         PyObject* session_dict = NULL;
477         session_dict = FindSessionDictionary(session_dictionary_name);
478         retval = pfunc(debugger_sb, session_dict);
479
480         Py_XINCREF (session_dict);
481
482         Py_XINCREF(retval);
483     }
484
485     if (retval)
486         return retval;
487     else
488         Py_RETURN_NONE;
489 }
490
491 SWIGEXPORT void*
492 LLDBSwigPythonCreateScriptedThreadPlan
493 (
494     const char *python_class_name,
495     const char *session_dictionary_name,
496     const lldb::ThreadPlanSP& thread_plan_sp
497 )
498 {
499     PyObject* retval = NULL;
500
501     if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
502         Py_RETURN_NONE;
503
504     // I do not want the SBThreadPlan to be deallocated when going out of scope because python
505     // has ownership of it and will manage memory for this object by itself
506     lldb::SBThreadPlan *tp_value = new lldb::SBThreadPlan(thread_plan_sp);
507
508     PyObject *ThreadPlan_PyObj = SBTypeToSWIGWrapper(tp_value);
509
510     if (ThreadPlan_PyObj == NULL)
511         Py_RETURN_NONE;
512
513     {
514         PyErr_Cleaner py_err_cleaner(true);
515
516         PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name, session_dictionary_name);
517
518         if (!pfunc)
519             return retval;
520
521         Py_INCREF(ThreadPlan_PyObj);
522
523         PyObject* session_dict = NULL;
524         session_dict = FindSessionDictionary(session_dictionary_name);
525         retval = pfunc(tp_value, session_dict);
526
527         // FIXME: At this point we should check that the class we found supports all the methods
528         // that we need.
529
530         Py_XINCREF (session_dict);
531
532         Py_XINCREF(retval);
533     }
534
535     if (retval)
536         return retval;
537     else
538         Py_RETURN_NONE;
539 }
540
541 SWIGEXPORT bool
542 LLDBSWIGPythonCallThreadPlan
543 (
544     void *implementor,
545     const char *method_name,
546     lldb_private::Event *event,
547     bool &got_error
548 )
549 {
550     bool ret_val = false;
551     got_error = false;
552
553
554     PyErr_Cleaner py_err_cleaner(false);
555
556     PyCallable pfunc = PyCallable::FindWithMemberFunction((PyObject *) implementor, method_name);
557
558     if (!pfunc)
559     {
560         return ret_val;
561     }
562
563     PyObject* py_return = Py_None;
564
565     if (event != NULL)
566     {
567         lldb::SBEvent sb_event(event);
568
569         PyObject *py_obj_event = SBTypeToSWIGWrapper(sb_event);
570
571         py_return = pfunc(py_obj_event);
572     }
573     else
574     {
575         py_return = pfunc();
576     }
577
578     if (PyErr_Occurred())
579     {
580         got_error = true;
581         printf ("Return value was neither false nor true for call to %s.\n", method_name);
582         PyErr_Print();
583     }
584     else
585     {
586         if (py_return == Py_True)
587             ret_val = true;
588         else if (py_return == Py_False)
589             ret_val = false;
590         else
591         {
592             // Somebody returned the wrong thing...
593             got_error = true;
594             printf ("Wrong return value type for call to %s.\n", method_name);
595         }
596     }
597
598     Py_XDECREF(py_return);
599
600     return ret_val;
601 }
602
603 // wrapper that calls an optional instance member of an object taking no arguments
604 static PyObject*
605 LLDBSwigPython_CallOptionalMember
606 (
607     PyObject* self,
608     char* callee_name,
609     PyObject* ret_if_not_found = Py_None,
610     bool* was_found = NULL
611 )
612 {
613     PyErr_Cleaner py_err_cleaner(false);
614
615     PyCallable pfunc = PyCallable::FindWithMemberFunction(self,callee_name);
616
617     if (!pfunc)
618     {
619         if (was_found)
620             *was_found = false;
621         Py_XINCREF(ret_if_not_found);
622         return ret_if_not_found;
623     }
624
625     if (was_found)
626         *was_found = true;
627
628     PyObject* py_return = pfunc();
629     return py_return;
630 }
631
632 SWIGEXPORT size_t
633 LLDBSwigPython_CalculateNumChildren
634 (
635     PyObject *implementor
636 )
637 {
638     size_t ret_val = UINT32_MAX;
639     bool int_match = false;
640
641     static char callee_name[] = "num_children";
642
643     PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, NULL);
644
645     if (!py_return)
646         return ret_val;
647
648     // PyInt_* are not available for Python 3 and above.
649 #if PY_MAJOR_VERSION < 3
650     if (PyInt_Check (py_return))
651     {
652         int_match = true;
653         ret_val = static_cast<size_t> (PyInt_AsLong (py_return));
654     }
655 #endif
656
657     // We want to check for PyLong only if the return value did not
658     // match PyInt. This is because we do not want to call PyLong_Check if
659     // PyInt_Check returns true but PyInt_AsLong generates an error.
660     if (!int_match && PyLong_Check (py_return))
661     {
662 #if PY_MAJOR_VERSION < 3
663         ret_val = static_cast<size_t> (PyLong_AsUnsignedLong (py_return));
664 #else
665         // PyLong_AsSize_t is available only for Python 3 and above.
666         ret_val = PyLong_AsSize_t (py_return);
667 #endif
668     }
669
670     Py_XDECREF(py_return);
671
672     if (PyErr_Occurred())
673     {
674         PyErr_Print();
675         PyErr_Clear();
676     }
677
678     return ret_val;
679 }
680
681 SWIGEXPORT PyObject*
682 LLDBSwigPython_GetChildAtIndex
683 (
684     PyObject *implementor,
685     uint32_t idx
686 )
687 {
688     PyErr_Cleaner py_err_cleaner(true);
689
690     PyCallable pfunc = PyCallable::FindWithMemberFunction(implementor,"get_child_at_index");
691
692     if (!pfunc)
693         return NULL;
694
695     PyObject *py_return = NULL;
696     py_return = pfunc(idx);
697
698     if (py_return == NULL || py_return == Py_None)
699     {
700         Py_XDECREF(py_return);
701         return NULL;
702     }
703
704     lldb::SBValue* sbvalue_ptr = NULL;
705
706     if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
707     {
708         Py_XDECREF(py_return);
709         return NULL;
710     }
711
712     if (sbvalue_ptr == NULL)
713         return NULL;
714
715     return py_return;
716 }
717
718 SWIGEXPORT int
719 LLDBSwigPython_GetIndexOfChildWithName
720 (
721     PyObject *implementor,
722     const char* child_name
723 )
724 {
725     PyErr_Cleaner py_err_cleaner(true);
726
727     PyCallable pfunc = PyCallable::FindWithMemberFunction(implementor,"get_child_index");
728
729     if (!pfunc)
730         return UINT32_MAX;
731
732     PyObject *py_return = NULL;
733     py_return = pfunc(child_name);
734
735     if (py_return == NULL || py_return == Py_None)
736     {
737         Py_XDECREF(py_return);
738         return UINT32_MAX;
739     }
740
741     long retval = PyInt_AsLong(py_return);
742     Py_XDECREF(py_return);
743
744     if (retval >= 0)
745         return (uint32_t)retval;
746
747     return UINT32_MAX;
748 }
749
750 SWIGEXPORT bool
751 LLDBSwigPython_UpdateSynthProviderInstance
752 (
753     PyObject *implementor
754 )
755 {
756     bool ret_val = false;
757
758     static char callee_name[] = "update";
759
760     PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name);
761
762     if (py_return == Py_True)
763         ret_val = true;
764
765     Py_XDECREF(py_return);
766
767     return ret_val;
768 }
769
770 SWIGEXPORT bool
771 LLDBSwigPython_MightHaveChildrenSynthProviderInstance
772 (
773     PyObject *implementor
774 )
775 {
776     bool ret_val = false;
777
778     static char callee_name[] = "has_children";
779
780     PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True);
781
782     if (py_return == Py_True)
783         ret_val = true;
784
785     Py_XDECREF(py_return);
786
787     return ret_val;
788 }
789
790 SWIGEXPORT PyObject*
791 LLDBSwigPython_GetValueSynthProviderInstance
792 (
793     PyObject *implementor
794 )
795 {
796     PyObject* ret_val = nullptr;
797
798     static char callee_name[] = "get_value";
799
800     PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_None);
801
802     if (py_return == Py_None || py_return == nullptr)
803         ret_val = nullptr;
804
805     lldb::SBValue* sbvalue_ptr = NULL;
806
807     if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
808         ret_val = nullptr;
809     else if (sbvalue_ptr == NULL)
810         ret_val = nullptr;
811     else
812         ret_val = py_return;
813
814     Py_XDECREF(py_return);
815     return ret_val;
816 }
817
818 SWIGEXPORT void*
819 LLDBSWIGPython_CastPyObjectToSBValue
820 (
821     PyObject* data
822 )
823 {
824     lldb::SBValue* sb_ptr = NULL;
825
826     int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
827
828     if (valid_cast == -1)
829         return NULL;
830
831     return sb_ptr;
832 }
833
834 // Currently, SBCommandReturnObjectReleaser wraps a unique pointer to an
835 // lldb_private::CommandReturnObject. This means that the destructor for the
836 // SB object will deallocate its contained CommandReturnObject. Because that
837 // object is used as the real return object for Python-based commands, we want
838 // it to stay around. Thus, we release the unique pointer before returning from
839 // LLDBSwigPythonCallCommand, and to guarantee that the release will occur no
840 // matter how we exit from the function, we have a releaser object whose
841 // destructor does the right thing for us
842 class SBCommandReturnObjectReleaser
843 {
844 public:
845     SBCommandReturnObjectReleaser (lldb::SBCommandReturnObject &obj) :
846         m_command_return_object_ref (obj)
847     {
848     }
849
850     ~SBCommandReturnObjectReleaser ()
851     {
852         m_command_return_object_ref.Release();
853     }
854 private:
855     lldb::SBCommandReturnObject &m_command_return_object_ref;
856 };
857
858 SWIGEXPORT bool
859 LLDBSwigPythonCallCommand
860 (
861     const char *python_function_name,
862     const char *session_dictionary_name,
863     lldb::DebuggerSP& debugger,
864     const char* args,
865     lldb_private::CommandReturnObject& cmd_retobj,
866     lldb::ExecutionContextRefSP exe_ctx_ref_sp
867 )
868 {
869
870     lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj);
871     SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb);
872     lldb::SBDebugger debugger_sb(debugger);
873     lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
874
875     bool retval = false;
876
877     {
878         PyErr_Cleaner py_err_cleaner(true);
879         PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
880
881         if (!pfunc)
882             return retval;
883
884         PyObject* session_dict = NULL;
885         // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you
886         // see comment above for SBCommandReturnObjectReleaser for further details
887         PyObject* pvalue = NULL;
888
889         PyCallable::argc argc = pfunc.GetNumArguments();
890         if (argc.num_args == 5 || argc.varargs == true)
891             pvalue = pfunc(debugger_sb, args, exe_ctx_sb, &cmd_retobj_sb, session_dict = FindSessionDictionary(session_dictionary_name));
892         else
893             pvalue = pfunc(debugger_sb, args, &cmd_retobj_sb, session_dict = FindSessionDictionary(session_dictionary_name));
894
895         Py_XINCREF (session_dict);
896         Py_XDECREF (pvalue);
897
898         retval = true;
899     }
900
901     return retval;
902 }
903
904 SWIGEXPORT bool
905 LLDBSwigPythonCallCommandObject
906 (
907     PyObject *implementor,
908     lldb::DebuggerSP& debugger,
909     const char* args,
910     lldb_private::CommandReturnObject& cmd_retobj,
911     lldb::ExecutionContextRefSP exe_ctx_ref_sp
912 )
913 {
914
915     lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj);
916     SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb);
917     lldb::SBDebugger debugger_sb(debugger);
918     lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
919
920     PyErr_Cleaner py_err_cleaner(true);
921
922     PyCallable pfunc = PyCallable::FindWithMemberFunction(implementor,"__call__");
923
924     if (!pfunc)
925         return false;
926
927     // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you
928     // see comment above for SBCommandReturnObjectReleaser for further details
929     PyObject* pvalue = NULL;
930
931     pvalue = pfunc(debugger_sb, args, exe_ctx_sb, &cmd_retobj_sb);
932
933     Py_XDECREF (pvalue);
934
935     return true;
936 }
937
938 SWIGEXPORT void*
939 LLDBSWIGPythonCreateOSPlugin
940 (
941     const char *python_class_name,
942     const char *session_dictionary_name,
943     const lldb::ProcessSP& process_sp
944 )
945 {
946     PyObject* retval = NULL;
947
948     if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
949         Py_RETURN_NONE;
950
951     // I do not want the SBProcess to be deallocated when going out of scope because python
952     // has ownership of it and will manage memory for this object by itself
953     lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp);
954
955     PyObject *SBProc_PyObj = SBTypeToSWIGWrapper(process_sb);
956
957     if (SBProc_PyObj == NULL)
958         Py_RETURN_NONE;
959
960     {
961         PyErr_Cleaner py_err_cleaner(true);
962
963         PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name,session_dictionary_name);
964
965         if (!pfunc)
966             return retval;
967
968         Py_INCREF(SBProc_PyObj);
969
970         PyObject* session_dict = NULL;
971         session_dict = session_dict = FindSessionDictionary(session_dictionary_name);
972         retval = pfunc(SBProc_PyObj);
973
974         Py_XINCREF (session_dict);
975
976         Py_XINCREF(retval);
977     }
978
979     if (retval)
980         return retval;
981     else
982         Py_RETURN_NONE;
983 }
984
985 SWIGEXPORT void*
986 LLDBSWIGPython_GetDynamicSetting (void* module, const char* setting, const lldb::TargetSP& target_sp)
987 {
988
989     if (!module || !setting)
990         Py_RETURN_NONE;
991
992     lldb::SBTarget target_sb(target_sp);
993
994     PyObject *pvalue = NULL;
995
996     {
997         PyErr_Cleaner py_err_cleaner(true);
998         PyCallable pfunc = PyCallable::FindWithFunctionName("get_dynamic_setting",(PyObject *)module);
999
1000         if (!pfunc)
1001             Py_RETURN_NONE;
1002
1003         pvalue = pfunc(target_sb, setting);
1004     }
1005
1006     return pvalue;
1007 }
1008
1009 SWIGEXPORT bool
1010 LLDBSWIGPythonRunScriptKeywordProcess
1011 (const char* python_function_name,
1012 const char* session_dictionary_name,
1013 lldb::ProcessSP& process,
1014 std::string& output)
1015
1016 {
1017     bool retval = false;
1018
1019     if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
1020         return retval;
1021
1022     lldb::SBProcess process_sb(process);
1023
1024     {
1025         PyErr_Cleaner py_err_cleaner(true);
1026
1027         PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1028
1029         if (!pfunc)
1030             return retval;
1031
1032         PyObject* session_dict = NULL;
1033         PyObject* pvalue = NULL;
1034         pvalue = pfunc(process_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1035
1036         Py_XINCREF (session_dict);
1037
1038         if (PyObjectToString(pvalue,output))
1039             retval = true;
1040
1041         Py_XDECREF(pvalue);
1042     }
1043
1044     return retval;
1045 }
1046
1047 SWIGEXPORT bool
1048 LLDBSWIGPythonRunScriptKeywordThread
1049 (const char* python_function_name,
1050 const char* session_dictionary_name,
1051 lldb::ThreadSP& thread,
1052 std::string& output)
1053
1054 {
1055     bool retval = false;
1056
1057     if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
1058         return retval;
1059
1060     lldb::SBThread thread_sb(thread);
1061
1062     {
1063         PyErr_Cleaner py_err_cleaner(true);
1064
1065         PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1066
1067         if (!pfunc)
1068             return retval;
1069
1070         PyObject* session_dict = NULL;
1071         PyObject* pvalue = NULL;
1072         pvalue = pfunc(thread_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1073
1074         Py_XINCREF (session_dict);
1075
1076         if (PyObjectToString(pvalue,output))
1077             retval = true;
1078
1079         Py_XDECREF(pvalue);
1080     }
1081
1082     return retval;
1083 }
1084
1085 SWIGEXPORT bool
1086 LLDBSWIGPythonRunScriptKeywordTarget
1087 (const char* python_function_name,
1088 const char* session_dictionary_name,
1089 lldb::TargetSP& target,
1090 std::string& output)
1091
1092 {
1093     bool retval = false;
1094
1095     if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
1096         return retval;
1097
1098     lldb::SBTarget target_sb(target);
1099
1100     {
1101         PyErr_Cleaner py_err_cleaner(true);
1102
1103         PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1104
1105         if (!pfunc)
1106             return retval;
1107
1108         PyObject* session_dict = NULL;
1109         PyObject* pvalue = NULL;
1110         pvalue = pfunc(target_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1111
1112         Py_XINCREF (session_dict);
1113
1114         if (PyObjectToString(pvalue,output))
1115             retval = true;
1116
1117         Py_XDECREF(pvalue);
1118     }
1119
1120     return retval;
1121 }
1122
1123 SWIGEXPORT bool
1124 LLDBSWIGPythonRunScriptKeywordFrame
1125 (const char* python_function_name,
1126 const char* session_dictionary_name,
1127 lldb::StackFrameSP& frame,
1128 std::string& output)
1129
1130 {
1131     bool retval = false;
1132
1133     if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
1134         return retval;
1135
1136     lldb::SBFrame frame_sb(frame);
1137
1138     {
1139         PyErr_Cleaner py_err_cleaner(true);
1140
1141         PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1142
1143         if (!pfunc)
1144             return retval;
1145
1146         PyObject* session_dict = NULL;
1147         PyObject* pvalue = NULL;
1148         pvalue = pfunc(frame_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1149
1150         Py_XINCREF (session_dict);
1151
1152         if (PyObjectToString(pvalue,output))
1153             retval = true;
1154
1155         Py_XDECREF(pvalue);
1156     }
1157
1158     return retval;
1159 }
1160
1161 SWIGEXPORT bool
1162 LLDBSWIGPythonRunScriptKeywordValue
1163 (const char* python_function_name,
1164 const char* session_dictionary_name,
1165 lldb::ValueObjectSP& value,
1166 std::string& output)
1167
1168 {
1169     bool retval = false;
1170
1171     if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
1172         return retval;
1173
1174     lldb::SBValue value_sb(value);
1175
1176     {
1177         PyErr_Cleaner py_err_cleaner(true);
1178
1179         PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1180
1181         if (!pfunc)
1182             return retval;
1183
1184         PyObject* session_dict = NULL;
1185         PyObject* pvalue = NULL;
1186         pvalue = pfunc(value_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1187
1188         Py_XINCREF (session_dict);
1189
1190         if (PyObjectToString(pvalue,output))
1191             retval = true;
1192
1193         Py_XDECREF(pvalue);
1194     }
1195
1196     return retval;
1197 }
1198
1199 SWIGEXPORT bool
1200 LLDBSwigPythonCallModuleInit
1201 (
1202     const char *python_module_name,
1203     const char *session_dictionary_name,
1204     lldb::DebuggerSP& debugger
1205 )
1206 {
1207     bool retval = false;
1208
1209     lldb::SBDebugger debugger_sb(debugger);
1210
1211     std::string python_function_name_string = python_module_name;
1212     python_function_name_string += ".__lldb_init_module";
1213     const char* python_function_name = python_function_name_string.c_str();
1214
1215     {
1216         PyErr_Cleaner py_err_cleaner(true);
1217
1218         PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1219
1220         if (!pfunc)
1221             return true;
1222
1223         PyObject* session_dict = NULL;
1224         PyObject* pvalue = NULL;
1225         pvalue = pfunc(debugger_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1226
1227         Py_XINCREF (session_dict);
1228
1229         retval = true;
1230
1231         Py_XDECREF(pvalue);
1232     }
1233
1234     return retval;
1235 }
1236 %}
1237
1238
1239 %runtime %{
1240 // Forward declaration to be inserted at the start of LLDBWrapPython.h
1241 #include "lldb/API/SBDebugger.h"
1242 #include "lldb/API/SBValue.h"
1243
1244 SWIGEXPORT lldb::ValueObjectSP
1245 LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data)
1246 {
1247     lldb::ValueObjectSP valobj_sp;
1248     if (data)
1249     {
1250         lldb::SBValue* sb_ptr = (lldb::SBValue *)data;
1251         valobj_sp = sb_ptr->GetSP();
1252     }
1253     return valobj_sp;
1254 }
1255
1256 #ifdef __cplusplus
1257 extern "C" {
1258 #endif
1259
1260 void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton);
1261
1262 #ifdef __cplusplus
1263 }
1264 #endif
1265 %}
1266
1267 %wrapper %{
1268
1269
1270 // For the LogOutputCallback functions
1271 void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) {
1272     if (baton != Py_None) {
1273       SWIG_PYTHON_THREAD_BEGIN_BLOCK;
1274       PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str);
1275       SWIG_PYTHON_THREAD_END_BLOCK;
1276     }
1277 }
1278 %}