12
12
13
13
#include " SwiftLanguageRuntime.h"
14
14
#include " Plugins/LanguageRuntime/Swift/LLDBMemoryReader.h"
15
+ #include " Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h"
15
16
#include " ReflectionContextInterface.h"
16
17
#include " SwiftMetadataCache.h"
17
18
34
35
#include " lldb/Interpreter/CommandObject.h"
35
36
#include " lldb/Interpreter/CommandObjectMultiword.h"
36
37
#include " lldb/Interpreter/CommandReturnObject.h"
38
+ #include " lldb/Symbol/CompilerType.h"
37
39
#include " lldb/Symbol/FuncUnwinders.h"
38
40
#include " lldb/Symbol/Function.h"
39
41
#include " lldb/Symbol/VariableList.h"
43
45
#include " lldb/Utility/LLDBLog.h"
44
46
#include " lldb/Utility/Log.h"
45
47
#include " lldb/Utility/OptionParsing.h"
48
+ #include " lldb/Utility/StructuredData.h"
46
49
#include " lldb/Utility/Timer.h"
47
50
#include " lldb/ValueObject/ValueObject.h"
48
51
#include " lldb/ValueObject/ValueObjectCast.h"
52
55
#include " lldb/lldb-enumerations.h"
53
56
#include " swift/AST/ASTMangler.h"
54
57
#include " swift/Demangling/Demangle.h"
55
- #include " swift/RemoteInspection/ReflectionContext.h"
56
58
#include " swift/RemoteAST/RemoteAST.h"
59
+ #include " swift/RemoteInspection/ReflectionContext.h"
60
+ #include " swift/Threading/ThreadLocalStorage.h"
57
61
58
62
#include " clang/AST/ASTContext.h"
59
63
#include " clang/AST/DeclCXX.h"
60
64
61
65
#include " llvm/ADT/StringRef.h"
62
66
#include " llvm/BinaryFormat/Dwarf.h"
63
67
#include " llvm/Support/raw_ostream.h"
68
+ #include " llvm/Support/Error.h"
69
+ #include " llvm/Support/FormatAdapters.h"
64
70
#include " llvm/Support/Memory.h"
65
71
66
72
// FIXME: we should not need this
@@ -2181,6 +2187,61 @@ class CommandObjectLanguageSwiftTaskSelect final : public CommandObjectParsed {
2181
2187
}
2182
2188
};
2183
2189
2190
+ class CommandObjectLanguageSwiftTaskInfo final : public CommandObjectParsed {
2191
+ public:
2192
+ CommandObjectLanguageSwiftTaskInfo (CommandInterpreter &interpreter)
2193
+ : CommandObjectParsed(interpreter, " info" ,
2194
+ " Print the Task being run on the current thread." ) {
2195
+ }
2196
+
2197
+ private:
2198
+ void DoExecute (Args &command, CommandReturnObject &result) override {
2199
+ if (!m_exe_ctx.GetThreadPtr ()) {
2200
+ result.AppendError (" must be run from a running process and valid thread" );
2201
+ return ;
2202
+ }
2203
+
2204
+ auto task_addr_or_err =
2205
+ GetTaskAddrFromThreadLocalStorage (m_exe_ctx.GetThreadRef ());
2206
+ if (auto error = task_addr_or_err.takeError ()) {
2207
+ result.AppendError (toString (std::move (error)));
2208
+ return ;
2209
+ }
2210
+
2211
+ auto ts_or_err = m_exe_ctx.GetTargetRef ().GetScratchTypeSystemForLanguage (
2212
+ eLanguageTypeSwift);
2213
+ if (auto error = ts_or_err.takeError ()) {
2214
+ result.AppendErrorWithFormatv (" could not get Swift type system: {0}" ,
2215
+ llvm::fmt_consume (std::move (error)));
2216
+ return ;
2217
+ }
2218
+
2219
+ auto *ts = llvm::dyn_cast_or_null<TypeSystemSwiftTypeRef>(ts_or_err->get ());
2220
+ if (!ts) {
2221
+ result.AppendError (" could not get Swift type system" );
2222
+ return ;
2223
+ }
2224
+
2225
+ addr_t task_addr = task_addr_or_err.get ();
2226
+ // TypeMangling for "Swift.UnsafeCurrentTask"
2227
+ CompilerType task_type =
2228
+ ts->GetTypeFromMangledTypename (ConstString (" $sSctD" ));
2229
+ auto task_sp = ValueObject::CreateValueObjectFromAddress (
2230
+ " current_task" , task_addr, m_exe_ctx, task_type, false );
2231
+ if (auto synthetic_sp = task_sp->GetSyntheticValue ())
2232
+ task_sp = synthetic_sp;
2233
+
2234
+ auto error = task_sp->Dump (result.GetOutputStream ());
2235
+ if (error) {
2236
+ result.AppendErrorWithFormatv (" failed to print current task: {0}" ,
2237
+ toString (std::move (error)));
2238
+ return ;
2239
+ }
2240
+
2241
+ result.SetStatus (lldb::eReturnStatusSuccessFinishResult);
2242
+ }
2243
+ };
2244
+
2184
2245
class CommandObjectLanguageSwiftTask final : public CommandObjectMultiword {
2185
2246
public:
2186
2247
CommandObjectLanguageSwiftTask (CommandInterpreter &interpreter)
@@ -2193,6 +2254,9 @@ class CommandObjectLanguageSwiftTask final : public CommandObjectMultiword {
2193
2254
LoadSubCommand (
2194
2255
" select" ,
2195
2256
CommandObjectSP (new CommandObjectLanguageSwiftTaskSelect (interpreter)));
2257
+ LoadSubCommand (
2258
+ " info" ,
2259
+ CommandObjectSP (new CommandObjectLanguageSwiftTaskInfo (interpreter)));
2196
2260
}
2197
2261
};
2198
2262
@@ -2654,4 +2718,33 @@ std::optional<lldb::addr_t> SwiftLanguageRuntime::TrySkipVirtualParentProlog(
2654
2718
: sc.function ->GetPrologueByteSize ();
2655
2719
return pc_value + prologue_size;
2656
2720
}
2721
+
2722
+ llvm::Expected<lldb::addr_t > GetTaskAddrFromThreadLocalStorage (Thread &thread) {
2723
+ #if !SWIFT_THREADING_USE_RESERVED_TLS_KEYS
2724
+ return llvm::createStringError (
2725
+ " getting the current task from a thread is not supported" );
2726
+ #else
2727
+ // Compute the thread local storage address for this thread.
2728
+ addr_t tsd_addr = LLDB_INVALID_ADDRESS;
2729
+ if (auto info_sp = thread.GetExtendedInfo ())
2730
+ if (auto *info_dict = info_sp->GetAsDictionary ())
2731
+ info_dict->GetValueForKeyAsInteger (" tsd_address" , tsd_addr);
2732
+
2733
+ if (tsd_addr == LLDB_INVALID_ADDRESS)
2734
+ return llvm::createStringError (" could not read current task from thread" );
2735
+
2736
+ // Offset of the Task pointer in a Thread's local storage.
2737
+ Process &process = *thread.GetProcess ();
2738
+ size_t ptr_size = process.GetAddressByteSize ();
2739
+ uint64_t task_ptr_offset_in_tls =
2740
+ swift::tls_get_key (swift::tls_key::concurrency_task) * ptr_size;
2741
+ addr_t task_addr_location = tsd_addr + task_ptr_offset_in_tls;
2742
+ Status status;
2743
+ addr_t task_addr = process.ReadPointerFromMemory (task_addr_location, status);
2744
+ if (status.Fail ())
2745
+ return llvm::createStringError (" could not get current task from thread: %s" ,
2746
+ status.AsCString ());
2747
+ return task_addr;
2748
+ #endif
2749
+ }
2657
2750
} // namespace lldb_private
0 commit comments