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 | { |
| 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 | |
| 104 | static PyObject* |
| 105 | FindSessionDictionary(const char *session_dictionary_name) |
| 106 | { |
| 107 | return ResolvePythonName(session_dictionary_name, NULL); |
| 108 | } |
| 109 | |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 110 | // 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 | |
| 113 | SWIGEXPORT bool |
| 114 | LLDBSwigPythonBreakpointCallbackFunction |
| 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 Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 135 | PyObject *session_dict, *pfunc; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 136 | PyObject *pargs, *pvalue; |
| 137 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 138 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 139 | if (session_dict != NULL) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 140 | { |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 141 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 142 | if (pfunc != NULL) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 143 | { |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 144 | // Set up the arguments and call the function. |
| 145 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 146 | if (PyCallable_Check (pfunc)) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 147 | { |
| 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 | |
| 189 | SWIGEXPORT std::string |
| 190 | LLDBSwigPythonCallTypeScript |
| 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 Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 201 | 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] | 202 | |
| 203 | if (ValObj_PyObj == NULL) |
| 204 | return retval; |
| 205 | |
| 206 | if (!python_function_name || !session_dictionary_name) |
| 207 | return retval; |
| 208 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 209 | PyObject *session_dict, *pfunc; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 210 | PyObject *pargs, *pvalue; |
| 211 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 212 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 213 | if (session_dict != NULL) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 214 | { |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 215 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 216 | if (pfunc != NULL) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 217 | { |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 218 | // Set up the arguments and call the function. |
| 219 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 220 | if (PyCallable_Check (pfunc)) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 221 | { |
| 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 | { |
| 237 | if (pvalue != Py_None) |
| 238 | 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 | |
| 270 | SWIGEXPORT void* |
| 271 | LLDBSwigPythonCreateSyntheticProvider |
| 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 Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 283 | // 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 Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 286 | |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 287 | PyObject *ValObj_PyObj = SWIG_NewPointerObj((void *)valobj_sb, SWIGTYPE_p_lldb__SBValue, SWIG_POINTER_OWN); |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 288 | |
| 289 | if (ValObj_PyObj == NULL) |
| 290 | Py_RETURN_NONE; |
| 291 | |
| 292 | const char* python_function_name = python_class_name.c_str(); |
| 293 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 294 | PyObject *session_dict, *pfunc; |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 295 | PyObject *pvalue; |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 296 | |
| 297 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 298 | if (session_dict != NULL) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 299 | { |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 300 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 301 | if (pfunc != NULL) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 302 | { |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 303 | // Set up the arguments and call the function. |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 304 | |
| 305 | if (PyCallable_Check (pfunc)) |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 306 | { |
| 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 | /* |
| 368 | these four calls below are meant to support |
| 369 | Python-based synthetic children providers |
| 370 | they essentially mimic the four pure virtual |
| 371 | method calls provided by the frontend class |
| 372 | */ |
| 373 | |
| 374 | SWIGEXPORT uint32_t |
| 375 | LLDBSwigPython_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 | |
| 409 | SWIGEXPORT PyObject* |
| 410 | LLDBSwigPython_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 | |
| 449 | SWIGEXPORT int |
| 450 | LLDBSwigPython_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 Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 485 | SWIGEXPORT void |
| 486 | LLDBSwigPython_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 Granata | 1239c1a | 2011-08-11 19:20:44 +0000 | [diff] [blame] | 500 | if (PyErr_Occurred()) |
| 501 | { |
| 502 | PyErr_Clear(); |
| 503 | } |
| 504 | |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 505 | if (pmeth == NULL || pmeth == Py_None) |
| 506 | { |
| 507 | Py_XDECREF(pmeth); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | if (PyCallable_Check(pmeth) == 0) |
| 512 | { |
Enrico Granata | 1239c1a | 2011-08-11 19:20:44 +0000 | [diff] [blame] | 513 | if (PyErr_Occurred()) |
| 514 | { |
| 515 | PyErr_Clear(); |
| 516 | } |
| 517 | |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 518 | Py_XDECREF(pmeth); |
| 519 | return; |
| 520 | } |
| 521 | |
Enrico Granata | 1239c1a | 2011-08-11 19:20:44 +0000 | [diff] [blame] | 522 | if (PyErr_Occurred()) |
| 523 | { |
| 524 | PyErr_Clear(); |
| 525 | } |
| 526 | |
Enrico Granata | 6a319e4 | 2011-07-26 21:02:56 +0000 | [diff] [blame] | 527 | 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 Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 543 | SWIGEXPORT void* |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 544 | LLDBSWIGPython_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 Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 559 | // 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 |
| 567 | class SBCommandReturnObjectReleaser |
| 568 | { |
| 569 | public: |
| 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 | } |
| 579 | private: |
| 580 | lldb::SBCommandReturnObject &m_command_return_object_ref; |
| 581 | }; |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 582 | |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 583 | SWIGEXPORT bool |
| 584 | LLDBSwigPythonCallCommand |
| 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 Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 591 | lldb_private::CommandReturnObject& cmd_retobj |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 592 | ) |
| 593 | { |
| 594 | |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 595 | lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj); |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 596 | SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb); |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 597 | lldb::SBDebugger debugger_sb(debugger); |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 598 | |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 599 | bool retval = false; |
| 600 | |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 601 | 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 Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 603 | |
| 604 | if (DebuggerObj_PyObj == NULL) |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 605 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 606 | |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 607 | if (CmdRetObj_PyObj == NULL) |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 608 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 609 | |
| 610 | if (!python_function_name || !session_dictionary_name) |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 611 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 612 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 613 | PyObject *session_dict, *pfunc; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 614 | PyObject *pargs, *pvalue; |
| 615 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 616 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 617 | if (session_dict != NULL) |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 618 | { |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 619 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 620 | if (pfunc != NULL) |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 621 | { |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 622 | // Set up the arguments and call the function. |
| 623 | |
Enrico Granata | 960691f | 2011-08-22 17:34:47 +0000 | [diff] [blame] | 624 | if (PyCallable_Check (pfunc)) |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 625 | { |
| 626 | pargs = PyTuple_New (4); |
| 627 | if (pargs == NULL) |
| 628 | { |
| 629 | if (PyErr_Occurred()) |
| 630 | PyErr_Clear(); |
Enrico Granata | 872959b | 2011-08-20 00:26:17 +0000 | [diff] [blame] | 631 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj |
| 635 | PyTuple_SetItem (pargs, 1, PyString_FromString(args)); |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 636 | 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] | 637 | 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 | } |
| 648 | else // return value is an error string |
| 649 | { |
| 650 | err_msg.assign(PyString_AsString(pvalue)); |
| 651 | retval = false; |
| 652 | } |
| 653 | Py_DECREF (pvalue); |
| 654 | } |
| 655 | else if (PyErr_Occurred ()) |
| 656 | { |
| 657 | PyErr_Print(); |
| 658 | PyErr_Clear(); |
| 659 | } |
| 660 | Py_INCREF (session_dict); |
| 661 | } |
| 662 | else if (PyErr_Occurred()) |
| 663 | { |
| 664 | PyErr_Print(); |
| 665 | PyErr_Clear(); |
| 666 | } |
| 667 | } |
| 668 | else if (PyErr_Occurred()) |
| 669 | { |
| 670 | PyErr_Print(); |
| 671 | PyErr_Clear(); |
| 672 | } |
| 673 | } |
| 674 | else if (PyErr_Occurred ()) |
| 675 | { |
| 676 | PyErr_Print(); |
| 677 | PyErr_Clear (); |
| 678 | } |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame^] | 679 | return retval; |
| 680 | } |
| 681 | |
| 682 | SWIGEXPORT bool |
| 683 | LLDBSwigPythonCallModuleInit |
| 684 | ( |
| 685 | const std::string python_module_name, |
| 686 | const char *session_dictionary_name, |
| 687 | lldb::DebuggerSP& debugger |
| 688 | ) |
| 689 | { |
| 690 | |
| 691 | lldb::SBDebugger debugger_sb(debugger); |
| 692 | |
| 693 | bool retval = false; |
| 694 | |
| 695 | PyObject *DebuggerObj_PyObj = SWIG_NewPointerObj((void *) &debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0); |
| 696 | |
| 697 | if (DebuggerObj_PyObj == NULL) |
| 698 | return retval; |
| 699 | |
| 700 | if (!(python_module_name.length()) || !session_dictionary_name) |
| 701 | return retval; |
| 702 | |
| 703 | PyObject *session_dict, *pfunc; |
| 704 | PyObject *pargs, *pvalue; |
| 705 | |
| 706 | session_dict = FindSessionDictionary (session_dictionary_name); |
| 707 | |
| 708 | std::string python_function_name_string = python_module_name + (".__lldb_init_module"); |
| 709 | const char* python_function_name = python_function_name_string.c_str(); |
| 710 | |
| 711 | if (session_dict != NULL) |
| 712 | { |
| 713 | pfunc = ResolvePythonName (python_function_name, session_dict); |
| 714 | |
| 715 | if (PyErr_Occurred()) // this might not exist.. let's make sure we handle that |
| 716 | { |
| 717 | PyErr_Clear(); |
| 718 | return true; |
| 719 | } |
| 720 | |
| 721 | if (pfunc == NULL) |
| 722 | return true; |
| 723 | else |
| 724 | { |
| 725 | // Set up the arguments and call the function. |
| 726 | |
| 727 | if (PyCallable_Check (pfunc)) |
| 728 | { |
| 729 | pargs = PyTuple_New (2); |
| 730 | if (pargs == NULL) |
| 731 | { |
| 732 | if (PyErr_Occurred()) |
| 733 | PyErr_Clear(); |
| 734 | return retval; |
| 735 | } |
| 736 | |
| 737 | PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj); // This "steals" a reference to DebuggerObj_PyObj |
| 738 | PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict |
| 739 | pvalue = PyObject_CallObject (pfunc, pargs); |
| 740 | Py_DECREF (pargs); |
| 741 | |
| 742 | if (PyErr_Occurred ()) |
| 743 | { |
| 744 | PyErr_Print(); |
| 745 | PyErr_Clear(); |
| 746 | } |
| 747 | else |
| 748 | { |
| 749 | retval = true; |
| 750 | Py_XDECREF (pvalue); |
| 751 | } |
| 752 | Py_INCREF (session_dict); |
| 753 | } |
| 754 | else if (PyErr_Occurred()) |
| 755 | { |
| 756 | PyErr_Print(); |
| 757 | PyErr_Clear(); |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | return retval; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Johnny Chen | f307cf7 | 2011-07-26 19:09:03 +0000 | [diff] [blame] | 764 | %} |