|
| 1 | +//===-- DumpValueObjectOptionsTests.cpp -----------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "Plugins/Platform/Linux/PlatformLinux.h" |
| 10 | +#include "Plugins/ScriptInterpreter/None/ScriptInterpreterNone.h" |
| 11 | +#include "Plugins/TypeSystem/Clang/TypeSystemClang.h" |
| 12 | +#include "TestingSupport/SubsystemRAII.h" |
| 13 | +#include "TestingSupport/Symbol/ClangTestUtils.h" |
| 14 | +#include "lldb/Core/Debugger.h" |
| 15 | +#include "lldb/Core/ValueObject.h" |
| 16 | +#include "lldb/Core/ValueObjectConstResult.h" |
| 17 | +#include "lldb/DataFormatters/DumpValueObjectOptions.h" |
| 18 | + |
| 19 | +#include "gtest/gtest.h" |
| 20 | + |
| 21 | +using namespace lldb; |
| 22 | +using namespace lldb_private; |
| 23 | + |
| 24 | +struct MockProcess : Process { |
| 25 | + MockProcess(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp) |
| 26 | + : Process(target_sp, listener_sp) {} |
| 27 | + |
| 28 | + llvm::StringRef GetPluginName() override { return "mock process"; } |
| 29 | + |
| 30 | + bool CanDebug(lldb::TargetSP target, bool plugin_specified_by_name) override { |
| 31 | + return false; |
| 32 | + }; |
| 33 | + |
| 34 | + Status DoDestroy() override { return {}; } |
| 35 | + |
| 36 | + void RefreshStateAfterStop() override {} |
| 37 | + |
| 38 | + bool DoUpdateThreadList(ThreadList &old_thread_list, |
| 39 | + ThreadList &new_thread_list) override { |
| 40 | + return false; |
| 41 | + }; |
| 42 | + |
| 43 | + size_t DoReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, |
| 44 | + Status &error) override { |
| 45 | + // No need to read memory in these tests. |
| 46 | + return size; |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +class ValueObjectMockProcessTest : public ::testing::Test { |
| 51 | +public: |
| 52 | + void SetUp() override { |
| 53 | + ArchSpec arch("i386-pc-linux"); |
| 54 | + Platform::SetHostPlatform( |
| 55 | + platform_linux::PlatformLinux::CreateInstance(true, &arch)); |
| 56 | + m_debugger_sp = Debugger::CreateInstance(); |
| 57 | + ASSERT_TRUE(m_debugger_sp); |
| 58 | + m_debugger_sp->GetTargetList().CreateTarget(*m_debugger_sp, "", arch, |
| 59 | + eLoadDependentsNo, |
| 60 | + m_platform_sp, m_target_sp); |
| 61 | + ASSERT_TRUE(m_target_sp); |
| 62 | + ASSERT_TRUE(m_target_sp->GetArchitecture().IsValid()); |
| 63 | + ASSERT_TRUE(m_platform_sp); |
| 64 | + m_listener_sp = Listener::MakeListener("dummy"); |
| 65 | + m_process_sp = std::make_shared<MockProcess>(m_target_sp, m_listener_sp); |
| 66 | + ASSERT_TRUE(m_process_sp); |
| 67 | + m_exe_ctx = ExecutionContext(m_process_sp); |
| 68 | + |
| 69 | + m_holder = std::make_unique<clang_utils::TypeSystemClangHolder>("test"); |
| 70 | + m_type_system = m_holder->GetAST(); |
| 71 | + } |
| 72 | + |
| 73 | + CompilerType |
| 74 | + MakeEnumType(const std::vector<std::pair<const char *, int>> enumerators) { |
| 75 | + CompilerType uint_type = m_type_system->GetBuiltinTypeForEncodingAndBitSize( |
| 76 | + lldb::eEncodingUint, 32); |
| 77 | + CompilerType enum_type = m_type_system->CreateEnumerationType( |
| 78 | + "TestEnum", m_type_system->GetTranslationUnitDecl(), |
| 79 | + OptionalClangModuleID(), Declaration(), uint_type, false); |
| 80 | + |
| 81 | + m_type_system->StartTagDeclarationDefinition(enum_type); |
| 82 | + Declaration decl; |
| 83 | + for (auto [name, value] : enumerators) |
| 84 | + m_type_system->AddEnumerationValueToEnumerationType(enum_type, decl, name, |
| 85 | + value, 32); |
| 86 | + m_type_system->CompleteTagDeclarationDefinition(enum_type); |
| 87 | + |
| 88 | + return enum_type; |
| 89 | + } |
| 90 | + |
| 91 | + void TestDumpValueObject( |
| 92 | + CompilerType enum_type, |
| 93 | + const std::vector< |
| 94 | + std::tuple<uint32_t, DumpValueObjectOptions, const char *>> &tests) { |
| 95 | + StreamString strm; |
| 96 | + ConstString var_name("test_var"); |
| 97 | + ByteOrder endian = endian::InlHostByteOrder(); |
| 98 | + ExecutionContextScope *exe_scope = m_exe_ctx.GetBestExecutionContextScope(); |
| 99 | + for (auto [value, options, expected] : tests) { |
| 100 | + DataExtractor data_extractor{&value, sizeof(value), endian, 4}; |
| 101 | + ValueObjectConstResult::Create(exe_scope, enum_type, var_name, |
| 102 | + data_extractor) |
| 103 | + ->Dump(strm, options); |
| 104 | + ASSERT_STREQ(strm.GetString().str().c_str(), expected); |
| 105 | + strm.Clear(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + ExecutionContext m_exe_ctx; |
| 110 | + TypeSystemClang *m_type_system; |
| 111 | + |
| 112 | +private: |
| 113 | + SubsystemRAII<FileSystem, HostInfo, platform_linux::PlatformLinux, |
| 114 | + ScriptInterpreterNone> |
| 115 | + m_subsystems; |
| 116 | + |
| 117 | + std::unique_ptr<clang_utils::TypeSystemClangHolder> m_holder; |
| 118 | + lldb::DebuggerSP m_debugger_sp; |
| 119 | + lldb::TargetSP m_target_sp; |
| 120 | + lldb::PlatformSP m_platform_sp; |
| 121 | + lldb::ListenerSP m_listener_sp; |
| 122 | + lldb::ProcessSP m_process_sp; |
| 123 | +}; |
| 124 | + |
| 125 | +TEST_F(ValueObjectMockProcessTest, Enum) { |
| 126 | + // This is not a bitfield-like enum, so values are printed as decimal by |
| 127 | + // default. Also we only show the enumerator name if the value is an |
| 128 | + // exact match. |
| 129 | + TestDumpValueObject( |
| 130 | + MakeEnumType({{"test_2", 2}, {"test_3", 3}}), |
| 131 | + {{0, {}, "(TestEnum) test_var = 0\n"}, |
| 132 | + {1, {}, "(TestEnum) test_var = 1\n"}, |
| 133 | + {2, {}, "(TestEnum) test_var = test_2\n"}, |
| 134 | + {3, {}, "(TestEnum) test_var = test_3\n"}, |
| 135 | + {4, {}, "(TestEnum) test_var = 4\n"}, |
| 136 | + {5, {}, "(TestEnum) test_var = 5\n"}, |
| 137 | + {1, DumpValueObjectOptions().SetHideRootName(true), "(TestEnum) 1\n"}, |
| 138 | + {1, DumpValueObjectOptions().SetHideRootType(true), "test_var = 1\n"}, |
| 139 | + {1, DumpValueObjectOptions().SetHideRootName(true).SetHideRootType(true), |
| 140 | + "1\n"}, |
| 141 | + {1, DumpValueObjectOptions().SetHideName(true), "(TestEnum) 1\n"}, |
| 142 | + {1, DumpValueObjectOptions().SetHideValue(true), |
| 143 | + "(TestEnum) test_var =\n"}, |
| 144 | + {1, DumpValueObjectOptions().SetHideName(true).SetHideValue(true), |
| 145 | + "(TestEnum) \n"}}); |
| 146 | +} |
| 147 | + |
| 148 | +TEST_F(ValueObjectMockProcessTest, BitFieldLikeEnum) { |
| 149 | + // These enumerators set individual bits in the value, as if it were a flag |
| 150 | + // set. lldb treats this as a "bitfield like enum". This means we show values |
| 151 | + // as hex, a value of 0 shows nothing, and values with no exact enumerator are |
| 152 | + // shown as combinations of the other values. |
| 153 | + TestDumpValueObject( |
| 154 | + MakeEnumType({{"test_2", 2}, {"test_4", 4}}), |
| 155 | + { |
| 156 | + {0, {}, "(TestEnum) test_var =\n"}, |
| 157 | + {1, {}, "(TestEnum) test_var = 0x1\n"}, |
| 158 | + {2, {}, "(TestEnum) test_var = test_2\n"}, |
| 159 | + {4, {}, "(TestEnum) test_var = test_4\n"}, |
| 160 | + {6, {}, "(TestEnum) test_var = test_2 | test_4\n"}, |
| 161 | + {7, {}, "(TestEnum) test_var = test_2 | test_4 | 0x1\n"}, |
| 162 | + {8, {}, "(TestEnum) test_var = 0x8\n"}, |
| 163 | + {1, DumpValueObjectOptions().SetHideRootName(true), |
| 164 | + "(TestEnum) 0x1\n"}, |
| 165 | + {1, DumpValueObjectOptions().SetHideRootType(true), |
| 166 | + "test_var = 0x1\n"}, |
| 167 | + {1, |
| 168 | + DumpValueObjectOptions().SetHideRootName(true).SetHideRootType(true), |
| 169 | + "0x1\n"}, |
| 170 | + {1, DumpValueObjectOptions().SetHideName(true), "(TestEnum) 0x1\n"}, |
| 171 | + {1, DumpValueObjectOptions().SetHideValue(true), |
| 172 | + "(TestEnum) test_var =\n"}, |
| 173 | + {1, DumpValueObjectOptions().SetHideName(true).SetHideValue(true), |
| 174 | + "(TestEnum) \n"}, |
| 175 | + }); |
| 176 | +} |
0 commit comments