From b4afb0b69bc07b5df72e2db29af4bc09bcafa526 Mon Sep 17 00:00:00 2001 From: AbdAlRahman Gad Date: Tue, 1 Oct 2024 21:15:13 +0300 Subject: [PATCH 1/5] [lldb] Change SymbolContext::GetAddressRangeFromHereToEndLine to return Expected (NFC) Signed-off-by: AbdAlRahman Gad --- lldb/include/lldb/Symbol/SymbolContext.h | 4 ++-- lldb/source/API/SBThread.cpp | 8 +++++++- lldb/source/Commands/CommandObjectThread.cpp | 13 +++++++----- lldb/source/Symbol/SymbolContext.cpp | 21 ++++++++------------ 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/lldb/include/lldb/Symbol/SymbolContext.h b/lldb/include/lldb/Symbol/SymbolContext.h index 0bc707070f8504..653e414d9aa4f1 100644 --- a/lldb/include/lldb/Symbol/SymbolContext.h +++ b/lldb/include/lldb/Symbol/SymbolContext.h @@ -201,8 +201,8 @@ class SymbolContext { bool GetAddressRange(uint32_t scope, uint32_t range_idx, bool use_inline_block_range, AddressRange &range) const; - bool GetAddressRangeFromHereToEndLine(uint32_t end_line, AddressRange &range, - Status &error); + llvm::Expected GetAddressRangeFromHereToEndLine(uint32_t end_line, + AddressRange &range); /// Find the best global data symbol visible from this context. /// diff --git a/lldb/source/API/SBThread.cpp b/lldb/source/API/SBThread.cpp index 7508eed5d6fdbd..76c321e6df90f8 100644 --- a/lldb/source/API/SBThread.cpp +++ b/lldb/source/API/SBThread.cpp @@ -605,8 +605,14 @@ void SBThread::StepInto(const char *target_name, uint32_t end_line, if (end_line == LLDB_INVALID_LINE_NUMBER) range = sc.line_entry.range; else { - if (!sc.GetAddressRangeFromHereToEndLine(end_line, range, error.ref())) + auto get_address_range_from_here_to_end_line_or_err = + sc.GetAddressRangeFromHereToEndLine(end_line, range); + if (!get_address_range_from_here_to_end_line_or_err) { + error = Status::FromErrorString( + toString(get_address_range_from_here_to_end_line_or_err.takeError()) + .c_str()); return; + } } const LazyBool step_out_avoids_code_without_debug_info = diff --git a/lldb/source/Commands/CommandObjectThread.cpp b/lldb/source/Commands/CommandObjectThread.cpp index edbec0e305db74..d9ae4590773995 100644 --- a/lldb/source/Commands/CommandObjectThread.cpp +++ b/lldb/source/Commands/CommandObjectThread.cpp @@ -489,11 +489,14 @@ class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed { AddressRange range; SymbolContext sc = frame->GetSymbolContext(eSymbolContextEverything); if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER) { - Status error; - if (!sc.GetAddressRangeFromHereToEndLine(m_options.m_end_line, range, - error)) { - result.AppendErrorWithFormat("invalid end-line option: %s.", - error.AsCString()); + auto get_address_range_from_here_to_end_line_or_err = + sc.GetAddressRangeFromHereToEndLine(m_options.m_end_line, range); + if (!get_address_range_from_here_to_end_line_or_err) { + result.AppendErrorWithFormat( + "invalid end-line option: %s.", + toString( + get_address_range_from_here_to_end_line_or_err.takeError()) + .c_str()); return; } } else if (m_options.m_end_line_is_block_end) { diff --git a/lldb/source/Symbol/SymbolContext.cpp b/lldb/source/Symbol/SymbolContext.cpp index a1020a5aae4e79..ac7864cb3a6623 100644 --- a/lldb/source/Symbol/SymbolContext.cpp +++ b/lldb/source/Symbol/SymbolContext.cpp @@ -706,20 +706,18 @@ LineEntry SymbolContext::GetFunctionStartLineEntry() const { return LineEntry(); } -bool SymbolContext::GetAddressRangeFromHereToEndLine(uint32_t end_line, - AddressRange &range, - Status &error) { +llvm::Expected +SymbolContext::GetAddressRangeFromHereToEndLine(uint32_t end_line, + AddressRange &range) { if (!line_entry.IsValid()) { - error = Status::FromErrorString("Symbol context has no line table."); - return false; + return llvm::createStringError("Symbol context has no line table."); } range = line_entry.range; if (line_entry.line > end_line) { - error = Status::FromErrorStringWithFormat( + return llvm::createStringError( "end line option %d must be after the current line: %d", end_line, line_entry.line); - return false; } uint32_t line_index = 0; @@ -740,29 +738,26 @@ bool SymbolContext::GetAddressRangeFromHereToEndLine(uint32_t end_line, if (!found) { // Can't find the index of the SymbolContext's line entry in the // SymbolContext's CompUnit. - error = Status::FromErrorString( + return llvm::createStringError( "Can't find the current line entry in the CompUnit - can't process " "the end-line option"); - return false; } line_index = comp_unit->FindLineEntry(line_index, end_line, nullptr, false, &end_entry); if (line_index == UINT32_MAX) { - error = Status::FromErrorStringWithFormat( + return llvm::createStringError( "could not find a line table entry corresponding " "to end line number %d", end_line); - return false; } Block *func_block = GetFunctionBlock(); if (func_block && func_block->GetRangeIndexContainingAddress( end_entry.range.GetBaseAddress()) == UINT32_MAX) { - error = Status::FromErrorStringWithFormat( + return llvm::createStringError( "end line number %d is not contained within the current function.", end_line); - return false; } lldb::addr_t range_size = end_entry.range.GetBaseAddress().GetFileAddress() - From afb115aa439ad9ddd667017eca212920627cc5ff Mon Sep 17 00:00:00 2001 From: AbdAlRahman Gad Date: Thu, 3 Oct 2024 11:24:18 +0300 Subject: [PATCH 2/5] [lldb] Change SymbolContext::GetAddressRangeFromHereToEndLine to return llvm::Error (NFC) Signed-off-by: AbdAlRahman Gad --- lldb/include/lldb/Symbol/SymbolContext.h | 4 ++-- lldb/source/API/SBThread.cpp | 9 +++------ lldb/source/Commands/CommandObjectThread.cpp | 11 ++++------- lldb/source/Symbol/SymbolContext.cpp | 4 ++-- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/lldb/include/lldb/Symbol/SymbolContext.h b/lldb/include/lldb/Symbol/SymbolContext.h index 653e414d9aa4f1..f65f57b0d11034 100644 --- a/lldb/include/lldb/Symbol/SymbolContext.h +++ b/lldb/include/lldb/Symbol/SymbolContext.h @@ -201,8 +201,8 @@ class SymbolContext { bool GetAddressRange(uint32_t scope, uint32_t range_idx, bool use_inline_block_range, AddressRange &range) const; - llvm::Expected GetAddressRangeFromHereToEndLine(uint32_t end_line, - AddressRange &range); + llvm::Error GetAddressRangeFromHereToEndLine(uint32_t end_line, + AddressRange &range); /// Find the best global data symbol visible from this context. /// diff --git a/lldb/source/API/SBThread.cpp b/lldb/source/API/SBThread.cpp index 76c321e6df90f8..a99456e06d0329 100644 --- a/lldb/source/API/SBThread.cpp +++ b/lldb/source/API/SBThread.cpp @@ -605,12 +605,9 @@ void SBThread::StepInto(const char *target_name, uint32_t end_line, if (end_line == LLDB_INVALID_LINE_NUMBER) range = sc.line_entry.range; else { - auto get_address_range_from_here_to_end_line_or_err = - sc.GetAddressRangeFromHereToEndLine(end_line, range); - if (!get_address_range_from_here_to_end_line_or_err) { - error = Status::FromErrorString( - toString(get_address_range_from_here_to_end_line_or_err.takeError()) - .c_str()); + llvm::Error err = sc.GetAddressRangeFromHereToEndLine(end_line, range); + if (err) { + error = Status::FromErrorString(llvm::toString(std::move(err)).c_str()); return; } } diff --git a/lldb/source/Commands/CommandObjectThread.cpp b/lldb/source/Commands/CommandObjectThread.cpp index d9ae4590773995..507b711a727fe5 100644 --- a/lldb/source/Commands/CommandObjectThread.cpp +++ b/lldb/source/Commands/CommandObjectThread.cpp @@ -489,14 +489,11 @@ class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed { AddressRange range; SymbolContext sc = frame->GetSymbolContext(eSymbolContextEverything); if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER) { - auto get_address_range_from_here_to_end_line_or_err = + llvm::Error err = sc.GetAddressRangeFromHereToEndLine(m_options.m_end_line, range); - if (!get_address_range_from_here_to_end_line_or_err) { - result.AppendErrorWithFormat( - "invalid end-line option: %s.", - toString( - get_address_range_from_here_to_end_line_or_err.takeError()) - .c_str()); + if (err) { + result.AppendErrorWithFormat("invalid end-line option: %s.", + llvm::toString(std::move(err)).c_str()); return; } } else if (m_options.m_end_line_is_block_end) { diff --git a/lldb/source/Symbol/SymbolContext.cpp b/lldb/source/Symbol/SymbolContext.cpp index ac7864cb3a6623..de083e81206e2a 100644 --- a/lldb/source/Symbol/SymbolContext.cpp +++ b/lldb/source/Symbol/SymbolContext.cpp @@ -706,7 +706,7 @@ LineEntry SymbolContext::GetFunctionStartLineEntry() const { return LineEntry(); } -llvm::Expected +llvm::Error SymbolContext::GetAddressRangeFromHereToEndLine(uint32_t end_line, AddressRange &range) { if (!line_entry.IsValid()) { @@ -763,7 +763,7 @@ SymbolContext::GetAddressRangeFromHereToEndLine(uint32_t end_line, lldb::addr_t range_size = end_entry.range.GetBaseAddress().GetFileAddress() - range.GetBaseAddress().GetFileAddress(); range.SetByteSize(range_size); - return true; + return llvm::Error::success(); } const Symbol *SymbolContext::FindBestGlobalDataSymbol(ConstString name, From 8cbe329e00d7b79942f66a1dd6e0e0c08fd02473 Mon Sep 17 00:00:00 2001 From: AbdAlRahman Gad Date: Thu, 3 Oct 2024 13:44:30 +0300 Subject: [PATCH 3/5] use clang-format Signed-off-by: AbdAlRahman Gad --- lldb/source/Commands/CommandObjectThread.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lldb/source/Commands/CommandObjectThread.cpp b/lldb/source/Commands/CommandObjectThread.cpp index 507b711a727fe5..22ac04f9f689c1 100644 --- a/lldb/source/Commands/CommandObjectThread.cpp +++ b/lldb/source/Commands/CommandObjectThread.cpp @@ -492,8 +492,9 @@ class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed { llvm::Error err = sc.GetAddressRangeFromHereToEndLine(m_options.m_end_line, range); if (err) { - result.AppendErrorWithFormat("invalid end-line option: %s.", - llvm::toString(std::move(err)).c_str()); + result.AppendErrorWithFormat( + "invalid end-line option: %s.", + llvm::toString(std::move(err)).c_str()); return; } } else if (m_options.m_end_line_is_block_end) { From 8a34b944363ac57cdb4ef08de80576050d7c5477 Mon Sep 17 00:00:00 2001 From: AbdAlRahman Gad <89566409+AbdAlRahmanGad@users.noreply.github.com> Date: Tue, 8 Oct 2024 05:05:38 +0300 Subject: [PATCH 4/5] replace `AppendErrorWithFormat` with `AppendErrorWithFormatv` Co-authored-by: Adrian Prantl --- lldb/source/Commands/CommandObjectThread.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lldb/source/Commands/CommandObjectThread.cpp b/lldb/source/Commands/CommandObjectThread.cpp index 22ac04f9f689c1..ac3fc8a68658af 100644 --- a/lldb/source/Commands/CommandObjectThread.cpp +++ b/lldb/source/Commands/CommandObjectThread.cpp @@ -492,7 +492,9 @@ class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed { llvm::Error err = sc.GetAddressRangeFromHereToEndLine(m_options.m_end_line, range); if (err) { - result.AppendErrorWithFormat( + result.AppendErrorWithFormatv( + "invalid end-line option: {0}.", + llvm::toString(std::move(err))); "invalid end-line option: %s.", llvm::toString(std::move(err)).c_str()); return; From 1d8dc8d6c7f9c4567463bb45e2ea6f39ecd15b79 Mon Sep 17 00:00:00 2001 From: AbdAlRahman Gad Date: Tue, 8 Oct 2024 07:36:34 +0300 Subject: [PATCH 5/5] replace `AppendErrorWithFormat` with `AppendErrorWithFormatv` Signed-off-by: AbdAlRahman Gad --- lldb/source/Commands/CommandObjectThread.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lldb/source/Commands/CommandObjectThread.cpp b/lldb/source/Commands/CommandObjectThread.cpp index ac3fc8a68658af..e199c5d05ad0fe 100644 --- a/lldb/source/Commands/CommandObjectThread.cpp +++ b/lldb/source/Commands/CommandObjectThread.cpp @@ -492,11 +492,8 @@ class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed { llvm::Error err = sc.GetAddressRangeFromHereToEndLine(m_options.m_end_line, range); if (err) { - result.AppendErrorWithFormatv( - "invalid end-line option: {0}.", - llvm::toString(std::move(err))); - "invalid end-line option: %s.", - llvm::toString(std::move(err)).c_str()); + result.AppendErrorWithFormatv("invalid end-line option: {0}.", + llvm::toString(std::move(err))); return; } } else if (m_options.m_end_line_is_block_end) {