[lldb] Make GetSelectedOrDummyTarget return the target by reference (NFC)

Return references from GetDummyTarget and GetSelectedOrDummyTarget. This
matches how the APIs are already used in practice.
This commit is contained in:
Jonas Devlieghere 2020-11-09 15:25:59 -08:00
parent 554939583a
commit b2fa3b922e
8 changed files with 31 additions and 47 deletions

View File

@ -334,8 +334,8 @@ public:
// This is for use in the command interpreter, when you either want the // This is for use in the command interpreter, when you either want the
// selected target, or if no target is present you want to prime the dummy // selected target, or if no target is present you want to prime the dummy
// target with entities that will be copied over to new targets. // target with entities that will be copied over to new targets.
Target *GetSelectedOrDummyTarget(bool prefer_dummy = false); Target &GetSelectedOrDummyTarget(bool prefer_dummy = false);
Target *GetDummyTarget() { return m_dummy_target_sp.get(); } Target &GetDummyTarget() { return *m_dummy_target_sp; }
lldb::BroadcasterManagerSP GetBroadcasterManager() { lldb::BroadcasterManagerSP GetBroadcasterManager() {
return m_broadcaster_manager_sp; return m_broadcaster_manager_sp;

View File

@ -212,7 +212,7 @@ public:
bool GetInjectLocalVariables(ExecutionContext *exe_ctx) const; bool GetInjectLocalVariables(ExecutionContext *exe_ctx) const;
void SetInjectLocalVariables(ExecutionContext *exe_ctx, bool b); void SetInjectLocalVariables(ExecutionContext *exe_ctx, bool b);
void SetRequireHardwareBreakpoints(bool b); void SetRequireHardwareBreakpoints(bool b);
bool GetRequireHardwareBreakpoints() const; bool GetRequireHardwareBreakpoints() const;
@ -1252,7 +1252,7 @@ public:
StructuredDataImpl *m_extra_args; // We own this structured data, StructuredDataImpl *m_extra_args; // We own this structured data,
// but the SD itself manages the UP. // but the SD itself manages the UP.
/// This holds the python callback object. /// This holds the python callback object.
StructuredData::GenericSP m_implementation_sp; StructuredData::GenericSP m_implementation_sp;
/// Use CreateStopHook to make a new empty stop hook. The GetCommandPointer /// Use CreateStopHook to make a new empty stop hook. The GetCommandPointer
/// and fill it with commands, and SetSpecifier to set the specifier shared /// and fill it with commands, and SetSpecifier to set the specifier shared
@ -1374,12 +1374,12 @@ protected:
lldb::PlatformSP m_platform_sp; ///< The platform for this target. lldb::PlatformSP m_platform_sp; ///< The platform for this target.
std::recursive_mutex m_mutex; ///< An API mutex that is used by the lldb::SB* std::recursive_mutex m_mutex; ///< An API mutex that is used by the lldb::SB*
/// classes make the SB interface thread safe /// classes make the SB interface thread safe
/// When the private state thread calls SB API's - usually because it is /// When the private state thread calls SB API's - usually because it is
/// running OS plugin or Python ThreadPlan code - it should not block on the /// running OS plugin or Python ThreadPlan code - it should not block on the
/// API mutex that is held by the code that kicked off the sequence of events /// API mutex that is held by the code that kicked off the sequence of events
/// that led us to run the code. We hand out this mutex instead when we /// that led us to run the code. We hand out this mutex instead when we
/// detect that code is running on the private state thread. /// detect that code is running on the private state thread.
std::recursive_mutex m_private_mutex; std::recursive_mutex m_private_mutex;
Arch m_arch; Arch m_arch;
ModuleList m_images; ///< The list of images for this process (shared ModuleList m_images; ///< The list of images for this process (shared
/// libraries and anything dynamically loaded). /// libraries and anything dynamically loaded).
@ -1458,7 +1458,7 @@ private:
bool ProcessIsValid(); bool ProcessIsValid();
// Copy breakpoints, stop hooks and so forth from the dummy target: // Copy breakpoints, stop hooks and so forth from the dummy target:
void PrimeFromDummyTarget(Target *dummy_target); void PrimeFromDummyTarget(Target &target);
void AddBreakpoint(lldb::BreakpointSP breakpoint_sp, bool internal); void AddBreakpoint(lldb::BreakpointSP breakpoint_sp, bool internal);

View File

@ -858,7 +858,7 @@ SBTarget SBDebugger::GetDummyTarget() {
SBTarget sb_target; SBTarget sb_target;
if (m_opaque_sp) { if (m_opaque_sp) {
sb_target.SetSP(m_opaque_sp->GetDummyTarget()->shared_from_this()); sb_target.SetSP(m_opaque_sp->GetDummyTarget().shared_from_this());
} }
Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
LLDB_LOGF(log, "SBDebugger(%p)::GetDummyTarget() => SBTarget(%p)", LLDB_LOGF(log, "SBDebugger(%p)::GetDummyTarget() => SBTarget(%p)",

View File

@ -304,11 +304,8 @@ void CommandObjectExpression::HandleCompletion(CompletionRequest &request) {
return; return;
ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Target *exe_target = exe_ctx.GetTargetPtr();
Target *target = exe_ctx.GetTargetPtr(); Target &target = exe_target ? *exe_target : GetDummyTarget();
if (!target)
target = &GetDummyTarget();
unsigned cursor_pos = request.GetRawCursorPos(); unsigned cursor_pos = request.GetRawCursorPos();
// Get the full user input including the suffix. The suffix is necessary // Get the full user input including the suffix. The suffix is necessary
@ -342,7 +339,7 @@ void CommandObjectExpression::HandleCompletion(CompletionRequest &request) {
auto language = exe_ctx.GetFrameRef().GetLanguage(); auto language = exe_ctx.GetFrameRef().GetLanguage();
Status error; Status error;
lldb::UserExpressionSP expr(target->GetUserExpressionForLanguage( lldb::UserExpressionSP expr(target.GetUserExpressionForLanguage(
code, llvm::StringRef(), language, UserExpression::eResultTypeAny, code, llvm::StringRef(), language, UserExpression::eResultTypeAny,
options, nullptr, error)); options, nullptr, error));
if (error.Fail()) if (error.Fail())
@ -411,22 +408,19 @@ bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr,
// command object DoExecute has finished when doing multi-line expression // command object DoExecute has finished when doing multi-line expression
// that use an input reader... // that use an input reader...
ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Target *exe_target = exe_ctx.GetTargetPtr();
Target *target = exe_ctx.GetTargetPtr(); Target &target = exe_target ? *exe_target : GetDummyTarget();
if (!target)
target = &GetDummyTarget();
lldb::ValueObjectSP result_valobj_sp; lldb::ValueObjectSP result_valobj_sp;
StackFrame *frame = exe_ctx.GetFramePtr(); StackFrame *frame = exe_ctx.GetFramePtr();
const EvaluateExpressionOptions options = GetEvalOptions(*target); const EvaluateExpressionOptions options = GetEvalOptions(target);
ExpressionResults success = target->EvaluateExpression( ExpressionResults success = target.EvaluateExpression(
expr, frame, result_valobj_sp, options, &m_fixed_expression); expr, frame, result_valobj_sp, options, &m_fixed_expression);
// We only tell you about the FixIt if we applied it. The compiler errors // We only tell you about the FixIt if we applied it. The compiler errors
// will suggest the FixIt if it parsed. // will suggest the FixIt if it parsed.
if (!m_fixed_expression.empty() && target->GetEnableNotifyAboutFixIts()) { if (!m_fixed_expression.empty() && target.GetEnableNotifyAboutFixIts()) {
if (success == eExpressionCompleted) if (success == eExpressionCompleted)
error_stream.Printf(" Fix-it applied, fixed expression was: \n %s\n", error_stream.Printf(" Fix-it applied, fixed expression was: \n %s\n",
m_fixed_expression.c_str()); m_fixed_expression.c_str());

View File

@ -1578,14 +1578,11 @@ void Debugger::JoinIOHandlerThread() {
} }
} }
Target *Debugger::GetSelectedOrDummyTarget(bool prefer_dummy) { Target &Debugger::GetSelectedOrDummyTarget(bool prefer_dummy) {
Target *target = nullptr;
if (!prefer_dummy) { if (!prefer_dummy) {
target = m_target_list.GetSelectedTarget().get(); if (TargetSP target = m_target_list.GetSelectedTarget())
if (target) return *target;
return target;
} }
return GetDummyTarget(); return GetDummyTarget();
} }

View File

@ -1523,16 +1523,12 @@ Status CommandInterpreter::PreprocessCommand(std::string &command) {
end_backtick - expr_content_start); end_backtick - expr_content_start);
ExecutionContext exe_ctx(GetExecutionContext()); ExecutionContext exe_ctx(GetExecutionContext());
Target *target = exe_ctx.GetTargetPtr();
// Get a dummy target to allow for calculator mode while processing // Get a dummy target to allow for calculator mode while processing
// backticks. This also helps break the infinite loop caused when target is // backticks. This also helps break the infinite loop caused when target is
// null. // null.
if (!target) Target *exe_target = exe_ctx.GetTargetPtr();
target = m_debugger.GetDummyTarget(); Target &target = exe_target ? *exe_target : m_debugger.GetDummyTarget();
if (!target)
continue;
ValueObjectSP expr_result_valobj_sp; ValueObjectSP expr_result_valobj_sp;
@ -1545,8 +1541,8 @@ Status CommandInterpreter::PreprocessCommand(std::string &command) {
options.SetTimeout(llvm::None); options.SetTimeout(llvm::None);
ExpressionResults expr_result = ExpressionResults expr_result =
target->EvaluateExpression(expr_str.c_str(), exe_ctx.GetFramePtr(), target.EvaluateExpression(expr_str.c_str(), exe_ctx.GetFramePtr(),
expr_result_valobj_sp, options); expr_result_valobj_sp, options);
if (expr_result == eExpressionCompleted) { if (expr_result == eExpressionCompleted) {
Scalar scalar; Scalar scalar;

View File

@ -930,11 +930,11 @@ const char *CommandObject::GetArgumentDescriptionAsCString(
} }
Target &CommandObject::GetDummyTarget() { Target &CommandObject::GetDummyTarget() {
return *m_interpreter.GetDebugger().GetDummyTarget(); return m_interpreter.GetDebugger().GetDummyTarget();
} }
Target &CommandObject::GetSelectedOrDummyTarget(bool prefer_dummy) { Target &CommandObject::GetSelectedOrDummyTarget(bool prefer_dummy) {
return *m_interpreter.GetDebugger().GetSelectedOrDummyTarget(prefer_dummy); return m_interpreter.GetDebugger().GetSelectedOrDummyTarget(prefer_dummy);
} }
Target &CommandObject::GetSelectedTarget() { Target &CommandObject::GetSelectedTarget() {

View File

@ -128,13 +128,10 @@ Target::~Target() {
DeleteCurrentProcess(); DeleteCurrentProcess();
} }
void Target::PrimeFromDummyTarget(Target *target) { void Target::PrimeFromDummyTarget(Target &target) {
if (!target) m_stop_hooks = target.m_stop_hooks;
return;
m_stop_hooks = target->m_stop_hooks; for (const auto &breakpoint_sp : target.m_breakpoint_list.Breakpoints()) {
for (const auto &breakpoint_sp : target->m_breakpoint_list.Breakpoints()) {
if (breakpoint_sp->IsInternal()) if (breakpoint_sp->IsInternal())
continue; continue;
@ -143,14 +140,14 @@ void Target::PrimeFromDummyTarget(Target *target) {
AddBreakpoint(std::move(new_bp), false); AddBreakpoint(std::move(new_bp), false);
} }
for (auto bp_name_entry : target->m_breakpoint_names) { for (auto bp_name_entry : target.m_breakpoint_names) {
BreakpointName *new_bp_name = new BreakpointName(*bp_name_entry.second); BreakpointName *new_bp_name = new BreakpointName(*bp_name_entry.second);
AddBreakpointName(new_bp_name); AddBreakpointName(new_bp_name);
} }
m_frame_recognizer_manager_up = std::make_unique<StackFrameRecognizerManager>( m_frame_recognizer_manager_up = std::make_unique<StackFrameRecognizerManager>(
*target->m_frame_recognizer_manager_up); *target.m_frame_recognizer_manager_up);
} }
void Target::Dump(Stream *s, lldb::DescriptionLevel description_level) { void Target::Dump(Stream *s, lldb::DescriptionLevel description_level) {