1061da546Spatrick%header %{ 2061da546Spatrick 3*f6aab3d8Srobertclass PyErr_Cleaner { 4061da546Spatrickpublic: 5*f6aab3d8Srobert PyErr_Cleaner(bool print = false) : m_print(print) {} 6061da546Spatrick 7*f6aab3d8Srobert ~PyErr_Cleaner() { 8*f6aab3d8Srobert if (PyErr_Occurred()) { 9061da546Spatrick if (m_print && !PyErr_ExceptionMatches(PyExc_SystemExit)) 10061da546Spatrick PyErr_Print(); 11061da546Spatrick PyErr_Clear(); 12061da546Spatrick } 13061da546Spatrick } 14061da546Spatrick 15061da546Spatrickprivate: 16061da546Spatrick bool m_print; 17061da546Spatrick}; 18061da546Spatrick 19*f6aab3d8Srobertllvm::Expected<bool> lldb_private::LLDBSwigPythonBreakpointCallbackFunction( 20*f6aab3d8Srobert const char *python_function_name, const char *session_dictionary_name, 21061da546Spatrick const lldb::StackFrameSP &frame_sp, 22061da546Spatrick const lldb::BreakpointLocationSP &bp_loc_sp, 23*f6aab3d8Srobert const lldb_private::StructuredDataImpl &args_impl) { 24061da546Spatrick using namespace llvm; 25061da546Spatrick 26061da546Spatrick lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp); 27061da546Spatrick 28061da546Spatrick PyErr_Cleaner py_err_cleaner(true); 29*f6aab3d8Srobert auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( 30*f6aab3d8Srobert session_dictionary_name); 31*f6aab3d8Srobert auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 32*f6aab3d8Srobert python_function_name, dict); 33061da546Spatrick 34061da546Spatrick unsigned max_positional_args; 35061da546Spatrick if (auto arg_info = pfunc.GetArgInfo()) 36061da546Spatrick max_positional_args = arg_info.get().max_positional_args; 37061da546Spatrick else 38061da546Spatrick return arg_info.takeError(); 39061da546Spatrick 40*f6aab3d8Srobert PythonObject frame_arg = ToSWIGWrapper(frame_sp); 41*f6aab3d8Srobert PythonObject bp_loc_arg = ToSWIGWrapper(bp_loc_sp); 42061da546Spatrick 43*f6aab3d8Srobert auto result = 44*f6aab3d8Srobert max_positional_args < 4 45*f6aab3d8Srobert ? pfunc.Call(frame_arg, bp_loc_arg, dict) 46*f6aab3d8Srobert : pfunc.Call(frame_arg, bp_loc_arg, ToSWIGWrapper(args_impl), dict); 47061da546Spatrick 48061da546Spatrick if (!result) 49061da546Spatrick return result.takeError(); 50061da546Spatrick 51061da546Spatrick // Only False counts as false! 52061da546Spatrick return result.get().get() != Py_False; 53061da546Spatrick} 54061da546Spatrick 55*f6aab3d8Srobert// resolve a dotted Python name in the form 56*f6aab3d8Srobert// foo.bar.baz.Foobar to an actual Python object 57*f6aab3d8Srobert// if pmodule is NULL, the __main__ module will be used 58*f6aab3d8Srobert// as the starting point for the search 59be691f3bSpatrick 60*f6aab3d8Srobert// This function is called by 61*f6aab3d8Srobert// lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...) and is 62*f6aab3d8Srobert// used when a script command is attached to a breakpoint for execution. 63be691f3bSpatrick 64*f6aab3d8Srobert// This function is called by 65*f6aab3d8Srobert// lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...) and is 66*f6aab3d8Srobert// used when a script command is attached to a watchpoint for execution. 67061da546Spatrick 68*f6aab3d8Srobertbool lldb_private::LLDBSwigPythonWatchpointCallbackFunction( 69*f6aab3d8Srobert const char *python_function_name, const char *session_dictionary_name, 70*f6aab3d8Srobert const lldb::StackFrameSP &frame_sp, const lldb::WatchpointSP &wp_sp) { 71061da546Spatrick 72061da546Spatrick bool stop_at_watchpoint = true; 73061da546Spatrick 74061da546Spatrick PyErr_Cleaner py_err_cleaner(true); 75061da546Spatrick 76*f6aab3d8Srobert auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( 77*f6aab3d8Srobert session_dictionary_name); 78*f6aab3d8Srobert auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 79*f6aab3d8Srobert python_function_name, dict); 80061da546Spatrick 81061da546Spatrick if (!pfunc.IsAllocated()) 82061da546Spatrick return stop_at_watchpoint; 83061da546Spatrick 84*f6aab3d8Srobert PythonObject result = 85*f6aab3d8Srobert pfunc(ToSWIGWrapper(frame_sp), ToSWIGWrapper(wp_sp), dict); 86061da546Spatrick 87061da546Spatrick if (result.get() == Py_False) 88061da546Spatrick stop_at_watchpoint = false; 89061da546Spatrick 90061da546Spatrick return stop_at_watchpoint; 91061da546Spatrick} 92061da546Spatrick 93*f6aab3d8Srobert// This function is called by 94*f6aab3d8Srobert// ScriptInterpreterPython::FormatterMatchingCallbackFunction and it's used when 95*f6aab3d8Srobert// a data formatter provides the name of a callback to inspect a candidate type 96*f6aab3d8Srobert// before considering a match. 97*f6aab3d8Srobertbool lldb_private::LLDBSwigPythonFormatterCallbackFunction( 98*f6aab3d8Srobert const char *python_function_name, const char *session_dictionary_name, 99*f6aab3d8Srobert lldb::TypeImplSP type_impl_sp) { 100*f6aab3d8Srobert 101*f6aab3d8Srobert PyErr_Cleaner py_err_cleaner(true); 102*f6aab3d8Srobert 103*f6aab3d8Srobert auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( 104*f6aab3d8Srobert session_dictionary_name); 105*f6aab3d8Srobert auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 106*f6aab3d8Srobert python_function_name, dict); 107*f6aab3d8Srobert 108*f6aab3d8Srobert if (!pfunc.IsAllocated()) 109*f6aab3d8Srobert return false; 110*f6aab3d8Srobert 111*f6aab3d8Srobert PythonObject result = 112*f6aab3d8Srobert pfunc(ToSWIGWrapper(type_impl_sp), dict); 113*f6aab3d8Srobert 114*f6aab3d8Srobert // Only if everything goes okay and the function returns True we'll consider 115*f6aab3d8Srobert // it a match. 116*f6aab3d8Srobert return result.get() == Py_True; 117*f6aab3d8Srobert} 118*f6aab3d8Srobert 119*f6aab3d8Srobertbool lldb_private::LLDBSwigPythonCallTypeScript( 120*f6aab3d8Srobert const char *python_function_name, const void *session_dictionary, 121*f6aab3d8Srobert const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper, 122*f6aab3d8Srobert const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval) { 123061da546Spatrick 124061da546Spatrick retval.clear(); 125061da546Spatrick 126061da546Spatrick if (!python_function_name || !session_dictionary) 127061da546Spatrick return false; 128061da546Spatrick 129061da546Spatrick PyObject *pfunc_impl = nullptr; 130061da546Spatrick 131*f6aab3d8Srobert if (pyfunct_wrapper && *pyfunct_wrapper && 132*f6aab3d8Srobert PyFunction_Check(*pyfunct_wrapper)) { 133061da546Spatrick pfunc_impl = (PyObject *)(*pyfunct_wrapper); 134*f6aab3d8Srobert if (pfunc_impl->ob_refcnt == 1) { 135061da546Spatrick Py_XDECREF(pfunc_impl); 136061da546Spatrick pfunc_impl = NULL; 137061da546Spatrick } 138061da546Spatrick } 139061da546Spatrick 140061da546Spatrick PyObject *py_dict = (PyObject *)session_dictionary; 141061da546Spatrick if (!PythonDictionary::Check(py_dict)) 142061da546Spatrick return true; 143061da546Spatrick 144061da546Spatrick PythonDictionary dict(PyRefType::Borrowed, py_dict); 145061da546Spatrick 146061da546Spatrick PyErr_Cleaner pyerr_cleanup(true); // show Python errors 147061da546Spatrick 148061da546Spatrick PythonCallable pfunc(PyRefType::Borrowed, pfunc_impl); 149061da546Spatrick 150*f6aab3d8Srobert if (!pfunc.IsAllocated()) { 151*f6aab3d8Srobert pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 152*f6aab3d8Srobert python_function_name, dict); 153061da546Spatrick if (!pfunc.IsAllocated()) 154061da546Spatrick return false; 155061da546Spatrick 156*f6aab3d8Srobert if (pyfunct_wrapper) { 157061da546Spatrick *pyfunct_wrapper = pfunc.get(); 158061da546Spatrick Py_XINCREF(pfunc.get()); 159061da546Spatrick } 160061da546Spatrick } 161061da546Spatrick 162061da546Spatrick PythonObject result; 163061da546Spatrick auto argc = pfunc.GetArgInfo(); 164061da546Spatrick if (!argc) { 165061da546Spatrick llvm::consumeError(argc.takeError()); 166061da546Spatrick return false; 167061da546Spatrick } 168061da546Spatrick 169*f6aab3d8Srobert PythonObject value_arg = ToSWIGWrapper(valobj_sp); 170061da546Spatrick 171061da546Spatrick if (argc.get().max_positional_args < 3) 172061da546Spatrick result = pfunc(value_arg, dict); 173061da546Spatrick else 174*f6aab3d8Srobert result = pfunc(value_arg, dict, ToSWIGWrapper(*options_sp)); 175061da546Spatrick 176061da546Spatrick retval = result.Str().GetString().str(); 177061da546Spatrick 178061da546Spatrick return true; 179061da546Spatrick} 180061da546Spatrick 181*f6aab3d8SrobertPythonObject lldb_private::LLDBSwigPythonCreateSyntheticProvider( 182*f6aab3d8Srobert const char *python_class_name, const char *session_dictionary_name, 183*f6aab3d8Srobert const lldb::ValueObjectSP &valobj_sp) { 184*f6aab3d8Srobert if (python_class_name == NULL || python_class_name[0] == '\0' || 185*f6aab3d8Srobert !session_dictionary_name) 186*f6aab3d8Srobert return PythonObject(); 187061da546Spatrick 188061da546Spatrick PyErr_Cleaner py_err_cleaner(true); 189061da546Spatrick 190*f6aab3d8Srobert auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( 191*f6aab3d8Srobert session_dictionary_name); 192*f6aab3d8Srobert auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 193*f6aab3d8Srobert python_class_name, dict); 194061da546Spatrick 195061da546Spatrick if (!pfunc.IsAllocated()) 196*f6aab3d8Srobert return PythonObject(); 197061da546Spatrick 198*f6aab3d8Srobert auto sb_value = std::make_unique<lldb::SBValue>(valobj_sp); 199061da546Spatrick sb_value->SetPreferSyntheticValue(false); 200061da546Spatrick 201*f6aab3d8Srobert PythonObject val_arg = ToSWIGWrapper(std::move(sb_value)); 202061da546Spatrick if (!val_arg.IsAllocated()) 203*f6aab3d8Srobert return PythonObject(); 204061da546Spatrick 205061da546Spatrick PythonObject result = pfunc(val_arg, dict); 206061da546Spatrick 207061da546Spatrick if (result.IsAllocated()) 208*f6aab3d8Srobert return result; 209061da546Spatrick 210*f6aab3d8Srobert return PythonObject(); 211061da546Spatrick} 212061da546Spatrick 213*f6aab3d8SrobertPythonObject lldb_private::LLDBSwigPythonCreateCommandObject( 214*f6aab3d8Srobert const char *python_class_name, const char *session_dictionary_name, 215*f6aab3d8Srobert lldb::DebuggerSP debugger_sp) { 216*f6aab3d8Srobert if (python_class_name == NULL || python_class_name[0] == '\0' || 217*f6aab3d8Srobert !session_dictionary_name) 218*f6aab3d8Srobert return PythonObject(); 219061da546Spatrick 220061da546Spatrick PyErr_Cleaner py_err_cleaner(true); 221*f6aab3d8Srobert auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( 222*f6aab3d8Srobert session_dictionary_name); 223*f6aab3d8Srobert auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 224*f6aab3d8Srobert python_class_name, dict); 225061da546Spatrick 226061da546Spatrick if (!pfunc.IsAllocated()) 227*f6aab3d8Srobert return PythonObject(); 228061da546Spatrick 229*f6aab3d8Srobert return pfunc(ToSWIGWrapper(std::move(debugger_sp)), dict); 230061da546Spatrick} 231061da546Spatrick 232*f6aab3d8SrobertPythonObject lldb_private::LLDBSwigPythonCreateScriptedObject( 233*f6aab3d8Srobert const char *python_class_name, const char *session_dictionary_name, 234*f6aab3d8Srobert lldb::ExecutionContextRefSP exe_ctx_sp, 235*f6aab3d8Srobert const lldb_private::StructuredDataImpl &args_impl, 236*f6aab3d8Srobert std::string &error_string) { 237*f6aab3d8Srobert if (python_class_name == NULL || python_class_name[0] == '\0' || 238*f6aab3d8Srobert !session_dictionary_name) 239*f6aab3d8Srobert return PythonObject(); 240061da546Spatrick 241061da546Spatrick PyErr_Cleaner py_err_cleaner(true); 242061da546Spatrick 243*f6aab3d8Srobert auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( 244*f6aab3d8Srobert session_dictionary_name); 245*f6aab3d8Srobert auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 246*f6aab3d8Srobert python_class_name, dict); 247061da546Spatrick 248061da546Spatrick if (!pfunc.IsAllocated()) { 249061da546Spatrick error_string.append("could not find script class: "); 250061da546Spatrick error_string.append(python_class_name); 251*f6aab3d8Srobert return PythonObject(); 252061da546Spatrick } 253061da546Spatrick 254be691f3bSpatrick llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo(); 255be691f3bSpatrick if (!arg_info) { 256be691f3bSpatrick llvm::handleAllErrors( 257be691f3bSpatrick arg_info.takeError(), 258*f6aab3d8Srobert [&](PythonException &E) { error_string.append(E.ReadBacktrace()); }, 259be691f3bSpatrick [&](const llvm::ErrorInfoBase &E) { 260be691f3bSpatrick error_string.append(E.message()); 261be691f3bSpatrick }); 262*f6aab3d8Srobert return PythonObject(); 263be691f3bSpatrick } 264be691f3bSpatrick 265be691f3bSpatrick PythonObject result = {}; 266be691f3bSpatrick if (arg_info.get().max_positional_args == 2) { 267*f6aab3d8Srobert result = pfunc(ToSWIGWrapper(exe_ctx_sp), ToSWIGWrapper(args_impl)); 268be691f3bSpatrick } else { 269*f6aab3d8Srobert error_string.assign("wrong number of arguments in __init__, should be 2 " 270*f6aab3d8Srobert "(not including self)"); 271*f6aab3d8Srobert } 272*f6aab3d8Srobert return result; 273be691f3bSpatrick} 274be691f3bSpatrick 275*f6aab3d8SrobertPythonObject lldb_private::LLDBSwigPythonCreateScriptedThreadPlan( 276*f6aab3d8Srobert const char *python_class_name, const char *session_dictionary_name, 277*f6aab3d8Srobert const lldb_private::StructuredDataImpl &args_impl, 278*f6aab3d8Srobert std::string &error_string, const lldb::ThreadPlanSP &thread_plan_sp) { 279*f6aab3d8Srobert if (python_class_name == NULL || python_class_name[0] == '\0' || 280*f6aab3d8Srobert !session_dictionary_name) 281*f6aab3d8Srobert return PythonObject(); 282be691f3bSpatrick 283be691f3bSpatrick PyErr_Cleaner py_err_cleaner(true); 284be691f3bSpatrick 285*f6aab3d8Srobert auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( 286*f6aab3d8Srobert session_dictionary_name); 287*f6aab3d8Srobert auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 288*f6aab3d8Srobert python_class_name, dict); 289be691f3bSpatrick 290be691f3bSpatrick if (!pfunc.IsAllocated()) { 291be691f3bSpatrick error_string.append("could not find script class: "); 292be691f3bSpatrick error_string.append(python_class_name); 293*f6aab3d8Srobert return PythonObject(); 294be691f3bSpatrick } 295be691f3bSpatrick 296*f6aab3d8Srobert PythonObject tp_arg = ToSWIGWrapper(thread_plan_sp); 297061da546Spatrick 298061da546Spatrick llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo(); 299061da546Spatrick if (!arg_info) { 300061da546Spatrick llvm::handleAllErrors( 301061da546Spatrick arg_info.takeError(), 302*f6aab3d8Srobert [&](PythonException &E) { error_string.append(E.ReadBacktrace()); }, 303061da546Spatrick [&](const llvm::ErrorInfoBase &E) { 304061da546Spatrick error_string.append(E.message()); 305061da546Spatrick }); 306*f6aab3d8Srobert return PythonObject(); 307061da546Spatrick } 308061da546Spatrick 309061da546Spatrick PythonObject result = {}; 310*f6aab3d8Srobert auto args_sb = std::make_unique<lldb::SBStructuredData>(args_impl); 311061da546Spatrick if (arg_info.get().max_positional_args == 2) { 312*f6aab3d8Srobert if (args_sb->IsValid()) { 313*f6aab3d8Srobert error_string.assign( 314*f6aab3d8Srobert "args passed, but __init__ does not take an args dictionary"); 315*f6aab3d8Srobert return PythonObject(); 316061da546Spatrick } 317061da546Spatrick result = pfunc(tp_arg, dict); 318061da546Spatrick } else if (arg_info.get().max_positional_args >= 3) { 319*f6aab3d8Srobert result = pfunc(tp_arg, ToSWIGWrapper(std::move(args_sb)), dict); 320061da546Spatrick } else { 321*f6aab3d8Srobert error_string.assign("wrong number of arguments in __init__, should be 2 or " 322*f6aab3d8Srobert "3 (not including self)"); 323*f6aab3d8Srobert return PythonObject(); 324061da546Spatrick } 325061da546Spatrick 326*f6aab3d8Srobert // FIXME: At this point we should check that the class we found supports all 327*f6aab3d8Srobert // the methods that we need. 328061da546Spatrick 329*f6aab3d8Srobert return result; 330061da546Spatrick} 331061da546Spatrick 332*f6aab3d8Srobertbool lldb_private::LLDBSWIGPythonCallThreadPlan( 333*f6aab3d8Srobert void *implementor, const char *method_name, lldb_private::Event *event, 334*f6aab3d8Srobert bool &got_error) { 335061da546Spatrick got_error = false; 336061da546Spatrick 337061da546Spatrick PyErr_Cleaner py_err_cleaner(false); 338061da546Spatrick PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor)); 339061da546Spatrick auto pfunc = self.ResolveName<PythonCallable>(method_name); 340061da546Spatrick 341061da546Spatrick if (!pfunc.IsAllocated()) 342061da546Spatrick return false; 343061da546Spatrick 344061da546Spatrick PythonObject result; 345*f6aab3d8Srobert if (event != nullptr) { 346*f6aab3d8Srobert ScopedPythonObject<SBEvent> event_arg = ToSWIGWrapper(event); 347*f6aab3d8Srobert result = pfunc(event_arg.obj()); 348*f6aab3d8Srobert } else 349061da546Spatrick result = pfunc(); 350061da546Spatrick 351*f6aab3d8Srobert if (PyErr_Occurred()) { 352061da546Spatrick got_error = true; 353*f6aab3d8Srobert printf("Return value was neither false nor true for call to %s.\n", 354*f6aab3d8Srobert method_name); 355061da546Spatrick PyErr_Print(); 356061da546Spatrick return false; 357061da546Spatrick } 358061da546Spatrick 359061da546Spatrick if (result.get() == Py_True) 360061da546Spatrick return true; 361061da546Spatrick else if (result.get() == Py_False) 362061da546Spatrick return false; 363061da546Spatrick 364061da546Spatrick // Somebody returned the wrong thing... 365061da546Spatrick got_error = true; 366061da546Spatrick printf("Wrong return value type for call to %s.\n", method_name); 367061da546Spatrick return false; 368061da546Spatrick} 369061da546Spatrick 370*f6aab3d8SrobertPythonObject lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver( 371*f6aab3d8Srobert const char *python_class_name, const char *session_dictionary_name, 372*f6aab3d8Srobert const StructuredDataImpl &args_impl, 373*f6aab3d8Srobert const lldb::BreakpointSP &breakpoint_sp) { 374*f6aab3d8Srobert 375*f6aab3d8Srobert if (python_class_name == NULL || python_class_name[0] == '\0' || 376*f6aab3d8Srobert !session_dictionary_name) 377*f6aab3d8Srobert return PythonObject(); 378061da546Spatrick 379061da546Spatrick PyErr_Cleaner py_err_cleaner(true); 380061da546Spatrick 381*f6aab3d8Srobert auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( 382*f6aab3d8Srobert session_dictionary_name); 383*f6aab3d8Srobert auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 384*f6aab3d8Srobert python_class_name, dict); 385061da546Spatrick 386061da546Spatrick if (!pfunc.IsAllocated()) 387*f6aab3d8Srobert return PythonObject(); 388061da546Spatrick 389*f6aab3d8Srobert PythonObject result = 390*f6aab3d8Srobert pfunc(ToSWIGWrapper(breakpoint_sp), ToSWIGWrapper(args_impl), dict); 391*f6aab3d8Srobert // FIXME: At this point we should check that the class we found supports all 392*f6aab3d8Srobert // the methods that we need. 393061da546Spatrick 394*f6aab3d8Srobert if (result.IsAllocated()) { 395061da546Spatrick // Check that __callback__ is defined: 396061da546Spatrick auto callback_func = result.ResolveName<PythonCallable>("__callback__"); 397061da546Spatrick if (callback_func.IsAllocated()) 398*f6aab3d8Srobert return result; 399061da546Spatrick } 400*f6aab3d8Srobert return PythonObject(); 401061da546Spatrick} 402061da546Spatrick 403*f6aab3d8Srobertunsigned int lldb_private::LLDBSwigPythonCallBreakpointResolver( 404*f6aab3d8Srobert void *implementor, const char *method_name, 405*f6aab3d8Srobert lldb_private::SymbolContext *sym_ctx) { 406061da546Spatrick PyErr_Cleaner py_err_cleaner(false); 407061da546Spatrick PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor)); 408061da546Spatrick auto pfunc = self.ResolveName<PythonCallable>(method_name); 409061da546Spatrick 410061da546Spatrick if (!pfunc.IsAllocated()) 411061da546Spatrick return 0; 412061da546Spatrick 413*f6aab3d8Srobert PythonObject result = sym_ctx ? pfunc(ToSWIGWrapper(*sym_ctx)) : pfunc(); 414061da546Spatrick 415*f6aab3d8Srobert if (PyErr_Occurred()) { 416061da546Spatrick PyErr_Print(); 417dda28197Spatrick PyErr_Clear(); 418061da546Spatrick return 0; 419061da546Spatrick } 420061da546Spatrick 421061da546Spatrick // The callback will return a bool, but we're need to also return ints 422061da546Spatrick // so we're squirrelling the bool through as an int... And if you return 423061da546Spatrick // nothing, we'll continue. 424061da546Spatrick if (strcmp(method_name, "__callback__") == 0) { 425061da546Spatrick if (result.get() == Py_False) 426061da546Spatrick return 0; 427061da546Spatrick else 428061da546Spatrick return 1; 429061da546Spatrick } 430061da546Spatrick 431dda28197Spatrick long long ret_val = unwrapOrSetPythonException(As<long long>(result)); 432061da546Spatrick 433dda28197Spatrick if (PyErr_Occurred()) { 434dda28197Spatrick PyErr_Print(); 435dda28197Spatrick PyErr_Clear(); 436dda28197Spatrick return 0; 437dda28197Spatrick } 438061da546Spatrick 439061da546Spatrick return ret_val; 440061da546Spatrick} 441061da546Spatrick 442*f6aab3d8SrobertPythonObject lldb_private::LLDBSwigPythonCreateScriptedStopHook( 443*f6aab3d8Srobert lldb::TargetSP target_sp, const char *python_class_name, 444*f6aab3d8Srobert const char *session_dictionary_name, const StructuredDataImpl &args_impl, 445*f6aab3d8Srobert Status &error) { 446be691f3bSpatrick if (python_class_name == NULL || python_class_name[0] == '\0') { 447be691f3bSpatrick error.SetErrorString("Empty class name."); 448*f6aab3d8Srobert return PythonObject(); 449be691f3bSpatrick } 450be691f3bSpatrick if (!session_dictionary_name) { 451be691f3bSpatrick error.SetErrorString("No session dictionary"); 452*f6aab3d8Srobert return PythonObject(); 453be691f3bSpatrick } 454be691f3bSpatrick 455be691f3bSpatrick PyErr_Cleaner py_err_cleaner(true); 456be691f3bSpatrick 457*f6aab3d8Srobert auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( 458be691f3bSpatrick session_dictionary_name); 459*f6aab3d8Srobert auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 460be691f3bSpatrick python_class_name, dict); 461be691f3bSpatrick 462be691f3bSpatrick if (!pfunc.IsAllocated()) { 463be691f3bSpatrick error.SetErrorStringWithFormat("Could not find class: %s.", 464be691f3bSpatrick python_class_name); 465*f6aab3d8Srobert return PythonObject(); 466be691f3bSpatrick } 467be691f3bSpatrick 468*f6aab3d8Srobert PythonObject result = 469*f6aab3d8Srobert pfunc(ToSWIGWrapper(target_sp), ToSWIGWrapper(args_impl), dict); 470be691f3bSpatrick 471*f6aab3d8Srobert if (result.IsAllocated()) { 472be691f3bSpatrick // Check that the handle_stop callback is defined: 473be691f3bSpatrick auto callback_func = result.ResolveName<PythonCallable>("handle_stop"); 474be691f3bSpatrick if (callback_func.IsAllocated()) { 475be691f3bSpatrick if (auto args_info = callback_func.GetArgInfo()) { 476be691f3bSpatrick size_t num_args = (*args_info).max_positional_args; 477be691f3bSpatrick if (num_args != 2) { 478*f6aab3d8Srobert error.SetErrorStringWithFormat( 479*f6aab3d8Srobert "Wrong number of args for " 480be691f3bSpatrick "handle_stop callback, should be 2 (excluding self), got: %zu", 481be691f3bSpatrick num_args); 482*f6aab3d8Srobert return PythonObject(); 483be691f3bSpatrick } else 484*f6aab3d8Srobert return result; 485be691f3bSpatrick } else { 486be691f3bSpatrick error.SetErrorString("Couldn't get num arguments for handle_stop " 487be691f3bSpatrick "callback."); 488*f6aab3d8Srobert return PythonObject(); 489be691f3bSpatrick } 490*f6aab3d8Srobert return result; 491*f6aab3d8Srobert } else { 492be691f3bSpatrick error.SetErrorStringWithFormat("Class \"%s\" is missing the required " 493be691f3bSpatrick "handle_stop callback.", 494be691f3bSpatrick python_class_name); 495be691f3bSpatrick } 496be691f3bSpatrick } 497*f6aab3d8Srobert return PythonObject(); 498be691f3bSpatrick} 499be691f3bSpatrick 500*f6aab3d8Srobertbool lldb_private::LLDBSwigPythonStopHookCallHandleStop( 501*f6aab3d8Srobert void *implementor, lldb::ExecutionContextRefSP exc_ctx_sp, 502*f6aab3d8Srobert lldb::StreamSP stream) { 503be691f3bSpatrick // handle_stop will return a bool with the meaning "should_stop"... 504be691f3bSpatrick // If you return nothing we'll assume we are going to stop. 505be691f3bSpatrick // Also any errors should return true, since we should stop on error. 506be691f3bSpatrick 507be691f3bSpatrick PyErr_Cleaner py_err_cleaner(false); 508be691f3bSpatrick PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor)); 509be691f3bSpatrick auto pfunc = self.ResolveName<PythonCallable>("handle_stop"); 510be691f3bSpatrick 511be691f3bSpatrick if (!pfunc.IsAllocated()) 512be691f3bSpatrick return true; 513be691f3bSpatrick 514*f6aab3d8Srobert auto *sb_stream = new lldb::SBStream(); 515*f6aab3d8Srobert PythonObject sb_stream_arg = 516*f6aab3d8Srobert ToSWIGWrapper(std::unique_ptr<lldb::SBStream>(sb_stream)); 517*f6aab3d8Srobert PythonObject result = 518*f6aab3d8Srobert pfunc(ToSWIGWrapper(std::move(exc_ctx_sp)), sb_stream_arg); 519be691f3bSpatrick 520*f6aab3d8Srobert if (PyErr_Occurred()) { 521be691f3bSpatrick stream->PutCString("Python error occurred handling stop-hook."); 522be691f3bSpatrick PyErr_Print(); 523be691f3bSpatrick PyErr_Clear(); 524be691f3bSpatrick return true; 525be691f3bSpatrick } 526be691f3bSpatrick 527be691f3bSpatrick // Now add the result to the output stream. SBStream only 528be691f3bSpatrick // makes an internally help StreamString which I can't interpose, so I 529be691f3bSpatrick // have to copy it over here. 530*f6aab3d8Srobert stream->PutCString(sb_stream->GetData()); 531be691f3bSpatrick 532be691f3bSpatrick if (result.get() == Py_False) 533be691f3bSpatrick return false; 534be691f3bSpatrick else 535be691f3bSpatrick return true; 536be691f3bSpatrick} 537be691f3bSpatrick 538*f6aab3d8Srobert// wrapper that calls an optional instance member of an object taking no 539*f6aab3d8Srobert// arguments 540*f6aab3d8Srobertstatic PyObject *LLDBSwigPython_CallOptionalMember( 541*f6aab3d8Srobert PyObject * implementor, char *callee_name, 542*f6aab3d8Srobert PyObject *ret_if_not_found = Py_None, bool *was_found = NULL) { 543061da546Spatrick PyErr_Cleaner py_err_cleaner(false); 544061da546Spatrick 545061da546Spatrick PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor)); 546061da546Spatrick auto pfunc = self.ResolveName<PythonCallable>(callee_name); 547061da546Spatrick 548*f6aab3d8Srobert if (!pfunc.IsAllocated()) { 549061da546Spatrick if (was_found) 550061da546Spatrick *was_found = false; 551061da546Spatrick Py_XINCREF(ret_if_not_found); 552061da546Spatrick return ret_if_not_found; 553061da546Spatrick } 554061da546Spatrick 555061da546Spatrick if (was_found) 556061da546Spatrick *was_found = true; 557061da546Spatrick 558061da546Spatrick PythonObject result = pfunc(); 559061da546Spatrick return result.release(); 560061da546Spatrick} 561061da546Spatrick 562*f6aab3d8Srobertsize_t lldb_private::LLDBSwigPython_CalculateNumChildren(PyObject * implementor, 563*f6aab3d8Srobert uint32_t max) { 564061da546Spatrick PythonObject self(PyRefType::Borrowed, implementor); 565061da546Spatrick auto pfunc = self.ResolveName<PythonCallable>("num_children"); 566061da546Spatrick 567061da546Spatrick if (!pfunc.IsAllocated()) 568061da546Spatrick return 0; 569061da546Spatrick 570061da546Spatrick auto arg_info = pfunc.GetArgInfo(); 571061da546Spatrick if (!arg_info) { 572061da546Spatrick llvm::consumeError(arg_info.takeError()); 573061da546Spatrick return 0; 574061da546Spatrick } 575061da546Spatrick 576dda28197Spatrick size_t ret_val; 577061da546Spatrick if (arg_info.get().max_positional_args < 1) 578dda28197Spatrick ret_val = unwrapOrSetPythonException(As<long long>(pfunc.Call())); 579061da546Spatrick else 580*f6aab3d8Srobert ret_val = unwrapOrSetPythonException( 581*f6aab3d8Srobert As<long long>(pfunc.Call(PythonInteger(max)))); 582061da546Spatrick 583*f6aab3d8Srobert if (PyErr_Occurred()) { 584061da546Spatrick PyErr_Print(); 585061da546Spatrick PyErr_Clear(); 586dda28197Spatrick return 0; 587061da546Spatrick } 588061da546Spatrick 589061da546Spatrick if (arg_info.get().max_positional_args < 1) 590061da546Spatrick ret_val = std::min(ret_val, static_cast<size_t>(max)); 591061da546Spatrick 592061da546Spatrick return ret_val; 593061da546Spatrick} 594061da546Spatrick 595*f6aab3d8SrobertPyObject *lldb_private::LLDBSwigPython_GetChildAtIndex(PyObject * implementor, 596*f6aab3d8Srobert uint32_t idx) { 597061da546Spatrick PyErr_Cleaner py_err_cleaner(true); 598061da546Spatrick 599061da546Spatrick PythonObject self(PyRefType::Borrowed, implementor); 600061da546Spatrick auto pfunc = self.ResolveName<PythonCallable>("get_child_at_index"); 601061da546Spatrick 602061da546Spatrick if (!pfunc.IsAllocated()) 603061da546Spatrick return nullptr; 604061da546Spatrick 605061da546Spatrick PythonObject result = pfunc(PythonInteger(idx)); 606061da546Spatrick 607061da546Spatrick if (!result.IsAllocated()) 608061da546Spatrick return nullptr; 609061da546Spatrick 610061da546Spatrick lldb::SBValue *sbvalue_ptr = nullptr; 611*f6aab3d8Srobert if (SWIG_ConvertPtr(result.get(), (void **)&sbvalue_ptr, 612*f6aab3d8Srobert SWIGTYPE_p_lldb__SBValue, 0) == -1) 613061da546Spatrick return nullptr; 614061da546Spatrick 615061da546Spatrick if (sbvalue_ptr == nullptr) 616061da546Spatrick return nullptr; 617061da546Spatrick 618061da546Spatrick return result.release(); 619061da546Spatrick} 620061da546Spatrick 621*f6aab3d8Srobertint lldb_private::LLDBSwigPython_GetIndexOfChildWithName( 622*f6aab3d8Srobert PyObject * implementor, const char *child_name) { 623061da546Spatrick PyErr_Cleaner py_err_cleaner(true); 624061da546Spatrick 625061da546Spatrick PythonObject self(PyRefType::Borrowed, implementor); 626061da546Spatrick auto pfunc = self.ResolveName<PythonCallable>("get_child_index"); 627061da546Spatrick 628061da546Spatrick if (!pfunc.IsAllocated()) 629061da546Spatrick return UINT32_MAX; 630061da546Spatrick 631dda28197Spatrick llvm::Expected<PythonObject> result = pfunc.Call(PythonString(child_name)); 632061da546Spatrick 633*f6aab3d8Srobert long long retval = 634*f6aab3d8Srobert unwrapOrSetPythonException(As<long long>(std::move(result))); 635dda28197Spatrick 636dda28197Spatrick if (PyErr_Occurred()) { 637dda28197Spatrick PyErr_Clear(); // FIXME print this? do something else 638061da546Spatrick return UINT32_MAX; 639dda28197Spatrick } 640061da546Spatrick 641061da546Spatrick if (retval >= 0) 642061da546Spatrick return (uint32_t)retval; 643061da546Spatrick 644061da546Spatrick return UINT32_MAX; 645061da546Spatrick} 646061da546Spatrick 647*f6aab3d8Srobertbool lldb_private::LLDBSwigPython_UpdateSynthProviderInstance(PyObject * 648*f6aab3d8Srobert implementor) { 649061da546Spatrick bool ret_val = false; 650061da546Spatrick 651061da546Spatrick static char callee_name[] = "update"; 652061da546Spatrick 653*f6aab3d8Srobert PyObject *py_return = 654*f6aab3d8Srobert LLDBSwigPython_CallOptionalMember(implementor, callee_name); 655061da546Spatrick 656061da546Spatrick if (py_return == Py_True) 657061da546Spatrick ret_val = true; 658061da546Spatrick 659061da546Spatrick Py_XDECREF(py_return); 660061da546Spatrick 661061da546Spatrick return ret_val; 662061da546Spatrick} 663061da546Spatrick 664*f6aab3d8Srobertbool lldb_private::LLDBSwigPython_MightHaveChildrenSynthProviderInstance( 665*f6aab3d8Srobert PyObject * implementor) { 666061da546Spatrick bool ret_val = false; 667061da546Spatrick 668061da546Spatrick static char callee_name[] = "has_children"; 669061da546Spatrick 670*f6aab3d8Srobert PyObject *py_return = 671*f6aab3d8Srobert LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_True); 672061da546Spatrick 673061da546Spatrick if (py_return == Py_True) 674061da546Spatrick ret_val = true; 675061da546Spatrick 676061da546Spatrick Py_XDECREF(py_return); 677061da546Spatrick 678061da546Spatrick return ret_val; 679061da546Spatrick} 680061da546Spatrick 681*f6aab3d8SrobertPyObject *lldb_private::LLDBSwigPython_GetValueSynthProviderInstance( 682*f6aab3d8Srobert PyObject * implementor) { 683061da546Spatrick PyObject *ret_val = nullptr; 684061da546Spatrick 685061da546Spatrick static char callee_name[] = "get_value"; 686061da546Spatrick 687*f6aab3d8Srobert PyObject *py_return = 688*f6aab3d8Srobert LLDBSwigPython_CallOptionalMember(implementor, callee_name, Py_None); 689061da546Spatrick 690061da546Spatrick if (py_return == Py_None || py_return == nullptr) 691061da546Spatrick ret_val = nullptr; 692061da546Spatrick 693061da546Spatrick lldb::SBValue *sbvalue_ptr = NULL; 694061da546Spatrick 695*f6aab3d8Srobert if (SWIG_ConvertPtr(py_return, (void **)&sbvalue_ptr, 696*f6aab3d8Srobert SWIGTYPE_p_lldb__SBValue, 0) == -1) 697061da546Spatrick ret_val = nullptr; 698061da546Spatrick else if (sbvalue_ptr == NULL) 699061da546Spatrick ret_val = nullptr; 700061da546Spatrick else 701061da546Spatrick ret_val = py_return; 702061da546Spatrick 703061da546Spatrick Py_XDECREF(py_return); 704061da546Spatrick return ret_val; 705061da546Spatrick} 706061da546Spatrick 707*f6aab3d8Srobertvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBData(PyObject * data) { 708be691f3bSpatrick lldb::SBData *sb_ptr = nullptr; 709be691f3bSpatrick 710*f6aab3d8Srobert int valid_cast = 711*f6aab3d8Srobert SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBData, 0); 712be691f3bSpatrick 713be691f3bSpatrick if (valid_cast == -1) 714be691f3bSpatrick return NULL; 715be691f3bSpatrick 716be691f3bSpatrick return sb_ptr; 717be691f3bSpatrick} 718be691f3bSpatrick 719*f6aab3d8Srobertvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBError(PyObject * data) { 720be691f3bSpatrick lldb::SBError *sb_ptr = nullptr; 721be691f3bSpatrick 722*f6aab3d8Srobert int valid_cast = 723*f6aab3d8Srobert SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBError, 0); 724be691f3bSpatrick 725be691f3bSpatrick if (valid_cast == -1) 726be691f3bSpatrick return NULL; 727be691f3bSpatrick 728be691f3bSpatrick return sb_ptr; 729be691f3bSpatrick} 730be691f3bSpatrick 731*f6aab3d8Srobertvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBValue(PyObject * data) { 732061da546Spatrick lldb::SBValue *sb_ptr = NULL; 733061da546Spatrick 734*f6aab3d8Srobert int valid_cast = 735*f6aab3d8Srobert SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0); 736061da546Spatrick 737061da546Spatrick if (valid_cast == -1) 738061da546Spatrick return NULL; 739061da546Spatrick 740061da546Spatrick return sb_ptr; 741061da546Spatrick} 742061da546Spatrick 743*f6aab3d8Srobertvoid *lldb_private::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject * 744*f6aab3d8Srobert data) { 745*f6aab3d8Srobert lldb::SBMemoryRegionInfo *sb_ptr = NULL; 746*f6aab3d8Srobert 747*f6aab3d8Srobert int valid_cast = SWIG_ConvertPtr(data, (void **)&sb_ptr, 748*f6aab3d8Srobert SWIGTYPE_p_lldb__SBMemoryRegionInfo, 0); 749*f6aab3d8Srobert 750*f6aab3d8Srobert if (valid_cast == -1) 751*f6aab3d8Srobert return NULL; 752*f6aab3d8Srobert 753*f6aab3d8Srobert return sb_ptr; 754*f6aab3d8Srobert} 755*f6aab3d8Srobert 756*f6aab3d8Srobertbool lldb_private::LLDBSwigPythonCallCommand( 757*f6aab3d8Srobert const char *python_function_name, const char *session_dictionary_name, 758*f6aab3d8Srobert lldb::DebuggerSP debugger, const char *args, 759061da546Spatrick lldb_private::CommandReturnObject &cmd_retobj, 760*f6aab3d8Srobert lldb::ExecutionContextRefSP exe_ctx_ref_sp) { 761061da546Spatrick 762061da546Spatrick PyErr_Cleaner py_err_cleaner(true); 763*f6aab3d8Srobert auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( 764*f6aab3d8Srobert session_dictionary_name); 765*f6aab3d8Srobert auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 766*f6aab3d8Srobert python_function_name, dict); 767061da546Spatrick 768061da546Spatrick if (!pfunc.IsAllocated()) 769061da546Spatrick return false; 770061da546Spatrick 771061da546Spatrick auto argc = pfunc.GetArgInfo(); 772061da546Spatrick if (!argc) { 773061da546Spatrick llvm::consumeError(argc.takeError()); 774061da546Spatrick return false; 775061da546Spatrick } 776*f6aab3d8Srobert PythonObject debugger_arg = ToSWIGWrapper(std::move(debugger)); 777*f6aab3d8Srobert auto cmd_retobj_arg = ToSWIGWrapper(cmd_retobj); 778061da546Spatrick 779061da546Spatrick if (argc.get().max_positional_args < 5u) 780*f6aab3d8Srobert pfunc(debugger_arg, PythonString(args), cmd_retobj_arg.obj(), dict); 781061da546Spatrick else 782*f6aab3d8Srobert pfunc(debugger_arg, PythonString(args), 783*f6aab3d8Srobert ToSWIGWrapper(std::move(exe_ctx_ref_sp)), cmd_retobj_arg.obj(), dict); 784061da546Spatrick 785061da546Spatrick return true; 786061da546Spatrick} 787061da546Spatrick 788*f6aab3d8Srobertbool lldb_private::LLDBSwigPythonCallCommandObject( 789*f6aab3d8Srobert PyObject *implementor, lldb::DebuggerSP debugger, const char *args, 790061da546Spatrick lldb_private::CommandReturnObject &cmd_retobj, 791*f6aab3d8Srobert lldb::ExecutionContextRefSP exe_ctx_ref_sp) { 792061da546Spatrick 793061da546Spatrick PyErr_Cleaner py_err_cleaner(true); 794061da546Spatrick 795061da546Spatrick PythonObject self(PyRefType::Borrowed, implementor); 796061da546Spatrick auto pfunc = self.ResolveName<PythonCallable>("__call__"); 797061da546Spatrick 798061da546Spatrick if (!pfunc.IsAllocated()) 799061da546Spatrick return false; 800061da546Spatrick 801*f6aab3d8Srobert auto cmd_retobj_arg = ToSWIGWrapper(cmd_retobj); 802061da546Spatrick 803*f6aab3d8Srobert pfunc(ToSWIGWrapper(std::move(debugger)), PythonString(args), 804*f6aab3d8Srobert ToSWIGWrapper(exe_ctx_ref_sp), cmd_retobj_arg.obj()); 805061da546Spatrick 806061da546Spatrick return true; 807061da546Spatrick} 808061da546Spatrick 809*f6aab3d8SrobertPythonObject lldb_private::LLDBSWIGPythonCreateOSPlugin( 810*f6aab3d8Srobert const char *python_class_name, const char *session_dictionary_name, 811*f6aab3d8Srobert const lldb::ProcessSP &process_sp) { 812*f6aab3d8Srobert if (python_class_name == NULL || python_class_name[0] == '\0' || 813*f6aab3d8Srobert !session_dictionary_name) 814*f6aab3d8Srobert return PythonObject(); 815061da546Spatrick 816061da546Spatrick PyErr_Cleaner py_err_cleaner(true); 817061da546Spatrick 818*f6aab3d8Srobert auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( 819*f6aab3d8Srobert session_dictionary_name); 820*f6aab3d8Srobert auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 821*f6aab3d8Srobert python_class_name, dict); 822061da546Spatrick 823061da546Spatrick if (!pfunc.IsAllocated()) 824*f6aab3d8Srobert return PythonObject(); 825061da546Spatrick 826*f6aab3d8Srobert return pfunc(ToSWIGWrapper(process_sp)); 827061da546Spatrick} 828061da546Spatrick 829*f6aab3d8SrobertPythonObject lldb_private::LLDBSWIGPython_CreateFrameRecognizer( 830*f6aab3d8Srobert const char *python_class_name, const char *session_dictionary_name) { 831*f6aab3d8Srobert if (python_class_name == NULL || python_class_name[0] == '\0' || 832*f6aab3d8Srobert !session_dictionary_name) 833*f6aab3d8Srobert return PythonObject(); 834061da546Spatrick 835061da546Spatrick PyErr_Cleaner py_err_cleaner(true); 836061da546Spatrick 837*f6aab3d8Srobert auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( 838*f6aab3d8Srobert session_dictionary_name); 839*f6aab3d8Srobert auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 840*f6aab3d8Srobert python_class_name, dict); 841061da546Spatrick 842061da546Spatrick if (!pfunc.IsAllocated()) 843*f6aab3d8Srobert return PythonObject(); 844061da546Spatrick 845*f6aab3d8Srobert return pfunc(); 846061da546Spatrick} 847061da546Spatrick 848*f6aab3d8SrobertPyObject *lldb_private::LLDBSwigPython_GetRecognizedArguments( 849*f6aab3d8Srobert PyObject * implementor, const lldb::StackFrameSP &frame_sp) { 850061da546Spatrick static char callee_name[] = "get_recognized_arguments"; 851061da546Spatrick 852*f6aab3d8Srobert PythonObject arg = ToSWIGWrapper(frame_sp); 853061da546Spatrick 854061da546Spatrick PythonString str(callee_name); 855*f6aab3d8Srobert PyObject *result = 856*f6aab3d8Srobert PyObject_CallMethodObjArgs(implementor, str.get(), arg.get(), NULL); 857061da546Spatrick return result; 858061da546Spatrick} 859061da546Spatrick 860*f6aab3d8Srobertvoid *lldb_private::LLDBSWIGPython_GetDynamicSetting( 861*f6aab3d8Srobert void *module, const char *setting, const lldb::TargetSP &target_sp) { 862061da546Spatrick if (!module || !setting) 863061da546Spatrick Py_RETURN_NONE; 864061da546Spatrick 865061da546Spatrick PyErr_Cleaner py_err_cleaner(true); 866061da546Spatrick PythonObject py_module(PyRefType::Borrowed, (PyObject *)module); 867061da546Spatrick auto pfunc = py_module.ResolveName<PythonCallable>("get_dynamic_setting"); 868061da546Spatrick 869061da546Spatrick if (!pfunc.IsAllocated()) 870061da546Spatrick Py_RETURN_NONE; 871061da546Spatrick 872*f6aab3d8Srobert auto result = pfunc(ToSWIGWrapper(target_sp), PythonString(setting)); 873061da546Spatrick 874061da546Spatrick return result.release(); 875061da546Spatrick} 876061da546Spatrick 877*f6aab3d8Srobertbool lldb_private::LLDBSWIGPythonRunScriptKeywordProcess( 878*f6aab3d8Srobert const char *python_function_name, const char *session_dictionary_name, 879*f6aab3d8Srobert const lldb::ProcessSP &process, std::string &output) { 880061da546Spatrick 881*f6aab3d8Srobert if (python_function_name == NULL || python_function_name[0] == '\0' || 882*f6aab3d8Srobert !session_dictionary_name) 883061da546Spatrick return false; 884061da546Spatrick 885061da546Spatrick PyErr_Cleaner py_err_cleaner(true); 886061da546Spatrick 887*f6aab3d8Srobert auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( 888*f6aab3d8Srobert session_dictionary_name); 889*f6aab3d8Srobert auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 890*f6aab3d8Srobert python_function_name, dict); 891061da546Spatrick 892061da546Spatrick if (!pfunc.IsAllocated()) 893061da546Spatrick return false; 894061da546Spatrick 895*f6aab3d8Srobert auto result = pfunc(ToSWIGWrapper(process), dict); 896061da546Spatrick 897061da546Spatrick output = result.Str().GetString().str(); 898061da546Spatrick 899061da546Spatrick return true; 900061da546Spatrick} 901061da546Spatrick 902*f6aab3d8Srobertstd::optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordThread( 903*f6aab3d8Srobert const char *python_function_name, const char *session_dictionary_name, 904*f6aab3d8Srobert lldb::ThreadSP thread) { 905*f6aab3d8Srobert if (python_function_name == NULL || python_function_name[0] == '\0' || 906*f6aab3d8Srobert !session_dictionary_name) 907*f6aab3d8Srobert return std::nullopt; 908061da546Spatrick 909*f6aab3d8Srobert PyErr_Cleaner py_err_cleaner(true); 910*f6aab3d8Srobert 911*f6aab3d8Srobert auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( 912*f6aab3d8Srobert session_dictionary_name); 913*f6aab3d8Srobert auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 914*f6aab3d8Srobert python_function_name, dict); 915*f6aab3d8Srobert 916*f6aab3d8Srobert if (!pfunc.IsAllocated()) 917*f6aab3d8Srobert return std::nullopt; 918*f6aab3d8Srobert 919*f6aab3d8Srobert auto result = pfunc(ToSWIGWrapper(std::move(thread)), dict); 920*f6aab3d8Srobert 921*f6aab3d8Srobert return result.Str().GetString().str(); 922*f6aab3d8Srobert} 923*f6aab3d8Srobert 924*f6aab3d8Srobertbool lldb_private::LLDBSWIGPythonRunScriptKeywordTarget( 925*f6aab3d8Srobert const char *python_function_name, const char *session_dictionary_name, 926*f6aab3d8Srobert const lldb::TargetSP &target, std::string &output) { 927*f6aab3d8Srobert 928*f6aab3d8Srobert if (python_function_name == NULL || python_function_name[0] == '\0' || 929*f6aab3d8Srobert !session_dictionary_name) 930061da546Spatrick return false; 931061da546Spatrick 932061da546Spatrick PyErr_Cleaner py_err_cleaner(true); 933061da546Spatrick 934*f6aab3d8Srobert auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( 935*f6aab3d8Srobert session_dictionary_name); 936*f6aab3d8Srobert auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 937*f6aab3d8Srobert python_function_name, dict); 938061da546Spatrick 939061da546Spatrick if (!pfunc.IsAllocated()) 940061da546Spatrick return false; 941061da546Spatrick 942*f6aab3d8Srobert auto result = pfunc(ToSWIGWrapper(target), dict); 943061da546Spatrick 944061da546Spatrick output = result.Str().GetString().str(); 945061da546Spatrick 946061da546Spatrick return true; 947061da546Spatrick} 948061da546Spatrick 949*f6aab3d8Srobertstd::optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordFrame( 950*f6aab3d8Srobert const char *python_function_name, const char *session_dictionary_name, 951*f6aab3d8Srobert lldb::StackFrameSP frame) { 952*f6aab3d8Srobert if (python_function_name == NULL || python_function_name[0] == '\0' || 953*f6aab3d8Srobert !session_dictionary_name) 954*f6aab3d8Srobert return std::nullopt; 955061da546Spatrick 956*f6aab3d8Srobert PyErr_Cleaner py_err_cleaner(true); 957*f6aab3d8Srobert 958*f6aab3d8Srobert auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( 959*f6aab3d8Srobert session_dictionary_name); 960*f6aab3d8Srobert auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 961*f6aab3d8Srobert python_function_name, dict); 962*f6aab3d8Srobert 963*f6aab3d8Srobert if (!pfunc.IsAllocated()) 964*f6aab3d8Srobert return std::nullopt; 965*f6aab3d8Srobert 966*f6aab3d8Srobert auto result = pfunc(ToSWIGWrapper(std::move(frame)), dict); 967*f6aab3d8Srobert 968*f6aab3d8Srobert return result.Str().GetString().str(); 969*f6aab3d8Srobert} 970*f6aab3d8Srobert 971*f6aab3d8Srobertbool lldb_private::LLDBSWIGPythonRunScriptKeywordValue( 972*f6aab3d8Srobert const char *python_function_name, const char *session_dictionary_name, 973*f6aab3d8Srobert const lldb::ValueObjectSP &value, std::string &output) { 974*f6aab3d8Srobert 975*f6aab3d8Srobert if (python_function_name == NULL || python_function_name[0] == '\0' || 976*f6aab3d8Srobert !session_dictionary_name) 977061da546Spatrick return false; 978061da546Spatrick 979061da546Spatrick PyErr_Cleaner py_err_cleaner(true); 980061da546Spatrick 981*f6aab3d8Srobert auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( 982*f6aab3d8Srobert session_dictionary_name); 983*f6aab3d8Srobert auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 984*f6aab3d8Srobert python_function_name, dict); 985061da546Spatrick 986061da546Spatrick if (!pfunc.IsAllocated()) 987061da546Spatrick return false; 988061da546Spatrick 989*f6aab3d8Srobert auto result = pfunc(ToSWIGWrapper(value), dict); 990061da546Spatrick 991061da546Spatrick output = result.Str().GetString().str(); 992061da546Spatrick 993061da546Spatrick return true; 994061da546Spatrick} 995061da546Spatrick 996*f6aab3d8Srobertbool lldb_private::LLDBSwigPythonCallModuleInit( 997*f6aab3d8Srobert const char *python_module_name, const char *session_dictionary_name, 998*f6aab3d8Srobert lldb::DebuggerSP debugger) { 999061da546Spatrick std::string python_function_name_string = python_module_name; 1000061da546Spatrick python_function_name_string += ".__lldb_init_module"; 1001061da546Spatrick const char *python_function_name = python_function_name_string.c_str(); 1002061da546Spatrick 1003061da546Spatrick PyErr_Cleaner py_err_cleaner(true); 1004061da546Spatrick 1005*f6aab3d8Srobert auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>( 1006*f6aab3d8Srobert session_dictionary_name); 1007*f6aab3d8Srobert auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>( 1008*f6aab3d8Srobert python_function_name, dict); 1009061da546Spatrick 1010061da546Spatrick // This method is optional and need not exist. So if we don't find it, 1011061da546Spatrick // it's actually a success, not a failure. 1012061da546Spatrick if (!pfunc.IsAllocated()) 1013061da546Spatrick return true; 1014061da546Spatrick 1015*f6aab3d8Srobert pfunc(ToSWIGWrapper(std::move(debugger)), dict); 1016061da546Spatrick 1017061da546Spatrick return true; 1018061da546Spatrick} 1019061da546Spatrick 1020*f6aab3d8Srobertlldb::ValueObjectSP lldb_private::LLDBSWIGPython_GetValueObjectSPFromSBValue( 1021*f6aab3d8Srobert void *data) { 1022061da546Spatrick lldb::ValueObjectSP valobj_sp; 1023*f6aab3d8Srobert if (data) { 1024061da546Spatrick lldb::SBValue *sb_ptr = (lldb::SBValue *)data; 1025061da546Spatrick valobj_sp = sb_ptr->GetSP(); 1026061da546Spatrick } 1027061da546Spatrick return valobj_sp; 1028061da546Spatrick} 1029061da546Spatrick 1030061da546Spatrick// For the LogOutputCallback functions 1031*f6aab3d8Srobertstatic void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, 1032*f6aab3d8Srobert void *baton) { 1033061da546Spatrick if (baton != Py_None) { 1034061da546Spatrick SWIG_PYTHON_THREAD_BEGIN_BLOCK; 1035*f6aab3d8Srobert PyObject *result = PyObject_CallFunction( 1036*f6aab3d8Srobert reinterpret_cast<PyObject *>(baton), const_cast<char *>("s"), str); 1037061da546Spatrick Py_XDECREF(result); 1038061da546Spatrick SWIG_PYTHON_THREAD_END_BLOCK; 1039061da546Spatrick } 1040061da546Spatrick} 1041061da546Spatrick%} 1042