blob: f6e3afd4c3950800f343d68e9fd79d589ef6dd7d [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{
34
35 //printf("Resolving %s\n", name);
36
37 if (!name || !name[0])
38 return pmodule;
39
40 PyErr_Cleaner pyerr_cleanup(true); // show Python errors
41
42 PyObject* main_dict;
43
44 if (!pmodule)
45 {
46 pmodule = PyImport_AddModule ("__main__");
47 if (!pmodule)
48 return NULL;
49 }
50
51 if (!PyDict_Check(pmodule))
52 {
53 main_dict = PyModule_GetDict (pmodule);
54 if (!main_dict)
55 return NULL;
56 }
57 else
58 main_dict = pmodule;
59
60 const char* dot_pos = ::strchr(name, '.');
61
62 PyObject *dest_object;
63 PyObject *key, *value;
64 Py_ssize_t pos = 0;
65
66 if (!dot_pos)
67 {
68 if (PyDict_Check (main_dict))
69 {
70 dest_object = NULL;
71 while (PyDict_Next (main_dict, &pos, &key, &value))
72 {
73 // We have stolen references to the key and value objects in the dictionary; we need to increment
74 // them now so that Python's garbage collector doesn't collect them out from under us.
75 Py_INCREF (key);
76 Py_INCREF (value);
77 //printf("Comparing %s and %s\n", name, PyString_AsString (key));
78 if (strcmp (PyString_AsString (key), name) == 0)
79 {
80 dest_object = value;
81 break;
82 }
83 }
84 }
85
86 if (!dest_object || dest_object == Py_None)
87 return NULL;
88 return dest_object;
89 }
90 // foo.bar.ba
91 // 0123456789
92 // len = 3 - 0
93 size_t len = dot_pos - name;
94 std::string piece(name,len);
95 dest_object = ResolvePythonName(piece.c_str(), main_dict);
96 //printf("Resolved %s to %p\n", piece.c_str(), dest_object);
97 if (!dest_object)
98 return NULL;
99 //printf("Now moving to resolve %s\n", dot_pos+1);
100 return ResolvePythonName(dot_pos+1,dest_object); // tail recursion.. should be optimized by the compiler
101
102}
103
104static PyObject*
105FindSessionDictionary(const char *session_dictionary_name)
106{
107 return ResolvePythonName(session_dictionary_name, NULL);
108}
109
Johnny Chenf307cf72011-07-26 19:09:03 +0000110// This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...)
111// and is used when a script command is attached to a breakpoint for execution.
112
113SWIGEXPORT bool
114LLDBSwigPythonBreakpointCallbackFunction
115(
116 const char *python_function_name,
117 const char *session_dictionary_name,
118 const lldb::StackFrameSP& frame_sp,
119 const lldb::BreakpointLocationSP& bp_loc_sp
120)
121{
122 lldb::SBFrame sb_frame (frame_sp);
123 lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
124
125 bool stop_at_breakpoint = true;
126 PyObject *Frame_PyObj = SWIG_NewPointerObj((void *) &sb_frame, SWIGTYPE_p_lldb__SBFrame, 0);
127 PyObject *Bp_Loc_PyObj = SWIG_NewPointerObj ((void *) &sb_bp_loc, SWIGTYPE_p_lldb__SBBreakpointLocation, 0);
128
129 if (Frame_PyObj == NULL || Bp_Loc_PyObj == NULL)
130 return stop_at_breakpoint;
131
132 if (!python_function_name || !session_dictionary_name)
133 return stop_at_breakpoint;
134
Enrico Granata960691f2011-08-22 17:34:47 +0000135 PyObject *session_dict, *pfunc;
Johnny Chenf307cf72011-07-26 19:09:03 +0000136 PyObject *pargs, *pvalue;
137
Enrico Granata960691f2011-08-22 17:34:47 +0000138 session_dict = FindSessionDictionary (session_dictionary_name);
139 if (session_dict != NULL)
Johnny Chenf307cf72011-07-26 19:09:03 +0000140 {
Enrico Granata960691f2011-08-22 17:34:47 +0000141 pfunc = ResolvePythonName (python_function_name, session_dict);
142 if (pfunc != NULL)
Johnny Chenf307cf72011-07-26 19:09:03 +0000143 {
Johnny Chenf307cf72011-07-26 19:09:03 +0000144 // Set up the arguments and call the function.
145
Enrico Granata960691f2011-08-22 17:34:47 +0000146 if (PyCallable_Check (pfunc))
Johnny Chenf307cf72011-07-26 19:09:03 +0000147 {
148 pargs = PyTuple_New (3);
149 if (pargs == NULL)
150 {
151 if (PyErr_Occurred())
152 PyErr_Clear();
153 return stop_at_breakpoint;
154 }
155
156 PyTuple_SetItem (pargs, 0, Frame_PyObj); // This "steals" a reference to Frame_PyObj
157 PyTuple_SetItem (pargs, 1, Bp_Loc_PyObj); // This "steals" a reference to Bp_Loc_PyObj
158 PyTuple_SetItem (pargs, 2, session_dict); // This "steals" a reference to session_dict
159 pvalue = PyObject_CallObject (pfunc, pargs);
160 Py_DECREF (pargs);
161
162 if (pvalue != NULL)
163 {
164 Py_DECREF (pvalue);
165 }
166 else if (PyErr_Occurred ())
167 {
168 PyErr_Clear();
169 }
170 Py_INCREF (session_dict);
171 }
172 else if (PyErr_Occurred())
173 {
174 PyErr_Clear();
175 }
176 }
177 else if (PyErr_Occurred())
178 {
179 PyErr_Clear();
180 }
181 }
182 else if (PyErr_Occurred ())
183 {
184 PyErr_Clear ();
185 }
186 return stop_at_breakpoint;
187}
188
189SWIGEXPORT std::string
190LLDBSwigPythonCallTypeScript
191(
192 const char *python_function_name,
193 const char *session_dictionary_name,
194 const lldb::ValueObjectSP& valobj_sp
195)
196{
197 lldb::SBValue sb_value (valobj_sp);
198
199 std::string retval = "";
200
Enrico Granata3370f0c2011-08-19 23:56:34 +0000201 PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *) &sb_value, SWIGTYPE_p_lldb__SBValue, 0);
Johnny Chenf307cf72011-07-26 19:09:03 +0000202
203 if (ValObj_PyObj == NULL)
204 return retval;
205
206 if (!python_function_name || !session_dictionary_name)
207 return retval;
208
Enrico Granata960691f2011-08-22 17:34:47 +0000209 PyObject *session_dict, *pfunc;
Johnny Chenf307cf72011-07-26 19:09:03 +0000210 PyObject *pargs, *pvalue;
211
Enrico Granata960691f2011-08-22 17:34:47 +0000212 session_dict = FindSessionDictionary (session_dictionary_name);
213 if (session_dict != NULL)
Johnny Chenf307cf72011-07-26 19:09:03 +0000214 {
Enrico Granata960691f2011-08-22 17:34:47 +0000215 pfunc = ResolvePythonName (python_function_name, session_dict);
216 if (pfunc != NULL)
Johnny Chenf307cf72011-07-26 19:09:03 +0000217 {
Johnny Chenf307cf72011-07-26 19:09:03 +0000218 // Set up the arguments and call the function.
219
Enrico Granata960691f2011-08-22 17:34:47 +0000220 if (PyCallable_Check (pfunc))
Johnny Chenf307cf72011-07-26 19:09:03 +0000221 {
222 pargs = PyTuple_New (2);
223 if (pargs == NULL)
224 {
225 if (PyErr_Occurred())
226 PyErr_Clear();
227 return retval;
228 }
229
230 PyTuple_SetItem (pargs, 0, ValObj_PyObj); // This "steals" a reference to ValObj_PyObj
231 PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict
232 pvalue = PyObject_CallObject (pfunc, pargs);
233 Py_DECREF (pargs);
234
235 if (pvalue != NULL)
236 {
Johnny Chena0d06cd2011-12-14 23:27:53 +0000237 if (pvalue != Py_None && PyString_CheckExact(pvalue))
Johnny Chenf307cf72011-07-26 19:09:03 +0000238 retval = std::string(PyString_AsString(pvalue));
239 else
240 retval = "None";
241 Py_DECREF (pvalue);
242 }
243 else if (PyErr_Occurred ())
244 {
245 PyErr_Print();
246 PyErr_Clear();
247 }
248 Py_INCREF (session_dict);
249 }
250 else if (PyErr_Occurred())
251 {
252 PyErr_Print();
253 PyErr_Clear();
254 }
255 }
256 else if (PyErr_Occurred())
257 {
258 PyErr_Print();
259 PyErr_Clear();
260 }
261 }
262 else if (PyErr_Occurred ())
263 {
264 PyErr_Print();
265 PyErr_Clear ();
266 }
267 return retval;
268}
269
270SWIGEXPORT void*
271LLDBSwigPythonCreateSyntheticProvider
272(
273 const std::string python_class_name,
274 const char *session_dictionary_name,
275 const lldb::ValueObjectSP& valobj_sp
276)
277{
278 PyObject* retval = NULL;
279
280 if (python_class_name.empty() || !session_dictionary_name)
281 Py_RETURN_NONE;
282
Enrico Granata3370f0c2011-08-19 23:56:34 +0000283 // I do not want the SBValue to be deallocated when going out of scope because python
284 // has ownership of it and will manage memory for this object by itself
285 lldb::SBValue *valobj_sb = new lldb::SBValue(valobj_sp);
Johnny Chenf307cf72011-07-26 19:09:03 +0000286
Enrico Granata3370f0c2011-08-19 23:56:34 +0000287 PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *)valobj_sb, SWIGTYPE_p_lldb__SBValue, SWIG_POINTER_OWN);
Johnny Chenf307cf72011-07-26 19:09:03 +0000288
289 if (ValObj_PyObj == NULL)
290 Py_RETURN_NONE;
291
292 const char* python_function_name = python_class_name.c_str();
293
Enrico Granata960691f2011-08-22 17:34:47 +0000294 PyObject *session_dict, *pfunc;
Johnny Chenf307cf72011-07-26 19:09:03 +0000295 PyObject *pvalue;
Enrico Granata960691f2011-08-22 17:34:47 +0000296
297 session_dict = FindSessionDictionary (session_dictionary_name);
298 if (session_dict != NULL)
Johnny Chenf307cf72011-07-26 19:09:03 +0000299 {
Enrico Granata960691f2011-08-22 17:34:47 +0000300 pfunc = ResolvePythonName (python_function_name, session_dict);
301 if (pfunc != NULL)
Johnny Chenf307cf72011-07-26 19:09:03 +0000302 {
Johnny Chenf307cf72011-07-26 19:09:03 +0000303 // Set up the arguments and call the function.
Enrico Granata960691f2011-08-22 17:34:47 +0000304
305 if (PyCallable_Check (pfunc))
Johnny Chenf307cf72011-07-26 19:09:03 +0000306 {
307 PyObject *argList = Py_BuildValue("SS", ValObj_PyObj, session_dict);
308
309 if (PyErr_Occurred ())
310 {
311 PyErr_Print();
312 PyErr_Clear();
313 return retval;
314 }
315
316 if (argList == NULL)
317 {
318 return retval;
319 }
320
321 Py_INCREF(ValObj_PyObj);
322
323 pvalue = PyObject_CallObject(pfunc, argList);
324
325 Py_DECREF(argList);
326
327 if (pvalue != NULL)
328 {
329 if (pvalue != Py_None)
330 retval = pvalue;
331 else
332 {
333 retval = Py_None;
334 Py_INCREF(retval);
335 }
336 }
337 else if (PyErr_Occurred ())
338 {
339 PyErr_Print();
340 PyErr_Clear();
341 }
342 Py_INCREF (session_dict);
343 }
344 else if (PyErr_Occurred())
345 {
346 PyErr_Print();
347 PyErr_Clear();
348 }
349 }
350 else if (PyErr_Occurred())
351 {
352 PyErr_Print();
353 PyErr_Clear();
354 }
355 }
356 else if (PyErr_Occurred ())
357 {
358 PyErr_Print();
359 PyErr_Clear ();
360 }
361 if (retval)
362 return retval;
363 else
364 Py_RETURN_NONE;
365}
366
367/*
368these four calls below are meant to support
369Python-based synthetic children providers
370they essentially mimic the four pure virtual
371method calls provided by the frontend class
372*/
373
374SWIGEXPORT uint32_t
375LLDBSwigPython_CalculateNumChildren
376(
377 PyObject *implementor
378)
379{
380
381 static char callee_name[] = "num_children";
382
383 if (implementor == NULL || implementor == Py_None)
384 return 0;
385 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL);
386 if (PyErr_Occurred())
387 {
388 PyErr_Print();
389 PyErr_Clear();
390 }
391
392 if (py_return == NULL || py_return == Py_None)
393 {
394 Py_XDECREF(py_return);
395 return UINT32_MAX;
396 }
397 long retval = PyInt_AsLong(py_return);
398 Py_DECREF(py_return);
399 if (retval >= 0)
400 return (uint32_t)retval;
401 if (PyErr_Occurred())
402 {
403 PyErr_Print();
404 PyErr_Clear();
405 }
406 return 0;
407}
408
409SWIGEXPORT PyObject*
410LLDBSwigPython_GetChildAtIndex
411(
412 PyObject *implementor,
413 uint32_t idx
414)
415{
416
417 static char callee_name[] = "get_child_at_index";
418 static char param_format[] = "i";
419
420 if (implementor == NULL || implementor == Py_None)
421 return NULL;
422 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, idx);
423 if (PyErr_Occurred())
424 {
425 PyErr_Print();
426 PyErr_Clear();
427 }
428
429 if (py_return == NULL || py_return == Py_None)
430 {
431 Py_XDECREF(py_return);
432 return NULL;
433 }
434
435 lldb::SBValue* sbvalue_ptr = NULL;
436
437 if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
438 {
439 Py_DECREF(py_return);
440 return NULL;
441 }
442
443 if (sbvalue_ptr == NULL)
444 return NULL;
445
446 return py_return;
447}
448
449SWIGEXPORT int
450LLDBSwigPython_GetIndexOfChildWithName
451(
452 PyObject *implementor,
453 const char* child_name
454)
455{
456 static char callee_name[] = "get_child_index";
457 static char param_format[] = "s";
458
459 if (implementor == NULL || implementor == Py_None)
460 return 0;
461 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, child_name);
462 if (PyErr_Occurred())
463 {
464 PyErr_Print();
465 PyErr_Clear();
466 }
467
468 if (py_return == NULL || py_return == Py_None)
469 {
470 Py_XDECREF(py_return);
471 return UINT32_MAX;
472 }
473 long retval = PyInt_AsLong(py_return);
474 Py_DECREF(py_return);
475 if (retval >= 0)
476 return (uint32_t)retval;
477 if (PyErr_Occurred())
478 {
479 PyErr_Print();
480 PyErr_Clear();
481 }
482 return 0;
483}
484
Enrico Granata6a319e42011-07-26 21:02:56 +0000485SWIGEXPORT void
486LLDBSwigPython_UpdateSynthProviderInstance
487(
488 PyObject *implementor
489)
490{
491 static char callee_name[] = "update";
492
493 if (implementor == NULL || implementor == Py_None)
494 return;
495
496 // 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
497 // other synth provider calls are mandatory, so we want to fail in a very obvious way if they are missing!
498 PyObject* pmeth = PyObject_GetAttrString(implementor, callee_name);
499
Enrico Granata1239c1a2011-08-11 19:20:44 +0000500 if (PyErr_Occurred())
501 {
502 PyErr_Clear();
503 }
504
Enrico Granata6a319e42011-07-26 21:02:56 +0000505 if (pmeth == NULL || pmeth == Py_None)
506 {
507 Py_XDECREF(pmeth);
508 return;
509 }
510
511 if (PyCallable_Check(pmeth) == 0)
512 {
Enrico Granata1239c1a2011-08-11 19:20:44 +0000513 if (PyErr_Occurred())
514 {
515 PyErr_Clear();
516 }
517
Enrico Granata6a319e42011-07-26 21:02:56 +0000518 Py_XDECREF(pmeth);
519 return;
520 }
521
Enrico Granata1239c1a2011-08-11 19:20:44 +0000522 if (PyErr_Occurred())
523 {
524 PyErr_Clear();
525 }
526
Enrico Granata6a319e42011-07-26 21:02:56 +0000527 Py_XDECREF(pmeth);
528
529 // right now we know this function exists and is callable..
530 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL);
531
532 // if it fails, print the error but otherwise go on
533 if (PyErr_Occurred())
534 {
535 PyErr_Print();
536 PyErr_Clear();
537 }
538
539 Py_XDECREF(py_return);
540
541}
542
Enrico Granata91544802011-09-06 19:20:51 +0000543SWIGEXPORT void*
Johnny Chenf307cf72011-07-26 19:09:03 +0000544LLDBSWIGPython_CastPyObjectToSBValue
545(
546 PyObject* data
547)
548{
549 lldb::SBValue* sb_ptr = NULL;
550
551 int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
552
553 if (valid_cast == -1)
554 return NULL;
555
556 return sb_ptr;
557}
558
Enrico Granata872959b2011-08-20 00:26:17 +0000559// Currently, SBCommandReturnObjectReleaser wraps an std::auto_ptr to an
560// lldb_private::CommandReturnObject. This means that the destructor for the
561// SB object will deallocate its contained CommandReturnObject. Because that
562// object is used as the real return object for Python-based commands, we want
563// it to stay around. Thus, we release the auto_ptr before returning from
564// LLDBSwigPythonCallCommand, and to guarantee that the release will occur no
565// matter how we exit from the function, we have a releaser object whose
566// destructor does the right thing for us
567class SBCommandReturnObjectReleaser
568{
569public:
570 SBCommandReturnObjectReleaser (lldb::SBCommandReturnObject &obj) :
571 m_command_return_object_ref (obj)
572 {
573 }
574
575 ~SBCommandReturnObjectReleaser ()
576 {
577 m_command_return_object_ref.Release();
578 }
579private:
580 lldb::SBCommandReturnObject &m_command_return_object_ref;
581};
Enrico Granata3370f0c2011-08-19 23:56:34 +0000582
Enrico Granatac2a28252011-08-16 16:49:25 +0000583SWIGEXPORT bool
584LLDBSwigPythonCallCommand
585(
586 const char *python_function_name,
587 const char *session_dictionary_name,
588 lldb::DebuggerSP& debugger,
589 const char* args,
590 std::string& err_msg,
Enrico Granata3370f0c2011-08-19 23:56:34 +0000591 lldb_private::CommandReturnObject& cmd_retobj
Enrico Granatac2a28252011-08-16 16:49:25 +0000592)
593{
594
Enrico Granata3370f0c2011-08-19 23:56:34 +0000595 lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj);
Enrico Granata872959b2011-08-20 00:26:17 +0000596 SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb);
Enrico Granata3370f0c2011-08-19 23:56:34 +0000597 lldb::SBDebugger debugger_sb(debugger);
Enrico Granata6b1596d2011-08-16 23:24:13 +0000598
Enrico Granatac2a28252011-08-16 16:49:25 +0000599 bool retval = false;
600
Enrico Granata3370f0c2011-08-19 23:56:34 +0000601 PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
602 PyObject *CmdRetObj_PyObj = SWIG_NewPointerObj((void *) &cmd_retobj_sb, SWIGTYPE_p_lldb__SBCommandReturnObject, 0);
Enrico Granatac2a28252011-08-16 16:49:25 +0000603
604 if (DebuggerObj_PyObj == NULL)
Enrico Granata872959b2011-08-20 00:26:17 +0000605 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000606
Enrico Granata6b1596d2011-08-16 23:24:13 +0000607 if (CmdRetObj_PyObj == NULL)
Enrico Granata872959b2011-08-20 00:26:17 +0000608 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000609
610 if (!python_function_name || !session_dictionary_name)
Enrico Granata872959b2011-08-20 00:26:17 +0000611 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000612
Enrico Granata960691f2011-08-22 17:34:47 +0000613 PyObject *session_dict, *pfunc;
Enrico Granatac2a28252011-08-16 16:49:25 +0000614 PyObject *pargs, *pvalue;
615
Enrico Granata960691f2011-08-22 17:34:47 +0000616 session_dict = FindSessionDictionary (session_dictionary_name);
617 if (session_dict != NULL)
Enrico Granatac2a28252011-08-16 16:49:25 +0000618 {
Enrico Granata960691f2011-08-22 17:34:47 +0000619 pfunc = ResolvePythonName (python_function_name, session_dict);
620 if (pfunc != NULL)
Enrico Granatac2a28252011-08-16 16:49:25 +0000621 {
Enrico Granatac2a28252011-08-16 16:49:25 +0000622 // Set up the arguments and call the function.
623
Enrico Granata960691f2011-08-22 17:34:47 +0000624 if (PyCallable_Check (pfunc))
Enrico Granatac2a28252011-08-16 16:49:25 +0000625 {
626 pargs = PyTuple_New (4);
627 if (pargs == NULL)
628 {
629 if (PyErr_Occurred())
630 PyErr_Clear();
Enrico Granata872959b2011-08-20 00:26:17 +0000631 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000632 }
633
634 PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj
635 PyTuple_SetItem (pargs, 1, PyString_FromString(args));
Enrico Granata6b1596d2011-08-16 23:24:13 +0000636 PyTuple_SetItem (pargs, 2, CmdRetObj_PyObj); // This "steals" a reference to CmdRetObj_PyObj
Enrico Granatac2a28252011-08-16 16:49:25 +0000637 PyTuple_SetItem (pargs, 3, session_dict); // This "steals" a reference to session_dict
638 pvalue = PyObject_CallObject (pfunc, pargs);
639 Py_DECREF (pargs);
640
641 if (pvalue != NULL)
642 {
643 if (pvalue == Py_None) // no error
644 {
645 err_msg.clear();
646 retval = true;
647 }
Johnny Chen2fcf4122011-12-14 20:40:27 +0000648 else
Enrico Granatac2a28252011-08-16 16:49:25 +0000649 {
Johnny Chen2fcf4122011-12-14 20:40:27 +0000650 // return value is an error string
651 if (PyString_CheckExact(pvalue))
652 err_msg.assign(PyString_AsString(pvalue));
Enrico Granatac2a28252011-08-16 16:49:25 +0000653 retval = false;
654 }
655 Py_DECREF (pvalue);
656 }
657 else if (PyErr_Occurred ())
658 {
659 PyErr_Print();
660 PyErr_Clear();
661 }
662 Py_INCREF (session_dict);
663 }
664 else if (PyErr_Occurred())
665 {
666 PyErr_Print();
667 PyErr_Clear();
668 }
669 }
670 else if (PyErr_Occurred())
671 {
672 PyErr_Print();
673 PyErr_Clear();
674 }
675 }
676 else if (PyErr_Occurred ())
677 {
678 PyErr_Print();
679 PyErr_Clear ();
680 }
Enrico Granata59df36f2011-10-17 21:45:27 +0000681 return retval;
682}
683
684SWIGEXPORT bool
685LLDBSwigPythonCallModuleInit
686(
687 const std::string python_module_name,
688 const char *session_dictionary_name,
689 lldb::DebuggerSP& debugger
690)
691{
692
693 lldb::SBDebugger debugger_sb(debugger);
694
695 bool retval = false;
696
697 PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
698
699 if (DebuggerObj_PyObj == NULL)
700 return retval;
701
702 if (!(python_module_name.length()) || !session_dictionary_name)
703 return retval;
704
705 PyObject *session_dict, *pfunc;
706 PyObject *pargs, *pvalue;
707
708 session_dict = FindSessionDictionary (session_dictionary_name);
709
710 std::string python_function_name_string = python_module_name + (".__lldb_init_module");
711 const char* python_function_name = python_function_name_string.c_str();
712
713 if (session_dict != NULL)
714 {
715 pfunc = ResolvePythonName (python_function_name, session_dict);
716
717 if (PyErr_Occurred()) // this might not exist.. let's make sure we handle that
718 {
719 PyErr_Clear();
720 return true;
721 }
722
723 if (pfunc == NULL)
724 return true;
725 else
726 {
727 // Set up the arguments and call the function.
728
729 if (PyCallable_Check (pfunc))
730 {
731 pargs = PyTuple_New (2);
732 if (pargs == NULL)
733 {
734 if (PyErr_Occurred())
735 PyErr_Clear();
736 return retval;
737 }
738
739 PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj
740 PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict
741 pvalue = PyObject_CallObject (pfunc, pargs);
742 Py_DECREF (pargs);
743
744 if (PyErr_Occurred ())
745 {
746 PyErr_Print();
747 PyErr_Clear();
748 }
749 else
750 {
751 retval = true;
752 Py_XDECREF (pvalue);
753 }
754 Py_INCREF (session_dict);
755 }
756 else if (PyErr_Occurred())
757 {
758 PyErr_Print();
759 PyErr_Clear();
760 }
761 }
762 }
763 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000764}
765
Johnny Chenf307cf72011-07-26 19:09:03 +0000766%}