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 = GetDebugger().GetSelectedTarget().get();
254 PlatformSP platform_sp;
255 if (target) {
256 platform_sp = target->GetPlatform();
257 }
258 if (!platform_sp) {
260 }
261 if (platform_sp) {
262 platform_sp->GetStatus(ostrm);
264 } else {
265 result.AppendError("no platform is currently selected\n");
266 }
267 }
268};
269
270// "platform connect <connect-url>"
272public:
275 interpreter, "platform connect",
276 "Select the current platform by providing a connection URL.",
277 "platform connect <connect-url>", 0) {
279 }
280
281 ~CommandObjectPlatformConnect() override = default;
282
283protected:
284 void DoExecute(Args &args, CommandReturnObject &result) override {
285 Stream &ostrm = result.GetOutputStream();
286
287 PlatformSP platform_sp(
288 GetDebugger().GetPlatformList().GetSelectedPlatform());
289 if (platform_sp) {
290 Status error(platform_sp->ConnectRemote(args));
291 if (error.Success()) {
292 platform_sp->GetStatus(ostrm);
294
295 platform_sp->ConnectToWaitingProcesses(GetDebugger(), error);
296 if (error.Fail()) {
297 result.AppendError(error.AsCString());
298 }
299 } else {
300 result.AppendErrorWithFormat("%s\n", error.AsCString());
301 }
302 } else {
303 result.AppendError("no platform is currently selected\n");
304 }
305 }
306
307 Options *GetOptions() override {
308 PlatformSP platform_sp(
309 GetDebugger().GetPlatformList().GetSelectedPlatform());
310 OptionGroupOptions *m_platform_options = nullptr;
311 if (platform_sp) {
312 m_platform_options = platform_sp->GetConnectionOptions(m_interpreter);
313 if (m_platform_options != nullptr && !m_platform_options->m_did_finalize)
314 m_platform_options->Finalize();
315 }
316 return m_platform_options;
317 }
318};
319
320// "platform disconnect"
322public:
324 : CommandObjectParsed(interpreter, "platform disconnect",
325 "Disconnect from the current platform.",
326 "platform disconnect", 0) {}
327
329
330protected:
331 void DoExecute(Args &args, CommandReturnObject &result) override {
332 PlatformSP platform_sp(
333 GetDebugger().GetPlatformList().GetSelectedPlatform());
334 if (platform_sp) {
335 if (args.GetArgumentCount() == 0) {
337
338 if (platform_sp->IsConnected()) {
339 // Cache the instance name if there is one since we are about to
340 // disconnect and the name might go with it.
341 const char *hostname_cstr = platform_sp->GetHostname();
342 std::string hostname;
343 if (hostname_cstr)
344 hostname.assign(hostname_cstr);
345
346 error = platform_sp->DisconnectRemote();
347 if (error.Success()) {
348 Stream &ostrm = result.GetOutputStream();
349 if (hostname.empty())
350 ostrm.Format("Disconnected from \"{0}\"\n",
351 platform_sp->GetPluginName());
352 else
353 ostrm.Printf("Disconnected from \"%s\"\n", hostname.c_str());
355 } else {
356 result.AppendErrorWithFormat("%s", error.AsCString());
357 }
358 } else {
359 // Not connected...
360 result.AppendErrorWithFormatv("not connected to '{0}'",
361 platform_sp->GetPluginName());
362 }
363 } else {
364 // Bad args
365 result.AppendError(
366 "\"platform disconnect\" doesn't take any arguments");
367 }
368 } else {
369 result.AppendError("no platform is currently selected");
370 }
371 }
372};
373
374// "platform settings"
376public:
378 : CommandObjectParsed(interpreter, "platform settings",
379 "Set settings for the current target's platform.",
380 "platform settings", 0),
381 m_option_working_dir(LLDB_OPT_SET_1, false, "working-dir", 'w',
383 "The working directory for the platform.") {
385 }
386
387 ~CommandObjectPlatformSettings() override = default;
388
389protected:
390 void DoExecute(Args &args, CommandReturnObject &result) override {
391 PlatformSP platform_sp(
392 GetDebugger().GetPlatformList().GetSelectedPlatform());
393 if (platform_sp) {
394 if (m_option_working_dir.GetOptionValue().OptionWasSet())
395 platform_sp->SetWorkingDirectory(
396 m_option_working_dir.GetOptionValue().GetCurrentValue());
397 } else {
398 result.AppendError("no platform is currently selected");
399 }
400 }
401
402 Options *GetOptions() override {
403 if (!m_options.DidFinalize())
404 m_options.Finalize();
405 return &m_options;
406 }
407
410};
411
412// "platform mkdir"
414public:
416 : CommandObjectParsed(interpreter, "platform mkdir",
417 "Make a new directory on the remote end.", nullptr,
418 0) {
420 }
421
422 ~CommandObjectPlatformMkDir() override = default;
423
424 void DoExecute(Args &args, CommandReturnObject &result) override {
425 PlatformSP platform_sp(
426 GetDebugger().GetPlatformList().GetSelectedPlatform());
427 if (platform_sp) {
428 std::string cmd_line;
429 args.GetCommandString(cmd_line);
430 uint32_t mode;
431 const OptionPermissions *options_permissions =
432 (const OptionPermissions *)m_options.GetGroupWithOption('r');
433 if (options_permissions)
434 mode = options_permissions->m_permissions;
435 else
436 mode = lldb::eFilePermissionsUserRWX | lldb::eFilePermissionsGroupRWX |
437 lldb::eFilePermissionsWorldRX;
438 Status error = platform_sp->MakeDirectory(FileSpec(cmd_line), mode);
439 if (error.Success()) {
441 } else {
442 result.AppendError(error.AsCString());
443 }
444 } else {
445 result.AppendError("no platform currently selected\n");
446 }
447 }
448
449 Options *GetOptions() override {
450 if (!m_options.DidFinalize()) {
452 m_options.Finalize();
453 }
454 return &m_options;
455 }
456
459};
460
461// "platform fopen"
463public:
465 : CommandObjectParsed(interpreter, "platform file open",
466 "Open a file on the remote end.", nullptr, 0) {
468 }
469
470 ~CommandObjectPlatformFOpen() override = default;
471
472 void DoExecute(Args &args, CommandReturnObject &result) override {
473 PlatformSP platform_sp(
474 GetDebugger().GetPlatformList().GetSelectedPlatform());
475 if (platform_sp) {
477 std::string cmd_line;
478 args.GetCommandString(cmd_line);
479 mode_t perms;
480 const OptionPermissions *options_permissions =
481 (const OptionPermissions *)m_options.GetGroupWithOption('r');
482 if (options_permissions)
483 perms = options_permissions->m_permissions;
484 else
485 perms = lldb::eFilePermissionsUserRW | lldb::eFilePermissionsGroupRW |
486 lldb::eFilePermissionsWorldRead;
487 lldb::user_id_t fd = platform_sp->OpenFile(
488 FileSpec(cmd_line),
490 perms, error);
491 if (error.Success()) {
492 result.AppendMessageWithFormatv("File Descriptor = {0}", fd);
494 } else {
495 result.AppendError(error.AsCString());
496 }
497 } else {
498 result.AppendError("no platform currently selected\n");
499 }
500 }
501
502 Options *GetOptions() override {
503 if (!m_options.DidFinalize()) {
505 m_options.Finalize();
506 }
507 return &m_options;
508 }
509
512};
513
514// "platform fclose"
516public:
518 : CommandObjectParsed(interpreter, "platform file close",
519 "Close a file on the remote end.", nullptr, 0) {
521 }
522
523 ~CommandObjectPlatformFClose() override = default;
524
525 void DoExecute(Args &args, CommandReturnObject &result) override {
526 PlatformSP platform_sp(
527 GetDebugger().GetPlatformList().GetSelectedPlatform());
528 if (platform_sp) {
529 std::string cmd_line;
530 args.GetCommandString(cmd_line);
532 if (!llvm::to_integer(cmd_line, fd)) {
533 result.AppendErrorWithFormatv("'{0}' is not a valid file descriptor.\n",
534 cmd_line);
535 return;
536 }
538 bool success = platform_sp->CloseFile(fd, error);
539 if (success) {
540 result.AppendMessageWithFormatv("file {0} closed.", fd);
542 } else {
543 result.AppendError(error.AsCString());
544 }
545 } else {
546 result.AppendError("no platform currently selected\n");
547 }
548 }
549};
550
551// "platform fread"
552
553#define LLDB_OPTIONS_platform_fread
554#include "CommandOptions.inc"
555
557public:
559 : CommandObjectParsed(interpreter, "platform file read",
560 "Read data from a file on the remote end.", nullptr,
561 0) {
563 }
564
565 ~CommandObjectPlatformFRead() override = default;
566
567 void DoExecute(Args &args, CommandReturnObject &result) override {
568 PlatformSP platform_sp(
569 GetDebugger().GetPlatformList().GetSelectedPlatform());
570 if (platform_sp) {
571 std::string cmd_line;
572 args.GetCommandString(cmd_line);
574 if (!llvm::to_integer(cmd_line, fd)) {
575 result.AppendErrorWithFormatv("'{0}' is not a valid file descriptor.\n",
576 cmd_line);
577 return;
578 }
579 std::string buffer(m_options.m_count, 0);
581 uint64_t retcode = platform_sp->ReadFile(
582 fd, m_options.m_offset, &buffer[0], m_options.m_count, error);
583 if (retcode != UINT64_MAX) {
584 result.AppendMessageWithFormatv("Return = {0}", retcode);
585 result.AppendMessageWithFormatv("Data = \"{0}\"", buffer.c_str());
587 } else {
588 result.AppendError(error.AsCString());
589 }
590 } else {
591 result.AppendError("no platform currently selected\n");
592 }
593 }
594
595 Options *GetOptions() override { return &m_options; }
596
597protected:
598 class CommandOptions : public Options {
599 public:
600 CommandOptions() = default;
601
602 ~CommandOptions() override = default;
603
604 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
605 ExecutionContext *execution_context) override {
607 char short_option = (char)m_getopt_table[option_idx].val;
608
609 switch (short_option) {
610 case 'o':
611 if (option_arg.getAsInteger(0, m_offset))
612 error = Status::FromErrorStringWithFormat("invalid offset: '%s'",
613 option_arg.str().c_str());
614 break;
615 case 'c':
616 if (option_arg.getAsInteger(0, m_count))
617 error = Status::FromErrorStringWithFormat("invalid offset: '%s'",
618 option_arg.str().c_str());
619 break;
620 default:
621 llvm_unreachable("Unimplemented option");
622 }
623
624 return error;
625 }
626
627 void OptionParsingStarting(ExecutionContext *execution_context) override {
628 m_offset = 0;
629 m_count = 1;
630 }
631
632 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
633 return llvm::ArrayRef(g_platform_fread_options);
634 }
635
636 // Instance variables to hold the values for command options.
637
638 uint32_t m_offset;
639 uint32_t m_count;
640 };
641
643};
644
645// "platform fwrite"
646
647#define LLDB_OPTIONS_platform_fwrite
648#include "CommandOptions.inc"
649
651public:
653 : CommandObjectParsed(interpreter, "platform file write",
654 "Write data to a file on the remote end.", nullptr,
655 0) {
657 }
658
659 ~CommandObjectPlatformFWrite() override = default;
660
661 void DoExecute(Args &args, CommandReturnObject &result) override {
662 PlatformSP platform_sp(
663 GetDebugger().GetPlatformList().GetSelectedPlatform());
664 if (platform_sp) {
665 std::string cmd_line;
666 args.GetCommandString(cmd_line);
669 if (!llvm::to_integer(cmd_line, fd)) {
670 result.AppendErrorWithFormatv("'{0}' is not a valid file descriptor.",
671 cmd_line);
672 return;
673 }
674 uint64_t retcode =
675 platform_sp->WriteFile(fd, m_options.m_offset, &m_options.m_data[0],
676 m_options.m_data.size(), error);
677 if (retcode != UINT64_MAX) {
678 result.AppendMessageWithFormatv("Return = {0}", retcode);
680 } else {
681 result.AppendError(error.AsCString());
682 }
683 } else {
684 result.AppendError("no platform currently selected\n");
685 }
686 }
687
688 Options *GetOptions() override { return &m_options; }
689
690protected:
691 class CommandOptions : public Options {
692 public:
693 CommandOptions() = default;
694
695 ~CommandOptions() override = default;
696
697 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
698 ExecutionContext *execution_context) override {
700 char short_option = (char)m_getopt_table[option_idx].val;
701
702 switch (short_option) {
703 case 'o':
704 if (option_arg.getAsInteger(0, m_offset))
705 error = Status::FromErrorStringWithFormat("invalid offset: '%s'",
706 option_arg.str().c_str());
707 break;
708 case 'd':
709 m_data.assign(std::string(option_arg));
710 break;
711 default:
712 llvm_unreachable("Unimplemented option");
713 }
714
715 return error;
716 }
717
718 void OptionParsingStarting(ExecutionContext *execution_context) override {
719 m_offset = 0;
720 m_data.clear();
721 }
722
723 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
724 return llvm::ArrayRef(g_platform_fwrite_options);
725 }
726
727 // Instance variables to hold the values for command options.
728
729 uint32_t m_offset;
730 std::string m_data;
731 };
732
734};
735
737public:
738 // Constructors and Destructors
741 interpreter, "platform file",
742 "Commands to access files on the current platform.",
743 "platform file [open|close|read|write] ...") {
745 "open", CommandObjectSP(new CommandObjectPlatformFOpen(interpreter)));
747 "close", CommandObjectSP(new CommandObjectPlatformFClose(interpreter)));
749 "read", CommandObjectSP(new CommandObjectPlatformFRead(interpreter)));
751 "write", CommandObjectSP(new CommandObjectPlatformFWrite(interpreter)));
752 }
753
754 ~CommandObjectPlatformFile() override = default;
755
756private:
757 // For CommandObjectPlatform only
761};
762
763// "platform get-file remote-file-path host-file-path"
765public:
768 interpreter, "platform get-file",
769 "Transfer a file from the remote end to the local host.",
770 "platform get-file <remote-file-spec> <local-file-spec>", 0) {
772 R"(Examples:
773
774(lldb) platform get-file /the/remote/file/path /the/local/file/path
775
776 Transfer a file from the remote end with file path /the/remote/file/path to the local host.)");
777
778 CommandArgumentEntry arg1, arg2;
779 CommandArgumentData file_arg_remote, file_arg_host;
780
781 // Define the first (and only) variant of this arg.
782 file_arg_remote.arg_type = eArgTypeRemoteFilename;
783 file_arg_remote.arg_repetition = eArgRepeatPlain;
784 // There is only one variant this argument could be; put it into the
785 // argument entry.
786 arg1.push_back(file_arg_remote);
787
788 // Define the second (and only) variant of this arg.
789 file_arg_host.arg_type = eArgTypeFilename;
790 file_arg_host.arg_repetition = eArgRepeatPlain;
791 // There is only one variant this argument could be; put it into the
792 // argument entry.
793 arg2.push_back(file_arg_host);
794
795 // Push the data for the first and the second arguments into the
796 // m_arguments vector.
797 m_arguments.push_back(arg1);
798 m_arguments.push_back(arg2);
799 }
801 ~CommandObjectPlatformGetFile() override = default;
802
803 void
805 OptionElementVector &opt_element_vector) override {
806 if (request.GetCursorIndex() == 0)
809 nullptr);
810 else if (request.GetCursorIndex() == 1)
813 }
814
815 void DoExecute(Args &args, CommandReturnObject &result) override {
816 // If the number of arguments is incorrect, issue an error message.
817 if (args.GetArgumentCount() != 2) {
818 result.AppendError("required arguments missing; specify both the "
819 "source and destination file paths");
820 return;
821 }
822
823 PlatformSP platform_sp(
824 GetDebugger().GetPlatformList().GetSelectedPlatform());
825 if (platform_sp) {
826 const char *remote_file_path = args.GetArgumentAtIndex(0);
827 const char *local_file_path = args.GetArgumentAtIndex(1);
828 Status error = platform_sp->GetFile(FileSpec(remote_file_path),
829 FileSpec(local_file_path));
830 if (error.Success()) {
832 "successfully get-file from {0} (remote) to {1} (host)",
833 remote_file_path, local_file_path);
835 } else {
836 result.AppendMessageWithFormatv("get-file failed: {0}",
837 error.AsCString());
838 }
839 } else {
840 result.AppendError("no platform currently selected\n");
841 }
842 }
843};
844
845// "platform get-size remote-file-path"
847public:
849 : CommandObjectParsed(interpreter, "platform get-size",
850 "Get the file size from the remote end.",
851 "platform get-size <remote-file-spec>", 0) {
853 R"(Examples:
854
855(lldb) platform get-size /the/remote/file/path
856
857 Get the file size from the remote end with path /the/remote/file/path.)");
861
862 ~CommandObjectPlatformGetSize() override = default;
863
864 void DoExecute(Args &args, CommandReturnObject &result) override {
865 // If the number of arguments is incorrect, issue an error message.
866 if (args.GetArgumentCount() != 1) {
867 result.AppendError("required argument missing; specify the source file "
868 "path as the only argument");
869 return;
870 }
871
872 PlatformSP platform_sp(
873 GetDebugger().GetPlatformList().GetSelectedPlatform());
874 if (platform_sp) {
875 std::string remote_file_path(args.GetArgumentAtIndex(0));
876 user_id_t size = platform_sp->GetFileSize(FileSpec(remote_file_path));
877 if (size != UINT64_MAX) {
878 result.AppendMessageWithFormatv("File size of {0} (remote): {1}",
879 remote_file_path.c_str(), size);
881 } else {
883 "Error getting file size of {0} (remote)",
884 remote_file_path.c_str());
885 }
886 } else {
887 result.AppendError("no platform currently selected\n");
888 }
889 }
890};
891
892// "platform get-permissions remote-file-path"
894public:
896 : CommandObjectParsed(interpreter, "platform get-permissions",
897 "Get the file permission bits from the remote end.",
898 "platform get-permissions <remote-file-spec>", 0) {
900 R"(Examples:
901
902(lldb) platform get-permissions /the/remote/file/path
903
904 Get the file permissions from the remote end with path /the/remote/file/path.)");
908
909 ~CommandObjectPlatformGetPermissions() override = default;
910
911 void 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");
916 return;
917 }
918
919 PlatformSP platform_sp(
920 GetDebugger().GetPlatformList().GetSelectedPlatform());
921 if (platform_sp) {
922 std::string remote_file_path(args.GetArgumentAtIndex(0));
923 uint32_t permissions;
924 Status error = platform_sp->GetFilePermissions(FileSpec(remote_file_path),
925 permissions);
926 if (error.Success()) {
928 "File permissions of {0} (remote): 0o{1}", remote_file_path,
929 llvm::format("%04o", permissions));
931 } else
932 result.AppendError(error.AsCString());
933 } else {
934 result.AppendError("no platform currently selected\n");
935 }
936 }
937};
938
939// "platform file-exists remote-file-path"
941public:
943 : CommandObjectParsed(interpreter, "platform file-exists",
944 "Check if the file exists on the remote end.",
945 "platform file-exists <remote-file-spec>", 0) {
947 R"(Examples:
948
949(lldb) platform file-exists /the/remote/file/path
950
951 Check if /the/remote/file/path exists on the remote end.)");
955
956 ~CommandObjectPlatformFileExists() override = default;
957
958 void DoExecute(Args &args, CommandReturnObject &result) override {
959 // If the number of arguments is incorrect, issue an error message.
960 if (args.GetArgumentCount() != 1) {
961 result.AppendError("required argument missing; specify the source file "
962 "path as the only argument");
963 return;
964 }
965
966 PlatformSP platform_sp(
967 GetDebugger().GetPlatformList().GetSelectedPlatform());
968 if (platform_sp) {
969 std::string remote_file_path(args.GetArgumentAtIndex(0));
970 bool exists = platform_sp->GetFileExists(FileSpec(remote_file_path));
971 result.AppendMessageWithFormatv("File {0} (remote) {1}",
972 remote_file_path.c_str(),
973 exists ? "exists" : "does not exist");
975 } else {
976 result.AppendError("no platform currently selected\n");
977 }
978 }
979};
980
981// "platform put-file"
983public:
986 interpreter, "platform put-file",
987 "Transfer a file from this system to the remote end.",
988 "platform put-file <source> [<destination>]", 0) {
990 R"(Examples:
991
992(lldb) platform put-file /source/foo.txt /destination/bar.txt
993
994(lldb) platform put-file /source/foo.txt
995
996 Relative source file paths are resolved against lldb's local working directory.
998 Omitting the destination places the file in the platform working directory.)");
1001 m_arguments.push_back({source_arg});
1002 m_arguments.push_back({path_arg});
1003 }
1004
1005 ~CommandObjectPlatformPutFile() override = default;
1006
1007 void
1008 HandleArgumentCompletion(CompletionRequest &request,
1009 OptionElementVector &opt_element_vector) override {
1010 if (request.GetCursorIndex() == 0)
1013 else if (request.GetCursorIndex() == 1)
1016 nullptr);
1017 }
1018
1019 void DoExecute(Args &args, CommandReturnObject &result) override {
1020 const char *src = args.GetArgumentAtIndex(0);
1021 const char *dst = args.GetArgumentAtIndex(1);
1022
1023 FileSpec src_fs(src);
1024 FileSystem::Instance().Resolve(src_fs);
1025 FileSpec dst_fs(dst ? dst : src_fs.GetFilename().GetCString());
1026
1027 PlatformSP platform_sp(
1028 GetDebugger().GetPlatformList().GetSelectedPlatform());
1029 if (platform_sp) {
1030 Status error(platform_sp->PutFile(src_fs, dst_fs));
1031 if (error.Success()) {
1033 } else {
1034 result.AppendError(error.AsCString());
1035 }
1036 } else {
1037 result.AppendError("no platform currently selected\n");
1038 }
1039 }
1040};
1041
1042// "platform process launch"
1044public:
1046 : CommandObjectParsed(interpreter, "platform process launch",
1047 "Launch a new process on a remote platform.",
1048 "platform process launch program",
1049 eCommandRequiresTarget | eCommandTryTargetAPILock),
1050 m_class_options("scripted process", true, 'C', 'k', 'v', 0) {
1051 m_all_options.Append(&m_options);
1054 m_all_options.Finalize();
1056 }
1057
1058 void
1060 OptionElementVector &opt_element_vector) override {
1061 // I didn't make a type for RemoteRunArgs, but since we're going to run
1062 // this on the remote system we should use the remote completer.
1065 nullptr);
1066 }
1067
1069
1070 Options *GetOptions() override { return &m_all_options; }
1071
1072protected:
1073 void DoExecute(Args &args, CommandReturnObject &result) override {
1074 Target *target = GetDebugger().GetSelectedTarget().get();
1075 PlatformSP platform_sp;
1076 if (target) {
1077 platform_sp = target->GetPlatform();
1078 }
1079 if (!platform_sp) {
1081 }
1082
1083 if (platform_sp) {
1084 Status error;
1085 const size_t argc = args.GetArgumentCount();
1086 Target *target = m_exe_ctx.GetTargetPtr();
1087 Module *exe_module = target->GetExecutableModulePointer();
1088 if (exe_module) {
1089 m_options.launch_info.GetExecutableFile() = exe_module->GetFileSpec();
1090 llvm::SmallString<128> exe_path;
1091 m_options.launch_info.GetExecutableFile().GetPath(exe_path);
1092 if (!exe_path.empty())
1093 m_options.launch_info.GetArguments().AppendArgument(exe_path);
1094 m_options.launch_info.GetArchitecture() = exe_module->GetArchitecture();
1095 }
1096
1097 if (!m_class_options.GetName().empty()) {
1098 m_options.launch_info.SetProcessPluginName("ScriptedProcess");
1099 ScriptedMetadataSP metadata_sp = std::make_shared<ScriptedMetadata>(
1100 m_class_options.GetName(), m_class_options.GetStructuredData());
1101 m_options.launch_info.SetScriptedMetadata(metadata_sp);
1102 target->SetProcessLaunchInfo(m_options.launch_info);
1103 }
1104
1105 if (argc > 0) {
1106 if (m_options.launch_info.GetExecutableFile()) {
1107 // We already have an executable file, so we will use this and all
1108 // arguments to this function are extra arguments
1109 m_options.launch_info.GetArguments().AppendArguments(args);
1110 } else {
1111 // We don't have any file yet, so the first argument is our
1112 // executable, and the rest are program arguments
1113 const bool first_arg_is_executable = true;
1114 m_options.launch_info.SetArguments(args, first_arg_is_executable);
1115 }
1116 }
1117
1118 if (m_options.launch_info.GetExecutableFile()) {
1119 Debugger &debugger = GetDebugger();
1120
1121 if (argc == 0) {
1122 // If no arguments were given to the command, use target.run-args.
1123 Args target_run_args;
1124 target->GetRunArguments(target_run_args);
1125 m_options.launch_info.GetArguments().AppendArguments(target_run_args);
1126 }
1127
1128 ProcessSP process_sp(platform_sp->DebugProcess(
1129 m_options.launch_info, debugger, *target, error));
1130
1131 if (!process_sp && error.Success()) {
1132 result.AppendError("failed to launch or debug process");
1133 return;
1134 } else if (!error.Success()) {
1135 result.AppendError(error.AsCString());
1136 return;
1137 }
1138
1139 const bool synchronous_execution =
1141 auto launch_info = m_options.launch_info;
1142 bool rebroadcast_first_stop =
1143 !synchronous_execution &&
1144 launch_info.GetFlags().Test(eLaunchFlagStopAtEntry);
1145
1146 EventSP first_stop_event_sp;
1147 StateType state = process_sp->WaitForProcessToStop(
1148 std::nullopt, &first_stop_event_sp, rebroadcast_first_stop,
1149 launch_info.GetHijackListener());
1150 process_sp->RestoreProcessEvents();
1151
1152 if (rebroadcast_first_stop) {
1153 assert(first_stop_event_sp);
1154 process_sp->BroadcastEvent(first_stop_event_sp);
1155 return;
1156 }
1157
1158 switch (state) {
1159 case eStateStopped: {
1160 if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
1161 break;
1162 if (synchronous_execution) {
1163 // Now we have handled the stop-from-attach, and we are just
1164 // switching to a synchronous resume. So we should switch to the
1165 // SyncResume hijacker.
1166 process_sp->ResumeSynchronous(&result.GetOutputStream());
1167 } else {
1168 error = process_sp->Resume();
1169 if (!error.Success()) {
1170 result.AppendErrorWithFormat(
1171 "process resume at entry point failed: %s",
1172 error.AsCString());
1173 }
1174 }
1175 } break;
1176 default:
1177 result.AppendErrorWithFormat(
1178 "initial process state wasn't stopped: %s",
1179 StateAsCString(state));
1180 break;
1181 }
1182
1183 if (process_sp && process_sp->IsAlive()) {
1185 return;
1186 }
1187 } else {
1188 result.AppendError("'platform process launch' uses the current target "
1189 "file and arguments, or the executable and its "
1190 "arguments can be specified in this command");
1191 return;
1192 }
1193 } else {
1194 result.AppendError("no platform is selected\n");
1195 }
1196 }
1197
1201};
1202
1203// "platform process list"
1204
1206#define LLDB_OPTIONS_platform_process_list
1207#include "CommandOptions.inc"
1208
1210public:
1212 : CommandObjectParsed(interpreter, "platform process list",
1213 "List processes on a remote platform by name, pid, "
1214 "or many other matching attributes.",
1215 "platform process list", 0) {}
1216
1218
1219 Options *GetOptions() override { return &m_options; }
1220
1221protected:
1222 void DoExecute(Args &args, CommandReturnObject &result) override {
1223 Target *target = GetDebugger().GetSelectedTarget().get();
1224 PlatformSP platform_sp;
1225 if (target) {
1226 platform_sp = target->GetPlatform();
1227 }
1228 if (!platform_sp) {
1230 }
1231
1232 if (platform_sp) {
1233 Stream &ostrm = result.GetOutputStream();
1234
1235 lldb::pid_t pid = m_options.match_info.GetProcessInfo().GetProcessID();
1236 if (pid != LLDB_INVALID_PROCESS_ID) {
1237 ProcessInstanceInfo proc_info;
1238 if (platform_sp->GetProcessInfo(pid, proc_info)) {
1240 m_options.verbose);
1241 proc_info.DumpAsTableRow(ostrm, platform_sp->GetUserIDResolver(),
1242 m_options.show_args, m_options.verbose);
1244 } else {
1245 result.AppendErrorWithFormat(
1246 "no process found with pid = %" PRIu64 "\n", pid);
1247 }
1248 } else {
1249 ProcessInstanceInfoList proc_infos;
1250 const uint32_t matches =
1251 platform_sp->FindProcesses(m_options.match_info, proc_infos);
1252 const char *match_desc = nullptr;
1253 const char *match_name =
1254 m_options.match_info.GetProcessInfo().GetName();
1255 if (match_name && match_name[0]) {
1256 switch (m_options.match_info.GetNameMatchType()) {
1257 case NameMatch::Ignore:
1258 break;
1259 case NameMatch::Equals:
1260 match_desc = "matched";
1261 break;
1263 match_desc = "contained";
1264 break;
1266 match_desc = "started with";
1267 break;
1269 match_desc = "ended with";
1270 break;
1272 match_desc = "matched the regular expression";
1273 break;
1274 }
1275 }
1276
1277 if (matches == 0) {
1278 if (match_desc)
1280 "no processes were found that {0} \"{1}\" on the \"{2}\" "
1281 "platform\n",
1282 match_desc, match_name, platform_sp->GetName());
1283 else
1285 "no processes were found on the \"{0}\" platform\n",
1286 platform_sp->GetName());
1287 } else {
1289 "{0} matching process{1} found on \"{2}\"", matches,
1290 matches > 1 ? "es were" : " was", platform_sp->GetName());
1291 if (match_desc)
1292 result.AppendMessageWithFormat(" whose name %s \"%s\"", match_desc,
1293 match_name);
1294 result.AppendMessageWithFormat("\n");
1296 m_options.verbose);
1297 for (uint32_t i = 0; i < matches; ++i) {
1298 proc_infos[i].DumpAsTableRow(
1299 ostrm, platform_sp->GetUserIDResolver(), m_options.show_args,
1300 m_options.verbose);
1301 }
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 = GetDebugger().GetSelectedTarget().get();
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 } else {
1505 // Not connected...
1506 result.AppendErrorWithFormatv("not connected to '{0}'",
1507 platform_sp->GetPluginName());
1508 }
1509 } else {
1510 // No args
1511 result.AppendError("one or more process id(s) must be specified");
1512 }
1513 } else {
1514 result.AppendError("no platform is currently selected");
1515 }
1516 }
1517};
1518
1519#define LLDB_OPTIONS_platform_process_attach
1520#include "CommandOptions.inc"
1521
1523public:
1525 : CommandObjectParsed(interpreter, "platform process attach",
1526 "Attach to a process.",
1527 "platform process attach <cmd-options>"),
1528 m_class_options("scripted process", true, 'C', 'k', 'v', 0) {
1529 m_all_options.Append(&m_options);
1532 m_all_options.Finalize();
1533 }
1534
1536
1537 void DoExecute(Args &command, CommandReturnObject &result) override {
1538 PlatformSP platform_sp(
1539 GetDebugger().GetPlatformList().GetSelectedPlatform());
1540 if (platform_sp) {
1541
1542 if (!m_class_options.GetName().empty()) {
1543 m_options.attach_info.SetProcessPluginName("ScriptedProcess");
1544 ScriptedMetadataSP metadata_sp = std::make_shared<ScriptedMetadata>(
1545 m_class_options.GetName(), m_class_options.GetStructuredData());
1546 m_options.attach_info.SetScriptedMetadata(metadata_sp);
1547 }
1548
1549 Status err;
1550 ProcessSP remote_process_sp = platform_sp->Attach(
1551 m_options.attach_info, GetDebugger(), nullptr, err);
1552 if (err.Fail()) {
1553 result.AppendError(err.AsCString());
1554 } else if (!remote_process_sp) {
1555 result.AppendError("could not attach: unknown reason");
1556 } else
1558 } else {
1559 result.AppendError("no platform is currently selected");
1560 }
1561 }
1562
1563 Options *GetOptions() override { return &m_all_options; }
1564
1565protected:
1569};
1570
1572public:
1573 // Constructors and Destructors
1575 : CommandObjectMultiword(interpreter, "platform process",
1576 "Commands to query, launch and attach to "
1577 "processes on the current platform.",
1578 "platform process [attach|launch|list] ...") {
1580 "attach",
1583 "launch",
1586 interpreter)));
1588 interpreter)));
1589 }
1590
1591 ~CommandObjectPlatformProcess() override = default;
1592
1593private:
1594 // For CommandObjectPlatform only
1598};
1599
1600// "platform shell"
1601#define LLDB_OPTIONS_platform_shell
1602#include "CommandOptions.inc"
1603
1605public:
1606 class CommandOptions : public Options {
1607 public:
1608 CommandOptions() = default;
1609
1610 ~CommandOptions() override = default;
1611
1612 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1613 return llvm::ArrayRef(g_platform_shell_options);
1614 }
1615
1616 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1617 ExecutionContext *execution_context) override {
1618 Status error;
1619
1620 const char short_option = (char)GetDefinitions()[option_idx].short_option;
1621
1622 switch (short_option) {
1623 case 'h':
1624 m_use_host_platform = true;
1625 break;
1626 case 't':
1627 uint32_t timeout_sec;
1628 if (option_arg.getAsInteger(10, timeout_sec))
1630 "could not convert \"%s\" to a numeric value.",
1631 option_arg.str().c_str());
1632 else
1633 m_timeout = std::chrono::seconds(timeout_sec);
1634 break;
1635 case 's': {
1636 if (option_arg.empty()) {
1638 "missing shell interpreter path for option -i|--interpreter.");
1639 return error;
1640 }
1641
1642 m_shell_interpreter = option_arg.str();
1643 break;
1644 }
1645 default:
1646 llvm_unreachable("Unimplemented option");
1647 }
1648
1649 return error;
1650 }
1651
1652 void OptionParsingStarting(ExecutionContext *execution_context) override {
1653 m_timeout.reset();
1654 m_use_host_platform = false;
1655 m_shell_interpreter.clear();
1656 }
1657
1658 Timeout<std::micro> m_timeout = std::chrono::seconds(10);
1661 };
1662
1664 : CommandObjectRaw(interpreter, "platform shell",
1665 "Run a shell command on the current platform.",
1666 "platform shell <shell-command>", 0) {
1668 }
1669
1670 ~CommandObjectPlatformShell() override = default;
1671
1672 Options *GetOptions() override { return &m_options; }
1673
1674 void DoExecute(llvm::StringRef raw_command_line,
1675 CommandReturnObject &result) override {
1677 m_options.NotifyOptionParsingStarting(&exe_ctx);
1678
1679 // Print out an usage syntax on an empty command line.
1680 if (raw_command_line.empty()) {
1681 result.GetOutputStream().Printf("%s\n", this->GetSyntax().str().c_str());
1682 return;
1683 }
1684
1685 const bool is_alias = !raw_command_line.contains("platform");
1686 OptionsWithRaw args(raw_command_line);
1687
1688 if (args.HasArgs())
1689 if (!ParseOptions(args.GetArgs(), result))
1690 return;
1691
1692 if (args.GetRawPart().empty()) {
1693 result.GetOutputStream().Printf("%s <shell-command>\n",
1694 is_alias ? "shell" : "platform shell");
1695 return;
1696 }
1697
1698 llvm::StringRef cmd = args.GetRawPart();
1699
1700 PlatformSP platform_sp(
1701 m_options.m_use_host_platform
1703 : GetDebugger().GetPlatformList().GetSelectedPlatform());
1704 Status error;
1705 if (platform_sp) {
1706 FileSpec working_dir{};
1707 std::string output;
1708 int status = -1;
1709 int signo = -1;
1710 error = (platform_sp->RunShellCommand(
1711 m_options.m_shell_interpreter, cmd, working_dir, &status, &signo,
1712 &output, nullptr, m_options.m_timeout));
1713 if (!output.empty())
1714 result.GetOutputStream().PutCString(output);
1715 if (status > 0) {
1716 if (signo > 0) {
1717 const char *signo_cstr = Host::GetSignalAsCString(signo);
1718 if (signo_cstr)
1719 result.GetOutputStream().Printf(
1720 "error: command returned with status %i and signal %s\n",
1721 status, signo_cstr);
1722 else
1723 result.GetOutputStream().Printf(
1724 "error: command returned with status %i and signal %i\n",
1725 status, signo);
1726 } else
1727 result.GetOutputStream().Printf(
1728 "error: command returned with status %i\n", status);
1729 }
1730 } else {
1731 result.GetOutputStream().Printf(
1732 "error: cannot run remote shell commands without a platform\n");
1734 "error: cannot run remote shell commands without a platform");
1735 }
1736
1737 if (error.Fail()) {
1738 result.AppendError(error.AsCString());
1739 } else {
1741 }
1742 }
1743
1745};
1746
1747// "platform install" - install a target to a remote end
1749public:
1752 interpreter, "platform target-install",
1753 "Install a target (bundle or executable file) to the remote end.",
1754 "platform target-install <local-thing> <remote-sandbox>", 0) {
1757 m_arguments.push_back({local_arg});
1758 m_arguments.push_back({remote_arg});
1759 }
1760
1761 ~CommandObjectPlatformInstall() override = default;
1762
1763 void
1765 OptionElementVector &opt_element_vector) override {
1766 if (request.GetCursorIndex())
1767 return;
1770 }
1771
1772 void DoExecute(Args &args, CommandReturnObject &result) override {
1773 if (args.GetArgumentCount() != 2) {
1774 result.AppendError("platform target-install takes two arguments");
1775 return;
1776 }
1777 // TODO: move the bulk of this code over to the platform itself
1778 FileSpec src(args.GetArgumentAtIndex(0));
1780 FileSpec dst(args.GetArgumentAtIndex(1));
1781 if (!FileSystem::Instance().Exists(src)) {
1782 result.AppendError("source location does not exist or is not accessible");
1783 return;
1784 }
1785 PlatformSP platform_sp(
1786 GetDebugger().GetPlatformList().GetSelectedPlatform());
1787 if (!platform_sp) {
1788 result.AppendError("no platform currently selected");
1789 return;
1790 }
1791
1792 Status error = platform_sp->Install(src, dst);
1793 if (error.Success()) {
1795 } else {
1796 result.AppendErrorWithFormat("install failed: %s", error.AsCString());
1797 }
1798 }
1799};
1800
1803 interpreter, "platform", "Commands to manage and create platforms.",
1804 "platform [connect|disconnect|info|list|status|select] ...") {
1805 LoadSubCommand("select",
1807 LoadSubCommand("list",
1808 CommandObjectSP(new CommandObjectPlatformList(interpreter)));
1809 LoadSubCommand("status",
1812 new CommandObjectPlatformConnect(interpreter)));
1814 "disconnect",
1817 interpreter)));
1818 LoadSubCommand("mkdir",
1820 LoadSubCommand("file",
1821 CommandObjectSP(new CommandObjectPlatformFile(interpreter)));
1822 LoadSubCommand("file-exists",
1825 interpreter)));
1826 LoadSubCommand("get-permissions",
1829 interpreter)));
1831 interpreter)));
1833 new CommandObjectPlatformProcess(interpreter)));
1834 LoadSubCommand("shell",
1837 "target-install",
1839}
1840
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() const
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)
virtual llvm::StringRef GetSyntax()
void void AppendError(llvm::StringRef in_string)
void SetStatus(lldb::ReturnStatus status)
void AppendErrorWithFormat(const char *format,...) __attribute__((format(printf
void AppendMessageWithFormat(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:87
lldb::TargetSP GetSelectedTarget()
Definition Debugger.h:186
CommandInterpreter & GetCommandInterpreter()
Definition Debugger.h:169
PlatformList & GetPlatformList()
Definition Debugger.h:209
"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.
@ eOpenOptionReadWrite
Definition File.h:53
@ eOpenOptionCanCreate
Definition File.h:56
static const char * GetSignalAsCString(int signo)
A class that describes an executable image and its associated object and symbol files.
Definition Module.h:90
const ArchSpec & GetArchitecture() const
Get const accessor for the module architecture.
Definition Module.cpp:1030
const FileSpec & GetFileSpec() const
Get const accessor for the module file specification.
Definition Module.h:446
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:1111
void SetSelectedPlatform(const lldb::PlatformSP &platform_sp)
Definition Platform.h:1119
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:226
static lldb::PlatformSP GetHostPlatform()
Get the native host platform plug-in.
Definition Platform.cpp:134
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)
Definition Stream.h:364
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:65
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:5151
bool GetRunArguments(Args &args) const
Definition Target.cpp:4730
Module * GetExecutableModulePointer()
Definition Target.cpp:1541
lldb::PlatformSP GetPlatform()
Definition Target.h:1678
#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
@ 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.