blob: addfa29dcd58b04a92d2f3282987728fc3e298cc [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 Molendab57e4a12013-11-04 09:33:30 +0000214 const lldb::StackFrameSP& 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 Molendab57e4a12013-11-04 09:33:30 +0000254 const lldb::StackFrameSP& 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 }
Enrico Granata0e0e9f52013-12-26 07:21:41 +0000357
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000358 PyCallable pfunc = PyCallable::FindWithPythonObject(pfunc_impl);
359
360 if (!pfunc)
361 return false;
362
363 pvalue = pfunc(sb_value,session_dict);
364
365 Py_INCREF (session_dict);
366
367 PyObjectToString(pvalue,retval);
368
369 Py_XDECREF (pvalue);
370 }
371 return true;
372}
373
374SWIGEXPORT void*
375LLDBSwigPythonCreateSyntheticProvider
376(
377 const char *python_class_name,
378 const char *session_dictionary_name,
379 const lldb::ValueObjectSP& valobj_sp
380)
381{
382 PyObject* retval = NULL;
383
384 if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
385 Py_RETURN_NONE;
386
387 // I do not want the SBValue to be deallocated when going out of scope because python
388 // has ownership of it and will manage memory for this object by itself
389 lldb::SBValue *sb_value = new lldb::SBValue(valobj_sp);
390 sb_value->SetPreferSyntheticValue(false);
391 PyObject *ValObj_PyObj = SBTypeToSWIGWrapper(sb_value);
392
393 if (ValObj_PyObj == NULL)
394 Py_RETURN_NONE;
395
396 {
397 PyErr_Cleaner py_err_cleaner(true);
398
399 PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name,session_dictionary_name);
400
401 if (!pfunc)
402 return retval;
403
404 Py_INCREF(ValObj_PyObj);
405
406 PyObject* session_dict = NULL;
407 session_dict = FindSessionDictionary(session_dictionary_name);
408 retval = pfunc(sb_value, session_dict);
409
410 Py_XINCREF (session_dict);
411
412 Py_XINCREF(retval);
413 }
414
415 if (retval)
416 return retval;
417 else
418 Py_RETURN_NONE;
419}
420
421// wrapper that calls an optional instance member of an object taking no arguments
422static PyObject*
423LLDBSwigPython_CallOptionalMember
424(
425 PyObject* self,
426 char* callee_name,
427 PyObject* ret_if_not_found = Py_None,
428 bool* was_found = NULL
429)
430{
431 PyErr_Cleaner py_err_cleaner(false);
432
433 PyCallable pfunc = PyCallable::FindWithMemberFunction(self,callee_name);
434
435 if (!pfunc)
436 {
437 if (was_found)
438 *was_found = false;
439 Py_XINCREF(ret_if_not_found);
440 return ret_if_not_found;
441 }
442
443 if (was_found)
444 *was_found = true;
445
446 PyObject* py_return = pfunc();
447 return py_return;
448}
449
450SWIGEXPORT uint32_t
451LLDBSwigPython_CalculateNumChildren
452(
453 PyObject *implementor
454)
455{
456 uint32_t ret_val = UINT32_MAX;
457
458 static char callee_name[] = "num_children";
459
460 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, NULL);
461
462 if (!py_return)
463 return ret_val;
464
465 if (PyInt_Check(py_return))
466 ret_val = PyInt_AsLong(py_return);
467
468 Py_XDECREF(py_return);
469
470 if (PyErr_Occurred())
471 {
472 PyErr_Print();
473 PyErr_Clear();
474 }
475
476 return ret_val;
477}
478
479SWIGEXPORT PyObject*
480LLDBSwigPython_GetChildAtIndex
481(
482 PyObject *implementor,
483 uint32_t idx
484)
485{
486 PyErr_Cleaner py_err_cleaner(true);
487
488 PyCallable pfunc = PyCallable::FindWithMemberFunction(implementor,"get_child_at_index");
489
490 if (!pfunc)
491 return NULL;
492
493 PyObject *py_return = NULL;
494 py_return = pfunc(idx);
495
496 if (py_return == NULL || py_return == Py_None)
497 {
498 Py_XDECREF(py_return);
499 return NULL;
500 }
501
502 lldb::SBValue* sbvalue_ptr = NULL;
503
504 if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
505 {
506 Py_XDECREF(py_return);
507 return NULL;
508 }
509
510 if (sbvalue_ptr == NULL)
511 return NULL;
512
513 return py_return;
514}
515
516SWIGEXPORT int
517LLDBSwigPython_GetIndexOfChildWithName
518(
519 PyObject *implementor,
520 const char* child_name
521)
522{
523 PyErr_Cleaner py_err_cleaner(true);
524
525 PyCallable pfunc = PyCallable::FindWithMemberFunction(implementor,"get_child_index");
526
527 if (!pfunc)
528 return UINT32_MAX;
529
530 PyObject *py_return = NULL;
531 py_return = pfunc(child_name);
532
533 if (py_return == NULL || py_return == Py_None)
534 {
535 Py_XDECREF(py_return);
536 return UINT32_MAX;
537 }
538
539 long retval = PyInt_AsLong(py_return);
540 Py_XDECREF(py_return);
541
542 if (retval >= 0)
543 return (uint32_t)retval;
544
545 return UINT32_MAX;
546}
547
548SWIGEXPORT bool
549LLDBSwigPython_UpdateSynthProviderInstance
550(
551 PyObject *implementor
552)
553{
554 bool ret_val = false;
555
556 static char callee_name[] = "update";
557
558 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name);
559
560 if (py_return == Py_True)
561 ret_val = true;
562
563 Py_XDECREF(py_return);
564
565 return ret_val;
566}
567
568SWIGEXPORT bool
569LLDBSwigPython_MightHaveChildrenSynthProviderInstance
570(
571 PyObject *implementor
572)
573{
574 bool ret_val = false;
575
576 static char callee_name[] = "has_children";
577
578 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True);
579
580 if (py_return == Py_True)
581 ret_val = true;
582
583 Py_XDECREF(py_return);
584
585 return ret_val;
586}
587
588SWIGEXPORT void*
589LLDBSWIGPython_CastPyObjectToSBValue
590(
591 PyObject* data
592)
593{
594 lldb::SBValue* sb_ptr = NULL;
595
596 int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
597
598 if (valid_cast == -1)
599 return NULL;
600
601 return sb_ptr;
602}
603
604// Currently, SBCommandReturnObjectReleaser wraps a unique pointer to an
605// lldb_private::CommandReturnObject. This means that the destructor for the
606// SB object will deallocate its contained CommandReturnObject. Because that
607// object is used as the real return object for Python-based commands, we want
608// it to stay around. Thus, we release the unique pointer before returning from
609// LLDBSwigPythonCallCommand, and to guarantee that the release will occur no
610// matter how we exit from the function, we have a releaser object whose
611// destructor does the right thing for us
612class SBCommandReturnObjectReleaser
613{
614public:
615 SBCommandReturnObjectReleaser (lldb::SBCommandReturnObject &obj) :
616 m_command_return_object_ref (obj)
617 {
618 }
619
620 ~SBCommandReturnObjectReleaser ()
621 {
622 m_command_return_object_ref.Release();
623 }
624private:
625 lldb::SBCommandReturnObject &m_command_return_object_ref;
626};
627
628SWIGEXPORT bool
629LLDBSwigPythonCallCommand
630(
631 const char *python_function_name,
632 const char *session_dictionary_name,
633 lldb::DebuggerSP& debugger,
634 const char* args,
635 lldb_private::CommandReturnObject& cmd_retobj
636)
637{
638
639 lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj);
640 SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb);
641 lldb::SBDebugger debugger_sb(debugger);
642
643 bool retval = false;
644
645 {
646 PyErr_Cleaner py_err_cleaner(true);
647 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
648
649 if (!pfunc)
650 return retval;
651
652 PyObject* session_dict = NULL;
653 // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you
654 // see comment above for SBCommandReturnObjectReleaser for further details
655 PyObject* pvalue = NULL;
656 pvalue = pfunc(debugger_sb, args, &cmd_retobj_sb, session_dict = FindSessionDictionary(session_dictionary_name));
657
658 Py_XINCREF (session_dict);
659 Py_XDECREF (pvalue);
660
661 retval = true;
662 }
663
664 return retval;
665}
666
667SWIGEXPORT void*
668LLDBSWIGPythonCreateOSPlugin
669(
670 const char *python_class_name,
671 const char *session_dictionary_name,
672 const lldb::ProcessSP& process_sp
673)
674{
675 PyObject* retval = NULL;
676
677 if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
678 Py_RETURN_NONE;
679
680 // I do not want the SBProcess to be deallocated when going out of scope because python
681 // has ownership of it and will manage memory for this object by itself
682 lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp);
683
684 PyObject *SBProc_PyObj = SBTypeToSWIGWrapper(process_sb);
685
686 if (SBProc_PyObj == NULL)
687 Py_RETURN_NONE;
688
689 {
690 PyErr_Cleaner py_err_cleaner(true);
691
692 PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name,session_dictionary_name);
693
694 if (!pfunc)
695 return retval;
696
697 Py_INCREF(SBProc_PyObj);
698
699 PyObject* session_dict = NULL;
700 session_dict = session_dict = FindSessionDictionary(session_dictionary_name);
701 retval = pfunc(SBProc_PyObj);
702
703 Py_XINCREF (session_dict);
704
705 Py_XINCREF(retval);
706 }
707
708 if (retval)
709 return retval;
710 else
711 Py_RETURN_NONE;
712}
713
Enrico Granatac0f8ca02013-10-14 21:39:38 +0000714SWIGEXPORT void*
Greg Claytonef8180a2013-10-15 00:14:28 +0000715LLDBSWIGPython_GetDynamicSetting (void* module, const char* setting, const lldb::TargetSP& target_sp)
Enrico Granatac0f8ca02013-10-14 21:39:38 +0000716{
717
718 if (!module || !setting)
719 Py_RETURN_NONE;
720
721 lldb::SBTarget target_sb(target_sp);
722
723 PyObject *pvalue = NULL;
724
725 {
726 PyErr_Cleaner py_err_cleaner(true);
727 PyCallable pfunc = PyCallable::FindWithFunctionName("get_dynamic_setting",(PyObject *)module);
728
729 if (!pfunc)
730 Py_RETURN_NONE;
731
732 pvalue = pfunc(target_sb, setting);
733 }
734
735 return pvalue;
736}
737
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000738SWIGEXPORT bool
739LLDBSWIGPythonRunScriptKeywordProcess
740(const char* python_function_name,
741const char* session_dictionary_name,
742lldb::ProcessSP& process,
743std::string& output)
744
745{
746 bool retval = false;
747
748 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
749 return retval;
750
751 lldb::SBProcess process_sb(process);
752
753 {
754 PyErr_Cleaner py_err_cleaner(true);
755
756 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
757
758 if (!pfunc)
759 return retval;
760
761 PyObject* session_dict = NULL;
762 PyObject* pvalue = NULL;
763 pvalue = pfunc(process_sb, session_dict = FindSessionDictionary(session_dictionary_name));
764
765 Py_XINCREF (session_dict);
766
767 if (PyObjectToString(pvalue,output))
768 retval = true;
769
770 Py_XDECREF(pvalue);
771 }
772
773 return retval;
774}
775
776SWIGEXPORT bool
777LLDBSWIGPythonRunScriptKeywordThread
778(const char* python_function_name,
779const char* session_dictionary_name,
780lldb::ThreadSP& thread,
781std::string& output)
782
783{
784 bool retval = false;
785
786 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
787 return retval;
788
789 lldb::SBThread thread_sb(thread);
790
791 {
792 PyErr_Cleaner py_err_cleaner(true);
793
794 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
795
796 if (!pfunc)
797 return retval;
798
799 PyObject* session_dict = NULL;
800 PyObject* pvalue = NULL;
801 pvalue = pfunc(thread_sb, session_dict = FindSessionDictionary(session_dictionary_name));
802
803 Py_XINCREF (session_dict);
804
805 if (PyObjectToString(pvalue,output))
806 retval = true;
807
808 Py_XDECREF(pvalue);
809 }
810
811 return retval;
812}
813
814SWIGEXPORT bool
815LLDBSWIGPythonRunScriptKeywordTarget
816(const char* python_function_name,
817const char* session_dictionary_name,
818lldb::TargetSP& target,
819std::string& output)
820
821{
822 bool retval = false;
823
824 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
825 return retval;
826
827 lldb::SBTarget target_sb(target);
828
829 {
830 PyErr_Cleaner py_err_cleaner(true);
831
832 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
833
834 if (!pfunc)
835 return retval;
836
837 PyObject* session_dict = NULL;
838 PyObject* pvalue = NULL;
839 pvalue = pfunc(target_sb, session_dict = FindSessionDictionary(session_dictionary_name));
840
841 Py_XINCREF (session_dict);
842
843 if (PyObjectToString(pvalue,output))
844 retval = true;
845
846 Py_XDECREF(pvalue);
847 }
848
849 return retval;
850}
851
852SWIGEXPORT bool
853LLDBSWIGPythonRunScriptKeywordFrame
854(const char* python_function_name,
855const char* session_dictionary_name,
Jason Molendab57e4a12013-11-04 09:33:30 +0000856lldb::StackFrameSP& frame,
Joerg Sonnenberger340a1752013-09-25 10:37:32 +0000857std::string& output)
858
859{
860 bool retval = false;
861
862 if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
863 return retval;
864
865 lldb::SBFrame frame_sb(frame);
866
867 {
868 PyErr_Cleaner py_err_cleaner(true);
869
870 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
871
872 if (!pfunc)
873 return retval;
874
875 PyObject* session_dict = NULL;
876 PyObject* pvalue = NULL;
877 pvalue = pfunc(frame_sb, session_dict = FindSessionDictionary(session_dictionary_name));
878
879 Py_XINCREF (session_dict);
880
881 if (PyObjectToString(pvalue,output))
882 retval = true;
883
884 Py_XDECREF(pvalue);
885 }
886
887 return retval;
888}
889
890SWIGEXPORT bool
891LLDBSwigPythonCallModuleInit
892(
893 const char *python_module_name,
894 const char *session_dictionary_name,
895 lldb::DebuggerSP& debugger
896)
897{
898 bool retval = false;
899
900 lldb::SBDebugger debugger_sb(debugger);
901
902 std::string python_function_name_string = python_module_name;
903 python_function_name_string += ".__lldb_init_module";
904 const char* python_function_name = python_function_name_string.c_str();
905
906 {
907 PyErr_Cleaner py_err_cleaner(true);
908
909 PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);
910
911 if (!pfunc)
912 return true;
913
914 PyObject* session_dict = NULL;
915 PyObject* pvalue = NULL;
916 pvalue = pfunc(debugger_sb, session_dict = FindSessionDictionary(session_dictionary_name));
917
918 Py_XINCREF (session_dict);
919
920 retval = true;
921
922 Py_XDECREF(pvalue);
923 }
924
925 return retval;
926}
927%}
928
929
930%runtime %{
931// Forward declaration to be inserted at the start of LLDBWrapPython.h
932// I used runtime as a hack to make SWIG place it where it's needed.
933// This is needed to use LLDBSwigPythonCallSBInputReaderCallback in the
934// typemaps and in the extensions (SBInputReader.__del__()).
935#include "lldb/API/SBInputReader.h"
936#include "lldb/API/SBDebugger.h"
937#include "lldb/API/SBValue.h"
938
939SWIGEXPORT lldb::ValueObjectSP
940LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data)
941{
942 lldb::ValueObjectSP valobj_sp;
943 if (data)
944 {
945 lldb::SBValue* sb_ptr = (lldb::SBValue *)data;
946 valobj_sp = sb_ptr->GetSP();
947 }
948 return valobj_sp;
949}
950
951#ifdef __cplusplus
952extern "C" {
953#endif
954
955size_t
956LLDBSwigPythonCallSBInputReaderCallback(void *baton,
957 lldb::SBInputReader *reader,
958 lldb::InputReaderAction notification,
959 const char*bytes,
960 size_t bytes_len);
961
962void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton);
963
964#ifdef __cplusplus
965}
966#endif
967%}
968
969%wrapper %{
970// For the InputReader Callback functions
971SWIGEXPORT size_t
972LLDBSwigPythonCallSBInputReaderCallback(void *baton,
973 lldb::SBInputReader *reader,
974 lldb::InputReaderAction notification,
975 const char*bytes,
976 size_t bytes_len) {
977 if (baton != Py_None) {
978 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
979
980 PyObject *py_InputReader = SBTypeToSWIGWrapper(reader);
981 PyObject *py_Notification = PyInt_FromLong(notification);
982 PyObject *py_Bytes = PyBytes_FromStringAndSize(bytes, bytes_len);
983
984 PyObject *tuple = PyTuple_Pack(3, py_InputReader, py_Notification, py_Bytes);
985 PyObject *res = PyObject_Call(reinterpret_cast<PyObject*>(baton), tuple, NULL);
986 Py_XDECREF(tuple);
987 Py_XDECREF(py_InputReader);
988 Py_XDECREF(py_Notification);
989 Py_XDECREF(py_Bytes);
990
991 if (res == NULL) {
992 PyObject *exc = PyErr_Occurred();
993 if (exc) {
994 ::puts("\nErroring out at LLDBSwigPythonCallSBInputReaderCallback");
995 PyErr_Print();
996 }
997 return 0;
998 }
999
1000 size_t result = 0;
1001 // If the callback misbehaves and returns Py_None, assume it returned 0
1002 if (res != Py_None)
1003 result = static_cast<size_t>(PyInt_AsSsize_t(res));
1004
1005 Py_XDECREF(res);
1006 SWIG_PYTHON_THREAD_END_BLOCK;
1007 return result;
1008 }
1009 return 0;
1010}
1011
1012// For the LogOutputCallback functions
1013void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) {
1014 if (baton != Py_None) {
1015 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
1016 PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str);
1017 SWIG_PYTHON_THREAD_END_BLOCK;
1018 }
1019}
1020%}