1 //===-- CommandObjectPlatform.cpp -----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "CommandObjectPlatform.h"
10 #include "CommandOptionsProcessLaunch.h"
11 #include "lldb/Core/Debugger.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/PluginManager.h"
14 #include "lldb/Host/OptionParser.h"
15 #include "lldb/Interpreter/CommandInterpreter.h"
16 #include "lldb/Interpreter/CommandOptionArgumentTable.h"
17 #include "lldb/Interpreter/CommandOptionValidators.h"
18 #include "lldb/Interpreter/CommandReturnObject.h"
19 #include "lldb/Interpreter/OptionGroupFile.h"
20 #include "lldb/Interpreter/OptionGroupPlatform.h"
21 #include "lldb/Target/ExecutionContext.h"
22 #include "lldb/Target/Platform.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Utility/Args.h"
26 #include "llvm/ADT/SmallString.h"
29 using namespace lldb_private;
31 static mode_t ParsePermissionString(const char *) = delete;
33 static mode_t ParsePermissionString(llvm::StringRef permissions) {
34 if (permissions.size() != 9)
36 bool user_r, user_w, user_x, group_r, group_w, group_x, world_r, world_w,
39 user_r = (permissions[0] == 'r');
40 user_w = (permissions[1] == 'w');
41 user_x = (permissions[2] == 'x');
43 group_r = (permissions[3] == 'r');
44 group_w = (permissions[4] == 'w');
45 group_x = (permissions[5] == 'x');
47 world_r = (permissions[6] == 'r');
48 world_w = (permissions[7] == 'w');
49 world_x = (permissions[8] == 'x');
51 mode_t user, group, world;
52 user = (user_r ? 4 : 0) | (user_w ? 2 : 0) | (user_x ? 1 : 0);
53 group = (group_r ? 4 : 0) | (group_w ? 2 : 0) | (group_x ? 1 : 0);
54 world = (world_r ? 4 : 0) | (world_w ? 2 : 0) | (world_x ? 1 : 0);
56 return user | group | world;
59 #define LLDB_OPTIONS_permissions
60 #include "CommandOptions.inc"
62 class OptionPermissions : public OptionGroup {
64 OptionPermissions() = default;
66 ~OptionPermissions() override = default;
69 SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
70 ExecutionContext *execution_context) override {
72 char short_option = (char)GetDefinitions()[option_idx].short_option;
73 switch (short_option) {
75 if (option_arg.getAsInteger(8, m_permissions)) {
77 error.SetErrorStringWithFormat("invalid value for permissions: %s",
78 option_arg.str().c_str());
83 mode_t perms = ParsePermissionString(option_arg);
84 if (perms == (mode_t)-1)
85 error.SetErrorStringWithFormat("invalid value for permissions: %s",
86 option_arg.str().c_str());
88 m_permissions = perms;
91 m_permissions |= lldb::eFilePermissionsUserRead;
94 m_permissions |= lldb::eFilePermissionsUserWrite;
97 m_permissions |= lldb::eFilePermissionsUserExecute;
100 m_permissions |= lldb::eFilePermissionsGroupRead;
103 m_permissions |= lldb::eFilePermissionsGroupWrite;
106 m_permissions |= lldb::eFilePermissionsGroupExecute;
109 m_permissions |= lldb::eFilePermissionsWorldRead;
112 m_permissions |= lldb::eFilePermissionsWorldWrite;
115 m_permissions |= lldb::eFilePermissionsWorldExecute;
118 llvm_unreachable("Unimplemented option");
124 void OptionParsingStarting(ExecutionContext *execution_context) override {
128 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
129 return llvm::makeArrayRef(g_permissions_options);
132 // Instance variables to hold the values for command options.
134 uint32_t m_permissions;
137 OptionPermissions(const OptionPermissions &) = delete;
138 const OptionPermissions &operator=(const OptionPermissions &) = delete;
141 // "platform select <platform-name>"
142 class CommandObjectPlatformSelect : public CommandObjectParsed {
144 CommandObjectPlatformSelect(CommandInterpreter &interpreter)
145 : CommandObjectParsed(interpreter, "platform select",
146 "Create a platform if needed and select it as the "
148 "platform select <platform-name>", 0),
150 false) // Don't include the "--platform" option by passing false
152 m_option_group.Append(&m_platform_options, LLDB_OPT_SET_ALL, 1);
153 m_option_group.Finalize();
154 CommandArgumentData platform_arg{eArgTypePlatform, eArgRepeatPlain};
155 m_arguments.push_back({platform_arg});
158 ~CommandObjectPlatformSelect() override = default;
160 void HandleCompletion(CompletionRequest &request) override {
161 CommandCompletions::PlatformPluginNames(GetCommandInterpreter(), request,
165 Options *GetOptions() override { return &m_option_group; }
168 bool DoExecute(Args &args, CommandReturnObject &result) override {
169 if (args.GetArgumentCount() == 1) {
170 const char *platform_name = args.GetArgumentAtIndex(0);
171 if (platform_name && platform_name[0]) {
172 const bool select = true;
173 m_platform_options.SetPlatformName(platform_name);
175 ArchSpec platform_arch;
176 PlatformSP platform_sp(m_platform_options.CreatePlatformWithOptions(
177 m_interpreter, ArchSpec(), select, error, platform_arch));
179 GetDebugger().GetPlatformList().SetSelectedPlatform(platform_sp);
181 platform_sp->GetStatus(result.GetOutputStream());
182 result.SetStatus(eReturnStatusSuccessFinishResult);
184 result.AppendError(error.AsCString());
187 result.AppendError("invalid platform name");
191 "platform create takes a platform name as an argument\n");
193 return result.Succeeded();
196 OptionGroupOptions m_option_group;
197 OptionGroupPlatform m_platform_options;
201 class CommandObjectPlatformList : public CommandObjectParsed {
203 CommandObjectPlatformList(CommandInterpreter &interpreter)
204 : CommandObjectParsed(interpreter, "platform list",
205 "List all platforms that are available.", nullptr,
208 ~CommandObjectPlatformList() override = default;
211 bool DoExecute(Args &args, CommandReturnObject &result) override {
212 Stream &ostrm = result.GetOutputStream();
213 ostrm.Printf("Available platforms:\n");
215 PlatformSP host_platform_sp(Platform::GetHostPlatform());
216 ostrm.Format("{0}: {1}\n", host_platform_sp->GetPluginName(),
217 host_platform_sp->GetDescription());
220 for (idx = 0; true; ++idx) {
221 llvm::StringRef plugin_name =
222 PluginManager::GetPlatformPluginNameAtIndex(idx);
223 if (plugin_name.empty())
225 llvm::StringRef plugin_desc =
226 PluginManager::GetPlatformPluginDescriptionAtIndex(idx);
227 ostrm.Format("{0}: {1}\n", plugin_name, plugin_desc);
231 result.AppendError("no platforms are available\n");
233 result.SetStatus(eReturnStatusSuccessFinishResult);
234 return result.Succeeded();
239 class CommandObjectPlatformStatus : public CommandObjectParsed {
241 CommandObjectPlatformStatus(CommandInterpreter &interpreter)
242 : CommandObjectParsed(interpreter, "platform status",
243 "Display status for the current platform.", nullptr,
246 ~CommandObjectPlatformStatus() override = default;
249 bool DoExecute(Args &args, CommandReturnObject &result) override {
250 Stream &ostrm = result.GetOutputStream();
252 Target *target = GetDebugger().GetSelectedTarget().get();
253 PlatformSP platform_sp;
255 platform_sp = target->GetPlatform();
258 platform_sp = GetDebugger().GetPlatformList().GetSelectedPlatform();
261 platform_sp->GetStatus(ostrm);
262 result.SetStatus(eReturnStatusSuccessFinishResult);
264 result.AppendError("no platform is currently selected\n");
266 return result.Succeeded();
270 // "platform connect <connect-url>"
271 class CommandObjectPlatformConnect : public CommandObjectParsed {
273 CommandObjectPlatformConnect(CommandInterpreter &interpreter)
274 : CommandObjectParsed(
275 interpreter, "platform connect",
276 "Select the current platform by providing a connection URL.",
277 "platform connect <connect-url>", 0) {
278 CommandArgumentData platform_arg{eArgTypeConnectURL, eArgRepeatPlain};
279 m_arguments.push_back({platform_arg});
282 ~CommandObjectPlatformConnect() override = default;
285 bool DoExecute(Args &args, CommandReturnObject &result) override {
286 Stream &ostrm = result.GetOutputStream();
288 PlatformSP platform_sp(
289 GetDebugger().GetPlatformList().GetSelectedPlatform());
291 Status error(platform_sp->ConnectRemote(args));
292 if (error.Success()) {
293 platform_sp->GetStatus(ostrm);
294 result.SetStatus(eReturnStatusSuccessFinishResult);
296 platform_sp->ConnectToWaitingProcesses(GetDebugger(), error);
298 result.AppendError(error.AsCString());
301 result.AppendErrorWithFormat("%s\n", error.AsCString());
304 result.AppendError("no platform is currently selected\n");
306 return result.Succeeded();
309 Options *GetOptions() override {
310 PlatformSP platform_sp(
311 GetDebugger().GetPlatformList().GetSelectedPlatform());
312 OptionGroupOptions *m_platform_options = nullptr;
314 m_platform_options = platform_sp->GetConnectionOptions(m_interpreter);
315 if (m_platform_options != nullptr && !m_platform_options->m_did_finalize)
316 m_platform_options->Finalize();
318 return m_platform_options;
322 // "platform disconnect"
323 class CommandObjectPlatformDisconnect : public CommandObjectParsed {
325 CommandObjectPlatformDisconnect(CommandInterpreter &interpreter)
326 : CommandObjectParsed(interpreter, "platform disconnect",
327 "Disconnect from the current platform.",
328 "platform disconnect", 0) {}
330 ~CommandObjectPlatformDisconnect() override = default;
333 bool DoExecute(Args &args, CommandReturnObject &result) override {
334 PlatformSP platform_sp(
335 GetDebugger().GetPlatformList().GetSelectedPlatform());
337 if (args.GetArgumentCount() == 0) {
340 if (platform_sp->IsConnected()) {
341 // Cache the instance name if there is one since we are about to
342 // disconnect and the name might go with it.
343 const char *hostname_cstr = platform_sp->GetHostname();
344 std::string hostname;
346 hostname.assign(hostname_cstr);
348 error = platform_sp->DisconnectRemote();
349 if (error.Success()) {
350 Stream &ostrm = result.GetOutputStream();
351 if (hostname.empty())
352 ostrm.Format("Disconnected from \"{0}\"\n",
353 platform_sp->GetPluginName());
355 ostrm.Printf("Disconnected from \"%s\"\n", hostname.c_str());
356 result.SetStatus(eReturnStatusSuccessFinishResult);
358 result.AppendErrorWithFormat("%s", error.AsCString());
362 result.AppendErrorWithFormatv("not connected to '{0}'",
363 platform_sp->GetPluginName());
368 "\"platform disconnect\" doesn't take any arguments");
371 result.AppendError("no platform is currently selected");
373 return result.Succeeded();
377 // "platform settings"
378 class CommandObjectPlatformSettings : public CommandObjectParsed {
380 CommandObjectPlatformSettings(CommandInterpreter &interpreter)
381 : CommandObjectParsed(interpreter, "platform settings",
382 "Set settings for the current target's platform, "
383 "or for a platform by name.",
384 "platform settings", 0),
385 m_option_working_dir(LLDB_OPT_SET_1, false, "working-dir", 'w',
386 CommandCompletions::eRemoteDiskDirectoryCompletion,
388 "The working directory for the platform.") {
389 m_options.Append(&m_option_working_dir, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
392 ~CommandObjectPlatformSettings() override = default;
395 bool DoExecute(Args &args, CommandReturnObject &result) override {
396 PlatformSP platform_sp(
397 GetDebugger().GetPlatformList().GetSelectedPlatform());
399 if (m_option_working_dir.GetOptionValue().OptionWasSet())
400 platform_sp->SetWorkingDirectory(
401 m_option_working_dir.GetOptionValue().GetCurrentValue());
403 result.AppendError("no platform is currently selected");
405 return result.Succeeded();
408 Options *GetOptions() override {
409 if (!m_options.DidFinalize())
410 m_options.Finalize();
414 OptionGroupOptions m_options;
415 OptionGroupFile m_option_working_dir;
419 class CommandObjectPlatformMkDir : public CommandObjectParsed {
421 CommandObjectPlatformMkDir(CommandInterpreter &interpreter)
422 : CommandObjectParsed(interpreter, "platform mkdir",
423 "Make a new directory on the remote end.", nullptr,
425 CommandArgumentData thread_arg{eArgTypePath, eArgRepeatPlain};
426 m_arguments.push_back({thread_arg});
429 ~CommandObjectPlatformMkDir() override = default;
431 bool DoExecute(Args &args, CommandReturnObject &result) override {
432 PlatformSP platform_sp(
433 GetDebugger().GetPlatformList().GetSelectedPlatform());
435 std::string cmd_line;
436 args.GetCommandString(cmd_line);
438 const OptionPermissions *options_permissions =
439 (const OptionPermissions *)m_options.GetGroupWithOption('r');
440 if (options_permissions)
441 mode = options_permissions->m_permissions;
443 mode = lldb::eFilePermissionsUserRWX | lldb::eFilePermissionsGroupRWX |
444 lldb::eFilePermissionsWorldRX;
445 Status error = platform_sp->MakeDirectory(FileSpec(cmd_line), mode);
446 if (error.Success()) {
447 result.SetStatus(eReturnStatusSuccessFinishResult);
449 result.AppendError(error.AsCString());
452 result.AppendError("no platform currently selected\n");
454 return result.Succeeded();
457 Options *GetOptions() override {
458 if (!m_options.DidFinalize()) {
459 m_options.Append(new OptionPermissions());
460 m_options.Finalize();
465 OptionGroupOptions m_options;
469 class CommandObjectPlatformFOpen : public CommandObjectParsed {
471 CommandObjectPlatformFOpen(CommandInterpreter &interpreter)
472 : CommandObjectParsed(interpreter, "platform file open",
473 "Open a file on the remote end.", nullptr, 0) {
474 CommandArgumentData path_arg{eArgTypePath, eArgRepeatPlain};
475 m_arguments.push_back({path_arg});
478 ~CommandObjectPlatformFOpen() override = default;
481 HandleArgumentCompletion(CompletionRequest &request,
482 OptionElementVector &opt_element_vector) override {
483 if (request.GetCursorIndex() == 0)
484 CommandCompletions::InvokeCommonCompletionCallbacks(
485 GetCommandInterpreter(),
486 CommandCompletions::eRemoteDiskFileCompletion, request, nullptr);
489 bool DoExecute(Args &args, CommandReturnObject &result) override {
490 PlatformSP platform_sp(
491 GetDebugger().GetPlatformList().GetSelectedPlatform());
494 std::string cmd_line;
495 args.GetCommandString(cmd_line);
497 const OptionPermissions *options_permissions =
498 (const OptionPermissions *)m_options.GetGroupWithOption('r');
499 if (options_permissions)
500 perms = options_permissions->m_permissions;
502 perms = lldb::eFilePermissionsUserRW | lldb::eFilePermissionsGroupRW |
503 lldb::eFilePermissionsWorldRead;
504 lldb::user_id_t fd = platform_sp->OpenFile(
506 File::eOpenOptionReadWrite | File::eOpenOptionCanCreate,
508 if (error.Success()) {
509 result.AppendMessageWithFormat("File Descriptor = %" PRIu64 "\n", fd);
510 result.SetStatus(eReturnStatusSuccessFinishResult);
512 result.AppendError(error.AsCString());
515 result.AppendError("no platform currently selected\n");
517 return result.Succeeded();
520 Options *GetOptions() override {
521 if (!m_options.DidFinalize()) {
522 m_options.Append(new OptionPermissions());
523 m_options.Finalize();
528 OptionGroupOptions m_options;
532 class CommandObjectPlatformFClose : public CommandObjectParsed {
534 CommandObjectPlatformFClose(CommandInterpreter &interpreter)
535 : CommandObjectParsed(interpreter, "platform file close",
536 "Close a file on the remote end.", nullptr, 0) {
537 CommandArgumentData path_arg{eArgTypeUnsignedInteger, eArgRepeatPlain};
538 m_arguments.push_back({path_arg});
541 ~CommandObjectPlatformFClose() override = default;
543 bool DoExecute(Args &args, CommandReturnObject &result) override {
544 PlatformSP platform_sp(
545 GetDebugger().GetPlatformList().GetSelectedPlatform());
547 std::string cmd_line;
548 args.GetCommandString(cmd_line);
550 if (!llvm::to_integer(cmd_line, fd)) {
551 result.AppendErrorWithFormatv("'{0}' is not a valid file descriptor.\n",
553 return result.Succeeded();
556 bool success = platform_sp->CloseFile(fd, error);
558 result.AppendMessageWithFormat("file %" PRIu64 " closed.\n", fd);
559 result.SetStatus(eReturnStatusSuccessFinishResult);
561 result.AppendError(error.AsCString());
564 result.AppendError("no platform currently selected\n");
566 return result.Succeeded();
572 #define LLDB_OPTIONS_platform_fread
573 #include "CommandOptions.inc"
575 class CommandObjectPlatformFRead : public CommandObjectParsed {
577 CommandObjectPlatformFRead(CommandInterpreter &interpreter)
578 : CommandObjectParsed(interpreter, "platform file read",
579 "Read data from a file on the remote end.", nullptr,
581 CommandArgumentData path_arg{eArgTypeUnsignedInteger, eArgRepeatPlain};
582 m_arguments.push_back({path_arg});
585 ~CommandObjectPlatformFRead() override = default;
587 bool DoExecute(Args &args, CommandReturnObject &result) override {
588 PlatformSP platform_sp(
589 GetDebugger().GetPlatformList().GetSelectedPlatform());
591 std::string cmd_line;
592 args.GetCommandString(cmd_line);
594 if (!llvm::to_integer(cmd_line, fd)) {
595 result.AppendErrorWithFormatv("'{0}' is not a valid file descriptor.\n",
597 return result.Succeeded();
599 std::string buffer(m_options.m_count, 0);
601 uint64_t retcode = platform_sp->ReadFile(
602 fd, m_options.m_offset, &buffer[0], m_options.m_count, error);
603 if (retcode != UINT64_MAX) {
604 result.AppendMessageWithFormat("Return = %" PRIu64 "\n", retcode);
605 result.AppendMessageWithFormat("Data = \"%s\"\n", buffer.c_str());
606 result.SetStatus(eReturnStatusSuccessFinishResult);
608 result.AppendError(error.AsCString());
611 result.AppendError("no platform currently selected\n");
613 return result.Succeeded();
616 Options *GetOptions() override { return &m_options; }
619 class CommandOptions : public Options {
621 CommandOptions() = default;
623 ~CommandOptions() override = default;
625 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
626 ExecutionContext *execution_context) override {
628 char short_option = (char)m_getopt_table[option_idx].val;
630 switch (short_option) {
632 if (option_arg.getAsInteger(0, m_offset))
633 error.SetErrorStringWithFormat("invalid offset: '%s'",
634 option_arg.str().c_str());
637 if (option_arg.getAsInteger(0, m_count))
638 error.SetErrorStringWithFormat("invalid offset: '%s'",
639 option_arg.str().c_str());
642 llvm_unreachable("Unimplemented option");
648 void OptionParsingStarting(ExecutionContext *execution_context) override {
653 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
654 return llvm::makeArrayRef(g_platform_fread_options);
657 // Instance variables to hold the values for command options.
663 CommandOptions m_options;
668 #define LLDB_OPTIONS_platform_fwrite
669 #include "CommandOptions.inc"
671 class CommandObjectPlatformFWrite : public CommandObjectParsed {
673 CommandObjectPlatformFWrite(CommandInterpreter &interpreter)
674 : CommandObjectParsed(interpreter, "platform file write",
675 "Write data to a file on the remote end.", nullptr,
677 CommandArgumentData path_arg{eArgTypeUnsignedInteger, eArgRepeatPlain};
678 m_arguments.push_back({path_arg});
681 ~CommandObjectPlatformFWrite() override = default;
683 bool DoExecute(Args &args, CommandReturnObject &result) override {
684 PlatformSP platform_sp(
685 GetDebugger().GetPlatformList().GetSelectedPlatform());
687 std::string cmd_line;
688 args.GetCommandString(cmd_line);
691 if (!llvm::to_integer(cmd_line, fd)) {
692 result.AppendErrorWithFormatv("'{0}' is not a valid file descriptor.",
694 return result.Succeeded();
697 platform_sp->WriteFile(fd, m_options.m_offset, &m_options.m_data[0],
698 m_options.m_data.size(), error);
699 if (retcode != UINT64_MAX) {
700 result.AppendMessageWithFormat("Return = %" PRIu64 "\n", retcode);
701 result.SetStatus(eReturnStatusSuccessFinishResult);
703 result.AppendError(error.AsCString());
706 result.AppendError("no platform currently selected\n");
708 return result.Succeeded();
711 Options *GetOptions() override { return &m_options; }
714 class CommandOptions : public Options {
716 CommandOptions() = default;
718 ~CommandOptions() override = default;
720 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
721 ExecutionContext *execution_context) override {
723 char short_option = (char)m_getopt_table[option_idx].val;
725 switch (short_option) {
727 if (option_arg.getAsInteger(0, m_offset))
728 error.SetErrorStringWithFormat("invalid offset: '%s'",
729 option_arg.str().c_str());
732 m_data.assign(std::string(option_arg));
735 llvm_unreachable("Unimplemented option");
741 void OptionParsingStarting(ExecutionContext *execution_context) override {
746 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
747 return llvm::makeArrayRef(g_platform_fwrite_options);
750 // Instance variables to hold the values for command options.
756 CommandOptions m_options;
759 class CommandObjectPlatformFile : public CommandObjectMultiword {
761 // Constructors and Destructors
762 CommandObjectPlatformFile(CommandInterpreter &interpreter)
763 : CommandObjectMultiword(
764 interpreter, "platform file",
765 "Commands to access files on the current platform.",
766 "platform file [open|close|read|write] ...") {
768 "open", CommandObjectSP(new CommandObjectPlatformFOpen(interpreter)));
770 "close", CommandObjectSP(new CommandObjectPlatformFClose(interpreter)));
772 "read", CommandObjectSP(new CommandObjectPlatformFRead(interpreter)));
774 "write", CommandObjectSP(new CommandObjectPlatformFWrite(interpreter)));
777 ~CommandObjectPlatformFile() override = default;
780 // For CommandObjectPlatform only
781 CommandObjectPlatformFile(const CommandObjectPlatformFile &) = delete;
782 const CommandObjectPlatformFile &
783 operator=(const CommandObjectPlatformFile &) = delete;
786 // "platform get-file remote-file-path host-file-path"
787 class CommandObjectPlatformGetFile : public CommandObjectParsed {
789 CommandObjectPlatformGetFile(CommandInterpreter &interpreter)
790 : CommandObjectParsed(
791 interpreter, "platform get-file",
792 "Transfer a file from the remote end to the local host.",
793 "platform get-file <remote-file-spec> <local-file-spec>", 0) {
797 (lldb) platform get-file /the/remote/file/path /the/local/file/path
799 Transfer a file from the remote end with file path /the/remote/file/path to the local host.)");
801 CommandArgumentEntry arg1, arg2;
802 CommandArgumentData file_arg_remote, file_arg_host;
804 // Define the first (and only) variant of this arg.
805 file_arg_remote.arg_type = eArgTypeFilename;
806 file_arg_remote.arg_repetition = eArgRepeatPlain;
807 // There is only one variant this argument could be; put it into the
809 arg1.push_back(file_arg_remote);
811 // Define the second (and only) variant of this arg.
812 file_arg_host.arg_type = eArgTypeFilename;
813 file_arg_host.arg_repetition = eArgRepeatPlain;
814 // There is only one variant this argument could be; put it into the
816 arg2.push_back(file_arg_host);
818 // Push the data for the first and the second arguments into the
819 // m_arguments vector.
820 m_arguments.push_back(arg1);
821 m_arguments.push_back(arg2);
824 ~CommandObjectPlatformGetFile() override = default;
827 HandleArgumentCompletion(CompletionRequest &request,
828 OptionElementVector &opt_element_vector) override {
829 if (request.GetCursorIndex() == 0)
830 CommandCompletions::InvokeCommonCompletionCallbacks(
831 GetCommandInterpreter(),
832 CommandCompletions::eRemoteDiskFileCompletion, request, nullptr);
833 else if (request.GetCursorIndex() == 1)
834 CommandCompletions::InvokeCommonCompletionCallbacks(
835 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
839 bool DoExecute(Args &args, CommandReturnObject &result) override {
840 // If the number of arguments is incorrect, issue an error message.
841 if (args.GetArgumentCount() != 2) {
842 result.AppendError("required arguments missing; specify both the "
843 "source and destination file paths");
847 PlatformSP platform_sp(
848 GetDebugger().GetPlatformList().GetSelectedPlatform());
850 const char *remote_file_path = args.GetArgumentAtIndex(0);
851 const char *local_file_path = args.GetArgumentAtIndex(1);
852 Status error = platform_sp->GetFile(FileSpec(remote_file_path),
853 FileSpec(local_file_path));
854 if (error.Success()) {
855 result.AppendMessageWithFormat(
856 "successfully get-file from %s (remote) to %s (host)\n",
857 remote_file_path, local_file_path);
858 result.SetStatus(eReturnStatusSuccessFinishResult);
860 result.AppendMessageWithFormat("get-file failed: %s\n",
864 result.AppendError("no platform currently selected\n");
866 return result.Succeeded();
870 // "platform get-size remote-file-path"
871 class CommandObjectPlatformGetSize : public CommandObjectParsed {
873 CommandObjectPlatformGetSize(CommandInterpreter &interpreter)
874 : CommandObjectParsed(interpreter, "platform get-size",
875 "Get the file size from the remote end.",
876 "platform get-size <remote-file-spec>", 0) {
880 (lldb) platform get-size /the/remote/file/path
882 Get the file size from the remote end with path /the/remote/file/path.)");
884 CommandArgumentEntry arg1;
885 CommandArgumentData file_arg_remote;
887 // Define the first (and only) variant of this arg.
888 file_arg_remote.arg_type = eArgTypeFilename;
889 file_arg_remote.arg_repetition = eArgRepeatPlain;
890 // There is only one variant this argument could be; put it into the
892 arg1.push_back(file_arg_remote);
894 // Push the data for the first argument into the m_arguments vector.
895 m_arguments.push_back(arg1);
898 ~CommandObjectPlatformGetSize() override = default;
901 HandleArgumentCompletion(CompletionRequest &request,
902 OptionElementVector &opt_element_vector) override {
903 if (request.GetCursorIndex() != 0)
906 CommandCompletions::InvokeCommonCompletionCallbacks(
907 GetCommandInterpreter(), CommandCompletions::eRemoteDiskFileCompletion,
911 bool DoExecute(Args &args, CommandReturnObject &result) override {
912 // If the number of arguments is incorrect, issue an error message.
913 if (args.GetArgumentCount() != 1) {
914 result.AppendError("required argument missing; specify the source file "
915 "path as the only argument");
919 PlatformSP platform_sp(
920 GetDebugger().GetPlatformList().GetSelectedPlatform());
922 std::string remote_file_path(args.GetArgumentAtIndex(0));
923 user_id_t size = platform_sp->GetFileSize(FileSpec(remote_file_path));
924 if (size != UINT64_MAX) {
925 result.AppendMessageWithFormat("File size of %s (remote): %" PRIu64
927 remote_file_path.c_str(), size);
928 result.SetStatus(eReturnStatusSuccessFinishResult);
930 result.AppendMessageWithFormat(
931 "Error getting file size of %s (remote)\n",
932 remote_file_path.c_str());
935 result.AppendError("no platform currently selected\n");
937 return result.Succeeded();
941 // "platform get-permissions remote-file-path"
942 class CommandObjectPlatformGetPermissions : public CommandObjectParsed {
944 CommandObjectPlatformGetPermissions(CommandInterpreter &interpreter)
945 : CommandObjectParsed(interpreter, "platform get-permissions",
946 "Get the file permission bits from the remote end.",
947 "platform get-permissions <remote-file-spec>", 0) {
951 (lldb) platform get-permissions /the/remote/file/path
953 Get the file permissions from the remote end with path /the/remote/file/path.)");
955 CommandArgumentEntry arg1;
956 CommandArgumentData file_arg_remote;
958 // Define the first (and only) variant of this arg.
959 file_arg_remote.arg_type = eArgTypeFilename;
960 file_arg_remote.arg_repetition = eArgRepeatPlain;
961 // There is only one variant this argument could be; put it into the
963 arg1.push_back(file_arg_remote);
965 // Push the data for the first argument into the m_arguments vector.
966 m_arguments.push_back(arg1);
969 ~CommandObjectPlatformGetPermissions() override = default;
972 HandleArgumentCompletion(CompletionRequest &request,
973 OptionElementVector &opt_element_vector) override {
974 if (request.GetCursorIndex() != 0)
977 CommandCompletions::InvokeCommonCompletionCallbacks(
978 GetCommandInterpreter(), CommandCompletions::eRemoteDiskFileCompletion,
982 bool DoExecute(Args &args, CommandReturnObject &result) override {
983 // If the number of arguments is incorrect, issue an error message.
984 if (args.GetArgumentCount() != 1) {
985 result.AppendError("required argument missing; specify the source file "
986 "path as the only argument");
990 PlatformSP platform_sp(
991 GetDebugger().GetPlatformList().GetSelectedPlatform());
993 std::string remote_file_path(args.GetArgumentAtIndex(0));
994 uint32_t permissions;
995 Status error = platform_sp->GetFilePermissions(FileSpec(remote_file_path),
997 if (error.Success()) {
998 result.AppendMessageWithFormat(
999 "File permissions of %s (remote): 0o%04" PRIo32 "\n",
1000 remote_file_path.c_str(), permissions);
1001 result.SetStatus(eReturnStatusSuccessFinishResult);
1003 result.AppendError(error.AsCString());
1005 result.AppendError("no platform currently selected\n");
1007 return result.Succeeded();
1011 // "platform file-exists remote-file-path"
1012 class CommandObjectPlatformFileExists : public CommandObjectParsed {
1014 CommandObjectPlatformFileExists(CommandInterpreter &interpreter)
1015 : CommandObjectParsed(interpreter, "platform file-exists",
1016 "Check if the file exists on the remote end.",
1017 "platform file-exists <remote-file-spec>", 0) {
1021 (lldb) platform file-exists /the/remote/file/path
1023 Check if /the/remote/file/path exists on the remote end.)");
1025 CommandArgumentEntry arg1;
1026 CommandArgumentData file_arg_remote;
1028 // Define the first (and only) variant of this arg.
1029 file_arg_remote.arg_type = eArgTypeFilename;
1030 file_arg_remote.arg_repetition = eArgRepeatPlain;
1031 // There is only one variant this argument could be; put it into the
1033 arg1.push_back(file_arg_remote);
1035 // Push the data for the first argument into the m_arguments vector.
1036 m_arguments.push_back(arg1);
1039 ~CommandObjectPlatformFileExists() override = default;
1042 HandleArgumentCompletion(CompletionRequest &request,
1043 OptionElementVector &opt_element_vector) override {
1044 if (request.GetCursorIndex() != 0)
1047 CommandCompletions::InvokeCommonCompletionCallbacks(
1048 GetCommandInterpreter(), CommandCompletions::eRemoteDiskFileCompletion,
1052 bool DoExecute(Args &args, CommandReturnObject &result) override {
1053 // If the number of arguments is incorrect, issue an error message.
1054 if (args.GetArgumentCount() != 1) {
1055 result.AppendError("required argument missing; specify the source file "
1056 "path as the only argument");
1060 PlatformSP platform_sp(
1061 GetDebugger().GetPlatformList().GetSelectedPlatform());
1063 std::string remote_file_path(args.GetArgumentAtIndex(0));
1064 bool exists = platform_sp->GetFileExists(FileSpec(remote_file_path));
1065 result.AppendMessageWithFormat(
1066 "File %s (remote) %s\n",
1067 remote_file_path.c_str(), exists ? "exists" : "does not exist");
1068 result.SetStatus(eReturnStatusSuccessFinishResult);
1070 result.AppendError("no platform currently selected\n");
1072 return result.Succeeded();
1076 // "platform put-file"
1077 class CommandObjectPlatformPutFile : public CommandObjectParsed {
1079 CommandObjectPlatformPutFile(CommandInterpreter &interpreter)
1080 : CommandObjectParsed(
1081 interpreter, "platform put-file",
1082 "Transfer a file from this system to the remote end.",
1083 "platform put-file <source> [<destination>]", 0) {
1087 (lldb) platform put-file /source/foo.txt /destination/bar.txt
1089 (lldb) platform put-file /source/foo.txt
1091 Relative source file paths are resolved against lldb's local working directory.
1093 Omitting the destination places the file in the platform working directory.)");
1094 CommandArgumentData source_arg{eArgTypePath, eArgRepeatPlain};
1095 CommandArgumentData path_arg{eArgTypePath, eArgRepeatOptional};
1096 m_arguments.push_back({source_arg});
1097 m_arguments.push_back({path_arg});
1100 ~CommandObjectPlatformPutFile() override = default;
1103 HandleArgumentCompletion(CompletionRequest &request,
1104 OptionElementVector &opt_element_vector) override {
1105 if (request.GetCursorIndex() == 0)
1106 CommandCompletions::InvokeCommonCompletionCallbacks(
1107 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
1109 else if (request.GetCursorIndex() == 1)
1110 CommandCompletions::InvokeCommonCompletionCallbacks(
1111 GetCommandInterpreter(),
1112 CommandCompletions::eRemoteDiskFileCompletion, request, nullptr);
1115 bool DoExecute(Args &args, CommandReturnObject &result) override {
1116 const char *src = args.GetArgumentAtIndex(0);
1117 const char *dst = args.GetArgumentAtIndex(1);
1119 FileSpec src_fs(src);
1120 FileSystem::Instance().Resolve(src_fs);
1121 FileSpec dst_fs(dst ? dst : src_fs.GetFilename().GetCString());
1123 PlatformSP platform_sp(
1124 GetDebugger().GetPlatformList().GetSelectedPlatform());
1126 Status error(platform_sp->PutFile(src_fs, dst_fs));
1127 if (error.Success()) {
1128 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1130 result.AppendError(error.AsCString());
1133 result.AppendError("no platform currently selected\n");
1135 return result.Succeeded();
1139 // "platform process launch"
1140 class CommandObjectPlatformProcessLaunch : public CommandObjectParsed {
1142 CommandObjectPlatformProcessLaunch(CommandInterpreter &interpreter)
1143 : CommandObjectParsed(interpreter, "platform process launch",
1144 "Launch a new process on a remote platform.",
1145 "platform process launch program",
1146 eCommandRequiresTarget | eCommandTryTargetAPILock) {
1147 m_all_options.Append(&m_options);
1148 m_all_options.Finalize();
1149 CommandArgumentData run_arg_arg{eArgTypeRunArgs, eArgRepeatStar};
1150 m_arguments.push_back({run_arg_arg});
1153 ~CommandObjectPlatformProcessLaunch() override = default;
1155 Options *GetOptions() override { return &m_all_options; }
1158 bool DoExecute(Args &args, CommandReturnObject &result) override {
1159 Target *target = GetDebugger().GetSelectedTarget().get();
1160 PlatformSP platform_sp;
1162 platform_sp = target->GetPlatform();
1165 platform_sp = GetDebugger().GetPlatformList().GetSelectedPlatform();
1170 const size_t argc = args.GetArgumentCount();
1171 Target *target = m_exe_ctx.GetTargetPtr();
1172 Module *exe_module = target->GetExecutableModulePointer();
1174 m_options.launch_info.GetExecutableFile() = exe_module->GetFileSpec();
1175 llvm::SmallString<128> exe_path;
1176 m_options.launch_info.GetExecutableFile().GetPath(exe_path);
1177 if (!exe_path.empty())
1178 m_options.launch_info.GetArguments().AppendArgument(exe_path);
1179 m_options.launch_info.GetArchitecture() = exe_module->GetArchitecture();
1183 if (m_options.launch_info.GetExecutableFile()) {
1184 // We already have an executable file, so we will use this and all
1185 // arguments to this function are extra arguments
1186 m_options.launch_info.GetArguments().AppendArguments(args);
1188 // We don't have any file yet, so the first argument is our
1189 // executable, and the rest are program arguments
1190 const bool first_arg_is_executable = true;
1191 m_options.launch_info.SetArguments(args, first_arg_is_executable);
1195 if (m_options.launch_info.GetExecutableFile()) {
1196 Debugger &debugger = GetDebugger();
1199 target->GetRunArguments(m_options.launch_info.GetArguments());
1201 ProcessSP process_sp(platform_sp->DebugProcess(
1202 m_options.launch_info, debugger, *target, error));
1203 if (process_sp && process_sp->IsAlive()) {
1204 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1208 if (error.Success())
1209 result.AppendError("process launch failed");
1211 result.AppendError(error.AsCString());
1213 result.AppendError("'platform process launch' uses the current target "
1214 "file and arguments, or the executable and its "
1215 "arguments can be specified in this command");
1219 result.AppendError("no platform is selected\n");
1221 return result.Succeeded();
1224 CommandOptionsProcessLaunch m_options;
1225 OptionGroupOptions m_all_options;
1228 // "platform process list"
1230 static PosixPlatformCommandOptionValidator posix_validator;
1231 #define LLDB_OPTIONS_platform_process_list
1232 #include "CommandOptions.inc"
1234 class CommandObjectPlatformProcessList : public CommandObjectParsed {
1236 CommandObjectPlatformProcessList(CommandInterpreter &interpreter)
1237 : CommandObjectParsed(interpreter, "platform process list",
1238 "List processes on a remote platform by name, pid, "
1239 "or many other matching attributes.",
1240 "platform process list", 0) {}
1242 ~CommandObjectPlatformProcessList() override = default;
1244 Options *GetOptions() override { return &m_options; }
1247 bool DoExecute(Args &args, CommandReturnObject &result) override {
1248 Target *target = GetDebugger().GetSelectedTarget().get();
1249 PlatformSP platform_sp;
1251 platform_sp = target->GetPlatform();
1254 platform_sp = GetDebugger().GetPlatformList().GetSelectedPlatform();
1260 Stream &ostrm = result.GetOutputStream();
1262 lldb::pid_t pid = m_options.match_info.GetProcessInfo().GetProcessID();
1263 if (pid != LLDB_INVALID_PROCESS_ID) {
1264 ProcessInstanceInfo proc_info;
1265 if (platform_sp->GetProcessInfo(pid, proc_info)) {
1266 ProcessInstanceInfo::DumpTableHeader(ostrm, m_options.show_args,
1268 proc_info.DumpAsTableRow(ostrm, platform_sp->GetUserIDResolver(),
1269 m_options.show_args, m_options.verbose);
1270 result.SetStatus(eReturnStatusSuccessFinishResult);
1272 result.AppendErrorWithFormat(
1273 "no process found with pid = %" PRIu64 "\n", pid);
1276 ProcessInstanceInfoList proc_infos;
1277 const uint32_t matches =
1278 platform_sp->FindProcesses(m_options.match_info, proc_infos);
1279 const char *match_desc = nullptr;
1280 const char *match_name =
1281 m_options.match_info.GetProcessInfo().GetName();
1282 if (match_name && match_name[0]) {
1283 switch (m_options.match_info.GetNameMatchType()) {
1284 case NameMatch::Ignore:
1286 case NameMatch::Equals:
1287 match_desc = "matched";
1289 case NameMatch::Contains:
1290 match_desc = "contained";
1292 case NameMatch::StartsWith:
1293 match_desc = "started with";
1295 case NameMatch::EndsWith:
1296 match_desc = "ended with";
1298 case NameMatch::RegularExpression:
1299 match_desc = "matched the regular expression";
1306 result.AppendErrorWithFormatv(
1307 "no processes were found that {0} \"{1}\" on the \"{2}\" "
1309 match_desc, match_name, platform_sp->GetName());
1311 result.AppendErrorWithFormatv(
1312 "no processes were found on the \"{0}\" platform\n",
1313 platform_sp->GetName());
1315 result.AppendMessageWithFormatv(
1316 "{0} matching process{1} found on \"{2}\"", matches,
1317 matches > 1 ? "es were" : " was", platform_sp->GetName());
1319 result.AppendMessageWithFormat(" whose name %s \"%s\"",
1320 match_desc, match_name);
1321 result.AppendMessageWithFormat("\n");
1322 ProcessInstanceInfo::DumpTableHeader(ostrm, m_options.show_args,
1324 for (uint32_t i = 0; i < matches; ++i) {
1325 proc_infos[i].DumpAsTableRow(
1326 ostrm, platform_sp->GetUserIDResolver(), m_options.show_args,
1333 result.AppendError("no platform is selected\n");
1335 return result.Succeeded();
1338 class CommandOptions : public Options {
1340 CommandOptions() = default;
1342 ~CommandOptions() override = default;
1344 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1345 ExecutionContext *execution_context) override {
1347 const int short_option = m_getopt_table[option_idx].val;
1348 bool success = false;
1350 uint32_t id = LLDB_INVALID_PROCESS_ID;
1351 success = !option_arg.getAsInteger(0, id);
1352 switch (short_option) {
1354 match_info.GetProcessInfo().SetProcessID(id);
1356 error.SetErrorStringWithFormat("invalid process ID string: '%s'",
1357 option_arg.str().c_str());
1361 match_info.GetProcessInfo().SetParentProcessID(id);
1363 error.SetErrorStringWithFormat(
1364 "invalid parent process ID string: '%s'",
1365 option_arg.str().c_str());
1369 match_info.GetProcessInfo().SetUserID(success ? id : UINT32_MAX);
1371 error.SetErrorStringWithFormat("invalid user ID string: '%s'",
1372 option_arg.str().c_str());
1376 match_info.GetProcessInfo().SetEffectiveUserID(success ? id
1379 error.SetErrorStringWithFormat(
1380 "invalid effective user ID string: '%s'",
1381 option_arg.str().c_str());
1385 match_info.GetProcessInfo().SetGroupID(success ? id : UINT32_MAX);
1387 error.SetErrorStringWithFormat("invalid group ID string: '%s'",
1388 option_arg.str().c_str());
1392 match_info.GetProcessInfo().SetEffectiveGroupID(success ? id
1395 error.SetErrorStringWithFormat(
1396 "invalid effective group ID string: '%s'",
1397 option_arg.str().c_str());
1401 TargetSP target_sp =
1402 execution_context ? execution_context->GetTargetSP() : TargetSP();
1403 DebuggerSP debugger_sp =
1404 target_sp ? target_sp->GetDebugger().shared_from_this()
1406 PlatformSP platform_sp =
1407 debugger_sp ? debugger_sp->GetPlatformList().GetSelectedPlatform()
1409 match_info.GetProcessInfo().GetArchitecture() =
1410 Platform::GetAugmentedArchSpec(platform_sp.get(), option_arg);
1414 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1415 option_arg, FileSpec::Style::native);
1416 match_info.SetNameMatchType(NameMatch::Equals);
1420 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1421 option_arg, FileSpec::Style::native);
1422 match_info.SetNameMatchType(NameMatch::EndsWith);
1426 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1427 option_arg, FileSpec::Style::native);
1428 match_info.SetNameMatchType(NameMatch::StartsWith);
1432 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1433 option_arg, FileSpec::Style::native);
1434 match_info.SetNameMatchType(NameMatch::Contains);
1438 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1439 option_arg, FileSpec::Style::native);
1440 match_info.SetNameMatchType(NameMatch::RegularExpression);
1452 match_info.SetMatchAllUsers(true);
1456 llvm_unreachable("Unimplemented option");
1462 void OptionParsingStarting(ExecutionContext *execution_context) override {
1468 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1469 return llvm::makeArrayRef(g_platform_process_list_options);
1472 // Instance variables to hold the values for command options.
1474 ProcessInstanceInfoMatch match_info;
1475 bool show_args = false;
1476 bool verbose = false;
1479 CommandOptions m_options;
1482 // "platform process info"
1483 class CommandObjectPlatformProcessInfo : public CommandObjectParsed {
1485 CommandObjectPlatformProcessInfo(CommandInterpreter &interpreter)
1486 : CommandObjectParsed(
1487 interpreter, "platform process info",
1488 "Get detailed information for one or more process by process ID.",
1489 "platform process info <pid> [<pid> <pid> ...]", 0) {
1490 CommandArgumentEntry arg;
1491 CommandArgumentData pid_args;
1493 // Define the first (and only) variant of this arg.
1494 pid_args.arg_type = eArgTypePid;
1495 pid_args.arg_repetition = eArgRepeatStar;
1497 // There is only one variant this argument could be; put it into the
1499 arg.push_back(pid_args);
1501 // Push the data for the first argument into the m_arguments vector.
1502 m_arguments.push_back(arg);
1505 ~CommandObjectPlatformProcessInfo() override = default;
1508 HandleArgumentCompletion(CompletionRequest &request,
1509 OptionElementVector &opt_element_vector) override {
1510 CommandCompletions::InvokeCommonCompletionCallbacks(
1511 GetCommandInterpreter(), CommandCompletions::eProcessIDCompletion,
1516 bool DoExecute(Args &args, CommandReturnObject &result) override {
1517 Target *target = GetDebugger().GetSelectedTarget().get();
1518 PlatformSP platform_sp;
1520 platform_sp = target->GetPlatform();
1523 platform_sp = GetDebugger().GetPlatformList().GetSelectedPlatform();
1527 const size_t argc = args.GetArgumentCount();
1531 if (platform_sp->IsConnected()) {
1532 Stream &ostrm = result.GetOutputStream();
1533 for (auto &entry : args.entries()) {
1535 if (entry.ref().getAsInteger(0, pid)) {
1536 result.AppendErrorWithFormat("invalid process ID argument '%s'",
1537 entry.ref().str().c_str());
1540 ProcessInstanceInfo proc_info;
1541 if (platform_sp->GetProcessInfo(pid, proc_info)) {
1542 ostrm.Printf("Process information for process %" PRIu64 ":\n",
1544 proc_info.Dump(ostrm, platform_sp->GetUserIDResolver());
1546 ostrm.Printf("error: no process information is available for "
1547 "process %" PRIu64 "\n",
1555 result.AppendErrorWithFormatv("not connected to '{0}'",
1556 platform_sp->GetPluginName());
1560 result.AppendError("one or more process id(s) must be specified");
1563 result.AppendError("no platform is currently selected");
1565 return result.Succeeded();
1569 #define LLDB_OPTIONS_platform_process_attach
1570 #include "CommandOptions.inc"
1572 class CommandObjectPlatformProcessAttach : public CommandObjectParsed {
1574 class CommandOptions : public Options {
1577 // Keep default values of all options in one place: OptionParsingStarting
1579 OptionParsingStarting(nullptr);
1582 ~CommandOptions() override = default;
1584 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1585 ExecutionContext *execution_context) override {
1587 char short_option = (char)m_getopt_table[option_idx].val;
1588 switch (short_option) {
1590 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
1591 if (option_arg.getAsInteger(0, pid)) {
1592 error.SetErrorStringWithFormat("invalid process ID '%s'",
1593 option_arg.str().c_str());
1595 attach_info.SetProcessID(pid);
1600 attach_info.SetProcessPluginName(option_arg);
1604 attach_info.GetExecutableFile().SetFile(option_arg,
1605 FileSpec::Style::native);
1609 attach_info.SetWaitForLaunch(true);
1613 llvm_unreachable("Unimplemented option");
1618 void OptionParsingStarting(ExecutionContext *execution_context) override {
1619 attach_info.Clear();
1622 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1623 return llvm::makeArrayRef(g_platform_process_attach_options);
1626 // Options table: Required for subclasses of Options.
1628 static OptionDefinition g_option_table[];
1630 // Instance variables to hold the values for command options.
1632 ProcessAttachInfo attach_info;
1635 CommandObjectPlatformProcessAttach(CommandInterpreter &interpreter)
1636 : CommandObjectParsed(interpreter, "platform process attach",
1637 "Attach to a process.",
1638 "platform process attach <cmd-options>") {}
1640 ~CommandObjectPlatformProcessAttach() override = default;
1642 bool DoExecute(Args &command, CommandReturnObject &result) override {
1643 PlatformSP platform_sp(
1644 GetDebugger().GetPlatformList().GetSelectedPlatform());
1647 ProcessSP remote_process_sp = platform_sp->Attach(
1648 m_options.attach_info, GetDebugger(), nullptr, err);
1650 result.AppendError(err.AsCString());
1651 } else if (!remote_process_sp) {
1652 result.AppendError("could not attach: unknown reason");
1654 result.SetStatus(eReturnStatusSuccessFinishResult);
1656 result.AppendError("no platform is currently selected");
1658 return result.Succeeded();
1661 Options *GetOptions() override { return &m_options; }
1664 CommandOptions m_options;
1667 class CommandObjectPlatformProcess : public CommandObjectMultiword {
1669 // Constructors and Destructors
1670 CommandObjectPlatformProcess(CommandInterpreter &interpreter)
1671 : CommandObjectMultiword(interpreter, "platform process",
1672 "Commands to query, launch and attach to "
1673 "processes on the current platform.",
1674 "platform process [attach|launch|list] ...") {
1677 CommandObjectSP(new CommandObjectPlatformProcessAttach(interpreter)));
1680 CommandObjectSP(new CommandObjectPlatformProcessLaunch(interpreter)));
1681 LoadSubCommand("info", CommandObjectSP(new CommandObjectPlatformProcessInfo(
1683 LoadSubCommand("list", CommandObjectSP(new CommandObjectPlatformProcessList(
1687 ~CommandObjectPlatformProcess() override = default;
1690 // For CommandObjectPlatform only
1691 CommandObjectPlatformProcess(const CommandObjectPlatformProcess &) = delete;
1692 const CommandObjectPlatformProcess &
1693 operator=(const CommandObjectPlatformProcess &) = delete;
1697 #define LLDB_OPTIONS_platform_shell
1698 #include "CommandOptions.inc"
1700 class CommandObjectPlatformShell : public CommandObjectRaw {
1702 class CommandOptions : public Options {
1704 CommandOptions() = default;
1706 ~CommandOptions() override = default;
1708 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1709 return llvm::makeArrayRef(g_platform_shell_options);
1712 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1713 ExecutionContext *execution_context) override {
1716 const char short_option = (char)GetDefinitions()[option_idx].short_option;
1718 switch (short_option) {
1720 m_use_host_platform = true;
1723 uint32_t timeout_sec;
1724 if (option_arg.getAsInteger(10, timeout_sec))
1725 error.SetErrorStringWithFormat(
1726 "could not convert \"%s\" to a numeric value.",
1727 option_arg.str().c_str());
1729 m_timeout = std::chrono::seconds(timeout_sec);
1732 if (option_arg.empty()) {
1733 error.SetErrorStringWithFormat(
1734 "missing shell interpreter path for option -i|--interpreter.");
1738 m_shell_interpreter = option_arg.str();
1742 llvm_unreachable("Unimplemented option");
1748 void OptionParsingStarting(ExecutionContext *execution_context) override {
1750 m_use_host_platform = false;
1751 m_shell_interpreter.clear();
1754 Timeout<std::micro> m_timeout = std::chrono::seconds(10);
1755 bool m_use_host_platform;
1756 std::string m_shell_interpreter;
1759 CommandObjectPlatformShell(CommandInterpreter &interpreter)
1760 : CommandObjectRaw(interpreter, "platform shell",
1761 "Run a shell command on the current platform.",
1762 "platform shell <shell-command>", 0) {
1763 CommandArgumentData thread_arg{eArgTypeNone, eArgRepeatStar};
1764 m_arguments.push_back({thread_arg});
1767 ~CommandObjectPlatformShell() override = default;
1769 Options *GetOptions() override { return &m_options; }
1771 bool DoExecute(llvm::StringRef raw_command_line,
1772 CommandReturnObject &result) override {
1773 ExecutionContext exe_ctx = GetCommandInterpreter().GetExecutionContext();
1774 m_options.NotifyOptionParsingStarting(&exe_ctx);
1776 // Print out an usage syntax on an empty command line.
1777 if (raw_command_line.empty()) {
1778 result.GetOutputStream().Printf("%s\n", this->GetSyntax().str().c_str());
1782 const bool is_alias = !raw_command_line.contains("platform");
1783 OptionsWithRaw args(raw_command_line);
1786 if (!ParseOptions(args.GetArgs(), result))
1789 if (args.GetRawPart().empty()) {
1790 result.GetOutputStream().Printf("%s <shell-command>\n",
1791 is_alias ? "shell" : "platform shell");
1795 llvm::StringRef cmd = args.GetRawPart();
1797 PlatformSP platform_sp(
1798 m_options.m_use_host_platform
1799 ? Platform::GetHostPlatform()
1800 : GetDebugger().GetPlatformList().GetSelectedPlatform());
1803 FileSpec working_dir{};
1807 error = (platform_sp->RunShellCommand(m_options.m_shell_interpreter, cmd,
1808 working_dir, &status, &signo,
1809 &output, m_options.m_timeout));
1810 if (!output.empty())
1811 result.GetOutputStream().PutCString(output);
1814 const char *signo_cstr = Host::GetSignalAsCString(signo);
1816 result.GetOutputStream().Printf(
1817 "error: command returned with status %i and signal %s\n",
1818 status, signo_cstr);
1820 result.GetOutputStream().Printf(
1821 "error: command returned with status %i and signal %i\n",
1824 result.GetOutputStream().Printf(
1825 "error: command returned with status %i\n", status);
1828 result.GetOutputStream().Printf(
1829 "error: cannot run remote shell commands without a platform\n");
1830 error.SetErrorString(
1831 "error: cannot run remote shell commands without a platform");
1835 result.AppendError(error.AsCString());
1837 result.SetStatus(eReturnStatusSuccessFinishResult);
1842 CommandOptions m_options;
1845 // "platform install" - install a target to a remote end
1846 class CommandObjectPlatformInstall : public CommandObjectParsed {
1848 CommandObjectPlatformInstall(CommandInterpreter &interpreter)
1849 : CommandObjectParsed(
1850 interpreter, "platform target-install",
1851 "Install a target (bundle or executable file) to the remote end.",
1852 "platform target-install <local-thing> <remote-sandbox>", 0) {
1853 CommandArgumentData local_arg{eArgTypePath, eArgRepeatPlain};
1854 CommandArgumentData remote_arg{eArgTypePath, eArgRepeatPlain};
1855 m_arguments.push_back({local_arg});
1856 m_arguments.push_back({remote_arg});
1859 ~CommandObjectPlatformInstall() override = default;
1862 HandleArgumentCompletion(CompletionRequest &request,
1863 OptionElementVector &opt_element_vector) override {
1864 if (request.GetCursorIndex())
1866 CommandCompletions::InvokeCommonCompletionCallbacks(
1867 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
1871 bool DoExecute(Args &args, CommandReturnObject &result) override {
1872 if (args.GetArgumentCount() != 2) {
1873 result.AppendError("platform target-install takes two arguments");
1876 // TODO: move the bulk of this code over to the platform itself
1877 FileSpec src(args.GetArgumentAtIndex(0));
1878 FileSystem::Instance().Resolve(src);
1879 FileSpec dst(args.GetArgumentAtIndex(1));
1880 if (!FileSystem::Instance().Exists(src)) {
1881 result.AppendError("source location does not exist or is not accessible");
1884 PlatformSP platform_sp(
1885 GetDebugger().GetPlatformList().GetSelectedPlatform());
1887 result.AppendError("no platform currently selected");
1891 Status error = platform_sp->Install(src, dst);
1892 if (error.Success()) {
1893 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1895 result.AppendErrorWithFormat("install failed: %s", error.AsCString());
1897 return result.Succeeded();
1901 CommandObjectPlatform::CommandObjectPlatform(CommandInterpreter &interpreter)
1902 : CommandObjectMultiword(
1903 interpreter, "platform", "Commands to manage and create platforms.",
1904 "platform [connect|disconnect|info|list|status|select] ...") {
1905 LoadSubCommand("select",
1906 CommandObjectSP(new CommandObjectPlatformSelect(interpreter)));
1907 LoadSubCommand("list",
1908 CommandObjectSP(new CommandObjectPlatformList(interpreter)));
1909 LoadSubCommand("status",
1910 CommandObjectSP(new CommandObjectPlatformStatus(interpreter)));
1911 LoadSubCommand("connect", CommandObjectSP(
1912 new CommandObjectPlatformConnect(interpreter)));
1915 CommandObjectSP(new CommandObjectPlatformDisconnect(interpreter)));
1916 LoadSubCommand("settings", CommandObjectSP(new CommandObjectPlatformSettings(
1918 LoadSubCommand("mkdir",
1919 CommandObjectSP(new CommandObjectPlatformMkDir(interpreter)));
1920 LoadSubCommand("file",
1921 CommandObjectSP(new CommandObjectPlatformFile(interpreter)));
1922 LoadSubCommand("file-exists",
1923 CommandObjectSP(new CommandObjectPlatformFileExists(interpreter)));
1924 LoadSubCommand("get-file", CommandObjectSP(new CommandObjectPlatformGetFile(
1926 LoadSubCommand("get-permissions",
1927 CommandObjectSP(new CommandObjectPlatformGetPermissions(interpreter)));
1928 LoadSubCommand("get-size", CommandObjectSP(new CommandObjectPlatformGetSize(
1930 LoadSubCommand("put-file", CommandObjectSP(new CommandObjectPlatformPutFile(
1932 LoadSubCommand("process", CommandObjectSP(
1933 new CommandObjectPlatformProcess(interpreter)));
1934 LoadSubCommand("shell",
1935 CommandObjectSP(new CommandObjectPlatformShell(interpreter)));
1938 CommandObjectSP(new CommandObjectPlatformInstall(interpreter)));
1941 CommandObjectPlatform::~CommandObjectPlatform() = default;