blob: 9b48f3b8b1f9d1f7e3d8ce64899829c630645d76 [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);
Johnny Chenf307cf72011-07-26 19:09:03 +0000339
Enrico Granatacf09f882012-03-19 22:58:49 +0000340 PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *)valobj_sb, SWIGTYPE_p_lldb__SBValue, 0);
Johnny Chenf307cf72011-07-26 19:09:03 +0000341
342 if (ValObj_PyObj == NULL)
343 Py_RETURN_NONE;
344
345 const char* python_function_name = python_class_name.c_str();
346
Enrico Granata960691f2011-08-22 17:34:47 +0000347 PyObject *session_dict, *pfunc;
Johnny Chenf307cf72011-07-26 19:09:03 +0000348 PyObject *pvalue;
Enrico Granata960691f2011-08-22 17:34:47 +0000349
350 session_dict = FindSessionDictionary (session_dictionary_name);
351 if (session_dict != NULL)
Johnny Chenf307cf72011-07-26 19:09:03 +0000352 {
Enrico Granata960691f2011-08-22 17:34:47 +0000353 pfunc = ResolvePythonName (python_function_name, session_dict);
354 if (pfunc != NULL)
Johnny Chenf307cf72011-07-26 19:09:03 +0000355 {
Johnny Chenf307cf72011-07-26 19:09:03 +0000356 // Set up the arguments and call the function.
Enrico Granata960691f2011-08-22 17:34:47 +0000357
358 if (PyCallable_Check (pfunc))
Johnny Chenf307cf72011-07-26 19:09:03 +0000359 {
360 PyObject *argList = Py_BuildValue("SS", ValObj_PyObj, session_dict);
361
362 if (PyErr_Occurred ())
363 {
364 PyErr_Print();
365 PyErr_Clear();
366 return retval;
367 }
368
369 if (argList == NULL)
370 {
371 return retval;
372 }
373
374 Py_INCREF(ValObj_PyObj);
375
376 pvalue = PyObject_CallObject(pfunc, argList);
377
378 Py_DECREF(argList);
379
380 if (pvalue != NULL)
381 {
382 if (pvalue != Py_None)
383 retval = pvalue;
384 else
385 {
386 retval = Py_None;
387 Py_INCREF(retval);
388 }
389 }
390 else if (PyErr_Occurred ())
391 {
392 PyErr_Print();
393 PyErr_Clear();
394 }
395 Py_INCREF (session_dict);
396 }
397 else if (PyErr_Occurred())
398 {
399 PyErr_Print();
400 PyErr_Clear();
401 }
402 }
403 else if (PyErr_Occurred())
404 {
405 PyErr_Print();
406 PyErr_Clear();
407 }
408 }
409 else if (PyErr_Occurred ())
410 {
411 PyErr_Print();
412 PyErr_Clear ();
413 }
414 if (retval)
415 return retval;
416 else
417 Py_RETURN_NONE;
418}
419
420/*
421these four calls below are meant to support
422Python-based synthetic children providers
423they essentially mimic the four pure virtual
424method calls provided by the frontend class
425*/
426
427SWIGEXPORT uint32_t
428LLDBSwigPython_CalculateNumChildren
429(
430 PyObject *implementor
431)
432{
433
434 static char callee_name[] = "num_children";
435
436 if (implementor == NULL || implementor == Py_None)
437 return 0;
438 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL);
439 if (PyErr_Occurred())
440 {
441 PyErr_Print();
442 PyErr_Clear();
443 }
444
445 if (py_return == NULL || py_return == Py_None)
446 {
447 Py_XDECREF(py_return);
448 return UINT32_MAX;
449 }
450 long retval = PyInt_AsLong(py_return);
451 Py_DECREF(py_return);
452 if (retval >= 0)
453 return (uint32_t)retval;
454 if (PyErr_Occurred())
455 {
456 PyErr_Print();
457 PyErr_Clear();
458 }
459 return 0;
460}
461
462SWIGEXPORT PyObject*
463LLDBSwigPython_GetChildAtIndex
464(
465 PyObject *implementor,
466 uint32_t idx
467)
468{
469
470 static char callee_name[] = "get_child_at_index";
471 static char param_format[] = "i";
472
473 if (implementor == NULL || implementor == Py_None)
474 return NULL;
475 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, idx);
476 if (PyErr_Occurred())
477 {
478 PyErr_Print();
479 PyErr_Clear();
480 }
481
482 if (py_return == NULL || py_return == Py_None)
483 {
484 Py_XDECREF(py_return);
485 return NULL;
486 }
487
488 lldb::SBValue* sbvalue_ptr = NULL;
489
490 if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
491 {
492 Py_DECREF(py_return);
493 return NULL;
494 }
495
496 if (sbvalue_ptr == NULL)
497 return NULL;
498
499 return py_return;
500}
501
502SWIGEXPORT int
503LLDBSwigPython_GetIndexOfChildWithName
504(
505 PyObject *implementor,
506 const char* child_name
507)
508{
509 static char callee_name[] = "get_child_index";
510 static char param_format[] = "s";
511
512 if (implementor == NULL || implementor == Py_None)
513 return 0;
514 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, child_name);
515 if (PyErr_Occurred())
516 {
517 PyErr_Print();
518 PyErr_Clear();
519 }
520
521 if (py_return == NULL || py_return == Py_None)
522 {
523 Py_XDECREF(py_return);
524 return UINT32_MAX;
525 }
526 long retval = PyInt_AsLong(py_return);
527 Py_DECREF(py_return);
528 if (retval >= 0)
529 return (uint32_t)retval;
530 if (PyErr_Occurred())
531 {
532 PyErr_Print();
533 PyErr_Clear();
534 }
535 return 0;
536}
537
Enrico Granatacf09f882012-03-19 22:58:49 +0000538SWIGEXPORT bool
Enrico Granata6a319e42011-07-26 21:02:56 +0000539LLDBSwigPython_UpdateSynthProviderInstance
540(
541 PyObject *implementor
542)
543{
Enrico Granatacf09f882012-03-19 22:58:49 +0000544
545 bool ret_val = false;
546
Enrico Granata6a319e42011-07-26 21:02:56 +0000547 static char callee_name[] = "update";
548
549 if (implementor == NULL || implementor == Py_None)
Enrico Granatacf09f882012-03-19 22:58:49 +0000550 return ret_val;
Enrico Granata6a319e42011-07-26 21:02:56 +0000551
552 // 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
553 // other synth provider calls are mandatory, so we want to fail in a very obvious way if they are missing!
554 PyObject* pmeth = PyObject_GetAttrString(implementor, callee_name);
555
Enrico Granata1239c1a2011-08-11 19:20:44 +0000556 if (PyErr_Occurred())
557 {
558 PyErr_Clear();
559 }
560
Enrico Granata6a319e42011-07-26 21:02:56 +0000561 if (pmeth == NULL || pmeth == Py_None)
562 {
563 Py_XDECREF(pmeth);
Enrico Granatacf09f882012-03-19 22:58:49 +0000564 return ret_val;
Enrico Granata6a319e42011-07-26 21:02:56 +0000565 }
566
567 if (PyCallable_Check(pmeth) == 0)
568 {
Enrico Granata1239c1a2011-08-11 19:20:44 +0000569 if (PyErr_Occurred())
570 {
571 PyErr_Clear();
572 }
573
Enrico Granata6a319e42011-07-26 21:02:56 +0000574 Py_XDECREF(pmeth);
Enrico Granatacf09f882012-03-19 22:58:49 +0000575 return ret_val;
Enrico Granata6a319e42011-07-26 21:02:56 +0000576 }
577
Enrico Granata1239c1a2011-08-11 19:20:44 +0000578 if (PyErr_Occurred())
579 {
580 PyErr_Clear();
581 }
582
Enrico Granata6a319e42011-07-26 21:02:56 +0000583 Py_XDECREF(pmeth);
584
585 // right now we know this function exists and is callable..
586 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL);
587
588 // if it fails, print the error but otherwise go on
589 if (PyErr_Occurred())
590 {
591 PyErr_Print();
592 PyErr_Clear();
593 }
Enrico Granatacf09f882012-03-19 22:58:49 +0000594
595 if (py_return == Py_True)
596 ret_val = true;
Enrico Granata6a319e42011-07-26 21:02:56 +0000597
598 Py_XDECREF(py_return);
Enrico Granatacf09f882012-03-19 22:58:49 +0000599
600 return ret_val;
Enrico Granata6a319e42011-07-26 21:02:56 +0000601
602}
603
Enrico Granata91544802011-09-06 19:20:51 +0000604SWIGEXPORT void*
Johnny Chenf307cf72011-07-26 19:09:03 +0000605LLDBSWIGPython_CastPyObjectToSBValue
606(
607 PyObject* data
608)
609{
610 lldb::SBValue* sb_ptr = NULL;
611
612 int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
613
614 if (valid_cast == -1)
615 return NULL;
616
617 return sb_ptr;
618}
619
Enrico Granata872959b2011-08-20 00:26:17 +0000620// Currently, SBCommandReturnObjectReleaser wraps an std::auto_ptr to an
621// lldb_private::CommandReturnObject. This means that the destructor for the
622// SB object will deallocate its contained CommandReturnObject. Because that
623// object is used as the real return object for Python-based commands, we want
624// it to stay around. Thus, we release the auto_ptr before returning from
625// LLDBSwigPythonCallCommand, and to guarantee that the release will occur no
626// matter how we exit from the function, we have a releaser object whose
627// destructor does the right thing for us
628class SBCommandReturnObjectReleaser
629{
630public:
631 SBCommandReturnObjectReleaser (lldb::SBCommandReturnObject &obj) :
632 m_command_return_object_ref (obj)
633 {
634 }
635
636 ~SBCommandReturnObjectReleaser ()
637 {
638 m_command_return_object_ref.Release();
639 }
640private:
641 lldb::SBCommandReturnObject &m_command_return_object_ref;
642};
Enrico Granata3370f0c2011-08-19 23:56:34 +0000643
Enrico Granatac2a28252011-08-16 16:49:25 +0000644SWIGEXPORT bool
645LLDBSwigPythonCallCommand
646(
647 const char *python_function_name,
648 const char *session_dictionary_name,
649 lldb::DebuggerSP& debugger,
650 const char* args,
651 std::string& err_msg,
Enrico Granata3370f0c2011-08-19 23:56:34 +0000652 lldb_private::CommandReturnObject& cmd_retobj
Enrico Granatac2a28252011-08-16 16:49:25 +0000653)
654{
655
Enrico Granata3370f0c2011-08-19 23:56:34 +0000656 lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj);
Enrico Granata872959b2011-08-20 00:26:17 +0000657 SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb);
Enrico Granata3370f0c2011-08-19 23:56:34 +0000658 lldb::SBDebugger debugger_sb(debugger);
Enrico Granata6b1596d2011-08-16 23:24:13 +0000659
Enrico Granatac2a28252011-08-16 16:49:25 +0000660 bool retval = false;
661
Enrico Granata3370f0c2011-08-19 23:56:34 +0000662 PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
663 PyObject *CmdRetObj_PyObj = SWIG_NewPointerObj((void *) &cmd_retobj_sb, SWIGTYPE_p_lldb__SBCommandReturnObject, 0);
Enrico Granatac2a28252011-08-16 16:49:25 +0000664
665 if (DebuggerObj_PyObj == NULL)
Enrico Granata872959b2011-08-20 00:26:17 +0000666 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000667
Enrico Granata6b1596d2011-08-16 23:24:13 +0000668 if (CmdRetObj_PyObj == NULL)
Enrico Granata872959b2011-08-20 00:26:17 +0000669 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000670
671 if (!python_function_name || !session_dictionary_name)
Enrico Granata872959b2011-08-20 00:26:17 +0000672 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000673
Enrico Granata960691f2011-08-22 17:34:47 +0000674 PyObject *session_dict, *pfunc;
Enrico Granatac2a28252011-08-16 16:49:25 +0000675 PyObject *pargs, *pvalue;
676
Enrico Granata960691f2011-08-22 17:34:47 +0000677 session_dict = FindSessionDictionary (session_dictionary_name);
678 if (session_dict != NULL)
Enrico Granatac2a28252011-08-16 16:49:25 +0000679 {
Enrico Granata960691f2011-08-22 17:34:47 +0000680 pfunc = ResolvePythonName (python_function_name, session_dict);
681 if (pfunc != NULL)
Enrico Granatac2a28252011-08-16 16:49:25 +0000682 {
Enrico Granatac2a28252011-08-16 16:49:25 +0000683 // Set up the arguments and call the function.
684
Enrico Granata960691f2011-08-22 17:34:47 +0000685 if (PyCallable_Check (pfunc))
Enrico Granatac2a28252011-08-16 16:49:25 +0000686 {
687 pargs = PyTuple_New (4);
688 if (pargs == NULL)
689 {
690 if (PyErr_Occurred())
691 PyErr_Clear();
Enrico Granata872959b2011-08-20 00:26:17 +0000692 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000693 }
694
695 PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj
696 PyTuple_SetItem (pargs, 1, PyString_FromString(args));
Enrico Granata6b1596d2011-08-16 23:24:13 +0000697 PyTuple_SetItem (pargs, 2, CmdRetObj_PyObj); // This "steals" a reference to CmdRetObj_PyObj
Enrico Granatac2a28252011-08-16 16:49:25 +0000698 PyTuple_SetItem (pargs, 3, session_dict); // This "steals" a reference to session_dict
699 pvalue = PyObject_CallObject (pfunc, pargs);
700 Py_DECREF (pargs);
701
702 if (pvalue != NULL)
703 {
704 if (pvalue == Py_None) // no error
705 {
706 err_msg.clear();
707 retval = true;
708 }
Johnny Chen2fcf4122011-12-14 20:40:27 +0000709 else
Enrico Granatac2a28252011-08-16 16:49:25 +0000710 {
Johnny Chen2fcf4122011-12-14 20:40:27 +0000711 // return value is an error string
712 if (PyString_CheckExact(pvalue))
713 err_msg.assign(PyString_AsString(pvalue));
Enrico Granatac2a28252011-08-16 16:49:25 +0000714 retval = false;
715 }
716 Py_DECREF (pvalue);
717 }
718 else if (PyErr_Occurred ())
719 {
720 PyErr_Print();
721 PyErr_Clear();
722 }
723 Py_INCREF (session_dict);
724 }
725 else if (PyErr_Occurred())
726 {
727 PyErr_Print();
728 PyErr_Clear();
729 }
730 }
731 else if (PyErr_Occurred())
732 {
733 PyErr_Print();
734 PyErr_Clear();
735 }
736 }
737 else if (PyErr_Occurred ())
738 {
739 PyErr_Print();
740 PyErr_Clear ();
741 }
Enrico Granata59df36f2011-10-17 21:45:27 +0000742 return retval;
743}
744
745SWIGEXPORT bool
746LLDBSwigPythonCallModuleInit
747(
748 const std::string python_module_name,
749 const char *session_dictionary_name,
750 lldb::DebuggerSP& debugger
751)
752{
753
754 lldb::SBDebugger debugger_sb(debugger);
755
756 bool retval = false;
757
758 PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
759
760 if (DebuggerObj_PyObj == NULL)
761 return retval;
762
763 if (!(python_module_name.length()) || !session_dictionary_name)
764 return retval;
765
766 PyObject *session_dict, *pfunc;
767 PyObject *pargs, *pvalue;
768
769 session_dict = FindSessionDictionary (session_dictionary_name);
770
771 std::string python_function_name_string = python_module_name + (".__lldb_init_module");
772 const char* python_function_name = python_function_name_string.c_str();
773
774 if (session_dict != NULL)
775 {
776 pfunc = ResolvePythonName (python_function_name, session_dict);
777
778 if (PyErr_Occurred()) // this might not exist.. let's make sure we handle that
779 {
780 PyErr_Clear();
781 return true;
782 }
783
784 if (pfunc == NULL)
785 return true;
786 else
787 {
788 // Set up the arguments and call the function.
789
790 if (PyCallable_Check (pfunc))
791 {
792 pargs = PyTuple_New (2);
793 if (pargs == NULL)
794 {
795 if (PyErr_Occurred())
796 PyErr_Clear();
797 return retval;
798 }
799
800 PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj
801 PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict
802 pvalue = PyObject_CallObject (pfunc, pargs);
803 Py_DECREF (pargs);
804
805 if (PyErr_Occurred ())
806 {
807 PyErr_Print();
808 PyErr_Clear();
809 }
810 else
811 {
812 retval = true;
813 Py_XDECREF (pvalue);
814 }
815 Py_INCREF (session_dict);
816 }
817 else if (PyErr_Occurred())
818 {
819 PyErr_Print();
820 PyErr_Clear();
821 }
822 }
823 }
824 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000825}
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +0000826%}
Enrico Granatac2a28252011-08-16 16:49:25 +0000827
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +0000828
829%runtime %{
830// Forward declaration to be inserted at the start of LLDBWrapPython.h
831// I used runtime as a hack to make SWIG place it where it's needed.
832// This is needed to use LLDBSwigPythonCallSBInputReaderCallback in the
833// typemaps and in the extensions (SBInputReader.__del__()).
834#include "SBInputReader.h"
835
836size_t
837LLDBSwigPythonCallSBInputReaderCallback(void *baton,
838 lldb::SBInputReader *reader,
839 lldb::InputReaderAction notification,
840 const char*bytes,
841 size_t bytes_len);
842%}
843
844%wrapper %{
845// For the InputReader Callback functions
846SWIGEXPORT size_t
847LLDBSwigPythonCallSBInputReaderCallback(void *baton,
848 lldb::SBInputReader *reader,
849 lldb::InputReaderAction notification,
850 const char*bytes,
851 size_t bytes_len) {
852 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
853
854 PyObject *py_InputReader = SWIG_NewPointerObj(reader, SWIGTYPE_p_lldb__SBInputReader, false);
855 PyObject *py_Notification = PyInt_FromLong(notification);
856 PyObject *py_Bytes = PyBytes_FromStringAndSize(bytes, bytes_len);
857
858 PyObject *tuple = PyTuple_Pack(3, py_InputReader, py_Notification, py_Bytes);
859 PyObject *res = PyObject_Call(reinterpret_cast<PyObject*>(baton), tuple, NULL);
860 Py_DECREF(tuple);
861 Py_DECREF(py_InputReader);
862 Py_DECREF(py_Notification);
863 Py_DECREF(py_Bytes);
864
865 if (res == NULL) {
866 PyObject *exc = PyErr_Occurred();
867 if (exc) {
868 ::puts("\nErroring out at LLDBSwigPythonCallSBInputReaderCallback");
869 PyErr_Print();
870 }
871 return 0;
872 }
873
874 size_t result = 0;
875 // If the callback misbehaves and returns Py_None, assume it returned 0
876 if (res != Py_None)
877 result = static_cast<size_t>(PyInt_AsSsize_t(res));
878
879 Py_DECREF(res);
880 SWIG_PYTHON_THREAD_END_BLOCK;
881 return result;
882}
Johnny Chenf307cf72011-07-26 19:09:03 +0000883%}