Author: labath Date: Mon Apr 29 03:55:22 2019 New Revision: 359436 URL: http://llvm.org/viewvc/llvm-project?rev=359436&view=rev Log: DWARFExpression: Fix implementation of DW_OP_pick
Summary: The DWARF spec states that the DWARF stack arguments are numbered from the top. Our implementation of DW_OP_pick was counting them from the bottom. This bug probably wasn't noticed because nobody (except my upcoming postfix-to-DWARF converter) uses DW_OP_pick, but I've cross-checked with gdb to confirm that counting from the top is the expected behavior. This patch fixes the implementation to match the spec and gdb behavior and adds a test. Reviewers: jasonmolenda, clayborg Subscribers: mgorny, aprantl, lldb-commits Differential Revision: https://reviews.llvm.org/D61182 Added: lldb/trunk/unittests/Expression/DWARFExpressionTest.cpp Modified: lldb/trunk/include/lldb/Utility/Scalar.h lldb/trunk/source/Expression/DWARFExpression.cpp lldb/trunk/source/Utility/Scalar.cpp lldb/trunk/unittests/Expression/CMakeLists.txt Modified: lldb/trunk/include/lldb/Utility/Scalar.h URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/Scalar.h?rev=359436&r1=359435&r2=359436&view=diff ============================================================================== --- lldb/trunk/include/lldb/Utility/Scalar.h (original) +++ lldb/trunk/include/lldb/Utility/Scalar.h Mon Apr 29 03:55:22 2019 @@ -339,6 +339,8 @@ bool operator<=(const Scalar &lhs, const bool operator>(const Scalar &lhs, const Scalar &rhs); bool operator>=(const Scalar &lhs, const Scalar &rhs); +llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Scalar &scalar); + } // namespace lldb_private #endif // LLDB_UTILITY_SCALAR_H Modified: lldb/trunk/source/Expression/DWARFExpression.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/DWARFExpression.cpp?rev=359436&r1=359435&r2=359436&view=diff ============================================================================== --- lldb/trunk/source/Expression/DWARFExpression.cpp (original) +++ lldb/trunk/source/Expression/DWARFExpression.cpp Mon Apr 29 03:55:22 2019 @@ -1751,7 +1751,7 @@ bool DWARFExpression::Evaluate( case DW_OP_pick: { uint8_t pick_idx = opcodes.GetU8(&offset); if (pick_idx < stack.size()) - stack.push_back(stack[pick_idx]); + stack.push_back(stack[stack.size() - 1 - pick_idx]); else { if (error_ptr) error_ptr->SetErrorStringWithFormat( Modified: lldb/trunk/source/Utility/Scalar.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/Scalar.cpp?rev=359436&r1=359435&r2=359436&view=diff ============================================================================== --- lldb/trunk/source/Utility/Scalar.cpp (original) +++ lldb/trunk/source/Utility/Scalar.cpp Mon Apr 29 03:55:22 2019 @@ -12,6 +12,7 @@ #include "lldb/Utility/Endian.h" #include "lldb/Utility/Status.h" #include "lldb/Utility/Stream.h" +#include "lldb/Utility/StreamString.h" #include "lldb/lldb-types.h" #include "llvm/ADT/SmallString.h" @@ -2844,3 +2845,9 @@ bool Scalar::SetBit(uint32_t bit) { } return false; } + +llvm::raw_ostream &lldb_private::operator<<(llvm::raw_ostream &os, const Scalar &scalar) { + StreamString s; + scalar.GetValue(&s, /*show_type*/ true); + return os << s.GetString(); +} Modified: lldb/trunk/unittests/Expression/CMakeLists.txt URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Expression/CMakeLists.txt?rev=359436&r1=359435&r2=359436&view=diff ============================================================================== --- lldb/trunk/unittests/Expression/CMakeLists.txt (original) +++ lldb/trunk/unittests/Expression/CMakeLists.txt Mon Apr 29 03:55:22 2019 @@ -1,4 +1,5 @@ add_lldb_unittest(ExpressionTests + DWARFExpressionTest.cpp ClangParserTest.cpp LINK_LIBS @@ -6,4 +7,5 @@ add_lldb_unittest(ExpressionTests lldbPluginExpressionParserClang lldbUtility lldbUtilityHelpers + LLVMTestingSupport ) Added: lldb/trunk/unittests/Expression/DWARFExpressionTest.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Expression/DWARFExpressionTest.cpp?rev=359436&view=auto ============================================================================== --- lldb/trunk/unittests/Expression/DWARFExpressionTest.cpp (added) +++ lldb/trunk/unittests/Expression/DWARFExpressionTest.cpp Mon Apr 29 03:55:22 2019 @@ -0,0 +1,42 @@ +//===-- DWARFExpressionTest.cpp ----------------------------------*- C++-*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "lldb/Expression/DWARFExpression.h" +#include "lldb/Core/Value.h" +#include "lldb/Core/dwarf.h" +#include "lldb/Utility/StreamString.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/Testing/Support/Error.h" +#include "gtest/gtest.h" + +using namespace lldb_private; + +static llvm::Expected<Scalar> Evaluate(llvm::ArrayRef<uint8_t> expr) { + DataExtractor extractor(expr.data(), expr.size(), lldb::eByteOrderLittle, + /*addr_size*/ 4); + + Value result; + Status status; + if (!DWARFExpression::Evaluate( + /*exe_ctx*/ nullptr, /*reg_ctx*/ nullptr, /*opcode_ctx*/ nullptr, + extractor, /*dwarf_cu*/ nullptr, /*offset*/ 0, expr.size(), + lldb::eRegisterKindLLDB, /*initial_value_ptr*/ nullptr, + /*object_address_ptr*/ nullptr, result, &status)) + return status.ToError(); + + return result.GetScalar(); +} + +TEST(DWARFExpression, DW_OP_pick) { + EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit1, DW_OP_lit0, DW_OP_pick, 0}), + llvm::HasValue(0)); + EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit1, DW_OP_lit0, DW_OP_pick, 1}), + llvm::HasValue(1)); + EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit1, DW_OP_lit0, DW_OP_pick, 2}), + llvm::Failed()); +} _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits