From 20ce8affce85ddea069e979a6e79a2c62ca7a0bc Mon Sep 17 00:00:00 2001 From: Fred Riss Date: Wed, 5 Feb 2020 21:47:33 -0800 Subject: [PATCH] [lldb/API] NFC: Reformat and simplify SBThread::GetStopDescription() Summary: This gets rid of some nesting and of the raw char* variable that caused the memory management bug we hit recently. This commit also removes the fallback code which should trigger when the StopInfo provides no stop description. All currently implemented StopInfos have a `GetDescription()` method that shouldn't return an empty description. Reviewers: JDevlieghere, labath, mib Subscribers: lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D74157 --- lldb/source/API/SBThread.cpp | 108 ++++++---------------------------- lldb/source/Target/Thread.cpp | 6 +- 2 files changed, 23 insertions(+), 91 deletions(-) diff --git a/lldb/source/API/SBThread.cpp b/lldb/source/API/SBThread.cpp index ad509b81d2bf..c7786534076e 100644 --- a/lldb/source/API/SBThread.cpp +++ b/lldb/source/API/SBThread.cpp @@ -40,7 +40,6 @@ #include "lldb/Target/ThreadPlanStepInstruction.h" #include "lldb/Target/ThreadPlanStepOut.h" #include "lldb/Target/ThreadPlanStepRange.h" -#include "lldb/Target/UnixSignals.h" #include "lldb/Utility/State.h" #include "lldb/Utility/Stream.h" #include "lldb/Utility/StructuredData.h" @@ -319,97 +318,26 @@ size_t SBThread::GetStopDescription(char *dst, size_t dst_len) { std::unique_lock lock; ExecutionContext exe_ctx(m_opaque_sp.get(), lock); - if (exe_ctx.HasThreadScope()) { - Process::StopLocker stop_locker; - if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) { - - StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo(); - if (stop_info_sp) { - std::string thread_stop_desc = - exe_ctx.GetThreadPtr()->GetStopDescription(); - const char *stop_desc = thread_stop_desc.c_str(); - - if (stop_desc[0] != '\0') { - if (dst) - return ::snprintf(dst, dst_len, "%s", stop_desc); - else { - // NULL dst passed in, return the length needed to contain the - // description - return ::strlen(stop_desc) + 1; // Include the NULL byte for size - } - } else { - size_t stop_desc_len = 0; - switch (stop_info_sp->GetStopReason()) { - case eStopReasonTrace: - case eStopReasonPlanComplete: { - static char trace_desc[] = "step"; - stop_desc = trace_desc; - stop_desc_len = - sizeof(trace_desc); // Include the NULL byte for size - } break; - - case eStopReasonBreakpoint: { - static char bp_desc[] = "breakpoint hit"; - stop_desc = bp_desc; - stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size - } break; - - case eStopReasonWatchpoint: { - static char wp_desc[] = "watchpoint hit"; - stop_desc = wp_desc; - stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size - } break; - - case eStopReasonSignal: { - stop_desc = - exe_ctx.GetProcessPtr()->GetUnixSignals()->GetSignalAsCString( - stop_info_sp->GetValue()); - if (stop_desc == nullptr || stop_desc[0] == '\0') { - static char signal_desc[] = "signal"; - stop_desc = signal_desc; - stop_desc_len = - sizeof(signal_desc); // Include the NULL byte for size - } - } break; - - case eStopReasonException: { - char exc_desc[] = "exception"; - stop_desc = exc_desc; - stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size - } break; - - case eStopReasonExec: { - char exc_desc[] = "exec"; - stop_desc = exc_desc; - stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size - } break; - - case eStopReasonThreadExiting: { - char limbo_desc[] = "thread exiting"; - stop_desc = limbo_desc; - stop_desc_len = sizeof(limbo_desc); - } break; - default: - break; - } - - if (stop_desc && stop_desc[0]) { - if (dst) - return ::snprintf(dst, dst_len, "%s", stop_desc) + - 1; // Include the NULL byte - - if (stop_desc_len == 0) - stop_desc_len = ::strlen(stop_desc) + 1; // Include the NULL byte - - return stop_desc_len; - } - } - } - } - } if (dst) *dst = 0; - return 0; + + if (!exe_ctx.HasThreadScope()) + return 0; + + Process::StopLocker stop_locker; + if (!stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) + return 0; + + std::string thread_stop_desc = exe_ctx.GetThreadPtr()->GetStopDescription(); + if (thread_stop_desc.empty()) + return 0; + + if (dst) + return ::snprintf(dst, dst_len, "%s", thread_stop_desc.c_str()) + 1; + + // NULL dst passed in, return the length needed to contain the + // description. + return thread_stop_desc.size() + 1; // Include the NULL byte for size } SBValue SBThread::GetStopReturnValue() { diff --git a/lldb/source/Target/Thread.cpp b/lldb/source/Target/Thread.cpp index 00f8b5ae276e..60d5617053ec 100644 --- a/lldb/source/Target/Thread.cpp +++ b/lldb/source/Target/Thread.cpp @@ -596,8 +596,12 @@ std::string Thread::GetStopDescription() { std::string Thread::GetStopDescriptionRaw() { StopInfoSP stop_info_sp = GetStopInfo(); std::string raw_stop_description; - if (stop_info_sp && stop_info_sp->IsValid()) + if (stop_info_sp && stop_info_sp->IsValid()) { raw_stop_description = stop_info_sp->GetDescription(); + assert((!raw_stop_description.empty() || + stop_info_sp->GetStopReason() == eStopReasonNone) && + "StopInfo returned an empty description."); + } return raw_stop_description; }