diff options
author | Jonas Devlieghere <jonas@devlieghere.com> | 2023-03-08 18:28:50 -0800 |
---|---|---|
committer | Jonas Devlieghere <jonas@devlieghere.com> | 2023-03-08 20:56:11 -0800 |
commit | cf3524a5746f9498280b3a9180b75575c0065d1a (patch) | |
tree | 8656caaac35964271d0c631cfb8e3d1f70163098 /lldb/unittests | |
parent | 8cf85a0cadb033fed3d96aa5283deb4bfbbaf2c8 (diff) | |
download | llvm-cf3524a5746f9498280b3a9180b75575c0065d1a.tar.gz |
[lldb] Introduce new SymbolFileJSON and ObjectFileJSON
Introduce a new object and symbol file format with the goal of mapping
addresses to symbol names. I'd like to think of is as an extremely
simple textual symtab. The file format consists of a triple, a UUID and
a list of symbols. JSON is used for the encoding, but that's mostly an
implementation detail. The goal of the format was to be simple and human
readable.
The new file format is motivated by two use cases:
- Stripped binaries: when a binary is stripped, you lose the ability to
do thing like setting symbolic breakpoints. You can keep the
unstripped binary around, but if all you need is the stripped
symbols then that's a lot of overhead. Instead, we could save the
stripped symbols to a file and load them in the debugger when
needed. I want to extend llvm-strip to have a mode where it emits
this new file format.
- Interactive crashlogs: with interactive crashlogs, if we don't have
the binary or the dSYM for a particular module, we currently show an
unnamed symbol for those frames. This is a regression compared to the
textual format, that has these frames pre-symbolicated. Given that
this information is available in the JSON crashlog, we need a way to
tell LLDB about it. With the new symbol file format, we can easily
synthesize a symbol file for each of those modules and load them to
symbolicate those frames.
Here's an example of the file format:
{
"triple": "arm64-apple-macosx13.0.0",
"uuid": "36D0CCE7-8ED2-3CA3-96B0-48C1764DA908",
"symbols": [
{
"name": "main",
"type": "code",
"size": 32,
"address": 4294983568
},
{
"name": "foo",
"type": "code",
"size": 8,
"address": 4294983560
}
]
}
Differential revision: https://reviews.llvm.org/D145180
Diffstat (limited to 'lldb/unittests')
-rw-r--r-- | lldb/unittests/Symbol/CMakeLists.txt | 1 | ||||
-rw-r--r-- | lldb/unittests/Symbol/JSONSymbolTest.cpp | 192 |
2 files changed, 193 insertions, 0 deletions
diff --git a/lldb/unittests/Symbol/CMakeLists.txt b/lldb/unittests/Symbol/CMakeLists.txt index 60c0a9bdcf46..e1d24357e33d 100644 --- a/lldb/unittests/Symbol/CMakeLists.txt +++ b/lldb/unittests/Symbol/CMakeLists.txt @@ -1,4 +1,5 @@ add_lldb_unittest(SymbolTests + JSONSymbolTest.cpp LocateSymbolFileTest.cpp MangledTest.cpp PostfixExpressionTest.cpp diff --git a/lldb/unittests/Symbol/JSONSymbolTest.cpp b/lldb/unittests/Symbol/JSONSymbolTest.cpp new file mode 100644 index 000000000000..76c34b89f902 --- /dev/null +++ b/lldb/unittests/Symbol/JSONSymbolTest.cpp @@ -0,0 +1,192 @@ +//===-- JSONSymbolTest.cpp ------------------------------------------------===// +// +// 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/Core/Section.h" +#include "lldb/Symbol/Symbol.h" +#include "llvm/Testing/Support/Error.h" + +#include "gtest/gtest.h" + +using namespace lldb; +using namespace llvm; +using namespace lldb_private; + +static std::string g_error_no_section_list = "no section list provided"; +static std::string g_error_both_value_and_address = + "symbol cannot contain both a value and an address"; +static std::string g_error_neither_value_or_address = + "symbol must contain either a value or an address"; + +TEST(JSONSymbolTest, DeserializeCodeAddress) { + std::string text = R"( +{ + "name": "foo", + "type": "code", + "size": 32, + "address": 4096 +})"; + + Expected<json::Value> json = json::parse(text); + ASSERT_TRUE(static_cast<bool>(json)); + + json::Path::Root root; + JSONSymbol json_symbol; + ASSERT_TRUE(fromJSON(*json, json_symbol, root)); + + SectionSP sect_sp(new Section( + /*module_sp=*/ModuleSP(), + /*obj_file=*/nullptr, + /*sect_id=*/1, + /*name=*/ConstString(".text"), + /*sect_type=*/eSectionTypeCode, + /*file_vm_addr=*/0x1000, + /*vm_size=*/0x1000, + /*file_offset=*/0, + /*file_size=*/0, + /*log2align=*/5, + /*flags=*/0x10203040)); + SectionList sect_list; + sect_list.AddSection(sect_sp); + + Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, §_list); + EXPECT_THAT_EXPECTED(symbol, llvm::Succeeded()); + EXPECT_EQ(symbol->GetName(), ConstString("foo")); + EXPECT_EQ(symbol->GetFileAddress(), static_cast<lldb::addr_t>(0x1000)); + EXPECT_EQ(symbol->GetType(), eSymbolTypeCode); +} + +TEST(JSONSymbolTest, DeserializeCodeValue) { + std::string text = R"( +{ + "name": "foo", + "type": "code", + "size": 32, + "value": 4096 +})"; + + Expected<json::Value> json = json::parse(text); + EXPECT_THAT_EXPECTED(json, llvm::Succeeded()); + + json::Path::Root root; + JSONSymbol json_symbol; + ASSERT_TRUE(fromJSON(*json, json_symbol, root)); + + SectionList sect_list; + + Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, §_list); + EXPECT_THAT_EXPECTED(symbol, llvm::Succeeded()); + EXPECT_EQ(symbol->GetName(), ConstString("foo")); + EXPECT_EQ(symbol->GetRawValue(), static_cast<lldb::addr_t>(0x1000)); + EXPECT_EQ(symbol->GetType(), eSymbolTypeCode); +} + +TEST(JSONSymbolTest, JSONInvalidValueAndAddress) { + std::string text = R"( +{ + "name": "foo", + "type": "code", + "size": 32, + "value": 4096, + "address": 4096 +})"; + + Expected<json::Value> json = json::parse(text); + EXPECT_THAT_EXPECTED(json, llvm::Succeeded()); + + json::Path::Root root; + JSONSymbol json_symbol; + ASSERT_FALSE(fromJSON(*json, json_symbol, root)); +} + +TEST(JSONSymbolTest, JSONInvalidNoValueOrAddress) { + std::string text = R"( +{ + "name": "foo", + "type": "code", + "size": 32 +})"; + + Expected<json::Value> json = json::parse(text); + EXPECT_THAT_EXPECTED(json, llvm::Succeeded()); + + json::Path::Root root; + JSONSymbol json_symbol; + ASSERT_FALSE(fromJSON(*json, json_symbol, root)); +} + +TEST(JSONSymbolTest, JSONInvalidType) { + std::string text = R"( +{ + "name": "foo", + "type": "bogus", + "value": 4096, + "size": 32 +})"; + + Expected<json::Value> json = json::parse(text); + EXPECT_THAT_EXPECTED(json, llvm::Succeeded()); + + json::Path::Root root; + JSONSymbol json_symbol; + ASSERT_FALSE(fromJSON(*json, json_symbol, root)); +} + +TEST(JSONSymbolTest, SymbolInvalidNoSectionList) { + JSONSymbol json_symbol; + json_symbol.value = 0x1; + + Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, nullptr); + EXPECT_THAT_EXPECTED(symbol, + llvm::FailedWithMessage(g_error_no_section_list)); +} + +TEST(JSONSymbolTest, SymbolInvalidValueAndAddress) { + JSONSymbol json_symbol; + json_symbol.value = 0x1; + json_symbol.address = 0x2; + + SectionList sect_list; + + Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, §_list); + EXPECT_THAT_EXPECTED(symbol, + llvm::FailedWithMessage(g_error_both_value_and_address)); +} + +TEST(JSONSymbolTest, SymbolInvalidNoValueOrAddress) { + JSONSymbol json_symbol; + + SectionList sect_list; + + Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, §_list); + EXPECT_THAT_EXPECTED( + symbol, llvm::FailedWithMessage(g_error_neither_value_or_address)); +} + +TEST(JSONSymbolTest, SymbolInvalidAddressNotInSection) { + JSONSymbol json_symbol; + json_symbol.address = 0x0fff; + + SectionSP sect_sp(new Section( + /*module_sp=*/ModuleSP(), + /*obj_file=*/nullptr, + /*sect_id=*/1, + /*name=*/ConstString(".text"), + /*sect_type=*/eSectionTypeCode, + /*file_vm_addr=*/0x1000, + /*vm_size=*/0x1000, + /*file_offset=*/0, + /*file_size=*/0, + /*log2align=*/5, + /*flags=*/0x10203040)); + SectionList sect_list; + sect_list.AddSection(sect_sp); + + Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, §_list); + EXPECT_THAT_EXPECTED( + symbol, llvm::FailedWithMessage("no section found for address: 0xfff")); +} |