blob: 1eaa52ba246017bce9ef7dddfc00be615647c751 [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;
Joerg Sonnenberger340a1752013-09-25 10:37:32 +000094 return ResolvePythonName(dot_pos+1,pmodule); // tail recursion.. should be optimized by the compiler
95 }
96}
97
98static PyObject*
99FindSessionDictionary(const char *session_dictionary_name)
100{
101 return ResolvePythonName(session_dictionary_name, NULL);
102}
103
104class PyCallable
105{
106public:
Enrico Granatad1fd3ce2014-10-01 20:51:50 +0000107 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
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000133 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;
Enrico Granata1ba73052014-01-29 23:18:58 +0000162 Py_INCREF(arg); // _SetItem steals a reference
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000163 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
210private:
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
234SWIGEXPORT bool
235LLDBSwigPythonBreakpointCallbackFunction
236(
237 const char *python_function_name,
238 const char *session_dictionary_name,
Jason Molendab57e4a12013-11-04 09:33:30 +0000239 const lldb::StackFrameSP& frame_sp,
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000240 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
274SWIGEXPORT bool
275LLDBSwigPythonWatchpointCallbackFunction
276(
277 const char *python_function_name,
278 const char *session_dictionary_name,
Jason Molendab57e4a12013-11-04 09:33:30 +0000279 const lldb::StackFrameSP& frame_sp,
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000280 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
311bool
312PyObjectToString (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
338SWIGEXPORT bool
339LLDBSwigPythonCallTypeScript
340(
341 const char *python_function_name,
342 const void *session_dictionary,
343 const lldb::ValueObjectSP& valobj_sp,
344 void** pyfunct_wrapper,
345 std::string& retval
346)
347{
348 lldb::SBValue sb_value (valobj_sp);
349
350 retval.clear();
351
352 if (!python_function_name || !session_dictionary)
353 return false;
354
355 PyObject *session_dict = (PyObject*)session_dictionary, *pfunc_impl = NULL, *pvalue = NULL;
356
357 if (pyfunct_wrapper && *pyfunct_wrapper && PyFunction_Check (*pyfunct_wrapper))
358 {
359 pfunc_impl = (PyObject*)(*pyfunct_wrapper);
360 if (pfunc_impl->ob_refcnt == 1)
361 {
362 Py_XDECREF(pfunc_impl);
363 pfunc_impl = NULL;
364 }
365 }
366
367 if (PyDict_Check(session_dict))
368 {
369 PyErr_Cleaner pyerr_cleanup(true); // show Python errors
370
371 if (!pfunc_impl)
372 {
373 pfunc_impl = ResolvePythonName (python_function_name, session_dict);
374 if (!pfunc_impl || !PyCallable_Check (pfunc_impl))
375 return false;
376 else
377 {
378 if (pyfunct_wrapper)
379 *pyfunct_wrapper = pfunc_impl;
380 }
381 }
Enrico Granata0e0e9f52013-12-26 07:21:41 +0000382
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000383 PyCallable pfunc = PyCallable::FindWithPythonObject(pfunc_impl);
384
385 if (!pfunc)
386 return false;
387
388 pvalue = pfunc(sb_value,session_dict);
389
390 Py_INCREF (session_dict);
391
392 PyObjectToString(pvalue,retval);
393
394 Py_XDECREF (pvalue);
395 }
396 return true;
397}
398
399SWIGEXPORT void*
400LLDBSwigPythonCreateSyntheticProvider
401(
402 const char *python_class_name,
403 const char *session_dictionary_name,
404 const lldb::ValueObjectSP& valobj_sp
405)
406{
407 PyObject* retval = NULL;
408
409 if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
410 Py_RETURN_NONE;
411
412 // I do not want the SBValue to be deallocated when going out of scope because python
413 // has ownership of it and will manage memory for this object by itself
414 lldb::SBValue *sb_value = new lldb::SBValue(valobj_sp);
415 sb_value->SetPreferSyntheticValue(false);
416 PyObject *ValObj_PyObj = SBTypeToSWIGWrapper(sb_value);
417
418 if (ValObj_PyObj == NULL)
419 Py_RETURN_NONE;
420
421 {
422 PyErr_Cleaner py_err_cleaner(true);
423
424 PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name,session_dictionary_name);
425
426 if (!pfunc)
427 return retval;
428
429 Py_INCREF(ValObj_PyObj);
430
431 PyObject* session_dict = NULL;
432 session_dict = FindSessionDictionary(session_dictionary_name);
433 retval = pfunc(sb_value, session_dict);
434
435 Py_XINCREF (session_dict);
436
437 Py_XINCREF(retval);
438 }
439
440 if (retval)
441 return retval;
442 else
443 Py_RETURN_NONE;
444}
445
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000446SWIGEXPORT void*
447LLDBSwigPythonCreateScriptedThreadPlan
448(
449 const char *python_class_name,
450 const char *session_dictionary_name,
451 const lldb::ThreadPlanSP& thread_plan_sp
452)
453{
454 PyObject* retval = NULL;
455
456 if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
457 Py_RETURN_NONE;
458
459 // I do not want the SBThreadPlan to be deallocated when going out of scope because python
460 // has ownership of it and will manage memory for this object by itself
461 lldb::SBThreadPlan *tp_value = new lldb::SBThreadPlan(thread_plan_sp);
462
463 PyObject *ThreadPlan_PyObj = SBTypeToSWIGWrapper(tp_value);
464
465 if (ThreadPlan_PyObj == NULL)
466 Py_RETURN_NONE;
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 Py_INCREF(ThreadPlan_PyObj);
477
478 PyObject* session_dict = NULL;
479 session_dict = FindSessionDictionary(session_dictionary_name);
480 retval = pfunc(tp_value, session_dict);
481
482 // FIXME: At this point we should check that the class we found supports all the methods
483 // that we need.
484
485 Py_XINCREF (session_dict);
486
487 Py_XINCREF(retval);
488 }
489
490 if (retval)
491 return retval;
492 else
493 Py_RETURN_NONE;
494}
495
496SWIGEXPORT bool
497LLDBSWIGPythonCallThreadPlan
498(
499 void *implementor,
500 const char *method_name,
501 lldb_private::Event *event,
502 bool &got_error
503)
504{
505 bool ret_val = false;
506 got_error = false;
507
508
509 PyErr_Cleaner py_err_cleaner(false);
510
511 PyCallable pfunc = PyCallable::FindWithMemberFunction((PyObject *) implementor, method_name);
512
513 if (!pfunc)
514 {
515 return ret_val;
516 }
517
518 PyObject* py_return = Py_None;
519
520 if (event != NULL)
521 {
522 lldb::SBEvent sb_event(event);
523
524 PyObject *py_obj_event = SBTypeToSWIGWrapper(sb_event);
525
526 py_return = pfunc(py_obj_event);
527 }
528 else
529 {
530 py_return = pfunc();
531 }
532
533 if (PyErr_Occurred())
534 {
535 got_error = true;
536 printf ("Return value was neither false nor true for call to %s.\n", method_name);
537 PyErr_Print();
538 }
539 else
540 {
541 if (py_return == Py_True)
542 ret_val = true;
543 else if (py_return == Py_False)
544 ret_val = false;
545 else
546 {
547 // Somebody returned the wrong thing...
548 got_error = true;
549 printf ("Wrong return value type for call to %s.\n", method_name);
550 }
551 }
552
553 Py_XDECREF(py_return);
554
555 return ret_val;
556}
557
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000558// wrapper that calls an optional instance member of an object taking no arguments
559static PyObject*
560LLDBSwigPython_CallOptionalMember
561(
562 PyObject* self,
563 char* callee_name,
564 PyObject* ret_if_not_found = Py_None,
565 bool* was_found = NULL
566)
567{
568 PyErr_Cleaner py_err_cleaner(false);
569
570 PyCallable pfunc = PyCallable::FindWithMemberFunction(self,callee_name);
571
572 if (!pfunc)
573 {
574 if (was_found)
575 *was_found = false;
576 Py_XINCREF(ret_if_not_found);
577 return ret_if_not_found;
578 }
579
580 if (was_found)
581 *was_found = true;
582
583 PyObject* py_return = pfunc();
584 return py_return;
585}
586
587SWIGEXPORT uint32_t
588LLDBSwigPython_CalculateNumChildren
589(
590 PyObject *implementor
591)
592{
593 uint32_t ret_val = UINT32_MAX;
594
595 static char callee_name[] = "num_children";
596
597 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, NULL);
598
599 if (!py_return)
600 return ret_val;
601
602 if (PyInt_Check(py_return))
603 ret_val = PyInt_AsLong(py_return);
604
605 Py_XDECREF(py_return);
606
607 if (PyErr_Occurred())
608 {
609 PyErr_Print();
610 PyErr_Clear();
611 }
612
613 return ret_val;
614}
615
616SWIGEXPORT PyObject*
617LLDBSwigPython_GetChildAtIndex
618(
619 PyObject *implementor,
620 uint32_t idx
621)
622{
623 PyErr_Cleaner py_err_cleaner(true);
624
625 PyCallable pfunc = PyCallable::FindWithMemberFunction(implementor,"get_child_at_index");
626
627 if (!pfunc)
628 return NULL;
629
630 PyObject *py_return = NULL;
631 py_return = pfunc(idx);
632
633 if (py_return == NULL || py_return == Py_None)
634 {
635 Py_XDECREF(py_return);
636 return NULL;
637 }
638
639 lldb::SBValue* sbvalue_ptr = NULL;
640
641 if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
642 {
643 Py_XDECREF(py_return);
644 return NULL;
645 }
646
647 if (sbvalue_ptr == NULL)
648 return NULL;
649
650 return py_return;
651}
652
653SWIGEXPORT int
654LLDBSwigPython_GetIndexOfChildWithName
655(
656 PyObject *implementor,
657 const char* child_name
658)
659{
660 PyErr_Cleaner py_err_cleaner(true);
661
662 PyCallable pfunc = PyCallable::FindWithMemberFunction(implementor,"get_child_index");
663
664 if (!pfunc)
665 return UINT32_MAX;
666
667 PyObject *py_return = NULL;
668 py_return = pfunc(child_name);
669
670 if (py_return == NULL || py_return == Py_None)
671 {
672 Py_XDECREF(py_return);
673 return UINT32_MAX;
674 }
675
676 long retval = PyInt_AsLong(py_return);
677 Py_XDECREF(py_return);
678
679 if (retval >= 0)
680 return (uint32_t)retval;
681
682 return UINT32_MAX;
683}
684
685SWIGEXPORT bool
686LLDBSwigPython_UpdateSynthProviderInstance
687(
688 PyObject *implementor
689)
690{
691 bool ret_val = false;
692
693 static char callee_name[] = "update";
694
695 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name);
696
697 if (py_return == Py_True)
698 ret_val = true;
699
700 Py_XDECREF(py_return);
701
702 return ret_val;
703}
704
705SWIGEXPORT bool
706LLDBSwigPython_MightHaveChildrenSynthProviderInstance
707(
708 PyObject *implementor
709)
710{
711 bool ret_val = false;
712
713 static char callee_name[] = "has_children";
714
715 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True);
716
717 if (py_return == Py_True)
718 ret_val = true;
719
720 Py_XDECREF(py_return);
721
722 return ret_val;
723}
724
Enrico Granatad07cfd32014-10-08 18:27:36 +0000725SWIGEXPORT PyObject*
726LLDBSwigPython_GetValueSynthProviderInstance
727(
728 PyObject *implementor
729)
730{
731 PyObject* ret_val = nullptr;
732
733 static char callee_name[] = "get_value";
734
735 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_None);
736
737 if (py_return == Py_None || py_return == nullptr)
738 ret_val = nullptr;
739
740 lldb::SBValue* sbvalue_ptr = NULL;
741
742 if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
743 ret_val = nullptr;
744 else if (sbvalue_ptr == NULL)
745 ret_val = nullptr;
746 else
747 ret_val = py_return;
748
749 Py_XDECREF(py_return);
750 return ret_val;
751}
752
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000753SWIGEXPORT void*
754LLDBSWIGPython_CastPyObjectToSBValue
755(
756 PyObject* data
757)
758{
759 lldb::SBValue* sb_ptr = NULL;
760
761 int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
762
763 if (valid_cast == -1)
764 return NULL;
765
766 return sb_ptr;
767}
768
769// Currently, SBCommandReturnObjectReleaser wraps a unique pointer to an
770// lldb_private::CommandReturnObject. This means that the destructor for the
771// SB object will deallocate its contained CommandReturnObject. Because that
772// object is used as the real return object for Python-based commands, we want
773// it to stay around. Thus, we release the unique pointer before returning from
774// LLDBSwigPythonCallCommand, and to guarantee that the release will occur no
775// matter how we exit from the function, we have a releaser object whose
776// destructor does the right thing for us
777class SBCommandReturnObjectReleaser
778{
779public:
780 SBCommandReturnObjectReleaser (lldb::SBCommandReturnObject &obj) :
781 m_command_return_object_ref (obj)
782 {
783 }
784
785 ~SBCommandReturnObjectReleaser ()
786 {
787 m_command_return_object_ref.Release();
788 }
789private:
790 lldb::SBCommandReturnObject &m_command_return_object_ref;
791};
792
793SWIGEXPORT bool
794LLDBSwigPythonCallCommand
795(
796 const char *python_function_name,
797 const char *session_dictionary_name,
798 lldb::DebuggerSP& debugger,
799 const char* args,
Enrico Granata06be0592014-10-01 21:47:29 +0000800 lldb_private::CommandReturnObject& cmd_retobj,
801 lldb::ExecutionContextRefSP exe_ctx_ref_sp
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000802)
803{
804
805 lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj);
806 SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb);
807 lldb::SBDebugger debugger_sb(debugger);
Enrico Granata06be0592014-10-01 21:47:29 +0000808 lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000809
810 bool retval = false;
811
812 {
813 PyErr_Cleaner py_err_cleaner(true);
814 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
815
816 if (!pfunc)
817 return retval;
818
819 PyObject* session_dict = NULL;
820 // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you
821 // see comment above for SBCommandReturnObjectReleaser for further details
822 PyObject* pvalue = NULL;
Enrico Granata06be0592014-10-01 21:47:29 +0000823
824 PyCallable::argc argc = pfunc.GetNumArguments();
825 if (argc.num_args == 5 || argc.varargs == true)
826 pvalue = pfunc(debugger_sb, args, exe_ctx_sb, &cmd_retobj_sb, session_dict = FindSessionDictionary(session_dictionary_name));
827 else
828 pvalue = pfunc(debugger_sb, args, &cmd_retobj_sb, session_dict = FindSessionDictionary(session_dictionary_name));
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000829
830 Py_XINCREF (session_dict);
831 Py_XDECREF (pvalue);
832
833 retval = true;
834 }
835
836 return retval;
837}
838
839SWIGEXPORT void*
840LLDBSWIGPythonCreateOSPlugin
841(
842 const char *python_class_name,
843 const char *session_dictionary_name,
844 const lldb::ProcessSP& process_sp
845)
846{
847 PyObject* retval = NULL;
848
849 if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
850 Py_RETURN_NONE;
851
852 // I do not want the SBProcess to be deallocated when going out of scope because python
853 // has ownership of it and will manage memory for this object by itself
854 lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp);
855
856 PyObject *SBProc_PyObj = SBTypeToSWIGWrapper(process_sb);
857
858 if (SBProc_PyObj == NULL)
859 Py_RETURN_NONE;
860
861 {
862 PyErr_Cleaner py_err_cleaner(true);
863
864 PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name,session_dictionary_name);
865
866 if (!pfunc)
867 return retval;
868
869 Py_INCREF(SBProc_PyObj);
870
871 PyObject* session_dict = NULL;
872 session_dict = session_dict = FindSessionDictionary(session_dictionary_name);
873 retval = pfunc(SBProc_PyObj);
874
875 Py_XINCREF (session_dict);
876
877 Py_XINCREF(retval);
878 }
879
880 if (retval)
881 return retval;
882 else
883 Py_RETURN_NONE;
884}
885
Enrico Granatac0f8ca02013-10-14 21:39:38 +0000886SWIGEXPORT void*
Greg Claytonef8180a2013-10-15 00:14:28 +0000887LLDBSWIGPython_GetDynamicSetting (void* module, const char* setting, const lldb::TargetSP& target_sp)
Enrico Granatac0f8ca02013-10-14 21:39:38 +0000888{
889
890 if (!module || !setting)
891 Py_RETURN_NONE;
892
893 lldb::SBTarget target_sb(target_sp);
894
895 PyObject *pvalue = NULL;
896
897 {
898 PyErr_Cleaner py_err_cleaner(true);
899 PyCallable pfunc = PyCallable::FindWithFunctionName("get_dynamic_setting",(PyObject *)module);
900
901 if (!pfunc)
902 Py_RETURN_NONE;
903
904 pvalue = pfunc(target_sb, setting);
905 }
906
907 return pvalue;
908}
909
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000910SWIGEXPORT bool
911LLDBSWIGPythonRunScriptKeywordProcess
912(const char* python_function_name,
913const char* session_dictionary_name,
914lldb::ProcessSP& process,
915std::string& output)
916
917{
918 bool retval = false;
919
920 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
921 return retval;
922
923 lldb::SBProcess process_sb(process);
924
925 {
926 PyErr_Cleaner py_err_cleaner(true);
927
928 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
929
930 if (!pfunc)
931 return retval;
932
933 PyObject* session_dict = NULL;
934 PyObject* pvalue = NULL;
935 pvalue = pfunc(process_sb, session_dict = FindSessionDictionary(session_dictionary_name));
936
937 Py_XINCREF (session_dict);
938
939 if (PyObjectToString(pvalue,output))
940 retval = true;
941
942 Py_XDECREF(pvalue);
943 }
944
945 return retval;
946}
947
948SWIGEXPORT bool
949LLDBSWIGPythonRunScriptKeywordThread
950(const char* python_function_name,
951const char* session_dictionary_name,
952lldb::ThreadSP& thread,
953std::string& output)
954
955{
956 bool retval = false;
957
958 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
959 return retval;
960
961 lldb::SBThread thread_sb(thread);
962
963 {
964 PyErr_Cleaner py_err_cleaner(true);
965
966 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
967
968 if (!pfunc)
969 return retval;
970
971 PyObject* session_dict = NULL;
972 PyObject* pvalue = NULL;
973 pvalue = pfunc(thread_sb, session_dict = FindSessionDictionary(session_dictionary_name));
974
975 Py_XINCREF (session_dict);
976
977 if (PyObjectToString(pvalue,output))
978 retval = true;
979
980 Py_XDECREF(pvalue);
981 }
982
983 return retval;
984}
985
986SWIGEXPORT bool
987LLDBSWIGPythonRunScriptKeywordTarget
988(const char* python_function_name,
989const char* session_dictionary_name,
990lldb::TargetSP& target,
991std::string& output)
992
993{
994 bool retval = false;
995
996 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
997 return retval;
998
999 lldb::SBTarget target_sb(target);
1000
1001 {
1002 PyErr_Cleaner py_err_cleaner(true);
1003
1004 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1005
1006 if (!pfunc)
1007 return retval;
1008
1009 PyObject* session_dict = NULL;
1010 PyObject* pvalue = NULL;
1011 pvalue = pfunc(target_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1012
1013 Py_XINCREF (session_dict);
1014
1015 if (PyObjectToString(pvalue,output))
1016 retval = true;
1017
1018 Py_XDECREF(pvalue);
1019 }
1020
1021 return retval;
1022}
1023
1024SWIGEXPORT bool
1025LLDBSWIGPythonRunScriptKeywordFrame
1026(const char* python_function_name,
1027const char* session_dictionary_name,
Jason Molendab57e4a12013-11-04 09:33:30 +00001028lldb::StackFrameSP& frame,
Joerg Sonnenberger340a1752013-09-25 10:37:32 +00001029std::string& output)
1030
1031{
1032 bool retval = false;
1033
1034 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
1035 return retval;
1036
1037 lldb::SBFrame frame_sb(frame);
1038
1039 {
1040 PyErr_Cleaner py_err_cleaner(true);
1041
1042 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1043
1044 if (!pfunc)
1045 return retval;
1046
1047 PyObject* session_dict = NULL;
1048 PyObject* pvalue = NULL;
1049 pvalue = pfunc(frame_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1050
1051 Py_XINCREF (session_dict);
1052
1053 if (PyObjectToString(pvalue,output))
1054 retval = true;
1055
1056 Py_XDECREF(pvalue);
1057 }
1058
1059 return retval;
1060}
1061
1062SWIGEXPORT bool
Enrico Granata88282c62014-10-28 21:07:00 +00001063LLDBSWIGPythonRunScriptKeywordValue
1064(const char* python_function_name,
1065const char* session_dictionary_name,
1066lldb::ValueObjectSP& value,
1067std::string& output)
1068
1069{
1070 bool retval = false;
1071
1072 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
1073 return retval;
1074
1075 lldb::SBValue value_sb(value);
1076
1077 {
1078 PyErr_Cleaner py_err_cleaner(true);
1079
1080 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1081
1082 if (!pfunc)
1083 return retval;
1084
1085 PyObject* session_dict = NULL;
1086 PyObject* pvalue = NULL;
1087 pvalue = pfunc(value_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1088
1089 Py_XINCREF (session_dict);
1090
1091 if (PyObjectToString(pvalue,output))
1092 retval = true;
1093
1094 Py_XDECREF(pvalue);
1095 }
1096
1097 return retval;
1098}
1099
1100SWIGEXPORT bool
Joerg Sonnenberger340a1752013-09-25 10:37:32 +00001101LLDBSwigPythonCallModuleInit
1102(
1103 const char *python_module_name,
1104 const char *session_dictionary_name,
1105 lldb::DebuggerSP& debugger
1106)
1107{
1108 bool retval = false;
1109
1110 lldb::SBDebugger debugger_sb(debugger);
1111
1112 std::string python_function_name_string = python_module_name;
1113 python_function_name_string += ".__lldb_init_module";
1114 const char* python_function_name = python_function_name_string.c_str();
1115
1116 {
1117 PyErr_Cleaner py_err_cleaner(true);
1118
1119 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1120
1121 if (!pfunc)
1122 return true;
1123
1124 PyObject* session_dict = NULL;
1125 PyObject* pvalue = NULL;
1126 pvalue = pfunc(debugger_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1127
1128 Py_XINCREF (session_dict);
1129
1130 retval = true;
1131
1132 Py_XDECREF(pvalue);
1133 }
1134
1135 return retval;
1136}
1137%}
1138
1139
1140%runtime %{
1141// Forward declaration to be inserted at the start of LLDBWrapPython.h
Joerg Sonnenberger340a1752013-09-25 10:37:32 +00001142#include "lldb/API/SBDebugger.h"
1143#include "lldb/API/SBValue.h"
1144
1145SWIGEXPORT lldb::ValueObjectSP
1146LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data)
1147{
1148 lldb::ValueObjectSP valobj_sp;
1149 if (data)
1150 {
1151 lldb::SBValue* sb_ptr = (lldb::SBValue *)data;
1152 valobj_sp = sb_ptr->GetSP();
1153 }
1154 return valobj_sp;
1155}
1156
1157#ifdef __cplusplus
1158extern "C" {
1159#endif
1160
Joerg Sonnenberger340a1752013-09-25 10:37:32 +00001161void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton);
1162
1163#ifdef __cplusplus
1164}
1165#endif
1166%}
1167
1168%wrapper %{
Greg Clayton44d93782014-01-27 23:43:24 +00001169
Joerg Sonnenberger340a1752013-09-25 10:37:32 +00001170
1171// For the LogOutputCallback functions
1172void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) {
1173 if (baton != Py_None) {
1174 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
1175 PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str);
1176 SWIG_PYTHON_THREAD_END_BLOCK;
1177 }
1178}
1179%}