Skip to content

Commit

Permalink
[lldb/Utility] Introduce UnimplementedError
Browse files Browse the repository at this point in the history
This is essentially a replacement for the PacketUnimplementedError
previously present in the gdb-remote server code.

The reason I am introducing a generic error is because I wanted the
native process classes to be able to signal that they do not support
some functionality. They could not use PacketUnimplementedError as they
are independent of a specific transport protocol. Putting the error
class in the the native process code was also not ideal because the
gdb-remote code is also used for lldb-server's platform mode, which does
not (should not) know how to debug individual processes.

I'm putting it under Utility, as I think it can be generally useful for
notifying about unsupported/unimplemented functionality (and in
particular, for programatically testing whether something is
unsupported).

Differential Revision: https://reviews.llvm.org/D89121
  • Loading branch information
labath committed Oct 12, 2020
1 parent 8f1de22 commit e2f1fe3
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 30 deletions.
28 changes: 28 additions & 0 deletions lldb/include/lldb/Utility/UnimplementedError.h
@@ -0,0 +1,28 @@
//===-- UnimplementedError.h ------------------------------------*- 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
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_UTILITY_UNIMPLEMENTEDERROR_H
#define LLDB_UTILITY_UNIMPLEMENTEDERROR_H

#include "llvm/Support/Errc.h"
#include "llvm/Support/Error.h"

namespace lldb_private {
class UnimplementedError : public llvm::ErrorInfo<UnimplementedError> {
public:
static char ID;

void log(llvm::raw_ostream &OS) const override { OS << "Not implemented"; }

std::error_code convertToErrorCode() const override {
return llvm::errc::not_supported;
};
};
} // namespace lldb_private

#endif // LLDB_UTILITY_UNIMPLEMENTEDERROR_H
Expand Up @@ -12,11 +12,11 @@

#include "GDBRemoteCommunicationServer.h"

#include <cstring>

#include "ProcessGDBRemoteLog.h"
#include "lldb/Utility/StreamString.h"
#include "lldb/Utility/StringExtractorGDBRemote.h"
#include "lldb/Utility/UnimplementedError.h"
#include <cstring>

using namespace lldb;
using namespace lldb_private;
Expand Down Expand Up @@ -113,18 +113,17 @@ GDBRemoteCommunicationServer::SendErrorResponse(const Status &error) {

GDBRemoteCommunication::PacketResult
GDBRemoteCommunicationServer::SendErrorResponse(llvm::Error error) {
assert(error);
std::unique_ptr<llvm::ErrorInfoBase> EIB;
std::unique_ptr<PacketUnimplementedError> PUE;
std::unique_ptr<UnimplementedError> UE;
llvm::handleAllErrors(
std::move(error),
[&](std::unique_ptr<PacketUnimplementedError> E) { PUE = std::move(E); },
[&](std::unique_ptr<UnimplementedError> E) { UE = std::move(E); },
[&](std::unique_ptr<llvm::ErrorInfoBase> E) { EIB = std::move(E); });

if (EIB)
return SendErrorResponse(Status(llvm::Error(std::move(EIB))));
if (PUE)
return SendUnimplementedResponse(PUE->message().c_str());
return SendErrorResponse(Status("Unknown Error"));
return SendUnimplementedResponse("");
}

GDBRemoteCommunication::PacketResult
Expand Down Expand Up @@ -152,5 +151,3 @@ GDBRemoteCommunicationServer::SendOKResponse() {
bool GDBRemoteCommunicationServer::HandshakeWithClient() {
return GetAck() == PacketResult::Success;
}

char PacketUnimplementedError::ID;
Expand Up @@ -79,18 +79,6 @@ class GDBRemoteCommunicationServer : public GDBRemoteCommunication {
operator=(const GDBRemoteCommunicationServer &) = delete;
};

class PacketUnimplementedError
: public llvm::ErrorInfo<PacketUnimplementedError, llvm::StringError> {
public:
static char ID;
using llvm::ErrorInfo<PacketUnimplementedError,
llvm::StringError>::ErrorInfo; // inherit constructors
PacketUnimplementedError(const llvm::Twine &S)
: ErrorInfo(S, llvm::errc::not_supported) {}

PacketUnimplementedError() : ErrorInfo(llvm::errc::not_supported) {}
};

} // namespace process_gdb_remote
} // namespace lldb_private

Expand Down
Expand Up @@ -10,13 +10,12 @@

#include "lldb/Host/Config.h"

#include "GDBRemoteCommunicationServerLLGS.h"
#include "lldb/Utility/GDBRemote.h"

#include <chrono>
#include <cstring>
#include <thread>

#include "GDBRemoteCommunicationServerLLGS.h"
#include "lldb/Host/ConnectionFileDescriptor.h"
#include "lldb/Host/Debug.h"
#include "lldb/Host/File.h"
Expand All @@ -32,11 +31,13 @@
#include "lldb/Utility/Args.h"
#include "lldb/Utility/DataBuffer.h"
#include "lldb/Utility/Endian.h"
#include "lldb/Utility/GDBRemote.h"
#include "lldb/Utility/LLDBAssert.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/RegisterValue.h"
#include "lldb/Utility/State.h"
#include "lldb/Utility/StreamString.h"
#include "lldb/Utility/UnimplementedError.h"
#include "lldb/Utility/UriParser.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Support/JSON.h"
Expand Down Expand Up @@ -2876,8 +2877,7 @@ GDBRemoteCommunicationServerLLGS::ReadXferObject(llvm::StringRef object,
if (object == "features" && annex == "target.xml")
return BuildTargetXml();

return llvm::make_error<PacketUnimplementedError>(
"Xfer object not supported");
return llvm::make_error<UnimplementedError>();
}

GDBRemoteCommunication::PacketResult
Expand Down
1 change: 1 addition & 0 deletions lldb/source/Utility/CMakeLists.txt
Expand Up @@ -65,6 +65,7 @@ add_lldb_library(lldbUtility
StructuredData.cpp
TildeExpressionResolver.cpp
Timer.cpp
UnimplementedError.cpp
UUID.cpp
UriParser.cpp
UserID.cpp
Expand Down
11 changes: 11 additions & 0 deletions lldb/source/Utility/UnimplementedError.cpp
@@ -0,0 +1,11 @@
//===-- UnimplementedError.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/Utility/UnimplementedError.h"

char lldb_private::UnimplementedError::ID;
Expand Up @@ -9,9 +9,9 @@
#include "gtest/gtest.h"

#include "GDBRemoteTestUtils.h"

#include "Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.h"
#include "lldb/Utility/Connection.h"
#include "lldb/Utility/UnimplementedError.h"

namespace lldb_private {
namespace process_gdb_remote {
Expand Down Expand Up @@ -39,8 +39,7 @@ TEST(GDBRemoteCommunicationServerTest, SendErrorResponse_Status) {
TEST(GDBRemoteCommunicationServerTest, SendErrorResponse_UnimplementedError) {
MockServerWithMockConnection server;

auto error =
llvm::make_error<PacketUnimplementedError>("Test unimplemented error");
auto error = llvm::make_error<UnimplementedError>();
server.SendErrorResponse(std::move(error));

EXPECT_THAT(server.GetPackets(), testing::ElementsAre("$#00"));
Expand All @@ -61,8 +60,8 @@ TEST(GDBRemoteCommunicationServerTest, SendErrorResponse_StringError) {
TEST(GDBRemoteCommunicationServerTest, SendErrorResponse_ErrorList) {
MockServerWithMockConnection server;

auto error = llvm::joinErrors(llvm::make_error<PacketUnimplementedError>(),
llvm::make_error<PacketUnimplementedError>());
auto error = llvm::joinErrors(llvm::make_error<UnimplementedError>(),
llvm::make_error<UnimplementedError>());

server.SendErrorResponse(std::move(error));
// Make sure only one packet is sent even when there are multiple errors.
Expand Down

0 comments on commit e2f1fe3

Please sign in to comment.