@@ -38,6 +38,49 @@ void OperatingSystemSwiftTasks::Terminate() {
38
38
PluginManager::UnregisterPlugin (CreateInstance);
39
39
}
40
40
41
+ // / A wrapper around ThreadMemory providing lazy name evaluation, as this is
42
+ // / expensive to compute for Swift Task.
43
+ class SwiftTaskThreadMemory : public ThreadMemory {
44
+ public:
45
+ SwiftTaskThreadMemory (lldb_private::Process &process, lldb::tid_t tid,
46
+ lldb::addr_t register_data_addr)
47
+ : ThreadMemory(process, tid, register_data_addr) {}
48
+
49
+ void SetTaskAddr (lldb::addr_t task_addr) { m_task_addr = task_addr; }
50
+
51
+ const char *GetName () override {
52
+ if (m_task_name.empty ())
53
+ m_task_name = FindTaskName ();
54
+ return m_task_name.c_str ();
55
+ }
56
+
57
+ private:
58
+ std::string GetDefaultTaskName () const {
59
+ return llvm::formatv (" Task {0}" , GetID ());
60
+ }
61
+
62
+ // / If possible, read a user-provided task name from memory, otherwise use a
63
+ // / default name. This never returns an empty string.
64
+ std::string FindTaskName () const {
65
+ llvm::Expected<std::optional<std::string>> task_name =
66
+ GetTaskName (m_task_addr, *GetProcess ());
67
+ if (auto err = task_name.takeError ()) {
68
+ LLDB_LOG_ERROR (GetLog (LLDBLog::OS), std::move (err),
69
+ " OperatingSystemSwiftTasks: failed while looking for name "
70
+ " of task {1:x}: {0}" ,
71
+ m_task_addr);
72
+ return GetDefaultTaskName ();
73
+ }
74
+
75
+ if (!task_name->has_value ())
76
+ return GetDefaultTaskName ();
77
+ return llvm::formatv (" {0} (Task {1})" , *task_name, GetID ());
78
+ }
79
+
80
+ std::string m_task_name = " " ;
81
+ lldb::addr_t m_task_addr = LLDB_INVALID_ADDRESS;
82
+ };
83
+
41
84
OperatingSystem *OperatingSystemSwiftTasks::CreateInstance (Process *process,
42
85
bool force) {
43
86
if (!process || !process->GetTarget ().GetSwiftUseTasksPlugin ())
@@ -78,9 +121,9 @@ OperatingSystemSwiftTasks::OperatingSystemSwiftTasks(
78
121
lldb_private::Process &process)
79
122
: OperatingSystem(&process) {}
80
123
81
- ThreadSP OperatingSystemSwiftTasks::FindOrCreateSwiftThread (
82
- ThreadList &old_thread_list, uint64_t task_id ,
83
- std::optional<std::string> task_name ) {
124
+ ThreadSP
125
+ OperatingSystemSwiftTasks::FindOrCreateSwiftThread ( ThreadList &old_thread_list,
126
+ uint64_t task_id ) {
84
127
// Mask higher bits to avoid conflicts with core thread IDs.
85
128
uint64_t masked_task_id = 0x0000000f00000000 | task_id;
86
129
@@ -89,15 +132,8 @@ ThreadSP OperatingSystemSwiftTasks::FindOrCreateSwiftThread(
89
132
IsOperatingSystemPluginThread (old_thread))
90
133
return old_thread;
91
134
92
- std::string name;
93
- if (task_name)
94
- name = llvm::formatv (" {0} (Task {1})" , *task_name, task_id);
95
- else
96
- name = llvm::formatv (" Task {0}" , task_id);
97
-
98
- return std::make_shared<ThreadMemoryProvidingName>(*m_process, masked_task_id,
99
- /* register_data_addr*/ 0 ,
100
- name);
135
+ return std::make_shared<SwiftTaskThreadMemory>(*m_process, masked_task_id,
136
+ /* register_data_addr*/ 0 );
101
137
}
102
138
103
139
static std::optional<addr_t > FindTaskAddress (TaskInspector &task_inspector,
@@ -132,19 +168,6 @@ static std::optional<uint64_t> FindTaskId(addr_t task_addr, Process &process) {
132
168
return task_id;
133
169
}
134
170
135
- static std::optional<std::string> FindTaskName (addr_t task_addr,
136
- Process &process) {
137
- auto task_name_or_err = GetTaskName (task_addr, process);
138
- if (auto err = task_name_or_err.takeError ()) {
139
- LLDB_LOG_ERROR (GetLog (LLDBLog::OS), std::move (err),
140
- " OperatingSystemSwiftTasks: failed while looking for name "
141
- " of task {1:x}: {0}" ,
142
- task_addr);
143
- return {};
144
- }
145
- return *task_name_or_err;
146
- }
147
-
148
171
bool OperatingSystemSwiftTasks::UpdateThreadList (ThreadList &old_thread_list,
149
172
ThreadList &core_thread_list,
150
173
ThreadList &new_thread_list) {
@@ -173,9 +196,9 @@ bool OperatingSystemSwiftTasks::UpdateThreadList(ThreadList &old_thread_list,
173
196
continue ;
174
197
}
175
198
176
- ThreadSP swift_thread = FindOrCreateSwiftThread (
177
- old_thread_list, *task_id, FindTaskName (*task_addr, *m_process));
199
+ ThreadSP swift_thread = FindOrCreateSwiftThread (old_thread_list, *task_id);
178
200
swift_thread->SetBackingThread (real_thread);
201
+ static_cast <SwiftTaskThreadMemory &>(*swift_thread).SetTaskAddr (*task_addr);
179
202
new_thread_list.AddThread (swift_thread);
180
203
LLDB_LOGF (log,
181
204
" OperatingSystemSwiftTasks: mapping thread IDs: %" PRIx64
0 commit comments