LLDB mainline
CommandObjectPlatform.cpp
Go to the documentation of this file.
1//===-- CommandObjectPlatform.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
12#include "lldb/Core/Debugger.h"
13#include "lldb/Core/Module.h"
25#include "lldb/Target/Process.h"
26#include "lldb/Utility/Args.h"
28#include "lldb/Utility/State.h"
29
30#include "llvm/ADT/SmallString.h"
31
32using namespace lldb;
33using namespace lldb_private;
34
35static mode_t ParsePermissionString(const char *) = delete;
36
37static mode_t ParsePermissionString(llvm::StringRef permissions) {
38 if (permissions.size() != 9)
39 return (mode_t)(-1);
40 bool user_r, user_w, user_x, group_r, group_w, group_x, world_r, world_w,
41 world_x;
42
43 user_r = (permissions[0] == 'r');
44 user_w = (permissions[1] == 'w');
45 user_x = (permissions[2] == 'x');
46
47 group_r = (permissions[3] == 'r');
48 group_w = (permissions[4] == 'w');
49 group_x = (permissions[5] == 'x');
50
51 world_r = (permissions[6] == 'r');
52 world_w = (permissions[7] == 'w');
53 world_x = (permissions[8] == 'x');
54
55 mode_t user, group, world;
56 user = (user_r ? 4 : 0) | (user_w ? 2 : 0) | (user_x ? 1 : 0);
57 group = (group_r ? 4 : 0) | (group_w ? 2 : 0) | (group_x ? 1 : 0);
58 world = (world_r ? 4 : 0) | (world_w ? 2 : 0) | (world_x ? 1 : 0);
59
60 return user | group | world;
61}
62
63#define LLDB_OPTIONS_permissions
64#include "CommandOptions.inc"
65
67public:
68 OptionPermissions() = default;
69
70 ~OptionPermissions() override = default;
71
73 SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
74 ExecutionContext *execution_context) override {
76 char short_option = (char)GetDefinitions()[option_idx].short_option;
77 switch (short_option) {
78 case 'v': {
79 if (option_arg.getAsInteger(8, m_permissions)) {
80 m_permissions = 0777;
82 "invalid value for permissions: %s", option_arg.str().c_str());
83 }
84
85 } break;
86 case 's': {
87 mode_t perms = ParsePermissionString(option_arg);
88 if (perms == (mode_t)-1)
90 "invalid value for permissions: %s", option_arg.str().c_str());
91 else
92 m_permissions = perms;
93 } break;
94 case 'r':
95 m_permissions |= lldb::eFilePermissionsUserRead;
96 break;
97 case 'w':
98 m_permissions |= lldb::eFilePermissionsUserWrite;
99 break;
100 case 'x':
101 m_permissions |= lldb::eFilePermissionsUserExecute;
102 break;
103 case 'R':
104 m_permissions |= lldb::eFilePermissionsGroupRead;
105 break;
106 case 'W':
107 m_permissions |= lldb::eFilePermissionsGroupWrite;
108 break;
109 case 'X':
110 m_permissions |= lldb::eFilePermissionsGroupExecute;
111 break;
112 case 'd':
113 m_permissions |= lldb::eFilePermissionsWorldRead;
114 break;
115 case 't':
116 m_permissions |= lldb::eFilePermissionsWorldWrite;
117 break;
118 case 'e':
119 m_permissions |= lldb::eFilePermissionsWorldExecute;
120 break;
121 default:
122 llvm_unreachable("Unimplemented option");
123 }
124
125 return error;
126 }
127
128 void OptionParsingStarting(ExecutionContext *execution_context) override {
129 m_permissions = 0;
130 }
131
132 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
133 return llvm::ArrayRef(g_permissions_options);
134 }
135
136 // Instance variables to hold the values for command options.
137
139
140private:
143};
144
145// "platform select <platform-name>"
147public:
149 : CommandObjectParsed(interpreter, "platform select",
150 "Create a platform if needed and select it as the "
151 "current platform.",
152 "platform select <platform-name>", 0),
154 false) // Don't include the "--platform" option by passing false
155 {
157 m_option_group.Finalize();
159 }
160
161 ~CommandObjectPlatformSelect() override = default;
162
167
168 Options *GetOptions() override { return &m_option_group; }
169
170protected:
171 void DoExecute(Args &args, CommandReturnObject &result) override {
172 if (args.GetArgumentCount() == 1) {
173 const char *platform_name = args.GetArgumentAtIndex(0);
174 if (platform_name && platform_name[0]) {
175 const bool select = true;
176 m_platform_options.SetPlatformName(platform_name);
178 ArchSpec platform_arch;
179 PlatformSP platform_sp(m_platform_options.CreatePlatformWithOptions(
180 m_interpreter, ArchSpec(), select, error, platform_arch));
181 if (platform_sp) {
183
184 platform_sp->GetStatus(result.GetOutputStream());
186 } else {
187 result.AppendError(error.AsCString());
188 }
189 } else {
190 result.AppendError("invalid platform name");
191 }
192 } else {
193 result.AppendError(
194 "platform create takes a platform name as an argument\n");
195 }
196 }
197
200};
201
202// "platform list"
204public:
206 : CommandObjectParsed(interpreter, "platform list",
207 "List all platforms that are available.", nullptr,
208 0) {}
209
210 ~CommandObjectPlatformList() override = default;
211
212protected:
213 void DoExecute(Args &args, CommandReturnObject &result) override {
214 Stream &ostrm = result.GetOutputStream();
215 ostrm.Printf("Available platforms:\n");
216
217 PlatformSP host_platform_sp(Platform::GetHostPlatform());
218 ostrm.Format("{0}: {1}\n", host_platform_sp->GetPluginName(),
219 host_platform_sp->GetDescription());
220
221 uint32_t idx;
222 for (idx = 0; true; ++idx) {
223 llvm::StringRef plugin_name =
225 if (plugin_name.empty())
226 break;
227 llvm::StringRef plugin_desc =
229 ostrm.Format("{0}: {1}\n", plugin_name, plugin_desc);
230 }
231
232 if (idx == 0) {
233 result.AppendError("no platforms are available\n");
234 } else
236 }
237};
238
239// "platform status"
241public:
243 : CommandObjectParsed(interpreter, "platform status",
244 "Display status for the current platform.", nullptr,
245 0) {}
246
247 ~CommandObjectPlatformStatus() override = default;
248
249protected:
250 void DoExecute(Args &args, CommandReturnObject &result) override {
251 Stream &ostrm = result.GetOutputStream();
252
253 Target *target = GetTarget();
254 PlatformSP platform_sp;
255 if (target)
256 platform_sp = target->GetPlatform();
257 if (!platform_sp)
259 if (platform_sp) {
260 platform_sp->GetStatus(ostrm);
262 } else {
263 result.AppendError("no platform is currently selected\n");
264 }
265 }
266};
267
268// "platform connect <connect-url>"
270public:
273 interpreter, "platform connect",
274 "Select the current platform by providing a connection URL.",
275 "platform connect <connect-url>", 0) {
277 }
278
279 ~CommandObjectPlatformConnect() override = default;
280
281protected:
282 void DoExecute(Args &args, CommandReturnObject &result) override {
283 Stream &ostrm = result.GetOutputStream();
284
285 PlatformSP platform_sp(
286 GetDebugger().GetPlatformList().GetSelectedPlatform());
287 if (platform_sp) {
288 Status error(platform_sp->ConnectRemote(args));
289 if (error.Success()) {
290 platform_sp->GetStatus(ostrm);
292
293 platform_sp->ConnectToWaitingProcesses(GetDebugger(), error);
294 if (error.Fail()) {
295 result.AppendError(error.AsCString());
296 }
297 } else {
298 result.AppendErrorWithFormat("%s", error.AsCString());
299 }
300 } else {
301 result.AppendError("no platform is currently selected\n");
302 }
303 }
304
305 Options *GetOptions() override {
306 PlatformSP platform_sp(
307 GetDebugger().GetPlatformList().GetSelectedPlatform());
308 OptionGroupOptions *m_platform_options = nullptr;
309 if (platform_sp) {
310 m_platform_options = platform_sp->GetConnectionOptions(m_interpreter);
311 if (m_platform_options != nullptr && !m_platform_options->m_did_finalize)
312 m_platform_options->Finalize();
313 }
314 return m_platform_options;
315 }
316};
317
318// "platform disconnect"
320public:
322 : CommandObjectParsed(interpreter, "platform disconnect",
323 "Disconnect from the current platform.",
324 "platform disconnect", 0) {}
325
327
328protected:
329 void DoExecute(Args &args, CommandReturnObject &result) override {
330 PlatformSP platform_sp(
331 GetDebugger().GetPlatformList().GetSelectedPlatform());
332 if (platform_sp) {
333 if (args.GetArgumentCount() == 0) {
335
336 if (platform_sp->IsConnected()) {
337 // Cache the instance name if there is one since we are about to
338 // disconnect and the name might go with it.
339 const char *hostname_cstr = platform_sp->GetHostname();
340 std::string hostname;
341 if (hostname_cstr)
342 hostname.assign(hostname_cstr);
343
344 error = platform_sp->DisconnectRemote();
345 if (error.Success()) {
346 Stream &ostrm = result.GetOutputStream();
347 if (hostname.empty())
348 ostrm.Format("Disconnected from \"{0}\"\n",
349 platform_sp->GetPluginName());
350 else
351 ostrm.Printf("Disconnected from \"%s\"\n", hostname.c_str());
353 } else {
354 result.AppendErrorWithFormat("%s", error.AsCString());
355 }
356 } else {
357 // Not connected...
358 result.AppendErrorWithFormatv("not connected to '{0}'",
359 platform_sp->GetPluginName());
360 }
361 } else {
362 // Bad args
363 result.AppendError(
364 "\"platform disconnect\" doesn't take any arguments");
365 }
366 } else {
367 result.AppendError("no platform is currently selected");
368 }
369 }
370};
371
372// "platform settings"
374public:
376 : CommandObjectParsed(interpreter, "platform settings",
377 "Set settings for the current target's platform.",
378 "platform settings", 0),
379 m_option_working_dir(LLDB_OPT_SET_1, false, "working-dir", 'w',
381 "The working directory for the platform.") {
383 }
384
385 ~CommandObjectPlatformSettings() override = default;
386
387protected:
388 void DoExecute(Args &args, CommandReturnObject &result) override {
389 PlatformSP platform_sp(
390 GetDebugger().GetPlatformList().GetSelectedPlatform());
391 if (platform_sp) {
392 if (m_option_working_dir.GetOptionValue().OptionWasSet())
393 platform_sp->SetWorkingDirectory(
394 m_option_working_dir.GetOptionValue().GetCurrentValue());
396 } else {
397 result.AppendError("no platform is currently selected");
398 }
399 }
400
401 Options *GetOptions() override {
402 if (!m_options.DidFinalize())
403 m_options.Finalize();
404 return &m_options;
405 }
406
409};
410
411// "platform mkdir"
413public:
415 : CommandObjectParsed(interpreter, "platform mkdir",
416 "Make a new directory on the remote end.", nullptr,
417 0) {
419 }
420
421 ~CommandObjectPlatformMkDir() override = default;
422
423 void DoExecute(Args &args, CommandReturnObject &result) override {
424 PlatformSP platform_sp(
425 GetDebugger().GetPlatformList().GetSelectedPlatform());
426 if (platform_sp) {
427 std::string cmd_line;
428 args.GetCommandString(cmd_line);
429 uint32_t mode;
430 const OptionPermissions *options_permissions =
431 (const OptionPermissions *)m_options.GetGroupWithOption('r');
432 if (options_permissions)
433 mode = options_permissions->m_permissions;
434 else
435 mode = lldb::eFilePermissionsUserRWX | lldb::eFilePermissionsGroupRWX |
436 lldb::eFilePermissionsWorldRX;
437 Status error = platform_sp->MakeDirectory(FileSpec(cmd_line), mode);
438 if (error.Success()) {
440 } else {
441 result.AppendError(error.AsCString());
442 }
443 } else {
444 result.AppendError("no platform currently selected\n");
445 }
446 }
447
448 Options *GetOptions() override {
449 if (!m_options.DidFinalize()) {
451 m_options.Finalize();
452 }
453 return &m_options;
454 }
455
458};
459
460// "platform fopen"
462public:
464 : CommandObjectParsed(interpreter, "platform file open",
465 "Open a file on the remote end.", nullptr, 0) {
467 }
468
469 ~CommandObjectPlatformFOpen() override = default;
470
471 void DoExecute(Args &args, CommandReturnObject &result) override {
472 PlatformSP platform_sp(
473 GetDebugger().GetPlatformList().GetSelectedPlatform());
474 if (platform_sp) {
476 std::string cmd_line;
477 args.GetCommandString(cmd_line);
478 mode_t perms;
479 const OptionPermissions *options_permissions =
480 (const OptionPermissions *)m_options.GetGroupWithOption('r');
481 if (options_permissions)
482 perms = options_permissions->m_permissions;
483 else
484 perms = lldb::eFilePermissionsUserRW | lldb::eFilePermissionsGroupRW |
485 lldb::eFilePermissionsWorldRead;
486 lldb::user_id_t fd = platform_sp->OpenFile(
487 FileSpec(cmd_line),
489 perms, error);
490 if (error.Success()) {
491 result.AppendMessageWithFormatv("File Descriptor = {0}", fd);
493 } else {
494 result.AppendError(error.AsCString());
495 }
496 } else {
497 result.AppendError("no platform currently selected\n");
498 }
499 }
500
501 Options *GetOptions() override {
502 if (!m_options.DidFinalize()) {
504 m_options.Finalize();
505 }
506 return &m_options;
507 }
508
511};
512
513// "platform fclose"
515public:
517 : CommandObjectParsed(interpreter, "platform file close",
518 "Close a file on the remote end.", nullptr, 0) {
520 }
521
522 ~CommandObjectPlatformFClose() override = default;
523
524 void DoExecute(Args &args, CommandReturnObject &result) override {
525 PlatformSP platform_sp(
526 GetDebugger().GetPlatformList().GetSelectedPlatform());
527 if (platform_sp) {
528 std::string cmd_line;
529 args.GetCommandString(cmd_line);
531 if (!llvm::to_integer(cmd_line, fd)) {
532 result.AppendErrorWithFormatv("'{0}' is not a valid file descriptor.\n",
533 cmd_line);
534 return;
535 }
537 bool success = platform_sp->CloseFile(fd, error);
538 if (success) {
539 result.AppendMessageWithFormatv("file {0} closed.", fd);
541 } else {
542 result.AppendError(error.AsCString());
543 }
544 } else {
545 result.AppendError("no platform currently selected\n");
546 }
547 }
548};
549
550// "platform fread"
551
552#define LLDB_OPTIONS_platform_fread
553#include "CommandOptions.inc"
554
556public:
558 : CommandObjectParsed(interpreter, "platform file read",
559 "Read data from a file on the remote end.", nullptr,
560 0) {
562 }
563
564 ~CommandObjectPlatformFRead() override = default;
565
566 void DoExecute(Args &args, CommandReturnObject &result) override {
567 PlatformSP platform_sp(
568 GetDebugger().GetPlatformList().GetSelectedPlatform());
569 if (platform_sp) {
570 std::string cmd_line;
571 args.GetCommandString(cmd_line);
573 if (!llvm::to_integer(cmd_line, fd)) {
574 result.AppendErrorWithFormatv("'{0}' is not a valid file descriptor.\n",
575 cmd_line);
576 return;
577 }
578 std::string buffer(m_options.m_count, 0);
580 uint64_t retcode = platform_sp->ReadFile(
581 fd, m_options.m_offset, &buffer[0], m_options.m_count, error);
582 if (retcode != UINT64_MAX) {
583 result.AppendMessageWithFormatv("Return = {0}", retcode);
584 result.AppendMessageWithFormatv("Data = \"{0}\"", buffer.c_str());
586 } else {
587 result.AppendError(error.AsCString());
588 }
589 } else {
590 result.AppendError("no platform currently selected\n");
591 }
592 }
593
594 Options *GetOptions() override { return &m_options; }
595
596protected:
597 class CommandOptions : public Options {
598 public:
599 CommandOptions() = default;
600
601 ~CommandOptions() override = default;
602
603 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
604 ExecutionContext *execution_context) override {
606 char short_option = (char)m_getopt_table[option_idx].val;
607
608 switch (short_option) {
609 case 'o':
610 if (option_arg.getAsInteger(0, m_offset))
611 error = Status::FromErrorStringWithFormat("invalid offset: '%s'",
612 option_arg.str().c_str());
613 break;
614 case 'c':
615 if (option_arg.getAsInteger(0, m_count))
616 error = Status::FromErrorStringWithFormat("invalid offset: '%s'",
617 option_arg.str().c_str());
618 break;
619 default:
620 llvm_unreachable("Unimplemented option");
621 }
622
623 return error;
624 }
625
626 void OptionParsingStarting(ExecutionContext *execution_context) override {
627 m_offset = 0;
628 m_count = 1;
629 }
630
631 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
632 return llvm::ArrayRef(g_platform_fread_options);
633 }
634
635 // Instance variables to hold the values for command options.
636
637 uint32_t m_offset;
638 uint32_t m_count;
639 };
640
642};
643
644// "platform fwrite"
645
646#define LLDB_OPTIONS_platform_fwrite
647#include "CommandOptions.inc"
648
650public:
652 : CommandObjectParsed(interpreter, "platform file write",
653 "Write data to a file on the remote end.", nullptr,
654 0) {
656 }
657
658 ~CommandObjectPlatformFWrite() override = default;
659
660 void DoExecute(Args &args, CommandReturnObject &result) override {
661 PlatformSP platform_sp(
662 GetDebugger().GetPlatformList().GetSelectedPlatform());
663 if (platform_sp) {
664 std::string cmd_line;
665 args.GetCommandString(cmd_line);
668 if (!llvm::to_integer(cmd_line, fd)) {
669 result.AppendErrorWithFormatv("'{0}' is not a valid file descriptor.",
670 cmd_line);
671 return;
672 }
673 uint64_t retcode =
674 platform_sp->WriteFile(fd, m_options.m_offset, &m_options.m_data[0],
675 m_options.m_data.size(), error);
676 if (retcode != UINT64_MAX) {
677 result.AppendMessageWithFormatv("Return = {0}", retcode);
679 } else {
680 result.AppendError(error.AsCString());
681 }
682 } else {
683 result.AppendError("no platform currently selected\n");
684 }
685 }
686
687 Options *GetOptions() override { return &m_options; }
688
689protected:
690 class CommandOptions : public Options {
691 public:
692 CommandOptions() = default;
693
694 ~CommandOptions() override = default;
695
696 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
697 ExecutionContext *execution_context) override {
699 char short_option = (char)m_getopt_table[option_idx].val;
700
701 switch (short_option) {
702 case 'o':
703 if (option_arg.getAsInteger(0, m_offset))
704 error = Status::FromErrorStringWithFormat("invalid offset: '%s'",
705 option_arg.str().c_str());
706 break;
707 case 'd':
708 m_data.assign(std::string(option_arg));
709 break;
710 default:
711 llvm_unreachable("Unimplemented option");
712 }
713
714 return error;
715 }
716
717 void OptionParsingStarting(ExecutionContext *execution_context) override {
718 m_offset = 0;
719 m_data.clear();
720 }
721
722 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
723 return llvm::ArrayRef(g_platform_fwrite_options);
724 }
725
726 // Instance variables to hold the values for command options.
727
728 uint32_t m_offset;
729 std::string m_data;
730 };
731
733};
734
736public:
737 // Constructors and Destructors
740 interpreter, "platform file",
741 "Commands to access files on the current platform.",
742 "platform file [open|close|read|write] ...") {
744 "open", CommandObjectSP(new CommandObjectPlatformFOpen(interpreter)));
746 "close", CommandObjectSP(new CommandObjectPlatformFClose(interpreter)));
748 "read", CommandObjectSP(new CommandObjectPlatformFRead(interpreter)));
750 "write", CommandObjectSP(new CommandObjectPlatformFWrite(interpreter)));
751 }
752
753 ~CommandObjectPlatformFile() override = default;
754
755private:
756 // For CommandObjectPlatform only
760};
761
762// "platform get-file remote-file-path host-file-path"
764public:
767 interpreter, "platform get-file",
768 "Transfer a file from the remote end to the local host.",
769 "platform get-file <remote-file-spec> <local-file-spec>", 0) {
771 R"(Examples:
772
773(lldb) platform get-file /the/remote/file/path /the/local/file/path
774
775 Transfer a file from the remote end with file path /the/remote/file/path to the local host.)");
776
777 CommandArgumentEntry arg1, arg2;
778 CommandArgumentData file_arg_remote, file_arg_host;
779
780 // Define the first (and only) variant of this arg.
781 file_arg_remote.arg_type = eArgTypeRemoteFilename;
782 file_arg_remote.arg_repetition = eArgRepeatPlain;
783 // There is only one variant this argument could be; put it into the
784 // argument entry.
785 arg1.push_back(file_arg_remote);
786
787 // Define the second (and only) variant of this arg.
788 file_arg_host.arg_type = eArgTypeFilename;
789 file_arg_host.arg_repetition = eArgRepeatPlain;
790 // There is only one variant this argument could be; put it into the
791 // argument entry.
792 arg2.push_back(file_arg_host);
793
794 // Push the data for the first and the second arguments into the
795 // m_arguments vector.
796 m_arguments.push_back(arg1);
797 m_arguments.push_back(arg2);
798 }
800 ~CommandObjectPlatformGetFile() override = default;
801
802 void
804 OptionElementVector &opt_element_vector) override {
805 if (request.GetCursorIndex() == 0)
808 nullptr);
809 else if (request.GetCursorIndex() == 1)
812 }
813
814 void DoExecute(Args &args, CommandReturnObject &result) override {
815 // If the number of arguments is incorrect, issue an error message.
816 if (args.GetArgumentCount() != 2) {
817 result.AppendError("required arguments missing; specify both the "
818 "source and destination file paths");
819 return;
820 }
821
822 PlatformSP platform_sp(
823 GetDebugger().GetPlatformList().GetSelectedPlatform());
824 if (platform_sp) {
825 const char *remote_file_path = args.GetArgumentAtIndex(0);
826 const char *local_file_path = args.GetArgumentAtIndex(1);
827 Status error = platform_sp->GetFile(FileSpec(remote_file_path),
828 FileSpec(local_file_path));
829 if (error.Success()) {
831 "successfully get-file from {0} (remote) to {1} (host)",
832 remote_file_path, local_file_path);
834 } else {
835 result.AppendErrorWithFormatv("get-file failed: {0}",
836 error.AsCString());
837 }
838 } else {
839 result.AppendError("no platform currently selected\n");
840 }
841 }
842};
843
844// "platform get-size remote-file-path"
846public:
848 : CommandObjectParsed(interpreter, "platform get-size",
849 "Get the file size from the remote end.",
850 "platform get-size <remote-file-spec>", 0) {
852 R"(Examples:
853
854(lldb) platform get-size /the/remote/file/path
855
856 Get the file size from the remote end with path /the/remote/file/path.)");
860
861 ~CommandObjectPlatformGetSize() override = default;
862
863 void DoExecute(Args &args, CommandReturnObject &result) override {
864 // If the number of arguments is incorrect, issue an error message.
865 if (args.GetArgumentCount() != 1) {
866 result.AppendError("required argument missing; specify the source file "
867 "path as the only argument");
868 return;
869 }
870
871 PlatformSP platform_sp(
872 GetDebugger().GetPlatformList().GetSelectedPlatform());
873 if (platform_sp) {
874 std::string remote_file_path(args.GetArgumentAtIndex(0));
875 user_id_t size = platform_sp->GetFileSize(FileSpec(remote_file_path));
876 if (size != UINT64_MAX) {
877 result.AppendMessageWithFormatv("File size of {0} (remote): {1}",
878 remote_file_path.c_str(), size);
880 } else {
881 result.AppendErrorWithFormatv("failed to get file size of {0} (remote)",
882 remote_file_path.c_str());
883 }
884 } else {
885 result.AppendError("no platform currently selected\n");
886 }
887 }
888};
889
890// "platform get-permissions remote-file-path"
892public:
894 : CommandObjectParsed(interpreter, "platform get-permissions",
895 "Get the file permission bits from the remote end.",
896 "platform get-permissions <remote-file-spec>", 0) {
898 R"(Examples:
899
900(lldb) platform get-permissions /the/remote/file/path
901
902 Get the file permissions from the remote end with path /the/remote/file/path.)");
906
907 ~CommandObjectPlatformGetPermissions() override = default;
908
909 void DoExecute(Args &args, CommandReturnObject &result) override {
910 // If the number of arguments is incorrect, issue an error message.
911 if (args.GetArgumentCount() != 1) {
912 result.AppendError("required argument missing; specify the source file "
913 "path as the only argument");
914 return;
915 }
916
917 PlatformSP platform_sp(
918 GetDebugger().GetPlatformList().GetSelectedPlatform());
919 if (platform_sp) {
920 std::string remote_file_path(args.GetArgumentAtIndex(0));
921 uint32_t permissions;
922 Status error = platform_sp->GetFilePermissions(FileSpec(remote_file_path),
923 permissions);
924 if (error.Success()) {
926 "File permissions of {0} (remote): 0o{1}", remote_file_path,
927 llvm::format("%04o", permissions));
929 } else
930 result.AppendError(error.AsCString());
931 } else {
932 result.AppendError("no platform currently selected\n");
933 }
934 }
935};
936
937// "platform file-exists remote-file-path"
939public:
941 : CommandObjectParsed(interpreter, "platform file-exists",
942 "Check if the file exists on the remote end.",
943 "platform file-exists <remote-file-spec>", 0) {
945 R"(Examples:
946
947(lldb) platform file-exists /the/remote/file/path
948
949 Check if /the/remote/file/path exists on the remote end.)");
953
954 ~CommandObjectPlatformFileExists() override = default;
955
956 void DoExecute(Args &args, CommandReturnObject &result) override {
957 // If the number of arguments is incorrect, issue an error message.
958 if (args.GetArgumentCount() != 1) {
959 result.AppendError("required argument missing; specify the source file "
960 "path as the only argument");
961 return;
962 }
963
964 PlatformSP platform_sp(
965 GetDebugger().GetPlatformList().GetSelectedPlatform());
966 if (platform_sp) {
967 std::string remote_file_path(args.GetArgumentAtIndex(0));
968 bool exists = platform_sp->GetFileExists(FileSpec(remote_file_path));
969 result.AppendMessageWithFormatv("File {0} (remote) {1}",
970 remote_file_path.c_str(),
971 exists ? "exists" : "does not exist");
973 } else {
974 result.AppendError("no platform currently selected\n");
975 }
976 }
977};
978
979// "platform put-file"
981public:
984 interpreter, "platform put-file",
985 "Transfer a file from this system to the remote end.",
986 "platform put-file <source> [<destination>]", 0) {
988 R"(Examples:
989
990(lldb) platform put-file /source/foo.txt /destination/bar.txt
991
992(lldb) platform put-file /source/foo.txt
993
994 Relative source file paths are resolved against lldb's local working directory.
996 Omitting the destination places the file in the platform working directory.)");
999 m_arguments.push_back({source_arg});
1000 m_arguments.push_back({path_arg});
1001 }
1002
1003 ~CommandObjectPlatformPutFile() override = default;
1004
1005 void
1006 HandleArgumentCompletion(CompletionRequest &request,
1007 OptionElementVector &opt_element_vector) override {
1008 if (request.GetCursorIndex() == 0)
1011 else if (request.GetCursorIndex() == 1)
1014 nullptr);
1015 }
1016
1017 void DoExecute(Args &args, CommandReturnObject &result) override {
1018 const char *src = args.GetArgumentAtIndex(0);
1019 const char *dst = args.GetArgumentAtIndex(1);
1020
1021 FileSpec src_fs(src);
1022 FileSystem::Instance().Resolve(src_fs);
1023 FileSpec dst_fs(dst ? dst : src_fs.GetFilename().GetCString());
1024
1025 PlatformSP platform_sp(
1026 GetDebugger().GetPlatformList().GetSelectedPlatform());
1027 if (platform_sp) {
1028 Status error(platform_sp->PutFile(src_fs, dst_fs));
1029 if (error.Success()) {
1031 } else {
1032 result.AppendError(error.AsCString());
1033 }
1034 } else {
1035 result.AppendError("no platform currently selected\n");
1036 }
1037 }
1038};
1039
1040// "platform process launch"
1042public:
1044 : CommandObjectParsed(interpreter, "platform process launch",
1045 "Launch a new process on a remote platform.",
1046 "platform process launch program",
1047 eCommandRequiresTarget | eCommandTryTargetAPILock),
1048 m_class_options("scripted process", true, 'C', 'k', 'v', 0) {
1049 m_all_options.Append(&m_options);
1052 m_all_options.Finalize();
1054 }
1055
1056 void
1058 OptionElementVector &opt_element_vector) override {
1059 // I didn't make a type for RemoteRunArgs, but since we're going to run
1060 // this on the remote system we should use the remote completer.
1063 nullptr);
1064 }
1065
1067
1068 Options *GetOptions() override { return &m_all_options; }
1069
1070protected:
1071 void DoExecute(Args &args, CommandReturnObject &result) override {
1072 Target *target = GetTarget();
1073 assert(target && "target guaranteed by eCommandRequiresTarget");
1074 PlatformSP platform_sp = target->GetPlatform();
1075 if (!platform_sp) {
1077 }
1078
1079 if (platform_sp) {
1080 Status error;
1081 const size_t argc = args.GetArgumentCount();
1082 Module *exe_module = target->GetExecutableModulePointer();
1083 if (exe_module) {
1084 m_options.launch_info.GetExecutableFile() = exe_module->GetFileSpec();
1085 llvm::SmallString<128> exe_path;
1086 m_options.launch_info.GetExecutableFile().GetPath(exe_path);
1087 if (!exe_path.empty())
1088 m_options.launch_info.GetArguments().AppendArgument(exe_path);
1089 m_options.launch_info.GetArchitecture() = exe_module->GetArchitecture();
1090 }
1091
1092 if (!m_class_options.GetName().empty()) {
1093 m_options.launch_info.SetProcessPluginName("ScriptedProcess");
1094 ScriptedMetadataSP metadata_sp = std::make_shared<ScriptedMetadata>(
1095 m_class_options.GetName(), m_class_options.GetStructuredData());
1096 m_options.launch_info.SetScriptedMetadata(metadata_sp);
1097 target->SetProcessLaunchInfo(m_options.launch_info);
1098 }
1099
1100 if (argc > 0) {
1101 if (m_options.launch_info.GetExecutableFile()) {
1102 // We already have an executable file, so we will use this and all
1103 // arguments to this function are extra arguments
1104 m_options.launch_info.GetArguments().AppendArguments(args);
1105 } else {
1106 // We don't have any file yet, so the first argument is our
1107 // executable, and the rest are program arguments
1108 const bool first_arg_is_executable = true;
1109 m_options.launch_info.SetArguments(args, first_arg_is_executable);
1110 }
1111 }
1112
1113 if (m_options.launch_info.GetExecutableFile()) {
1114 Debugger &debugger = GetDebugger();
1115
1116 if (argc == 0) {
1117 // If no arguments were given to the command, use target->run-args.
1118 Args target_run_args;
1119 target->GetRunArguments(target_run_args);
1120 m_options.launch_info.GetArguments().AppendArguments(target_run_args);
1121 }
1122
1123 ProcessSP process_sp(platform_sp->DebugProcess(
1124 m_options.launch_info, debugger, *target, error));
1125
1126 if (!process_sp && error.Success()) {
1127 result.AppendError("failed to launch or debug process");
1128 return;
1129 } else if (!error.Success()) {
1130 result.AppendError(error.AsCString());
1131 return;
1132 }
1133
1134 const bool synchronous_execution =
1136 auto launch_info = m_options.launch_info;
1137 bool rebroadcast_first_stop =
1138 !synchronous_execution &&
1139 launch_info.GetFlags().Test(eLaunchFlagStopAtEntry);
1140
1141 EventSP first_stop_event_sp;
1142 StateType state = process_sp->WaitForProcessToStop(
1143 std::nullopt, &first_stop_event_sp, rebroadcast_first_stop,
1144 launch_info.GetHijackListener());
1145 process_sp->RestoreProcessEvents();
1146
1147 if (rebroadcast_first_stop) {
1148 assert(first_stop_event_sp);
1149 process_sp->BroadcastEvent(first_stop_event_sp);
1151 return;
1152 }
1153
1154 switch (state) {
1155 case eStateStopped: {
1156 if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
1157 break;
1158 if (synchronous_execution) {
1159 // Now we have handled the stop-from-attach, and we are just
1160 // switching to a synchronous resume. So we should switch to the
1161 // SyncResume hijacker.
1162 process_sp->ResumeSynchronous(&result.GetOutputStream());
1163 } else {
1164 error = process_sp->Resume();
1165 if (!error.Success()) {
1166 result.AppendErrorWithFormat(
1167 "process resume at entry point failed: %s",
1168 error.AsCString());
1169 }
1170 }
1171 } break;
1172 default:
1173 result.AppendErrorWithFormat(
1174 "initial process state wasn't stopped: %s",
1175 StateAsCString(state));
1176 break;
1177 }
1178
1179 if (process_sp && process_sp->IsAlive()) {
1181 return;
1182 }
1183 if (result.GetStatus() != eReturnStatusFailed)
1185 } else {
1186 result.AppendError("'platform process launch' uses the current target "
1187 "file and arguments, or the executable and its "
1188 "arguments can be specified in this command");
1189 return;
1190 }
1191 } else {
1192 result.AppendError("no platform is selected\n");
1193 }
1194 }
1195
1199};
1200
1201// "platform process list"
1202
1204#define LLDB_OPTIONS_platform_process_list
1205#include "CommandOptions.inc"
1206
1208public:
1210 : CommandObjectParsed(interpreter, "platform process list",
1211 "List processes on a remote platform by name, pid, "
1212 "or many other matching attributes.",
1213 "platform process list", 0) {}
1214
1216
1217 Options *GetOptions() override { return &m_options; }
1218
1219protected:
1220 void DoExecute(Args &args, CommandReturnObject &result) override {
1221 Target *target = GetTarget();
1222 PlatformSP platform_sp;
1223 if (target) {
1224 platform_sp = target->GetPlatform();
1225 }
1226 if (!platform_sp) {
1228 }
1229
1230 if (platform_sp) {
1231 Stream &ostrm = result.GetOutputStream();
1232
1233 lldb::pid_t pid = m_options.match_info.GetProcessInfo().GetProcessID();
1234 if (pid != LLDB_INVALID_PROCESS_ID) {
1235 ProcessInstanceInfo proc_info;
1236 if (platform_sp->GetProcessInfo(pid, proc_info)) {
1238 m_options.verbose);
1239 proc_info.DumpAsTableRow(ostrm, platform_sp->GetUserIDResolver(),
1240 m_options.show_args, m_options.verbose);
1242 } else {
1243 result.AppendErrorWithFormat("no process found with pid = %" PRIu64,
1244 pid);
1245 }
1246 } else {
1247 ProcessInstanceInfoList proc_infos;
1248 const uint32_t matches =
1249 platform_sp->FindProcesses(m_options.match_info, proc_infos);
1250 const char *match_desc = nullptr;
1251 const char *match_name =
1252 m_options.match_info.GetProcessInfo().GetName();
1253 if (match_name && match_name[0]) {
1254 switch (m_options.match_info.GetNameMatchType()) {
1255 case NameMatch::Ignore:
1256 break;
1257 case NameMatch::Equals:
1258 match_desc = "matched";
1259 break;
1261 match_desc = "contained";
1262 break;
1264 match_desc = "started with";
1265 break;
1267 match_desc = "ended with";
1268 break;
1270 match_desc = "matched the regular expression";
1271 break;
1272 }
1273 }
1274
1275 if (matches == 0) {
1276 if (match_desc)
1278 "no processes were found that {0} \"{1}\" on the \"{2}\" "
1279 "platform\n",
1280 match_desc, match_name, platform_sp->GetName());
1281 else
1283 "no processes were found on the \"{0}\" platform\n",
1284 platform_sp->GetName());
1285 } else {
1287 "{0} matching process{1} found on \"{2}\"", matches,
1288 matches > 1 ? "es were" : " was", platform_sp->GetName());
1289 Stream &strm = result.GetOutputStream();
1290 if (match_desc)
1291 strm << llvm::formatv(" whose name {0} \"{1}\"", match_desc,
1292 match_name);
1293 strm.PutChar('\n');
1295 m_options.verbose);
1296 for (uint32_t i = 0; i < matches; ++i) {
1297 proc_infos[i].DumpAsTableRow(
1298 ostrm, platform_sp->GetUserIDResolver(), m_options.show_args,
1299 m_options.verbose);
1300 }
1302 }
1303 }
1304 } else {
1305 result.AppendError("no platform is selected\n");
1306 }
1307 }
1308
1309 class CommandOptions : public Options {
1310 public:
1311 CommandOptions() = default;
1312
1313 ~CommandOptions() override = default;
1314
1315 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1316 ExecutionContext *execution_context) override {
1317 Status error;
1318 const int short_option = m_getopt_table[option_idx].val;
1319 bool success = false;
1320
1321 uint32_t id = LLDB_INVALID_PROCESS_ID;
1322 success = !option_arg.getAsInteger(0, id);
1323 switch (short_option) {
1324 case 'p': {
1325 match_info.GetProcessInfo().SetProcessID(id);
1326 if (!success)
1328 "invalid process ID string: '%s'", option_arg.str().c_str());
1329 break;
1330 }
1331 case 'P':
1332 match_info.GetProcessInfo().SetParentProcessID(id);
1333 if (!success)
1335 "invalid parent process ID string: '%s'",
1336 option_arg.str().c_str());
1337 break;
1338
1339 case 'u':
1340 match_info.GetProcessInfo().SetUserID(success ? id : UINT32_MAX);
1341 if (!success)
1343 "invalid user ID string: '%s'", option_arg.str().c_str());
1344 break;
1345
1346 case 'U':
1347 match_info.GetProcessInfo().SetEffectiveUserID(success ? id
1348 : UINT32_MAX);
1349 if (!success)
1351 "invalid effective user ID string: '%s'",
1352 option_arg.str().c_str());
1353 break;
1354
1355 case 'g':
1356 match_info.GetProcessInfo().SetGroupID(success ? id : UINT32_MAX);
1357 if (!success)
1359 "invalid group ID string: '%s'", option_arg.str().c_str());
1360 break;
1361
1362 case 'G':
1363 match_info.GetProcessInfo().SetEffectiveGroupID(success ? id
1364 : UINT32_MAX);
1365 if (!success)
1367 "invalid effective group ID string: '%s'",
1368 option_arg.str().c_str());
1369 break;
1370
1371 case 'a': {
1372 TargetSP target_sp =
1373 execution_context ? execution_context->GetTargetSP() : TargetSP();
1374 DebuggerSP debugger_sp =
1375 target_sp ? target_sp->GetDebugger().shared_from_this()
1376 : DebuggerSP();
1377 PlatformSP platform_sp =
1378 debugger_sp ? debugger_sp->GetPlatformList().GetSelectedPlatform()
1379 : PlatformSP();
1380 match_info.GetProcessInfo().GetArchitecture() =
1381 Platform::GetAugmentedArchSpec(platform_sp.get(), option_arg);
1382 } break;
1383
1384 case 'n':
1385 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1386 option_arg, FileSpec::Style::native);
1387 match_info.SetNameMatchType(NameMatch::Equals);
1388 break;
1389
1390 case 'e':
1391 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1392 option_arg, FileSpec::Style::native);
1393 match_info.SetNameMatchType(NameMatch::EndsWith);
1394 break;
1395
1396 case 's':
1397 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1398 option_arg, FileSpec::Style::native);
1399 match_info.SetNameMatchType(NameMatch::StartsWith);
1400 break;
1401
1402 case 'c':
1403 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1404 option_arg, FileSpec::Style::native);
1405 match_info.SetNameMatchType(NameMatch::Contains);
1406 break;
1407
1408 case 'r':
1409 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1410 option_arg, FileSpec::Style::native);
1411 match_info.SetNameMatchType(NameMatch::RegularExpression);
1412 break;
1413
1414 case 'A':
1415 show_args = true;
1416 break;
1417
1418 case 'v':
1419 verbose = true;
1420 break;
1421
1422 case 'x':
1423 match_info.SetMatchAllUsers(true);
1424 break;
1425
1426 default:
1427 llvm_unreachable("Unimplemented option");
1428 }
1429
1430 return error;
1431 }
1432
1433 void OptionParsingStarting(ExecutionContext *execution_context) override {
1434 match_info.Clear();
1435 show_args = false;
1436 verbose = false;
1437 }
1438
1439 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1440 return llvm::ArrayRef(g_platform_process_list_options);
1441 }
1442
1443 // Instance variables to hold the values for command options.
1444
1446 bool show_args = false;
1447 bool verbose = false;
1448 };
1449
1451};
1452
1453// "platform process info"
1455public:
1458 interpreter, "platform process info",
1459 "Get detailed information for one or more process by process ID.",
1460 "platform process info <pid> [<pid> <pid> ...]", 0) {
1462 }
1463
1465
1466protected:
1467 void DoExecute(Args &args, CommandReturnObject &result) override {
1468 Target *target = GetTarget();
1469 PlatformSP platform_sp;
1470 if (target) {
1471 platform_sp = target->GetPlatform();
1472 }
1473 if (!platform_sp) {
1475 }
1476
1477 if (platform_sp) {
1478 const size_t argc = args.GetArgumentCount();
1479 if (argc > 0) {
1480 Status error;
1481
1482 if (platform_sp->IsConnected()) {
1483 Stream &ostrm = result.GetOutputStream();
1484 for (auto &entry : args.entries()) {
1485 lldb::pid_t pid;
1486 if (entry.ref().getAsInteger(0, pid)) {
1487 result.AppendErrorWithFormat("invalid process ID argument '%s'",
1488 entry.ref().str().c_str());
1489 break;
1490 } else {
1491 ProcessInstanceInfo proc_info;
1492 if (platform_sp->GetProcessInfo(pid, proc_info)) {
1493 ostrm.Printf("Process information for process %" PRIu64 ":\n",
1494 pid);
1495 proc_info.Dump(ostrm, platform_sp->GetUserIDResolver());
1496 } else {
1497 ostrm.Printf("error: no process information is available for "
1498 "process %" PRIu64 "\n",
1499 pid);
1500 }
1501 ostrm.EOL();
1502 }
1503 }
1504 if (result.GetStatus() != eReturnStatusFailed)
1506 } else {
1507 // Not connected...
1508 result.AppendErrorWithFormatv("not connected to '{0}'",
1509 platform_sp->GetPluginName());
1510 }
1511 } else {
1512 // No args
1513 result.AppendError("one or more process id(s) must be specified");
1514 }
1515 } else {
1516 result.AppendError("no platform is currently selected");
1517 }
1518 }
1519};
1520
1521#define LLDB_OPTIONS_platform_process_attach
1522#include "CommandOptions.inc"
1523
1525public:
1527 : CommandObjectParsed(interpreter, "platform process attach",
1528 "Attach to a process.",
1529 "platform process attach <cmd-options>"),
1530 m_class_options("scripted process", true, 'C', 'k', 'v', 0) {
1531 m_all_options.Append(&m_options);
1534 m_all_options.Finalize();
1535 }
1536
1538
1539 void DoExecute(Args &command, CommandReturnObject &result) override {
1540 PlatformSP platform_sp(
1541 GetDebugger().GetPlatformList().GetSelectedPlatform());
1542 if (platform_sp) {
1543
1544 if (!m_class_options.GetName().empty()) {
1545 m_options.attach_info.SetProcessPluginName("ScriptedProcess");
1546 ScriptedMetadataSP metadata_sp = std::make_shared<ScriptedMetadata>(
1547 m_class_options.GetName(), m_class_options.GetStructuredData());
1548 m_options.attach_info.SetScriptedMetadata(metadata_sp);
1549 }
1550
1551 Status err;
1552 ProcessSP remote_process_sp = platform_sp->Attach(
1553 m_options.attach_info, GetDebugger(), nullptr, err);
1554 if (err.Fail()) {
1555 result.AppendError(err.AsCString());
1556 } else if (!remote_process_sp) {
1557 result.AppendError("could not attach: unknown reason");
1558 } else
1560 } else {
1561 result.AppendError("no platform is currently selected");
1562 }
1563 }
1564
1565 Options *GetOptions() override { return &m_all_options; }
1566
1567protected:
1571};
1572
1574public:
1575 // Constructors and Destructors
1577 : CommandObjectMultiword(interpreter, "platform process",
1578 "Commands to query, launch and attach to "
1579 "processes on the current platform.",
1580 "platform process [attach|launch|list] ...") {
1582 "attach",
1585 "launch",
1588 interpreter)));
1590 interpreter)));
1591 }
1592
1593 ~CommandObjectPlatformProcess() override = default;
1594
1595private:
1596 // For CommandObjectPlatform only
1600};
1601
1602// "platform shell"
1603#define LLDB_OPTIONS_platform_shell
1604#include "CommandOptions.inc"
1605
1607public:
1608 class CommandOptions : public Options {
1609 public:
1610 CommandOptions() = default;
1611
1612 ~CommandOptions() override = default;
1613
1614 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1615 return llvm::ArrayRef(g_platform_shell_options);
1616 }
1617
1618 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1619 ExecutionContext *execution_context) override {
1620 Status error;
1621
1622 const char short_option = (char)GetDefinitions()[option_idx].short_option;
1623
1624 switch (short_option) {
1625 case 'h':
1626 m_use_host_platform = true;
1627 break;
1628 case 't':
1629 uint32_t timeout_sec;
1630 if (option_arg.getAsInteger(10, timeout_sec))
1632 "could not convert \"%s\" to a numeric value.",
1633 option_arg.str().c_str());
1634 else
1635 m_timeout = std::chrono::seconds(timeout_sec);
1636 break;
1637 case 's': {
1638 if (option_arg.empty()) {
1640 "missing shell interpreter path for option -i|--interpreter.");
1641 return error;
1642 }
1643
1644 m_shell_interpreter = option_arg.str();
1645 break;
1646 }
1647 default:
1648 llvm_unreachable("Unimplemented option");
1649 }
1650
1651 return error;
1652 }
1653
1654 void OptionParsingStarting(ExecutionContext *execution_context) override {
1655 m_timeout.reset();
1656 m_use_host_platform = false;
1657 m_shell_interpreter.clear();
1658 }
1659
1660 Timeout<std::micro> m_timeout = std::chrono::seconds(10);
1663 };
1664
1666 : CommandObjectRaw(interpreter, "platform shell",
1667 "Run a shell command on the current platform.",
1668 "platform shell <shell-command>", 0) {
1670 }
1671
1672 ~CommandObjectPlatformShell() override = default;
1673
1674 Options *GetOptions() override { return &m_options; }
1675
1676 void DoExecute(llvm::StringRef raw_command_line,
1677 CommandReturnObject &result) override {
1679 m_options.NotifyOptionParsingStarting(&exe_ctx);
1680
1681 // Print out an usage syntax on an empty command line.
1682 if (raw_command_line.empty()) {
1683 result.GetOutputStream().Printf("%s\n", this->GetSyntax().str().c_str());
1684 return;
1685 }
1686
1687 const bool is_alias = !raw_command_line.contains("platform");
1688 OptionsWithRaw args(raw_command_line);
1689
1690 if (args.HasArgs())
1691 if (!ParseOptions(args.GetArgs(), result))
1692 return;
1693
1694 if (args.GetRawPart().empty()) {
1695 result.GetOutputStream().Printf("%s <shell-command>\n",
1696 is_alias ? "shell" : "platform shell");
1697 return;
1698 }
1699
1700 llvm::StringRef cmd = args.GetRawPart();
1701
1702 PlatformSP platform_sp(
1703 m_options.m_use_host_platform
1705 : GetDebugger().GetPlatformList().GetSelectedPlatform());
1706 Status error;
1707 if (platform_sp) {
1708 FileSpec working_dir{};
1709 std::string output;
1710 int status = -1;
1711 int signo = -1;
1712 error = (platform_sp->RunShellCommand(
1713 m_options.m_shell_interpreter, cmd, working_dir, &status, &signo,
1714 &output, nullptr, m_options.m_timeout));
1715 if (!output.empty())
1716 result.GetOutputStream().PutCString(output);
1717 if (status > 0) {
1718 if (signo > 0) {
1719 const char *signo_cstr = Host::GetSignalAsCString(signo);
1720 if (signo_cstr)
1721 result.GetOutputStream().Printf(
1722 "error: command returned with status %i and signal %s\n",
1723 status, signo_cstr);
1724 else
1725 result.GetOutputStream().Printf(
1726 "error: command returned with status %i and signal %i\n",
1727 status, signo);
1728 } else
1729 result.GetOutputStream().Printf(
1730 "error: command returned with status %i\n", status);
1731 }
1732 } else {
1733 result.GetOutputStream().Printf(
1734 "error: cannot run remote shell commands without a platform\n");
1736 "error: cannot run remote shell commands without a platform");
1737 }
1738
1739 if (error.Fail()) {
1740 result.AppendError(error.AsCString());
1741 } else {
1743 }
1744 }
1745
1747};
1748
1749// "platform install" - install a target to a remote end
1751public:
1754 interpreter, "platform target-install",
1755 "Install a target (bundle or executable file) to the remote end.",
1756 "platform target-install <local-thing> <remote-sandbox>", 0) {
1759 m_arguments.push_back({local_arg});
1760 m_arguments.push_back({remote_arg});
1761 }
1762
1763 ~CommandObjectPlatformInstall() override = default;
1764
1765 void
1767 OptionElementVector &opt_element_vector) override {
1768 if (request.GetCursorIndex())
1769 return;
1772 }
1773
1774 void DoExecute(Args &args, CommandReturnObject &result) override {
1775 if (args.GetArgumentCount() != 2) {
1776 result.AppendError("platform target-install takes two arguments");
1777 return;
1778 }
1779 // TODO: move the bulk of this code over to the platform itself
1780 FileSpec src(args.GetArgumentAtIndex(0));
1782 FileSpec dst(args.GetArgumentAtIndex(1));
1783 if (!FileSystem::Instance().Exists(src)) {
1784 result.AppendError("source location does not exist or is not accessible");
1785 return;
1786 }
1787 PlatformSP platform_sp(
1788 GetDebugger().GetPlatformList().GetSelectedPlatform());
1789 if (!platform_sp) {
1790 result.AppendError("no platform currently selected");
1791 return;
1792 }
1793
1794 Status error = platform_sp->Install(src, dst);
1795 if (error.Success()) {
1797 } else {
1798 result.AppendErrorWithFormat("install failed: %s", error.AsCString());
1799 }
1800 }
1801};
1802
1805 interpreter, "platform", "Commands to manage and create platforms.",
1806 "platform [connect|disconnect|info|list|status|select] ...") {
1807 LoadSubCommand("select",
1809 LoadSubCommand("list",
1810 CommandObjectSP(new CommandObjectPlatformList(interpreter)));
1811 LoadSubCommand("status",
1814 new CommandObjectPlatformConnect(interpreter)));
1816 "disconnect",
1819 interpreter)));
1820 LoadSubCommand("mkdir",
1822 LoadSubCommand("file",
1823 CommandObjectSP(new CommandObjectPlatformFile(interpreter)));
1824 LoadSubCommand("file-exists",
1827 interpreter)));
1828 LoadSubCommand("get-permissions",
1831 interpreter)));
1833 interpreter)));
1835 new CommandObjectPlatformProcess(interpreter)));
1836 LoadSubCommand("shell",
1839 "target-install",
1841}
1842
static mode_t ParsePermissionString(const char *)=delete
static PosixPlatformCommandOptionValidator posix_validator
static llvm::raw_ostream & error(Stream &strm)
~CommandObjectPlatformConnect() override=default
CommandObjectPlatformConnect(CommandInterpreter &interpreter)
void DoExecute(Args &args, CommandReturnObject &result) override
~CommandObjectPlatformDisconnect() override=default
CommandObjectPlatformDisconnect(CommandInterpreter &interpreter)
void DoExecute(Args &args, CommandReturnObject &result) override
CommandObjectPlatformFClose(CommandInterpreter &interpreter)
~CommandObjectPlatformFClose() override=default
void DoExecute(Args &args, CommandReturnObject &result) override
~CommandObjectPlatformFOpen() override=default
void DoExecute(Args &args, CommandReturnObject &result) override
CommandObjectPlatformFOpen(CommandInterpreter &interpreter)
void OptionParsingStarting(ExecutionContext *execution_context) override
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
CommandObjectPlatformFRead(CommandInterpreter &interpreter)
~CommandObjectPlatformFRead() override=default
void DoExecute(Args &args, CommandReturnObject &result) override
void OptionParsingStarting(ExecutionContext *execution_context) override
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
~CommandObjectPlatformFWrite() override=default
CommandObjectPlatformFWrite(CommandInterpreter &interpreter)
void DoExecute(Args &args, CommandReturnObject &result) override
void DoExecute(Args &args, CommandReturnObject &result) override
CommandObjectPlatformFileExists(CommandInterpreter &interpreter)
~CommandObjectPlatformFileExists() override=default
CommandObjectPlatformFile(CommandInterpreter &interpreter)
const CommandObjectPlatformFile & operator=(const CommandObjectPlatformFile &)=delete
CommandObjectPlatformFile(const CommandObjectPlatformFile &)=delete
~CommandObjectPlatformFile() override=default
void DoExecute(Args &args, CommandReturnObject &result) override
~CommandObjectPlatformGetFile() override=default
CommandObjectPlatformGetFile(CommandInterpreter &interpreter)
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
~CommandObjectPlatformGetPermissions() override=default
CommandObjectPlatformGetPermissions(CommandInterpreter &interpreter)
void DoExecute(Args &args, CommandReturnObject &result) override
CommandObjectPlatformGetSize(CommandInterpreter &interpreter)
void DoExecute(Args &args, CommandReturnObject &result) override
~CommandObjectPlatformGetSize() override=default
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
~CommandObjectPlatformInstall() override=default
CommandObjectPlatformInstall(CommandInterpreter &interpreter)
void DoExecute(Args &args, CommandReturnObject &result) override
void DoExecute(Args &args, CommandReturnObject &result) override
CommandObjectPlatformList(CommandInterpreter &interpreter)
~CommandObjectPlatformList() override=default
void DoExecute(Args &args, CommandReturnObject &result) override
CommandObjectPlatformMkDir(CommandInterpreter &interpreter)
~CommandObjectPlatformMkDir() override=default
void DoExecute(Args &command, CommandReturnObject &result) override
CommandObjectPlatformProcessAttach(CommandInterpreter &interpreter)
~CommandObjectPlatformProcessAttach() override=default
OptionGroupPythonClassWithDict m_class_options
~CommandObjectPlatformProcessInfo() override=default
void DoExecute(Args &args, CommandReturnObject &result) override
CommandObjectPlatformProcessInfo(CommandInterpreter &interpreter)
void DoExecute(Args &args, CommandReturnObject &result) override
~CommandObjectPlatformProcessLaunch() override=default
OptionGroupPythonClassWithDict m_class_options
CommandObjectPlatformProcessLaunch(CommandInterpreter &interpreter)
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
void OptionParsingStarting(ExecutionContext *execution_context) override
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
~CommandObjectPlatformProcessList() override=default
void DoExecute(Args &args, CommandReturnObject &result) override
CommandObjectPlatformProcessList(CommandInterpreter &interpreter)
const CommandObjectPlatformProcess & operator=(const CommandObjectPlatformProcess &)=delete
CommandObjectPlatformProcess(const CommandObjectPlatformProcess &)=delete
~CommandObjectPlatformProcess() override=default
CommandObjectPlatformProcess(CommandInterpreter &interpreter)
void DoExecute(Args &args, CommandReturnObject &result) override
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
CommandObjectPlatformPutFile(CommandInterpreter &interpreter)
~CommandObjectPlatformPutFile() override=default
~CommandObjectPlatformSelect() override=default
void DoExecute(Args &args, CommandReturnObject &result) override
void HandleCompletion(CompletionRequest &request) override
This default version handles calling option argument completions and then calls HandleArgumentComplet...
CommandObjectPlatformSelect(CommandInterpreter &interpreter)
CommandObjectPlatformSettings(CommandInterpreter &interpreter)
void DoExecute(Args &args, CommandReturnObject &result) override
~CommandObjectPlatformSettings() override=default
void OptionParsingStarting(ExecutionContext *execution_context) override
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
void DoExecute(llvm::StringRef raw_command_line, CommandReturnObject &result) override
~CommandObjectPlatformShell() override=default
CommandObjectPlatformShell(CommandInterpreter &interpreter)
~CommandObjectPlatformStatus() override=default
void DoExecute(Args &args, CommandReturnObject &result) override
CommandObjectPlatformStatus(CommandInterpreter &interpreter)
const OptionPermissions & operator=(const OptionPermissions &)=delete
void OptionParsingStarting(ExecutionContext *execution_context) override
OptionPermissions()=default
lldb_private::Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
~OptionPermissions() override=default
OptionPermissions(const OptionPermissions &)=delete
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
An architecture specification class.
Definition ArchSpec.h:32
A command line argument class.
Definition Args.h:33
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition Args.h:120
llvm::ArrayRef< ArgEntry > entries() const
Definition Args.h:132
const char * GetArgumentAtIndex(size_t idx) const
Gets the NULL terminated C string argument pointer for the argument at index idx.
Definition Args.cpp:273
bool GetCommandString(std::string &command) const
Definition Args.cpp:215
static bool InvokeCommonCompletionCallbacks(CommandInterpreter &interpreter, uint32_t completion_mask, lldb_private::CompletionRequest &request, SearchFilter *searcher)
static void PlatformPluginNames(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
ExecutionContext GetExecutionContext(bool adopt_dummy_target=true) const
Returns the execution context the interpreter should run a command in.
bool LoadSubCommand(llvm::StringRef cmd_name, const lldb::CommandObjectSP &command_obj) override
CommandObjectMultiword(CommandInterpreter &interpreter, const char *name, const char *help=nullptr, const char *syntax=nullptr, uint32_t flags=0)
CommandObjectParsed(CommandInterpreter &interpreter, const char *name, const char *help=nullptr, const char *syntax=nullptr, uint32_t flags=0)
CommandObjectPlatform(CommandInterpreter &interpreter)
CommandObjectRaw(CommandInterpreter &interpreter, llvm::StringRef name, llvm::StringRef help="", llvm::StringRef syntax="", uint32_t flags=0)
std::vector< CommandArgumentData > CommandArgumentEntry
virtual void SetHelpLong(llvm::StringRef str)
void AddSimpleArgumentList(lldb::CommandArgumentType arg_type, ArgumentRepetitionType repetition_type=eArgRepeatPlain)
std::vector< CommandArgumentEntry > m_arguments
CommandInterpreter & GetCommandInterpreter()
CommandInterpreter & m_interpreter
bool ParseOptions(Args &args, CommandReturnObject &result)
Target * GetTarget()
Get the target this command should operate on.
virtual llvm::StringRef GetSyntax()
void AppendError(llvm::StringRef in_string)
void SetStatus(lldb::ReturnStatus status)
void AppendErrorWithFormat(const char *format,...) __attribute__((format(printf
void void AppendMessageWithFormatv(const char *format, Args &&...args)
void AppendErrorWithFormatv(const char *format, Args &&...args)
"lldb/Utility/ArgCompletionRequest.h"
A class to manage flag bits.
Definition Debugger.h:100
CommandInterpreter & GetCommandInterpreter()
Definition Debugger.h:182
PlatformList & GetPlatformList()
Definition Debugger.h:222
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
const lldb::TargetSP & GetTargetSP() const
Get accessor to get the target shared pointer.
A file utility class.
Definition FileSpec.h:57
static FileSystem & Instance()
void Resolve(llvm::SmallVectorImpl< char > &path, bool force_make_absolute=false)
Resolve path to make it canonical.
static const char * GetSignalAsCString(int signo)
A class that describes an executable image and its associated object and symbol files.
Definition Module.h:91
const ArchSpec & GetArchitecture() const
Get const accessor for the module architecture.
Definition Module.cpp:1019
const FileSpec & GetFileSpec() const
Get const accessor for the module file specification.
Definition Module.h:447
A pair of an option list with a 'raw' string as a suffix.
Definition Args.h:319
bool HasArgs() const
Returns true if there are any arguments before the raw suffix.
Definition Args.h:330
Args & GetArgs()
Returns the list of arguments.
Definition Args.h:335
const std::string & GetRawPart() const
Returns the raw suffix part of the parsed string.
Definition Args.h:368
A command line option parsing protocol class.
Definition Options.h:58
std::vector< Option > m_getopt_table
Definition Options.h:198
lldb::PlatformSP GetSelectedPlatform()
Select the active platform.
Definition Platform.h:1185
void SetSelectedPlatform(const lldb::PlatformSP &platform_sp)
Definition Platform.h:1193
static ArchSpec GetAugmentedArchSpec(Platform *platform, llvm::StringRef triple)
Augments the triple either with information from platform or the host system (if platform is null).
Definition Platform.cpp:321
static lldb::PlatformSP GetHostPlatform()
Get the native host platform plug-in.
Definition Platform.cpp:139
static llvm::StringRef GetPlatformPluginDescriptionAtIndex(uint32_t idx)
static llvm::StringRef GetPlatformPluginNameAtIndex(uint32_t idx)
static void DumpTableHeader(Stream &s, bool show_args, bool verbose)
void Dump(Stream &s, UserIDResolver &resolver) const
void DumpAsTableRow(Stream &s, UserIDResolver &resolver, bool show_args, bool verbose) const
An error handling class.
Definition Status.h:118
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
bool Fail() const
Test for error condition.
Definition Status.cpp:293
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition Status.cpp:194
A stream class that can stream formatted output to a file.
Definition Stream.h:28
void Format(const char *format, Args &&... args)
Forwards the arguments to llvm::formatv and writes to the stream.
Definition Stream.h:370
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
size_t PutChar(char ch)
Definition Stream.cpp:131
size_t EOL()
Output and End of Line character to the stream.
Definition Stream.cpp:155
void SetProcessLaunchInfo(const ProcessLaunchInfo &launch_info)
Definition Target.cpp:5742
bool GetRunArguments(Args &args) const
Definition Target.cpp:5315
Module * GetExecutableModulePointer()
Definition Target.cpp:1609
lldb::PlatformSP GetPlatform()
Definition Target.h:1969
#define LLDB_OPT_SET_1
#define UINT64_MAX
#define LLDB_OPT_SET_2
#define LLDB_OPT_SET_ALL
#define UINT32_MAX
#define LLDB_INVALID_PROCESS_ID
A class that represents a running process on the host machine.
std::vector< OptionArgElement > OptionElementVector
Definition Options.h:43
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
Definition State.cpp:14
std::vector< ProcessInstanceInfo > ProcessInstanceInfoList
Definition Host.h:32
@ eRemoteDiskDirectoryCompletion
@ eRemoteDiskFileCompletion
std::shared_ptr< lldb_private::ScriptedMetadata > ScriptedMetadataSP
std::shared_ptr< lldb_private::CommandObject > CommandObjectSP
std::shared_ptr< lldb_private::Platform > PlatformSP
StateType
Process and Thread States.
@ eStateStopped
Process or thread is stopped and can be examined.
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::Debugger > DebuggerSP
std::shared_ptr< lldb_private::Event > EventSP
@ eReturnStatusFailed
@ eReturnStatusSuccessFinishResult
@ eReturnStatusSuccessFinishNoResult
uint64_t pid_t
Definition lldb-types.h:83
@ eArgTypeRemoteFilename
@ eArgTypeUnsignedInteger
uint64_t user_id_t
Definition lldb-types.h:82
std::shared_ptr< lldb_private::Target > TargetSP
Used to build individual command argument lists.