blob: 84375fd1547ebe7d494a4e95f27f8a43fbf91d76 [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,
Enrico Granata7e4df562014-11-22 00:02:47 +0000345 const lldb::TypeSummaryOptionsSP& options_sp,
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000346 std::string& retval
347)
348{
349 lldb::SBValue sb_value (valobj_sp);
Enrico Granata7e4df562014-11-22 00:02:47 +0000350 lldb::SBTypeSummaryOptions sb_options(options_sp.get());
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000351
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 }
Enrico Granata0e0e9f52013-12-26 07:21:41 +0000384
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000385 PyCallable pfunc = PyCallable::FindWithPythonObject(pfunc_impl);
386
387 if (!pfunc)
388 return false;
Enrico Granata7e4df562014-11-22 00:02:47 +0000389
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
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000397 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
408SWIGEXPORT void*
409LLDBSwigPythonCreateSyntheticProvider
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
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000455SWIGEXPORT void*
456LLDBSwigPythonCreateScriptedThreadPlan
457(
458 const char *python_class_name,
459 const char *session_dictionary_name,
460 const lldb::ThreadPlanSP& thread_plan_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 // I do not want the SBThreadPlan to be deallocated when going out of scope because python
469 // has ownership of it and will manage memory for this object by itself
470 lldb::SBThreadPlan *tp_value = new lldb::SBThreadPlan(thread_plan_sp);
471
472 PyObject *ThreadPlan_PyObj = SBTypeToSWIGWrapper(tp_value);
473
474 if (ThreadPlan_PyObj == NULL)
475 Py_RETURN_NONE;
476
477 {
478 PyErr_Cleaner py_err_cleaner(true);
479
480 PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name, session_dictionary_name);
481
482 if (!pfunc)
483 return retval;
484
485 Py_INCREF(ThreadPlan_PyObj);
486
487 PyObject* session_dict = NULL;
488 session_dict = FindSessionDictionary(session_dictionary_name);
489 retval = pfunc(tp_value, session_dict);
490
491 // FIXME: At this point we should check that the class we found supports all the methods
492 // that we need.
493
494 Py_XINCREF (session_dict);
495
496 Py_XINCREF(retval);
497 }
498
499 if (retval)
500 return retval;
501 else
502 Py_RETURN_NONE;
503}
504
505SWIGEXPORT bool
506LLDBSWIGPythonCallThreadPlan
507(
508 void *implementor,
509 const char *method_name,
510 lldb_private::Event *event,
511 bool &got_error
512)
513{
514 bool ret_val = false;
515 got_error = false;
516
517
518 PyErr_Cleaner py_err_cleaner(false);
519
520 PyCallable pfunc = PyCallable::FindWithMemberFunction((PyObject *) implementor, method_name);
521
522 if (!pfunc)
523 {
524 return ret_val;
525 }
526
527 PyObject* py_return = Py_None;
528
529 if (event != NULL)
530 {
531 lldb::SBEvent sb_event(event);
532
533 PyObject *py_obj_event = SBTypeToSWIGWrapper(sb_event);
534
535 py_return = pfunc(py_obj_event);
536 }
537 else
538 {
539 py_return = pfunc();
540 }
541
542 if (PyErr_Occurred())
543 {
544 got_error = true;
545 printf ("Return value was neither false nor true for call to %s.\n", method_name);
546 PyErr_Print();
547 }
548 else
549 {
550 if (py_return == Py_True)
551 ret_val = true;
552 else if (py_return == Py_False)
553 ret_val = false;
554 else
555 {
556 // Somebody returned the wrong thing...
557 got_error = true;
558 printf ("Wrong return value type for call to %s.\n", method_name);
559 }
560 }
561
562 Py_XDECREF(py_return);
563
564 return ret_val;
565}
566
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000567// wrapper that calls an optional instance member of an object taking no arguments
568static PyObject*
569LLDBSwigPython_CallOptionalMember
570(
571 PyObject* self,
572 char* callee_name,
573 PyObject* ret_if_not_found = Py_None,
574 bool* was_found = NULL
575)
576{
577 PyErr_Cleaner py_err_cleaner(false);
578
579 PyCallable pfunc = PyCallable::FindWithMemberFunction(self,callee_name);
580
581 if (!pfunc)
582 {
583 if (was_found)
584 *was_found = false;
585 Py_XINCREF(ret_if_not_found);
586 return ret_if_not_found;
587 }
588
589 if (was_found)
590 *was_found = true;
591
592 PyObject* py_return = pfunc();
593 return py_return;
594}
595
596SWIGEXPORT uint32_t
597LLDBSwigPython_CalculateNumChildren
598(
599 PyObject *implementor
600)
601{
602 uint32_t ret_val = UINT32_MAX;
603
604 static char callee_name[] = "num_children";
605
606 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, NULL);
607
608 if (!py_return)
609 return ret_val;
610
611 if (PyInt_Check(py_return))
612 ret_val = PyInt_AsLong(py_return);
613
614 Py_XDECREF(py_return);
615
616 if (PyErr_Occurred())
617 {
618 PyErr_Print();
619 PyErr_Clear();
620 }
621
622 return ret_val;
623}
624
625SWIGEXPORT PyObject*
626LLDBSwigPython_GetChildAtIndex
627(
628 PyObject *implementor,
629 uint32_t idx
630)
631{
632 PyErr_Cleaner py_err_cleaner(true);
633
634 PyCallable pfunc = PyCallable::FindWithMemberFunction(implementor,"get_child_at_index");
635
636 if (!pfunc)
637 return NULL;
638
639 PyObject *py_return = NULL;
640 py_return = pfunc(idx);
641
642 if (py_return == NULL || py_return == Py_None)
643 {
644 Py_XDECREF(py_return);
645 return NULL;
646 }
647
648 lldb::SBValue* sbvalue_ptr = NULL;
649
650 if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
651 {
652 Py_XDECREF(py_return);
653 return NULL;
654 }
655
656 if (sbvalue_ptr == NULL)
657 return NULL;
658
659 return py_return;
660}
661
662SWIGEXPORT int
663LLDBSwigPython_GetIndexOfChildWithName
664(
665 PyObject *implementor,
666 const char* child_name
667)
668{
669 PyErr_Cleaner py_err_cleaner(true);
670
671 PyCallable pfunc = PyCallable::FindWithMemberFunction(implementor,"get_child_index");
672
673 if (!pfunc)
674 return UINT32_MAX;
675
676 PyObject *py_return = NULL;
677 py_return = pfunc(child_name);
678
679 if (py_return == NULL || py_return == Py_None)
680 {
681 Py_XDECREF(py_return);
682 return UINT32_MAX;
683 }
684
685 long retval = PyInt_AsLong(py_return);
686 Py_XDECREF(py_return);
687
688 if (retval >= 0)
689 return (uint32_t)retval;
690
691 return UINT32_MAX;
692}
693
694SWIGEXPORT bool
695LLDBSwigPython_UpdateSynthProviderInstance
696(
697 PyObject *implementor
698)
699{
700 bool ret_val = false;
701
702 static char callee_name[] = "update";
703
704 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name);
705
706 if (py_return == Py_True)
707 ret_val = true;
708
709 Py_XDECREF(py_return);
710
711 return ret_val;
712}
713
714SWIGEXPORT bool
715LLDBSwigPython_MightHaveChildrenSynthProviderInstance
716(
717 PyObject *implementor
718)
719{
720 bool ret_val = false;
721
722 static char callee_name[] = "has_children";
723
724 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True);
725
726 if (py_return == Py_True)
727 ret_val = true;
728
729 Py_XDECREF(py_return);
730
731 return ret_val;
732}
733
Enrico Granatad07cfd32014-10-08 18:27:36 +0000734SWIGEXPORT PyObject*
735LLDBSwigPython_GetValueSynthProviderInstance
736(
737 PyObject *implementor
738)
739{
740 PyObject* ret_val = nullptr;
741
742 static char callee_name[] = "get_value";
743
744 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_None);
745
746 if (py_return == Py_None || py_return == nullptr)
747 ret_val = nullptr;
748
749 lldb::SBValue* sbvalue_ptr = NULL;
750
751 if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
752 ret_val = nullptr;
753 else if (sbvalue_ptr == NULL)
754 ret_val = nullptr;
755 else
756 ret_val = py_return;
757
758 Py_XDECREF(py_return);
759 return ret_val;
760}
761
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000762SWIGEXPORT void*
763LLDBSWIGPython_CastPyObjectToSBValue
764(
765 PyObject* data
766)
767{
768 lldb::SBValue* sb_ptr = NULL;
769
770 int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
771
772 if (valid_cast == -1)
773 return NULL;
774
775 return sb_ptr;
776}
777
778// Currently, SBCommandReturnObjectReleaser wraps a unique pointer to an
779// lldb_private::CommandReturnObject. This means that the destructor for the
780// SB object will deallocate its contained CommandReturnObject. Because that
781// object is used as the real return object for Python-based commands, we want
782// it to stay around. Thus, we release the unique pointer before returning from
783// LLDBSwigPythonCallCommand, and to guarantee that the release will occur no
784// matter how we exit from the function, we have a releaser object whose
785// destructor does the right thing for us
786class SBCommandReturnObjectReleaser
787{
788public:
789 SBCommandReturnObjectReleaser (lldb::SBCommandReturnObject &obj) :
790 m_command_return_object_ref (obj)
791 {
792 }
793
794 ~SBCommandReturnObjectReleaser ()
795 {
796 m_command_return_object_ref.Release();
797 }
798private:
799 lldb::SBCommandReturnObject &m_command_return_object_ref;
800};
801
802SWIGEXPORT bool
803LLDBSwigPythonCallCommand
804(
805 const char *python_function_name,
806 const char *session_dictionary_name,
807 lldb::DebuggerSP& debugger,
808 const char* args,
Enrico Granata06be0592014-10-01 21:47:29 +0000809 lldb_private::CommandReturnObject& cmd_retobj,
810 lldb::ExecutionContextRefSP exe_ctx_ref_sp
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000811)
812{
813
814 lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj);
815 SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb);
816 lldb::SBDebugger debugger_sb(debugger);
Enrico Granata06be0592014-10-01 21:47:29 +0000817 lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000818
819 bool retval = false;
820
821 {
822 PyErr_Cleaner py_err_cleaner(true);
823 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
824
825 if (!pfunc)
826 return retval;
827
828 PyObject* session_dict = NULL;
829 // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you
830 // see comment above for SBCommandReturnObjectReleaser for further details
831 PyObject* pvalue = NULL;
Enrico Granata06be0592014-10-01 21:47:29 +0000832
833 PyCallable::argc argc = pfunc.GetNumArguments();
834 if (argc.num_args == 5 || argc.varargs == true)
835 pvalue = pfunc(debugger_sb, args, exe_ctx_sb, &cmd_retobj_sb, session_dict = FindSessionDictionary(session_dictionary_name));
836 else
837 pvalue = pfunc(debugger_sb, args, &cmd_retobj_sb, session_dict = FindSessionDictionary(session_dictionary_name));
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000838
839 Py_XINCREF (session_dict);
840 Py_XDECREF (pvalue);
841
842 retval = true;
843 }
844
845 return retval;
846}
847
848SWIGEXPORT void*
849LLDBSWIGPythonCreateOSPlugin
850(
851 const char *python_class_name,
852 const char *session_dictionary_name,
853 const lldb::ProcessSP& process_sp
854)
855{
856 PyObject* retval = NULL;
857
858 if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
859 Py_RETURN_NONE;
860
861 // I do not want the SBProcess to be deallocated when going out of scope because python
862 // has ownership of it and will manage memory for this object by itself
863 lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp);
864
865 PyObject *SBProc_PyObj = SBTypeToSWIGWrapper(process_sb);
866
867 if (SBProc_PyObj == NULL)
868 Py_RETURN_NONE;
869
870 {
871 PyErr_Cleaner py_err_cleaner(true);
872
873 PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name,session_dictionary_name);
874
875 if (!pfunc)
876 return retval;
877
878 Py_INCREF(SBProc_PyObj);
879
880 PyObject* session_dict = NULL;
881 session_dict = session_dict = FindSessionDictionary(session_dictionary_name);
882 retval = pfunc(SBProc_PyObj);
883
884 Py_XINCREF (session_dict);
885
886 Py_XINCREF(retval);
887 }
888
889 if (retval)
890 return retval;
891 else
892 Py_RETURN_NONE;
893}
894
Enrico Granatac0f8ca02013-10-14 21:39:38 +0000895SWIGEXPORT void*
Greg Claytonef8180a2013-10-15 00:14:28 +0000896LLDBSWIGPython_GetDynamicSetting (void* module, const char* setting, const lldb::TargetSP& target_sp)
Enrico Granatac0f8ca02013-10-14 21:39:38 +0000897{
898
899 if (!module || !setting)
900 Py_RETURN_NONE;
901
902 lldb::SBTarget target_sb(target_sp);
903
904 PyObject *pvalue = NULL;
905
906 {
907 PyErr_Cleaner py_err_cleaner(true);
908 PyCallable pfunc = PyCallable::FindWithFunctionName("get_dynamic_setting",(PyObject *)module);
909
910 if (!pfunc)
911 Py_RETURN_NONE;
912
913 pvalue = pfunc(target_sb, setting);
914 }
915
916 return pvalue;
917}
918
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000919SWIGEXPORT bool
920LLDBSWIGPythonRunScriptKeywordProcess
921(const char* python_function_name,
922const char* session_dictionary_name,
923lldb::ProcessSP& process,
924std::string& output)
925
926{
927 bool retval = false;
928
929 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
930 return retval;
931
932 lldb::SBProcess process_sb(process);
933
934 {
935 PyErr_Cleaner py_err_cleaner(true);
936
937 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
938
939 if (!pfunc)
940 return retval;
941
942 PyObject* session_dict = NULL;
943 PyObject* pvalue = NULL;
944 pvalue = pfunc(process_sb, session_dict = FindSessionDictionary(session_dictionary_name));
945
946 Py_XINCREF (session_dict);
947
948 if (PyObjectToString(pvalue,output))
949 retval = true;
950
951 Py_XDECREF(pvalue);
952 }
953
954 return retval;
955}
956
957SWIGEXPORT bool
958LLDBSWIGPythonRunScriptKeywordThread
959(const char* python_function_name,
960const char* session_dictionary_name,
961lldb::ThreadSP& thread,
962std::string& output)
963
964{
965 bool retval = false;
966
967 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
968 return retval;
969
970 lldb::SBThread thread_sb(thread);
971
972 {
973 PyErr_Cleaner py_err_cleaner(true);
974
975 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
976
977 if (!pfunc)
978 return retval;
979
980 PyObject* session_dict = NULL;
981 PyObject* pvalue = NULL;
982 pvalue = pfunc(thread_sb, session_dict = FindSessionDictionary(session_dictionary_name));
983
984 Py_XINCREF (session_dict);
985
986 if (PyObjectToString(pvalue,output))
987 retval = true;
988
989 Py_XDECREF(pvalue);
990 }
991
992 return retval;
993}
994
995SWIGEXPORT bool
996LLDBSWIGPythonRunScriptKeywordTarget
997(const char* python_function_name,
998const char* session_dictionary_name,
999lldb::TargetSP& target,
1000std::string& output)
1001
1002{
1003 bool retval = false;
1004
1005 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
1006 return retval;
1007
1008 lldb::SBTarget target_sb(target);
1009
1010 {
1011 PyErr_Cleaner py_err_cleaner(true);
1012
1013 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1014
1015 if (!pfunc)
1016 return retval;
1017
1018 PyObject* session_dict = NULL;
1019 PyObject* pvalue = NULL;
1020 pvalue = pfunc(target_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1021
1022 Py_XINCREF (session_dict);
1023
1024 if (PyObjectToString(pvalue,output))
1025 retval = true;
1026
1027 Py_XDECREF(pvalue);
1028 }
1029
1030 return retval;
1031}
1032
1033SWIGEXPORT bool
1034LLDBSWIGPythonRunScriptKeywordFrame
1035(const char* python_function_name,
1036const char* session_dictionary_name,
Jason Molendab57e4a12013-11-04 09:33:30 +00001037lldb::StackFrameSP& frame,
Joerg Sonnenberger340a1752013-09-25 10:37:32 +00001038std::string& output)
1039
1040{
1041 bool retval = false;
1042
1043 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
1044 return retval;
1045
1046 lldb::SBFrame frame_sb(frame);
1047
1048 {
1049 PyErr_Cleaner py_err_cleaner(true);
1050
1051 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1052
1053 if (!pfunc)
1054 return retval;
1055
1056 PyObject* session_dict = NULL;
1057 PyObject* pvalue = NULL;
1058 pvalue = pfunc(frame_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1059
1060 Py_XINCREF (session_dict);
1061
1062 if (PyObjectToString(pvalue,output))
1063 retval = true;
1064
1065 Py_XDECREF(pvalue);
1066 }
1067
1068 return retval;
1069}
1070
1071SWIGEXPORT bool
Enrico Granata88282c62014-10-28 21:07:00 +00001072LLDBSWIGPythonRunScriptKeywordValue
1073(const char* python_function_name,
1074const char* session_dictionary_name,
1075lldb::ValueObjectSP& value,
1076std::string& output)
1077
1078{
1079 bool retval = false;
1080
1081 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
1082 return retval;
1083
1084 lldb::SBValue value_sb(value);
1085
1086 {
1087 PyErr_Cleaner py_err_cleaner(true);
1088
1089 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1090
1091 if (!pfunc)
1092 return retval;
1093
1094 PyObject* session_dict = NULL;
1095 PyObject* pvalue = NULL;
1096 pvalue = pfunc(value_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1097
1098 Py_XINCREF (session_dict);
1099
1100 if (PyObjectToString(pvalue,output))
1101 retval = true;
1102
1103 Py_XDECREF(pvalue);
1104 }
1105
1106 return retval;
1107}
1108
1109SWIGEXPORT bool
Joerg Sonnenberger340a1752013-09-25 10:37:32 +00001110LLDBSwigPythonCallModuleInit
1111(
1112 const char *python_module_name,
1113 const char *session_dictionary_name,
1114 lldb::DebuggerSP& debugger
1115)
1116{
1117 bool retval = false;
1118
1119 lldb::SBDebugger debugger_sb(debugger);
1120
1121 std::string python_function_name_string = python_module_name;
1122 python_function_name_string += ".__lldb_init_module";
1123 const char* python_function_name = python_function_name_string.c_str();
1124
1125 {
1126 PyErr_Cleaner py_err_cleaner(true);
1127
1128 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
1129
1130 if (!pfunc)
1131 return true;
1132
1133 PyObject* session_dict = NULL;
1134 PyObject* pvalue = NULL;
1135 pvalue = pfunc(debugger_sb, session_dict = FindSessionDictionary(session_dictionary_name));
1136
1137 Py_XINCREF (session_dict);
1138
1139 retval = true;
1140
1141 Py_XDECREF(pvalue);
1142 }
1143
1144 return retval;
1145}
1146%}
1147
1148
1149%runtime %{
1150// Forward declaration to be inserted at the start of LLDBWrapPython.h
Joerg Sonnenberger340a1752013-09-25 10:37:32 +00001151#include "lldb/API/SBDebugger.h"
1152#include "lldb/API/SBValue.h"
1153
1154SWIGEXPORT lldb::ValueObjectSP
1155LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data)
1156{
1157 lldb::ValueObjectSP valobj_sp;
1158 if (data)
1159 {
1160 lldb::SBValue* sb_ptr = (lldb::SBValue *)data;
1161 valobj_sp = sb_ptr->GetSP();
1162 }
1163 return valobj_sp;
1164}
1165
1166#ifdef __cplusplus
1167extern "C" {
1168#endif
1169
Joerg Sonnenberger340a1752013-09-25 10:37:32 +00001170void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton);
1171
1172#ifdef __cplusplus
1173}
1174#endif
1175%}
1176
1177%wrapper %{
Greg Clayton44d93782014-01-27 23:43:24 +00001178
Joerg Sonnenberger340a1752013-09-25 10:37:32 +00001179
1180// For the LogOutputCallback functions
1181void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) {
1182 if (baton != Py_None) {
1183 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
1184 PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str);
1185 SWIG_PYTHON_THREAD_END_BLOCK;
1186 }
1187}
1188%}