blob: 3277e780e26ab5bf8e0ec9045715334ce5b91f1a [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
Enrico Granata1328b142012-02-29 03:28:49 +0000179SWIGEXPORT bool
Johnny Chenf307cf72011-07-26 19:09:03 +0000180LLDBSwigPythonCallTypeScript
181(
182 const char *python_function_name,
Enrico Granata1328b142012-02-29 03:28:49 +0000183 const void *session_dictionary,
184 const lldb::ValueObjectSP& valobj_sp,
185 void** pyfunct_wrapper,
186 std::string& retval
Johnny Chenf307cf72011-07-26 19:09:03 +0000187)
188{
189 lldb::SBValue sb_value (valobj_sp);
190
Enrico Granata3370f0c2011-08-19 23:56:34 +0000191 PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *) &sb_value, SWIGTYPE_p_lldb__SBValue, 0);
Johnny Chenf307cf72011-07-26 19:09:03 +0000192
193 if (ValObj_PyObj == NULL)
Enrico Granata1328b142012-02-29 03:28:49 +0000194 return false;
Johnny Chenf307cf72011-07-26 19:09:03 +0000195
Enrico Granata1328b142012-02-29 03:28:49 +0000196 if (!python_function_name || !session_dictionary)
197 return false;
Johnny Chenf307cf72011-07-26 19:09:03 +0000198
Enrico Granata1328b142012-02-29 03:28:49 +0000199 PyObject *session_dict = (PyObject*)session_dictionary, *pfunc = NULL, *pargs = NULL, *pvalue = NULL;
Johnny Chenf307cf72011-07-26 19:09:03 +0000200
Enrico Granata1328b142012-02-29 03:28:49 +0000201 if (pyfunct_wrapper && *pyfunct_wrapper && PyFunction_Check (*pyfunct_wrapper))
Johnny Chenf307cf72011-07-26 19:09:03 +0000202 {
Enrico Granata1328b142012-02-29 03:28:49 +0000203 pfunc = (PyObject*)(*pyfunct_wrapper);
204 if (pfunc->ob_refcnt == 1)
Johnny Chenf307cf72011-07-26 19:09:03 +0000205 {
Enrico Granata1328b142012-02-29 03:28:49 +0000206 Py_XDECREF(pfunc);
207 pfunc = NULL;
Johnny Chenf307cf72011-07-26 19:09:03 +0000208 }
209 }
Enrico Granata1328b142012-02-29 03:28:49 +0000210
211 if (PyDict_Check(session_dict))
Johnny Chenf307cf72011-07-26 19:09:03 +0000212 {
Enrico Granata1328b142012-02-29 03:28:49 +0000213 PyErr_Cleaner pyerr_cleanup(true); // show Python errors
214
215 if (!pfunc)
216 {
217 pfunc = ResolvePythonName (python_function_name, session_dict);
218 if (!pfunc || !PyFunction_Check (pfunc))
219 return false;
220 else
221 {
222 if (pyfunct_wrapper)
223 *pyfunct_wrapper = pfunc;
224 }
225 }
226 /*else
227 printf("caching works!!!!\n");*/
228
229 pargs = PyTuple_Pack(2, ValObj_PyObj, session_dict);
230 if (pargs == NULL)
231 return false;
232
233 pvalue = PyObject_CallObject (pfunc, pargs);
234 Py_DECREF (pargs);
235
236 if (pvalue != NULL && pvalue != Py_None && PyString_Check(pvalue))
237 retval.assign(PyString_AsString(pvalue));
238 Py_XDECREF (pvalue);
239 Py_INCREF (session_dict);
Johnny Chenf307cf72011-07-26 19:09:03 +0000240 }
Enrico Granata1328b142012-02-29 03:28:49 +0000241 return true;
Johnny Chenf307cf72011-07-26 19:09:03 +0000242}
243
244SWIGEXPORT void*
245LLDBSwigPythonCreateSyntheticProvider
246(
247 const std::string python_class_name,
248 const char *session_dictionary_name,
249 const lldb::ValueObjectSP& valobj_sp
250)
251{
252 PyObject* retval = NULL;
253
254 if (python_class_name.empty() || !session_dictionary_name)
255 Py_RETURN_NONE;
256
Enrico Granata3370f0c2011-08-19 23:56:34 +0000257 // I do not want the SBValue to be deallocated when going out of scope because python
258 // has ownership of it and will manage memory for this object by itself
259 lldb::SBValue *valobj_sb = new lldb::SBValue(valobj_sp);
Johnny Chenf307cf72011-07-26 19:09:03 +0000260
Enrico Granata3370f0c2011-08-19 23:56:34 +0000261 PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *)valobj_sb, SWIGTYPE_p_lldb__SBValue, SWIG_POINTER_OWN);
Johnny Chenf307cf72011-07-26 19:09:03 +0000262
263 if (ValObj_PyObj == NULL)
264 Py_RETURN_NONE;
265
266 const char* python_function_name = python_class_name.c_str();
267
Enrico Granata960691f2011-08-22 17:34:47 +0000268 PyObject *session_dict, *pfunc;
Johnny Chenf307cf72011-07-26 19:09:03 +0000269 PyObject *pvalue;
Enrico Granata960691f2011-08-22 17:34:47 +0000270
271 session_dict = FindSessionDictionary (session_dictionary_name);
272 if (session_dict != NULL)
Johnny Chenf307cf72011-07-26 19:09:03 +0000273 {
Enrico Granata960691f2011-08-22 17:34:47 +0000274 pfunc = ResolvePythonName (python_function_name, session_dict);
275 if (pfunc != NULL)
Johnny Chenf307cf72011-07-26 19:09:03 +0000276 {
Johnny Chenf307cf72011-07-26 19:09:03 +0000277 // Set up the arguments and call the function.
Enrico Granata960691f2011-08-22 17:34:47 +0000278
279 if (PyCallable_Check (pfunc))
Johnny Chenf307cf72011-07-26 19:09:03 +0000280 {
281 PyObject *argList = Py_BuildValue("SS", ValObj_PyObj, session_dict);
282
283 if (PyErr_Occurred ())
284 {
285 PyErr_Print();
286 PyErr_Clear();
287 return retval;
288 }
289
290 if (argList == NULL)
291 {
292 return retval;
293 }
294
295 Py_INCREF(ValObj_PyObj);
296
297 pvalue = PyObject_CallObject(pfunc, argList);
298
299 Py_DECREF(argList);
300
301 if (pvalue != NULL)
302 {
303 if (pvalue != Py_None)
304 retval = pvalue;
305 else
306 {
307 retval = Py_None;
308 Py_INCREF(retval);
309 }
310 }
311 else if (PyErr_Occurred ())
312 {
313 PyErr_Print();
314 PyErr_Clear();
315 }
316 Py_INCREF (session_dict);
317 }
318 else if (PyErr_Occurred())
319 {
320 PyErr_Print();
321 PyErr_Clear();
322 }
323 }
324 else if (PyErr_Occurred())
325 {
326 PyErr_Print();
327 PyErr_Clear();
328 }
329 }
330 else if (PyErr_Occurred ())
331 {
332 PyErr_Print();
333 PyErr_Clear ();
334 }
335 if (retval)
336 return retval;
337 else
338 Py_RETURN_NONE;
339}
340
341/*
342these four calls below are meant to support
343Python-based synthetic children providers
344they essentially mimic the four pure virtual
345method calls provided by the frontend class
346*/
347
348SWIGEXPORT uint32_t
349LLDBSwigPython_CalculateNumChildren
350(
351 PyObject *implementor
352)
353{
354
355 static char callee_name[] = "num_children";
356
357 if (implementor == NULL || implementor == Py_None)
358 return 0;
359 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL);
360 if (PyErr_Occurred())
361 {
362 PyErr_Print();
363 PyErr_Clear();
364 }
365
366 if (py_return == NULL || py_return == Py_None)
367 {
368 Py_XDECREF(py_return);
369 return UINT32_MAX;
370 }
371 long retval = PyInt_AsLong(py_return);
372 Py_DECREF(py_return);
373 if (retval >= 0)
374 return (uint32_t)retval;
375 if (PyErr_Occurred())
376 {
377 PyErr_Print();
378 PyErr_Clear();
379 }
380 return 0;
381}
382
383SWIGEXPORT PyObject*
384LLDBSwigPython_GetChildAtIndex
385(
386 PyObject *implementor,
387 uint32_t idx
388)
389{
390
391 static char callee_name[] = "get_child_at_index";
392 static char param_format[] = "i";
393
394 if (implementor == NULL || implementor == Py_None)
395 return NULL;
396 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, idx);
397 if (PyErr_Occurred())
398 {
399 PyErr_Print();
400 PyErr_Clear();
401 }
402
403 if (py_return == NULL || py_return == Py_None)
404 {
405 Py_XDECREF(py_return);
406 return NULL;
407 }
408
409 lldb::SBValue* sbvalue_ptr = NULL;
410
411 if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
412 {
413 Py_DECREF(py_return);
414 return NULL;
415 }
416
417 if (sbvalue_ptr == NULL)
418 return NULL;
419
420 return py_return;
421}
422
423SWIGEXPORT int
424LLDBSwigPython_GetIndexOfChildWithName
425(
426 PyObject *implementor,
427 const char* child_name
428)
429{
430 static char callee_name[] = "get_child_index";
431 static char param_format[] = "s";
432
433 if (implementor == NULL || implementor == Py_None)
434 return 0;
435 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, child_name);
436 if (PyErr_Occurred())
437 {
438 PyErr_Print();
439 PyErr_Clear();
440 }
441
442 if (py_return == NULL || py_return == Py_None)
443 {
444 Py_XDECREF(py_return);
445 return UINT32_MAX;
446 }
447 long retval = PyInt_AsLong(py_return);
448 Py_DECREF(py_return);
449 if (retval >= 0)
450 return (uint32_t)retval;
451 if (PyErr_Occurred())
452 {
453 PyErr_Print();
454 PyErr_Clear();
455 }
456 return 0;
457}
458
Enrico Granata6a319e42011-07-26 21:02:56 +0000459SWIGEXPORT void
460LLDBSwigPython_UpdateSynthProviderInstance
461(
462 PyObject *implementor
463)
464{
465 static char callee_name[] = "update";
466
467 if (implementor == NULL || implementor == Py_None)
468 return;
469
470 // 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
471 // other synth provider calls are mandatory, so we want to fail in a very obvious way if they are missing!
472 PyObject* pmeth = PyObject_GetAttrString(implementor, callee_name);
473
Enrico Granata1239c1a2011-08-11 19:20:44 +0000474 if (PyErr_Occurred())
475 {
476 PyErr_Clear();
477 }
478
Enrico Granata6a319e42011-07-26 21:02:56 +0000479 if (pmeth == NULL || pmeth == Py_None)
480 {
481 Py_XDECREF(pmeth);
482 return;
483 }
484
485 if (PyCallable_Check(pmeth) == 0)
486 {
Enrico Granata1239c1a2011-08-11 19:20:44 +0000487 if (PyErr_Occurred())
488 {
489 PyErr_Clear();
490 }
491
Enrico Granata6a319e42011-07-26 21:02:56 +0000492 Py_XDECREF(pmeth);
493 return;
494 }
495
Enrico Granata1239c1a2011-08-11 19:20:44 +0000496 if (PyErr_Occurred())
497 {
498 PyErr_Clear();
499 }
500
Enrico Granata6a319e42011-07-26 21:02:56 +0000501 Py_XDECREF(pmeth);
502
503 // right now we know this function exists and is callable..
504 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL);
505
506 // if it fails, print the error but otherwise go on
507 if (PyErr_Occurred())
508 {
509 PyErr_Print();
510 PyErr_Clear();
511 }
512
513 Py_XDECREF(py_return);
514
515}
516
Enrico Granata91544802011-09-06 19:20:51 +0000517SWIGEXPORT void*
Johnny Chenf307cf72011-07-26 19:09:03 +0000518LLDBSWIGPython_CastPyObjectToSBValue
519(
520 PyObject* data
521)
522{
523 lldb::SBValue* sb_ptr = NULL;
524
525 int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
526
527 if (valid_cast == -1)
528 return NULL;
529
530 return sb_ptr;
531}
532
Enrico Granata872959b2011-08-20 00:26:17 +0000533// Currently, SBCommandReturnObjectReleaser wraps an std::auto_ptr to an
534// lldb_private::CommandReturnObject. This means that the destructor for the
535// SB object will deallocate its contained CommandReturnObject. Because that
536// object is used as the real return object for Python-based commands, we want
537// it to stay around. Thus, we release the auto_ptr before returning from
538// LLDBSwigPythonCallCommand, and to guarantee that the release will occur no
539// matter how we exit from the function, we have a releaser object whose
540// destructor does the right thing for us
541class SBCommandReturnObjectReleaser
542{
543public:
544 SBCommandReturnObjectReleaser (lldb::SBCommandReturnObject &obj) :
545 m_command_return_object_ref (obj)
546 {
547 }
548
549 ~SBCommandReturnObjectReleaser ()
550 {
551 m_command_return_object_ref.Release();
552 }
553private:
554 lldb::SBCommandReturnObject &m_command_return_object_ref;
555};
Enrico Granata3370f0c2011-08-19 23:56:34 +0000556
Enrico Granatac2a28252011-08-16 16:49:25 +0000557SWIGEXPORT bool
558LLDBSwigPythonCallCommand
559(
560 const char *python_function_name,
561 const char *session_dictionary_name,
562 lldb::DebuggerSP& debugger,
563 const char* args,
564 std::string& err_msg,
Enrico Granata3370f0c2011-08-19 23:56:34 +0000565 lldb_private::CommandReturnObject& cmd_retobj
Enrico Granatac2a28252011-08-16 16:49:25 +0000566)
567{
568
Enrico Granata3370f0c2011-08-19 23:56:34 +0000569 lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj);
Enrico Granata872959b2011-08-20 00:26:17 +0000570 SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb);
Enrico Granata3370f0c2011-08-19 23:56:34 +0000571 lldb::SBDebugger debugger_sb(debugger);
Enrico Granata6b1596d2011-08-16 23:24:13 +0000572
Enrico Granatac2a28252011-08-16 16:49:25 +0000573 bool retval = false;
574
Enrico Granata3370f0c2011-08-19 23:56:34 +0000575 PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
576 PyObject *CmdRetObj_PyObj = SWIG_NewPointerObj((void *) &cmd_retobj_sb, SWIGTYPE_p_lldb__SBCommandReturnObject, 0);
Enrico Granatac2a28252011-08-16 16:49:25 +0000577
578 if (DebuggerObj_PyObj == NULL)
Enrico Granata872959b2011-08-20 00:26:17 +0000579 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000580
Enrico Granata6b1596d2011-08-16 23:24:13 +0000581 if (CmdRetObj_PyObj == NULL)
Enrico Granata872959b2011-08-20 00:26:17 +0000582 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000583
584 if (!python_function_name || !session_dictionary_name)
Enrico Granata872959b2011-08-20 00:26:17 +0000585 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000586
Enrico Granata960691f2011-08-22 17:34:47 +0000587 PyObject *session_dict, *pfunc;
Enrico Granatac2a28252011-08-16 16:49:25 +0000588 PyObject *pargs, *pvalue;
589
Enrico Granata960691f2011-08-22 17:34:47 +0000590 session_dict = FindSessionDictionary (session_dictionary_name);
591 if (session_dict != NULL)
Enrico Granatac2a28252011-08-16 16:49:25 +0000592 {
Enrico Granata960691f2011-08-22 17:34:47 +0000593 pfunc = ResolvePythonName (python_function_name, session_dict);
594 if (pfunc != NULL)
Enrico Granatac2a28252011-08-16 16:49:25 +0000595 {
Enrico Granatac2a28252011-08-16 16:49:25 +0000596 // Set up the arguments and call the function.
597
Enrico Granata960691f2011-08-22 17:34:47 +0000598 if (PyCallable_Check (pfunc))
Enrico Granatac2a28252011-08-16 16:49:25 +0000599 {
600 pargs = PyTuple_New (4);
601 if (pargs == NULL)
602 {
603 if (PyErr_Occurred())
604 PyErr_Clear();
Enrico Granata872959b2011-08-20 00:26:17 +0000605 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000606 }
607
608 PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj
609 PyTuple_SetItem (pargs, 1, PyString_FromString(args));
Enrico Granata6b1596d2011-08-16 23:24:13 +0000610 PyTuple_SetItem (pargs, 2, CmdRetObj_PyObj); // This "steals" a reference to CmdRetObj_PyObj
Enrico Granatac2a28252011-08-16 16:49:25 +0000611 PyTuple_SetItem (pargs, 3, session_dict); // This "steals" a reference to session_dict
612 pvalue = PyObject_CallObject (pfunc, pargs);
613 Py_DECREF (pargs);
614
615 if (pvalue != NULL)
616 {
617 if (pvalue == Py_None) // no error
618 {
619 err_msg.clear();
620 retval = true;
621 }
Johnny Chen2fcf4122011-12-14 20:40:27 +0000622 else
Enrico Granatac2a28252011-08-16 16:49:25 +0000623 {
Johnny Chen2fcf4122011-12-14 20:40:27 +0000624 // return value is an error string
625 if (PyString_CheckExact(pvalue))
626 err_msg.assign(PyString_AsString(pvalue));
Enrico Granatac2a28252011-08-16 16:49:25 +0000627 retval = false;
628 }
629 Py_DECREF (pvalue);
630 }
631 else if (PyErr_Occurred ())
632 {
633 PyErr_Print();
634 PyErr_Clear();
635 }
636 Py_INCREF (session_dict);
637 }
638 else if (PyErr_Occurred())
639 {
640 PyErr_Print();
641 PyErr_Clear();
642 }
643 }
644 else if (PyErr_Occurred())
645 {
646 PyErr_Print();
647 PyErr_Clear();
648 }
649 }
650 else if (PyErr_Occurred ())
651 {
652 PyErr_Print();
653 PyErr_Clear ();
654 }
Enrico Granata59df36f2011-10-17 21:45:27 +0000655 return retval;
656}
657
658SWIGEXPORT bool
659LLDBSwigPythonCallModuleInit
660(
661 const std::string python_module_name,
662 const char *session_dictionary_name,
663 lldb::DebuggerSP& debugger
664)
665{
666
667 lldb::SBDebugger debugger_sb(debugger);
668
669 bool retval = false;
670
671 PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
672
673 if (DebuggerObj_PyObj == NULL)
674 return retval;
675
676 if (!(python_module_name.length()) || !session_dictionary_name)
677 return retval;
678
679 PyObject *session_dict, *pfunc;
680 PyObject *pargs, *pvalue;
681
682 session_dict = FindSessionDictionary (session_dictionary_name);
683
684 std::string python_function_name_string = python_module_name + (".__lldb_init_module");
685 const char* python_function_name = python_function_name_string.c_str();
686
687 if (session_dict != NULL)
688 {
689 pfunc = ResolvePythonName (python_function_name, session_dict);
690
691 if (PyErr_Occurred()) // this might not exist.. let's make sure we handle that
692 {
693 PyErr_Clear();
694 return true;
695 }
696
697 if (pfunc == NULL)
698 return true;
699 else
700 {
701 // Set up the arguments and call the function.
702
703 if (PyCallable_Check (pfunc))
704 {
705 pargs = PyTuple_New (2);
706 if (pargs == NULL)
707 {
708 if (PyErr_Occurred())
709 PyErr_Clear();
710 return retval;
711 }
712
713 PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj
714 PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict
715 pvalue = PyObject_CallObject (pfunc, pargs);
716 Py_DECREF (pargs);
717
718 if (PyErr_Occurred ())
719 {
720 PyErr_Print();
721 PyErr_Clear();
722 }
723 else
724 {
725 retval = true;
726 Py_XDECREF (pvalue);
727 }
728 Py_INCREF (session_dict);
729 }
730 else if (PyErr_Occurred())
731 {
732 PyErr_Print();
733 PyErr_Clear();
734 }
735 }
736 }
737 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000738}
739
Johnny Chenf307cf72011-07-26 19:09:03 +0000740%}