blob: 199cc639cb25e4d416fb025e4899c809d8a7f457 [file] [log] [blame]
Johnny Chenf307cf72011-07-26 19:09:03 +00001%wrapper %{
2
Enrico Granata960691f2011-08-22 17:34:47 +00003class PyErr_Cleaner
4{
5public:
6 PyErr_Cleaner(bool print=false) :
7 m_print(print)
8 {
9 }
10
11 ~PyErr_Cleaner()
12 {
13 if (PyErr_Occurred())
14 {
15 if(m_print)
16 PyErr_Print();
17 PyErr_Clear();
18 }
19 }
20
21private:
22 bool m_print;
23};
24
25// resolve a dotted Python name in the form
26// foo.bar.baz.Foobar to an actual Python object
27// if pmodule is NULL, the __main__ module will be used
28// as the starting point for the search
29
30static PyObject*
31ResolvePythonName(const char* name,
32 PyObject* pmodule = NULL)
33{
Enrico Granata1328b142012-02-29 03:28:49 +000034 if (!name)
Enrico Granata960691f2011-08-22 17:34:47 +000035 return pmodule;
36
37 PyErr_Cleaner pyerr_cleanup(true); // show Python errors
38
39 PyObject* main_dict;
40
41 if (!pmodule)
42 {
43 pmodule = PyImport_AddModule ("__main__");
44 if (!pmodule)
45 return NULL;
46 }
47
48 if (!PyDict_Check(pmodule))
49 {
50 main_dict = PyModule_GetDict (pmodule);
51 if (!main_dict)
52 return NULL;
53 }
54 else
55 main_dict = pmodule;
56
57 const char* dot_pos = ::strchr(name, '.');
58
59 PyObject *dest_object;
60 PyObject *key, *value;
61 Py_ssize_t pos = 0;
62
63 if (!dot_pos)
64 {
Enrico Granata1328b142012-02-29 03:28:49 +000065 dest_object = NULL;
66 while (PyDict_Next (main_dict, &pos, &key, &value))
Enrico Granata960691f2011-08-22 17:34:47 +000067 {
Enrico Granata1328b142012-02-29 03:28:49 +000068 // We have stolen references to the key and value objects in the dictionary; we need to increment
69 // them now so that Python's garbage collector doesn't collect them out from under us.
70 Py_INCREF (key);
71 Py_INCREF (value);
72 if (strcmp (PyString_AsString (key), name) == 0)
Enrico Granata960691f2011-08-22 17:34:47 +000073 {
Enrico Granata1328b142012-02-29 03:28:49 +000074 dest_object = value;
75 break;
Enrico Granata960691f2011-08-22 17:34:47 +000076 }
Enrico Granata1328b142012-02-29 03:28:49 +000077 }
Enrico Granata960691f2011-08-22 17:34:47 +000078 if (!dest_object || dest_object == Py_None)
79 return NULL;
80 return dest_object;
81 }
Enrico Granata1328b142012-02-29 03:28:49 +000082 else
83 {
84 size_t len = dot_pos - name;
85 std::string piece(name,len);
86 pmodule = ResolvePythonName(piece.c_str(), main_dict);
87 if (!pmodule)
88 return NULL;
89 name = dot_pos+1;
90 return ResolvePythonName(dot_pos+1,pmodule); // tail recursion.. should be optimized by the compiler
91 }
Enrico Granata960691f2011-08-22 17:34:47 +000092}
93
94static PyObject*
95FindSessionDictionary(const char *session_dictionary_name)
96{
97 return ResolvePythonName(session_dictionary_name, NULL);
98}
99
Johnny Chenf307cf72011-07-26 19:09:03 +0000100// This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...)
101// and is used when a script command is attached to a breakpoint for execution.
102
103SWIGEXPORT bool
104LLDBSwigPythonBreakpointCallbackFunction
105(
106 const char *python_function_name,
107 const char *session_dictionary_name,
108 const lldb::StackFrameSP& frame_sp,
109 const lldb::BreakpointLocationSP& bp_loc_sp
110)
111{
112 lldb::SBFrame sb_frame (frame_sp);
113 lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
114
115 bool stop_at_breakpoint = true;
116 PyObject *Frame_PyObj = SWIG_NewPointerObj((void *) &sb_frame, SWIGTYPE_p_lldb__SBFrame, 0);
117 PyObject *Bp_Loc_PyObj = SWIG_NewPointerObj ((void *) &sb_bp_loc, SWIGTYPE_p_lldb__SBBreakpointLocation, 0);
118
119 if (Frame_PyObj == NULL || Bp_Loc_PyObj == NULL)
120 return stop_at_breakpoint;
121
122 if (!python_function_name || !session_dictionary_name)
123 return stop_at_breakpoint;
124
Enrico Granata960691f2011-08-22 17:34:47 +0000125 PyObject *session_dict, *pfunc;
Johnny Chenf307cf72011-07-26 19:09:03 +0000126 PyObject *pargs, *pvalue;
127
Enrico Granata960691f2011-08-22 17:34:47 +0000128 session_dict = FindSessionDictionary (session_dictionary_name);
129 if (session_dict != NULL)
Johnny Chenf307cf72011-07-26 19:09:03 +0000130 {
Enrico Granata960691f2011-08-22 17:34:47 +0000131 pfunc = ResolvePythonName (python_function_name, session_dict);
132 if (pfunc != NULL)
Johnny Chenf307cf72011-07-26 19:09:03 +0000133 {
Johnny Chenf307cf72011-07-26 19:09:03 +0000134 // Set up the arguments and call the function.
135
Enrico Granata960691f2011-08-22 17:34:47 +0000136 if (PyCallable_Check (pfunc))
Johnny Chenf307cf72011-07-26 19:09:03 +0000137 {
138 pargs = PyTuple_New (3);
139 if (pargs == NULL)
140 {
141 if (PyErr_Occurred())
142 PyErr_Clear();
143 return stop_at_breakpoint;
144 }
145
146 PyTuple_SetItem (pargs, 0, Frame_PyObj); // This "steals" a reference to Frame_PyObj
147 PyTuple_SetItem (pargs, 1, Bp_Loc_PyObj); // This "steals" a reference to Bp_Loc_PyObj
148 PyTuple_SetItem (pargs, 2, session_dict); // This "steals" a reference to session_dict
149 pvalue = PyObject_CallObject (pfunc, pargs);
150 Py_DECREF (pargs);
151
152 if (pvalue != NULL)
153 {
154 Py_DECREF (pvalue);
155 }
156 else if (PyErr_Occurred ())
157 {
158 PyErr_Clear();
159 }
160 Py_INCREF (session_dict);
161 }
162 else if (PyErr_Occurred())
163 {
164 PyErr_Clear();
165 }
166 }
167 else if (PyErr_Occurred())
168 {
169 PyErr_Clear();
170 }
171 }
172 else if (PyErr_Occurred ())
173 {
174 PyErr_Clear ();
175 }
176 return stop_at_breakpoint;
177}
178
Johnny Chenf3ec4612012-08-09 23:09:42 +0000179// This function is called by lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...)
180// and is used when a script command is attached to a watchpoint for execution.
181
182SWIGEXPORT bool
183LLDBSwigPythonWatchpointCallbackFunction
184(
185 const char *python_function_name,
186 const char *session_dictionary_name,
187 const lldb::StackFrameSP& frame_sp,
188 const lldb::WatchpointSP& wp_sp
189)
190{
191 lldb::SBFrame sb_frame (frame_sp);
192 lldb::SBWatchpoint sb_wp(wp_sp);
193
194 bool stop_at_watchpoint = true;
195 PyObject *Frame_PyObj = SWIG_NewPointerObj((void *) &sb_frame, SWIGTYPE_p_lldb__SBFrame, 0);
196 PyObject *Wp_PyObj = SWIG_NewPointerObj ((void *) &sb_wp, SWIGTYPE_p_lldb__SBWatchpoint, 0);
197
198 if (Frame_PyObj == NULL || Wp_PyObj == NULL)
199 return stop_at_watchpoint;
200
201 if (!python_function_name || !session_dictionary_name)
202 return stop_at_watchpoint;
203
204 PyObject *session_dict, *pfunc;
205 PyObject *pargs, *pvalue;
206
207 session_dict = FindSessionDictionary (session_dictionary_name);
208 if (session_dict != NULL)
209 {
210 pfunc = ResolvePythonName (python_function_name, session_dict);
211 if (pfunc != NULL)
212 {
213 // Set up the arguments and call the function.
214
215 if (PyCallable_Check (pfunc))
216 {
217 pargs = PyTuple_New (3);
218 if (pargs == NULL)
219 {
220 if (PyErr_Occurred())
221 PyErr_Clear();
222 return stop_at_watchpoint;
223 }
224
225 PyTuple_SetItem (pargs, 0, Frame_PyObj); // This "steals" a reference to Frame_PyObj
226 PyTuple_SetItem (pargs, 1, Wp_PyObj); // This "steals" a reference to Wp_PyObj
227 PyTuple_SetItem (pargs, 2, session_dict); // This "steals" a reference to session_dict
228 pvalue = PyObject_CallObject (pfunc, pargs);
229 Py_DECREF (pargs);
230
231 if (pvalue != NULL)
232 {
233 Py_DECREF (pvalue);
234 }
235 else if (PyErr_Occurred ())
236 {
237 PyErr_Clear();
238 }
239 Py_INCREF (session_dict);
240 }
241 else if (PyErr_Occurred())
242 {
243 PyErr_Clear();
244 }
245 }
246 else if (PyErr_Occurred())
247 {
248 PyErr_Clear();
249 }
250 }
251 else if (PyErr_Occurred ())
252 {
253 PyErr_Clear ();
254 }
255 return stop_at_watchpoint;
256}
257
Enrico Granata1328b142012-02-29 03:28:49 +0000258SWIGEXPORT bool
Johnny Chenf307cf72011-07-26 19:09:03 +0000259LLDBSwigPythonCallTypeScript
260(
261 const char *python_function_name,
Enrico Granata1328b142012-02-29 03:28:49 +0000262 const void *session_dictionary,
263 const lldb::ValueObjectSP& valobj_sp,
264 void** pyfunct_wrapper,
265 std::string& retval
Johnny Chenf307cf72011-07-26 19:09:03 +0000266)
267{
268 lldb::SBValue sb_value (valobj_sp);
269
Enrico Granata3370f0c2011-08-19 23:56:34 +0000270 PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *) &sb_value, SWIGTYPE_p_lldb__SBValue, 0);
Johnny Chenf307cf72011-07-26 19:09:03 +0000271
272 if (ValObj_PyObj == NULL)
Enrico Granata1328b142012-02-29 03:28:49 +0000273 return false;
Johnny Chenf307cf72011-07-26 19:09:03 +0000274
Enrico Granata1328b142012-02-29 03:28:49 +0000275 if (!python_function_name || !session_dictionary)
276 return false;
Johnny Chenf307cf72011-07-26 19:09:03 +0000277
Enrico Granata1328b142012-02-29 03:28:49 +0000278 PyObject *session_dict = (PyObject*)session_dictionary, *pfunc = NULL, *pargs = NULL, *pvalue = NULL;
Johnny Chenf307cf72011-07-26 19:09:03 +0000279
Enrico Granata1328b142012-02-29 03:28:49 +0000280 if (pyfunct_wrapper && *pyfunct_wrapper && PyFunction_Check (*pyfunct_wrapper))
Johnny Chenf307cf72011-07-26 19:09:03 +0000281 {
Enrico Granata1328b142012-02-29 03:28:49 +0000282 pfunc = (PyObject*)(*pyfunct_wrapper);
283 if (pfunc->ob_refcnt == 1)
Johnny Chenf307cf72011-07-26 19:09:03 +0000284 {
Enrico Granata1328b142012-02-29 03:28:49 +0000285 Py_XDECREF(pfunc);
286 pfunc = NULL;
Johnny Chenf307cf72011-07-26 19:09:03 +0000287 }
288 }
Enrico Granata1328b142012-02-29 03:28:49 +0000289
290 if (PyDict_Check(session_dict))
Johnny Chenf307cf72011-07-26 19:09:03 +0000291 {
Enrico Granata1328b142012-02-29 03:28:49 +0000292 PyErr_Cleaner pyerr_cleanup(true); // show Python errors
293
294 if (!pfunc)
295 {
296 pfunc = ResolvePythonName (python_function_name, session_dict);
297 if (!pfunc || !PyFunction_Check (pfunc))
298 return false;
299 else
300 {
301 if (pyfunct_wrapper)
302 *pyfunct_wrapper = pfunc;
303 }
304 }
305 /*else
306 printf("caching works!!!!\n");*/
307
308 pargs = PyTuple_Pack(2, ValObj_PyObj, session_dict);
309 if (pargs == NULL)
310 return false;
311
312 pvalue = PyObject_CallObject (pfunc, pargs);
313 Py_DECREF (pargs);
314
315 if (pvalue != NULL && pvalue != Py_None && PyString_Check(pvalue))
316 retval.assign(PyString_AsString(pvalue));
317 Py_XDECREF (pvalue);
318 Py_INCREF (session_dict);
Johnny Chenf307cf72011-07-26 19:09:03 +0000319 }
Enrico Granata1328b142012-02-29 03:28:49 +0000320 return true;
Johnny Chenf307cf72011-07-26 19:09:03 +0000321}
322
323SWIGEXPORT void*
324LLDBSwigPythonCreateSyntheticProvider
325(
326 const std::string python_class_name,
327 const char *session_dictionary_name,
328 const lldb::ValueObjectSP& valobj_sp
329)
330{
331 PyObject* retval = NULL;
332
333 if (python_class_name.empty() || !session_dictionary_name)
334 Py_RETURN_NONE;
335
Enrico Granata3370f0c2011-08-19 23:56:34 +0000336 // I do not want the SBValue to be deallocated when going out of scope because python
337 // has ownership of it and will manage memory for this object by itself
338 lldb::SBValue *valobj_sb = new lldb::SBValue(valobj_sp);
Enrico Granata3d656c72012-10-22 18:18:36 +0000339 valobj_sb->SetPreferSyntheticValue(false);
Johnny Chenf307cf72011-07-26 19:09:03 +0000340
Enrico Granatacf09f882012-03-19 22:58:49 +0000341 PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *)valobj_sb, SWIGTYPE_p_lldb__SBValue, 0);
Johnny Chenf307cf72011-07-26 19:09:03 +0000342
343 if (ValObj_PyObj == NULL)
344 Py_RETURN_NONE;
345
346 const char* python_function_name = python_class_name.c_str();
347
Enrico Granata960691f2011-08-22 17:34:47 +0000348 PyObject *session_dict, *pfunc;
Johnny Chenf307cf72011-07-26 19:09:03 +0000349 PyObject *pvalue;
Enrico Granata960691f2011-08-22 17:34:47 +0000350
351 session_dict = FindSessionDictionary (session_dictionary_name);
352 if (session_dict != NULL)
Johnny Chenf307cf72011-07-26 19:09:03 +0000353 {
Enrico Granata960691f2011-08-22 17:34:47 +0000354 pfunc = ResolvePythonName (python_function_name, session_dict);
355 if (pfunc != NULL)
Johnny Chenf307cf72011-07-26 19:09:03 +0000356 {
Johnny Chenf307cf72011-07-26 19:09:03 +0000357 // Set up the arguments and call the function.
Enrico Granata960691f2011-08-22 17:34:47 +0000358
359 if (PyCallable_Check (pfunc))
Johnny Chenf307cf72011-07-26 19:09:03 +0000360 {
361 PyObject *argList = Py_BuildValue("SS", ValObj_PyObj, session_dict);
362
363 if (PyErr_Occurred ())
364 {
365 PyErr_Print();
366 PyErr_Clear();
367 return retval;
368 }
369
370 if (argList == NULL)
371 {
372 return retval;
373 }
374
375 Py_INCREF(ValObj_PyObj);
376
377 pvalue = PyObject_CallObject(pfunc, argList);
378
379 Py_DECREF(argList);
380
381 if (pvalue != NULL)
382 {
383 if (pvalue != Py_None)
384 retval = pvalue;
385 else
386 {
387 retval = Py_None;
388 Py_INCREF(retval);
389 }
390 }
391 else if (PyErr_Occurred ())
392 {
393 PyErr_Print();
394 PyErr_Clear();
395 }
396 Py_INCREF (session_dict);
397 }
398 else if (PyErr_Occurred())
399 {
400 PyErr_Print();
401 PyErr_Clear();
402 }
403 }
404 else if (PyErr_Occurred())
405 {
406 PyErr_Print();
407 PyErr_Clear();
408 }
409 }
410 else if (PyErr_Occurred ())
411 {
412 PyErr_Print();
413 PyErr_Clear ();
414 }
415 if (retval)
416 return retval;
417 else
418 Py_RETURN_NONE;
419}
420
Enrico Granata800332c2012-10-23 19:54:09 +0000421// 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 if (self == NULL || self == Py_None)
432 {
433 if (was_found)
434 *was_found = false;
435 Py_XINCREF(ret_if_not_found);
436 return ret_if_not_found;
437 }
438
439 PyObject* pmeth = PyObject_GetAttrString(self, callee_name);
440
441 if (PyErr_Occurred())
442 {
443 PyErr_Clear();
444 }
445
446 if (pmeth == NULL || pmeth == Py_None)
447 {
448 if (was_found)
449 *was_found = false;
450 Py_XDECREF(pmeth);
451 Py_XINCREF(ret_if_not_found);
452 return ret_if_not_found;
453 }
454
455 if (PyCallable_Check(pmeth) == 0)
456 {
457 if (PyErr_Occurred())
458 {
459 PyErr_Clear();
460 }
461
462 Py_XDECREF(pmeth);
463 if (was_found)
464 *was_found = false;
465 Py_XINCREF(ret_if_not_found);
466 return ret_if_not_found;
467 }
468
469 if (was_found)
470 *was_found = true;
471
472 if (PyErr_Occurred())
473 {
474 PyErr_Clear();
475 }
476
477 Py_XDECREF(pmeth);
478
479 // right now we know this function exists and is callable..
480 PyObject* py_return = PyObject_CallMethod(self, callee_name, NULL);
481
482 // if it fails, print the error but otherwise go on
483 if (PyErr_Occurred())
484 {
485 PyErr_Print();
486 PyErr_Clear();
487 }
488
489 return py_return;
490}
Johnny Chenf307cf72011-07-26 19:09:03 +0000491
492SWIGEXPORT uint32_t
493LLDBSwigPython_CalculateNumChildren
494(
495 PyObject *implementor
496)
497{
Enrico Granata800332c2012-10-23 19:54:09 +0000498 uint32_t ret_val = UINT32_MAX;
Johnny Chenf307cf72011-07-26 19:09:03 +0000499
500 static char callee_name[] = "num_children";
501
Enrico Granata800332c2012-10-23 19:54:09 +0000502 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, NULL);
Johnny Chenf307cf72011-07-26 19:09:03 +0000503
Enrico Granata800332c2012-10-23 19:54:09 +0000504 if (!py_return)
505 return ret_val;
506
507 if (PyInt_Check(py_return))
508 ret_val = PyInt_AsLong(py_return);
509
510 Py_XDECREF(py_return);
511
Johnny Chenf307cf72011-07-26 19:09:03 +0000512 if (PyErr_Occurred())
513 {
514 PyErr_Print();
515 PyErr_Clear();
516 }
Enrico Granata800332c2012-10-23 19:54:09 +0000517
518 return ret_val;
Johnny Chenf307cf72011-07-26 19:09:03 +0000519}
520
521SWIGEXPORT PyObject*
522LLDBSwigPython_GetChildAtIndex
523(
524 PyObject *implementor,
525 uint32_t idx
526)
527{
528
529 static char callee_name[] = "get_child_at_index";
530 static char param_format[] = "i";
531
532 if (implementor == NULL || implementor == Py_None)
533 return NULL;
534 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, idx);
535 if (PyErr_Occurred())
536 {
537 PyErr_Print();
538 PyErr_Clear();
539 }
540
541 if (py_return == NULL || py_return == Py_None)
542 {
543 Py_XDECREF(py_return);
544 return NULL;
545 }
546
547 lldb::SBValue* sbvalue_ptr = NULL;
548
549 if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
550 {
551 Py_DECREF(py_return);
552 return NULL;
553 }
554
555 if (sbvalue_ptr == NULL)
556 return NULL;
557
558 return py_return;
559}
560
561SWIGEXPORT int
562LLDBSwigPython_GetIndexOfChildWithName
563(
564 PyObject *implementor,
565 const char* child_name
566)
567{
568 static char callee_name[] = "get_child_index";
569 static char param_format[] = "s";
570
571 if (implementor == NULL || implementor == Py_None)
572 return 0;
573 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, child_name);
574 if (PyErr_Occurred())
575 {
576 PyErr_Print();
577 PyErr_Clear();
578 }
579
580 if (py_return == NULL || py_return == Py_None)
581 {
582 Py_XDECREF(py_return);
583 return UINT32_MAX;
584 }
585 long retval = PyInt_AsLong(py_return);
586 Py_DECREF(py_return);
587 if (retval >= 0)
588 return (uint32_t)retval;
589 if (PyErr_Occurred())
590 {
591 PyErr_Print();
592 PyErr_Clear();
593 }
594 return 0;
595}
596
Enrico Granatacf09f882012-03-19 22:58:49 +0000597SWIGEXPORT bool
Enrico Granata6a319e42011-07-26 21:02:56 +0000598LLDBSwigPython_UpdateSynthProviderInstance
599(
600 PyObject *implementor
601)
602{
Enrico Granatacf09f882012-03-19 22:58:49 +0000603 bool ret_val = false;
604
Enrico Granata6a319e42011-07-26 21:02:56 +0000605 static char callee_name[] = "update";
606
Enrico Granata800332c2012-10-23 19:54:09 +0000607 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name);
Enrico Granata6a319e42011-07-26 21:02:56 +0000608
Enrico Granatacf09f882012-03-19 22:58:49 +0000609 if (py_return == Py_True)
610 ret_val = true;
Enrico Granata6a319e42011-07-26 21:02:56 +0000611
612 Py_XDECREF(py_return);
Enrico Granatacf09f882012-03-19 22:58:49 +0000613
614 return ret_val;
Enrico Granata800332c2012-10-23 19:54:09 +0000615}
Enrico Granata6a319e42011-07-26 21:02:56 +0000616
Enrico Granata800332c2012-10-23 19:54:09 +0000617SWIGEXPORT bool
618LLDBSwigPython_MightHaveChildrenSynthProviderInstance
619(
620 PyObject *implementor
621)
622{
623 bool ret_val = false;
624
625 static char callee_name[] = "has_children";
626
627 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True);
628
629 if (py_return == Py_True)
630 ret_val = true;
631
632 Py_XDECREF(py_return);
633
634 return ret_val;
Enrico Granata6a319e42011-07-26 21:02:56 +0000635}
636
Enrico Granata91544802011-09-06 19:20:51 +0000637SWIGEXPORT void*
Johnny Chenf307cf72011-07-26 19:09:03 +0000638LLDBSWIGPython_CastPyObjectToSBValue
639(
640 PyObject* data
641)
642{
643 lldb::SBValue* sb_ptr = NULL;
644
645 int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
646
647 if (valid_cast == -1)
648 return NULL;
649
650 return sb_ptr;
651}
652
Enrico Granata872959b2011-08-20 00:26:17 +0000653// Currently, SBCommandReturnObjectReleaser wraps an std::auto_ptr to an
654// lldb_private::CommandReturnObject. This means that the destructor for the
655// SB object will deallocate its contained CommandReturnObject. Because that
656// object is used as the real return object for Python-based commands, we want
657// it to stay around. Thus, we release the auto_ptr before returning from
658// LLDBSwigPythonCallCommand, and to guarantee that the release will occur no
659// matter how we exit from the function, we have a releaser object whose
660// destructor does the right thing for us
661class SBCommandReturnObjectReleaser
662{
663public:
664 SBCommandReturnObjectReleaser (lldb::SBCommandReturnObject &obj) :
665 m_command_return_object_ref (obj)
666 {
667 }
668
669 ~SBCommandReturnObjectReleaser ()
670 {
671 m_command_return_object_ref.Release();
672 }
673private:
674 lldb::SBCommandReturnObject &m_command_return_object_ref;
675};
Enrico Granata3370f0c2011-08-19 23:56:34 +0000676
Enrico Granatac2a28252011-08-16 16:49:25 +0000677SWIGEXPORT bool
678LLDBSwigPythonCallCommand
679(
680 const char *python_function_name,
681 const char *session_dictionary_name,
682 lldb::DebuggerSP& debugger,
683 const char* args,
684 std::string& err_msg,
Enrico Granata3370f0c2011-08-19 23:56:34 +0000685 lldb_private::CommandReturnObject& cmd_retobj
Enrico Granatac2a28252011-08-16 16:49:25 +0000686)
687{
688
Enrico Granata3370f0c2011-08-19 23:56:34 +0000689 lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj);
Enrico Granata872959b2011-08-20 00:26:17 +0000690 SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb);
Enrico Granata3370f0c2011-08-19 23:56:34 +0000691 lldb::SBDebugger debugger_sb(debugger);
Enrico Granata6b1596d2011-08-16 23:24:13 +0000692
Enrico Granatac2a28252011-08-16 16:49:25 +0000693 bool retval = false;
694
Enrico Granata3370f0c2011-08-19 23:56:34 +0000695 PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
696 PyObject *CmdRetObj_PyObj = SWIG_NewPointerObj((void *) &cmd_retobj_sb, SWIGTYPE_p_lldb__SBCommandReturnObject, 0);
Enrico Granatac2a28252011-08-16 16:49:25 +0000697
698 if (DebuggerObj_PyObj == NULL)
Enrico Granata872959b2011-08-20 00:26:17 +0000699 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000700
Enrico Granata6b1596d2011-08-16 23:24:13 +0000701 if (CmdRetObj_PyObj == NULL)
Enrico Granata872959b2011-08-20 00:26:17 +0000702 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000703
704 if (!python_function_name || !session_dictionary_name)
Enrico Granata872959b2011-08-20 00:26:17 +0000705 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000706
Enrico Granata960691f2011-08-22 17:34:47 +0000707 PyObject *session_dict, *pfunc;
Enrico Granatac2a28252011-08-16 16:49:25 +0000708 PyObject *pargs, *pvalue;
709
Enrico Granata960691f2011-08-22 17:34:47 +0000710 session_dict = FindSessionDictionary (session_dictionary_name);
711 if (session_dict != NULL)
Enrico Granatac2a28252011-08-16 16:49:25 +0000712 {
Enrico Granata960691f2011-08-22 17:34:47 +0000713 pfunc = ResolvePythonName (python_function_name, session_dict);
714 if (pfunc != NULL)
Enrico Granatac2a28252011-08-16 16:49:25 +0000715 {
Enrico Granatac2a28252011-08-16 16:49:25 +0000716 // Set up the arguments and call the function.
717
Enrico Granata960691f2011-08-22 17:34:47 +0000718 if (PyCallable_Check (pfunc))
Enrico Granatac2a28252011-08-16 16:49:25 +0000719 {
720 pargs = PyTuple_New (4);
721 if (pargs == NULL)
722 {
723 if (PyErr_Occurred())
724 PyErr_Clear();
Enrico Granata872959b2011-08-20 00:26:17 +0000725 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000726 }
727
728 PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj
729 PyTuple_SetItem (pargs, 1, PyString_FromString(args));
Enrico Granata6b1596d2011-08-16 23:24:13 +0000730 PyTuple_SetItem (pargs, 2, CmdRetObj_PyObj); // This "steals" a reference to CmdRetObj_PyObj
Enrico Granatac2a28252011-08-16 16:49:25 +0000731 PyTuple_SetItem (pargs, 3, session_dict); // This "steals" a reference to session_dict
732 pvalue = PyObject_CallObject (pfunc, pargs);
733 Py_DECREF (pargs);
734
735 if (pvalue != NULL)
736 {
737 if (pvalue == Py_None) // no error
738 {
739 err_msg.clear();
740 retval = true;
741 }
Johnny Chen2fcf4122011-12-14 20:40:27 +0000742 else
Enrico Granatac2a28252011-08-16 16:49:25 +0000743 {
Johnny Chen2fcf4122011-12-14 20:40:27 +0000744 // return value is an error string
745 if (PyString_CheckExact(pvalue))
746 err_msg.assign(PyString_AsString(pvalue));
Enrico Granatac2a28252011-08-16 16:49:25 +0000747 retval = false;
748 }
749 Py_DECREF (pvalue);
750 }
751 else if (PyErr_Occurred ())
752 {
753 PyErr_Print();
754 PyErr_Clear();
755 }
756 Py_INCREF (session_dict);
757 }
758 else if (PyErr_Occurred())
759 {
760 PyErr_Print();
761 PyErr_Clear();
762 }
763 }
764 else if (PyErr_Occurred())
765 {
766 PyErr_Print();
767 PyErr_Clear();
768 }
769 }
770 else if (PyErr_Occurred ())
771 {
772 PyErr_Print();
773 PyErr_Clear ();
774 }
Enrico Granata59df36f2011-10-17 21:45:27 +0000775 return retval;
776}
777
Enrico Granata155ee912012-08-24 00:30:47 +0000778SWIGEXPORT void*
779LLDBSWIGPythonCreateOSPlugin
780(
781 const std::string python_class_name,
782 const char *session_dictionary_name,
783 const lldb::ProcessSP& process_sp
784)
785{
786 PyObject* retval = NULL;
787
788 if (python_class_name.empty() || !session_dictionary_name)
789 Py_RETURN_NONE;
790
791 // I do not want the SBValue to be deallocated when going out of scope because python
792 // has ownership of it and will manage memory for this object by itself
793 lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp);
794
795 PyObject *SBProc_PyObj = SWIG_NewPointerObj((void *)process_sb, SWIGTYPE_p_lldb__SBProcess, 0);
796
797 if (SBProc_PyObj == NULL)
798 Py_RETURN_NONE;
799
800 const char* python_function_name = python_class_name.c_str();
801
802 PyObject *session_dict, *pfunc;
803 PyObject *pvalue;
804
805 session_dict = FindSessionDictionary (session_dictionary_name);
806 if (session_dict != NULL)
807 {
808 pfunc = ResolvePythonName (python_function_name, session_dict);
809 if (pfunc != NULL)
810 {
811 // Set up the arguments and call the function.
812
813 if (PyCallable_Check (pfunc))
814 {
Enrico Granatacec963a2012-08-24 01:34:39 +0000815 PyObject *argList = Py_BuildValue("(O)", SBProc_PyObj);
Enrico Granata155ee912012-08-24 00:30:47 +0000816
817 if (PyErr_Occurred ())
818 {
819 PyErr_Print();
820 PyErr_Clear();
821 return retval;
822 }
823
824 if (argList == NULL)
825 {
826 return retval;
827 }
828
829 Py_INCREF(SBProc_PyObj);
830
831 pvalue = PyObject_CallObject(pfunc, argList);
832
833 Py_DECREF(argList);
834
835 if (pvalue != NULL)
836 {
837 if (pvalue != Py_None)
838 retval = pvalue;
839 else
840 {
841 retval = Py_None;
842 Py_INCREF(retval);
843 }
844 }
845 else if (PyErr_Occurred ())
846 {
847 PyErr_Print();
848 PyErr_Clear();
849 }
850 Py_INCREF (session_dict);
851 }
852 else if (PyErr_Occurred())
853 {
854 PyErr_Print();
855 PyErr_Clear();
856 }
857 }
858 else if (PyErr_Occurred())
859 {
860 PyErr_Print();
861 PyErr_Clear();
862 }
863 }
864 else if (PyErr_Occurred ())
865 {
866 PyErr_Print();
867 PyErr_Clear ();
868 }
869 if (retval)
870 return retval;
871 else
872 Py_RETURN_NONE;
873}
874
Enrico Granata59df36f2011-10-17 21:45:27 +0000875SWIGEXPORT bool
876LLDBSwigPythonCallModuleInit
877(
878 const std::string python_module_name,
879 const char *session_dictionary_name,
880 lldb::DebuggerSP& debugger
881)
882{
883
884 lldb::SBDebugger debugger_sb(debugger);
885
886 bool retval = false;
887
888 PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
889
890 if (DebuggerObj_PyObj == NULL)
891 return retval;
892
893 if (!(python_module_name.length()) || !session_dictionary_name)
894 return retval;
895
896 PyObject *session_dict, *pfunc;
897 PyObject *pargs, *pvalue;
898
899 session_dict = FindSessionDictionary (session_dictionary_name);
900
901 std::string python_function_name_string = python_module_name + (".__lldb_init_module");
902 const char* python_function_name = python_function_name_string.c_str();
903
904 if (session_dict != NULL)
905 {
906 pfunc = ResolvePythonName (python_function_name, session_dict);
907
908 if (PyErr_Occurred()) // this might not exist.. let's make sure we handle that
909 {
910 PyErr_Clear();
911 return true;
912 }
913
914 if (pfunc == NULL)
915 return true;
916 else
917 {
918 // Set up the arguments and call the function.
919
920 if (PyCallable_Check (pfunc))
921 {
922 pargs = PyTuple_New (2);
923 if (pargs == NULL)
924 {
925 if (PyErr_Occurred())
926 PyErr_Clear();
927 return retval;
928 }
929
930 PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj
931 PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict
932 pvalue = PyObject_CallObject (pfunc, pargs);
933 Py_DECREF (pargs);
934
935 if (PyErr_Occurred ())
936 {
937 PyErr_Print();
938 PyErr_Clear();
939 }
940 else
941 {
942 retval = true;
943 Py_XDECREF (pvalue);
944 }
945 Py_INCREF (session_dict);
946 }
947 else if (PyErr_Occurred())
948 {
949 PyErr_Print();
950 PyErr_Clear();
951 }
952 }
953 }
954 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000955}
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +0000956%}
Enrico Granatac2a28252011-08-16 16:49:25 +0000957
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +0000958
959%runtime %{
960// Forward declaration to be inserted at the start of LLDBWrapPython.h
961// I used runtime as a hack to make SWIG place it where it's needed.
962// This is needed to use LLDBSwigPythonCallSBInputReaderCallback in the
963// typemaps and in the extensions (SBInputReader.__del__()).
Filipe Cabecinhas85fbe9d2012-08-22 18:10:45 +0000964#include "lldb/API/SBInputReader.h"
Filipe Cabecinhas43898d72012-08-25 00:29:07 +0000965#include "lldb/API/SBDebugger.h"
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +0000966
967size_t
968LLDBSwigPythonCallSBInputReaderCallback(void *baton,
969 lldb::SBInputReader *reader,
970 lldb::InputReaderAction notification,
971 const char*bytes,
972 size_t bytes_len);
Filipe Cabecinhas43898d72012-08-25 00:29:07 +0000973
974void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton);
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +0000975%}
976
977%wrapper %{
978// For the InputReader Callback functions
979SWIGEXPORT size_t
980LLDBSwigPythonCallSBInputReaderCallback(void *baton,
981 lldb::SBInputReader *reader,
982 lldb::InputReaderAction notification,
983 const char*bytes,
984 size_t bytes_len) {
Filipe Cabecinhas43898d72012-08-25 00:29:07 +0000985 if (baton != Py_None) {
986 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
987
988 PyObject *py_InputReader = SWIG_NewPointerObj(reader, SWIGTYPE_p_lldb__SBInputReader, false);
989 PyObject *py_Notification = PyInt_FromLong(notification);
990 PyObject *py_Bytes = PyBytes_FromStringAndSize(bytes, bytes_len);
991
992 PyObject *tuple = PyTuple_Pack(3, py_InputReader, py_Notification, py_Bytes);
993 PyObject *res = PyObject_Call(reinterpret_cast<PyObject*>(baton), tuple, NULL);
994 Py_DECREF(tuple);
995 Py_DECREF(py_InputReader);
996 Py_DECREF(py_Notification);
997 Py_DECREF(py_Bytes);
998
999 if (res == NULL) {
1000 PyObject *exc = PyErr_Occurred();
1001 if (exc) {
1002 ::puts("\nErroring out at LLDBSwigPythonCallSBInputReaderCallback");
1003 PyErr_Print();
1004 }
1005 return 0;
1006 }
1007
1008 size_t result = 0;
1009 // If the callback misbehaves and returns Py_None, assume it returned 0
1010 if (res != Py_None)
1011 result = static_cast<size_t>(PyInt_AsSsize_t(res));
1012
1013 Py_DECREF(res);
1014 SWIG_PYTHON_THREAD_END_BLOCK;
1015 return result;
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +00001016 }
Enrico Granatae25c6312012-08-27 18:30:45 +00001017 return 0;
Filipe Cabecinhas43898d72012-08-25 00:29:07 +00001018}
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +00001019
Filipe Cabecinhas43898d72012-08-25 00:29:07 +00001020// For the LogOutputCallback functions
1021void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) {
1022 if (baton != Py_None) {
1023 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
1024 PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str);
1025 SWIG_PYTHON_THREAD_END_BLOCK;
1026 }
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +00001027}
Johnny Chenf307cf72011-07-26 19:09:03 +00001028%}