1 //===-- InstrumentationRuntimeStopInfo.cpp --------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "lldb/Target/InstrumentationRuntimeStopInfo.h"
10
11 #include "lldb/Core/Module.h"
12 #include "lldb/Target/InstrumentationRuntime.h"
13 #include "lldb/Target/Process.h"
14 #include "lldb/lldb-enumerations.h"
15 #include "lldb/lldb-private.h"
16
17 using namespace lldb;
18 using namespace lldb_private;
19
IsStoppedInDarwinSanitizer(Thread & thread,Module & module)20 static bool IsStoppedInDarwinSanitizer(Thread &thread, Module &module) {
21 return module.GetFileSpec().GetFilename().GetStringRef().starts_with(
22 "libclang_rt.");
23 }
24
InstrumentationRuntimeStopInfo(Thread & thread,std::string description,StructuredData::ObjectSP additional_data)25 InstrumentationRuntimeStopInfo::InstrumentationRuntimeStopInfo(
26 Thread &thread, std::string description,
27 StructuredData::ObjectSP additional_data)
28 : StopInfo(thread, 0) {
29 m_extended_info = additional_data;
30 m_description = description;
31 }
32
GetDescription()33 const char *InstrumentationRuntimeStopInfo::GetDescription() {
34 return m_description.c_str();
35 }
36
37 StopInfoSP
CreateStopReasonWithInstrumentationData(Thread & thread,std::string description,StructuredData::ObjectSP additionalData)38 InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData(
39 Thread &thread, std::string description,
40 StructuredData::ObjectSP additionalData) {
41 return StopInfoSP(
42 new InstrumentationRuntimeStopInfo(thread, description, additionalData));
43 }
44
45 std::optional<uint32_t>
GetSuggestedStackFrameIndex(bool inlined_stack)46 InstrumentationRuntimeStopInfo::GetSuggestedStackFrameIndex(
47 bool inlined_stack) {
48 ThreadSP thread_sp = GetThread();
49 if (!thread_sp)
50 return std::nullopt;
51
52 // Defensive upper-bound of when we stop walking up the frames in
53 // case we somehow ended up looking at an infinite recursion.
54 constexpr size_t max_stack_depth = 128;
55
56 // Start at parent frame.
57 size_t stack_idx = 1;
58 StackFrameSP most_relevant_frame_sp =
59 thread_sp->GetStackFrameAtIndex(stack_idx);
60
61 while (most_relevant_frame_sp && stack_idx <= max_stack_depth) {
62 auto const &sc =
63 most_relevant_frame_sp->GetSymbolContext(lldb::eSymbolContextModule);
64
65 if (!sc.module_sp)
66 return std::nullopt;
67
68 // Found a frame outside of the sanitizer runtime libraries.
69 // That's the one we want to display.
70 if (!IsStoppedInDarwinSanitizer(*thread_sp, *sc.module_sp))
71 return stack_idx;
72
73 ++stack_idx;
74 most_relevant_frame_sp = thread_sp->GetStackFrameAtIndex(stack_idx);
75 }
76
77 return stack_idx;
78 }
79