Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 1 | %wrapper %{ |
| 2 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 3 | class PyErr_Cleaner |
| 4 | { |
| 5 | public: |
| 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 | |
| 21 | private: |
| 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 | |
| 30 | static PyObject* |
| 31 | ResolvePythonName(const char* name, |
| 32 | PyObject* pmodule = NULL) |
| 33 | { |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 34 | if (!name) |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 35 | 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 Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 65 | dest_object = NULL; |
| 66 | while (PyDict_Next (main_dict, &pos, &key, &value)) |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 67 | { |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 68 | // 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 Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 73 | { |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 74 | dest_object = value; |
| 75 | break; |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 76 | } |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 77 | } |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 78 | if (!dest_object || dest_object == Py_None) |
| 79 | return NULL; |
| 80 | return dest_object; |
| 81 | } |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 82 | 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 Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static PyObject* |
| 95 | FindSessionDictionary(const char *session_dictionary_name) |
| 96 | { |
| 97 | return ResolvePythonName(session_dictionary_name, NULL); |
| 98 | } |
| 99 | |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 100 | // 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 | |
| 103 | SWIGEXPORT bool |
| 104 | LLDBSwigPythonBreakpointCallbackFunction |
| 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 Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 125 | PyObject *session_dict, *pfunc; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 126 | PyObject *pargs, *pvalue; |
| 127 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 128 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 129 | if (session_dict != NULL) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 130 | { |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 131 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 132 | if (pfunc != NULL) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 133 | { |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 134 | // Set up the arguments and call the function. |
| 135 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 136 | if (PyCallable_Check (pfunc)) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 137 | { |
| 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 Chen | f3ec461 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 179 | // 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 | |
| 182 | SWIGEXPORT bool |
| 183 | LLDBSwigPythonWatchpointCallbackFunction |
| 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 Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 258 | SWIGEXPORT bool |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 259 | LLDBSwigPythonCallTypeScript |
| 260 | ( |
| 261 | const char *python_function_name, |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 262 | const void *session_dictionary, |
| 263 | const lldb::ValueObjectSP& valobj_sp, |
| 264 | void** pyfunct_wrapper, |
| 265 | std::string& retval |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 266 | ) |
| 267 | { |
| 268 | lldb::SBValue sb_value (valobj_sp); |
| 269 | |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 270 | PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *) &sb_value, SWIGTYPE_p_lldb__SBValue, 0); |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 271 | |
| 272 | if (ValObj_PyObj == NULL) |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 273 | return false; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 274 | |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 275 | if (!python_function_name || !session_dictionary) |
| 276 | return false; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 277 | |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 278 | PyObject *session_dict = (PyObject*)session_dictionary, *pfunc = NULL, *pargs = NULL, *pvalue = NULL; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 279 | |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 280 | if (pyfunct_wrapper && *pyfunct_wrapper && PyFunction_Check (*pyfunct_wrapper)) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 281 | { |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 282 | pfunc = (PyObject*)(*pyfunct_wrapper); |
| 283 | if (pfunc->ob_refcnt == 1) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 284 | { |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 285 | Py_XDECREF(pfunc); |
| 286 | pfunc = NULL; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 289 | |
| 290 | if (PyDict_Check(session_dict)) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 291 | { |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 292 | 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 Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 319 | } |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 320 | return true; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | SWIGEXPORT void* |
| 324 | LLDBSwigPythonCreateSyntheticProvider |
| 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 Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 336 | // I do not want the SBValue to be deallocated when going out of scope because python |
| 337 | // has ownership of it and will manage memory for this object by itself |
| 338 | lldb::SBValue *valobj_sb = new lldb::SBValue(valobj_sp); |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 339 | |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 340 | PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *)valobj_sb, SWIGTYPE_p_lldb__SBValue, 0); |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 341 | |
| 342 | if (ValObj_PyObj == NULL) |
| 343 | Py_RETURN_NONE; |
| 344 | |
| 345 | const char* python_function_name = python_class_name.c_str(); |
| 346 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 347 | PyObject *session_dict, *pfunc; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 348 | PyObject *pvalue; |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 349 | |
| 350 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 351 | if (session_dict != NULL) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 352 | { |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 353 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 354 | if (pfunc != NULL) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 355 | { |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 356 | // Set up the arguments and call the function. |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 357 | |
| 358 | if (PyCallable_Check (pfunc)) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 359 | { |
| 360 | PyObject *argList = Py_BuildValue("SS", ValObj_PyObj, session_dict); |
| 361 | |
| 362 | if (PyErr_Occurred ()) |
| 363 | { |
| 364 | PyErr_Print(); |
| 365 | PyErr_Clear(); |
| 366 | return retval; |
| 367 | } |
| 368 | |
| 369 | if (argList == NULL) |
| 370 | { |
| 371 | return retval; |
| 372 | } |
| 373 | |
| 374 | Py_INCREF(ValObj_PyObj); |
| 375 | |
| 376 | pvalue = PyObject_CallObject(pfunc, argList); |
| 377 | |
| 378 | Py_DECREF(argList); |
| 379 | |
| 380 | if (pvalue != NULL) |
| 381 | { |
| 382 | if (pvalue != Py_None) |
| 383 | retval = pvalue; |
| 384 | else |
| 385 | { |
| 386 | retval = Py_None; |
| 387 | Py_INCREF(retval); |
| 388 | } |
| 389 | } |
| 390 | else if (PyErr_Occurred ()) |
| 391 | { |
| 392 | PyErr_Print(); |
| 393 | PyErr_Clear(); |
| 394 | } |
| 395 | Py_INCREF (session_dict); |
| 396 | } |
| 397 | else if (PyErr_Occurred()) |
| 398 | { |
| 399 | PyErr_Print(); |
| 400 | PyErr_Clear(); |
| 401 | } |
| 402 | } |
| 403 | else if (PyErr_Occurred()) |
| 404 | { |
| 405 | PyErr_Print(); |
| 406 | PyErr_Clear(); |
| 407 | } |
| 408 | } |
| 409 | else if (PyErr_Occurred ()) |
| 410 | { |
| 411 | PyErr_Print(); |
| 412 | PyErr_Clear (); |
| 413 | } |
| 414 | if (retval) |
| 415 | return retval; |
| 416 | else |
| 417 | Py_RETURN_NONE; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | these four calls below are meant to support |
| 422 | Python-based synthetic children providers |
| 423 | they essentially mimic the four pure virtual |
| 424 | method calls provided by the frontend class |
| 425 | */ |
| 426 | |
| 427 | SWIGEXPORT uint32_t |
| 428 | LLDBSwigPython_CalculateNumChildren |
| 429 | ( |
| 430 | PyObject *implementor |
| 431 | ) |
| 432 | { |
| 433 | |
| 434 | static char callee_name[] = "num_children"; |
| 435 | |
| 436 | if (implementor == NULL || implementor == Py_None) |
| 437 | return 0; |
| 438 | PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL); |
| 439 | if (PyErr_Occurred()) |
| 440 | { |
| 441 | PyErr_Print(); |
| 442 | PyErr_Clear(); |
| 443 | } |
| 444 | |
| 445 | if (py_return == NULL || py_return == Py_None) |
| 446 | { |
| 447 | Py_XDECREF(py_return); |
| 448 | return UINT32_MAX; |
| 449 | } |
| 450 | long retval = PyInt_AsLong(py_return); |
| 451 | Py_DECREF(py_return); |
| 452 | if (retval >= 0) |
| 453 | return (uint32_t)retval; |
| 454 | if (PyErr_Occurred()) |
| 455 | { |
| 456 | PyErr_Print(); |
| 457 | PyErr_Clear(); |
| 458 | } |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | SWIGEXPORT PyObject* |
| 463 | LLDBSwigPython_GetChildAtIndex |
| 464 | ( |
| 465 | PyObject *implementor, |
| 466 | uint32_t idx |
| 467 | ) |
| 468 | { |
| 469 | |
| 470 | static char callee_name[] = "get_child_at_index"; |
| 471 | static char param_format[] = "i"; |
| 472 | |
| 473 | if (implementor == NULL || implementor == Py_None) |
| 474 | return NULL; |
| 475 | PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, idx); |
| 476 | if (PyErr_Occurred()) |
| 477 | { |
| 478 | PyErr_Print(); |
| 479 | PyErr_Clear(); |
| 480 | } |
| 481 | |
| 482 | if (py_return == NULL || py_return == Py_None) |
| 483 | { |
| 484 | Py_XDECREF(py_return); |
| 485 | return NULL; |
| 486 | } |
| 487 | |
| 488 | lldb::SBValue* sbvalue_ptr = NULL; |
| 489 | |
| 490 | if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1) |
| 491 | { |
| 492 | Py_DECREF(py_return); |
| 493 | return NULL; |
| 494 | } |
| 495 | |
| 496 | if (sbvalue_ptr == NULL) |
| 497 | return NULL; |
| 498 | |
| 499 | return py_return; |
| 500 | } |
| 501 | |
| 502 | SWIGEXPORT int |
| 503 | LLDBSwigPython_GetIndexOfChildWithName |
| 504 | ( |
| 505 | PyObject *implementor, |
| 506 | const char* child_name |
| 507 | ) |
| 508 | { |
| 509 | static char callee_name[] = "get_child_index"; |
| 510 | static char param_format[] = "s"; |
| 511 | |
| 512 | if (implementor == NULL || implementor == Py_None) |
| 513 | return 0; |
| 514 | PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, child_name); |
| 515 | if (PyErr_Occurred()) |
| 516 | { |
| 517 | PyErr_Print(); |
| 518 | PyErr_Clear(); |
| 519 | } |
| 520 | |
| 521 | if (py_return == NULL || py_return == Py_None) |
| 522 | { |
| 523 | Py_XDECREF(py_return); |
| 524 | return UINT32_MAX; |
| 525 | } |
| 526 | long retval = PyInt_AsLong(py_return); |
| 527 | Py_DECREF(py_return); |
| 528 | if (retval >= 0) |
| 529 | return (uint32_t)retval; |
| 530 | if (PyErr_Occurred()) |
| 531 | { |
| 532 | PyErr_Print(); |
| 533 | PyErr_Clear(); |
| 534 | } |
| 535 | return 0; |
| 536 | } |
| 537 | |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 538 | SWIGEXPORT bool |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 539 | LLDBSwigPython_UpdateSynthProviderInstance |
| 540 | ( |
| 541 | PyObject *implementor |
| 542 | ) |
| 543 | { |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 544 | |
| 545 | bool ret_val = false; |
| 546 | |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 547 | static char callee_name[] = "update"; |
| 548 | |
| 549 | if (implementor == NULL || implementor == Py_None) |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 550 | return ret_val; |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 551 | |
| 552 | // all this code is here because update is optional, so we don't want to bother trying to call it unless it's been def:ined for us |
| 553 | // other synth provider calls are mandatory, so we want to fail in a very obvious way if they are missing! |
| 554 | PyObject* pmeth = PyObject_GetAttrString(implementor, callee_name); |
| 555 | |
Enrico Granata | 1239c1a | 2011-08-11 19:20:44 +0000 | [diff] [blame] | 556 | if (PyErr_Occurred()) |
| 557 | { |
| 558 | PyErr_Clear(); |
| 559 | } |
| 560 | |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 561 | if (pmeth == NULL || pmeth == Py_None) |
| 562 | { |
| 563 | Py_XDECREF(pmeth); |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 564 | return ret_val; |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | if (PyCallable_Check(pmeth) == 0) |
| 568 | { |
Enrico Granata | 1239c1a | 2011-08-11 19:20:44 +0000 | [diff] [blame] | 569 | if (PyErr_Occurred()) |
| 570 | { |
| 571 | PyErr_Clear(); |
| 572 | } |
| 573 | |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 574 | Py_XDECREF(pmeth); |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 575 | return ret_val; |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Enrico Granata | 1239c1a | 2011-08-11 19:20:44 +0000 | [diff] [blame] | 578 | if (PyErr_Occurred()) |
| 579 | { |
| 580 | PyErr_Clear(); |
| 581 | } |
| 582 | |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 583 | Py_XDECREF(pmeth); |
| 584 | |
| 585 | // right now we know this function exists and is callable.. |
| 586 | PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL); |
| 587 | |
| 588 | // if it fails, print the error but otherwise go on |
| 589 | if (PyErr_Occurred()) |
| 590 | { |
| 591 | PyErr_Print(); |
| 592 | PyErr_Clear(); |
| 593 | } |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 594 | |
| 595 | if (py_return == Py_True) |
| 596 | ret_val = true; |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 597 | |
| 598 | Py_XDECREF(py_return); |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 599 | |
| 600 | return ret_val; |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 601 | |
| 602 | } |
| 603 | |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 604 | SWIGEXPORT void* |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 605 | LLDBSWIGPython_CastPyObjectToSBValue |
| 606 | ( |
| 607 | PyObject* data |
| 608 | ) |
| 609 | { |
| 610 | lldb::SBValue* sb_ptr = NULL; |
| 611 | |
| 612 | int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0); |
| 613 | |
| 614 | if (valid_cast == -1) |
| 615 | return NULL; |
| 616 | |
| 617 | return sb_ptr; |
| 618 | } |
| 619 | |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 620 | // Currently, SBCommandReturnObjectReleaser wraps an std::auto_ptr to an |
| 621 | // lldb_private::CommandReturnObject. This means that the destructor for the |
| 622 | // SB object will deallocate its contained CommandReturnObject. Because that |
| 623 | // object is used as the real return object for Python-based commands, we want |
| 624 | // it to stay around. Thus, we release the auto_ptr before returning from |
| 625 | // LLDBSwigPythonCallCommand, and to guarantee that the release will occur no |
| 626 | // matter how we exit from the function, we have a releaser object whose |
| 627 | // destructor does the right thing for us |
| 628 | class SBCommandReturnObjectReleaser |
| 629 | { |
| 630 | public: |
| 631 | SBCommandReturnObjectReleaser (lldb::SBCommandReturnObject &obj) : |
| 632 | m_command_return_object_ref (obj) |
| 633 | { |
| 634 | } |
| 635 | |
| 636 | ~SBCommandReturnObjectReleaser () |
| 637 | { |
| 638 | m_command_return_object_ref.Release(); |
| 639 | } |
| 640 | private: |
| 641 | lldb::SBCommandReturnObject &m_command_return_object_ref; |
| 642 | }; |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 643 | |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 644 | SWIGEXPORT bool |
| 645 | LLDBSwigPythonCallCommand |
| 646 | ( |
| 647 | const char *python_function_name, |
| 648 | const char *session_dictionary_name, |
| 649 | lldb::DebuggerSP& debugger, |
| 650 | const char* args, |
| 651 | std::string& err_msg, |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 652 | lldb_private::CommandReturnObject& cmd_retobj |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 653 | ) |
| 654 | { |
| 655 | |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 656 | lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj); |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 657 | SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb); |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 658 | lldb::SBDebugger debugger_sb(debugger); |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 659 | |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 660 | bool retval = false; |
| 661 | |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 662 | PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0); |
| 663 | PyObject *CmdRetObj_PyObj = SWIG_NewPointerObj((void *) &cmd_retobj_sb, SWIGTYPE_p_lldb__SBCommandReturnObject, 0); |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 664 | |
| 665 | if (DebuggerObj_PyObj == NULL) |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 666 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 667 | |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 668 | if (CmdRetObj_PyObj == NULL) |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 669 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 670 | |
| 671 | if (!python_function_name || !session_dictionary_name) |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 672 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 673 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 674 | PyObject *session_dict, *pfunc; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 675 | PyObject *pargs, *pvalue; |
| 676 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 677 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 678 | if (session_dict != NULL) |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 679 | { |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 680 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 681 | if (pfunc != NULL) |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 682 | { |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 683 | // Set up the arguments and call the function. |
| 684 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 685 | if (PyCallable_Check (pfunc)) |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 686 | { |
| 687 | pargs = PyTuple_New (4); |
| 688 | if (pargs == NULL) |
| 689 | { |
| 690 | if (PyErr_Occurred()) |
| 691 | PyErr_Clear(); |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 692 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj |
| 696 | PyTuple_SetItem (pargs, 1, PyString_FromString(args)); |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 697 | PyTuple_SetItem (pargs, 2, CmdRetObj_PyObj); // This "steals" a reference to CmdRetObj_PyObj |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 698 | PyTuple_SetItem (pargs, 3, session_dict); // This "steals" a reference to session_dict |
| 699 | pvalue = PyObject_CallObject (pfunc, pargs); |
| 700 | Py_DECREF (pargs); |
| 701 | |
| 702 | if (pvalue != NULL) |
| 703 | { |
| 704 | if (pvalue == Py_None) // no error |
| 705 | { |
| 706 | err_msg.clear(); |
| 707 | retval = true; |
| 708 | } |
Johnny Chen | 2fcf412 | 2011-12-14 20:40:27 +0000 | [diff] [blame] | 709 | else |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 710 | { |
Johnny Chen | 2fcf412 | 2011-12-14 20:40:27 +0000 | [diff] [blame] | 711 | // return value is an error string |
| 712 | if (PyString_CheckExact(pvalue)) |
| 713 | err_msg.assign(PyString_AsString(pvalue)); |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 714 | retval = false; |
| 715 | } |
| 716 | Py_DECREF (pvalue); |
| 717 | } |
| 718 | else if (PyErr_Occurred ()) |
| 719 | { |
| 720 | PyErr_Print(); |
| 721 | PyErr_Clear(); |
| 722 | } |
| 723 | Py_INCREF (session_dict); |
| 724 | } |
| 725 | else if (PyErr_Occurred()) |
| 726 | { |
| 727 | PyErr_Print(); |
| 728 | PyErr_Clear(); |
| 729 | } |
| 730 | } |
| 731 | else if (PyErr_Occurred()) |
| 732 | { |
| 733 | PyErr_Print(); |
| 734 | PyErr_Clear(); |
| 735 | } |
| 736 | } |
| 737 | else if (PyErr_Occurred ()) |
| 738 | { |
| 739 | PyErr_Print(); |
| 740 | PyErr_Clear (); |
| 741 | } |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 742 | return retval; |
| 743 | } |
| 744 | |
| 745 | SWIGEXPORT bool |
| 746 | LLDBSwigPythonCallModuleInit |
| 747 | ( |
| 748 | const std::string python_module_name, |
| 749 | const char *session_dictionary_name, |
| 750 | lldb::DebuggerSP& debugger |
| 751 | ) |
| 752 | { |
| 753 | |
| 754 | lldb::SBDebugger debugger_sb(debugger); |
| 755 | |
| 756 | bool retval = false; |
| 757 | |
| 758 | PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0); |
| 759 | |
| 760 | if (DebuggerObj_PyObj == NULL) |
| 761 | return retval; |
| 762 | |
| 763 | if (!(python_module_name.length()) || !session_dictionary_name) |
| 764 | return retval; |
| 765 | |
| 766 | PyObject *session_dict, *pfunc; |
| 767 | PyObject *pargs, *pvalue; |
| 768 | |
| 769 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 770 | |
| 771 | std::string python_function_name_string = python_module_name + (".__lldb_init_module"); |
| 772 | const char* python_function_name = python_function_name_string.c_str(); |
| 773 | |
| 774 | if (session_dict != NULL) |
| 775 | { |
| 776 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 777 | |
| 778 | if (PyErr_Occurred()) // this might not exist.. let's make sure we handle that |
| 779 | { |
| 780 | PyErr_Clear(); |
| 781 | return true; |
| 782 | } |
| 783 | |
| 784 | if (pfunc == NULL) |
| 785 | return true; |
| 786 | else |
| 787 | { |
| 788 | // Set up the arguments and call the function. |
| 789 | |
| 790 | if (PyCallable_Check (pfunc)) |
| 791 | { |
| 792 | pargs = PyTuple_New (2); |
| 793 | if (pargs == NULL) |
| 794 | { |
| 795 | if (PyErr_Occurred()) |
| 796 | PyErr_Clear(); |
| 797 | return retval; |
| 798 | } |
| 799 | |
| 800 | PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj |
| 801 | PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict |
| 802 | pvalue = PyObject_CallObject (pfunc, pargs); |
| 803 | Py_DECREF (pargs); |
| 804 | |
| 805 | if (PyErr_Occurred ()) |
| 806 | { |
| 807 | PyErr_Print(); |
| 808 | PyErr_Clear(); |
| 809 | } |
| 810 | else |
| 811 | { |
| 812 | retval = true; |
| 813 | Py_XDECREF (pvalue); |
| 814 | } |
| 815 | Py_INCREF (session_dict); |
| 816 | } |
| 817 | else if (PyErr_Occurred()) |
| 818 | { |
| 819 | PyErr_Print(); |
| 820 | PyErr_Clear(); |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 825 | } |
Filipe Cabecinhas | 55ea73d | 2012-08-22 13:25:10 +0000 | [diff] [blame^] | 826 | %} |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 827 | |
Filipe Cabecinhas | 55ea73d | 2012-08-22 13:25:10 +0000 | [diff] [blame^] | 828 | |
| 829 | %runtime %{ |
| 830 | // Forward declaration to be inserted at the start of LLDBWrapPython.h |
| 831 | // I used runtime as a hack to make SWIG place it where it's needed. |
| 832 | // This is needed to use LLDBSwigPythonCallSBInputReaderCallback in the |
| 833 | // typemaps and in the extensions (SBInputReader.__del__()). |
| 834 | #include "SBInputReader.h" |
| 835 | |
| 836 | size_t |
| 837 | LLDBSwigPythonCallSBInputReaderCallback(void *baton, |
| 838 | lldb::SBInputReader *reader, |
| 839 | lldb::InputReaderAction notification, |
| 840 | const char*bytes, |
| 841 | size_t bytes_len); |
| 842 | %} |
| 843 | |
| 844 | %wrapper %{ |
| 845 | // For the InputReader Callback functions |
| 846 | SWIGEXPORT size_t |
| 847 | LLDBSwigPythonCallSBInputReaderCallback(void *baton, |
| 848 | lldb::SBInputReader *reader, |
| 849 | lldb::InputReaderAction notification, |
| 850 | const char*bytes, |
| 851 | size_t bytes_len) { |
| 852 | SWIG_PYTHON_THREAD_BEGIN_BLOCK; |
| 853 | |
| 854 | PyObject *py_InputReader = SWIG_NewPointerObj(reader, SWIGTYPE_p_lldb__SBInputReader, false); |
| 855 | PyObject *py_Notification = PyInt_FromLong(notification); |
| 856 | PyObject *py_Bytes = PyBytes_FromStringAndSize(bytes, bytes_len); |
| 857 | |
| 858 | PyObject *tuple = PyTuple_Pack(3, py_InputReader, py_Notification, py_Bytes); |
| 859 | PyObject *res = PyObject_Call(reinterpret_cast<PyObject*>(baton), tuple, NULL); |
| 860 | Py_DECREF(tuple); |
| 861 | Py_DECREF(py_InputReader); |
| 862 | Py_DECREF(py_Notification); |
| 863 | Py_DECREF(py_Bytes); |
| 864 | |
| 865 | if (res == NULL) { |
| 866 | PyObject *exc = PyErr_Occurred(); |
| 867 | if (exc) { |
| 868 | ::puts("\nErroring out at LLDBSwigPythonCallSBInputReaderCallback"); |
| 869 | PyErr_Print(); |
| 870 | } |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | size_t result = 0; |
| 875 | // If the callback misbehaves and returns Py_None, assume it returned 0 |
| 876 | if (res != Py_None) |
| 877 | result = static_cast<size_t>(PyInt_AsSsize_t(res)); |
| 878 | |
| 879 | Py_DECREF(res); |
| 880 | SWIG_PYTHON_THREAD_END_BLOCK; |
| 881 | return result; |
| 882 | } |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 883 | %} |