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