Skip to content

Commit 56e929b

Browse files
felipepiovezankastiglione
authored andcommitted
[lldb][swift] Add helper function to find Task addresses in TLS
(cherry picked from commit a267f0f947abd97f1b5911bfd6a7be83317aed16)
1 parent 6f7170a commit 56e929b

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "lldb/Utility/LLDBLog.h"
4444
#include "lldb/Utility/Log.h"
4545
#include "lldb/Utility/OptionParsing.h"
46+
#include "lldb/Utility/StructuredData.h"
4647
#include "lldb/Utility/Timer.h"
4748
#include "lldb/ValueObject/ValueObject.h"
4849
#include "lldb/ValueObject/ValueObjectCast.h"
@@ -52,8 +53,9 @@
5253
#include "lldb/lldb-enumerations.h"
5354
#include "swift/AST/ASTMangler.h"
5455
#include "swift/Demangling/Demangle.h"
55-
#include "swift/RemoteInspection/ReflectionContext.h"
5656
#include "swift/RemoteAST/RemoteAST.h"
57+
#include "swift/RemoteInspection/ReflectionContext.h"
58+
#include "swift/Threading/ThreadLocalStorage.h"
5759

5860
#include "clang/AST/ASTContext.h"
5961
#include "clang/AST/DeclCXX.h"
@@ -2639,4 +2641,31 @@ std::optional<lldb::addr_t> SwiftLanguageRuntime::TrySkipVirtualParentProlog(
26392641
: sc.function->GetPrologueByteSize();
26402642
return pc_value + prologue_size;
26412643
}
2644+
2645+
std::optional<lldb::addr_t> GetTaskAddrFromThreadLocalStorage(Thread &thread) {
2646+
// Compute the thread local storage address for this thread.
2647+
StructuredData::ObjectSP info_root_sp = thread.GetExtendedInfo();
2648+
if (!info_root_sp)
2649+
return {};
2650+
StructuredData::ObjectSP node =
2651+
info_root_sp->GetObjectForDotSeparatedPath("tsd_address");
2652+
if (!node)
2653+
return {};
2654+
StructuredData::UnsignedInteger *raw_tsd_addr = node->GetAsUnsignedInteger();
2655+
if (!raw_tsd_addr)
2656+
return {};
2657+
addr_t tsd_addr = raw_tsd_addr->GetUnsignedIntegerValue();
2658+
2659+
// Offset of the Task pointer in a Thread's local storage.
2660+
Process &process = *thread.GetProcess();
2661+
size_t ptr_size = process.GetAddressByteSize();
2662+
uint64_t task_ptr_offset_in_tls =
2663+
swift::tls_get_key(swift::tls_key::concurrency_task) * ptr_size;
2664+
addr_t task_addr_location = tsd_addr + task_ptr_offset_in_tls;
2665+
Status error;
2666+
addr_t task_addr = process.ReadPointerFromMemory(task_addr_location, error);
2667+
if (error.Fail())
2668+
return {};
2669+
return task_addr;
2670+
}
26422671
} // namespace lldb_private

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,10 @@ struct AsyncUnwindRegisterNumbers {
849849

850850
std::optional<AsyncUnwindRegisterNumbers>
851851
GetAsyncUnwindRegisterNumbers(llvm::Triple::ArchType triple);
852+
853+
/// Inspects thread local storage to find the address of the currently executing
854+
/// task.
855+
std::optional<lldb::addr_t> GetTaskAddrFromThreadLocalStorage(Thread &thread);
852856
} // namespace lldb_private
853857

854858
#endif // liblldb_SwiftLanguageRuntime_h_

0 commit comments

Comments
 (0)