[lldb/python] Avoid more dangling pointers in python glue code

This commit is contained in:
Pavel Labath
2021-12-17 13:02:21 +01:00
parent ede7c2438f
commit 2efc6892d8
8 changed files with 64 additions and 103 deletions

View File

@@ -38,14 +38,12 @@ llvm::Expected<bool> lldb_private::LLDBSwigPythonBreakpointCallbackFunction(
return arg_info.takeError();
PythonObject frame_arg = ToSWIGWrapper(frame_sp);
PythonObject bp_loc_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_bp_loc));
PythonObject bp_loc_arg = ToSWIGWrapper(bp_loc_sp);
auto result = [&]() -> Expected<PythonObject> {
// If the called function doesn't take extra_args, drop them here:
if (max_positional_args < 4)
return pfunc.Call(frame_arg, bp_loc_arg, dict);
return pfunc.Call(frame_arg, bp_loc_arg, ToSWIGWrapper(args_impl), dict);
}();
auto result =
max_positional_args < 4
? pfunc.Call(frame_arg, bp_loc_arg, dict)
: pfunc.Call(frame_arg, bp_loc_arg, ToSWIGWrapper(args_impl), dict);
if (!result)
return result.takeError();
@@ -70,7 +68,6 @@ llvm::Expected<bool> lldb_private::LLDBSwigPythonBreakpointCallbackFunction(
bool lldb_private::LLDBSwigPythonWatchpointCallbackFunction(
const char *python_function_name, const char *session_dictionary_name,
const lldb::StackFrameSP &frame_sp, const lldb::WatchpointSP &wp_sp) {
lldb::SBWatchpoint sb_wp(wp_sp);
bool stop_at_watchpoint = true;
@@ -84,9 +81,8 @@ bool lldb_private::LLDBSwigPythonWatchpointCallbackFunction(
if (!pfunc.IsAllocated())
return stop_at_watchpoint;
PythonObject frame_arg = ToSWIGWrapper(frame_sp);
PythonObject wp_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_wp));
PythonObject result = pfunc(frame_arg, wp_arg, dict);
PythonObject result =
pfunc(ToSWIGWrapper(frame_sp), ToSWIGWrapper(wp_sp), dict);
if (result.get() == Py_False)
stop_at_watchpoint = false;
@@ -98,7 +94,6 @@ bool lldb_private::LLDBSwigPythonCallTypeScript(
const char *python_function_name, const void *session_dictionary,
const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper,
const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval) {
lldb::SBTypeSummaryOptions sb_options(options_sp.get());
retval.clear();
@@ -146,12 +141,11 @@ bool lldb_private::LLDBSwigPythonCallTypeScript(
}
PythonObject value_arg = ToSWIGWrapper(valobj_sp);
PythonObject options_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_options));
if (argc.get().max_positional_args < 3)
result = pfunc(value_arg, dict);
else
result = pfunc(value_arg, dict, options_arg);
result = pfunc(value_arg, dict, ToSWIGWrapper(*options_sp));
retval = result.Str().GetString().str();
@@ -452,13 +446,7 @@ unsigned int lldb_private::LLDBSwigPythonCallBreakpointResolver(
if (!pfunc.IsAllocated())
return 0;
PythonObject result;
if (sym_ctx != nullptr) {
lldb::SBSymbolContext sb_sym_ctx(sym_ctx);
PythonObject sym_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_sym_ctx));
result = pfunc(sym_ctx_arg);
} else
result = pfunc();
PythonObject result = sym_ctx ? pfunc(ToSWIGWrapper(*sym_ctx)) : pfunc();
if (PyErr_Occurred()) {
PyErr_Print();
@@ -560,12 +548,11 @@ bool lldb_private::LLDBSwigPythonStopHookCallHandleStop(
if (!pfunc.IsAllocated())
return true;
PythonObject result;
lldb::SBExecutionContext sb_exc_ctx(exc_ctx_sp);
PythonObject exc_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_exc_ctx));
lldb::SBStream sb_stream;
PythonObject sb_stream_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_stream));
result = pfunc(exc_ctx_arg, sb_stream_arg);
auto *sb_stream = new lldb::SBStream();
PythonObject sb_stream_arg =
ToSWIGWrapper(std::unique_ptr<lldb::SBStream>(sb_stream));
PythonObject result =
pfunc(ToSWIGWrapper(std::move(exc_ctx_sp)), sb_stream_arg);
if (PyErr_Occurred()) {
stream->PutCString("Python error occurred handling stop-hook.");
@@ -577,7 +564,7 @@ bool lldb_private::LLDBSwigPythonStopHookCallHandleStop(
// Now add the result to the output stream. SBStream only
// makes an internally help StreamString which I can't interpose, so I
// have to copy it over here.
stream->PutCString(sb_stream.GetData());
stream->PutCString(sb_stream->GetData());
if (result.get() == Py_False)
return false;
@@ -809,7 +796,6 @@ bool lldb_private::LLDBSwigPythonCallCommand(
lldb_private::CommandReturnObject &cmd_retobj,
lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj);
lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
PyErr_Cleaner py_err_cleaner(true);
auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
@@ -826,14 +812,14 @@ bool lldb_private::LLDBSwigPythonCallCommand(
return false;
}
PythonObject debugger_arg = ToSWIGWrapper(std::move(debugger));
PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb));
PythonObject cmd_retobj_arg(PyRefType::Owned,
SBTypeToSWIGWrapper(cmd_retobj_sb));
if (argc.get().max_positional_args < 5u)
pfunc(debugger_arg, PythonString(args), cmd_retobj_arg, dict);
else
pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg, dict);
pfunc(debugger_arg, PythonString(args),
ToSWIGWrapper(std::move(exe_ctx_ref_sp)), cmd_retobj_arg, dict);
return true;
}
@@ -843,7 +829,6 @@ bool lldb_private::LLDBSwigPythonCallCommandObject(
lldb_private::CommandReturnObject &cmd_retobj,
lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj);
lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);
PyErr_Cleaner py_err_cleaner(true);
@@ -853,12 +838,11 @@ bool lldb_private::LLDBSwigPythonCallCommandObject(
if (!pfunc.IsAllocated())
return false;
PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb));
PythonObject cmd_retobj_arg(PyRefType::Owned,
SBTypeToSWIGWrapper(cmd_retobj_sb));
pfunc(ToSWIGWrapper(std::move(debugger)), PythonString(args), exe_ctx_arg,
cmd_retobj_arg);
pfunc(ToSWIGWrapper(std::move(debugger)), PythonString(args),
ToSWIGWrapper(exe_ctx_ref_sp), cmd_retobj_arg);
return true;
}