blob: 0843af126d1fe2ac544fc0bea6c20e57d927b254 [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
139 PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *) &valobj_sp, SWIGTYPE_p_lldb__SBValue, 0);
140
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
266 lldb::ValueObjectSP* valobj_sp_ptr = new lldb::ValueObjectSP(valobj_sp);
267
268 PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *) valobj_sp_ptr, SWIGTYPE_p_lldb__SBValue, SWIG_POINTER_OWN);
269
270 if (ValObj_PyObj == NULL)
271 Py_RETURN_NONE;
272
273 const char* python_function_name = python_class_name.c_str();
274
275 PyObject *pmodule, *main_dict, *session_dict, *pfunc;
276 PyObject *pvalue;
277
278 pmodule = PyImport_AddModule ("__main__");
279 if (pmodule != NULL)
280 {
281 main_dict = PyModule_GetDict (pmodule);
282 if (main_dict != NULL)
283 {
284 PyObject *key, *value;
285 Py_ssize_t pos = 0;
286
287 // Find the current session's dictionary in the main module's dictionary.
288
289 if (PyDict_Check (main_dict))
290 {
291 session_dict = NULL;
292 while (PyDict_Next (main_dict, &pos, &key, &value))
293 {
294 // We have stolen references to the key and value objects in the dictionary; we need to increment
295 // them now so that Python's garbage collector doesn't collect them out from under us.
296 Py_INCREF (key);
297 Py_INCREF (value);
298 if (strcmp (PyString_AsString (key), session_dictionary_name) == 0)
299 {
300 session_dict = value;
301 break;
302 }
303 }
304 }
305
306 if (!session_dict || !PyDict_Check (session_dict))
307 return retval;
308
309 // Find the function we need to call in the current session's dictionary.
310
311 pos = 0;
312 pfunc = NULL;
313 while (PyDict_Next (session_dict, &pos, &key, &value))
314 {
315 if (PyString_Check (key))
316 {
317 // We have stolen references to the key and value objects in the dictionary; we need to increment
318 // them now so that Python's garbage collector doesn't collect them out from under us.
319 Py_INCREF (key);
320 Py_INCREF (value);
321 if (strcmp (PyString_AsString (key), python_function_name) == 0)
322 {
323 pfunc = value;
324 break;
325 }
326 }
327 }
328
329 // Set up the arguments and call the function.
330
331 if (pfunc && PyCallable_Check (pfunc))
332 {
333 PyObject *argList = Py_BuildValue("SS", ValObj_PyObj, session_dict);
334
335 if (PyErr_Occurred ())
336 {
337 PyErr_Print();
338 PyErr_Clear();
339 return retval;
340 }
341
342 if (argList == NULL)
343 {
344 return retval;
345 }
346
347 Py_INCREF(ValObj_PyObj);
348
349 pvalue = PyObject_CallObject(pfunc, argList);
350
351 Py_DECREF(argList);
352
353 if (pvalue != NULL)
354 {
355 if (pvalue != Py_None)
356 retval = pvalue;
357 else
358 {
359 retval = Py_None;
360 Py_INCREF(retval);
361 }
362 }
363 else if (PyErr_Occurred ())
364 {
365 PyErr_Print();
366 PyErr_Clear();
367 }
368 Py_INCREF (session_dict);
369 }
370 else if (PyErr_Occurred())
371 {
372 PyErr_Print();
373 PyErr_Clear();
374 }
375 }
376 else if (PyErr_Occurred())
377 {
378 PyErr_Print();
379 PyErr_Clear();
380 }
381 }
382 else if (PyErr_Occurred ())
383 {
384 PyErr_Print();
385 PyErr_Clear ();
386 }
387 if (retval)
388 return retval;
389 else
390 Py_RETURN_NONE;
391}
392
393/*
394these four calls below are meant to support
395Python-based synthetic children providers
396they essentially mimic the four pure virtual
397method calls provided by the frontend class
398*/
399
400SWIGEXPORT uint32_t
401LLDBSwigPython_CalculateNumChildren
402(
403 PyObject *implementor
404)
405{
406
407 static char callee_name[] = "num_children";
408
409 if (implementor == NULL || implementor == Py_None)
410 return 0;
411 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL);
412 if (PyErr_Occurred())
413 {
414 PyErr_Print();
415 PyErr_Clear();
416 }
417
418 if (py_return == NULL || py_return == Py_None)
419 {
420 Py_XDECREF(py_return);
421 return UINT32_MAX;
422 }
423 long retval = PyInt_AsLong(py_return);
424 Py_DECREF(py_return);
425 if (retval >= 0)
426 return (uint32_t)retval;
427 if (PyErr_Occurred())
428 {
429 PyErr_Print();
430 PyErr_Clear();
431 }
432 return 0;
433}
434
435SWIGEXPORT PyObject*
436LLDBSwigPython_GetChildAtIndex
437(
438 PyObject *implementor,
439 uint32_t idx
440)
441{
442
443 static char callee_name[] = "get_child_at_index";
444 static char param_format[] = "i";
445
446 if (implementor == NULL || implementor == Py_None)
447 return NULL;
448 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, idx);
449 if (PyErr_Occurred())
450 {
451 PyErr_Print();
452 PyErr_Clear();
453 }
454
455 if (py_return == NULL || py_return == Py_None)
456 {
457 Py_XDECREF(py_return);
458 return NULL;
459 }
460
461 lldb::SBValue* sbvalue_ptr = NULL;
462
463 if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
464 {
465 Py_DECREF(py_return);
466 return NULL;
467 }
468
469 if (sbvalue_ptr == NULL)
470 return NULL;
471
472 return py_return;
473}
474
475SWIGEXPORT int
476LLDBSwigPython_GetIndexOfChildWithName
477(
478 PyObject *implementor,
479 const char* child_name
480)
481{
482 static char callee_name[] = "get_child_index";
483 static char param_format[] = "s";
484
485 if (implementor == NULL || implementor == Py_None)
486 return 0;
487 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, child_name);
488 if (PyErr_Occurred())
489 {
490 PyErr_Print();
491 PyErr_Clear();
492 }
493
494 if (py_return == NULL || py_return == Py_None)
495 {
496 Py_XDECREF(py_return);
497 return UINT32_MAX;
498 }
499 long retval = PyInt_AsLong(py_return);
500 Py_DECREF(py_return);
501 if (retval >= 0)
502 return (uint32_t)retval;
503 if (PyErr_Occurred())
504 {
505 PyErr_Print();
506 PyErr_Clear();
507 }
508 return 0;
509}
510
Enrico Granata6a319e42011-07-26 21:02:56 +0000511SWIGEXPORT void
512LLDBSwigPython_UpdateSynthProviderInstance
513(
514 PyObject *implementor
515)
516{
517 static char callee_name[] = "update";
518
519 if (implementor == NULL || implementor == Py_None)
520 return;
521
522 // 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
523 // other synth provider calls are mandatory, so we want to fail in a very obvious way if they are missing!
524 PyObject* pmeth = PyObject_GetAttrString(implementor, callee_name);
525
Enrico Granata1239c1a2011-08-11 19:20:44 +0000526 if (PyErr_Occurred())
527 {
528 PyErr_Clear();
529 }
530
Enrico Granata6a319e42011-07-26 21:02:56 +0000531 if (pmeth == NULL || pmeth == Py_None)
532 {
533 Py_XDECREF(pmeth);
534 return;
535 }
536
537 if (PyCallable_Check(pmeth) == 0)
538 {
Enrico Granata1239c1a2011-08-11 19:20:44 +0000539 if (PyErr_Occurred())
540 {
541 PyErr_Clear();
542 }
543
Enrico Granata6a319e42011-07-26 21:02:56 +0000544 Py_XDECREF(pmeth);
545 return;
546 }
547
Enrico Granata1239c1a2011-08-11 19:20:44 +0000548 if (PyErr_Occurred())
549 {
550 PyErr_Clear();
551 }
552
Enrico Granata6a319e42011-07-26 21:02:56 +0000553 Py_XDECREF(pmeth);
554
555 // right now we know this function exists and is callable..
556 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL);
557
558 // if it fails, print the error but otherwise go on
559 if (PyErr_Occurred())
560 {
561 PyErr_Print();
562 PyErr_Clear();
563 }
564
565 Py_XDECREF(py_return);
566
567}
568
Johnny Chenf307cf72011-07-26 19:09:03 +0000569SWIGEXPORT lldb::SBValue*
570LLDBSWIGPython_CastPyObjectToSBValue
571(
572 PyObject* data
573)
574{
575 lldb::SBValue* sb_ptr = NULL;
576
577 int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
578
579 if (valid_cast == -1)
580 return NULL;
581
582 return sb_ptr;
583}
584
Enrico Granatac2a28252011-08-16 16:49:25 +0000585SWIGEXPORT bool
586LLDBSwigPythonCallCommand
587(
588 const char *python_function_name,
589 const char *session_dictionary_name,
590 lldb::DebuggerSP& debugger,
591 const char* args,
592 std::string& err_msg,
593 lldb::SBStream& stream
594)
595{
596
597 bool retval = false;
598
599 PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger, SWIGTYPE_p_lldb__SBDebugger, 0);
600 PyObject *StreamObj_PyObj = SWIG_NewPointerObj((void *) &stream, SWIGTYPE_p_lldb__SBStream, 0);
601
602 if (DebuggerObj_PyObj == NULL)
603 return retval;
604
605 if (StreamObj_PyObj == NULL)
606 return retval;
607
608 if (!python_function_name || !session_dictionary_name)
609 return retval;
610
611 PyObject *pmodule, *main_dict, *session_dict, *pfunc;
612 PyObject *pargs, *pvalue;
613
614 pmodule = PyImport_AddModule ("__main__");
615 if (pmodule != NULL)
616 {
617 main_dict = PyModule_GetDict (pmodule);
618 if (main_dict != NULL)
619 {
620 PyObject *key, *value;
621 Py_ssize_t pos = 0;
622
623 // Find the current session's dictionary in the main module's dictionary.
624
625 if (PyDict_Check (main_dict))
626 {
627 session_dict = NULL;
628 while (PyDict_Next (main_dict, &pos, &key, &value))
629 {
630 // We have stolen references to the key and value objects in the dictionary; we need to increment
631 // them now so that Python's garbage collector doesn't collect them out from under us.
632 Py_INCREF (key);
633 Py_INCREF (value);
634 if (strcmp (PyString_AsString (key), session_dictionary_name) == 0)
635 {
636 session_dict = value;
637 break;
638 }
639 }
640 }
641
642 if (!session_dict || !PyDict_Check (session_dict))
643 return retval;
644
645 // Find the function we need to call in the current session's dictionary.
646
647 pos = 0;
648 pfunc = NULL;
649 while (PyDict_Next (session_dict, &pos, &key, &value))
650 {
651 if (PyString_Check (key))
652 {
653 // We have stolen references to the key and value objects in the dictionary; we need to increment
654 // them now so that Python's garbage collector doesn't collect them out from under us.
655 Py_INCREF (key);
656 Py_INCREF (value);
657 if (strcmp (PyString_AsString (key), python_function_name) == 0)
658 {
659 pfunc = value;
660 break;
661 }
662 }
663 }
664
665 // Set up the arguments and call the function.
666
667 if (pfunc && PyCallable_Check (pfunc))
668 {
669 pargs = PyTuple_New (4);
670 if (pargs == NULL)
671 {
672 if (PyErr_Occurred())
673 PyErr_Clear();
674 return retval;
675 }
676
677 PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj
678 PyTuple_SetItem (pargs, 1, PyString_FromString(args));
679 PyTuple_SetItem (pargs, 2, StreamObj_PyObj); // This "steals" a reference to StreamObj_PyObj
680 PyTuple_SetItem (pargs, 3, session_dict); // This "steals" a reference to session_dict
681 pvalue = PyObject_CallObject (pfunc, pargs);
682 Py_DECREF (pargs);
683
684 if (pvalue != NULL)
685 {
686 if (pvalue == Py_None) // no error
687 {
688 err_msg.clear();
689 retval = true;
690 }
691 else // return value is an error string
692 {
693 err_msg.assign(PyString_AsString(pvalue));
694 retval = false;
695 }
696 Py_DECREF (pvalue);
697 }
698 else if (PyErr_Occurred ())
699 {
700 PyErr_Print();
701 PyErr_Clear();
702 }
703 Py_INCREF (session_dict);
704 }
705 else if (PyErr_Occurred())
706 {
707 PyErr_Print();
708 PyErr_Clear();
709 }
710 }
711 else if (PyErr_Occurred())
712 {
713 PyErr_Print();
714 PyErr_Clear();
715 }
716 }
717 else if (PyErr_Occurred ())
718 {
719 PyErr_Print();
720 PyErr_Clear ();
721 }
722 return retval;
723}
724
Johnny Chenf307cf72011-07-26 19:09:03 +0000725%}