15ffd83dbSDimitry Andric //===-- InstrumentationRuntimeUBSan.cpp -----------------------------------===//
25ffd83dbSDimitry Andric //
35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ffd83dbSDimitry Andric //
75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
85ffd83dbSDimitry Andric 
95ffd83dbSDimitry Andric #include "InstrumentationRuntimeUBSan.h"
105ffd83dbSDimitry Andric 
115ffd83dbSDimitry Andric #include "Plugins/Process/Utility/HistoryThread.h"
125ffd83dbSDimitry Andric #include "lldb/Breakpoint/StoppointCallbackContext.h"
135ffd83dbSDimitry Andric #include "lldb/Core/Debugger.h"
145ffd83dbSDimitry Andric #include "lldb/Core/Module.h"
155ffd83dbSDimitry Andric #include "lldb/Core/PluginInterface.h"
165ffd83dbSDimitry Andric #include "lldb/Core/PluginManager.h"
175ffd83dbSDimitry Andric #include "lldb/Core/ValueObject.h"
185ffd83dbSDimitry Andric #include "lldb/Expression/UserExpression.h"
19*5f757f3fSDimitry Andric #include "lldb/Host/StreamFile.h"
205ffd83dbSDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
215ffd83dbSDimitry Andric #include "lldb/Symbol/Symbol.h"
225ffd83dbSDimitry Andric #include "lldb/Symbol/SymbolContext.h"
235ffd83dbSDimitry Andric #include "lldb/Symbol/Variable.h"
245ffd83dbSDimitry Andric #include "lldb/Symbol/VariableList.h"
255ffd83dbSDimitry Andric #include "lldb/Target/InstrumentationRuntimeStopInfo.h"
265ffd83dbSDimitry Andric #include "lldb/Target/SectionLoadList.h"
275ffd83dbSDimitry Andric #include "lldb/Target/StopInfo.h"
285ffd83dbSDimitry Andric #include "lldb/Target/Target.h"
295ffd83dbSDimitry Andric #include "lldb/Target/Thread.h"
305ffd83dbSDimitry Andric #include "lldb/Utility/RegularExpression.h"
315ffd83dbSDimitry Andric #include "lldb/Utility/Stream.h"
32fe6060f1SDimitry Andric #include <cctype>
335ffd83dbSDimitry Andric 
345ffd83dbSDimitry Andric #include <memory>
355ffd83dbSDimitry Andric 
365ffd83dbSDimitry Andric using namespace lldb;
375ffd83dbSDimitry Andric using namespace lldb_private;
385ffd83dbSDimitry Andric 
LLDB_PLUGIN_DEFINE(InstrumentationRuntimeUBSan)395ffd83dbSDimitry Andric LLDB_PLUGIN_DEFINE(InstrumentationRuntimeUBSan)
405ffd83dbSDimitry Andric 
415ffd83dbSDimitry Andric InstrumentationRuntimeUBSan::~InstrumentationRuntimeUBSan() { Deactivate(); }
425ffd83dbSDimitry Andric 
435ffd83dbSDimitry Andric lldb::InstrumentationRuntimeSP
CreateInstance(const lldb::ProcessSP & process_sp)445ffd83dbSDimitry Andric InstrumentationRuntimeUBSan::CreateInstance(const lldb::ProcessSP &process_sp) {
455ffd83dbSDimitry Andric   return InstrumentationRuntimeSP(new InstrumentationRuntimeUBSan(process_sp));
465ffd83dbSDimitry Andric }
475ffd83dbSDimitry Andric 
Initialize()485ffd83dbSDimitry Andric void InstrumentationRuntimeUBSan::Initialize() {
495ffd83dbSDimitry Andric   PluginManager::RegisterPlugin(
505ffd83dbSDimitry Andric       GetPluginNameStatic(),
515ffd83dbSDimitry Andric       "UndefinedBehaviorSanitizer instrumentation runtime plugin.",
525ffd83dbSDimitry Andric       CreateInstance, GetTypeStatic);
535ffd83dbSDimitry Andric }
545ffd83dbSDimitry Andric 
Terminate()555ffd83dbSDimitry Andric void InstrumentationRuntimeUBSan::Terminate() {
565ffd83dbSDimitry Andric   PluginManager::UnregisterPlugin(CreateInstance);
575ffd83dbSDimitry Andric }
585ffd83dbSDimitry Andric 
GetTypeStatic()595ffd83dbSDimitry Andric lldb::InstrumentationRuntimeType InstrumentationRuntimeUBSan::GetTypeStatic() {
605ffd83dbSDimitry Andric   return eInstrumentationRuntimeTypeUndefinedBehaviorSanitizer;
615ffd83dbSDimitry Andric }
625ffd83dbSDimitry Andric 
635ffd83dbSDimitry Andric static const char *ub_sanitizer_retrieve_report_data_prefix = R"(
645ffd83dbSDimitry Andric extern "C" {
655ffd83dbSDimitry Andric void
665ffd83dbSDimitry Andric __ubsan_get_current_report_data(const char **OutIssueKind,
675ffd83dbSDimitry Andric     const char **OutMessage, const char **OutFilename, unsigned *OutLine,
685ffd83dbSDimitry Andric     unsigned *OutCol, char **OutMemoryAddr);
695ffd83dbSDimitry Andric }
7006c3fb27SDimitry Andric )";
715ffd83dbSDimitry Andric 
7206c3fb27SDimitry Andric static const char *ub_sanitizer_retrieve_report_data_command = R"(
7306c3fb27SDimitry Andric struct {
745ffd83dbSDimitry Andric   const char *issue_kind;
755ffd83dbSDimitry Andric   const char *message;
765ffd83dbSDimitry Andric   const char *filename;
775ffd83dbSDimitry Andric   unsigned line;
785ffd83dbSDimitry Andric   unsigned col;
795ffd83dbSDimitry Andric   char *memory_addr;
8006c3fb27SDimitry Andric } t;
815ffd83dbSDimitry Andric 
825ffd83dbSDimitry Andric __ubsan_get_current_report_data(&t.issue_kind, &t.message, &t.filename, &t.line,
835ffd83dbSDimitry Andric                                 &t.col, &t.memory_addr);
845ffd83dbSDimitry Andric t;
855ffd83dbSDimitry Andric )";
865ffd83dbSDimitry Andric 
RetrieveUnsigned(ValueObjectSP return_value_sp,ProcessSP process_sp,const std::string & expression_path)875ffd83dbSDimitry Andric static addr_t RetrieveUnsigned(ValueObjectSP return_value_sp,
885ffd83dbSDimitry Andric                                ProcessSP process_sp,
895ffd83dbSDimitry Andric                                const std::string &expression_path) {
905ffd83dbSDimitry Andric   return return_value_sp->GetValueForExpressionPath(expression_path.c_str())
915ffd83dbSDimitry Andric       ->GetValueAsUnsigned(0);
925ffd83dbSDimitry Andric }
935ffd83dbSDimitry Andric 
RetrieveString(ValueObjectSP return_value_sp,ProcessSP process_sp,const std::string & expression_path)945ffd83dbSDimitry Andric static std::string RetrieveString(ValueObjectSP return_value_sp,
955ffd83dbSDimitry Andric                                   ProcessSP process_sp,
965ffd83dbSDimitry Andric                                   const std::string &expression_path) {
975ffd83dbSDimitry Andric   addr_t ptr = RetrieveUnsigned(return_value_sp, process_sp, expression_path);
985ffd83dbSDimitry Andric   std::string str;
995ffd83dbSDimitry Andric   Status error;
1005ffd83dbSDimitry Andric   process_sp->ReadCStringFromMemory(ptr, str, error);
1015ffd83dbSDimitry Andric   return str;
1025ffd83dbSDimitry Andric }
1035ffd83dbSDimitry Andric 
RetrieveReportData(ExecutionContextRef exe_ctx_ref)1045ffd83dbSDimitry Andric StructuredData::ObjectSP InstrumentationRuntimeUBSan::RetrieveReportData(
1055ffd83dbSDimitry Andric     ExecutionContextRef exe_ctx_ref) {
1065ffd83dbSDimitry Andric   ProcessSP process_sp = GetProcessSP();
1075ffd83dbSDimitry Andric   if (!process_sp)
1085ffd83dbSDimitry Andric     return StructuredData::ObjectSP();
1095ffd83dbSDimitry Andric 
1105ffd83dbSDimitry Andric   ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();
11106c3fb27SDimitry Andric   StackFrameSP frame_sp =
11206c3fb27SDimitry Andric       thread_sp->GetSelectedFrame(DoNoSelectMostRelevantFrame);
1135ffd83dbSDimitry Andric   ModuleSP runtime_module_sp = GetRuntimeModuleSP();
1145ffd83dbSDimitry Andric   Target &target = process_sp->GetTarget();
1155ffd83dbSDimitry Andric 
1165ffd83dbSDimitry Andric   if (!frame_sp)
1175ffd83dbSDimitry Andric     return StructuredData::ObjectSP();
1185ffd83dbSDimitry Andric 
1195ffd83dbSDimitry Andric   StreamFileSP Stream = target.GetDebugger().GetOutputStreamSP();
1205ffd83dbSDimitry Andric 
1215ffd83dbSDimitry Andric   EvaluateExpressionOptions options;
1225ffd83dbSDimitry Andric   options.SetUnwindOnError(true);
1235ffd83dbSDimitry Andric   options.SetTryAllThreads(true);
1245ffd83dbSDimitry Andric   options.SetStopOthers(true);
1255ffd83dbSDimitry Andric   options.SetIgnoreBreakpoints(true);
1265ffd83dbSDimitry Andric   options.SetTimeout(process_sp->GetUtilityExpressionTimeout());
1275ffd83dbSDimitry Andric   options.SetPrefix(ub_sanitizer_retrieve_report_data_prefix);
1285ffd83dbSDimitry Andric   options.SetAutoApplyFixIts(false);
1295ffd83dbSDimitry Andric   options.SetLanguage(eLanguageTypeObjC_plus_plus);
1305ffd83dbSDimitry Andric 
1315ffd83dbSDimitry Andric   ValueObjectSP main_value;
1325ffd83dbSDimitry Andric   ExecutionContext exe_ctx;
1335ffd83dbSDimitry Andric   Status eval_error;
1345ffd83dbSDimitry Andric   frame_sp->CalculateExecutionContext(exe_ctx);
1355ffd83dbSDimitry Andric   ExpressionResults result = UserExpression::Evaluate(
1365ffd83dbSDimitry Andric       exe_ctx, options, ub_sanitizer_retrieve_report_data_command, "",
1375ffd83dbSDimitry Andric       main_value, eval_error);
1385ffd83dbSDimitry Andric   if (result != eExpressionCompleted) {
13981ad6265SDimitry Andric     StreamString ss;
14081ad6265SDimitry Andric     ss << "cannot evaluate UndefinedBehaviorSanitizer expression:\n";
14181ad6265SDimitry Andric     ss << eval_error.AsCString();
14281ad6265SDimitry Andric     Debugger::ReportWarning(ss.GetString().str(),
14381ad6265SDimitry Andric                             process_sp->GetTarget().GetDebugger().GetID());
1445ffd83dbSDimitry Andric     return StructuredData::ObjectSP();
1455ffd83dbSDimitry Andric   }
1465ffd83dbSDimitry Andric 
1475ffd83dbSDimitry Andric   // Gather the PCs of the user frames in the backtrace.
1485ffd83dbSDimitry Andric   StructuredData::Array *trace = new StructuredData::Array();
1495ffd83dbSDimitry Andric   auto trace_sp = StructuredData::ObjectSP(trace);
1505ffd83dbSDimitry Andric   for (unsigned I = 0; I < thread_sp->GetStackFrameCount(); ++I) {
151fe6060f1SDimitry Andric     const Address FCA = thread_sp->GetStackFrameAtIndex(I)
152fe6060f1SDimitry Andric                             ->GetFrameCodeAddressForSymbolication();
1535ffd83dbSDimitry Andric     if (FCA.GetModule() == runtime_module_sp) // Skip PCs from the runtime.
1545ffd83dbSDimitry Andric       continue;
1555ffd83dbSDimitry Andric 
1565ffd83dbSDimitry Andric     lldb::addr_t PC = FCA.GetLoadAddress(&target);
15706c3fb27SDimitry Andric     trace->AddIntegerItem(PC);
1585ffd83dbSDimitry Andric   }
1595ffd83dbSDimitry Andric 
1605ffd83dbSDimitry Andric   std::string IssueKind = RetrieveString(main_value, process_sp, ".issue_kind");
1615ffd83dbSDimitry Andric   std::string ErrMessage = RetrieveString(main_value, process_sp, ".message");
1625ffd83dbSDimitry Andric   std::string Filename = RetrieveString(main_value, process_sp, ".filename");
1635ffd83dbSDimitry Andric   unsigned Line = RetrieveUnsigned(main_value, process_sp, ".line");
1645ffd83dbSDimitry Andric   unsigned Col = RetrieveUnsigned(main_value, process_sp, ".col");
1655ffd83dbSDimitry Andric   uintptr_t MemoryAddr =
1665ffd83dbSDimitry Andric       RetrieveUnsigned(main_value, process_sp, ".memory_addr");
1675ffd83dbSDimitry Andric 
1685ffd83dbSDimitry Andric   auto *d = new StructuredData::Dictionary();
1695ffd83dbSDimitry Andric   auto dict_sp = StructuredData::ObjectSP(d);
1705ffd83dbSDimitry Andric   d->AddStringItem("instrumentation_class", "UndefinedBehaviorSanitizer");
1715ffd83dbSDimitry Andric   d->AddStringItem("description", IssueKind);
1725ffd83dbSDimitry Andric   d->AddStringItem("summary", ErrMessage);
1735ffd83dbSDimitry Andric   d->AddStringItem("filename", Filename);
1745ffd83dbSDimitry Andric   d->AddIntegerItem("line", Line);
1755ffd83dbSDimitry Andric   d->AddIntegerItem("col", Col);
1765ffd83dbSDimitry Andric   d->AddIntegerItem("memory_address", MemoryAddr);
1775ffd83dbSDimitry Andric   d->AddIntegerItem("tid", thread_sp->GetID());
1785ffd83dbSDimitry Andric   d->AddItem("trace", trace_sp);
1795ffd83dbSDimitry Andric   return dict_sp;
1805ffd83dbSDimitry Andric }
1815ffd83dbSDimitry Andric 
GetStopReasonDescription(StructuredData::ObjectSP report)1825ffd83dbSDimitry Andric static std::string GetStopReasonDescription(StructuredData::ObjectSP report) {
1835ffd83dbSDimitry Andric   llvm::StringRef stop_reason_description_ref;
1845ffd83dbSDimitry Andric   report->GetAsDictionary()->GetValueForKeyAsString(
1855ffd83dbSDimitry Andric       "description", stop_reason_description_ref);
1865ffd83dbSDimitry Andric   std::string stop_reason_description =
1875ffd83dbSDimitry Andric       std::string(stop_reason_description_ref);
1885ffd83dbSDimitry Andric 
1895ffd83dbSDimitry Andric   if (!stop_reason_description.size()) {
1905ffd83dbSDimitry Andric     stop_reason_description = "Undefined behavior detected";
1915ffd83dbSDimitry Andric   } else {
1925ffd83dbSDimitry Andric     stop_reason_description[0] = toupper(stop_reason_description[0]);
1935ffd83dbSDimitry Andric     for (unsigned I = 1; I < stop_reason_description.size(); ++I)
1945ffd83dbSDimitry Andric       if (stop_reason_description[I] == '-')
1955ffd83dbSDimitry Andric         stop_reason_description[I] = ' ';
1965ffd83dbSDimitry Andric   }
1975ffd83dbSDimitry Andric   return stop_reason_description;
1985ffd83dbSDimitry Andric }
1995ffd83dbSDimitry Andric 
NotifyBreakpointHit(void * baton,StoppointCallbackContext * context,user_id_t break_id,user_id_t break_loc_id)2005ffd83dbSDimitry Andric bool InstrumentationRuntimeUBSan::NotifyBreakpointHit(
2015ffd83dbSDimitry Andric     void *baton, StoppointCallbackContext *context, user_id_t break_id,
2025ffd83dbSDimitry Andric     user_id_t break_loc_id) {
2035ffd83dbSDimitry Andric   assert(baton && "null baton");
2045ffd83dbSDimitry Andric   if (!baton)
2055ffd83dbSDimitry Andric     return false; ///< false => resume execution.
2065ffd83dbSDimitry Andric 
2075ffd83dbSDimitry Andric   InstrumentationRuntimeUBSan *const instance =
2085ffd83dbSDimitry Andric       static_cast<InstrumentationRuntimeUBSan *>(baton);
2095ffd83dbSDimitry Andric 
2105ffd83dbSDimitry Andric   ProcessSP process_sp = instance->GetProcessSP();
2115ffd83dbSDimitry Andric   ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
2125ffd83dbSDimitry Andric   if (!process_sp || !thread_sp ||
2135ffd83dbSDimitry Andric       process_sp != context->exe_ctx_ref.GetProcessSP())
2145ffd83dbSDimitry Andric     return false;
2155ffd83dbSDimitry Andric 
2165ffd83dbSDimitry Andric   if (process_sp->GetModIDRef().IsLastResumeForUserExpression())
2175ffd83dbSDimitry Andric     return false;
2185ffd83dbSDimitry Andric 
2195ffd83dbSDimitry Andric   StructuredData::ObjectSP report =
2205ffd83dbSDimitry Andric       instance->RetrieveReportData(context->exe_ctx_ref);
2215ffd83dbSDimitry Andric 
2225ffd83dbSDimitry Andric   if (report) {
2235ffd83dbSDimitry Andric     thread_sp->SetStopInfo(
2245ffd83dbSDimitry Andric         InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData(
2255ffd83dbSDimitry Andric             *thread_sp, GetStopReasonDescription(report), report));
2265ffd83dbSDimitry Andric     return true;
2275ffd83dbSDimitry Andric   }
2285ffd83dbSDimitry Andric 
2295ffd83dbSDimitry Andric   return false;
2305ffd83dbSDimitry Andric }
2315ffd83dbSDimitry Andric 
2325ffd83dbSDimitry Andric const RegularExpression &
GetPatternForRuntimeLibrary()2335ffd83dbSDimitry Andric InstrumentationRuntimeUBSan::GetPatternForRuntimeLibrary() {
2345ffd83dbSDimitry Andric   static RegularExpression regex(llvm::StringRef("libclang_rt\\.(a|t|ub)san_"));
2355ffd83dbSDimitry Andric   return regex;
2365ffd83dbSDimitry Andric }
2375ffd83dbSDimitry Andric 
CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp)2385ffd83dbSDimitry Andric bool InstrumentationRuntimeUBSan::CheckIfRuntimeIsValid(
2395ffd83dbSDimitry Andric     const lldb::ModuleSP module_sp) {
2405ffd83dbSDimitry Andric   static ConstString ubsan_test_sym("__ubsan_on_report");
2415ffd83dbSDimitry Andric   const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(
2425ffd83dbSDimitry Andric       ubsan_test_sym, lldb::eSymbolTypeAny);
2435ffd83dbSDimitry Andric   return symbol != nullptr;
2445ffd83dbSDimitry Andric }
2455ffd83dbSDimitry Andric 
2465ffd83dbSDimitry Andric // FIXME: Factor out all the logic we have in common with the {a,t}san plugins.
Activate()2475ffd83dbSDimitry Andric void InstrumentationRuntimeUBSan::Activate() {
2485ffd83dbSDimitry Andric   if (IsActive())
2495ffd83dbSDimitry Andric     return;
2505ffd83dbSDimitry Andric 
2515ffd83dbSDimitry Andric   ProcessSP process_sp = GetProcessSP();
2525ffd83dbSDimitry Andric   if (!process_sp)
2535ffd83dbSDimitry Andric     return;
2545ffd83dbSDimitry Andric 
2555ffd83dbSDimitry Andric   ModuleSP runtime_module_sp = GetRuntimeModuleSP();
2565ffd83dbSDimitry Andric 
2575ffd83dbSDimitry Andric   ConstString symbol_name("__ubsan_on_report");
2585ffd83dbSDimitry Andric   const Symbol *symbol = runtime_module_sp->FindFirstSymbolWithNameAndType(
2595ffd83dbSDimitry Andric       symbol_name, eSymbolTypeCode);
2605ffd83dbSDimitry Andric 
2615ffd83dbSDimitry Andric   if (symbol == nullptr)
2625ffd83dbSDimitry Andric     return;
2635ffd83dbSDimitry Andric 
2645ffd83dbSDimitry Andric   if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
2655ffd83dbSDimitry Andric     return;
2665ffd83dbSDimitry Andric 
2675ffd83dbSDimitry Andric   Target &target = process_sp->GetTarget();
2685ffd83dbSDimitry Andric   addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
2695ffd83dbSDimitry Andric 
2705ffd83dbSDimitry Andric   if (symbol_address == LLDB_INVALID_ADDRESS)
2715ffd83dbSDimitry Andric     return;
2725ffd83dbSDimitry Andric 
2735ffd83dbSDimitry Andric   Breakpoint *breakpoint =
2745ffd83dbSDimitry Andric       process_sp->GetTarget()
2755ffd83dbSDimitry Andric           .CreateBreakpoint(symbol_address, /*internal=*/true,
2765ffd83dbSDimitry Andric                             /*hardware=*/false)
2775ffd83dbSDimitry Andric           .get();
278bdd1243dSDimitry Andric   const bool sync = false;
2795ffd83dbSDimitry Andric   breakpoint->SetCallback(InstrumentationRuntimeUBSan::NotifyBreakpointHit,
280bdd1243dSDimitry Andric                           this, sync);
2815ffd83dbSDimitry Andric   breakpoint->SetBreakpointKind("undefined-behavior-sanitizer-report");
2825ffd83dbSDimitry Andric   SetBreakpointID(breakpoint->GetID());
2835ffd83dbSDimitry Andric 
2845ffd83dbSDimitry Andric   SetActive(true);
2855ffd83dbSDimitry Andric }
2865ffd83dbSDimitry Andric 
Deactivate()2875ffd83dbSDimitry Andric void InstrumentationRuntimeUBSan::Deactivate() {
2885ffd83dbSDimitry Andric   SetActive(false);
2895ffd83dbSDimitry Andric 
2905ffd83dbSDimitry Andric   auto BID = GetBreakpointID();
2915ffd83dbSDimitry Andric   if (BID == LLDB_INVALID_BREAK_ID)
2925ffd83dbSDimitry Andric     return;
2935ffd83dbSDimitry Andric 
2945ffd83dbSDimitry Andric   if (ProcessSP process_sp = GetProcessSP()) {
2955ffd83dbSDimitry Andric     process_sp->GetTarget().RemoveBreakpointByID(BID);
2965ffd83dbSDimitry Andric     SetBreakpointID(LLDB_INVALID_BREAK_ID);
2975ffd83dbSDimitry Andric   }
2985ffd83dbSDimitry Andric }
2995ffd83dbSDimitry Andric 
3005ffd83dbSDimitry Andric lldb::ThreadCollectionSP
GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info)3015ffd83dbSDimitry Andric InstrumentationRuntimeUBSan::GetBacktracesFromExtendedStopInfo(
3025ffd83dbSDimitry Andric     StructuredData::ObjectSP info) {
3035ffd83dbSDimitry Andric   ThreadCollectionSP threads;
3045ffd83dbSDimitry Andric   threads = std::make_shared<ThreadCollection>();
3055ffd83dbSDimitry Andric 
3065ffd83dbSDimitry Andric   ProcessSP process_sp = GetProcessSP();
3075ffd83dbSDimitry Andric 
3085ffd83dbSDimitry Andric   if (info->GetObjectForDotSeparatedPath("instrumentation_class")
3095ffd83dbSDimitry Andric           ->GetStringValue() != "UndefinedBehaviorSanitizer")
3105ffd83dbSDimitry Andric     return threads;
3115ffd83dbSDimitry Andric 
3125ffd83dbSDimitry Andric   std::vector<lldb::addr_t> PCs;
3135ffd83dbSDimitry Andric   auto trace = info->GetObjectForDotSeparatedPath("trace")->GetAsArray();
3145ffd83dbSDimitry Andric   trace->ForEach([&PCs](StructuredData::Object *PC) -> bool {
31506c3fb27SDimitry Andric     PCs.push_back(PC->GetUnsignedIntegerValue());
3165ffd83dbSDimitry Andric     return true;
3175ffd83dbSDimitry Andric   });
3185ffd83dbSDimitry Andric 
3195ffd83dbSDimitry Andric   if (PCs.empty())
3205ffd83dbSDimitry Andric     return threads;
3215ffd83dbSDimitry Andric 
3225ffd83dbSDimitry Andric   StructuredData::ObjectSP thread_id_obj =
3235ffd83dbSDimitry Andric       info->GetObjectForDotSeparatedPath("tid");
32406c3fb27SDimitry Andric   tid_t tid = thread_id_obj ? thread_id_obj->GetUnsignedIntegerValue() : 0;
3255ffd83dbSDimitry Andric 
326fe6060f1SDimitry Andric   // We gather symbolication addresses above, so no need for HistoryThread to
327fe6060f1SDimitry Andric   // try to infer the call addresses.
328fe6060f1SDimitry Andric   bool pcs_are_call_addresses = true;
329fe6060f1SDimitry Andric   ThreadSP new_thread_sp = std::make_shared<HistoryThread>(
330fe6060f1SDimitry Andric       *process_sp, tid, PCs, pcs_are_call_addresses);
3315ffd83dbSDimitry Andric   std::string stop_reason_description = GetStopReasonDescription(info);
3325ffd83dbSDimitry Andric   new_thread_sp->SetName(stop_reason_description.c_str());
3335ffd83dbSDimitry Andric 
3345ffd83dbSDimitry Andric   // Save this in the Process' ExtendedThreadList so a strong pointer retains
3355ffd83dbSDimitry Andric   // the object
3365ffd83dbSDimitry Andric   process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
3375ffd83dbSDimitry Andric   threads->AddThread(new_thread_sp);
3385ffd83dbSDimitry Andric 
3395ffd83dbSDimitry Andric   return threads;
3405ffd83dbSDimitry Andric }
341