blob: 1442608d6bb4ca004f3249edfcd9243bafa842b5 [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 Granatacf09f882012-03-19 22:58:49 +0000261 PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *)valobj_sb, SWIGTYPE_p_lldb__SBValue, 0);
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 Granatacf09f882012-03-19 22:58:49 +0000459SWIGEXPORT bool
Enrico Granata6a319e42011-07-26 21:02:56 +0000460LLDBSwigPython_UpdateSynthProviderInstance
461(
462 PyObject *implementor
463)
464{
Enrico Granatacf09f882012-03-19 22:58:49 +0000465
466 bool ret_val = false;
467
Enrico Granata6a319e42011-07-26 21:02:56 +0000468 static char callee_name[] = "update";
469
470 if (implementor == NULL || implementor == Py_None)
Enrico Granatacf09f882012-03-19 22:58:49 +0000471 return ret_val;
Enrico Granata6a319e42011-07-26 21:02:56 +0000472
473 // 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
474 // other synth provider calls are mandatory, so we want to fail in a very obvious way if they are missing!
475 PyObject* pmeth = PyObject_GetAttrString(implementor, callee_name);
476
Enrico Granata1239c1a2011-08-11 19:20:44 +0000477 if (PyErr_Occurred())
478 {
479 PyErr_Clear();
480 }
481
Enrico Granata6a319e42011-07-26 21:02:56 +0000482 if (pmeth == NULL || pmeth == Py_None)
483 {
484 Py_XDECREF(pmeth);
Enrico Granatacf09f882012-03-19 22:58:49 +0000485 return ret_val;
Enrico Granata6a319e42011-07-26 21:02:56 +0000486 }
487
488 if (PyCallable_Check(pmeth) == 0)
489 {
Enrico Granata1239c1a2011-08-11 19:20:44 +0000490 if (PyErr_Occurred())
491 {
492 PyErr_Clear();
493 }
494
Enrico Granata6a319e42011-07-26 21:02:56 +0000495 Py_XDECREF(pmeth);
Enrico Granatacf09f882012-03-19 22:58:49 +0000496 return ret_val;
Enrico Granata6a319e42011-07-26 21:02:56 +0000497 }
498
Enrico Granata1239c1a2011-08-11 19:20:44 +0000499 if (PyErr_Occurred())
500 {
501 PyErr_Clear();
502 }
503
Enrico Granata6a319e42011-07-26 21:02:56 +0000504 Py_XDECREF(pmeth);
505
506 // right now we know this function exists and is callable..
507 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL);
508
509 // if it fails, print the error but otherwise go on
510 if (PyErr_Occurred())
511 {
512 PyErr_Print();
513 PyErr_Clear();
514 }
Enrico Granatacf09f882012-03-19 22:58:49 +0000515
516 if (py_return == Py_True)
517 ret_val = true;
Enrico Granata6a319e42011-07-26 21:02:56 +0000518
519 Py_XDECREF(py_return);
Enrico Granatacf09f882012-03-19 22:58:49 +0000520
521 return ret_val;
Enrico Granata6a319e42011-07-26 21:02:56 +0000522
523}
524
Enrico Granata91544802011-09-06 19:20:51 +0000525SWIGEXPORT void*
Johnny Chenf307cf72011-07-26 19:09:03 +0000526LLDBSWIGPython_CastPyObjectToSBValue
527(
528 PyObject* data
529)
530{
531 lldb::SBValue* sb_ptr = NULL;
532
533 int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
534
535 if (valid_cast == -1)
536 return NULL;
537
538 return sb_ptr;
539}
540
Enrico Granata872959b2011-08-20 00:26:17 +0000541// Currently, SBCommandReturnObjectReleaser wraps an std::auto_ptr to an
542// lldb_private::CommandReturnObject. This means that the destructor for the
543// SB object will deallocate its contained CommandReturnObject. Because that
544// object is used as the real return object for Python-based commands, we want
545// it to stay around. Thus, we release the auto_ptr before returning from
546// LLDBSwigPythonCallCommand, and to guarantee that the release will occur no
547// matter how we exit from the function, we have a releaser object whose
548// destructor does the right thing for us
549class SBCommandReturnObjectReleaser
550{
551public:
552 SBCommandReturnObjectReleaser (lldb::SBCommandReturnObject &obj) :
553 m_command_return_object_ref (obj)
554 {
555 }
556
557 ~SBCommandReturnObjectReleaser ()
558 {
559 m_command_return_object_ref.Release();
560 }
561private:
562 lldb::SBCommandReturnObject &m_command_return_object_ref;
563};
Enrico Granata3370f0c2011-08-19 23:56:34 +0000564
Enrico Granatac2a28252011-08-16 16:49:25 +0000565SWIGEXPORT bool
566LLDBSwigPythonCallCommand
567(
568 const char *python_function_name,
569 const char *session_dictionary_name,
570 lldb::DebuggerSP& debugger,
571 const char* args,
572 std::string& err_msg,
Enrico Granata3370f0c2011-08-19 23:56:34 +0000573 lldb_private::CommandReturnObject& cmd_retobj
Enrico Granatac2a28252011-08-16 16:49:25 +0000574)
575{
576
Enrico Granata3370f0c2011-08-19 23:56:34 +0000577 lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj);
Enrico Granata872959b2011-08-20 00:26:17 +0000578 SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb);
Enrico Granata3370f0c2011-08-19 23:56:34 +0000579 lldb::SBDebugger debugger_sb(debugger);
Enrico Granata6b1596d2011-08-16 23:24:13 +0000580
Enrico Granatac2a28252011-08-16 16:49:25 +0000581 bool retval = false;
582
Enrico Granata3370f0c2011-08-19 23:56:34 +0000583 PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
584 PyObject *CmdRetObj_PyObj = SWIG_NewPointerObj((void *) &cmd_retobj_sb, SWIGTYPE_p_lldb__SBCommandReturnObject, 0);
Enrico Granatac2a28252011-08-16 16:49:25 +0000585
586 if (DebuggerObj_PyObj == NULL)
Enrico Granata872959b2011-08-20 00:26:17 +0000587 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000588
Enrico Granata6b1596d2011-08-16 23:24:13 +0000589 if (CmdRetObj_PyObj == NULL)
Enrico Granata872959b2011-08-20 00:26:17 +0000590 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000591
592 if (!python_function_name || !session_dictionary_name)
Enrico Granata872959b2011-08-20 00:26:17 +0000593 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000594
Enrico Granata960691f2011-08-22 17:34:47 +0000595 PyObject *session_dict, *pfunc;
Enrico Granatac2a28252011-08-16 16:49:25 +0000596 PyObject *pargs, *pvalue;
597
Enrico Granata960691f2011-08-22 17:34:47 +0000598 session_dict = FindSessionDictionary (session_dictionary_name);
599 if (session_dict != NULL)
Enrico Granatac2a28252011-08-16 16:49:25 +0000600 {
Enrico Granata960691f2011-08-22 17:34:47 +0000601 pfunc = ResolvePythonName (python_function_name, session_dict);
602 if (pfunc != NULL)
Enrico Granatac2a28252011-08-16 16:49:25 +0000603 {
Enrico Granatac2a28252011-08-16 16:49:25 +0000604 // Set up the arguments and call the function.
605
Enrico Granata960691f2011-08-22 17:34:47 +0000606 if (PyCallable_Check (pfunc))
Enrico Granatac2a28252011-08-16 16:49:25 +0000607 {
608 pargs = PyTuple_New (4);
609 if (pargs == NULL)
610 {
611 if (PyErr_Occurred())
612 PyErr_Clear();
Enrico Granata872959b2011-08-20 00:26:17 +0000613 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000614 }
615
616 PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj
617 PyTuple_SetItem (pargs, 1, PyString_FromString(args));
Enrico Granata6b1596d2011-08-16 23:24:13 +0000618 PyTuple_SetItem (pargs, 2, CmdRetObj_PyObj); // This "steals" a reference to CmdRetObj_PyObj
Enrico Granatac2a28252011-08-16 16:49:25 +0000619 PyTuple_SetItem (pargs, 3, session_dict); // This "steals" a reference to session_dict
620 pvalue = PyObject_CallObject (pfunc, pargs);
621 Py_DECREF (pargs);
622
623 if (pvalue != NULL)
624 {
625 if (pvalue == Py_None) // no error
626 {
627 err_msg.clear();
628 retval = true;
629 }
Johnny Chen2fcf4122011-12-14 20:40:27 +0000630 else
Enrico Granatac2a28252011-08-16 16:49:25 +0000631 {
Johnny Chen2fcf4122011-12-14 20:40:27 +0000632 // return value is an error string
633 if (PyString_CheckExact(pvalue))
634 err_msg.assign(PyString_AsString(pvalue));
Enrico Granatac2a28252011-08-16 16:49:25 +0000635 retval = false;
636 }
637 Py_DECREF (pvalue);
638 }
639 else if (PyErr_Occurred ())
640 {
641 PyErr_Print();
642 PyErr_Clear();
643 }
644 Py_INCREF (session_dict);
645 }
646 else if (PyErr_Occurred())
647 {
648 PyErr_Print();
649 PyErr_Clear();
650 }
651 }
652 else if (PyErr_Occurred())
653 {
654 PyErr_Print();
655 PyErr_Clear();
656 }
657 }
658 else if (PyErr_Occurred ())
659 {
660 PyErr_Print();
661 PyErr_Clear ();
662 }
Enrico Granata59df36f2011-10-17 21:45:27 +0000663 return retval;
664}
665
666SWIGEXPORT bool
667LLDBSwigPythonCallModuleInit
668(
669 const std::string python_module_name,
670 const char *session_dictionary_name,
671 lldb::DebuggerSP& debugger
672)
673{
674
675 lldb::SBDebugger debugger_sb(debugger);
676
677 bool retval = false;
678
679 PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
680
681 if (DebuggerObj_PyObj == NULL)
682 return retval;
683
684 if (!(python_module_name.length()) || !session_dictionary_name)
685 return retval;
686
687 PyObject *session_dict, *pfunc;
688 PyObject *pargs, *pvalue;
689
690 session_dict = FindSessionDictionary (session_dictionary_name);
691
692 std::string python_function_name_string = python_module_name + (".__lldb_init_module");
693 const char* python_function_name = python_function_name_string.c_str();
694
695 if (session_dict != NULL)
696 {
697 pfunc = ResolvePythonName (python_function_name, session_dict);
698
699 if (PyErr_Occurred()) // this might not exist.. let's make sure we handle that
700 {
701 PyErr_Clear();
702 return true;
703 }
704
705 if (pfunc == NULL)
706 return true;
707 else
708 {
709 // Set up the arguments and call the function.
710
711 if (PyCallable_Check (pfunc))
712 {
713 pargs = PyTuple_New (2);
714 if (pargs == NULL)
715 {
716 if (PyErr_Occurred())
717 PyErr_Clear();
718 return retval;
719 }
720
721 PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj
722 PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict
723 pvalue = PyObject_CallObject (pfunc, pargs);
724 Py_DECREF (pargs);
725
726 if (PyErr_Occurred ())
727 {
728 PyErr_Print();
729 PyErr_Clear();
730 }
731 else
732 {
733 retval = true;
734 Py_XDECREF (pvalue);
735 }
736 Py_INCREF (session_dict);
737 }
738 else if (PyErr_Occurred())
739 {
740 PyErr_Print();
741 PyErr_Clear();
742 }
743 }
744 }
745 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000746}
747
Johnny Chenf307cf72011-07-26 19:09:03 +0000748%}