blob: 2189193cfbbe008855c3152cb4959231f3f3bf83 [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
Greg Claytonec1e8232013-01-16 19:53:55 +0000270 retval.clear();
271
Enrico Granata3370f0c2011-08-19 23:56:34 +0000272 PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *) &sb_value, SWIGTYPE_p_lldb__SBValue, 0);
Johnny Chenf307cf72011-07-26 19:09:03 +0000273
274 if (ValObj_PyObj == NULL)
Enrico Granata1328b142012-02-29 03:28:49 +0000275 return false;
Johnny Chenf307cf72011-07-26 19:09:03 +0000276
Enrico Granata1328b142012-02-29 03:28:49 +0000277 if (!python_function_name || !session_dictionary)
278 return false;
Johnny Chenf307cf72011-07-26 19:09:03 +0000279
Enrico Granata1328b142012-02-29 03:28:49 +0000280 PyObject *session_dict = (PyObject*)session_dictionary, *pfunc = NULL, *pargs = NULL, *pvalue = NULL;
Johnny Chenf307cf72011-07-26 19:09:03 +0000281
Enrico Granata1328b142012-02-29 03:28:49 +0000282 if (pyfunct_wrapper && *pyfunct_wrapper && PyFunction_Check (*pyfunct_wrapper))
Johnny Chenf307cf72011-07-26 19:09:03 +0000283 {
Enrico Granata1328b142012-02-29 03:28:49 +0000284 pfunc = (PyObject*)(*pyfunct_wrapper);
285 if (pfunc->ob_refcnt == 1)
Johnny Chenf307cf72011-07-26 19:09:03 +0000286 {
Enrico Granata1328b142012-02-29 03:28:49 +0000287 Py_XDECREF(pfunc);
288 pfunc = NULL;
Johnny Chenf307cf72011-07-26 19:09:03 +0000289 }
290 }
Enrico Granata1328b142012-02-29 03:28:49 +0000291
292 if (PyDict_Check(session_dict))
Johnny Chenf307cf72011-07-26 19:09:03 +0000293 {
Enrico Granata1328b142012-02-29 03:28:49 +0000294 PyErr_Cleaner pyerr_cleanup(true); // show Python errors
295
296 if (!pfunc)
297 {
298 pfunc = ResolvePythonName (python_function_name, session_dict);
299 if (!pfunc || !PyFunction_Check (pfunc))
300 return false;
301 else
302 {
303 if (pyfunct_wrapper)
304 *pyfunct_wrapper = pfunc;
305 }
306 }
307 /*else
308 printf("caching works!!!!\n");*/
309
310 pargs = PyTuple_Pack(2, ValObj_PyObj, session_dict);
311 if (pargs == NULL)
312 return false;
313
314 pvalue = PyObject_CallObject (pfunc, pargs);
315 Py_DECREF (pargs);
316
317 if (pvalue != NULL && pvalue != Py_None && PyString_Check(pvalue))
318 retval.assign(PyString_AsString(pvalue));
319 Py_XDECREF (pvalue);
320 Py_INCREF (session_dict);
Johnny Chenf307cf72011-07-26 19:09:03 +0000321 }
Enrico Granata1328b142012-02-29 03:28:49 +0000322 return true;
Johnny Chenf307cf72011-07-26 19:09:03 +0000323}
324
325SWIGEXPORT void*
326LLDBSwigPythonCreateSyntheticProvider
327(
Greg Claytonec1e8232013-01-16 19:53:55 +0000328 const char *python_class_name,
Johnny Chenf307cf72011-07-26 19:09:03 +0000329 const char *session_dictionary_name,
330 const lldb::ValueObjectSP& valobj_sp
331)
332{
333 PyObject* retval = NULL;
334
Greg Claytonec1e8232013-01-16 19:53:55 +0000335 if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
Johnny Chenf307cf72011-07-26 19:09:03 +0000336 Py_RETURN_NONE;
337
Enrico Granata3370f0c2011-08-19 23:56:34 +0000338 // I do not want the SBValue to be deallocated when going out of scope because python
339 // has ownership of it and will manage memory for this object by itself
340 lldb::SBValue *valobj_sb = new lldb::SBValue(valobj_sp);
Enrico Granata3d656c72012-10-22 18:18:36 +0000341 valobj_sb->SetPreferSyntheticValue(false);
Johnny Chenf307cf72011-07-26 19:09:03 +0000342
Enrico Granatacf09f882012-03-19 22:58:49 +0000343 PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *)valobj_sb, SWIGTYPE_p_lldb__SBValue, 0);
Johnny Chenf307cf72011-07-26 19:09:03 +0000344
345 if (ValObj_PyObj == NULL)
346 Py_RETURN_NONE;
347
Greg Claytonec1e8232013-01-16 19:53:55 +0000348 const char* python_function_name = python_class_name;
Johnny Chenf307cf72011-07-26 19:09:03 +0000349
Enrico Granata960691f2011-08-22 17:34:47 +0000350 PyObject *session_dict, *pfunc;
Johnny Chenf307cf72011-07-26 19:09:03 +0000351 PyObject *pvalue;
Enrico Granata960691f2011-08-22 17:34:47 +0000352
353 session_dict = FindSessionDictionary (session_dictionary_name);
354 if (session_dict != NULL)
Johnny Chenf307cf72011-07-26 19:09:03 +0000355 {
Enrico Granata960691f2011-08-22 17:34:47 +0000356 pfunc = ResolvePythonName (python_function_name, session_dict);
357 if (pfunc != NULL)
Johnny Chenf307cf72011-07-26 19:09:03 +0000358 {
Johnny Chenf307cf72011-07-26 19:09:03 +0000359 // Set up the arguments and call the function.
Enrico Granata960691f2011-08-22 17:34:47 +0000360
361 if (PyCallable_Check (pfunc))
Johnny Chenf307cf72011-07-26 19:09:03 +0000362 {
363 PyObject *argList = Py_BuildValue("SS", ValObj_PyObj, session_dict);
364
365 if (PyErr_Occurred ())
366 {
367 PyErr_Print();
368 PyErr_Clear();
369 return retval;
370 }
371
372 if (argList == NULL)
373 {
374 return retval;
375 }
376
377 Py_INCREF(ValObj_PyObj);
378
379 pvalue = PyObject_CallObject(pfunc, argList);
380
381 Py_DECREF(argList);
382
383 if (pvalue != NULL)
384 {
385 if (pvalue != Py_None)
386 retval = pvalue;
387 else
388 {
389 retval = Py_None;
390 Py_INCREF(retval);
391 }
392 }
393 else if (PyErr_Occurred ())
394 {
395 PyErr_Print();
396 PyErr_Clear();
397 }
398 Py_INCREF (session_dict);
399 }
400 else if (PyErr_Occurred())
401 {
402 PyErr_Print();
403 PyErr_Clear();
404 }
405 }
406 else if (PyErr_Occurred())
407 {
408 PyErr_Print();
409 PyErr_Clear();
410 }
411 }
412 else if (PyErr_Occurred ())
413 {
414 PyErr_Print();
415 PyErr_Clear ();
416 }
417 if (retval)
418 return retval;
419 else
420 Py_RETURN_NONE;
421}
422
Enrico Granata800332c2012-10-23 19:54:09 +0000423// wrapper that calls an optional instance member of an object taking no arguments
424static PyObject*
425LLDBSwigPython_CallOptionalMember
426(
427 PyObject* self,
428 char* callee_name,
429 PyObject* ret_if_not_found = Py_None,
430 bool* was_found = NULL
431)
432{
433 if (self == NULL || self == Py_None)
434 {
435 if (was_found)
436 *was_found = false;
437 Py_XINCREF(ret_if_not_found);
438 return ret_if_not_found;
439 }
440
441 PyObject* pmeth = PyObject_GetAttrString(self, callee_name);
442
443 if (PyErr_Occurred())
444 {
445 PyErr_Clear();
446 }
447
448 if (pmeth == NULL || pmeth == Py_None)
449 {
450 if (was_found)
451 *was_found = false;
452 Py_XDECREF(pmeth);
453 Py_XINCREF(ret_if_not_found);
454 return ret_if_not_found;
455 }
456
457 if (PyCallable_Check(pmeth) == 0)
458 {
459 if (PyErr_Occurred())
460 {
461 PyErr_Clear();
462 }
463
464 Py_XDECREF(pmeth);
465 if (was_found)
466 *was_found = false;
467 Py_XINCREF(ret_if_not_found);
468 return ret_if_not_found;
469 }
470
471 if (was_found)
472 *was_found = true;
473
474 if (PyErr_Occurred())
475 {
476 PyErr_Clear();
477 }
478
479 Py_XDECREF(pmeth);
480
481 // right now we know this function exists and is callable..
482 PyObject* py_return = PyObject_CallMethod(self, callee_name, NULL);
483
484 // if it fails, print the error but otherwise go on
485 if (PyErr_Occurred())
486 {
487 PyErr_Print();
488 PyErr_Clear();
489 }
490
491 return py_return;
492}
Johnny Chenf307cf72011-07-26 19:09:03 +0000493
494SWIGEXPORT uint32_t
495LLDBSwigPython_CalculateNumChildren
496(
497 PyObject *implementor
498)
499{
Enrico Granata800332c2012-10-23 19:54:09 +0000500 uint32_t ret_val = UINT32_MAX;
Johnny Chenf307cf72011-07-26 19:09:03 +0000501
502 static char callee_name[] = "num_children";
503
Enrico Granata800332c2012-10-23 19:54:09 +0000504 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, NULL);
Johnny Chenf307cf72011-07-26 19:09:03 +0000505
Enrico Granata800332c2012-10-23 19:54:09 +0000506 if (!py_return)
507 return ret_val;
508
509 if (PyInt_Check(py_return))
510 ret_val = PyInt_AsLong(py_return);
511
512 Py_XDECREF(py_return);
513
Johnny Chenf307cf72011-07-26 19:09:03 +0000514 if (PyErr_Occurred())
515 {
516 PyErr_Print();
517 PyErr_Clear();
518 }
Enrico Granata800332c2012-10-23 19:54:09 +0000519
520 return ret_val;
Johnny Chenf307cf72011-07-26 19:09:03 +0000521}
522
523SWIGEXPORT PyObject*
524LLDBSwigPython_GetChildAtIndex
525(
526 PyObject *implementor,
527 uint32_t idx
528)
529{
530
531 static char callee_name[] = "get_child_at_index";
532 static char param_format[] = "i";
533
534 if (implementor == NULL || implementor == Py_None)
535 return NULL;
536 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, idx);
537 if (PyErr_Occurred())
538 {
539 PyErr_Print();
540 PyErr_Clear();
541 }
542
543 if (py_return == NULL || py_return == Py_None)
544 {
545 Py_XDECREF(py_return);
546 return NULL;
547 }
548
549 lldb::SBValue* sbvalue_ptr = NULL;
550
551 if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
552 {
553 Py_DECREF(py_return);
554 return NULL;
555 }
556
557 if (sbvalue_ptr == NULL)
558 return NULL;
559
560 return py_return;
561}
562
563SWIGEXPORT int
564LLDBSwigPython_GetIndexOfChildWithName
565(
566 PyObject *implementor,
567 const char* child_name
568)
569{
570 static char callee_name[] = "get_child_index";
571 static char param_format[] = "s";
572
573 if (implementor == NULL || implementor == Py_None)
574 return 0;
575 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, child_name);
576 if (PyErr_Occurred())
577 {
578 PyErr_Print();
579 PyErr_Clear();
580 }
581
582 if (py_return == NULL || py_return == Py_None)
583 {
584 Py_XDECREF(py_return);
585 return UINT32_MAX;
586 }
587 long retval = PyInt_AsLong(py_return);
588 Py_DECREF(py_return);
589 if (retval >= 0)
590 return (uint32_t)retval;
591 if (PyErr_Occurred())
592 {
593 PyErr_Print();
594 PyErr_Clear();
595 }
596 return 0;
597}
598
Enrico Granatacf09f882012-03-19 22:58:49 +0000599SWIGEXPORT bool
Enrico Granata6a319e42011-07-26 21:02:56 +0000600LLDBSwigPython_UpdateSynthProviderInstance
601(
602 PyObject *implementor
603)
604{
Enrico Granatacf09f882012-03-19 22:58:49 +0000605 bool ret_val = false;
606
Enrico Granata6a319e42011-07-26 21:02:56 +0000607 static char callee_name[] = "update";
608
Enrico Granata800332c2012-10-23 19:54:09 +0000609 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name);
Enrico Granata6a319e42011-07-26 21:02:56 +0000610
Enrico Granatacf09f882012-03-19 22:58:49 +0000611 if (py_return == Py_True)
612 ret_val = true;
Enrico Granata6a319e42011-07-26 21:02:56 +0000613
614 Py_XDECREF(py_return);
Enrico Granatacf09f882012-03-19 22:58:49 +0000615
616 return ret_val;
Enrico Granata800332c2012-10-23 19:54:09 +0000617}
Enrico Granata6a319e42011-07-26 21:02:56 +0000618
Enrico Granata800332c2012-10-23 19:54:09 +0000619SWIGEXPORT bool
620LLDBSwigPython_MightHaveChildrenSynthProviderInstance
621(
622 PyObject *implementor
623)
624{
625 bool ret_val = false;
626
627 static char callee_name[] = "has_children";
628
629 PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True);
630
631 if (py_return == Py_True)
632 ret_val = true;
633
634 Py_XDECREF(py_return);
635
636 return ret_val;
Enrico Granata6a319e42011-07-26 21:02:56 +0000637}
638
Enrico Granata91544802011-09-06 19:20:51 +0000639SWIGEXPORT void*
Johnny Chenf307cf72011-07-26 19:09:03 +0000640LLDBSWIGPython_CastPyObjectToSBValue
641(
642 PyObject* data
643)
644{
645 lldb::SBValue* sb_ptr = NULL;
646
647 int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
648
649 if (valid_cast == -1)
650 return NULL;
651
652 return sb_ptr;
653}
654
Enrico Granata872959b2011-08-20 00:26:17 +0000655// Currently, SBCommandReturnObjectReleaser wraps an std::auto_ptr to an
656// lldb_private::CommandReturnObject. This means that the destructor for the
657// SB object will deallocate its contained CommandReturnObject. Because that
658// object is used as the real return object for Python-based commands, we want
659// it to stay around. Thus, we release the auto_ptr before returning from
660// LLDBSwigPythonCallCommand, and to guarantee that the release will occur no
661// matter how we exit from the function, we have a releaser object whose
662// destructor does the right thing for us
663class SBCommandReturnObjectReleaser
664{
665public:
666 SBCommandReturnObjectReleaser (lldb::SBCommandReturnObject &obj) :
667 m_command_return_object_ref (obj)
668 {
669 }
670
671 ~SBCommandReturnObjectReleaser ()
672 {
673 m_command_return_object_ref.Release();
674 }
675private:
676 lldb::SBCommandReturnObject &m_command_return_object_ref;
677};
Enrico Granata3370f0c2011-08-19 23:56:34 +0000678
Enrico Granatac2a28252011-08-16 16:49:25 +0000679SWIGEXPORT bool
680LLDBSwigPythonCallCommand
681(
682 const char *python_function_name,
683 const char *session_dictionary_name,
684 lldb::DebuggerSP& debugger,
685 const char* args,
686 std::string& err_msg,
Enrico Granata3370f0c2011-08-19 23:56:34 +0000687 lldb_private::CommandReturnObject& cmd_retobj
Enrico Granatac2a28252011-08-16 16:49:25 +0000688)
689{
690
Enrico Granata3370f0c2011-08-19 23:56:34 +0000691 lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj);
Enrico Granata872959b2011-08-20 00:26:17 +0000692 SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb);
Enrico Granata3370f0c2011-08-19 23:56:34 +0000693 lldb::SBDebugger debugger_sb(debugger);
Enrico Granata6b1596d2011-08-16 23:24:13 +0000694
Enrico Granatac2a28252011-08-16 16:49:25 +0000695 bool retval = false;
696
Enrico Granata3370f0c2011-08-19 23:56:34 +0000697 PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
698 PyObject *CmdRetObj_PyObj = SWIG_NewPointerObj((void *) &cmd_retobj_sb, SWIGTYPE_p_lldb__SBCommandReturnObject, 0);
Enrico Granatac2a28252011-08-16 16:49:25 +0000699
700 if (DebuggerObj_PyObj == NULL)
Enrico Granata872959b2011-08-20 00:26:17 +0000701 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000702
Enrico Granata6b1596d2011-08-16 23:24:13 +0000703 if (CmdRetObj_PyObj == NULL)
Enrico Granata872959b2011-08-20 00:26:17 +0000704 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000705
706 if (!python_function_name || !session_dictionary_name)
Enrico Granata872959b2011-08-20 00:26:17 +0000707 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000708
Enrico Granata960691f2011-08-22 17:34:47 +0000709 PyObject *session_dict, *pfunc;
Enrico Granatac2a28252011-08-16 16:49:25 +0000710 PyObject *pargs, *pvalue;
711
Enrico Granata960691f2011-08-22 17:34:47 +0000712 session_dict = FindSessionDictionary (session_dictionary_name);
713 if (session_dict != NULL)
Enrico Granatac2a28252011-08-16 16:49:25 +0000714 {
Enrico Granata960691f2011-08-22 17:34:47 +0000715 pfunc = ResolvePythonName (python_function_name, session_dict);
716 if (pfunc != NULL)
Enrico Granatac2a28252011-08-16 16:49:25 +0000717 {
Enrico Granatac2a28252011-08-16 16:49:25 +0000718 // Set up the arguments and call the function.
719
Enrico Granata960691f2011-08-22 17:34:47 +0000720 if (PyCallable_Check (pfunc))
Enrico Granatac2a28252011-08-16 16:49:25 +0000721 {
722 pargs = PyTuple_New (4);
723 if (pargs == NULL)
724 {
725 if (PyErr_Occurred())
726 PyErr_Clear();
Enrico Granata872959b2011-08-20 00:26:17 +0000727 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000728 }
729
730 PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj
731 PyTuple_SetItem (pargs, 1, PyString_FromString(args));
Enrico Granata6b1596d2011-08-16 23:24:13 +0000732 PyTuple_SetItem (pargs, 2, CmdRetObj_PyObj); // This "steals" a reference to CmdRetObj_PyObj
Enrico Granatac2a28252011-08-16 16:49:25 +0000733 PyTuple_SetItem (pargs, 3, session_dict); // This "steals" a reference to session_dict
734 pvalue = PyObject_CallObject (pfunc, pargs);
735 Py_DECREF (pargs);
736
737 if (pvalue != NULL)
738 {
739 if (pvalue == Py_None) // no error
740 {
741 err_msg.clear();
742 retval = true;
743 }
Johnny Chen2fcf4122011-12-14 20:40:27 +0000744 else
Enrico Granatac2a28252011-08-16 16:49:25 +0000745 {
Johnny Chen2fcf4122011-12-14 20:40:27 +0000746 // return value is an error string
747 if (PyString_CheckExact(pvalue))
748 err_msg.assign(PyString_AsString(pvalue));
Enrico Granatac2a28252011-08-16 16:49:25 +0000749 retval = false;
750 }
751 Py_DECREF (pvalue);
752 }
753 else if (PyErr_Occurred ())
754 {
755 PyErr_Print();
756 PyErr_Clear();
757 }
758 Py_INCREF (session_dict);
759 }
760 else if (PyErr_Occurred())
761 {
762 PyErr_Print();
763 PyErr_Clear();
764 }
765 }
766 else if (PyErr_Occurred())
767 {
768 PyErr_Print();
769 PyErr_Clear();
770 }
771 }
772 else if (PyErr_Occurred ())
773 {
774 PyErr_Print();
775 PyErr_Clear ();
776 }
Enrico Granata59df36f2011-10-17 21:45:27 +0000777 return retval;
778}
779
Enrico Granata155ee912012-08-24 00:30:47 +0000780SWIGEXPORT void*
781LLDBSWIGPythonCreateOSPlugin
782(
Greg Claytonec1e8232013-01-16 19:53:55 +0000783 const char *python_class_name,
Enrico Granata155ee912012-08-24 00:30:47 +0000784 const char *session_dictionary_name,
785 const lldb::ProcessSP& process_sp
786)
787{
788 PyObject* retval = NULL;
789
Greg Claytonec1e8232013-01-16 19:53:55 +0000790 if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
Enrico Granata155ee912012-08-24 00:30:47 +0000791 Py_RETURN_NONE;
792
793 // I do not want the SBValue to be deallocated when going out of scope because python
794 // has ownership of it and will manage memory for this object by itself
795 lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp);
796
797 PyObject *SBProc_PyObj = SWIG_NewPointerObj((void *)process_sb, SWIGTYPE_p_lldb__SBProcess, 0);
798
799 if (SBProc_PyObj == NULL)
800 Py_RETURN_NONE;
801
Greg Claytonec1e8232013-01-16 19:53:55 +0000802 const char* python_function_name = python_class_name;
Enrico Granata155ee912012-08-24 00:30:47 +0000803
804 PyObject *session_dict, *pfunc;
805 PyObject *pvalue;
806
807 session_dict = FindSessionDictionary (session_dictionary_name);
808 if (session_dict != NULL)
809 {
810 pfunc = ResolvePythonName (python_function_name, session_dict);
811 if (pfunc != NULL)
812 {
813 // Set up the arguments and call the function.
814
815 if (PyCallable_Check (pfunc))
816 {
Enrico Granatacec963a2012-08-24 01:34:39 +0000817 PyObject *argList = Py_BuildValue("(O)", SBProc_PyObj);
Enrico Granata155ee912012-08-24 00:30:47 +0000818
819 if (PyErr_Occurred ())
820 {
821 PyErr_Print();
822 PyErr_Clear();
823 return retval;
824 }
825
826 if (argList == NULL)
827 {
828 return retval;
829 }
830
831 Py_INCREF(SBProc_PyObj);
832
833 pvalue = PyObject_CallObject(pfunc, argList);
834
835 Py_DECREF(argList);
836
837 if (pvalue != NULL)
838 {
839 if (pvalue != Py_None)
840 retval = pvalue;
841 else
842 {
843 retval = Py_None;
844 Py_INCREF(retval);
845 }
846 }
847 else if (PyErr_Occurred ())
848 {
849 PyErr_Print();
850 PyErr_Clear();
851 }
852 Py_INCREF (session_dict);
853 }
854 else if (PyErr_Occurred())
855 {
856 PyErr_Print();
857 PyErr_Clear();
858 }
859 }
860 else if (PyErr_Occurred())
861 {
862 PyErr_Print();
863 PyErr_Clear();
864 }
865 }
866 else if (PyErr_Occurred ())
867 {
868 PyErr_Print();
869 PyErr_Clear ();
870 }
871 if (retval)
872 return retval;
873 else
874 Py_RETURN_NONE;
875}
876
Enrico Granata59df36f2011-10-17 21:45:27 +0000877SWIGEXPORT bool
878LLDBSwigPythonCallModuleInit
879(
Greg Claytonec1e8232013-01-16 19:53:55 +0000880 const char *python_module_name,
Enrico Granata59df36f2011-10-17 21:45:27 +0000881 const char *session_dictionary_name,
882 lldb::DebuggerSP& debugger
883)
884{
885
886 lldb::SBDebugger debugger_sb(debugger);
887
888 bool retval = false;
889
890 PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
891
892 if (DebuggerObj_PyObj == NULL)
893 return retval;
894
Greg Claytonec1e8232013-01-16 19:53:55 +0000895 if (python_module_name == NULL || python_module_name[0] == '\0' || !session_dictionary_name)
Enrico Granata59df36f2011-10-17 21:45:27 +0000896 return retval;
897
898 PyObject *session_dict, *pfunc;
899 PyObject *pargs, *pvalue;
900
901 session_dict = FindSessionDictionary (session_dictionary_name);
902
Greg Claytonec1e8232013-01-16 19:53:55 +0000903 std::string python_function_name_string = python_module_name;
904 python_function_name_string += ".__lldb_init_module";
Enrico Granata59df36f2011-10-17 21:45:27 +0000905 const char* python_function_name = python_function_name_string.c_str();
906
907 if (session_dict != NULL)
908 {
909 pfunc = ResolvePythonName (python_function_name, session_dict);
910
911 if (PyErr_Occurred()) // this might not exist.. let's make sure we handle that
912 {
913 PyErr_Clear();
914 return true;
915 }
916
917 if (pfunc == NULL)
918 return true;
919 else
920 {
921 // Set up the arguments and call the function.
922
923 if (PyCallable_Check (pfunc))
924 {
925 pargs = PyTuple_New (2);
926 if (pargs == NULL)
927 {
928 if (PyErr_Occurred())
929 PyErr_Clear();
930 return retval;
931 }
932
933 PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj
934 PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict
935 pvalue = PyObject_CallObject (pfunc, pargs);
936 Py_DECREF (pargs);
937
938 if (PyErr_Occurred ())
939 {
940 PyErr_Print();
941 PyErr_Clear();
942 }
943 else
944 {
945 retval = true;
946 Py_XDECREF (pvalue);
947 }
948 Py_INCREF (session_dict);
949 }
950 else if (PyErr_Occurred())
951 {
952 PyErr_Print();
953 PyErr_Clear();
954 }
955 }
956 }
957 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000958}
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +0000959%}
Enrico Granatac2a28252011-08-16 16:49:25 +0000960
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +0000961
962%runtime %{
963// Forward declaration to be inserted at the start of LLDBWrapPython.h
964// I used runtime as a hack to make SWIG place it where it's needed.
965// This is needed to use LLDBSwigPythonCallSBInputReaderCallback in the
966// typemaps and in the extensions (SBInputReader.__del__()).
Filipe Cabecinhas85fbe9d2012-08-22 18:10:45 +0000967#include "lldb/API/SBInputReader.h"
Filipe Cabecinhas43898d72012-08-25 00:29:07 +0000968#include "lldb/API/SBDebugger.h"
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +0000969
Daniel Malea37459fc2012-11-29 16:38:44 +0000970#ifdef __cplusplus
971extern "C" {
972#endif
973
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +0000974size_t
975LLDBSwigPythonCallSBInputReaderCallback(void *baton,
976 lldb::SBInputReader *reader,
977 lldb::InputReaderAction notification,
978 const char*bytes,
979 size_t bytes_len);
Filipe Cabecinhas43898d72012-08-25 00:29:07 +0000980
981void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton);
Daniel Malea37459fc2012-11-29 16:38:44 +0000982
983#ifdef __cplusplus
984}
985#endif
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +0000986%}
987
988%wrapper %{
989// For the InputReader Callback functions
990SWIGEXPORT size_t
991LLDBSwigPythonCallSBInputReaderCallback(void *baton,
992 lldb::SBInputReader *reader,
993 lldb::InputReaderAction notification,
994 const char*bytes,
995 size_t bytes_len) {
Filipe Cabecinhas43898d72012-08-25 00:29:07 +0000996 if (baton != Py_None) {
997 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
998
999 PyObject *py_InputReader = SWIG_NewPointerObj(reader, SWIGTYPE_p_lldb__SBInputReader, false);
1000 PyObject *py_Notification = PyInt_FromLong(notification);
1001 PyObject *py_Bytes = PyBytes_FromStringAndSize(bytes, bytes_len);
1002
1003 PyObject *tuple = PyTuple_Pack(3, py_InputReader, py_Notification, py_Bytes);
1004 PyObject *res = PyObject_Call(reinterpret_cast<PyObject*>(baton), tuple, NULL);
1005 Py_DECREF(tuple);
1006 Py_DECREF(py_InputReader);
1007 Py_DECREF(py_Notification);
1008 Py_DECREF(py_Bytes);
1009
1010 if (res == NULL) {
1011 PyObject *exc = PyErr_Occurred();
1012 if (exc) {
1013 ::puts("\nErroring out at LLDBSwigPythonCallSBInputReaderCallback");
1014 PyErr_Print();
1015 }
1016 return 0;
1017 }
1018
1019 size_t result = 0;
1020 // If the callback misbehaves and returns Py_None, assume it returned 0
1021 if (res != Py_None)
1022 result = static_cast<size_t>(PyInt_AsSsize_t(res));
1023
1024 Py_DECREF(res);
1025 SWIG_PYTHON_THREAD_END_BLOCK;
1026 return result;
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +00001027 }
Enrico Granatae25c6312012-08-27 18:30:45 +00001028 return 0;
Filipe Cabecinhas43898d72012-08-25 00:29:07 +00001029}
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +00001030
Filipe Cabecinhas43898d72012-08-25 00:29:07 +00001031// For the LogOutputCallback functions
1032void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) {
1033 if (baton != Py_None) {
1034 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
1035 PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str);
1036 SWIG_PYTHON_THREAD_END_BLOCK;
1037 }
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +00001038}
Johnny Chenf307cf72011-07-26 19:09:03 +00001039%}