Skip to content

Commit 1b7c9ea

Browse files
committed
[lldb] Store StackFrameRecognizers in the target instead of a global list
Summary: Currently the frame recognizers are stored in a global list (the list in the StackFrameRecognizersManagerImpl singleton to be precise). All commands and plugins that modify the list are just modifying that global list of recognizers which is shared by all Target and Debugger instances. This is clearly against the idea of LLDB being usable as a library and it also leads to some very obscure errors as now multiple tests are sharing the used frame recognizers. For example D83400 is currently failing as it reorders some test_ functions which permanently changes the frame recognizers of all debuggers/targets. As all frame recognizers are also initialized in a 'once' guard, it's also impossible to every restore back the original frame recognizers once they are deleted in a process. This patch just moves the frame recognizers into the current target. This seems the way everyone assumes the system works as for example the assert frame recognizers is using the current target to find the function/so-name to look for (which only works if the recognizers are stored in the target). Reviewers: jingham, mib Reviewed By: jingham, mib Subscribers: MrHate, JDevlieghere Differential Revision: https://reviews.llvm.org/D83757
1 parent f5db241 commit 1b7c9ea

File tree

11 files changed

+233
-213
lines changed

11 files changed

+233
-213
lines changed

lldb/include/lldb/Target/StackFrameRecognizer.h

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "lldb/lldb-private-forward.h"
1818
#include "lldb/lldb-public.h"
1919

20+
#include <vector>
21+
2022
namespace lldb_private {
2123

2224
/// \class RecognizedStackFrame
@@ -95,37 +97,45 @@ class ScriptedStackFrameRecognizer : public StackFrameRecognizer {
9597
operator=(const ScriptedStackFrameRecognizer &) = delete;
9698
};
9799

98-
/// \class StackFrameRecognizerManager
99-
///
100-
/// Static class that provides a registry of known stack frame recognizers.
101-
/// Has static methods to add, enumerate, remove, query and invoke recognizers.
102-
100+
/// Class that provides a registry of known stack frame recognizers.
103101
class StackFrameRecognizerManager {
104102
public:
105-
static void AddRecognizer(lldb::StackFrameRecognizerSP recognizer,
106-
ConstString module,
107-
llvm::ArrayRef<ConstString> symbols,
108-
bool first_instruction_only = true);
103+
void AddRecognizer(lldb::StackFrameRecognizerSP recognizer,
104+
ConstString module, llvm::ArrayRef<ConstString> symbols,
105+
bool first_instruction_only = true);
106+
107+
void AddRecognizer(lldb::StackFrameRecognizerSP recognizer,
108+
lldb::RegularExpressionSP module,
109+
lldb::RegularExpressionSP symbol,
110+
bool first_instruction_only = true);
109111

110-
static void AddRecognizer(lldb::StackFrameRecognizerSP recognizer,
111-
lldb::RegularExpressionSP module,
112-
lldb::RegularExpressionSP symbol,
113-
bool first_instruction_only = true);
112+
void ForEach(std::function<
113+
void(uint32_t recognizer_id, std::string recognizer_name,
114+
std::string module, llvm::ArrayRef<ConstString> symbols,
115+
bool regexp)> const &callback);
114116

115-
static void
116-
ForEach(std::function<void(uint32_t recognizer_id,
117-
std::string recognizer_name, std::string module,
118-
llvm::ArrayRef<ConstString> symbols,
119-
bool regexp)> const &callback);
117+
bool RemoveRecognizerWithID(uint32_t recognizer_id);
120118

121-
static bool RemoveRecognizerWithID(uint32_t recognizer_id);
119+
void RemoveAllRecognizers();
122120

123-
static void RemoveAllRecognizers();
121+
lldb::StackFrameRecognizerSP GetRecognizerForFrame(lldb::StackFrameSP frame);
124122

125-
static lldb::StackFrameRecognizerSP GetRecognizerForFrame(
126-
lldb::StackFrameSP frame);
123+
lldb::RecognizedStackFrameSP RecognizeFrame(lldb::StackFrameSP frame);
124+
125+
private:
126+
struct RegisteredEntry {
127+
uint32_t recognizer_id;
128+
bool deleted;
129+
lldb::StackFrameRecognizerSP recognizer;
130+
bool is_regexp;
131+
ConstString module;
132+
lldb::RegularExpressionSP module_regexp;
133+
std::vector<ConstString> symbols;
134+
lldb::RegularExpressionSP symbol_regexp;
135+
bool first_instruction_only;
136+
};
127137

128-
static lldb::RecognizedStackFrameSP RecognizeFrame(lldb::StackFrameSP frame);
138+
std::deque<RegisteredEntry> m_recognizers;
129139
};
130140

131141
/// \class ValueObjectRecognizerSynthesizedValue

lldb/include/lldb/Target/Target.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,10 @@ class Target : public std::enable_shared_from_this<Target>,
12511251

12521252
void SetREPL(lldb::LanguageType language, lldb::REPLSP repl_sp);
12531253

1254+
StackFrameRecognizerManager &GetFrameRecognizerManager() {
1255+
return *m_frame_recognizer_manager_up;
1256+
}
1257+
12541258
protected:
12551259
/// Implementing of ModuleList::Notifier.
12561260

@@ -1325,6 +1329,8 @@ class Target : public std::enable_shared_from_this<Target>,
13251329
bool m_suppress_stop_hooks;
13261330
bool m_is_dummy_target;
13271331
unsigned m_next_persistent_variable_index = 0;
1332+
/// Stores the frame recognizers of this target.
1333+
lldb::StackFrameRecognizerManagerUP m_frame_recognizer_manager_up;
13281334

13291335
static void ImageSearchPathsChanged(const PathMappingList &path_list,
13301336
void *baton);

lldb/include/lldb/lldb-forward.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ typedef std::weak_ptr<lldb_private::StackFrame> StackFrameWP;
402402
typedef std::shared_ptr<lldb_private::StackFrameList> StackFrameListSP;
403403
typedef std::shared_ptr<lldb_private::StackFrameRecognizer>
404404
StackFrameRecognizerSP;
405+
typedef std::unique_ptr<lldb_private::StackFrameRecognizerManager>
406+
StackFrameRecognizerManagerUP;
405407
typedef std::shared_ptr<lldb_private::StopInfo> StopInfoSP;
406408
typedef std::shared_ptr<lldb_private::StoppointLocation> StoppointLocationSP;
407409
typedef std::shared_ptr<lldb_private::Stream> StreamSP;

lldb/source/Commands/CommandObjectFrame.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -898,12 +898,14 @@ bool CommandObjectFrameRecognizerAdd::DoExecute(Args &command,
898898
RegularExpressionSP(new RegularExpression(m_options.m_module));
899899
auto func =
900900
RegularExpressionSP(new RegularExpression(m_options.m_symbols.front()));
901-
StackFrameRecognizerManager::AddRecognizer(recognizer_sp, module, func);
901+
GetSelectedOrDummyTarget().GetFrameRecognizerManager().AddRecognizer(
902+
recognizer_sp, module, func);
902903
} else {
903904
auto module = ConstString(m_options.m_module);
904905
std::vector<ConstString> symbols(m_options.m_symbols.begin(),
905906
m_options.m_symbols.end());
906-
StackFrameRecognizerManager::AddRecognizer(recognizer_sp, module, symbols);
907+
GetSelectedOrDummyTarget().GetFrameRecognizerManager().AddRecognizer(
908+
recognizer_sp, module, symbols);
907909
}
908910
#endif
909911

@@ -921,7 +923,9 @@ class CommandObjectFrameRecognizerClear : public CommandObjectParsed {
921923

922924
protected:
923925
bool DoExecute(Args &command, CommandReturnObject &result) override {
924-
StackFrameRecognizerManager::RemoveAllRecognizers();
926+
GetSelectedOrDummyTarget()
927+
.GetFrameRecognizerManager()
928+
.RemoveAllRecognizers();
925929
result.SetStatus(eReturnStatusSuccessFinishResult);
926930
return result.Succeeded();
927931
}
@@ -941,7 +945,7 @@ class CommandObjectFrameRecognizerDelete : public CommandObjectParsed {
941945
if (request.GetCursorIndex() != 0)
942946
return;
943947

944-
StackFrameRecognizerManager::ForEach(
948+
GetSelectedOrDummyTarget().GetFrameRecognizerManager().ForEach(
945949
[&request](uint32_t rid, std::string rname, std::string module,
946950
llvm::ArrayRef<lldb_private::ConstString> symbols,
947951
bool regexp) {
@@ -973,7 +977,9 @@ class CommandObjectFrameRecognizerDelete : public CommandObjectParsed {
973977
return false;
974978
}
975979

976-
StackFrameRecognizerManager::RemoveAllRecognizers();
980+
GetSelectedOrDummyTarget()
981+
.GetFrameRecognizerManager()
982+
.RemoveAllRecognizers();
977983
result.SetStatus(eReturnStatusSuccessFinishResult);
978984
return result.Succeeded();
979985
}
@@ -993,7 +999,9 @@ class CommandObjectFrameRecognizerDelete : public CommandObjectParsed {
993999
return false;
9941000
}
9951001

996-
StackFrameRecognizerManager::RemoveRecognizerWithID(recognizer_id);
1002+
GetSelectedOrDummyTarget()
1003+
.GetFrameRecognizerManager()
1004+
.RemoveRecognizerWithID(recognizer_id);
9971005
result.SetStatus(eReturnStatusSuccessFinishResult);
9981006
return result.Succeeded();
9991007
}
@@ -1011,7 +1019,7 @@ class CommandObjectFrameRecognizerList : public CommandObjectParsed {
10111019
protected:
10121020
bool DoExecute(Args &command, CommandReturnObject &result) override {
10131021
bool any_printed = false;
1014-
StackFrameRecognizerManager::ForEach(
1022+
GetSelectedOrDummyTarget().GetFrameRecognizerManager().ForEach(
10151023
[&result, &any_printed](
10161024
uint32_t recognizer_id, std::string name, std::string module,
10171025
llvm::ArrayRef<ConstString> symbols, bool regexp) {
@@ -1106,8 +1114,9 @@ class CommandObjectFrameRecognizerInfo : public CommandObjectParsed {
11061114
return false;
11071115
}
11081116

1109-
auto recognizer =
1110-
StackFrameRecognizerManager::GetRecognizerForFrame(frame_sp);
1117+
auto recognizer = GetSelectedOrDummyTarget()
1118+
.GetFrameRecognizerManager()
1119+
.GetRecognizerForFrame(frame_sp);
11111120

11121121
Stream &output_stream = result.GetOutputStream();
11131122
output_stream.Printf("frame %d ", frame_index);

lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ ExtractRuntimeGlobalSymbol(Process *process, ConstString name,
407407
}
408408
}
409409

410-
static void RegisterObjCExceptionRecognizer();
410+
static void RegisterObjCExceptionRecognizer(Process *process);
411411

412412
AppleObjCRuntimeV2::AppleObjCRuntimeV2(Process *process,
413413
const ModuleSP &objc_module_sp)
@@ -429,7 +429,7 @@ AppleObjCRuntimeV2::AppleObjCRuntimeV2(Process *process,
429429
m_has_object_getClass =
430430
(objc_module_sp->FindFirstSymbolWithNameAndType(
431431
g_gdb_object_getClass, eSymbolTypeCode) != nullptr);
432-
RegisterObjCExceptionRecognizer();
432+
RegisterObjCExceptionRecognizer(process);
433433
}
434434

435435
bool AppleObjCRuntimeV2::GetDynamicTypeAndAddress(
@@ -2711,16 +2711,14 @@ class ObjCExceptionThrowFrameRecognizer : public StackFrameRecognizer {
27112711
};
27122712
};
27132713

2714-
static void RegisterObjCExceptionRecognizer() {
2715-
static llvm::once_flag g_once_flag;
2716-
llvm::call_once(g_once_flag, []() {
2717-
FileSpec module;
2718-
ConstString function;
2719-
std::tie(module, function) = AppleObjCRuntime::GetExceptionThrowLocation();
2720-
std::vector<ConstString> symbols = {function};
2721-
StackFrameRecognizerManager::AddRecognizer(
2722-
StackFrameRecognizerSP(new ObjCExceptionThrowFrameRecognizer()),
2723-
module.GetFilename(), symbols,
2724-
/*first_instruction_only*/ true);
2725-
});
2714+
static void RegisterObjCExceptionRecognizer(Process *process) {
2715+
FileSpec module;
2716+
ConstString function;
2717+
std::tie(module, function) = AppleObjCRuntime::GetExceptionThrowLocation();
2718+
std::vector<ConstString> symbols = {function};
2719+
2720+
process->GetTarget().GetFrameRecognizerManager().AddRecognizer(
2721+
StackFrameRecognizerSP(new ObjCExceptionThrowFrameRecognizer()),
2722+
module.GetFilename(), symbols,
2723+
/*first_instruction_only*/ true);
27262724
}

lldb/source/Target/AssertFrameRecognizer.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,17 @@ bool GetAssertLocation(llvm::Triple::OSType os, SymbolLocation &location) {
8686
}
8787

8888
void RegisterAssertFrameRecognizer(Process *process) {
89-
static llvm::once_flag g_once_flag;
90-
llvm::call_once(g_once_flag, [process]() {
91-
Target &target = process->GetTarget();
92-
llvm::Triple::OSType os = target.GetArchitecture().GetTriple().getOS();
93-
SymbolLocation location;
94-
95-
if (!GetAbortLocation(os, location))
96-
return;
97-
98-
StackFrameRecognizerManager::AddRecognizer(
99-
StackFrameRecognizerSP(new AssertFrameRecognizer()),
100-
location.module_spec.GetFilename(), location.symbols,
101-
/*first_instruction_only*/ false);
102-
});
89+
Target &target = process->GetTarget();
90+
llvm::Triple::OSType os = target.GetArchitecture().GetTriple().getOS();
91+
SymbolLocation location;
92+
93+
if (!GetAbortLocation(os, location))
94+
return;
95+
96+
target.GetFrameRecognizerManager().AddRecognizer(
97+
StackFrameRecognizerSP(new AssertFrameRecognizer()),
98+
location.module_spec.GetFilename(), location.symbols,
99+
/*first_instruction_only*/ false);
103100
}
104101

105102
} // namespace lldb_private

lldb/source/Target/StackFrame.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,8 +1956,11 @@ bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source,
19561956

19571957
RecognizedStackFrameSP StackFrame::GetRecognizedFrame() {
19581958
if (!m_recognized_frame_sp) {
1959-
m_recognized_frame_sp =
1960-
StackFrameRecognizerManager::RecognizeFrame(CalculateStackFrame());
1959+
m_recognized_frame_sp = GetThread()
1960+
->GetProcess()
1961+
->GetTarget()
1962+
.GetFrameRecognizerManager()
1963+
.RecognizeFrame(CalculateStackFrame());
19611964
}
19621965
return m_recognized_frame_sp;
19631966
}

0 commit comments

Comments
 (0)