blob: 70948a01a0d21098b79b5ef5941b5768bf1b0b50 [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,
Jason Molendaf23bf742013-11-02 02:23:02 +0000214 const lldb::FrameSP& frame_sp,
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000215 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,
Jason Molendaf23bf742013-11-02 02:23:02 +0000254 const lldb::FrameSP& frame_sp,
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000255 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
Enrico Granatac0f8ca02013-10-14 21:39:38 +0000716SWIGEXPORT void*
Greg Claytonef8180a2013-10-15 00:14:28 +0000717LLDBSWIGPython_GetDynamicSetting (void* module, const char* setting, const lldb::TargetSP& target_sp)
Enrico Granatac0f8ca02013-10-14 21:39:38 +0000718{
719
720 if (!module || !setting)
721 Py_RETURN_NONE;
722
723 lldb::SBTarget target_sb(target_sp);
724
725 PyObject *pvalue = NULL;
726
727 {
728 PyErr_Cleaner py_err_cleaner(true);
729 PyCallable pfunc = PyCallable::FindWithFunctionName("get_dynamic_setting",(PyObject *)module);
730
731 if (!pfunc)
732 Py_RETURN_NONE;
733
734 pvalue = pfunc(target_sb, setting);
735 }
736
737 return pvalue;
738}
739
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000740SWIGEXPORT bool
741LLDBSWIGPythonRunScriptKeywordProcess
742(const char* python_function_name,
743const char* session_dictionary_name,
744lldb::ProcessSP& process,
745std::string& output)
746
747{
748 bool retval = false;
749
750 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
751 return retval;
752
753 lldb::SBProcess process_sb(process);
754
755 {
756 PyErr_Cleaner py_err_cleaner(true);
757
758 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
759
760 if (!pfunc)
761 return retval;
762
763 PyObject* session_dict = NULL;
764 PyObject* pvalue = NULL;
765 pvalue = pfunc(process_sb, session_dict = FindSessionDictionary(session_dictionary_name));
766
767 Py_XINCREF (session_dict);
768
769 if (PyObjectToString(pvalue,output))
770 retval = true;
771
772 Py_XDECREF(pvalue);
773 }
774
775 return retval;
776}
777
778SWIGEXPORT bool
779LLDBSWIGPythonRunScriptKeywordThread
780(const char* python_function_name,
781const char* session_dictionary_name,
782lldb::ThreadSP& thread,
783std::string& output)
784
785{
786 bool retval = false;
787
788 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
789 return retval;
790
791 lldb::SBThread thread_sb(thread);
792
793 {
794 PyErr_Cleaner py_err_cleaner(true);
795
796 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
797
798 if (!pfunc)
799 return retval;
800
801 PyObject* session_dict = NULL;
802 PyObject* pvalue = NULL;
803 pvalue = pfunc(thread_sb, session_dict = FindSessionDictionary(session_dictionary_name));
804
805 Py_XINCREF (session_dict);
806
807 if (PyObjectToString(pvalue,output))
808 retval = true;
809
810 Py_XDECREF(pvalue);
811 }
812
813 return retval;
814}
815
816SWIGEXPORT bool
817LLDBSWIGPythonRunScriptKeywordTarget
818(const char* python_function_name,
819const char* session_dictionary_name,
820lldb::TargetSP& target,
821std::string& output)
822
823{
824 bool retval = false;
825
826 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
827 return retval;
828
829 lldb::SBTarget target_sb(target);
830
831 {
832 PyErr_Cleaner py_err_cleaner(true);
833
834 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
835
836 if (!pfunc)
837 return retval;
838
839 PyObject* session_dict = NULL;
840 PyObject* pvalue = NULL;
841 pvalue = pfunc(target_sb, session_dict = FindSessionDictionary(session_dictionary_name));
842
843 Py_XINCREF (session_dict);
844
845 if (PyObjectToString(pvalue,output))
846 retval = true;
847
848 Py_XDECREF(pvalue);
849 }
850
851 return retval;
852}
853
854SWIGEXPORT bool
855LLDBSWIGPythonRunScriptKeywordFrame
856(const char* python_function_name,
857const char* session_dictionary_name,
Jason Molendaf23bf742013-11-02 02:23:02 +0000858lldb::FrameSP& frame,
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000859std::string& output)
860
861{
862 bool retval = false;
863
864 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
865 return retval;
866
867 lldb::SBFrame frame_sb(frame);
868
869 {
870 PyErr_Cleaner py_err_cleaner(true);
871
872 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
873
874 if (!pfunc)
875 return retval;
876
877 PyObject* session_dict = NULL;
878 PyObject* pvalue = NULL;
879 pvalue = pfunc(frame_sb, session_dict = FindSessionDictionary(session_dictionary_name));
880
881 Py_XINCREF (session_dict);
882
883 if (PyObjectToString(pvalue,output))
884 retval = true;
885
886 Py_XDECREF(pvalue);
887 }
888
889 return retval;
890}
891
892SWIGEXPORT bool
893LLDBSwigPythonCallModuleInit
894(
895 const char *python_module_name,
896 const char *session_dictionary_name,
897 lldb::DebuggerSP& debugger
898)
899{
900 bool retval = false;
901
902 lldb::SBDebugger debugger_sb(debugger);
903
904 std::string python_function_name_string = python_module_name;
905 python_function_name_string += ".__lldb_init_module";
906 const char* python_function_name = python_function_name_string.c_str();
907
908 {
909 PyErr_Cleaner py_err_cleaner(true);
910
911 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
912
913 if (!pfunc)
914 return true;
915
916 PyObject* session_dict = NULL;
917 PyObject* pvalue = NULL;
918 pvalue = pfunc(debugger_sb, session_dict = FindSessionDictionary(session_dictionary_name));
919
920 Py_XINCREF (session_dict);
921
922 retval = true;
923
924 Py_XDECREF(pvalue);
925 }
926
927 return retval;
928}
929%}
930
931
932%runtime %{
933// Forward declaration to be inserted at the start of LLDBWrapPython.h
934// I used runtime as a hack to make SWIG place it where it's needed.
935// This is needed to use LLDBSwigPythonCallSBInputReaderCallback in the
936// typemaps and in the extensions (SBInputReader.__del__()).
937#include "lldb/API/SBInputReader.h"
938#include "lldb/API/SBDebugger.h"
939#include "lldb/API/SBValue.h"
940
941SWIGEXPORT lldb::ValueObjectSP
942LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data)
943{
944 lldb::ValueObjectSP valobj_sp;
945 if (data)
946 {
947 lldb::SBValue* sb_ptr = (lldb::SBValue *)data;
948 valobj_sp = sb_ptr->GetSP();
949 }
950 return valobj_sp;
951}
952
953#ifdef __cplusplus
954extern "C" {
955#endif
956
957size_t
958LLDBSwigPythonCallSBInputReaderCallback(void *baton,
959 lldb::SBInputReader *reader,
960 lldb::InputReaderAction notification,
961 const char*bytes,
962 size_t bytes_len);
963
964void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton);
965
966#ifdef __cplusplus
967}
968#endif
969%}
970
971%wrapper %{
972// For the InputReader Callback functions
973SWIGEXPORT size_t
974LLDBSwigPythonCallSBInputReaderCallback(void *baton,
975 lldb::SBInputReader *reader,
976 lldb::InputReaderAction notification,
977 const char*bytes,
978 size_t bytes_len) {
979 if (baton != Py_None) {
980 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
981
982 PyObject *py_InputReader = SBTypeToSWIGWrapper(reader);
983 PyObject *py_Notification = PyInt_FromLong(notification);
984 PyObject *py_Bytes = PyBytes_FromStringAndSize(bytes, bytes_len);
985
986 PyObject *tuple = PyTuple_Pack(3, py_InputReader, py_Notification, py_Bytes);
987 PyObject *res = PyObject_Call(reinterpret_cast<PyObject*>(baton), tuple, NULL);
988 Py_XDECREF(tuple);
989 Py_XDECREF(py_InputReader);
990 Py_XDECREF(py_Notification);
991 Py_XDECREF(py_Bytes);
992
993 if (res == NULL) {
994 PyObject *exc = PyErr_Occurred();
995 if (exc) {
996 ::puts("\nErroring out at LLDBSwigPythonCallSBInputReaderCallback");
997 PyErr_Print();
998 }
999 return 0;
1000 }
1001
1002 size_t result = 0;
1003 // If the callback misbehaves and returns Py_None, assume it returned 0
1004 if (res != Py_None)
1005 result = static_cast<size_t>(PyInt_AsSsize_t(res));
1006
1007 Py_XDECREF(res);
1008 SWIG_PYTHON_THREAD_END_BLOCK;
1009 return result;
1010 }
1011 return 0;
1012}
1013
1014// For the LogOutputCallback functions
1015void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) {
1016 if (baton != Py_None) {
1017 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
1018 PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str);
1019 SWIG_PYTHON_THREAD_END_BLOCK;
1020 }
1021}
1022%}