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 if (target->IsDummyTarget())
255 target = nullptr;
256 PlatformSP platform_sp;
257 if (target)
258 platform_sp = target->GetPlatform();
259 if (!platform_sp)
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", 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());
398 } else {
399 result.AppendError("no platform is currently selected");
400 }
401 }
402
403 Options *GetOptions() override {
404 if (!m_options.DidFinalize())
405 m_options.Finalize();
406 return &m_options;
407 }
408
411};
412
413// "platform mkdir"
415public:
417 : CommandObjectParsed(interpreter, "platform mkdir",
418 "Make a new directory on the remote end.", nullptr,
419 0) {
421 }
422
423 ~CommandObjectPlatformMkDir() override = default;
424
425 void DoExecute(Args &args, CommandReturnObject &result) override {
426 PlatformSP platform_sp(
427 GetDebugger().GetPlatformList().GetSelectedPlatform());
428 if (platform_sp) {
429 std::string cmd_line;
430 args.GetCommandString(cmd_line);
431 uint32_t mode;
432 const OptionPermissions *options_permissions =
433 (const OptionPermissions *)m_options.GetGroupWithOption('r');
434 if (options_permissions)
435 mode = options_permissions->m_permissions;
436 else
437 mode = lldb::eFilePermissionsUserRWX | lldb::eFilePermissionsGroupRWX |
438 lldb::eFilePermissionsWorldRX;
439 Status error = platform_sp->MakeDirectory(FileSpec(cmd_line), mode);
440 if (error.Success()) {
442 } else {
443 result.AppendError(error.AsCString());
444 }
445 } else {
446 result.AppendError("no platform currently selected\n");
447 }
448 }
449
450 Options *GetOptions() override {
451 if (!m_options.DidFinalize()) {
453 m_options.Finalize();
454 }
455 return &m_options;
456 }
457
460};
461
462// "platform fopen"
464public:
466 : CommandObjectParsed(interpreter, "platform file open",
467 "Open a file on the remote end.", nullptr, 0) {
469 }
470
471 ~CommandObjectPlatformFOpen() override = default;
472
473 void DoExecute(Args &args, CommandReturnObject &result) override {
474 PlatformSP platform_sp(
475 GetDebugger().GetPlatformList().GetSelectedPlatform());
476 if (platform_sp) {
478 std::string cmd_line;
479 args.GetCommandString(cmd_line);
480 mode_t perms;
481 const OptionPermissions *options_permissions =
482 (const OptionPermissions *)m_options.GetGroupWithOption('r');
483 if (options_permissions)
484 perms = options_permissions->m_permissions;
485 else
486 perms = lldb::eFilePermissionsUserRW | lldb::eFilePermissionsGroupRW |
487 lldb::eFilePermissionsWorldRead;
488 lldb::user_id_t fd = platform_sp->OpenFile(
489 FileSpec(cmd_line),
491 perms, error);
492 if (error.Success()) {
493 result.AppendMessageWithFormatv("File Descriptor = {0}", fd);
495 } else {
496 result.AppendError(error.AsCString());
497 }
498 } else {
499 result.AppendError("no platform currently selected\n");
500 }
501 }
502
503 Options *GetOptions() override {
504 if (!m_options.DidFinalize()) {
506 m_options.Finalize();
507 }
508 return &m_options;
509 }
510
513};
514
515// "platform fclose"
517public:
519 : CommandObjectParsed(interpreter, "platform file close",
520 "Close a file on the remote end.", nullptr, 0) {
522 }
523
524 ~CommandObjectPlatformFClose() override = default;
525
526 void DoExecute(Args &args, CommandReturnObject &result) override {
527 PlatformSP platform_sp(
528 GetDebugger().GetPlatformList().GetSelectedPlatform());
529 if (platform_sp) {
530 std::string cmd_line;
531 args.GetCommandString(cmd_line);
533 if (!llvm::to_integer(cmd_line, fd)) {
534 result.AppendErrorWithFormatv("'{0}' is not a valid file descriptor.\n",
535 cmd_line);
536 return;
537 }
539 bool success = platform_sp->CloseFile(fd, error);
540 if (success) {
541 result.AppendMessageWithFormatv("file {0} closed.", fd);
543 } else {
544 result.AppendError(error.AsCString());
545 }
546 } else {
547 result.AppendError("no platform currently selected\n");
548 }
549 }
550};
551
552// "platform fread"
553
554#define LLDB_OPTIONS_platform_fread
555#include "CommandOptions.inc"
556
558public:
560 : CommandObjectParsed(interpreter, "platform file read",
561 "Read data from a file on the remote end.", nullptr,
562 0) {
564 }
565
566 ~CommandObjectPlatformFRead() override = default;
567
568 void DoExecute(Args &args, CommandReturnObject &result) override {
569 PlatformSP platform_sp(
570 GetDebugger().GetPlatformList().GetSelectedPlatform());
571 if (platform_sp) {
572 std::string cmd_line;
573 args.GetCommandString(cmd_line);
575 if (!llvm::to_integer(cmd_line, fd)) {
576 result.AppendErrorWithFormatv("'{0}' is not a valid file descriptor.\n",
577 cmd_line);
578 return;
579 }
580 std::string buffer(m_options.m_count, 0);
582 uint64_t retcode = platform_sp->ReadFile(
583 fd, m_options.m_offset, &buffer[0], m_options.m_count, error);
584 if (retcode != UINT64_MAX) {
585 result.AppendMessageWithFormatv("Return = {0}", retcode);
586 result.AppendMessageWithFormatv("Data = \"{0}\"", buffer.c_str());
588 } else {
589 result.AppendError(error.AsCString());
590 }
591 } else {
592 result.AppendError("no platform currently selected\n");
593 }
594 }
595
596 Options *GetOptions() override { return &m_options; }
597
598protected:
599 class CommandOptions : public Options {
600 public:
601 CommandOptions() = default;
602
603 ~CommandOptions() override = default;
604
605 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
606 ExecutionContext *execution_context) override {
608 char short_option = (char)m_getopt_table[option_idx].val;
609
610 switch (short_option) {
611 case 'o':
612 if (option_arg.getAsInteger(0, m_offset))
613 error = Status::FromErrorStringWithFormat("invalid offset: '%s'",
614 option_arg.str().c_str());
615 break;
616 case 'c':
617 if (option_arg.getAsInteger(0, m_count))
618 error = Status::FromErrorStringWithFormat("invalid offset: '%s'",
619 option_arg.str().c_str());
620 break;
621 default:
622 llvm_unreachable("Unimplemented option");
623 }
624
625 return error;
626 }
627
628 void OptionParsingStarting(ExecutionContext *execution_context) override {
629 m_offset = 0;
630 m_count = 1;
631 }
632
633 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
634 return llvm::ArrayRef(g_platform_fread_options);
635 }
636
637 // Instance variables to hold the values for command options.
638
639 uint32_t m_offset;
640 uint32_t m_count;
641 };
642
644};
645
646// "platform fwrite"
647
648#define LLDB_OPTIONS_platform_fwrite
649#include "CommandOptions.inc"
650
652public:
654 : CommandObjectParsed(interpreter, "platform file write",
655 "Write data to a file on the remote end.", nullptr,
656 0) {
658 }
659
660 ~CommandObjectPlatformFWrite() override = default;
661
662 void DoExecute(Args &args, CommandReturnObject &result) override {
663 PlatformSP platform_sp(
664 GetDebugger().GetPlatformList().GetSelectedPlatform());
665 if (platform_sp) {
666 std::string cmd_line;
667 args.GetCommandString(cmd_line);
670 if (!llvm::to_integer(cmd_line, fd)) {
671 result.AppendErrorWithFormatv("'{0}' is not a valid file descriptor.",
672 cmd_line);
673 return;
674 }
675 uint64_t retcode =
676 platform_sp->WriteFile(fd, m_options.m_offset, &m_options.m_data[0],
677 m_options.m_data.size(), error);
678 if (retcode != UINT64_MAX) {
679 result.AppendMessageWithFormatv("Return = {0}", retcode);
681 } else {
682 result.AppendError(error.AsCString());
683 }
684 } else {
685 result.AppendError("no platform currently selected\n");
686 }
687 }
688
689 Options *GetOptions() override { return &m_options; }
690
691protected:
692 class CommandOptions : public Options {
693 public:
694 CommandOptions() = default;
695
696 ~CommandOptions() override = default;
697
698 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
699 ExecutionContext *execution_context) override {
701 char short_option = (char)m_getopt_table[option_idx].val;
702
703 switch (short_option) {
704 case 'o':
705 if (option_arg.getAsInteger(0, m_offset))
706 error = Status::FromErrorStringWithFormat("invalid offset: '%s'",
707 option_arg.str().c_str());
708 break;
709 case 'd':
710 m_data.assign(std::string(option_arg));
711 break;
712 default:
713 llvm_unreachable("Unimplemented option");
714 }
715
716 return error;
717 }
718
719 void OptionParsingStarting(ExecutionContext *execution_context) override {
720 m_offset = 0;
721 m_data.clear();
722 }
723
724 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
725 return llvm::ArrayRef(g_platform_fwrite_options);
726 }
727
728 // Instance variables to hold the values for command options.
729
730 uint32_t m_offset;
731 std::string m_data;
732 };
733
735};
736
738public:
739 // Constructors and Destructors
742 interpreter, "platform file",
743 "Commands to access files on the current platform.",
744 "platform file [open|close|read|write] ...") {
746 "open", CommandObjectSP(new CommandObjectPlatformFOpen(interpreter)));
748 "close", CommandObjectSP(new CommandObjectPlatformFClose(interpreter)));
750 "read", CommandObjectSP(new CommandObjectPlatformFRead(interpreter)));
752 "write", CommandObjectSP(new CommandObjectPlatformFWrite(interpreter)));
753 }
754
755 ~CommandObjectPlatformFile() override = default;
756
757private:
758 // For CommandObjectPlatform only
762};
763
764// "platform get-file remote-file-path host-file-path"
766public:
769 interpreter, "platform get-file",
770 "Transfer a file from the remote end to the local host.",
771 "platform get-file <remote-file-spec> <local-file-spec>", 0) {
773 R"(Examples:
774
775(lldb) platform get-file /the/remote/file/path /the/local/file/path
776
777 Transfer a file from the remote end with file path /the/remote/file/path to the local host.)");
778
779 CommandArgumentEntry arg1, arg2;
780 CommandArgumentData file_arg_remote, file_arg_host;
781
782 // Define the first (and only) variant of this arg.
783 file_arg_remote.arg_type = eArgTypeRemoteFilename;
784 file_arg_remote.arg_repetition = eArgRepeatPlain;
785 // There is only one variant this argument could be; put it into the
786 // argument entry.
787 arg1.push_back(file_arg_remote);
788
789 // Define the second (and only) variant of this arg.
790 file_arg_host.arg_type = eArgTypeFilename;
791 file_arg_host.arg_repetition = eArgRepeatPlain;
792 // There is only one variant this argument could be; put it into the
793 // argument entry.
794 arg2.push_back(file_arg_host);
795
796 // Push the data for the first and the second arguments into the
797 // m_arguments vector.
798 m_arguments.push_back(arg1);
799 m_arguments.push_back(arg2);
800 }
802 ~CommandObjectPlatformGetFile() override = default;
803
804 void
806 OptionElementVector &opt_element_vector) override {
807 if (request.GetCursorIndex() == 0)
810 nullptr);
811 else if (request.GetCursorIndex() == 1)
814 }
815
816 void DoExecute(Args &args, CommandReturnObject &result) override {
817 // If the number of arguments is incorrect, issue an error message.
818 if (args.GetArgumentCount() != 2) {
819 result.AppendError("required arguments missing; specify both the "
820 "source and destination file paths");
821 return;
822 }
823
824 PlatformSP platform_sp(
825 GetDebugger().GetPlatformList().GetSelectedPlatform());
826 if (platform_sp) {
827 const char *remote_file_path = args.GetArgumentAtIndex(0);
828 const char *local_file_path = args.GetArgumentAtIndex(1);
829 Status error = platform_sp->GetFile(FileSpec(remote_file_path),
830 FileSpec(local_file_path));
831 if (error.Success()) {
833 "successfully get-file from {0} (remote) to {1} (host)",
834 remote_file_path, local_file_path);
836 } else {
837 result.AppendErrorWithFormatv("get-file failed: {0}",
838 error.AsCString());
839 }
840 } else {
841 result.AppendError("no platform currently selected\n");
842 }
843 }
844};
845
846// "platform get-size remote-file-path"
848public:
850 : CommandObjectParsed(interpreter, "platform get-size",
851 "Get the file size from the remote end.",
852 "platform get-size <remote-file-spec>", 0) {
854 R"(Examples:
855
856(lldb) platform get-size /the/remote/file/path
857
858 Get the file size from the remote end with path /the/remote/file/path.)");
862
863 ~CommandObjectPlatformGetSize() override = default;
864
865 void DoExecute(Args &args, CommandReturnObject &result) override {
866 // If the number of arguments is incorrect, issue an error message.
867 if (args.GetArgumentCount() != 1) {
868 result.AppendError("required argument missing; specify the source file "
869 "path as the only argument");
870 return;
871 }
872
873 PlatformSP platform_sp(
874 GetDebugger().GetPlatformList().GetSelectedPlatform());
875 if (platform_sp) {
876 std::string remote_file_path(args.GetArgumentAtIndex(0));
877 user_id_t size = platform_sp->GetFileSize(FileSpec(remote_file_path));
878 if (size != UINT64_MAX) {
879 result.AppendMessageWithFormatv("File size of {0} (remote): {1}",
880 remote_file_path.c_str(), size);
882 } else {
883 result.AppendErrorWithFormatv("failed to get 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 = &GetTarget();
1075 PlatformSP platform_sp = target->GetPlatform();
1076 if (!platform_sp) {
1078 }
1079
1080 if (platform_sp) {
1081 Status error;
1082 const size_t argc = args.GetArgumentCount();
1083 Module *exe_module = target->GetExecutableModulePointer();
1084 if (exe_module) {
1085 m_options.launch_info.GetExecutableFile() = exe_module->GetFileSpec();
1086 llvm::SmallString<128> exe_path;
1087 m_options.launch_info.GetExecutableFile().GetPath(exe_path);
1088 if (!exe_path.empty())
1089 m_options.launch_info.GetArguments().AppendArgument(exe_path);
1090 m_options.launch_info.GetArchitecture() = exe_module->GetArchitecture();
1091 }
1092
1093 if (!m_class_options.GetName().empty()) {
1094 m_options.launch_info.SetProcessPluginName("ScriptedProcess");
1095 ScriptedMetadataSP metadata_sp = std::make_shared<ScriptedMetadata>(
1096 m_class_options.GetName(), m_class_options.GetStructuredData());
1097 m_options.launch_info.SetScriptedMetadata(metadata_sp);
1098 target->SetProcessLaunchInfo(m_options.launch_info);
1099 }
1100
1101 if (argc > 0) {
1102 if (m_options.launch_info.GetExecutableFile()) {
1103 // We already have an executable file, so we will use this and all
1104 // arguments to this function are extra arguments
1105 m_options.launch_info.GetArguments().AppendArguments(args);
1106 } else {
1107 // We don't have any file yet, so the first argument is our
1108 // executable, and the rest are program arguments
1109 const bool first_arg_is_executable = true;
1110 m_options.launch_info.SetArguments(args, first_arg_is_executable);
1111 }
1112 }
1113
1114 if (m_options.launch_info.GetExecutableFile()) {
1115 Debugger &debugger = GetDebugger();
1116
1117 if (argc == 0) {
1118 // If no arguments were given to the command, use target.run-args.
1119 Args target_run_args;
1120 target->GetRunArguments(target_run_args);
1121 m_options.launch_info.GetArguments().AppendArguments(target_run_args);
1122 }
1123
1124 ProcessSP process_sp(platform_sp->DebugProcess(
1125 m_options.launch_info, debugger, *target, error));
1126
1127 if (!process_sp && error.Success()) {
1128 result.AppendError("failed to launch or debug process");
1129 return;
1130 } else if (!error.Success()) {
1131 result.AppendError(error.AsCString());
1132 return;
1133 }
1134
1135 const bool synchronous_execution =
1137 auto launch_info = m_options.launch_info;
1138 bool rebroadcast_first_stop =
1139 !synchronous_execution &&
1140 launch_info.GetFlags().Test(eLaunchFlagStopAtEntry);
1141
1142 EventSP first_stop_event_sp;
1143 StateType state = process_sp->WaitForProcessToStop(
1144 std::nullopt, &first_stop_event_sp, rebroadcast_first_stop,
1145 launch_info.GetHijackListener());
1146 process_sp->RestoreProcessEvents();
1147
1148 if (rebroadcast_first_stop) {
1149 assert(first_stop_event_sp);
1150 process_sp->BroadcastEvent(first_stop_event_sp);
1152 return;
1153 }
1154
1155 switch (state) {
1156 case eStateStopped: {
1157 if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
1158 break;
1159 if (synchronous_execution) {
1160 // Now we have handled the stop-from-attach, and we are just
1161 // switching to a synchronous resume. So we should switch to the
1162 // SyncResume hijacker.
1163 process_sp->ResumeSynchronous(&result.GetOutputStream());
1164 } else {
1165 error = process_sp->Resume();
1166 if (!error.Success()) {
1167 result.AppendErrorWithFormat(
1168 "process resume at entry point failed: %s",
1169 error.AsCString());
1170 }
1171 }
1172 } break;
1173 default:
1174 result.AppendErrorWithFormat(
1175 "initial process state wasn't stopped: %s",
1176 StateAsCString(state));
1177 break;
1178 }
1179
1180 if (process_sp && process_sp->IsAlive()) {
1182 return;
1183 }
1184 if (result.GetStatus() != eReturnStatusFailed)
1186 } else {
1187 result.AppendError("'platform process launch' uses the current target "
1188 "file and arguments, or the executable and its "
1189 "arguments can be specified in this command");
1190 return;
1191 }
1192 } else {
1193 result.AppendError("no platform is selected\n");
1194 }
1195 }
1196
1200};
1201
1202// "platform process list"
1203
1205#define LLDB_OPTIONS_platform_process_list
1206#include "CommandOptions.inc"
1207
1209public:
1211 : CommandObjectParsed(interpreter, "platform process list",
1212 "List processes on a remote platform by name, pid, "
1213 "or many other matching attributes.",
1214 "platform process list", 0) {}
1215
1217
1218 Options *GetOptions() override { return &m_options; }
1219
1220protected:
1221 void DoExecute(Args &args, CommandReturnObject &result) override {
1222 Target *target = &GetTarget();
1223 if (target->IsDummyTarget())
1224 target = nullptr;
1225 PlatformSP platform_sp;
1226 if (target) {
1227 platform_sp = target->GetPlatform();
1228 }
1229 if (!platform_sp) {
1231 }
1232
1233 if (platform_sp) {
1234 Stream &ostrm = result.GetOutputStream();
1235
1236 lldb::pid_t pid = m_options.match_info.GetProcessInfo().GetProcessID();
1237 if (pid != LLDB_INVALID_PROCESS_ID) {
1238 ProcessInstanceInfo proc_info;
1239 if (platform_sp->GetProcessInfo(pid, proc_info)) {
1241 m_options.verbose);
1242 proc_info.DumpAsTableRow(ostrm, platform_sp->GetUserIDResolver(),
1243 m_options.show_args, m_options.verbose);
1245 } else {
1246 result.AppendErrorWithFormat("no process found with pid = %" PRIu64,
1247 pid);
1248 }
1249 } else {
1250 ProcessInstanceInfoList proc_infos;
1251 const uint32_t matches =
1252 platform_sp->FindProcesses(m_options.match_info, proc_infos);
1253 const char *match_desc = nullptr;
1254 const char *match_name =
1255 m_options.match_info.GetProcessInfo().GetName();
1256 if (match_name && match_name[0]) {
1257 switch (m_options.match_info.GetNameMatchType()) {
1258 case NameMatch::Ignore:
1259 break;
1260 case NameMatch::Equals:
1261 match_desc = "matched";
1262 break;
1264 match_desc = "contained";
1265 break;
1267 match_desc = "started with";
1268 break;
1270 match_desc = "ended with";
1271 break;
1273 match_desc = "matched the regular expression";
1274 break;
1275 }
1276 }
1277
1278 if (matches == 0) {
1279 if (match_desc)
1281 "no processes were found that {0} \"{1}\" on the \"{2}\" "
1282 "platform\n",
1283 match_desc, match_name, platform_sp->GetName());
1284 else
1286 "no processes were found on the \"{0}\" platform\n",
1287 platform_sp->GetName());
1288 } else {
1290 "{0} matching process{1} found on \"{2}\"", matches,
1291 matches > 1 ? "es were" : " was", platform_sp->GetName());
1292 Stream &strm = result.GetOutputStream();
1293 if (match_desc)
1294 strm << llvm::formatv(" whose name {0} \"{1}\"", match_desc,
1295 match_name);
1296 strm.PutChar('\n');
1298 m_options.verbose);
1299 for (uint32_t i = 0; i < matches; ++i) {
1300 proc_infos[i].DumpAsTableRow(
1301 ostrm, platform_sp->GetUserIDResolver(), m_options.show_args,
1302 m_options.verbose);
1303 }
1305 }
1306 }
1307 } else {
1308 result.AppendError("no platform is selected\n");
1309 }
1310 }
1311
1312 class CommandOptions : public Options {
1313 public:
1314 CommandOptions() = default;
1315
1316 ~CommandOptions() override = default;
1317
1318 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1319 ExecutionContext *execution_context) override {
1320 Status error;
1321 const int short_option = m_getopt_table[option_idx].val;
1322 bool success = false;
1323
1324 uint32_t id = LLDB_INVALID_PROCESS_ID;
1325 success = !option_arg.getAsInteger(0, id);
1326 switch (short_option) {
1327 case 'p': {
1328 match_info.GetProcessInfo().SetProcessID(id);
1329 if (!success)
1331 "invalid process ID string: '%s'", option_arg.str().c_str());
1332 break;
1333 }
1334 case 'P':
1335 match_info.GetProcessInfo().SetParentProcessID(id);
1336 if (!success)
1338 "invalid parent process ID string: '%s'",
1339 option_arg.str().c_str());
1340 break;
1341
1342 case 'u':
1343 match_info.GetProcessInfo().SetUserID(success ? id : UINT32_MAX);
1344 if (!success)
1346 "invalid user ID string: '%s'", option_arg.str().c_str());
1347 break;
1348
1349 case 'U':
1350 match_info.GetProcessInfo().SetEffectiveUserID(success ? id
1351 : UINT32_MAX);
1352 if (!success)
1354 "invalid effective user ID string: '%s'",
1355 option_arg.str().c_str());
1356 break;
1357
1358 case 'g':
1359 match_info.GetProcessInfo().SetGroupID(success ? id : UINT32_MAX);
1360 if (!success)
1362 "invalid group ID string: '%s'", option_arg.str().c_str());
1363 break;
1364
1365 case 'G':
1366 match_info.GetProcessInfo().SetEffectiveGroupID(success ? id
1367 : UINT32_MAX);
1368 if (!success)
1370 "invalid effective group ID string: '%s'",
1371 option_arg.str().c_str());
1372 break;
1373
1374 case 'a': {
1375 TargetSP target_sp =
1376 execution_context ? execution_context->GetTargetSP() : TargetSP();
1377 DebuggerSP debugger_sp =
1378 target_sp ? target_sp->GetDebugger().shared_from_this()
1379 : DebuggerSP();
1380 PlatformSP platform_sp =
1381 debugger_sp ? debugger_sp->GetPlatformList().GetSelectedPlatform()
1382 : PlatformSP();
1383 match_info.GetProcessInfo().GetArchitecture() =
1384 Platform::GetAugmentedArchSpec(platform_sp.get(), option_arg);
1385 } break;
1386
1387 case 'n':
1388 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1389 option_arg, FileSpec::Style::native);
1390 match_info.SetNameMatchType(NameMatch::Equals);
1391 break;
1392
1393 case 'e':
1394 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1395 option_arg, FileSpec::Style::native);
1396 match_info.SetNameMatchType(NameMatch::EndsWith);
1397 break;
1398
1399 case 's':
1400 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1401 option_arg, FileSpec::Style::native);
1402 match_info.SetNameMatchType(NameMatch::StartsWith);
1403 break;
1404
1405 case 'c':
1406 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1407 option_arg, FileSpec::Style::native);
1408 match_info.SetNameMatchType(NameMatch::Contains);
1409 break;
1410
1411 case 'r':
1412 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1413 option_arg, FileSpec::Style::native);
1414 match_info.SetNameMatchType(NameMatch::RegularExpression);
1415 break;
1416
1417 case 'A':
1418 show_args = true;
1419 break;
1420
1421 case 'v':
1422 verbose = true;
1423 break;
1424
1425 case 'x':
1426 match_info.SetMatchAllUsers(true);
1427 break;
1428
1429 default:
1430 llvm_unreachable("Unimplemented option");
1431 }
1432
1433 return error;
1434 }
1435
1436 void OptionParsingStarting(ExecutionContext *execution_context) override {
1437 match_info.Clear();
1438 show_args = false;
1439 verbose = false;
1440 }
1441
1442 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1443 return llvm::ArrayRef(g_platform_process_list_options);
1444 }
1445
1446 // Instance variables to hold the values for command options.
1447
1449 bool show_args = false;
1450 bool verbose = false;
1451 };
1452
1454};
1455
1456// "platform process info"
1458public:
1461 interpreter, "platform process info",
1462 "Get detailed information for one or more process by process ID.",
1463 "platform process info <pid> [<pid> <pid> ...]", 0) {
1465 }
1466
1468
1469protected:
1470 void DoExecute(Args &args, CommandReturnObject &result) override {
1471 Target *target = &GetTarget();
1472 if (target->IsDummyTarget())
1473 target = nullptr;
1474 PlatformSP platform_sp;
1475 if (target) {
1476 platform_sp = target->GetPlatform();
1477 }
1478 if (!platform_sp) {
1480 }
1481
1482 if (platform_sp) {
1483 const size_t argc = args.GetArgumentCount();
1484 if (argc > 0) {
1485 Status error;
1486
1487 if (platform_sp->IsConnected()) {
1488 Stream &ostrm = result.GetOutputStream();
1489 for (auto &entry : args.entries()) {
1490 lldb::pid_t pid;
1491 if (entry.ref().getAsInteger(0, pid)) {
1492 result.AppendErrorWithFormat("invalid process ID argument '%s'",
1493 entry.ref().str().c_str());
1494 break;
1495 } else {
1496 ProcessInstanceInfo proc_info;
1497 if (platform_sp->GetProcessInfo(pid, proc_info)) {
1498 ostrm.Printf("Process information for process %" PRIu64 ":\n",
1499 pid);
1500 proc_info.Dump(ostrm, platform_sp->GetUserIDResolver());
1501 } else {
1502 ostrm.Printf("error: no process information is available for "
1503 "process %" PRIu64 "\n",
1504 pid);
1505 }
1506 ostrm.EOL();
1507 }
1508 }
1509 if (result.GetStatus() != eReturnStatusFailed)
1511 } else {
1512 // Not connected...
1513 result.AppendErrorWithFormatv("not connected to '{0}'",
1514 platform_sp->GetPluginName());
1515 }
1516 } else {
1517 // No args
1518 result.AppendError("one or more process id(s) must be specified");
1519 }
1520 } else {
1521 result.AppendError("no platform is currently selected");
1522 }
1523 }
1524};
1525
1526#define LLDB_OPTIONS_platform_process_attach
1527#include "CommandOptions.inc"
1528
1530public:
1532 : CommandObjectParsed(interpreter, "platform process attach",
1533 "Attach to a process.",
1534 "platform process attach <cmd-options>"),
1535 m_class_options("scripted process", true, 'C', 'k', 'v', 0) {
1536 m_all_options.Append(&m_options);
1539 m_all_options.Finalize();
1540 }
1541
1543
1544 void DoExecute(Args &command, CommandReturnObject &result) override {
1545 PlatformSP platform_sp(
1546 GetDebugger().GetPlatformList().GetSelectedPlatform());
1547 if (platform_sp) {
1548
1549 if (!m_class_options.GetName().empty()) {
1550 m_options.attach_info.SetProcessPluginName("ScriptedProcess");
1551 ScriptedMetadataSP metadata_sp = std::make_shared<ScriptedMetadata>(
1552 m_class_options.GetName(), m_class_options.GetStructuredData());
1553 m_options.attach_info.SetScriptedMetadata(metadata_sp);
1554 }
1555
1556 Status err;
1557 ProcessSP remote_process_sp = platform_sp->Attach(
1558 m_options.attach_info, GetDebugger(), nullptr, err);
1559 if (err.Fail()) {
1560 result.AppendError(err.AsCString());
1561 } else if (!remote_process_sp) {
1562 result.AppendError("could not attach: unknown reason");
1563 } else
1565 } else {
1566 result.AppendError("no platform is currently selected");
1567 }
1568 }
1569
1570 Options *GetOptions() override { return &m_all_options; }
1571
1572protected:
1576};
1577
1579public:
1580 // Constructors and Destructors
1582 : CommandObjectMultiword(interpreter, "platform process",
1583 "Commands to query, launch and attach to "
1584 "processes on the current platform.",
1585 "platform process [attach|launch|list] ...") {
1587 "attach",
1590 "launch",
1593 interpreter)));
1595 interpreter)));
1596 }
1597
1598 ~CommandObjectPlatformProcess() override = default;
1599
1600private:
1601 // For CommandObjectPlatform only
1605};
1606
1607// "platform shell"
1608#define LLDB_OPTIONS_platform_shell
1609#include "CommandOptions.inc"
1610
1612public:
1613 class CommandOptions : public Options {
1614 public:
1615 CommandOptions() = default;
1616
1617 ~CommandOptions() override = default;
1618
1619 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1620 return llvm::ArrayRef(g_platform_shell_options);
1621 }
1622
1623 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1624 ExecutionContext *execution_context) override {
1625 Status error;
1626
1627 const char short_option = (char)GetDefinitions()[option_idx].short_option;
1628
1629 switch (short_option) {
1630 case 'h':
1631 m_use_host_platform = true;
1632 break;
1633 case 't':
1634 uint32_t timeout_sec;
1635 if (option_arg.getAsInteger(10, timeout_sec))
1637 "could not convert \"%s\" to a numeric value.",
1638 option_arg.str().c_str());
1639 else
1640 m_timeout = std::chrono::seconds(timeout_sec);
1641 break;
1642 case 's': {
1643 if (option_arg.empty()) {
1645 "missing shell interpreter path for option -i|--interpreter.");
1646 return error;
1647 }
1648
1649 m_shell_interpreter = option_arg.str();
1650 break;
1651 }
1652 default:
1653 llvm_unreachable("Unimplemented option");
1654 }
1655
1656 return error;
1657 }
1658
1659 void OptionParsingStarting(ExecutionContext *execution_context) override {
1660 m_timeout.reset();
1661 m_use_host_platform = false;
1662 m_shell_interpreter.clear();
1663 }
1664
1665 Timeout<std::micro> m_timeout = std::chrono::seconds(10);
1668 };
1669
1671 : CommandObjectRaw(interpreter, "platform shell",
1672 "Run a shell command on the current platform.",
1673 "platform shell <shell-command>", 0) {
1675 }
1676
1677 ~CommandObjectPlatformShell() override = default;
1678
1679 Options *GetOptions() override { return &m_options; }
1680
1681 void DoExecute(llvm::StringRef raw_command_line,
1682 CommandReturnObject &result) override {
1684 m_options.NotifyOptionParsingStarting(&exe_ctx);
1685
1686 // Print out an usage syntax on an empty command line.
1687 if (raw_command_line.empty()) {
1688 result.GetOutputStream().Printf("%s\n", this->GetSyntax().str().c_str());
1689 return;
1690 }
1691
1692 const bool is_alias = !raw_command_line.contains("platform");
1693 OptionsWithRaw args(raw_command_line);
1694
1695 if (args.HasArgs())
1696 if (!ParseOptions(args.GetArgs(), result))
1697 return;
1698
1699 if (args.GetRawPart().empty()) {
1700 result.GetOutputStream().Printf("%s <shell-command>\n",
1701 is_alias ? "shell" : "platform shell");
1702 return;
1703 }
1704
1705 llvm::StringRef cmd = args.GetRawPart();
1706
1707 PlatformSP platform_sp(
1708 m_options.m_use_host_platform
1710 : GetDebugger().GetPlatformList().GetSelectedPlatform());
1711 Status error;
1712 if (platform_sp) {
1713 FileSpec working_dir{};
1714 std::string output;
1715 int status = -1;
1716 int signo = -1;
1717 error = (platform_sp->RunShellCommand(
1718 m_options.m_shell_interpreter, cmd, working_dir, &status, &signo,
1719 &output, nullptr, m_options.m_timeout));
1720 if (!output.empty())
1721 result.GetOutputStream().PutCString(output);
1722 if (status > 0) {
1723 if (signo > 0) {
1724 const char *signo_cstr = Host::GetSignalAsCString(signo);
1725 if (signo_cstr)
1726 result.GetOutputStream().Printf(
1727 "error: command returned with status %i and signal %s\n",
1728 status, signo_cstr);
1729 else
1730 result.GetOutputStream().Printf(
1731 "error: command returned with status %i and signal %i\n",
1732 status, signo);
1733 } else
1734 result.GetOutputStream().Printf(
1735 "error: command returned with status %i\n", status);
1736 }
1737 } else {
1738 result.GetOutputStream().Printf(
1739 "error: cannot run remote shell commands without a platform\n");
1741 "error: cannot run remote shell commands without a platform");
1742 }
1743
1744 if (error.Fail()) {
1745 result.AppendError(error.AsCString());
1746 } else {
1748 }
1749 }
1750
1752};
1753
1754// "platform install" - install a target to a remote end
1756public:
1759 interpreter, "platform target-install",
1760 "Install a target (bundle or executable file) to the remote end.",
1761 "platform target-install <local-thing> <remote-sandbox>", 0) {
1764 m_arguments.push_back({local_arg});
1765 m_arguments.push_back({remote_arg});
1766 }
1767
1768 ~CommandObjectPlatformInstall() override = default;
1769
1770 void
1772 OptionElementVector &opt_element_vector) override {
1773 if (request.GetCursorIndex())
1774 return;
1777 }
1778
1779 void DoExecute(Args &args, CommandReturnObject &result) override {
1780 if (args.GetArgumentCount() != 2) {
1781 result.AppendError("platform target-install takes two arguments");
1782 return;
1783 }
1784 // TODO: move the bulk of this code over to the platform itself
1785 FileSpec src(args.GetArgumentAtIndex(0));
1787 FileSpec dst(args.GetArgumentAtIndex(1));
1788 if (!FileSystem::Instance().Exists(src)) {
1789 result.AppendError("source location does not exist or is not accessible");
1790 return;
1791 }
1792 PlatformSP platform_sp(
1793 GetDebugger().GetPlatformList().GetSelectedPlatform());
1794 if (!platform_sp) {
1795 result.AppendError("no platform currently selected");
1796 return;
1797 }
1798
1799 Status error = platform_sp->Install(src, dst);
1800 if (error.Success()) {
1802 } else {
1803 result.AppendErrorWithFormat("install failed: %s", error.AsCString());
1804 }
1805 }
1806};
1807
1810 interpreter, "platform", "Commands to manage and create platforms.",
1811 "platform [connect|disconnect|info|list|status|select] ...") {
1812 LoadSubCommand("select",
1814 LoadSubCommand("list",
1815 CommandObjectSP(new CommandObjectPlatformList(interpreter)));
1816 LoadSubCommand("status",
1819 new CommandObjectPlatformConnect(interpreter)));
1821 "disconnect",
1824 interpreter)));
1825 LoadSubCommand("mkdir",
1827 LoadSubCommand("file",
1828 CommandObjectSP(new CommandObjectPlatformFile(interpreter)));
1829 LoadSubCommand("file-exists",
1832 interpreter)));
1833 LoadSubCommand("get-permissions",
1836 interpreter)));
1838 interpreter)));
1840 new CommandObjectPlatformProcess(interpreter)));
1841 LoadSubCommand("shell",
1844 "target-install",
1846}
1847
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 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.
@ 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:1026
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: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:5740
bool GetRunArguments(Args &args) const
Definition Target.cpp:5313
Module * GetExecutableModulePointer()
Definition Target.cpp:1610
lldb::PlatformSP GetPlatform()
Definition Target.h:1972
bool IsDummyTarget() const
Definition Target.h:670
#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.