af8ea65db899d8757795ff1b35666e9d487c2c75
[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         pvalue = pfunc(sb_value,session_dict);
398
399         Py_INCREF (session_dict);
400
401         PyObjectToString(pvalue,retval);
402
403         Py_XDECREF (pvalue);
404     }
405     return true;
406 }
407
408 SWIGEXPORT void*
409 LLDBSwigPythonCreateSyntheticProvider
410 (
411     const char *python_class_name,
412     const char *session_dictionary_name,
413     const lldb::ValueObjectSP& valobj_sp
414 )
415 {
416     PyObject* retval = NULL;
417
418     if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
419         Py_RETURN_NONE;
420
421     // I do not want the SBValue to be deallocated when going out of scope because python
422     // has ownership of it and will manage memory for this object by itself
423     lldb::SBValue *sb_value = new lldb::SBValue(valobj_sp);
424     sb_value->SetPreferSyntheticValue(false);
425     PyObject *ValObj_PyObj = SBTypeToSWIGWrapper(sb_value);
426
427     if (ValObj_PyObj == NULL)
428         Py_RETURN_NONE;
429
430     {
431         PyErr_Cleaner py_err_cleaner(true);
432
433         PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name,session_dictionary_name);
434
435         if (!pfunc)
436             return retval;
437
438         Py_INCREF(ValObj_PyObj);
439
440         PyObject* session_dict = NULL;
441         session_dict = FindSessionDictionary(session_dictionary_name);
442         retval = pfunc(sb_value, session_dict);
443
444         Py_XINCREF (session_dict);
445
446         Py_XINCREF(retval);
447     }
448
449     if (retval)
450         return retval;
451     else
452         Py_RETURN_NONE;
453 }
454
455 SWIGEXPORT void*
456 LLDBSwigPythonCreateCommandObject
457 (
458     const char *python_class_name,
459     const char *session_dictionary_name,
460     const lldb::DebuggerSP debugger_sp
461 )
462 {
463     PyObject* retval = NULL;
464
465     if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
466         Py_RETURN_NONE;
467
468     lldb::SBDebugger debugger_sb(debugger_sp);
469
470     {
471         PyErr_Cleaner py_err_cleaner(true);
472
473         PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name,session_dictionary_name);
474
475         if (!pfunc)
476             return retval;
477
478         PyObject* session_dict = NULL;
479         session_dict = FindSessionDictionary(session_dictionary_name);
480         retval = pfunc(debugger_sb, session_dict);
481
482         Py_XINCREF (session_dict);
483
484         Py_XINCREF(retval);
485     }
486
487     if (retval)
488         return retval;
489     else
490         Py_RETURN_NONE;
491 }
492
493 SWIGEXPORT void*
494 LLDBSwigPythonCreateScriptedThreadPlan
495 (
496     const char *python_class_name,
497     const char *session_dictionary_name,
498     const lldb::ThreadPlanSP& thread_plan_sp
499 )
500 {
501     PyObject* retval = NULL;
502
503     if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
504         Py_RETURN_NONE;
505
506     // I do not want the SBThreadPlan to be deallocated when going out of scope because python
507     // has ownership of it and will manage memory for this object by itself
508     lldb::SBThreadPlan *tp_value = new lldb::SBThreadPlan(thread_plan_sp);
509
510     PyObject *ThreadPlan_PyObj = SBTypeToSWIGWrapper(tp_value);
511
512     if (ThreadPlan_PyObj == NULL)
513         Py_RETURN_NONE;
514
515     {
516         PyErr_Cleaner py_err_cleaner(true);
517
518         PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name, session_dictionary_name);
519
520         if (!pfunc)
521             return retval;
522
523         Py_INCREF(ThreadPlan_PyObj);
524
525         PyObject* session_dict = NULL;
526         session_dict = FindSessionDictionary(session_dictionary_name);
527         retval = pfunc(tp_value, session_dict);
528
529         // FIXME: At this point we should check that the class we found supports all the methods
530         // that we need.
531
532         Py_XINCREF (session_dict);
533
534         Py_XINCREF(retval);
535     }
536
537     if (retval)
538         return retval;
539     else
540         Py_RETURN_NONE;
541 }
542
543 SWIGEXPORT bool
544 LLDBSWIGPythonCallThreadPlan
545 (
546     void *implementor,
547     const char *method_name,
548     lldb_private::Event *event,
549     bool &got_error
550 )
551 {
552     bool ret_val = false;
553     got_error = false;
554
555
556     PyErr_Cleaner py_err_cleaner(false);
557
558     PyCallable pfunc = PyCallable::FindWithMemberFunction((PyObject *) implementor, method_name);
559
560     if (!pfunc)
561     {
562         return ret_val;
563     }
564
565     PyObject* py_return = Py_None;
566
567     if (event != NULL)
568     {
569         lldb::SBEvent sb_event(event);
570
571         PyObject *py_obj_event = SBTypeToSWIGWrapper(sb_event);
572
573         py_return = pfunc(py_obj_event);
574     }
575     else
576     {
577         py_return = pfunc();
578     }
579
580     if (PyErr_Occurred())
581     {
582         got_error = true;
583         printf ("Return value was neither false nor true for call to %s.\n", method_name);
584         PyErr_Print();
585     }
586     else
587     {
588         if (py_return == Py_True)
589             ret_val = true;
590         else if (py_return == Py_False)
591             ret_val = false;
592         else
593         {
594             // Somebody returned the wrong thing...
595             got_error = true;
596             printf ("Wrong return value type for call to %s.\n", method_name);
597         }
598     }
599
600     Py_XDECREF(py_return);
601
602     return ret_val;
603 }
604
605 // wrapper that calls an optional instance member of an object taking no arguments
606 static PyObject*
607 LLDBSwigPython_CallOptionalMember
608 (
609     PyObject* self,
610     char* callee_name,
611     PyObject* ret_if_not_found = Py_None,
612     bool* was_found = NULL
613 )
614 {
615     PyErr_Cleaner py_err_cleaner(false);
616
617     PyCallable pfunc = PyCallable::FindWithMemberFunction(self,callee_name);
618
619     if (!pfunc)
620     {
621         if (was_found)
622             *was_found = false;
623         Py_XINCREF(ret_if_not_found);
624         return ret_if_not_found;
625     }
626
627     if (was_found)
628         *was_found = true;
629
630     PyObject* py_return = pfunc();
631     return py_return;
632 }
633
634 SWIGEXPORT size_t
635 LLDBSwigPython_CalculateNumChildren
636 (
637     PyObject *implementor
638 )
639 {
640     size_t ret_val = UINT32_MAX;
641     bool int_match = false;
642
643     static char callee_name[] = "num_children";
644
645     PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, NULL);
646
647     if (!py_return)
648         return ret_val;
649
650     // PyInt_* are not available for Python 3 and above.
651 #if PY_MAJOR_VERSION < 3
652     if (PyInt_Check (py_return))
653     {
654         int_match = true;
655         ret_val = static_cast<size_t> (PyInt_AsLong (py_return));
656     }
657 #endif
658
659     // We want to check for PyLong only if the return value did not
660     // match PyInt. This is because we do not want to call PyLong_Check if
661     // PyInt_Check returns true but PyInt_AsLong generates an error.
662     if (!int_match && PyLong_Check (py_return))
663     {
664 #if PY_MAJOR_VERSION < 3
665         ret_val = static_cast<size_t> (PyLong_AsUnsignedLong (py_return));
666 #else
667         // PyLong_AsSize_t is available only for Python 3 and above.
668         ret_val = PyLong_AsSize_t (py_return);
669 #endif
670     }
671
672     Py_XDECREF(py_return);
673
674     if (PyErr_Occurred())
675     {
676         PyErr_Print();
677         PyErr_Clear();
678     }
679
680     return ret_val;
681 }
682
683 SWIGEXPORT PyObject*
684 LLDBSwigPython_GetChildAtIndex
685 (
686     PyObject *implementor,
687     uint32_t idx
688 )
689 {
690     PyErr_Cleaner py_err_cleaner(true);
691
692     PyCallable pfunc = PyCallable::FindWithMemberFunction(implementor,"get_child_at_index");
693
694     if (!pfunc)
695         return NULL;
696
697     PyObject *py_return = NULL;
698     py_return = pfunc(idx);
699
700     if (py_return == NULL || py_return == Py_None)
701     {
702         Py_XDECREF(py_return);
703         return NULL;
704     }
705
706     lldb::SBValue* sbvalue_ptr = NULL;
707
708     if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
709     {
710         Py_XDECREF(py_return);
711         return NULL;
712     }
713
714     if (sbvalue_ptr == NULL)
715         return NULL;
716
717     return py_return;
718 }
719
720 SWIGEXPORT int
721 LLDBSwigPython_GetIndexOfChildWithName
722 (
723     PyObject *implementor,
724     const char* child_name
725 )
726 {
727     PyErr_Cleaner py_err_cleaner(true);
728
729     PyCallable pfunc = PyCallable::FindWithMemberFunction(implementor,"get_child_index");
730
731     if (!pfunc)
732         return UINT32_MAX;
733
734     PyObject *py_return = NULL;
735     py_return = pfunc(child_name);
736
737     if (py_return == NULL || py_return == Py_None)
738     {
739         Py_XDECREF(py_return);
740         return UINT32_MAX;
741     }
742
743     long retval = PyInt_AsLong(py_return);
744     Py_XDECREF(py_return);
745
746     if (retval >= 0)
747         return (uint32_t)retval;
748
749     return UINT32_MAX;
750 }
751
752 SWIGEXPORT bool
753 LLDBSwigPython_UpdateSynthProviderInstance
754 (
755     PyObject *implementor
756 )
757 {
758     bool ret_val = false;
759
760     static char callee_name[] = "update";
761
762     PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name);
763
764     if (py_return == Py_True)
765         ret_val = true;
766
767     Py_XDECREF(py_return);
768
769     return ret_val;
770 }
771
772 SWIGEXPORT bool
773 LLDBSwigPython_MightHaveChildrenSynthProviderInstance
774 (
775     PyObject *implementor
776 )
777 {
778     bool ret_val = false;
779
780     static char callee_name[] = "has_children";
781
782     PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True);
783
784     if (py_return == Py_True)
785         ret_val = true;
786
787     Py_XDECREF(py_return);
788
789     return ret_val;
790 }
791
792 SWIGEXPORT PyObject*
793 LLDBSwigPython_GetValueSynthProviderInstance
794 (
795     PyObject *implementor
796 )
797 {
798     PyObject* ret_val = nullptr;
799
800     static char callee_name[] = "get_value";
801
802     PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_None);
803
804     if (py_return == Py_None || py_return == nullptr)
805         ret_val = nullptr;
806
807     lldb::SBValue* sbvalue_ptr = NULL;
808
809     if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
810         ret_val = nullptr;
811     else if (sbvalue_ptr == NULL)
812         ret_val = nullptr;
813     else
814         ret_val = py_return;
815
816     Py_XDECREF(py_return);
817     return ret_val;
818 }
819
820 SWIGEXPORT void*
821 LLDBSWIGPython_CastPyObjectToSBValue
822 (
823     PyObject* data
824 )
825 {
826     lldb::SBValue* sb_ptr = NULL;
827
828     int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
829
830     if (valid_cast == -1)
831         return NULL;
832
833     return sb_ptr;
834 }
835
836 // Currently, SBCommandReturnObjectReleaser wraps a unique pointer to an
837 // lldb_private::CommandReturnObject. This means that the destructor for the
838 // SB object will deallocate its contained CommandReturnObject. Because that
839 // object is used as the real return object for Python-based commands, we want
840 // it to stay around. Thus, we release the unique pointer before returning from
841 // LLDBSwigPythonCallCommand, and to guarantee that the release will occur no
842 // matter how we exit from the function, we have a releaser object whose
843 // destructor does the right thing for us
844 class SBCommandReturnObjectReleaser
845 {
846 public:
847     SBCommandReturnObjectReleaser (lldb::SBCommandReturnObject &obj) :
848         m_command_return_object_ref (obj)
849     {
850     }
851
852     ~SBCommandReturnObjectReleaser ()
853     {
854         m_command_return_object_ref.Release();
855     }
856 private:
857     lldb::SBCommandReturnObject &m_command_return_object_ref;
858 };
859
860 SWIGEXPORT bool
861 LLDBSwigPythonCallCommand
862 (
863     const char *python_function_name,
864     const char *session_dictionary_name,
865     lldb::DebuggerSP& debugger,
866     const char* args,
867     lldb_private::CommandReturnObject& cmd_retobj,
868     lldb::ExecutionContextRefSP exe_ctx_ref_sp
869 )
870 {
871
872     lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj);
873     SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb);
874     lldb::SBDebugger debugger_sb(debugger);
875     lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
876
877     bool retval = false;
878
879     {
880         PyErr_Cleaner py_err_cleaner(true);
881         PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
882
883         if (!pfunc)
884             return retval;
885
886         PyObject* session_dict = NULL;
887         // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you
888         // see comment above for SBCommandReturnObjectReleaser for further details
889         PyObject* pvalue = NULL;
890
891         PyCallable::argc argc = pfunc.GetNumArguments();
892         if (argc.num_args == 5 || argc.varargs == true)
893             pvalue = pfunc(debugger_sb, args, exe_ctx_sb, &cmd_retobj_sb, session_dict = FindSessionDictionary(session_dictionary_name));
894         else
895             pvalue = pfunc(debugger_sb, args, &cmd_retobj_sb, session_dict = FindSessionDictionary(session_dictionary_name));
896
897         Py_XINCREF (session_dict);
898         Py_XDECREF (pvalue);
899
900         retval = true;
901     }
902
903     return retval;
904 }
905
906 SWIGEXPORT bool
907 LLDBSwigPythonCallCommandObject
908 (
909     PyObject *implementor,
910     lldb::DebuggerSP& debugger,
911     const char* args,
912     lldb_private::CommandReturnObject& cmd_retobj,
913     lldb::ExecutionContextRefSP exe_ctx_ref_sp
914 )
915 {
916
917     lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj);
918     SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb);
919     lldb::SBDebugger debugger_sb(debugger);
920     lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
921
922     PyErr_Cleaner py_err_cleaner(true);
923
924     PyCallable pfunc = PyCallable::FindWithMemberFunction(implementor,"__call__");
925
926     if (!pfunc)
927         return false;
928
929     // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you
930     // see comment above for SBCommandReturnObjectReleaser for further details
931     PyObject* pvalue = NULL;
932
933     pvalue = pfunc(debugger_sb, args, exe_ctx_sb, &cmd_retobj_sb);
934
935     Py_XDECREF (pvalue);
936
937     return true;
938 }
939
940 SWIGEXPORT void*
941 LLDBSWIGPythonCreateOSPlugin
942 (
943     const char *python_class_name,
944     const char *session_dictionary_name,
945     const lldb::ProcessSP& process_sp
946 )
947 {
948     PyObject* retval = NULL;
949
950     if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
951         Py_RETURN_NONE;
952
953     // I do not want the SBProcess to be deallocated when going out of scope because python
954     // has ownership of it and will manage memory for this object by itself
955     lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp);
956
957     PyObject *SBProc_PyObj = SBTypeToSWIGWrapper(process_sb);
958
959     if (SBProc_PyObj == NULL)
960         Py_RETURN_NONE;
961
962     {
963         PyErr_Cleaner py_err_cleaner(true);
964
965         PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name,session_dictionary_name);
966
967         if (!pfunc)
968             return retval;
969
970         Py_INCREF(SBProc_PyObj);
971
972         PyObject* session_dict = NULL;
973         session_dict = session_dict = FindSessionDictionary(session_dictionary_name);
974         retval = pfunc(SBProc_PyObj);
975
976         Py_XINCREF (session_dict);
977
978         Py_XINCREF(retval);
979     }
980
981     if (retval)
982         return retval;
983     else
984         Py_RETURN_NONE;
985 }
986
987 SWIGEXPORT void*
988 LLDBSWIGPython_GetDynamicSetting (void* module, const char* setting, const lldb::TargetSP& target_sp)
989 {
990
991     if (!module || !setting)
992         Py_RETURN_NONE;
993
994     lldb::SBTarget target_sb(target_sp);
995
996     PyObject *pvalue = NULL;
997
998     {
999         PyErr_Cleaner py_err_cleaner(true);
1000         PyCallable pfunc = PyCallable::FindWithFunctionName("get_dynamic_setting",(PyObject *)module);
1001
1002         if (!pfunc)
1003             Py_RETURN_NONE;
1004
1005         pvalue = pfunc(target_sb, setting);
1006     }
1007
1008     return pvalue;
1009 }
1010
1011 SWIGEXPORT bool
1012 LLDBSWIGPythonRunScriptKeywordProcess
1013 (const char* python_function_name,
1014 const char* session_dictionary_name,
1015 lldb::ProcessSP& process,
1016 std::string& output)
1017
1018 {
1019     bool retval = false;
1020
1021     if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
1022         return retval;
1023
1024     lldb::SBProcess process_sb(process);
1025
1026     {
1027         PyErr_Cleaner py_err_cleaner(true);
1028
1029         PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1030
1031         if (!pfunc)
1032             return retval;
1033
1034         PyObject* session_dict = NULL;
1035         PyObject* pvalue = NULL;
1036         pvalue = pfunc(process_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1037
1038         Py_XINCREF (session_dict);
1039
1040         if (PyObjectToString(pvalue,output))
1041             retval = true;
1042
1043         Py_XDECREF(pvalue);
1044     }
1045
1046     return retval;
1047 }
1048
1049 SWIGEXPORT bool
1050 LLDBSWIGPythonRunScriptKeywordThread
1051 (const char* python_function_name,
1052 const char* session_dictionary_name,
1053 lldb::ThreadSP& thread,
1054 std::string& output)
1055
1056 {
1057     bool retval = false;
1058
1059     if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
1060         return retval;
1061
1062     lldb::SBThread thread_sb(thread);
1063
1064     {
1065         PyErr_Cleaner py_err_cleaner(true);
1066
1067         PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1068
1069         if (!pfunc)
1070             return retval;
1071
1072         PyObject* session_dict = NULL;
1073         PyObject* pvalue = NULL;
1074         pvalue = pfunc(thread_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1075
1076         Py_XINCREF (session_dict);
1077
1078         if (PyObjectToString(pvalue,output))
1079             retval = true;
1080
1081         Py_XDECREF(pvalue);
1082     }
1083
1084     return retval;
1085 }
1086
1087 SWIGEXPORT bool
1088 LLDBSWIGPythonRunScriptKeywordTarget
1089 (const char* python_function_name,
1090 const char* session_dictionary_name,
1091 lldb::TargetSP& target,
1092 std::string& output)
1093
1094 {
1095     bool retval = false;
1096
1097     if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
1098         return retval;
1099
1100     lldb::SBTarget target_sb(target);
1101
1102     {
1103         PyErr_Cleaner py_err_cleaner(true);
1104
1105         PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1106
1107         if (!pfunc)
1108             return retval;
1109
1110         PyObject* session_dict = NULL;
1111         PyObject* pvalue = NULL;
1112         pvalue = pfunc(target_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1113
1114         Py_XINCREF (session_dict);
1115
1116         if (PyObjectToString(pvalue,output))
1117             retval = true;
1118
1119         Py_XDECREF(pvalue);
1120     }
1121
1122     return retval;
1123 }
1124
1125 SWIGEXPORT bool
1126 LLDBSWIGPythonRunScriptKeywordFrame
1127 (const char* python_function_name,
1128 const char* session_dictionary_name,
1129 lldb::StackFrameSP& frame,
1130 std::string& output)
1131
1132 {
1133     bool retval = false;
1134
1135     if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
1136         return retval;
1137
1138     lldb::SBFrame frame_sb(frame);
1139
1140     {
1141         PyErr_Cleaner py_err_cleaner(true);
1142
1143         PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1144
1145         if (!pfunc)
1146             return retval;
1147
1148         PyObject* session_dict = NULL;
1149         PyObject* pvalue = NULL;
1150         pvalue = pfunc(frame_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1151
1152         Py_XINCREF (session_dict);
1153
1154         if (PyObjectToString(pvalue,output))
1155             retval = true;
1156
1157         Py_XDECREF(pvalue);
1158     }
1159
1160     return retval;
1161 }
1162
1163 SWIGEXPORT bool
1164 LLDBSWIGPythonRunScriptKeywordValue
1165 (const char* python_function_name,
1166 const char* session_dictionary_name,
1167 lldb::ValueObjectSP& value,
1168 std::string& output)
1169
1170 {
1171     bool retval = false;
1172
1173     if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
1174         return retval;
1175
1176     lldb::SBValue value_sb(value);
1177
1178     {
1179         PyErr_Cleaner py_err_cleaner(true);
1180
1181         PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1182
1183         if (!pfunc)
1184             return retval;
1185
1186         PyObject* session_dict = NULL;
1187         PyObject* pvalue = NULL;
1188         pvalue = pfunc(value_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1189
1190         Py_XINCREF (session_dict);
1191
1192         if (PyObjectToString(pvalue,output))
1193             retval = true;
1194
1195         Py_XDECREF(pvalue);
1196     }
1197
1198     return retval;
1199 }
1200
1201 SWIGEXPORT bool
1202 LLDBSwigPythonCallModuleInit
1203 (
1204     const char *python_module_name,
1205     const char *session_dictionary_name,
1206     lldb::DebuggerSP& debugger
1207 )
1208 {
1209     bool retval = false;
1210
1211     lldb::SBDebugger debugger_sb(debugger);
1212
1213     std::string python_function_name_string = python_module_name;
1214     python_function_name_string += ".__lldb_init_module";
1215     const char* python_function_name = python_function_name_string.c_str();
1216
1217     {
1218         PyErr_Cleaner py_err_cleaner(true);
1219
1220         PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1221
1222         if (!pfunc)
1223             return true;
1224
1225         PyObject* session_dict = NULL;
1226         PyObject* pvalue = NULL;
1227         pvalue = pfunc(debugger_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1228
1229         Py_XINCREF (session_dict);
1230
1231         retval = true;
1232
1233         Py_XDECREF(pvalue);
1234     }
1235
1236     return retval;
1237 }
1238 %}
1239
1240
1241 %runtime %{
1242 // Forward declaration to be inserted at the start of LLDBWrapPython.h
1243 #include "lldb/API/SBDebugger.h"
1244 #include "lldb/API/SBValue.h"
1245
1246 SWIGEXPORT lldb::ValueObjectSP
1247 LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data)
1248 {
1249     lldb::ValueObjectSP valobj_sp;
1250     if (data)
1251     {
1252         lldb::SBValue* sb_ptr = (lldb::SBValue *)data;
1253         valobj_sp = sb_ptr->GetSP();
1254     }
1255     return valobj_sp;
1256 }
1257
1258 #ifdef __cplusplus
1259 extern "C" {
1260 #endif
1261
1262 void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton);
1263
1264 #ifdef __cplusplus
1265 }
1266 #endif
1267 %}
1268
1269 %wrapper %{
1270
1271
1272 // For the LogOutputCallback functions
1273 void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) {
1274     if (baton != Py_None) {
1275       SWIG_PYTHON_THREAD_BEGIN_BLOCK;
1276       PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str);
1277       SWIG_PYTHON_THREAD_END_BLOCK;
1278     }
1279 }
1280 %}