Index: lldb/docs/lldb-platform-packets.txt =================================================================== --- lldb/docs/lldb-platform-packets.txt +++ lldb/docs/lldb-platform-packets.txt @@ -237,6 +237,23 @@ // Continues to return the results of the qfProcessInfo. Once all matches // have been sent, Exx is returned to indicate end of matches. +//---------------------------------------------------------------------- +// qDiskAutocomplete +// +// BRIEF +// Get a list of matched disk files/directories by passing a boolean flag +// and a partial path. +// +// EXAMPLE +// +// receive: qDiskAutocomplete:0,6d61696e +// send: 00000008:6d61696e2e637070 +// receive: qDiskAutocomplete:1,746573 +// send: 00000005:746573742f00000006:74657374732f +// +// The boolean flag indicates if it is a file or directory completion. +// Matches are organized like [length1]:[result1][length2]:[result2]... + //---------------------------------------------------------------------- // vFile:size: // Index: lldb/include/lldb/Interpreter/CommandCompletions.h =================================================================== --- lldb/include/lldb/Interpreter/CommandCompletions.h +++ lldb/include/lldb/Interpreter/CommandCompletions.h @@ -37,10 +37,12 @@ eRegisterCompletion = (1u << 9), eBreakpointCompletion = (1u << 10), eProcessPluginCompletion = (1u << 11), + eRemoteDiskFileCompletion = (1u << 12), + eRemoteDiskDirectoryCompletion = (1u << 13), // This item serves two purposes. It is the last element in the enum, so // you can add custom enums starting from here in your Option class. Also // if you & in this bit the base code will not process the option. - eCustomCompletion = (1u << 12) + eCustomCompletion = (1u << 14) }; static bool InvokeCommonCompletionCallbacks( @@ -62,6 +64,14 @@ StringList &matches, TildeExpressionResolver &Resolver); + static void RemoteDiskFiles(CommandInterpreter &interpreter, + CompletionRequest &request, + SearchFilter *searcher); + + static void RemoteDiskDirectories(CommandInterpreter &interpreter, + CompletionRequest &request, + SearchFilter *searcher); + static void SourceFiles(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher); Index: lldb/include/lldb/Target/Platform.h =================================================================== --- lldb/include/lldb/Target/Platform.h +++ lldb/include/lldb/Target/Platform.h @@ -523,6 +523,9 @@ return UINT64_MAX; } + virtual void AutoCompleteDiskFileOrDirectory(CompletionRequest &request, + bool only_dir) {} + virtual uint64_t ReadFile(lldb::user_id_t fd, uint64_t offset, void *dst, uint64_t dst_len, Status &error) { error.SetErrorStringWithFormat( Index: lldb/include/lldb/Utility/StringExtractorGDBRemote.h =================================================================== --- lldb/include/lldb/Utility/StringExtractorGDBRemote.h +++ lldb/include/lldb/Utility/StringExtractorGDBRemote.h @@ -66,6 +66,7 @@ eServerPacketType_qUserName, eServerPacketType_qGetWorkingDir, eServerPacketType_qFileLoadAddress, + eServerPacketType_qDiskFileAutocomplete, eServerPacketType_QEnvironment, eServerPacketType_QEnableErrorStrings, eServerPacketType_QLaunchArch, Index: lldb/source/Commands/CommandCompletions.cpp =================================================================== --- lldb/source/Commands/CommandCompletions.cpp +++ lldb/source/Commands/CommandCompletions.cpp @@ -59,6 +59,9 @@ {eRegisterCompletion, CommandCompletions::Registers}, {eBreakpointCompletion, CommandCompletions::Breakpoints}, {eProcessPluginCompletion, CommandCompletions::ProcessPluginNames}, + {eRemoteDiskFileCompletion, CommandCompletions::RemoteDiskFiles}, + {eRemoteDiskDirectoryCompletion, + CommandCompletions::RemoteDiskDirectories}, {eNoCompletion, nullptr} // This one has to be last in the list. }; @@ -472,6 +475,24 @@ DiskFilesOrDirectories(partial_file_name, true, matches, Resolver); } +void CommandCompletions::RemoteDiskFiles(CommandInterpreter &interpreter, + CompletionRequest &request, + SearchFilter *searcher) { + lldb::PlatformSP platform_sp = + interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform(); + if (platform_sp) + platform_sp->AutoCompleteDiskFileOrDirectory(request, false); +} + +void CommandCompletions::RemoteDiskDirectories(CommandInterpreter &interpreter, + CompletionRequest &request, + SearchFilter *searcher) { + lldb::PlatformSP platform_sp = + interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform(); + if (platform_sp) + platform_sp->AutoCompleteDiskFileOrDirectory(request, true); +} + void CommandCompletions::Modules(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher) { Index: lldb/source/Commands/CommandObjectPlatform.cpp =================================================================== --- lldb/source/Commands/CommandObjectPlatform.cpp +++ lldb/source/Commands/CommandObjectPlatform.cpp @@ -392,7 +392,8 @@ "or for a platform by name.", "platform settings", 0), m_options(), - m_option_working_dir(LLDB_OPT_SET_1, false, "working-dir", 'w', 0, + m_option_working_dir(LLDB_OPT_SET_1, false, "working-dir", 'w', + CommandCompletions::eRemoteDiskDirectoryCompletion, eArgTypePath, "The working directory for the platform.") { m_options.Append(&m_option_working_dir, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); @@ -485,6 +486,15 @@ ~CommandObjectPlatformFOpen() override = default; + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { + if (request.GetCursorIndex() == 0) + CommandCompletions::InvokeCommonCompletionCallbacks( + GetCommandInterpreter(), + CommandCompletions::eRemoteDiskFileCompletion, request, nullptr); + } + bool DoExecute(Args &args, CommandReturnObject &result) override { PlatformSP platform_sp( GetDebugger().GetPlatformList().GetSelectedPlatform()); @@ -817,6 +827,19 @@ ~CommandObjectPlatformGetFile() override = default; + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { + if (request.GetCursorIndex() == 0) + CommandCompletions::InvokeCommonCompletionCallbacks( + GetCommandInterpreter(), + CommandCompletions::eRemoteDiskFileCompletion, request, nullptr); + else if (request.GetCursorIndex() == 1) + CommandCompletions::InvokeCommonCompletionCallbacks( + GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, + request, nullptr); + } + bool DoExecute(Args &args, CommandReturnObject &result) override { // If the number of arguments is incorrect, issue an error message. if (args.GetArgumentCount() != 2) { @@ -882,6 +905,17 @@ ~CommandObjectPlatformGetSize() override = default; + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { + if (request.GetCursorIndex() != 0) + return; + + CommandCompletions::InvokeCommonCompletionCallbacks( + GetCommandInterpreter(), CommandCompletions::eRemoteDiskFileCompletion, + request, nullptr); + } + bool DoExecute(Args &args, CommandReturnObject &result) override { // If the number of arguments is incorrect, issue an error message. if (args.GetArgumentCount() != 1) { @@ -927,6 +961,19 @@ ~CommandObjectPlatformPutFile() override = default; + void + HandleArgumentCompletion(CompletionRequest &request, + OptionElementVector &opt_element_vector) override { + if (request.GetCursorIndex() == 0) + CommandCompletions::InvokeCommonCompletionCallbacks( + GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, + request, nullptr); + else if (request.GetCursorIndex() == 1) + CommandCompletions::InvokeCommonCompletionCallbacks( + GetCommandInterpreter(), + CommandCompletions::eRemoteDiskFileCompletion, request, nullptr); + } + bool DoExecute(Args &args, CommandReturnObject &result) override { const char *src = args.GetArgumentAtIndex(0); const char *dst = args.GetArgumentAtIndex(1); Index: lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h =================================================================== --- lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h +++ lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h @@ -127,6 +127,9 @@ lldb::user_id_t GetFileSize(const FileSpec &file_spec) override; + void AutoCompleteDiskFileOrDirectory(CompletionRequest &request, + bool only_dir) override; + Status PutFile(const FileSpec &source, const FileSpec &destination, uint32_t uid = UINT32_MAX, uint32_t gid = UINT32_MAX) override; Index: lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp =================================================================== --- lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp +++ lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp @@ -661,6 +661,11 @@ return m_gdb_client.GetFileSize(file_spec); } +void PlatformRemoteGDBServer::AutoCompleteDiskFileOrDirectory( + CompletionRequest &request, bool only_dir) { + m_gdb_client.AutoCompleteDiskFileOrDirectory(request, only_dir); +} + uint64_t PlatformRemoteGDBServer::ReadFile(lldb::user_id_t fd, uint64_t offset, void *dst, uint64_t dst_len, Status &error) { Index: lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h =================================================================== --- lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h +++ lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h @@ -375,6 +375,9 @@ lldb::user_id_t GetFileSize(const FileSpec &file_spec); + void AutoCompleteDiskFileOrDirectory(CompletionRequest &request, + bool only_dir); + Status GetFilePermissions(const FileSpec &file_spec, uint32_t &file_permissions); Index: lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp =================================================================== --- lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp +++ lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp @@ -2981,6 +2981,28 @@ return UINT64_MAX; } +void GDBRemoteCommunicationClient::AutoCompleteDiskFileOrDirectory( + CompletionRequest &request, bool only_dir) { + lldb_private::StreamString stream; + stream.PutCString("qDiskAutocomplete:"); + stream.PutHex32(only_dir ? 1 : 0); + stream.PutChar(','); + stream.PutStringAsRawHex8(request.GetCursorArgumentPrefix()); + StringExtractorGDBRemote response; + if (SendPacketAndWaitForResponse(stream.GetString(), response, false) == + PacketResult::Success) { + size_t match_len = 0; + while ((match_len = response.GetHexMaxU32(false, 0)) != 0) { + if (response.GetChar() != ':') + break; + StreamString strm; + while (match_len--) + strm.PutChar(response.GetHexU8()); + request.AddCompletion(strm.GetString()); + } + } +} + Status GDBRemoteCommunicationClient::GetFilePermissions(const FileSpec &file_spec, uint32_t &file_permissions) { Index: lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.h =================================================================== --- lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.h +++ lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.h @@ -77,6 +77,8 @@ PacketResult Handle_vFile_MD5(StringExtractorGDBRemote &packet); + PacketResult Handle_qDiskFileAutoComplete(StringExtractorGDBRemote &packet); + PacketResult Handle_qEcho(StringExtractorGDBRemote &packet); PacketResult Handle_qModuleInfo(StringExtractorGDBRemote &packet); Index: lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp =================================================================== --- lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp +++ lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp @@ -25,6 +25,7 @@ #include "lldb/Host/Host.h" #include "lldb/Host/HostInfo.h" #include "lldb/Host/SafeMachO.h" +#include "lldb/Interpreter/CommandCompletions.h" #include "lldb/Interpreter/OptionArgParser.h" #include "lldb/Symbol/ObjectFile.h" #include "lldb/Target/Platform.h" @@ -33,6 +34,7 @@ #include "lldb/Utility/Log.h" #include "lldb/Utility/StreamString.h" #include "lldb/Utility/StructuredData.h" +#include "lldb/Utility/TildeExpressionResolver.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/Triple.h" #include "llvm/Support/JSON.h" @@ -88,6 +90,9 @@ RegisterMemberFunctionHandler( StringExtractorGDBRemote::eServerPacketType_QListThreadsInStopReply, &GDBRemoteCommunicationServerCommon::Handle_QListThreadsInStopReply); + RegisterMemberFunctionHandler( + StringExtractorGDBRemote::eServerPacketType_qDiskFileAutocomplete, + &GDBRemoteCommunicationServerCommon::Handle_qDiskFileAutoComplete); RegisterMemberFunctionHandler( StringExtractorGDBRemote::eServerPacketType_qEcho, &GDBRemoteCommunicationServerCommon::Handle_qEcho); @@ -723,6 +728,36 @@ return SendPacketNoLock(response.GetString()); } +GDBRemoteCommunication::PacketResult +GDBRemoteCommunicationServerCommon::Handle_qDiskFileAutoComplete( + StringExtractorGDBRemote &packet) { + packet.SetFilePos(::strlen("qDiskAutocomplete:")); + const bool only_dir = (packet.GetHexMaxU32(false, 0) == 1); + if (packet.GetChar() != ',') + return SendErrorResponse(85); + std::string path; + packet.GetHexByteString(path); + + StringList matches; + StandardTildeExpressionResolver resolver; + if (only_dir) + CommandCompletions::DiskDirectories(path, matches, resolver); + else + CommandCompletions::DiskFiles(path, matches, resolver); + + StreamString response; + for (const auto &match : matches) { + // matched strings are organized like "00000003:abc00000004:1234". + response.PutHex32(match.size()); + response.PutChar(':'); + // encode result strings into hex bytes to avoid unexpected error caused by + // special characters like '$'. + response.PutStringAsRawHex8(match.c_str()); + } + + return SendPacketNoLock(response.GetString()); +} + GDBRemoteCommunication::PacketResult GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell( StringExtractorGDBRemote &packet) { Index: lldb/source/Utility/StringExtractorGDBRemote.cpp =================================================================== --- lldb/source/Utility/StringExtractorGDBRemote.cpp +++ lldb/source/Utility/StringExtractorGDBRemote.cpp @@ -173,6 +173,11 @@ return eServerPacketType_qC; break; + case 'D': + if (PACKET_STARTS_WITH("qDiskAutocomplete:")) + return eServerPacketType_qDiskFileAutocomplete; + break; + case 'E': if (PACKET_STARTS_WITH("qEcho:")) return eServerPacketType_qEcho; Index: lldb/test/API/functionalities/gdb_remote_client/TestGDBRemoteDiskFileCompletion.py =================================================================== --- /dev/null +++ lldb/test/API/functionalities/gdb_remote_client/TestGDBRemoteDiskFileCompletion.py @@ -0,0 +1,20 @@ +from gdbclientutils import GDBRemoteTestBase + +class TestGDBRemoteDiskFileCompletion(GDBRemoteTestBase): + + def test_autocomplete_request(self): + """Test remote disk completion on remote-gdb-server plugin""" + + try: + self.runCmd("platform select remote-gdb-server") + self.runCmd("platform connect connect://localhost:%d" % + self.server.port) + self.assertTrue(self.dbg.GetSelectedPlatform().IsConnected()) + + self.complete_from_to('platform get-size t', ['test', '123']) + self.complete_from_to('platform get-file t', ['test', '123']) + self.complete_from_to('platform put-file foo t', ['test', '123']) + self.complete_from_to('platform file open t', ['test', '123']) + self.complete_from_to('platform settings -w t', ['test', '123']) + finally: + self.dbg.GetSelectedPlatform().DisconnectRemote() Index: lldb/test/API/functionalities/gdb_remote_client/gdbclientutils.py =================================================================== --- lldb/test/API/functionalities/gdb_remote_client/gdbclientutils.py +++ lldb/test/API/functionalities/gdb_remote_client/gdbclientutils.py @@ -178,6 +178,8 @@ return self.qsProcessInfo() if packet.startswith("qfProcessInfo"): return self.qfProcessInfo(packet) + if packet.startswith("qDiskAutocomplete:"): + return self.qDiskAutocomplete() return self.other(packet) @@ -282,6 +284,9 @@ def qMemoryRegionInfo(self): return "" + def qDiskAutocomplete(self): + return "00000004:7465737400000003:313233" + """ Raised when we receive a packet for which there is no default action. Override the responder class to implement behavior suitable for the test at Index: lldb/test/API/tools/lldb-server/TestGdbRemoteCompletion.py =================================================================== --- /dev/null +++ lldb/test/API/tools/lldb-server/TestGdbRemoteCompletion.py @@ -0,0 +1,32 @@ +import unittest2 +import gdbremote_testcase +from lldbsuite.test.decorators import llgs_test +from lldbsuite.test.lldbtest import * +from lldbsuite.test.lldbdwarf import DwarfOpcodeParser + +class GdbRemoteCompletionTestCase(gdbremote_testcase.GdbRemoteTestCaseBase, DwarfOpcodeParser): + + mydir = TestBase.compute_mydir(__file__) + + @llgs_test + def test_autocomplete_response(self): + self.init_llgs_test() + self.build() + + server = self.connect_to_debug_monitor() + self.assertIsNotNone(server) + + self.add_no_ack_remote_stream() + + # complete disk files + self.test_sequence.add_log_lines( + ["read packet: $qDiskAutocomplete:0,6d61696e#00", # 6d61696e is the byte array of string "main". + "send packet: $00000008:6d61696e2e637070#00"], # 6d61696e2e637070 is the byte array of string "main.cpp". + True) + # complete disk directories + self.test_sequence.add_log_lines( + ["read packet: $qDiskAutocomplete:1,746573#00", # 746573 is the byte array of string "tes". + "send packet: $00000005:746573742f#00"], # 746573742f is the byte array of string "test/". + True) + + self.expect_gdbremote_sequence()