blob: a3aeefb1f77b435abbf6db6ba4cc47d895f51612 [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);
Enrico Granata3d656c72012-10-22 18:18:36 +0000339 valobj_sb->SetPreferSyntheticValue(false);
Johnny Chenf307cf72011-07-26 19:09:03 +0000340
Enrico Granatacf09f882012-03-19 22:58:49 +0000341 PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *)valobj_sb, SWIGTYPE_p_lldb__SBValue, 0);
Johnny Chenf307cf72011-07-26 19:09:03 +0000342
343 if (ValObj_PyObj == NULL)
344 Py_RETURN_NONE;
345
346 const char* python_function_name = python_class_name.c_str();
347
Enrico Granata960691f2011-08-22 17:34:47 +0000348 PyObject *session_dict, *pfunc;
Johnny Chenf307cf72011-07-26 19:09:03 +0000349 PyObject *pvalue;
Enrico Granata960691f2011-08-22 17:34:47 +0000350
351 session_dict = FindSessionDictionary (session_dictionary_name);
352 if (session_dict != NULL)
Johnny Chenf307cf72011-07-26 19:09:03 +0000353 {
Enrico Granata960691f2011-08-22 17:34:47 +0000354 pfunc = ResolvePythonName (python_function_name, session_dict);
355 if (pfunc != NULL)
Johnny Chenf307cf72011-07-26 19:09:03 +0000356 {
Johnny Chenf307cf72011-07-26 19:09:03 +0000357 // Set up the arguments and call the function.
Enrico Granata960691f2011-08-22 17:34:47 +0000358
359 if (PyCallable_Check (pfunc))
Johnny Chenf307cf72011-07-26 19:09:03 +0000360 {
361 PyObject *argList = Py_BuildValue("SS", ValObj_PyObj, session_dict);
362
363 if (PyErr_Occurred ())
364 {
365 PyErr_Print();
366 PyErr_Clear();
367 return retval;
368 }
369
370 if (argList == NULL)
371 {
372 return retval;
373 }
374
375 Py_INCREF(ValObj_PyObj);
376
377 pvalue = PyObject_CallObject(pfunc, argList);
378
379 Py_DECREF(argList);
380
381 if (pvalue != NULL)
382 {
383 if (pvalue != Py_None)
384 retval = pvalue;
385 else
386 {
387 retval = Py_None;
388 Py_INCREF(retval);
389 }
390 }
391 else if (PyErr_Occurred ())
392 {
393 PyErr_Print();
394 PyErr_Clear();
395 }
396 Py_INCREF (session_dict);
397 }
398 else if (PyErr_Occurred())
399 {
400 PyErr_Print();
401 PyErr_Clear();
402 }
403 }
404 else if (PyErr_Occurred())
405 {
406 PyErr_Print();
407 PyErr_Clear();
408 }
409 }
410 else if (PyErr_Occurred ())
411 {
412 PyErr_Print();
413 PyErr_Clear ();
414 }
415 if (retval)
416 return retval;
417 else
418 Py_RETURN_NONE;
419}
420
421/*
422these four calls below are meant to support
423Python-based synthetic children providers
424they essentially mimic the four pure virtual
425method calls provided by the frontend class
426*/
427
428SWIGEXPORT uint32_t
429LLDBSwigPython_CalculateNumChildren
430(
431 PyObject *implementor
432)
433{
434
435 static char callee_name[] = "num_children";
436
437 if (implementor == NULL || implementor == Py_None)
438 return 0;
439 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL);
440 if (PyErr_Occurred())
441 {
442 PyErr_Print();
443 PyErr_Clear();
444 }
445
446 if (py_return == NULL || py_return == Py_None)
447 {
448 Py_XDECREF(py_return);
449 return UINT32_MAX;
450 }
451 long retval = PyInt_AsLong(py_return);
452 Py_DECREF(py_return);
453 if (retval >= 0)
454 return (uint32_t)retval;
455 if (PyErr_Occurred())
456 {
457 PyErr_Print();
458 PyErr_Clear();
459 }
460 return 0;
461}
462
463SWIGEXPORT PyObject*
464LLDBSwigPython_GetChildAtIndex
465(
466 PyObject *implementor,
467 uint32_t idx
468)
469{
470
471 static char callee_name[] = "get_child_at_index";
472 static char param_format[] = "i";
473
474 if (implementor == NULL || implementor == Py_None)
475 return NULL;
476 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, idx);
477 if (PyErr_Occurred())
478 {
479 PyErr_Print();
480 PyErr_Clear();
481 }
482
483 if (py_return == NULL || py_return == Py_None)
484 {
485 Py_XDECREF(py_return);
486 return NULL;
487 }
488
489 lldb::SBValue* sbvalue_ptr = NULL;
490
491 if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
492 {
493 Py_DECREF(py_return);
494 return NULL;
495 }
496
497 if (sbvalue_ptr == NULL)
498 return NULL;
499
500 return py_return;
501}
502
503SWIGEXPORT int
504LLDBSwigPython_GetIndexOfChildWithName
505(
506 PyObject *implementor,
507 const char* child_name
508)
509{
510 static char callee_name[] = "get_child_index";
511 static char param_format[] = "s";
512
513 if (implementor == NULL || implementor == Py_None)
514 return 0;
515 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, child_name);
516 if (PyErr_Occurred())
517 {
518 PyErr_Print();
519 PyErr_Clear();
520 }
521
522 if (py_return == NULL || py_return == Py_None)
523 {
524 Py_XDECREF(py_return);
525 return UINT32_MAX;
526 }
527 long retval = PyInt_AsLong(py_return);
528 Py_DECREF(py_return);
529 if (retval >= 0)
530 return (uint32_t)retval;
531 if (PyErr_Occurred())
532 {
533 PyErr_Print();
534 PyErr_Clear();
535 }
536 return 0;
537}
538
Enrico Granatacf09f882012-03-19 22:58:49 +0000539SWIGEXPORT bool
Enrico Granata6a319e42011-07-26 21:02:56 +0000540LLDBSwigPython_UpdateSynthProviderInstance
541(
542 PyObject *implementor
543)
544{
Enrico Granatacf09f882012-03-19 22:58:49 +0000545
546 bool ret_val = false;
547
Enrico Granata6a319e42011-07-26 21:02:56 +0000548 static char callee_name[] = "update";
549
550 if (implementor == NULL || implementor == Py_None)
Enrico Granatacf09f882012-03-19 22:58:49 +0000551 return ret_val;
Enrico Granata6a319e42011-07-26 21:02:56 +0000552
553 // 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
554 // other synth provider calls are mandatory, so we want to fail in a very obvious way if they are missing!
555 PyObject* pmeth = PyObject_GetAttrString(implementor, callee_name);
556
Enrico Granata1239c1a2011-08-11 19:20:44 +0000557 if (PyErr_Occurred())
558 {
559 PyErr_Clear();
560 }
561
Enrico Granata6a319e42011-07-26 21:02:56 +0000562 if (pmeth == NULL || pmeth == Py_None)
563 {
564 Py_XDECREF(pmeth);
Enrico Granatacf09f882012-03-19 22:58:49 +0000565 return ret_val;
Enrico Granata6a319e42011-07-26 21:02:56 +0000566 }
567
568 if (PyCallable_Check(pmeth) == 0)
569 {
Enrico Granata1239c1a2011-08-11 19:20:44 +0000570 if (PyErr_Occurred())
571 {
572 PyErr_Clear();
573 }
574
Enrico Granata6a319e42011-07-26 21:02:56 +0000575 Py_XDECREF(pmeth);
Enrico Granatacf09f882012-03-19 22:58:49 +0000576 return ret_val;
Enrico Granata6a319e42011-07-26 21:02:56 +0000577 }
578
Enrico Granata1239c1a2011-08-11 19:20:44 +0000579 if (PyErr_Occurred())
580 {
581 PyErr_Clear();
582 }
583
Enrico Granata6a319e42011-07-26 21:02:56 +0000584 Py_XDECREF(pmeth);
585
586 // right now we know this function exists and is callable..
587 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL);
588
589 // if it fails, print the error but otherwise go on
590 if (PyErr_Occurred())
591 {
592 PyErr_Print();
593 PyErr_Clear();
594 }
Enrico Granatacf09f882012-03-19 22:58:49 +0000595
596 if (py_return == Py_True)
597 ret_val = true;
Enrico Granata6a319e42011-07-26 21:02:56 +0000598
599 Py_XDECREF(py_return);
Enrico Granatacf09f882012-03-19 22:58:49 +0000600
601 return ret_val;
Enrico Granata6a319e42011-07-26 21:02:56 +0000602
603}
604
Enrico Granata91544802011-09-06 19:20:51 +0000605SWIGEXPORT void*
Johnny Chenf307cf72011-07-26 19:09:03 +0000606LLDBSWIGPython_CastPyObjectToSBValue
607(
608 PyObject* data
609)
610{
611 lldb::SBValue* sb_ptr = NULL;
612
613 int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
614
615 if (valid_cast == -1)
616 return NULL;
617
618 return sb_ptr;
619}
620
Enrico Granata872959b2011-08-20 00:26:17 +0000621// Currently, SBCommandReturnObjectReleaser wraps an std::auto_ptr to an
622// lldb_private::CommandReturnObject. This means that the destructor for the
623// SB object will deallocate its contained CommandReturnObject. Because that
624// object is used as the real return object for Python-based commands, we want
625// it to stay around. Thus, we release the auto_ptr before returning from
626// LLDBSwigPythonCallCommand, and to guarantee that the release will occur no
627// matter how we exit from the function, we have a releaser object whose
628// destructor does the right thing for us
629class SBCommandReturnObjectReleaser
630{
631public:
632 SBCommandReturnObjectReleaser (lldb::SBCommandReturnObject &obj) :
633 m_command_return_object_ref (obj)
634 {
635 }
636
637 ~SBCommandReturnObjectReleaser ()
638 {
639 m_command_return_object_ref.Release();
640 }
641private:
642 lldb::SBCommandReturnObject &m_command_return_object_ref;
643};
Enrico Granata3370f0c2011-08-19 23:56:34 +0000644
Enrico Granatac2a28252011-08-16 16:49:25 +0000645SWIGEXPORT bool
646LLDBSwigPythonCallCommand
647(
648 const char *python_function_name,
649 const char *session_dictionary_name,
650 lldb::DebuggerSP& debugger,
651 const char* args,
652 std::string& err_msg,
Enrico Granata3370f0c2011-08-19 23:56:34 +0000653 lldb_private::CommandReturnObject& cmd_retobj
Enrico Granatac2a28252011-08-16 16:49:25 +0000654)
655{
656
Enrico Granata3370f0c2011-08-19 23:56:34 +0000657 lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj);
Enrico Granata872959b2011-08-20 00:26:17 +0000658 SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb);
Enrico Granata3370f0c2011-08-19 23:56:34 +0000659 lldb::SBDebugger debugger_sb(debugger);
Enrico Granata6b1596d2011-08-16 23:24:13 +0000660
Enrico Granatac2a28252011-08-16 16:49:25 +0000661 bool retval = false;
662
Enrico Granata3370f0c2011-08-19 23:56:34 +0000663 PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
664 PyObject *CmdRetObj_PyObj = SWIG_NewPointerObj((void *) &cmd_retobj_sb, SWIGTYPE_p_lldb__SBCommandReturnObject, 0);
Enrico Granatac2a28252011-08-16 16:49:25 +0000665
666 if (DebuggerObj_PyObj == NULL)
Enrico Granata872959b2011-08-20 00:26:17 +0000667 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000668
Enrico Granata6b1596d2011-08-16 23:24:13 +0000669 if (CmdRetObj_PyObj == NULL)
Enrico Granata872959b2011-08-20 00:26:17 +0000670 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000671
672 if (!python_function_name || !session_dictionary_name)
Enrico Granata872959b2011-08-20 00:26:17 +0000673 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000674
Enrico Granata960691f2011-08-22 17:34:47 +0000675 PyObject *session_dict, *pfunc;
Enrico Granatac2a28252011-08-16 16:49:25 +0000676 PyObject *pargs, *pvalue;
677
Enrico Granata960691f2011-08-22 17:34:47 +0000678 session_dict = FindSessionDictionary (session_dictionary_name);
679 if (session_dict != NULL)
Enrico Granatac2a28252011-08-16 16:49:25 +0000680 {
Enrico Granata960691f2011-08-22 17:34:47 +0000681 pfunc = ResolvePythonName (python_function_name, session_dict);
682 if (pfunc != NULL)
Enrico Granatac2a28252011-08-16 16:49:25 +0000683 {
Enrico Granatac2a28252011-08-16 16:49:25 +0000684 // Set up the arguments and call the function.
685
Enrico Granata960691f2011-08-22 17:34:47 +0000686 if (PyCallable_Check (pfunc))
Enrico Granatac2a28252011-08-16 16:49:25 +0000687 {
688 pargs = PyTuple_New (4);
689 if (pargs == NULL)
690 {
691 if (PyErr_Occurred())
692 PyErr_Clear();
Enrico Granata872959b2011-08-20 00:26:17 +0000693 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000694 }
695
696 PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj
697 PyTuple_SetItem (pargs, 1, PyString_FromString(args));
Enrico Granata6b1596d2011-08-16 23:24:13 +0000698 PyTuple_SetItem (pargs, 2, CmdRetObj_PyObj); // This "steals" a reference to CmdRetObj_PyObj
Enrico Granatac2a28252011-08-16 16:49:25 +0000699 PyTuple_SetItem (pargs, 3, session_dict); // This "steals" a reference to session_dict
700 pvalue = PyObject_CallObject (pfunc, pargs);
701 Py_DECREF (pargs);
702
703 if (pvalue != NULL)
704 {
705 if (pvalue == Py_None) // no error
706 {
707 err_msg.clear();
708 retval = true;
709 }
Johnny Chen2fcf4122011-12-14 20:40:27 +0000710 else
Enrico Granatac2a28252011-08-16 16:49:25 +0000711 {
Johnny Chen2fcf4122011-12-14 20:40:27 +0000712 // return value is an error string
713 if (PyString_CheckExact(pvalue))
714 err_msg.assign(PyString_AsString(pvalue));
Enrico Granatac2a28252011-08-16 16:49:25 +0000715 retval = false;
716 }
717 Py_DECREF (pvalue);
718 }
719 else if (PyErr_Occurred ())
720 {
721 PyErr_Print();
722 PyErr_Clear();
723 }
724 Py_INCREF (session_dict);
725 }
726 else if (PyErr_Occurred())
727 {
728 PyErr_Print();
729 PyErr_Clear();
730 }
731 }
732 else if (PyErr_Occurred())
733 {
734 PyErr_Print();
735 PyErr_Clear();
736 }
737 }
738 else if (PyErr_Occurred ())
739 {
740 PyErr_Print();
741 PyErr_Clear ();
742 }
Enrico Granata59df36f2011-10-17 21:45:27 +0000743 return retval;
744}
745
Enrico Granata155ee912012-08-24 00:30:47 +0000746SWIGEXPORT void*
747LLDBSWIGPythonCreateOSPlugin
748(
749 const std::string python_class_name,
750 const char *session_dictionary_name,
751 const lldb::ProcessSP& process_sp
752)
753{
754 PyObject* retval = NULL;
755
756 if (python_class_name.empty() || !session_dictionary_name)
757 Py_RETURN_NONE;
758
759 // I do not want the SBValue to be deallocated when going out of scope because python
760 // has ownership of it and will manage memory for this object by itself
761 lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp);
762
763 PyObject *SBProc_PyObj = SWIG_NewPointerObj((void *)process_sb, SWIGTYPE_p_lldb__SBProcess, 0);
764
765 if (SBProc_PyObj == NULL)
766 Py_RETURN_NONE;
767
768 const char* python_function_name = python_class_name.c_str();
769
770 PyObject *session_dict, *pfunc;
771 PyObject *pvalue;
772
773 session_dict = FindSessionDictionary (session_dictionary_name);
774 if (session_dict != NULL)
775 {
776 pfunc = ResolvePythonName (python_function_name, session_dict);
777 if (pfunc != NULL)
778 {
779 // Set up the arguments and call the function.
780
781 if (PyCallable_Check (pfunc))
782 {
Enrico Granatacec963a2012-08-24 01:34:39 +0000783 PyObject *argList = Py_BuildValue("(O)", SBProc_PyObj);
Enrico Granata155ee912012-08-24 00:30:47 +0000784
785 if (PyErr_Occurred ())
786 {
787 PyErr_Print();
788 PyErr_Clear();
789 return retval;
790 }
791
792 if (argList == NULL)
793 {
794 return retval;
795 }
796
797 Py_INCREF(SBProc_PyObj);
798
799 pvalue = PyObject_CallObject(pfunc, argList);
800
801 Py_DECREF(argList);
802
803 if (pvalue != NULL)
804 {
805 if (pvalue != Py_None)
806 retval = pvalue;
807 else
808 {
809 retval = Py_None;
810 Py_INCREF(retval);
811 }
812 }
813 else if (PyErr_Occurred ())
814 {
815 PyErr_Print();
816 PyErr_Clear();
817 }
818 Py_INCREF (session_dict);
819 }
820 else if (PyErr_Occurred())
821 {
822 PyErr_Print();
823 PyErr_Clear();
824 }
825 }
826 else if (PyErr_Occurred())
827 {
828 PyErr_Print();
829 PyErr_Clear();
830 }
831 }
832 else if (PyErr_Occurred ())
833 {
834 PyErr_Print();
835 PyErr_Clear ();
836 }
837 if (retval)
838 return retval;
839 else
840 Py_RETURN_NONE;
841}
842
Enrico Granata59df36f2011-10-17 21:45:27 +0000843SWIGEXPORT bool
844LLDBSwigPythonCallModuleInit
845(
846 const std::string python_module_name,
847 const char *session_dictionary_name,
848 lldb::DebuggerSP& debugger
849)
850{
851
852 lldb::SBDebugger debugger_sb(debugger);
853
854 bool retval = false;
855
856 PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
857
858 if (DebuggerObj_PyObj == NULL)
859 return retval;
860
861 if (!(python_module_name.length()) || !session_dictionary_name)
862 return retval;
863
864 PyObject *session_dict, *pfunc;
865 PyObject *pargs, *pvalue;
866
867 session_dict = FindSessionDictionary (session_dictionary_name);
868
869 std::string python_function_name_string = python_module_name + (".__lldb_init_module");
870 const char* python_function_name = python_function_name_string.c_str();
871
872 if (session_dict != NULL)
873 {
874 pfunc = ResolvePythonName (python_function_name, session_dict);
875
876 if (PyErr_Occurred()) // this might not exist.. let's make sure we handle that
877 {
878 PyErr_Clear();
879 return true;
880 }
881
882 if (pfunc == NULL)
883 return true;
884 else
885 {
886 // Set up the arguments and call the function.
887
888 if (PyCallable_Check (pfunc))
889 {
890 pargs = PyTuple_New (2);
891 if (pargs == NULL)
892 {
893 if (PyErr_Occurred())
894 PyErr_Clear();
895 return retval;
896 }
897
898 PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj
899 PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict
900 pvalue = PyObject_CallObject (pfunc, pargs);
901 Py_DECREF (pargs);
902
903 if (PyErr_Occurred ())
904 {
905 PyErr_Print();
906 PyErr_Clear();
907 }
908 else
909 {
910 retval = true;
911 Py_XDECREF (pvalue);
912 }
913 Py_INCREF (session_dict);
914 }
915 else if (PyErr_Occurred())
916 {
917 PyErr_Print();
918 PyErr_Clear();
919 }
920 }
921 }
922 return retval;
Enrico Granatac2a28252011-08-16 16:49:25 +0000923}
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +0000924%}
Enrico Granatac2a28252011-08-16 16:49:25 +0000925
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +0000926
927%runtime %{
928// Forward declaration to be inserted at the start of LLDBWrapPython.h
929// I used runtime as a hack to make SWIG place it where it's needed.
930// This is needed to use LLDBSwigPythonCallSBInputReaderCallback in the
931// typemaps and in the extensions (SBInputReader.__del__()).
Filipe Cabecinhas85fbe9d2012-08-22 18:10:45 +0000932#include "lldb/API/SBInputReader.h"
Filipe Cabecinhas43898d72012-08-25 00:29:07 +0000933#include "lldb/API/SBDebugger.h"
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +0000934
935size_t
936LLDBSwigPythonCallSBInputReaderCallback(void *baton,
937 lldb::SBInputReader *reader,
938 lldb::InputReaderAction notification,
939 const char*bytes,
940 size_t bytes_len);
Filipe Cabecinhas43898d72012-08-25 00:29:07 +0000941
942void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton);
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +0000943%}
944
945%wrapper %{
946// For the InputReader Callback functions
947SWIGEXPORT size_t
948LLDBSwigPythonCallSBInputReaderCallback(void *baton,
949 lldb::SBInputReader *reader,
950 lldb::InputReaderAction notification,
951 const char*bytes,
952 size_t bytes_len) {
Filipe Cabecinhas43898d72012-08-25 00:29:07 +0000953 if (baton != Py_None) {
954 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
955
956 PyObject *py_InputReader = SWIG_NewPointerObj(reader, SWIGTYPE_p_lldb__SBInputReader, false);
957 PyObject *py_Notification = PyInt_FromLong(notification);
958 PyObject *py_Bytes = PyBytes_FromStringAndSize(bytes, bytes_len);
959
960 PyObject *tuple = PyTuple_Pack(3, py_InputReader, py_Notification, py_Bytes);
961 PyObject *res = PyObject_Call(reinterpret_cast<PyObject*>(baton), tuple, NULL);
962 Py_DECREF(tuple);
963 Py_DECREF(py_InputReader);
964 Py_DECREF(py_Notification);
965 Py_DECREF(py_Bytes);
966
967 if (res == NULL) {
968 PyObject *exc = PyErr_Occurred();
969 if (exc) {
970 ::puts("\nErroring out at LLDBSwigPythonCallSBInputReaderCallback");
971 PyErr_Print();
972 }
973 return 0;
974 }
975
976 size_t result = 0;
977 // If the callback misbehaves and returns Py_None, assume it returned 0
978 if (res != Py_None)
979 result = static_cast<size_t>(PyInt_AsSsize_t(res));
980
981 Py_DECREF(res);
982 SWIG_PYTHON_THREAD_END_BLOCK;
983 return result;
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +0000984 }
Enrico Granatae25c6312012-08-27 18:30:45 +0000985 return 0;
Filipe Cabecinhas43898d72012-08-25 00:29:07 +0000986}
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +0000987
Filipe Cabecinhas43898d72012-08-25 00:29:07 +0000988// For the LogOutputCallback functions
989void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) {
990 if (baton != Py_None) {
991 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
992 PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str);
993 SWIG_PYTHON_THREAD_END_BLOCK;
994 }
Filipe Cabecinhas55ea73d2012-08-22 13:25:10 +0000995}
Johnny Chenf307cf72011-07-26 19:09:03 +0000996%}