-
Notifications
You must be signed in to change notification settings - Fork 353
[lldb] Adopt new pointer and mangled typename based stringForPrintObject #11916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
38f5709
9cd7c44
3c969d6
caade22
defe915
ff4b066
714c3bb
efa060f
f6bf6dd
d8f6faa
5cb63c4
9442b97
b9e07a3
6f0d8fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -640,15 +640,22 @@ Status SwiftExpressionSourceCode::GetText( | |
| if (triple.isOSDarwin()) { | ||
| if (auto process_sp = exe_ctx.GetProcessSP()) { | ||
| os_vers << getAvailabilityName(triple) << " "; | ||
| auto platform = target->GetPlatform(); | ||
| bool is_simulator = platform->GetPluginName().ends_with("-simulator"); | ||
| if (is_simulator) { | ||
| // The simulators look like the host OS to Process, but Platform | ||
| // can the version out of an environment variable. | ||
| os_vers << platform->GetOSVersion(process_sp.get()).getAsString(); | ||
| if (options.GetUseContextFreeSwiftPrintObject()) { | ||
| // Disable availability by setting the OS version to 9999. This | ||
| // placeholder OS version used for future OS versions when building | ||
| // the Swift standard library locally. | ||
| os_vers << "9999"; | ||
| } else { | ||
| llvm::VersionTuple version = process_sp->GetHostOSVersion(); | ||
| os_vers << version.getAsString(); | ||
| auto platform = target->GetPlatform(); | ||
| bool is_simulator = platform->GetPluginName().ends_with("-simulator"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really not have a nicer way to tell if a Platform is a simulator?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's |
||
| if (is_simulator) { | ||
| // The simulators look like the host OS to Process, but Platform | ||
| // can get the version out of an environment variable. | ||
| os_vers << platform->GetOSVersion(process_sp.get()).getAsString(); | ||
| } else { | ||
| llvm::VersionTuple version = process_sp->GetHostOSVersion(); | ||
| os_vers << version.getAsString(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,7 @@ | |
| #include "lldb/Utility/Log.h" | ||
| #include "lldb/Utility/OptionParsing.h" | ||
| #include "lldb/Utility/Status.h" | ||
| #include "lldb/Utility/StreamString.h" | ||
| #include "lldb/Utility/StructuredData.h" | ||
| #include "lldb/Utility/Timer.h" | ||
| #include "lldb/ValueObject/ValueObject.h" | ||
|
|
@@ -835,7 +836,7 @@ SwiftLanguageRuntime::GetObjectDescriptionExpr_Ref(ValueObject &object) { | |
| object.GetValueAsUnsigned(0)).str(); | ||
|
|
||
| if (log) | ||
| log->Printf("[GetObjectDescriptionExpr_Result] expression: %s", | ||
| log->Printf("[GetObjectDescriptionExpr_Ref] expression: %s", | ||
| expr_string.GetData()); | ||
| return expr_str; | ||
| } | ||
|
|
@@ -911,25 +912,27 @@ std::string SwiftLanguageRuntime::GetObjectDescriptionExpr_Copy( | |
| return expr_string; | ||
| } | ||
|
|
||
| llvm::Error SwiftLanguageRuntime::RunObjectDescriptionExpr( | ||
| ValueObject &object, std::string &expr_string, Stream &result) { | ||
| static llvm::Expected<ValueObjectSP> | ||
| RunObjectDescription(ValueObject &object, std::string &expr_string, | ||
| Process &process, bool context_free = false) { | ||
| Log *log(GetLog(LLDBLog::DataFormatters | LLDBLog::Expressions)); | ||
| ValueObjectSP result_sp; | ||
| EvaluateExpressionOptions eval_options; | ||
| eval_options.SetUnwindOnError(true); | ||
| eval_options.SetLanguage(lldb::eLanguageTypeSwift); | ||
| eval_options.SetSuppressPersistentResult(true); | ||
| eval_options.SetIgnoreBreakpoints(true); | ||
| eval_options.SetTimeout(GetProcess().GetUtilityExpressionTimeout()); | ||
| eval_options.SetTimeout(process.GetUtilityExpressionTimeout()); | ||
| if (context_free) | ||
| eval_options.SetUseContextFreeSwiftPrintObject(); | ||
|
|
||
| StackFrameSP frame_sp = object.GetFrameSP(); | ||
| if (!frame_sp) | ||
| frame_sp = | ||
| GetProcess().GetThreadList().GetSelectedThread()->GetSelectedFrame( | ||
| DoNoSelectMostRelevantFrame); | ||
| frame_sp = process.GetThreadList().GetSelectedThread()->GetSelectedFrame( | ||
| DoNoSelectMostRelevantFrame); | ||
| if (!frame_sp) | ||
| return llvm::createStringError("no execution context to run expression in"); | ||
| auto eval_result = GetProcess().GetTarget().EvaluateExpression( | ||
| auto eval_result = process.GetTarget().EvaluateExpression( | ||
| expr_string, frame_sp.get(), result_sp, eval_options); | ||
|
|
||
| LLDB_LOG(log, "[RunObjectDescriptionExpr] {0}", toString(eval_result)); | ||
|
|
@@ -952,12 +955,17 @@ llvm::Error SwiftLanguageRuntime::RunObjectDescriptionExpr( | |
| return llvm::createStringError("expression produced invalid result type"); | ||
| } | ||
|
|
||
| return result_sp; | ||
| } | ||
|
|
||
| static llvm::Error DumpString(ValueObjectSP result_sp, Stream &strm) { | ||
| Log *log(GetLog(LLDBLog::DataFormatters | LLDBLog::Expressions)); | ||
| formatters::StringPrinter::ReadStringAndDumpToStreamOptions dump_options; | ||
| dump_options.SetEscapeNonPrintables(false); | ||
| dump_options.SetQuote('\0'); | ||
| dump_options.SetPrefixToken(nullptr); | ||
| if (formatters::swift::String_SummaryProvider( | ||
| *result_sp.get(), result, | ||
| *result_sp, strm, | ||
| TypeSummaryOptions() | ||
| .SetLanguage(lldb::eLanguageTypeSwift) | ||
| .SetCapping(eTypeSummaryUncapped), | ||
|
|
@@ -973,6 +981,15 @@ llvm::Error SwiftLanguageRuntime::RunObjectDescriptionExpr( | |
| return llvm::createStringError("expression produced unprintable string"); | ||
| } | ||
|
|
||
| llvm::Error SwiftLanguageRuntime::RunObjectDescriptionExpr( | ||
| ValueObject &object, std::string &expr_string, Stream &strm) { | ||
| auto result_or_err = RunObjectDescription(object, expr_string, GetProcess()); | ||
| if (!result_or_err) | ||
| return result_or_err.takeError(); | ||
|
|
||
| return DumpString(*result_or_err, strm); | ||
| } | ||
|
|
||
| static bool IsVariable(ValueObject &object) { | ||
| if (object.IsSynthetic()) | ||
| return IsVariable(*object.GetNonSyntheticValue()); | ||
|
|
@@ -1002,11 +1019,80 @@ static bool IsSwiftReferenceType(ValueObject &object) { | |
| return false; | ||
| } | ||
|
|
||
| static llvm::Error PrintObjectViaPointer(Stream &strm, ValueObject &object, | ||
| Process &process) { | ||
| Flags flags(object.GetCompilerType().GetTypeInfo()); | ||
| addr_t addr = LLDB_INVALID_ADDRESS; | ||
| if (flags.Test(eTypeInstanceIsPointer)) { | ||
| // Objects are pointers. | ||
| addr = object.GetValueAsUnsigned(LLDB_INVALID_ADDRESS); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use GetValueAsAddress.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm surprised we don't have a little wrapper for this in ValueObject, but you can do what GetValueAsAddress does, and call FixDataAddress of the addr you get back. @jasonmolenda will know for sure whether that's the right thing to do. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah if it's guaranteed to be an address it's safe to run it through Process::FixAnyAddress() (the method to use when you don't know if it's an address of code or data). With MTE managed heap, if the value might get put into a jitted expression (so used in code running in the inferior, with MTE) you need to retain the MTE nibble in the top byte. It's an edge case but something to mention, @felipepiovezan dealt with a tricky case where we needed to maintain metadata bits through to the point where we looked it up/use it, just recently, for something that ended up in a jitted expression.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jasonmolenda thanks, this address is data, and will be used in a jitted expression.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a call to |
||
| addr = process.FixDataAddress(addr); | ||
| } else { | ||
| // Get the address of non-object values (structs, enums). | ||
| auto addr_and_type = object.GetAddressOf(false); | ||
| if (addr_and_type.type != eAddressTypeLoad) { | ||
| return llvm::createStringError("address-of value object failed"); | ||
| } | ||
| addr = addr_and_type.address; | ||
| } | ||
|
|
||
| if (addr == 0 || addr == LLDB_INVALID_ADDRESS) | ||
| return llvm::createStringError("invalid address 0x%x", addr); | ||
|
|
||
| StringRef mangled_type_name = object.GetMangledTypeName(); | ||
| // Swift APIs that receive mangled names require the prefix removed. | ||
| mangled_type_name.consume_front("$s"); | ||
kastiglione marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| mangled_type_name.consume_front("$e"); // Embedded Swift prefix | ||
|
|
||
| std::string expr_string = | ||
| llvm::formatv( | ||
| "Swift._DebuggerSupport.stringForPrintObject(UnsafeRawPointer(" | ||
| "bitPattern: {0}), mangledTypeName: \"{1}\")", | ||
| addr, mangled_type_name) | ||
| .str(); | ||
|
|
||
| auto result_or_err = RunObjectDescription(object, expr_string, process, true); | ||
| if (!result_or_err) | ||
| return result_or_err.takeError(); | ||
|
|
||
| // A `(Bool, String)` tuple, where the bool indicates success/failure. | ||
| auto result_sp = *result_or_err; | ||
| auto success_sp = result_sp->GetChildAtIndex(0); | ||
| auto description_sp = result_sp->GetChildAtIndex(1); | ||
|
|
||
| StreamString string_result; | ||
| auto err = DumpString(description_sp, string_result); | ||
| if (err) { | ||
| return llvm::joinErrors( | ||
| std::move(err), | ||
| llvm::createStringError("decoding of description String failed")); | ||
| } | ||
|
|
||
| Status status; | ||
| if (!success_sp->IsLogicalTrue(status)) | ||
| // The Bool is false, which means the String is an error message. | ||
| return llvm::createStringError(string_result.GetData()); | ||
|
|
||
| strm.PutCString(string_result.GetString()); | ||
| return llvm::Error::success(); | ||
| } | ||
|
|
||
| llvm::Error SwiftLanguageRuntime::GetObjectDescription(Stream &str, | ||
| ValueObject &object) { | ||
| if (object.IsUninitializedReference()) | ||
| return llvm::createStringError("<uninitialized>"); | ||
|
|
||
| Log *log = GetLog(LLDBLog::DataFormatters | LLDBLog::Expressions); | ||
| if (GetProcess().GetTarget().GetSwiftUseContextFreePrintObject()) { | ||
| if (auto err = PrintObjectViaPointer(str, object, GetProcess())) { | ||
| LLDB_LOG_ERROR(log, std::move(err), | ||
| "stringForPrintObject(_:mangledTypeName) failed: {0}"); | ||
| } else { | ||
| LLDB_LOG(log, "stringForPrintObject(_:mangledTypeName:) succeeded"); | ||
| return llvm::Error::success(); | ||
| } | ||
| } | ||
|
|
||
| std::string expr_string; | ||
|
|
||
| if (::IsVariable(object) || ::IsSwiftResultVariable(object.GetName())) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| SWIFT_SOURCES := main.swift | ||
| SWIFTFLAGS_EXTRAS := -parse-as-library | ||
| include Makefile.rules |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adrian-prantl I've introduced this change to the PR. This prevents module loading when the expression is running the new
po.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This LGTM, assuming
m_options.GetUseContextFreeSwiftPrintObject()is only true in apo!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is only true in a
po.