blob: 109b45f0b8870cc7e37bd1d7cdd2d421ecd15a8a [file] [log] [blame]
Johnny Chenf307cf72011-07-26 19:09:03 +00001%wrapper %{
2
3// This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...)
4// and is used when a script command is attached to a breakpoint for execution.
5
6SWIGEXPORT bool
7LLDBSwigPythonBreakpointCallbackFunction
8(
9 const char *python_function_name,
10 const char *session_dictionary_name,
11 const lldb::StackFrameSP& frame_sp,
12 const lldb::BreakpointLocationSP& bp_loc_sp
13)
14{
15 lldb::SBFrame sb_frame (frame_sp);
16 lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
17
18 bool stop_at_breakpoint = true;
19 PyObject *Frame_PyObj = SWIG_NewPointerObj((void *) &sb_frame, SWIGTYPE_p_lldb__SBFrame, 0);
20 PyObject *Bp_Loc_PyObj = SWIG_NewPointerObj ((void *) &sb_bp_loc, SWIGTYPE_p_lldb__SBBreakpointLocation, 0);
21
22 if (Frame_PyObj == NULL || Bp_Loc_PyObj == NULL)
23 return stop_at_breakpoint;
24
25 if (!python_function_name || !session_dictionary_name)
26 return stop_at_breakpoint;
27
28 PyObject *pmodule, *main_dict, *session_dict, *pfunc;
29 PyObject *pargs, *pvalue;
30
31 pmodule = PyImport_AddModule ("__main__");
32 if (pmodule != NULL)
33 {
34 main_dict = PyModule_GetDict (pmodule);
35 if (main_dict != NULL)
36 {
37 PyObject *key, *value;
38 Py_ssize_t pos = 0;
39
40 // Find the current session's dictionary in the main module's dictionary.
41
42 if (PyDict_Check (main_dict))
43 {
44 session_dict = NULL;
45 while (PyDict_Next (main_dict, &pos, &key, &value))
46 {
47 // We have stolen references to the key and value objects in the dictionary; we need to increment
48 // them now so that Python's garbage collector doesn't collect them out from under us.
49 Py_INCREF (key);
50 Py_INCREF (value);
51 if (strcmp (PyString_AsString (key), session_dictionary_name) == 0)
52 {
53 session_dict = value;
54 break;
55 }
56 }
57 }
58
59 if (!session_dict || !PyDict_Check (session_dict))
60 return stop_at_breakpoint;
61
62 // Find the function we need to call in the current session's dictionary.
63
64 pos = 0;
65 pfunc = NULL;
66 while (PyDict_Next (session_dict, &pos, &key, &value))
67 {
68 if (PyString_Check (key))
69 {
70 // We have stolen references to the key and value objects in the dictionary; we need to increment
71 // them now so that Python's garbage collector doesn't collect them out from under us.
72 Py_INCREF (key);
73 Py_INCREF (value);
74 if (strcmp (PyString_AsString (key), python_function_name) == 0)
75 {
76 pfunc = value;
77 break;
78 }
79 }
80 }
81
82 // Set up the arguments and call the function.
83
84 if (pfunc && PyCallable_Check (pfunc))
85 {
86 pargs = PyTuple_New (3);
87 if (pargs == NULL)
88 {
89 if (PyErr_Occurred())
90 PyErr_Clear();
91 return stop_at_breakpoint;
92 }
93
94 PyTuple_SetItem (pargs, 0, Frame_PyObj); // This "steals" a reference to Frame_PyObj
95 PyTuple_SetItem (pargs, 1, Bp_Loc_PyObj); // This "steals" a reference to Bp_Loc_PyObj
96 PyTuple_SetItem (pargs, 2, session_dict); // This "steals" a reference to session_dict
97 pvalue = PyObject_CallObject (pfunc, pargs);
98 Py_DECREF (pargs);
99
100 if (pvalue != NULL)
101 {
102 Py_DECREF (pvalue);
103 }
104 else if (PyErr_Occurred ())
105 {
106 PyErr_Clear();
107 }
108 Py_INCREF (session_dict);
109 }
110 else if (PyErr_Occurred())
111 {
112 PyErr_Clear();
113 }
114 }
115 else if (PyErr_Occurred())
116 {
117 PyErr_Clear();
118 }
119 }
120 else if (PyErr_Occurred ())
121 {
122 PyErr_Clear ();
123 }
124 return stop_at_breakpoint;
125}
126
127SWIGEXPORT std::string
128LLDBSwigPythonCallTypeScript
129(
130 const char *python_function_name,
131 const char *session_dictionary_name,
132 const lldb::ValueObjectSP& valobj_sp
133)
134{
135 lldb::SBValue sb_value (valobj_sp);
136
137 std::string retval = "";
138
Enrico Granata3370f0c2011-08-19 23:56:34 +0000139 PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *) &sb_value, SWIGTYPE_p_lldb__SBValue, 0);
Johnny Chenf307cf72011-07-26 19:09:03 +0000140
141 if (ValObj_PyObj == NULL)
142 return retval;
143
144 if (!python_function_name || !session_dictionary_name)
145 return retval;
146
147 PyObject *pmodule, *main_dict, *session_dict, *pfunc;
148 PyObject *pargs, *pvalue;
149
150 pmodule = PyImport_AddModule ("__main__");
151 if (pmodule != NULL)
152 {
153 main_dict = PyModule_GetDict (pmodule);
154 if (main_dict != NULL)
155 {
156 PyObject *key, *value;
157 Py_ssize_t pos = 0;
158
159 // Find the current session's dictionary in the main module's dictionary.
160
161 if (PyDict_Check (main_dict))
162 {
163 session_dict = NULL;
164 while (PyDict_Next (main_dict, &pos, &key, &value))
165 {
166 // We have stolen references to the key and value objects in the dictionary; we need to increment
167 // them now so that Python's garbage collector doesn't collect them out from under us.
168 Py_INCREF (key);
169 Py_INCREF (value);
170 if (strcmp (PyString_AsString (key), session_dictionary_name) == 0)
171 {
172 session_dict = value;
173 break;
174 }
175 }
176 }
177
178 if (!session_dict || !PyDict_Check (session_dict))
179 return retval;
180
181 // Find the function we need to call in the current session's dictionary.
182
183 pos = 0;
184 pfunc = NULL;
185 while (PyDict_Next (session_dict, &pos, &key, &value))
186 {
187 if (PyString_Check (key))
188 {
189 // We have stolen references to the key and value objects in the dictionary; we need to increment
190 // them now so that Python's garbage collector doesn't collect them out from under us.
191 Py_INCREF (key);
192 Py_INCREF (value);
193 if (strcmp (PyString_AsString (key), python_function_name) == 0)
194 {
195 pfunc = value;
196 break;
197 }
198 }
199 }
200
201 // Set up the arguments and call the function.
202
203 if (pfunc && PyCallable_Check (pfunc))
204 {
205 pargs = PyTuple_New (2);
206 if (pargs == NULL)
207 {
208 if (PyErr_Occurred())
209 PyErr_Clear();
210 return retval;
211 }
212
213 PyTuple_SetItem (pargs, 0, ValObj_PyObj); // This "steals" a reference to ValObj_PyObj
214 PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict
215 pvalue = PyObject_CallObject (pfunc, pargs);
216 Py_DECREF (pargs);
217
218 if (pvalue != NULL)
219 {
220 if (pvalue != Py_None)
221 retval = std::string(PyString_AsString(pvalue));
222 else
223 retval = "None";
224 Py_DECREF (pvalue);
225 }
226 else if (PyErr_Occurred ())
227 {
228 PyErr_Print();
229 PyErr_Clear();
230 }
231 Py_INCREF (session_dict);
232 }
233 else if (PyErr_Occurred())
234 {
235 PyErr_Print();
236 PyErr_Clear();
237 }
238 }
239 else if (PyErr_Occurred())
240 {
241 PyErr_Print();
242 PyErr_Clear();
243 }
244 }
245 else if (PyErr_Occurred ())
246 {
247 PyErr_Print();
248 PyErr_Clear ();
249 }
250 return retval;
251}
252
253SWIGEXPORT void*
254LLDBSwigPythonCreateSyntheticProvider
255(
256 const std::string python_class_name,
257 const char *session_dictionary_name,
258 const lldb::ValueObjectSP& valobj_sp
259)
260{
261 PyObject* retval = NULL;
262
263 if (python_class_name.empty() || !session_dictionary_name)
264 Py_RETURN_NONE;
265
Enrico Granata3370f0c2011-08-19 23:56:34 +0000266 // I do not want the SBValue to be deallocated when going out of scope because python
267 // has ownership of it and will manage memory for this object by itself
268 lldb::SBValue *valobj_sb = new lldb::SBValue(valobj_sp);
Johnny Chenf307cf72011-07-26 19:09:03 +0000269
Enrico Granata3370f0c2011-08-19 23:56:34 +0000270 PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *)valobj_sb, SWIGTYPE_p_lldb__SBValue, SWIG_POINTER_OWN);
Johnny Chenf307cf72011-07-26 19:09:03 +0000271
272 if (ValObj_PyObj == NULL)
273 Py_RETURN_NONE;
274
275 const char* python_function_name = python_class_name.c_str();
276
277 PyObject *pmodule, *main_dict, *session_dict, *pfunc;
278 PyObject *pvalue;
279
280 pmodule = PyImport_AddModule ("__main__");
281 if (pmodule != NULL)
282 {
283 main_dict = PyModule_GetDict (pmodule);
284 if (main_dict != NULL)
285 {
286 PyObject *key, *value;
287 Py_ssize_t pos = 0;
288
289 // Find the current session's dictionary in the main module's dictionary.
290
291 if (PyDict_Check (main_dict))
292 {
293 session_dict = NULL;
294 while (PyDict_Next (main_dict, &pos, &key, &value))
295 {
296 // We have stolen references to the key and value objects in the dictionary; we need to increment
297 // them now so that Python's garbage collector doesn't collect them out from under us.
298 Py_INCREF (key);
299 Py_INCREF (value);
300 if (strcmp (PyString_AsString (key), session_dictionary_name) == 0)
301 {
302 session_dict = value;
303 break;
304 }
305 }
306 }
307
308 if (!session_dict || !PyDict_Check (session_dict))
309 return retval;
310
311 // Find the function we need to call in the current session's dictionary.
312
313 pos = 0;
314 pfunc = NULL;
315 while (PyDict_Next (session_dict, &pos, &key, &value))
316 {
317 if (PyString_Check (key))
318 {
319 // We have stolen references to the key and value objects in the dictionary; we need to increment
320 // them now so that Python's garbage collector doesn't collect them out from under us.
321 Py_INCREF (key);
322 Py_INCREF (value);
323 if (strcmp (PyString_AsString (key), python_function_name) == 0)
324 {
325 pfunc = value;
326 break;
327 }
328 }
329 }
330
331 // Set up the arguments and call the function.
332
333 if (pfunc && PyCallable_Check (pfunc))
334 {
335 PyObject *argList = Py_BuildValue("SS", ValObj_PyObj, session_dict);
336
337 if (PyErr_Occurred ())
338 {
339 PyErr_Print();
340 PyErr_Clear();
341 return retval;
342 }
343
344 if (argList == NULL)
345 {
346 return retval;
347 }
348
349 Py_INCREF(ValObj_PyObj);
350
351 pvalue = PyObject_CallObject(pfunc, argList);
352
353 Py_DECREF(argList);
354
355 if (pvalue != NULL)
356 {
357 if (pvalue != Py_None)
358 retval = pvalue;
359 else
360 {
361 retval = Py_None;
362 Py_INCREF(retval);
363 }
364 }
365 else if (PyErr_Occurred ())
366 {
367 PyErr_Print();
368 PyErr_Clear();
369 }
370 Py_INCREF (session_dict);
371 }
372 else if (PyErr_Occurred())
373 {
374 PyErr_Print();
375 PyErr_Clear();
376 }
377 }
378 else if (PyErr_Occurred())
379 {
380 PyErr_Print();
381 PyErr_Clear();
382 }
383 }
384 else if (PyErr_Occurred ())
385 {
386 PyErr_Print();
387 PyErr_Clear ();
388 }
389 if (retval)
390 return retval;
391 else
392 Py_RETURN_NONE;
393}
394
395/*
396these four calls below are meant to support
397Python-based synthetic children providers
398they essentially mimic the four pure virtual
399method calls provided by the frontend class
400*/
401
402SWIGEXPORT uint32_t
403LLDBSwigPython_CalculateNumChildren
404(
405 PyObject *implementor
406)
407{
408
409 static char callee_name[] = "num_children";
410
411 if (implementor == NULL || implementor == Py_None)
412 return 0;
413 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL);
414 if (PyErr_Occurred())
415 {
416 PyErr_Print();
417 PyErr_Clear();
418 }
419
420 if (py_return == NULL || py_return == Py_None)
421 {
422 Py_XDECREF(py_return);
423 return UINT32_MAX;
424 }
425 long retval = PyInt_AsLong(py_return);
426 Py_DECREF(py_return);
427 if (retval >= 0)
428 return (uint32_t)retval;
429 if (PyErr_Occurred())
430 {
431 PyErr_Print();
432 PyErr_Clear();
433 }
434 return 0;
435}
436
437SWIGEXPORT PyObject*
438LLDBSwigPython_GetChildAtIndex
439(
440 PyObject *implementor,
441 uint32_t idx
442)
443{
444
445 static char callee_name[] = "get_child_at_index";
446 static char param_format[] = "i";
447
448 if (implementor == NULL || implementor == Py_None)
449 return NULL;
450 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, idx);
451 if (PyErr_Occurred())
452 {
453 PyErr_Print();
454 PyErr_Clear();
455 }
456
457 if (py_return == NULL || py_return == Py_None)
458 {
459 Py_XDECREF(py_return);
460 return NULL;
461 }
462
463 lldb::SBValue* sbvalue_ptr = NULL;
464
465 if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
466 {
467 Py_DECREF(py_return);
468 return NULL;
469 }
470
471 if (sbvalue_ptr == NULL)
472 return NULL;
473
474 return py_return;
475}
476
477SWIGEXPORT int
478LLDBSwigPython_GetIndexOfChildWithName
479(
480 PyObject *implementor,
481 const char* child_name
482)
483{
484 static char callee_name[] = "get_child_index";
485 static char param_format[] = "s";
486
487 if (implementor == NULL || implementor == Py_None)
488 return 0;
489 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, child_name);
490 if (PyErr_Occurred())
491 {
492 PyErr_Print();
493 PyErr_Clear();
494 }
495
496 if (py_return == NULL || py_return == Py_None)
497 {
498 Py_XDECREF(py_return);
499 return UINT32_MAX;
500 }
501 long retval = PyInt_AsLong(py_return);
502 Py_DECREF(py_return);
503 if (retval >= 0)
504 return (uint32_t)retval;
505 if (PyErr_Occurred())
506 {
507 PyErr_Print();
508 PyErr_Clear();
509 }
510 return 0;
511}
512
Enrico Granata6a319e42011-07-26 21:02:56 +0000513SWIGEXPORT void
514LLDBSwigPython_UpdateSynthProviderInstance
515(
516 PyObject *implementor
517)
518{
519 static char callee_name[] = "update";
520
521 if (implementor == NULL || implementor == Py_None)
522 return;
523
524 // all this code is here because update is optional, so we don't want to bother trying to call it unless it's been def:ined for us
525 // other synth provider calls are mandatory, so we want to fail in a very obvious way if they are missing!
526 PyObject* pmeth = PyObject_GetAttrString(implementor, callee_name);
527
Enrico Granata1239c1a2011-08-11 19:20:44 +0000528 if (PyErr_Occurred())
529 {
530 PyErr_Clear();
531 }
532
Enrico Granata6a319e42011-07-26 21:02:56 +0000533 if (pmeth == NULL || pmeth == Py_None)
534 {
535 Py_XDECREF(pmeth);
536 return;
537 }
538
539 if (PyCallable_Check(pmeth) == 0)
540 {
Enrico Granata1239c1a2011-08-11 19:20:44 +0000541 if (PyErr_Occurred())
542 {
543 PyErr_Clear();
544 }
545
Enrico Granata6a319e42011-07-26 21:02:56 +0000546 Py_XDECREF(pmeth);
547 return;
548 }
549
Enrico Granata1239c1a2011-08-11 19:20:44 +0000550 if (PyErr_Occurred())
551 {
552 PyErr_Clear();
553 }
554
Enrico Granata6a319e42011-07-26 21:02:56 +0000555 Py_XDECREF(pmeth);
556
557 // right now we know this function exists and is callable..
558 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL);
559
560 // if it fails, print the error but otherwise go on
561 if (PyErr_Occurred())
562 {
563 PyErr_Print();
564 PyErr_Clear();
565 }
566
567 Py_XDECREF(py_return);
568
569}
570
Johnny Chenf307cf72011-07-26 19:09:03 +0000571SWIGEXPORT lldb::SBValue*
572LLDBSWIGPython_CastPyObjectToSBValue
573(
574 PyObject* data
575)
576{
577 lldb::SBValue* sb_ptr = NULL;
578
579 int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
580
581 if (valid_cast == -1)
582 return NULL;
583
584 return sb_ptr;
585}
586
Enrico Granata3370f0c2011-08-19 23:56:34 +0000587// we use this macro to bail out of LLDBSwigPythonCallCommand in order
588// to make sure that the that the SBCommandReturnObject will not destroy
589// the contained CommandReturnObject when going out of scope
590#define RETURN_RETVAL { \
591 cmd_retobj_sb.Release(); \
592 return retval; \
593}
594
Enrico Granatac2a28252011-08-16 16:49:25 +0000595SWIGEXPORT bool
596LLDBSwigPythonCallCommand
597(
598 const char *python_function_name,
599 const char *session_dictionary_name,
600 lldb::DebuggerSP& debugger,
601 const char* args,
602 std::string& err_msg,
Enrico Granata3370f0c2011-08-19 23:56:34 +0000603 lldb_private::CommandReturnObject& cmd_retobj
Enrico Granatac2a28252011-08-16 16:49:25 +0000604)
605{
606
Enrico Granata3370f0c2011-08-19 23:56:34 +0000607 lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj);
608 lldb::SBDebugger debugger_sb(debugger);
Enrico Granata6b1596d2011-08-16 23:24:13 +0000609
Enrico Granatac2a28252011-08-16 16:49:25 +0000610 bool retval = false;
611
Enrico Granata3370f0c2011-08-19 23:56:34 +0000612 PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
613 PyObject *CmdRetObj_PyObj = SWIG_NewPointerObj((void *) &cmd_retobj_sb, SWIGTYPE_p_lldb__SBCommandReturnObject, 0);
Enrico Granatac2a28252011-08-16 16:49:25 +0000614
615 if (DebuggerObj_PyObj == NULL)
Enrico Granata3370f0c2011-08-19 23:56:34 +0000616 RETURN_RETVAL;
Enrico Granatac2a28252011-08-16 16:49:25 +0000617
Enrico Granata6b1596d2011-08-16 23:24:13 +0000618 if (CmdRetObj_PyObj == NULL)
Enrico Granata3370f0c2011-08-19 23:56:34 +0000619 RETURN_RETVAL;
Enrico Granatac2a28252011-08-16 16:49:25 +0000620
621 if (!python_function_name || !session_dictionary_name)
Enrico Granata3370f0c2011-08-19 23:56:34 +0000622 RETURN_RETVAL;
Enrico Granatac2a28252011-08-16 16:49:25 +0000623
624 PyObject *pmodule, *main_dict, *session_dict, *pfunc;
625 PyObject *pargs, *pvalue;
626
627 pmodule = PyImport_AddModule ("__main__");
628 if (pmodule != NULL)
629 {
630 main_dict = PyModule_GetDict (pmodule);
631 if (main_dict != NULL)
632 {
633 PyObject *key, *value;
634 Py_ssize_t pos = 0;
635
636 // Find the current session's dictionary in the main module's dictionary.
637
638 if (PyDict_Check (main_dict))
639 {
640 session_dict = NULL;
641 while (PyDict_Next (main_dict, &pos, &key, &value))
642 {
643 // We have stolen references to the key and value objects in the dictionary; we need to increment
644 // them now so that Python's garbage collector doesn't collect them out from under us.
645 Py_INCREF (key);
646 Py_INCREF (value);
647 if (strcmp (PyString_AsString (key), session_dictionary_name) == 0)
648 {
649 session_dict = value;
650 break;
651 }
652 }
653 }
654
655 if (!session_dict || !PyDict_Check (session_dict))
Enrico Granata3370f0c2011-08-19 23:56:34 +0000656 RETURN_RETVAL;
Enrico Granatac2a28252011-08-16 16:49:25 +0000657
658 // Find the function we need to call in the current session's dictionary.
659
660 pos = 0;
661 pfunc = NULL;
662 while (PyDict_Next (session_dict, &pos, &key, &value))
663 {
664 if (PyString_Check (key))
665 {
666 // We have stolen references to the key and value objects in the dictionary; we need to increment
667 // them now so that Python's garbage collector doesn't collect them out from under us.
668 Py_INCREF (key);
669 Py_INCREF (value);
670 if (strcmp (PyString_AsString (key), python_function_name) == 0)
671 {
672 pfunc = value;
673 break;
674 }
675 }
676 }
677
678 // Set up the arguments and call the function.
679
680 if (pfunc && PyCallable_Check (pfunc))
681 {
682 pargs = PyTuple_New (4);
683 if (pargs == NULL)
684 {
685 if (PyErr_Occurred())
686 PyErr_Clear();
Enrico Granata3370f0c2011-08-19 23:56:34 +0000687 RETURN_RETVAL;
Enrico Granatac2a28252011-08-16 16:49:25 +0000688 }
689
690 PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj
691 PyTuple_SetItem (pargs, 1, PyString_FromString(args));
Enrico Granata6b1596d2011-08-16 23:24:13 +0000692 PyTuple_SetItem (pargs, 2, CmdRetObj_PyObj); // This "steals" a reference to CmdRetObj_PyObj
Enrico Granatac2a28252011-08-16 16:49:25 +0000693 PyTuple_SetItem (pargs, 3, session_dict); // This "steals" a reference to session_dict
694 pvalue = PyObject_CallObject (pfunc, pargs);
695 Py_DECREF (pargs);
696
697 if (pvalue != NULL)
698 {
699 if (pvalue == Py_None) // no error
700 {
701 err_msg.clear();
702 retval = true;
703 }
704 else // return value is an error string
705 {
706 err_msg.assign(PyString_AsString(pvalue));
707 retval = false;
708 }
709 Py_DECREF (pvalue);
710 }
711 else if (PyErr_Occurred ())
712 {
713 PyErr_Print();
714 PyErr_Clear();
715 }
716 Py_INCREF (session_dict);
717 }
718 else if (PyErr_Occurred())
719 {
720 PyErr_Print();
721 PyErr_Clear();
722 }
723 }
724 else if (PyErr_Occurred())
725 {
726 PyErr_Print();
727 PyErr_Clear();
728 }
729 }
730 else if (PyErr_Occurred ())
731 {
732 PyErr_Print();
733 PyErr_Clear ();
734 }
Enrico Granata3370f0c2011-08-19 23:56:34 +0000735 RETURN_RETVAL;
Enrico Granatac2a28252011-08-16 16:49:25 +0000736}
737
Enrico Granata3370f0c2011-08-19 23:56:34 +0000738#undef RETURN_RETVAL
739
Johnny Chenf307cf72011-07-26 19:09:03 +0000740%}