blob: cd133cfa8a2782444b8e6f967831c211c8ad9f24 [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 {
19 if(m_print)
20 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:
108
109 operator
110 bool ()
111 {
112 return m_callable != NULL;
113 }
114
115 template<typename ...Args>
116 PyObject*
117 operator () (Args... args)
118 {
119 return (*this)({SBTypeToSWIGWrapper(args)...});
120 }
121
122 PyObject*
123 operator () (std::initializer_list<PyObject*> args)
124 {
125 PyObject* retval = NULL;
126 PyObject* pargs = PyTuple_New (args.size());
127 if (pargs == NULL)
128 {
129 if (PyErr_Occurred())
130 PyErr_Clear();
131 return retval;
132 }
133 size_t idx = 0;
134 for (auto arg : args)
135 {
136 if (!arg)
137 return retval;
138 PyTuple_SetItem(pargs,idx,arg);
139 idx++;
140 }
141 retval = PyObject_CallObject (m_callable, pargs);
142 Py_XDECREF (pargs);
143 return retval;
144 }
145
146 static PyCallable
147 FindWithPythonObject (PyObject* pfunc)
148 {
149 return PyCallable(pfunc);
150 }
151
152 static PyCallable
153 FindWithFunctionName (const char *python_function_name,
154 const char *session_dictionary_name)
155 {
156 if (!python_function_name || !session_dictionary_name)
157 return PyCallable();
158 if ( (python_function_name[0] == 0) || (session_dictionary_name[0] == 0) )
159 return PyCallable();
160 return FindWithFunctionName(python_function_name,FindSessionDictionary (session_dictionary_name));
161 }
162
163 static PyCallable
164 FindWithFunctionName (const char *python_function_name,
165 PyObject *session_dict)
166 {
167 if (!python_function_name || !session_dict)
168 return PyCallable();
169 if ( (python_function_name[0] == 0))
170 return PyCallable();
171 return PyCallable(ResolvePythonName (python_function_name, session_dict));
172 }
173
174 static PyCallable
175 FindWithMemberFunction (PyObject *self,
176 const char *python_function_name)
177 {
178 if (self == NULL || self == Py_None)
179 return PyCallable();
180 if (!python_function_name || (python_function_name[0] == 0))
181 return PyCallable();
182 return PyCallable(PyObject_GetAttrString(self, python_function_name));
183 }
184
185private:
186 PyObject* m_callable;
187
188 PyCallable (PyObject *callable = NULL) :
189 m_callable(callable)
190 {
191 if (m_callable && PyCallable_Check(m_callable) == false)
192 m_callable = NULL;
193 }
194};
195
196%}
197
198%wrapper %{
199
200// resolve a dotted Python name in the form
201// foo.bar.baz.Foobar to an actual Python object
202// if pmodule is NULL, the __main__ module will be used
203// as the starting point for the search
204
205
206// This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...)
207// and is used when a script command is attached to a breakpoint for execution.
208
209SWIGEXPORT bool
210LLDBSwigPythonBreakpointCallbackFunction
211(
212 const char *python_function_name,
213 const char *session_dictionary_name,
214 const lldb::StackFrameSP& frame_sp,
215 const lldb::BreakpointLocationSP& bp_loc_sp
216)
217{
218 lldb::SBFrame sb_frame (frame_sp);
219 lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
220
221 bool stop_at_breakpoint = true;
222
223 {
224 PyErr_Cleaner py_err_cleaner(true);
225
226 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
227
228 if (!pfunc)
229 return stop_at_breakpoint;
230
231 PyObject* session_dict = NULL;
232 PyObject* pvalue = NULL;
233 pvalue = pfunc(sb_frame, sb_bp_loc, session_dict = FindSessionDictionary(session_dictionary_name));
234
235 Py_XINCREF (session_dict);
236
237 if (pvalue == Py_False)
238 stop_at_breakpoint = false;
239
240 Py_XDECREF (pvalue);
241 }
242
243 return stop_at_breakpoint;
244}
245
246// This function is called by lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...)
247// and is used when a script command is attached to a watchpoint for execution.
248
249SWIGEXPORT bool
250LLDBSwigPythonWatchpointCallbackFunction
251(
252 const char *python_function_name,
253 const char *session_dictionary_name,
254 const lldb::StackFrameSP& frame_sp,
255 const lldb::WatchpointSP& wp_sp
256)
257{
258 lldb::SBFrame sb_frame (frame_sp);
259 lldb::SBWatchpoint sb_wp(wp_sp);
260
261 bool stop_at_watchpoint = true;
262
263 {
264 PyErr_Cleaner py_err_cleaner(true);
265
266 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
267
268 if (!pfunc)
269 return stop_at_watchpoint;
270
271 PyObject* session_dict = NULL;
272 PyObject* pvalue = NULL;
273 pvalue = pfunc(sb_frame, sb_wp, session_dict = FindSessionDictionary(session_dictionary_name));
274
275 Py_XINCREF (session_dict);
276
277 if (pvalue == Py_False)
278 stop_at_watchpoint = false;
279
280 Py_XDECREF (pvalue);
281 }
282
283 return stop_at_watchpoint;
284}
285
286bool
287PyObjectToString (PyObject* object,
288 std::string& retval)
289{
290 retval.clear();
291 bool was_ok = false;
292 if (object != NULL && object != Py_None)
293 {
294 if (PyString_Check(object))
295 {
296 retval.assign(PyString_AsString(object));
297 was_ok = true;
298 }
299 else
300 {
301 PyObject* value_as_string = PyObject_Str(object);
302 if (value_as_string && value_as_string != Py_None && PyString_Check(value_as_string))
303 {
304 retval.assign(PyString_AsString(value_as_string));
305 was_ok = true;
306 }
307 Py_XDECREF(value_as_string);
308 }
309 }
310 return was_ok;
311}
312
313SWIGEXPORT bool
314LLDBSwigPythonCallTypeScript
315(
316 const char *python_function_name,
317 const void *session_dictionary,
318 const lldb::ValueObjectSP& valobj_sp,
319 void** pyfunct_wrapper,
320 std::string& retval
321)
322{
323 lldb::SBValue sb_value (valobj_sp);
324
325 retval.clear();
326
327 if (!python_function_name || !session_dictionary)
328 return false;
329
330 PyObject *session_dict = (PyObject*)session_dictionary, *pfunc_impl = NULL, *pvalue = NULL;
331
332 if (pyfunct_wrapper && *pyfunct_wrapper && PyFunction_Check (*pyfunct_wrapper))
333 {
334 pfunc_impl = (PyObject*)(*pyfunct_wrapper);
335 if (pfunc_impl->ob_refcnt == 1)
336 {
337 Py_XDECREF(pfunc_impl);
338 pfunc_impl = NULL;
339 }
340 }
341
342 if (PyDict_Check(session_dict))
343 {
344 PyErr_Cleaner pyerr_cleanup(true); // show Python errors
345
346 if (!pfunc_impl)
347 {
348 pfunc_impl = ResolvePythonName (python_function_name, session_dict);
349 if (!pfunc_impl || !PyCallable_Check (pfunc_impl))
350 return false;
351 else
352 {
353 if (pyfunct_wrapper)
354 *pyfunct_wrapper = pfunc_impl;
355 }
356 }
357 /*else
358 printf("caching works!!!!\n");*/
359
360 PyCallable pfunc = PyCallable::FindWithPythonObject(pfunc_impl);
361
362 if (!pfunc)
363 return false;
364
365 pvalue = pfunc(sb_value,session_dict);
366
367 Py_INCREF (session_dict);
368
369 PyObjectToString(pvalue,retval);
370
371 Py_XDECREF (pvalue);
372 }
373 return true;
374}
375
376SWIGEXPORT void*
377LLDBSwigPythonCreateSyntheticProvider
378(
379 const char *python_class_name,
380 const char *session_dictionary_name,
381 const lldb::ValueObjectSP& valobj_sp
382)
383{
384 PyObject* retval = NULL;
385
386 if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
387 Py_RETURN_NONE;
388
389 // I do not want the SBValue to be deallocated when going out of scope because python
390 // has ownership of it and will manage memory for this object by itself
391 lldb::SBValue *sb_value = new lldb::SBValue(valobj_sp);
392 sb_value->SetPreferSyntheticValue(false);
393 PyObject *ValObj_PyObj = SBTypeToSWIGWrapper(sb_value);
394
395 if (ValObj_PyObj == NULL)
396 Py_RETURN_NONE;
397
398 {
399 PyErr_Cleaner py_err_cleaner(true);
400
401 PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name,session_dictionary_name);
402
403 if (!pfunc)
404 return retval;
405
406 Py_INCREF(ValObj_PyObj);
407
408 PyObject* session_dict = NULL;
409 session_dict = FindSessionDictionary(session_dictionary_name);
410 retval = pfunc(sb_value, session_dict);
411
412 Py_XINCREF (session_dict);
413
414 Py_XINCREF(retval);
415 }
416
417 if (retval)
418 return retval;
419 else
420 Py_RETURN_NONE;
421}
422
423// wrapper that calls an optional instance member of an object taking no arguments
424static PyObject*
425LLDBSwigPython_CallOptionalMember
426(
427 PyObject* self,
428 char* callee_name,
429 PyObject* ret_if_not_found = Py_None,
430 bool* was_found = NULL
431)
432{
433 PyErr_Cleaner py_err_cleaner(false);
434
435 PyCallable pfunc = PyCallable::FindWithMemberFunction(self,callee_name);
436
437 if (!pfunc)
438 {
439 if (was_found)
440 *was_found = false;
441 Py_XINCREF(ret_if_not_found);
442 return ret_if_not_found;
443 }
444
445 if (was_found)
446 *was_found = true;
447
448 PyObject* py_return = pfunc();
449 return py_return;
450}
451
452SWIGEXPORT uint32_t
453LLDBSwigPython_CalculateNumChildren
454(
455 PyObject *implementor
456)
457{
458 uint32_t ret_val = UINT32_MAX;
459
460 static char callee_name[] = "num_children";
461
462 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, NULL);
463
464 if (!py_return)
465 return ret_val;
466
467 if (PyInt_Check(py_return))
468 ret_val = PyInt_AsLong(py_return);
469
470 Py_XDECREF(py_return);
471
472 if (PyErr_Occurred())
473 {
474 PyErr_Print();
475 PyErr_Clear();
476 }
477
478 return ret_val;
479}
480
481SWIGEXPORT PyObject*
482LLDBSwigPython_GetChildAtIndex
483(
484 PyObject *implementor,
485 uint32_t idx
486)
487{
488 PyErr_Cleaner py_err_cleaner(true);
489
490 PyCallable pfunc = PyCallable::FindWithMemberFunction(implementor,"get_child_at_index");
491
492 if (!pfunc)
493 return NULL;
494
495 PyObject *py_return = NULL;
496 py_return = pfunc(idx);
497
498 if (py_return == NULL || py_return == Py_None)
499 {
500 Py_XDECREF(py_return);
501 return NULL;
502 }
503
504 lldb::SBValue* sbvalue_ptr = NULL;
505
506 if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
507 {
508 Py_XDECREF(py_return);
509 return NULL;
510 }
511
512 if (sbvalue_ptr == NULL)
513 return NULL;
514
515 return py_return;
516}
517
518SWIGEXPORT int
519LLDBSwigPython_GetIndexOfChildWithName
520(
521 PyObject *implementor,
522 const char* child_name
523)
524{
525 PyErr_Cleaner py_err_cleaner(true);
526
527 PyCallable pfunc = PyCallable::FindWithMemberFunction(implementor,"get_child_index");
528
529 if (!pfunc)
530 return UINT32_MAX;
531
532 PyObject *py_return = NULL;
533 py_return = pfunc(child_name);
534
535 if (py_return == NULL || py_return == Py_None)
536 {
537 Py_XDECREF(py_return);
538 return UINT32_MAX;
539 }
540
541 long retval = PyInt_AsLong(py_return);
542 Py_XDECREF(py_return);
543
544 if (retval >= 0)
545 return (uint32_t)retval;
546
547 return UINT32_MAX;
548}
549
550SWIGEXPORT bool
551LLDBSwigPython_UpdateSynthProviderInstance
552(
553 PyObject *implementor
554)
555{
556 bool ret_val = false;
557
558 static char callee_name[] = "update";
559
560 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name);
561
562 if (py_return == Py_True)
563 ret_val = true;
564
565 Py_XDECREF(py_return);
566
567 return ret_val;
568}
569
570SWIGEXPORT bool
571LLDBSwigPython_MightHaveChildrenSynthProviderInstance
572(
573 PyObject *implementor
574)
575{
576 bool ret_val = false;
577
578 static char callee_name[] = "has_children";
579
580 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True);
581
582 if (py_return == Py_True)
583 ret_val = true;
584
585 Py_XDECREF(py_return);
586
587 return ret_val;
588}
589
590SWIGEXPORT void*
591LLDBSWIGPython_CastPyObjectToSBValue
592(
593 PyObject* data
594)
595{
596 lldb::SBValue* sb_ptr = NULL;
597
598 int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
599
600 if (valid_cast == -1)
601 return NULL;
602
603 return sb_ptr;
604}
605
606// Currently, SBCommandReturnObjectReleaser wraps a unique pointer to an
607// lldb_private::CommandReturnObject. This means that the destructor for the
608// SB object will deallocate its contained CommandReturnObject. Because that
609// object is used as the real return object for Python-based commands, we want
610// it to stay around. Thus, we release the unique pointer before returning from
611// LLDBSwigPythonCallCommand, and to guarantee that the release will occur no
612// matter how we exit from the function, we have a releaser object whose
613// destructor does the right thing for us
614class SBCommandReturnObjectReleaser
615{
616public:
617 SBCommandReturnObjectReleaser (lldb::SBCommandReturnObject &obj) :
618 m_command_return_object_ref (obj)
619 {
620 }
621
622 ~SBCommandReturnObjectReleaser ()
623 {
624 m_command_return_object_ref.Release();
625 }
626private:
627 lldb::SBCommandReturnObject &m_command_return_object_ref;
628};
629
630SWIGEXPORT bool
631LLDBSwigPythonCallCommand
632(
633 const char *python_function_name,
634 const char *session_dictionary_name,
635 lldb::DebuggerSP& debugger,
636 const char* args,
637 lldb_private::CommandReturnObject& cmd_retobj
638)
639{
640
641 lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj);
642 SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb);
643 lldb::SBDebugger debugger_sb(debugger);
644
645 bool retval = false;
646
647 {
648 PyErr_Cleaner py_err_cleaner(true);
649 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
650
651 if (!pfunc)
652 return retval;
653
654 PyObject* session_dict = NULL;
655 // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you
656 // see comment above for SBCommandReturnObjectReleaser for further details
657 PyObject* pvalue = NULL;
658 pvalue = pfunc(debugger_sb, args, &cmd_retobj_sb, session_dict = FindSessionDictionary(session_dictionary_name));
659
660 Py_XINCREF (session_dict);
661 Py_XDECREF (pvalue);
662
663 retval = true;
664 }
665
666 return retval;
667}
668
669SWIGEXPORT void*
670LLDBSWIGPythonCreateOSPlugin
671(
672 const char *python_class_name,
673 const char *session_dictionary_name,
674 const lldb::ProcessSP& process_sp
675)
676{
677 PyObject* retval = NULL;
678
679 if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
680 Py_RETURN_NONE;
681
682 // I do not want the SBProcess to be deallocated when going out of scope because python
683 // has ownership of it and will manage memory for this object by itself
684 lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp);
685
686 PyObject *SBProc_PyObj = SBTypeToSWIGWrapper(process_sb);
687
688 if (SBProc_PyObj == NULL)
689 Py_RETURN_NONE;
690
691 {
692 PyErr_Cleaner py_err_cleaner(true);
693
694 PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name,session_dictionary_name);
695
696 if (!pfunc)
697 return retval;
698
699 Py_INCREF(SBProc_PyObj);
700
701 PyObject* session_dict = NULL;
702 session_dict = session_dict = FindSessionDictionary(session_dictionary_name);
703 retval = pfunc(SBProc_PyObj);
704
705 Py_XINCREF (session_dict);
706
707 Py_XINCREF(retval);
708 }
709
710 if (retval)
711 return retval;
712 else
713 Py_RETURN_NONE;
714}
715
716SWIGEXPORT bool
717LLDBSWIGPythonRunScriptKeywordProcess
718(const char* python_function_name,
719const char* session_dictionary_name,
720lldb::ProcessSP& process,
721std::string& output)
722
723{
724 bool retval = false;
725
726 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
727 return retval;
728
729 lldb::SBProcess process_sb(process);
730
731 {
732 PyErr_Cleaner py_err_cleaner(true);
733
734 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
735
736 if (!pfunc)
737 return retval;
738
739 PyObject* session_dict = NULL;
740 PyObject* pvalue = NULL;
741 pvalue = pfunc(process_sb, session_dict = FindSessionDictionary(session_dictionary_name));
742
743 Py_XINCREF (session_dict);
744
745 if (PyObjectToString(pvalue,output))
746 retval = true;
747
748 Py_XDECREF(pvalue);
749 }
750
751 return retval;
752}
753
754SWIGEXPORT bool
755LLDBSWIGPythonRunScriptKeywordThread
756(const char* python_function_name,
757const char* session_dictionary_name,
758lldb::ThreadSP& thread,
759std::string& output)
760
761{
762 bool retval = false;
763
764 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
765 return retval;
766
767 lldb::SBThread thread_sb(thread);
768
769 {
770 PyErr_Cleaner py_err_cleaner(true);
771
772 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
773
774 if (!pfunc)
775 return retval;
776
777 PyObject* session_dict = NULL;
778 PyObject* pvalue = NULL;
779 pvalue = pfunc(thread_sb, session_dict = FindSessionDictionary(session_dictionary_name));
780
781 Py_XINCREF (session_dict);
782
783 if (PyObjectToString(pvalue,output))
784 retval = true;
785
786 Py_XDECREF(pvalue);
787 }
788
789 return retval;
790}
791
792SWIGEXPORT bool
793LLDBSWIGPythonRunScriptKeywordTarget
794(const char* python_function_name,
795const char* session_dictionary_name,
796lldb::TargetSP& target,
797std::string& output)
798
799{
800 bool retval = false;
801
802 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
803 return retval;
804
805 lldb::SBTarget target_sb(target);
806
807 {
808 PyErr_Cleaner py_err_cleaner(true);
809
810 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
811
812 if (!pfunc)
813 return retval;
814
815 PyObject* session_dict = NULL;
816 PyObject* pvalue = NULL;
817 pvalue = pfunc(target_sb, session_dict = FindSessionDictionary(session_dictionary_name));
818
819 Py_XINCREF (session_dict);
820
821 if (PyObjectToString(pvalue,output))
822 retval = true;
823
824 Py_XDECREF(pvalue);
825 }
826
827 return retval;
828}
829
830SWIGEXPORT bool
831LLDBSWIGPythonRunScriptKeywordFrame
832(const char* python_function_name,
833const char* session_dictionary_name,
834lldb::StackFrameSP& frame,
835std::string& output)
836
837{
838 bool retval = false;
839
840 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
841 return retval;
842
843 lldb::SBFrame frame_sb(frame);
844
845 {
846 PyErr_Cleaner py_err_cleaner(true);
847
848 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
849
850 if (!pfunc)
851 return retval;
852
853 PyObject* session_dict = NULL;
854 PyObject* pvalue = NULL;
855 pvalue = pfunc(frame_sb, session_dict = FindSessionDictionary(session_dictionary_name));
856
857 Py_XINCREF (session_dict);
858
859 if (PyObjectToString(pvalue,output))
860 retval = true;
861
862 Py_XDECREF(pvalue);
863 }
864
865 return retval;
866}
867
868SWIGEXPORT bool
869LLDBSwigPythonCallModuleInit
870(
871 const char *python_module_name,
872 const char *session_dictionary_name,
873 lldb::DebuggerSP& debugger
874)
875{
876 bool retval = false;
877
878 lldb::SBDebugger debugger_sb(debugger);
879
880 std::string python_function_name_string = python_module_name;
881 python_function_name_string += ".__lldb_init_module";
882 const char* python_function_name = python_function_name_string.c_str();
883
884 {
885 PyErr_Cleaner py_err_cleaner(true);
886
887 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
888
889 if (!pfunc)
890 return true;
891
892 PyObject* session_dict = NULL;
893 PyObject* pvalue = NULL;
894 pvalue = pfunc(debugger_sb, session_dict = FindSessionDictionary(session_dictionary_name));
895
896 Py_XINCREF (session_dict);
897
898 retval = true;
899
900 Py_XDECREF(pvalue);
901 }
902
903 return retval;
904}
905%}
906
907
908%runtime %{
909// Forward declaration to be inserted at the start of LLDBWrapPython.h
910// I used runtime as a hack to make SWIG place it where it's needed.
911// This is needed to use LLDBSwigPythonCallSBInputReaderCallback in the
912// typemaps and in the extensions (SBInputReader.__del__()).
913#include "lldb/API/SBInputReader.h"
914#include "lldb/API/SBDebugger.h"
915#include "lldb/API/SBValue.h"
916
917SWIGEXPORT lldb::ValueObjectSP
918LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data)
919{
920 lldb::ValueObjectSP valobj_sp;
921 if (data)
922 {
923 lldb::SBValue* sb_ptr = (lldb::SBValue *)data;
924 valobj_sp = sb_ptr->GetSP();
925 }
926 return valobj_sp;
927}
928
929#ifdef __cplusplus
930extern "C" {
931#endif
932
933size_t
934LLDBSwigPythonCallSBInputReaderCallback(void *baton,
935 lldb::SBInputReader *reader,
936 lldb::InputReaderAction notification,
937 const char*bytes,
938 size_t bytes_len);
939
940void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton);
941
942#ifdef __cplusplus
943}
944#endif
945%}
946
947%wrapper %{
948// For the InputReader Callback functions
949SWIGEXPORT size_t
950LLDBSwigPythonCallSBInputReaderCallback(void *baton,
951 lldb::SBInputReader *reader,
952 lldb::InputReaderAction notification,
953 const char*bytes,
954 size_t bytes_len) {
955 if (baton != Py_None) {
956 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
957
958 PyObject *py_InputReader = SBTypeToSWIGWrapper(reader);
959 PyObject *py_Notification = PyInt_FromLong(notification);
960 PyObject *py_Bytes = PyBytes_FromStringAndSize(bytes, bytes_len);
961
962 PyObject *tuple = PyTuple_Pack(3, py_InputReader, py_Notification, py_Bytes);
963 PyObject *res = PyObject_Call(reinterpret_cast<PyObject*>(baton), tuple, NULL);
964 Py_XDECREF(tuple);
965 Py_XDECREF(py_InputReader);
966 Py_XDECREF(py_Notification);
967 Py_XDECREF(py_Bytes);
968
969 if (res == NULL) {
970 PyObject *exc = PyErr_Occurred();
971 if (exc) {
972 ::puts("\nErroring out at LLDBSwigPythonCallSBInputReaderCallback");
973 PyErr_Print();
974 }
975 return 0;
976 }
977
978 size_t result = 0;
979 // If the callback misbehaves and returns Py_None, assume it returned 0
980 if (res != Py_None)
981 result = static_cast<size_t>(PyInt_AsSsize_t(res));
982
983 Py_XDECREF(res);
984 SWIG_PYTHON_THREAD_END_BLOCK;
985 return result;
986 }
987 return 0;
988}
989
990// For the LogOutputCallback functions
991void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) {
992 if (baton != Py_None) {
993 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
994 PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str);
995 SWIG_PYTHON_THREAD_END_BLOCK;
996 }
997}
998%}