1 //===-- CommandObjectPlatform.cpp -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "CommandObjectPlatform.h"
12 #include "lldb/Core/Debugger.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/PluginManager.h"
15 #include "lldb/Host/OptionParser.h"
16 #include "lldb/Host/StringConvert.h"
17 #include "lldb/Interpreter/CommandInterpreter.h"
18 #include "lldb/Interpreter/CommandOptionValidators.h"
19 #include "lldb/Interpreter/CommandReturnObject.h"
20 #include "lldb/Interpreter/OptionGroupFile.h"
21 #include "lldb/Interpreter/OptionGroupPlatform.h"
22 #include "lldb/Target/ExecutionContext.h"
23 #include "lldb/Target/Platform.h"
24 #include "lldb/Target/Process.h"
25 #include "lldb/Utility/Args.h"
26 #include "lldb/Utility/DataExtractor.h"
28 #include "llvm/ADT/SmallString.h"
29 #include "llvm/Support/Threading.h"
32 using namespace lldb_private;
34 static mode_t ParsePermissionString(const char *) = delete;
36 static mode_t ParsePermissionString(llvm::StringRef permissions) {
37 if (permissions.size() != 9)
39 bool user_r, user_w, user_x, group_r, group_w, group_x, world_r, world_w,
42 user_r = (permissions[0] == 'r');
43 user_w = (permissions[1] == 'w');
44 user_x = (permissions[2] == 'x');
46 group_r = (permissions[3] == 'r');
47 group_w = (permissions[4] == 'w');
48 group_x = (permissions[5] == 'x');
50 world_r = (permissions[6] == 'r');
51 world_w = (permissions[7] == 'w');
52 world_x = (permissions[8] == 'x');
54 mode_t user, group, world;
55 user = (user_r ? 4 : 0) | (user_w ? 2 : 0) | (user_x ? 1 : 0);
56 group = (group_r ? 4 : 0) | (group_w ? 2 : 0) | (group_x ? 1 : 0);
57 world = (world_r ? 4 : 0) | (world_w ? 2 : 0) | (world_x ? 1 : 0);
59 return user | group | world;
62 static constexpr OptionDefinition g_permissions_options[] = {
64 {LLDB_OPT_SET_ALL, false, "permissions-value", 'v', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePermissionsNumber, "Give out the numeric value for permissions (e.g. 757)"},
65 {LLDB_OPT_SET_ALL, false, "permissions-string", 's', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePermissionsString, "Give out the string value for permissions (e.g. rwxr-xr--)."},
66 {LLDB_OPT_SET_ALL, false, "user-read", 'r', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Allow user to read."},
67 {LLDB_OPT_SET_ALL, false, "user-write", 'w', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Allow user to write."},
68 {LLDB_OPT_SET_ALL, false, "user-exec", 'x', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Allow user to execute."},
69 {LLDB_OPT_SET_ALL, false, "group-read", 'R', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Allow group to read."},
70 {LLDB_OPT_SET_ALL, false, "group-write", 'W', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Allow group to write."},
71 {LLDB_OPT_SET_ALL, false, "group-exec", 'X', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Allow group to execute."},
72 {LLDB_OPT_SET_ALL, false, "world-read", 'd', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Allow world to read."},
73 {LLDB_OPT_SET_ALL, false, "world-write", 't', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Allow world to write."},
74 {LLDB_OPT_SET_ALL, false, "world-exec", 'e', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Allow world to execute."},
78 class OptionPermissions : public OptionGroup {
80 OptionPermissions() {}
82 ~OptionPermissions() override = default;
85 SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
86 ExecutionContext *execution_context) override {
88 char short_option = (char)GetDefinitions()[option_idx].short_option;
89 switch (short_option) {
91 if (option_arg.getAsInteger(8, m_permissions)) {
93 error.SetErrorStringWithFormat("invalid value for permissions: %s",
94 option_arg.str().c_str());
99 mode_t perms = ParsePermissionString(option_arg);
100 if (perms == (mode_t)-1)
101 error.SetErrorStringWithFormat("invalid value for permissions: %s",
102 option_arg.str().c_str());
104 m_permissions = perms;
107 m_permissions |= lldb::eFilePermissionsUserRead;
110 m_permissions |= lldb::eFilePermissionsUserWrite;
113 m_permissions |= lldb::eFilePermissionsUserExecute;
116 m_permissions |= lldb::eFilePermissionsGroupRead;
119 m_permissions |= lldb::eFilePermissionsGroupWrite;
122 m_permissions |= lldb::eFilePermissionsGroupExecute;
125 m_permissions |= lldb::eFilePermissionsWorldRead;
128 m_permissions |= lldb::eFilePermissionsWorldWrite;
131 m_permissions |= lldb::eFilePermissionsWorldExecute;
134 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
141 void OptionParsingStarting(ExecutionContext *execution_context) override {
145 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
146 return llvm::makeArrayRef(g_permissions_options);
149 // Instance variables to hold the values for command options.
151 uint32_t m_permissions;
154 DISALLOW_COPY_AND_ASSIGN(OptionPermissions);
157 //----------------------------------------------------------------------
158 // "platform select <platform-name>"
159 //----------------------------------------------------------------------
160 class CommandObjectPlatformSelect : public CommandObjectParsed {
162 CommandObjectPlatformSelect(CommandInterpreter &interpreter)
163 : CommandObjectParsed(interpreter, "platform select",
164 "Create a platform if needed and select it as the "
166 "platform select <platform-name>", 0),
169 false) // Don't include the "--platform" option by passing false
171 m_option_group.Append(&m_platform_options, LLDB_OPT_SET_ALL, 1);
172 m_option_group.Finalize();
175 ~CommandObjectPlatformSelect() override = default;
177 int HandleCompletion(CompletionRequest &request) override {
178 CommandCompletions::PlatformPluginNames(GetCommandInterpreter(), request,
180 return request.GetNumberOfMatches();
183 Options *GetOptions() override { return &m_option_group; }
186 bool DoExecute(Args &args, CommandReturnObject &result) override {
187 if (args.GetArgumentCount() == 1) {
188 const char *platform_name = args.GetArgumentAtIndex(0);
189 if (platform_name && platform_name[0]) {
190 const bool select = true;
191 m_platform_options.SetPlatformName(platform_name);
193 ArchSpec platform_arch;
194 PlatformSP platform_sp(m_platform_options.CreatePlatformWithOptions(
195 m_interpreter, ArchSpec(), select, error, platform_arch));
197 m_interpreter.GetDebugger().GetPlatformList().SetSelectedPlatform(
200 platform_sp->GetStatus(result.GetOutputStream());
201 result.SetStatus(eReturnStatusSuccessFinishResult);
203 result.AppendError(error.AsCString());
204 result.SetStatus(eReturnStatusFailed);
207 result.AppendError("invalid platform name");
208 result.SetStatus(eReturnStatusFailed);
212 "platform create takes a platform name as an argument\n");
213 result.SetStatus(eReturnStatusFailed);
215 return result.Succeeded();
218 OptionGroupOptions m_option_group;
219 OptionGroupPlatform m_platform_options;
222 //----------------------------------------------------------------------
224 //----------------------------------------------------------------------
225 class CommandObjectPlatformList : public CommandObjectParsed {
227 CommandObjectPlatformList(CommandInterpreter &interpreter)
228 : CommandObjectParsed(interpreter, "platform list",
229 "List all platforms that are available.", nullptr,
232 ~CommandObjectPlatformList() override = default;
235 bool DoExecute(Args &args, CommandReturnObject &result) override {
236 Stream &ostrm = result.GetOutputStream();
237 ostrm.Printf("Available platforms:\n");
239 PlatformSP host_platform_sp(Platform::GetHostPlatform());
240 ostrm.Printf("%s: %s\n", host_platform_sp->GetPluginName().GetCString(),
241 host_platform_sp->GetDescription());
244 for (idx = 0; 1; ++idx) {
245 const char *plugin_name =
246 PluginManager::GetPlatformPluginNameAtIndex(idx);
247 if (plugin_name == nullptr)
249 const char *plugin_desc =
250 PluginManager::GetPlatformPluginDescriptionAtIndex(idx);
251 if (plugin_desc == nullptr)
253 ostrm.Printf("%s: %s\n", plugin_name, plugin_desc);
257 result.AppendError("no platforms are available\n");
258 result.SetStatus(eReturnStatusFailed);
260 result.SetStatus(eReturnStatusSuccessFinishResult);
261 return result.Succeeded();
265 //----------------------------------------------------------------------
267 //----------------------------------------------------------------------
268 class CommandObjectPlatformStatus : public CommandObjectParsed {
270 CommandObjectPlatformStatus(CommandInterpreter &interpreter)
271 : CommandObjectParsed(interpreter, "platform status",
272 "Display status for the current platform.", nullptr,
275 ~CommandObjectPlatformStatus() override = default;
278 bool DoExecute(Args &args, CommandReturnObject &result) override {
279 Stream &ostrm = result.GetOutputStream();
281 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
282 PlatformSP platform_sp;
284 platform_sp = target->GetPlatform();
288 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform();
291 platform_sp->GetStatus(ostrm);
292 result.SetStatus(eReturnStatusSuccessFinishResult);
294 result.AppendError("no platform is currently selected\n");
295 result.SetStatus(eReturnStatusFailed);
297 return result.Succeeded();
301 //----------------------------------------------------------------------
302 // "platform connect <connect-url>"
303 //----------------------------------------------------------------------
304 class CommandObjectPlatformConnect : public CommandObjectParsed {
306 CommandObjectPlatformConnect(CommandInterpreter &interpreter)
307 : CommandObjectParsed(
308 interpreter, "platform connect",
309 "Select the current platform by providing a connection URL.",
310 "platform connect <connect-url>", 0) {}
312 ~CommandObjectPlatformConnect() override = default;
315 bool DoExecute(Args &args, CommandReturnObject &result) override {
316 Stream &ostrm = result.GetOutputStream();
318 PlatformSP platform_sp(
319 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
321 Status error(platform_sp->ConnectRemote(args));
322 if (error.Success()) {
323 platform_sp->GetStatus(ostrm);
324 result.SetStatus(eReturnStatusSuccessFinishResult);
326 platform_sp->ConnectToWaitingProcesses(m_interpreter.GetDebugger(),
329 result.AppendError(error.AsCString());
330 result.SetStatus(eReturnStatusFailed);
333 result.AppendErrorWithFormat("%s\n", error.AsCString());
334 result.SetStatus(eReturnStatusFailed);
337 result.AppendError("no platform is currently selected\n");
338 result.SetStatus(eReturnStatusFailed);
340 return result.Succeeded();
343 Options *GetOptions() override {
344 PlatformSP platform_sp(
345 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
346 OptionGroupOptions *m_platform_options = nullptr;
348 m_platform_options = platform_sp->GetConnectionOptions(m_interpreter);
349 if (m_platform_options != nullptr && !m_platform_options->m_did_finalize)
350 m_platform_options->Finalize();
352 return m_platform_options;
356 //----------------------------------------------------------------------
357 // "platform disconnect"
358 //----------------------------------------------------------------------
359 class CommandObjectPlatformDisconnect : public CommandObjectParsed {
361 CommandObjectPlatformDisconnect(CommandInterpreter &interpreter)
362 : CommandObjectParsed(interpreter, "platform disconnect",
363 "Disconnect from the current platform.",
364 "platform disconnect", 0) {}
366 ~CommandObjectPlatformDisconnect() override = default;
369 bool DoExecute(Args &args, CommandReturnObject &result) override {
370 PlatformSP platform_sp(
371 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
373 if (args.GetArgumentCount() == 0) {
376 if (platform_sp->IsConnected()) {
377 // Cache the instance name if there is one since we are about to
378 // disconnect and the name might go with it.
379 const char *hostname_cstr = platform_sp->GetHostname();
380 std::string hostname;
382 hostname.assign(hostname_cstr);
384 error = platform_sp->DisconnectRemote();
385 if (error.Success()) {
386 Stream &ostrm = result.GetOutputStream();
387 if (hostname.empty())
388 ostrm.Printf("Disconnected from \"%s\"\n",
389 platform_sp->GetPluginName().GetCString());
391 ostrm.Printf("Disconnected from \"%s\"\n", hostname.c_str());
392 result.SetStatus(eReturnStatusSuccessFinishResult);
394 result.AppendErrorWithFormat("%s", error.AsCString());
395 result.SetStatus(eReturnStatusFailed);
399 result.AppendErrorWithFormat(
400 "not connected to '%s'",
401 platform_sp->GetPluginName().GetCString());
402 result.SetStatus(eReturnStatusFailed);
407 "\"platform disconnect\" doesn't take any arguments");
408 result.SetStatus(eReturnStatusFailed);
411 result.AppendError("no platform is currently selected");
412 result.SetStatus(eReturnStatusFailed);
414 return result.Succeeded();
418 //----------------------------------------------------------------------
419 // "platform settings"
420 //----------------------------------------------------------------------
421 class CommandObjectPlatformSettings : public CommandObjectParsed {
423 CommandObjectPlatformSettings(CommandInterpreter &interpreter)
424 : CommandObjectParsed(interpreter, "platform settings",
425 "Set settings for the current target's platform, "
426 "or for a platform by name.",
427 "platform settings", 0),
429 m_option_working_dir(LLDB_OPT_SET_1, false, "working-dir", 'w', 0,
431 "The working directory for the platform.") {
432 m_options.Append(&m_option_working_dir, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
435 ~CommandObjectPlatformSettings() override = default;
438 bool DoExecute(Args &args, CommandReturnObject &result) override {
439 PlatformSP platform_sp(
440 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
442 if (m_option_working_dir.GetOptionValue().OptionWasSet())
443 platform_sp->SetWorkingDirectory(
444 m_option_working_dir.GetOptionValue().GetCurrentValue());
446 result.AppendError("no platform is currently selected");
447 result.SetStatus(eReturnStatusFailed);
449 return result.Succeeded();
452 Options *GetOptions() override {
453 if (!m_options.DidFinalize())
454 m_options.Finalize();
459 OptionGroupOptions m_options;
460 OptionGroupFile m_option_working_dir;
463 //----------------------------------------------------------------------
465 //----------------------------------------------------------------------
466 class CommandObjectPlatformMkDir : public CommandObjectParsed {
468 CommandObjectPlatformMkDir(CommandInterpreter &interpreter)
469 : CommandObjectParsed(interpreter, "platform mkdir",
470 "Make a new directory on the remote end.", nullptr,
474 ~CommandObjectPlatformMkDir() override = default;
476 bool DoExecute(Args &args, CommandReturnObject &result) override {
477 PlatformSP platform_sp(
478 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
480 std::string cmd_line;
481 args.GetCommandString(cmd_line);
483 const OptionPermissions *options_permissions =
484 (const OptionPermissions *)m_options.GetGroupWithOption('r');
485 if (options_permissions)
486 mode = options_permissions->m_permissions;
488 mode = lldb::eFilePermissionsUserRWX | lldb::eFilePermissionsGroupRWX |
489 lldb::eFilePermissionsWorldRX;
490 Status error = platform_sp->MakeDirectory(FileSpec(cmd_line), mode);
491 if (error.Success()) {
492 result.SetStatus(eReturnStatusSuccessFinishResult);
494 result.AppendError(error.AsCString());
495 result.SetStatus(eReturnStatusFailed);
498 result.AppendError("no platform currently selected\n");
499 result.SetStatus(eReturnStatusFailed);
501 return result.Succeeded();
504 Options *GetOptions() override {
505 if (!m_options.DidFinalize()) {
506 m_options.Append(new OptionPermissions());
507 m_options.Finalize();
512 OptionGroupOptions m_options;
515 //----------------------------------------------------------------------
517 //----------------------------------------------------------------------
518 class CommandObjectPlatformFOpen : public CommandObjectParsed {
520 CommandObjectPlatformFOpen(CommandInterpreter &interpreter)
521 : CommandObjectParsed(interpreter, "platform file open",
522 "Open a file on the remote end.", nullptr, 0),
525 ~CommandObjectPlatformFOpen() override = default;
527 bool DoExecute(Args &args, CommandReturnObject &result) override {
528 PlatformSP platform_sp(
529 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
532 std::string cmd_line;
533 args.GetCommandString(cmd_line);
535 const OptionPermissions *options_permissions =
536 (const OptionPermissions *)m_options.GetGroupWithOption('r');
537 if (options_permissions)
538 perms = options_permissions->m_permissions;
540 perms = lldb::eFilePermissionsUserRW | lldb::eFilePermissionsGroupRW |
541 lldb::eFilePermissionsWorldRead;
542 lldb::user_id_t fd = platform_sp->OpenFile(
544 File::eOpenOptionRead | File::eOpenOptionWrite |
545 File::eOpenOptionAppend | File::eOpenOptionCanCreate,
547 if (error.Success()) {
548 result.AppendMessageWithFormat("File Descriptor = %" PRIu64 "\n", fd);
549 result.SetStatus(eReturnStatusSuccessFinishResult);
551 result.AppendError(error.AsCString());
552 result.SetStatus(eReturnStatusFailed);
555 result.AppendError("no platform currently selected\n");
556 result.SetStatus(eReturnStatusFailed);
558 return result.Succeeded();
561 Options *GetOptions() override {
562 if (!m_options.DidFinalize()) {
563 m_options.Append(new OptionPermissions());
564 m_options.Finalize();
569 OptionGroupOptions m_options;
572 //----------------------------------------------------------------------
574 //----------------------------------------------------------------------
575 class CommandObjectPlatformFClose : public CommandObjectParsed {
577 CommandObjectPlatformFClose(CommandInterpreter &interpreter)
578 : CommandObjectParsed(interpreter, "platform file close",
579 "Close a file on the remote end.", nullptr, 0) {}
581 ~CommandObjectPlatformFClose() override = default;
583 bool DoExecute(Args &args, CommandReturnObject &result) override {
584 PlatformSP platform_sp(
585 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
587 std::string cmd_line;
588 args.GetCommandString(cmd_line);
589 const lldb::user_id_t fd =
590 StringConvert::ToUInt64(cmd_line.c_str(), UINT64_MAX);
592 bool success = platform_sp->CloseFile(fd, error);
594 result.AppendMessageWithFormat("file %" PRIu64 " closed.\n", fd);
595 result.SetStatus(eReturnStatusSuccessFinishResult);
597 result.AppendError(error.AsCString());
598 result.SetStatus(eReturnStatusFailed);
601 result.AppendError("no platform currently selected\n");
602 result.SetStatus(eReturnStatusFailed);
604 return result.Succeeded();
608 //----------------------------------------------------------------------
610 //----------------------------------------------------------------------
612 static constexpr OptionDefinition g_platform_fread_options[] = {
614 { LLDB_OPT_SET_1, false, "offset", 'o', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeIndex, "Offset into the file at which to start reading." },
615 { LLDB_OPT_SET_1, false, "count", 'c', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCount, "Number of bytes to read from the file." },
619 class CommandObjectPlatformFRead : public CommandObjectParsed {
621 CommandObjectPlatformFRead(CommandInterpreter &interpreter)
622 : CommandObjectParsed(interpreter, "platform file read",
623 "Read data from a file on the remote end.", nullptr,
627 ~CommandObjectPlatformFRead() override = default;
629 bool DoExecute(Args &args, CommandReturnObject &result) override {
630 PlatformSP platform_sp(
631 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
633 std::string cmd_line;
634 args.GetCommandString(cmd_line);
635 const lldb::user_id_t fd =
636 StringConvert::ToUInt64(cmd_line.c_str(), UINT64_MAX);
637 std::string buffer(m_options.m_count, 0);
639 uint32_t retcode = platform_sp->ReadFile(
640 fd, m_options.m_offset, &buffer[0], m_options.m_count, error);
641 result.AppendMessageWithFormat("Return = %d\n", retcode);
642 result.AppendMessageWithFormat("Data = \"%s\"\n", buffer.c_str());
643 result.SetStatus(eReturnStatusSuccessFinishResult);
645 result.AppendError("no platform currently selected\n");
646 result.SetStatus(eReturnStatusFailed);
648 return result.Succeeded();
651 Options *GetOptions() override { return &m_options; }
654 class CommandOptions : public Options {
656 CommandOptions() : Options() {}
658 ~CommandOptions() override = default;
660 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
661 ExecutionContext *execution_context) override {
663 char short_option = (char)m_getopt_table[option_idx].val;
665 switch (short_option) {
667 if (option_arg.getAsInteger(0, m_offset))
668 error.SetErrorStringWithFormat("invalid offset: '%s'",
669 option_arg.str().c_str());
672 if (option_arg.getAsInteger(0, m_count))
673 error.SetErrorStringWithFormat("invalid offset: '%s'",
674 option_arg.str().c_str());
677 error.SetErrorStringWithFormat("unrecognized option '%c'",
685 void OptionParsingStarting(ExecutionContext *execution_context) override {
690 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
691 return llvm::makeArrayRef(g_platform_fread_options);
694 // Instance variables to hold the values for command options.
700 CommandOptions m_options;
703 //----------------------------------------------------------------------
705 //----------------------------------------------------------------------
707 static constexpr OptionDefinition g_platform_fwrite_options[] = {
709 { LLDB_OPT_SET_1, false, "offset", 'o', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeIndex, "Offset into the file at which to start reading." },
710 { LLDB_OPT_SET_1, false, "data", 'd', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeValue, "Text to write to the file." },
714 class CommandObjectPlatformFWrite : public CommandObjectParsed {
716 CommandObjectPlatformFWrite(CommandInterpreter &interpreter)
717 : CommandObjectParsed(interpreter, "platform file write",
718 "Write data to a file on the remote end.", nullptr,
722 ~CommandObjectPlatformFWrite() override = default;
724 bool DoExecute(Args &args, CommandReturnObject &result) override {
725 PlatformSP platform_sp(
726 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
728 std::string cmd_line;
729 args.GetCommandString(cmd_line);
731 const lldb::user_id_t fd =
732 StringConvert::ToUInt64(cmd_line.c_str(), UINT64_MAX);
734 platform_sp->WriteFile(fd, m_options.m_offset, &m_options.m_data[0],
735 m_options.m_data.size(), error);
736 result.AppendMessageWithFormat("Return = %d\n", retcode);
737 result.SetStatus(eReturnStatusSuccessFinishResult);
739 result.AppendError("no platform currently selected\n");
740 result.SetStatus(eReturnStatusFailed);
742 return result.Succeeded();
745 Options *GetOptions() override { return &m_options; }
748 class CommandOptions : public Options {
750 CommandOptions() : Options() {}
752 ~CommandOptions() override = default;
754 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
755 ExecutionContext *execution_context) override {
757 char short_option = (char)m_getopt_table[option_idx].val;
759 switch (short_option) {
761 if (option_arg.getAsInteger(0, m_offset))
762 error.SetErrorStringWithFormat("invalid offset: '%s'",
763 option_arg.str().c_str());
766 m_data.assign(option_arg);
769 error.SetErrorStringWithFormat("unrecognized option '%c'",
777 void OptionParsingStarting(ExecutionContext *execution_context) override {
782 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
783 return llvm::makeArrayRef(g_platform_fwrite_options);
786 // Instance variables to hold the values for command options.
792 CommandOptions m_options;
795 class CommandObjectPlatformFile : public CommandObjectMultiword {
797 //------------------------------------------------------------------
798 // Constructors and Destructors
799 //------------------------------------------------------------------
800 CommandObjectPlatformFile(CommandInterpreter &interpreter)
801 : CommandObjectMultiword(
802 interpreter, "platform file",
803 "Commands to access files on the current platform.",
804 "platform file [open|close|read|write] ...") {
806 "open", CommandObjectSP(new CommandObjectPlatformFOpen(interpreter)));
808 "close", CommandObjectSP(new CommandObjectPlatformFClose(interpreter)));
810 "read", CommandObjectSP(new CommandObjectPlatformFRead(interpreter)));
812 "write", CommandObjectSP(new CommandObjectPlatformFWrite(interpreter)));
815 ~CommandObjectPlatformFile() override = default;
818 //------------------------------------------------------------------
819 // For CommandObjectPlatform only
820 //------------------------------------------------------------------
821 DISALLOW_COPY_AND_ASSIGN(CommandObjectPlatformFile);
824 //----------------------------------------------------------------------
825 // "platform get-file remote-file-path host-file-path"
826 //----------------------------------------------------------------------
827 class CommandObjectPlatformGetFile : public CommandObjectParsed {
829 CommandObjectPlatformGetFile(CommandInterpreter &interpreter)
830 : CommandObjectParsed(
831 interpreter, "platform get-file",
832 "Transfer a file from the remote end to the local host.",
833 "platform get-file <remote-file-spec> <local-file-spec>", 0) {
837 (lldb) platform get-file /the/remote/file/path /the/local/file/path
839 Transfer a file from the remote end with file path /the/remote/file/path to the local host.)");
841 CommandArgumentEntry arg1, arg2;
842 CommandArgumentData file_arg_remote, file_arg_host;
844 // Define the first (and only) variant of this arg.
845 file_arg_remote.arg_type = eArgTypeFilename;
846 file_arg_remote.arg_repetition = eArgRepeatPlain;
847 // There is only one variant this argument could be; put it into the
849 arg1.push_back(file_arg_remote);
851 // Define the second (and only) variant of this arg.
852 file_arg_host.arg_type = eArgTypeFilename;
853 file_arg_host.arg_repetition = eArgRepeatPlain;
854 // There is only one variant this argument could be; put it into the
856 arg2.push_back(file_arg_host);
858 // Push the data for the first and the second arguments into the
859 // m_arguments vector.
860 m_arguments.push_back(arg1);
861 m_arguments.push_back(arg2);
864 ~CommandObjectPlatformGetFile() override = default;
866 bool DoExecute(Args &args, CommandReturnObject &result) override {
867 // If the number of arguments is incorrect, issue an error message.
868 if (args.GetArgumentCount() != 2) {
869 result.GetErrorStream().Printf("error: required arguments missing; "
870 "specify both the source and destination "
872 result.SetStatus(eReturnStatusFailed);
876 PlatformSP platform_sp(
877 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
879 const char *remote_file_path = args.GetArgumentAtIndex(0);
880 const char *local_file_path = args.GetArgumentAtIndex(1);
881 Status error = platform_sp->GetFile(FileSpec(remote_file_path),
882 FileSpec(local_file_path));
883 if (error.Success()) {
884 result.AppendMessageWithFormat(
885 "successfully get-file from %s (remote) to %s (host)\n",
886 remote_file_path, local_file_path);
887 result.SetStatus(eReturnStatusSuccessFinishResult);
889 result.AppendMessageWithFormat("get-file failed: %s\n",
891 result.SetStatus(eReturnStatusFailed);
894 result.AppendError("no platform currently selected\n");
895 result.SetStatus(eReturnStatusFailed);
897 return result.Succeeded();
901 //----------------------------------------------------------------------
902 // "platform get-size remote-file-path"
903 //----------------------------------------------------------------------
904 class CommandObjectPlatformGetSize : public CommandObjectParsed {
906 CommandObjectPlatformGetSize(CommandInterpreter &interpreter)
907 : CommandObjectParsed(interpreter, "platform get-size",
908 "Get the file size from the remote end.",
909 "platform get-size <remote-file-spec>", 0) {
913 (lldb) platform get-size /the/remote/file/path
915 Get the file size from the remote end with path /the/remote/file/path.)");
917 CommandArgumentEntry arg1;
918 CommandArgumentData file_arg_remote;
920 // Define the first (and only) variant of this arg.
921 file_arg_remote.arg_type = eArgTypeFilename;
922 file_arg_remote.arg_repetition = eArgRepeatPlain;
923 // There is only one variant this argument could be; put it into the
925 arg1.push_back(file_arg_remote);
927 // Push the data for the first argument into the m_arguments vector.
928 m_arguments.push_back(arg1);
931 ~CommandObjectPlatformGetSize() override = default;
933 bool DoExecute(Args &args, CommandReturnObject &result) override {
934 // If the number of arguments is incorrect, issue an error message.
935 if (args.GetArgumentCount() != 1) {
936 result.GetErrorStream().Printf("error: required argument missing; "
937 "specify the source file path as the only "
939 result.SetStatus(eReturnStatusFailed);
943 PlatformSP platform_sp(
944 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
946 std::string remote_file_path(args.GetArgumentAtIndex(0));
947 user_id_t size = platform_sp->GetFileSize(FileSpec(remote_file_path));
948 if (size != UINT64_MAX) {
949 result.AppendMessageWithFormat("File size of %s (remote): %" PRIu64
951 remote_file_path.c_str(), size);
952 result.SetStatus(eReturnStatusSuccessFinishResult);
954 result.AppendMessageWithFormat(
955 "Error getting file size of %s (remote)\n",
956 remote_file_path.c_str());
957 result.SetStatus(eReturnStatusFailed);
960 result.AppendError("no platform currently selected\n");
961 result.SetStatus(eReturnStatusFailed);
963 return result.Succeeded();
967 //----------------------------------------------------------------------
968 // "platform put-file"
969 //----------------------------------------------------------------------
970 class CommandObjectPlatformPutFile : public CommandObjectParsed {
972 CommandObjectPlatformPutFile(CommandInterpreter &interpreter)
973 : CommandObjectParsed(
974 interpreter, "platform put-file",
975 "Transfer a file from this system to the remote end.", nullptr, 0) {
978 ~CommandObjectPlatformPutFile() override = default;
980 bool DoExecute(Args &args, CommandReturnObject &result) override {
981 const char *src = args.GetArgumentAtIndex(0);
982 const char *dst = args.GetArgumentAtIndex(1);
984 FileSpec src_fs(src);
985 FileSystem::Instance().Resolve(src_fs);
986 FileSpec dst_fs(dst ? dst : src_fs.GetFilename().GetCString());
988 PlatformSP platform_sp(
989 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
991 Status error(platform_sp->PutFile(src_fs, dst_fs));
992 if (error.Success()) {
993 result.SetStatus(eReturnStatusSuccessFinishNoResult);
995 result.AppendError(error.AsCString());
996 result.SetStatus(eReturnStatusFailed);
999 result.AppendError("no platform currently selected\n");
1000 result.SetStatus(eReturnStatusFailed);
1002 return result.Succeeded();
1006 //----------------------------------------------------------------------
1007 // "platform process launch"
1008 //----------------------------------------------------------------------
1009 class CommandObjectPlatformProcessLaunch : public CommandObjectParsed {
1011 CommandObjectPlatformProcessLaunch(CommandInterpreter &interpreter)
1012 : CommandObjectParsed(interpreter, "platform process launch",
1013 "Launch a new process on a remote platform.",
1014 "platform process launch program",
1015 eCommandRequiresTarget | eCommandTryTargetAPILock),
1018 ~CommandObjectPlatformProcessLaunch() override = default;
1020 Options *GetOptions() override { return &m_options; }
1023 bool DoExecute(Args &args, CommandReturnObject &result) override {
1024 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1025 PlatformSP platform_sp;
1027 platform_sp = target->GetPlatform();
1031 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform();
1036 const size_t argc = args.GetArgumentCount();
1037 Target *target = m_exe_ctx.GetTargetPtr();
1038 Module *exe_module = target->GetExecutableModulePointer();
1040 m_options.launch_info.GetExecutableFile() = exe_module->GetFileSpec();
1041 llvm::SmallString<128> exe_path;
1042 m_options.launch_info.GetExecutableFile().GetPath(exe_path);
1043 if (!exe_path.empty())
1044 m_options.launch_info.GetArguments().AppendArgument(exe_path);
1045 m_options.launch_info.GetArchitecture() = exe_module->GetArchitecture();
1049 if (m_options.launch_info.GetExecutableFile()) {
1050 // We already have an executable file, so we will use this and all
1051 // arguments to this function are extra arguments
1052 m_options.launch_info.GetArguments().AppendArguments(args);
1054 // We don't have any file yet, so the first argument is our
1055 // executable, and the rest are program arguments
1056 const bool first_arg_is_executable = true;
1057 m_options.launch_info.SetArguments(args, first_arg_is_executable);
1061 if (m_options.launch_info.GetExecutableFile()) {
1062 Debugger &debugger = m_interpreter.GetDebugger();
1065 target->GetRunArguments(m_options.launch_info.GetArguments());
1067 ProcessSP process_sp(platform_sp->DebugProcess(
1068 m_options.launch_info, debugger, target, error));
1069 if (process_sp && process_sp->IsAlive()) {
1070 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1074 if (error.Success())
1075 result.AppendError("process launch failed");
1077 result.AppendError(error.AsCString());
1078 result.SetStatus(eReturnStatusFailed);
1080 result.AppendError("'platform process launch' uses the current target "
1081 "file and arguments, or the executable and its "
1082 "arguments can be specified in this command");
1083 result.SetStatus(eReturnStatusFailed);
1087 result.AppendError("no platform is selected\n");
1089 return result.Succeeded();
1093 ProcessLaunchCommandOptions m_options;
1096 //----------------------------------------------------------------------
1097 // "platform process list"
1098 //----------------------------------------------------------------------
1100 static OptionDefinition g_platform_process_list_options[] = {
1102 { LLDB_OPT_SET_1, false, "pid", 'p', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePid, "List the process info for a specific process ID." },
1103 { LLDB_OPT_SET_2, true, "name", 'n', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeProcessName, "Find processes with executable basenames that match a string." },
1104 { LLDB_OPT_SET_3, true, "ends-with", 'e', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeProcessName, "Find processes with executable basenames that end with a string." },
1105 { LLDB_OPT_SET_4, true, "starts-with", 's', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeProcessName, "Find processes with executable basenames that start with a string." },
1106 { LLDB_OPT_SET_5, true, "contains", 'c', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeProcessName, "Find processes with executable basenames that contain a string." },
1107 { LLDB_OPT_SET_6, true, "regex", 'r', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeRegularExpression, "Find processes with executable basenames that match a regular expression." },
1108 { LLDB_OPT_SET_FROM_TO(2, 6), false, "parent", 'P', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePid, "Find processes that have a matching parent process ID." },
1109 { LLDB_OPT_SET_FROM_TO(2, 6), false, "uid", 'u', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeUnsignedInteger, "Find processes that have a matching user ID." },
1110 { LLDB_OPT_SET_FROM_TO(2, 6), false, "euid", 'U', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeUnsignedInteger, "Find processes that have a matching effective user ID." },
1111 { LLDB_OPT_SET_FROM_TO(2, 6), false, "gid", 'g', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeUnsignedInteger, "Find processes that have a matching group ID." },
1112 { LLDB_OPT_SET_FROM_TO(2, 6), false, "egid", 'G', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeUnsignedInteger, "Find processes that have a matching effective group ID." },
1113 { LLDB_OPT_SET_FROM_TO(2, 6), false, "arch", 'a', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeArchitecture, "Find processes that have a matching architecture." },
1114 { LLDB_OPT_SET_FROM_TO(1, 6), false, "show-args", 'A', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Show process arguments instead of the process executable basename." },
1115 { LLDB_OPT_SET_FROM_TO(1, 6), false, "verbose", 'v', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Enable verbose output." },
1119 class CommandObjectPlatformProcessList : public CommandObjectParsed {
1121 CommandObjectPlatformProcessList(CommandInterpreter &interpreter)
1122 : CommandObjectParsed(interpreter, "platform process list",
1123 "List processes on a remote platform by name, pid, "
1124 "or many other matching attributes.",
1125 "platform process list", 0),
1128 ~CommandObjectPlatformProcessList() override = default;
1130 Options *GetOptions() override { return &m_options; }
1133 bool DoExecute(Args &args, CommandReturnObject &result) override {
1134 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1135 PlatformSP platform_sp;
1137 platform_sp = target->GetPlatform();
1141 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform();
1146 if (args.GetArgumentCount() == 0) {
1148 Stream &ostrm = result.GetOutputStream();
1151 m_options.match_info.GetProcessInfo().GetProcessID();
1152 if (pid != LLDB_INVALID_PROCESS_ID) {
1153 ProcessInstanceInfo proc_info;
1154 if (platform_sp->GetProcessInfo(pid, proc_info)) {
1155 ProcessInstanceInfo::DumpTableHeader(ostrm, platform_sp.get(),
1156 m_options.show_args,
1158 proc_info.DumpAsTableRow(ostrm, platform_sp.get(),
1159 m_options.show_args, m_options.verbose);
1160 result.SetStatus(eReturnStatusSuccessFinishResult);
1162 result.AppendErrorWithFormat(
1163 "no process found with pid = %" PRIu64 "\n", pid);
1164 result.SetStatus(eReturnStatusFailed);
1167 ProcessInstanceInfoList proc_infos;
1168 const uint32_t matches =
1169 platform_sp->FindProcesses(m_options.match_info, proc_infos);
1170 const char *match_desc = nullptr;
1171 const char *match_name =
1172 m_options.match_info.GetProcessInfo().GetName();
1173 if (match_name && match_name[0]) {
1174 switch (m_options.match_info.GetNameMatchType()) {
1175 case NameMatch::Ignore:
1177 case NameMatch::Equals:
1178 match_desc = "matched";
1180 case NameMatch::Contains:
1181 match_desc = "contained";
1183 case NameMatch::StartsWith:
1184 match_desc = "started with";
1186 case NameMatch::EndsWith:
1187 match_desc = "ended with";
1189 case NameMatch::RegularExpression:
1190 match_desc = "matched the regular expression";
1197 result.AppendErrorWithFormat(
1198 "no processes were found that %s \"%s\" on the \"%s\" "
1200 match_desc, match_name,
1201 platform_sp->GetPluginName().GetCString());
1203 result.AppendErrorWithFormat(
1204 "no processes were found on the \"%s\" platform\n",
1205 platform_sp->GetPluginName().GetCString());
1206 result.SetStatus(eReturnStatusFailed);
1208 result.AppendMessageWithFormat(
1209 "%u matching process%s found on \"%s\"", matches,
1210 matches > 1 ? "es were" : " was",
1211 platform_sp->GetName().GetCString());
1213 result.AppendMessageWithFormat(" whose name %s \"%s\"",
1214 match_desc, match_name);
1215 result.AppendMessageWithFormat("\n");
1216 ProcessInstanceInfo::DumpTableHeader(ostrm, platform_sp.get(),
1217 m_options.show_args,
1219 for (uint32_t i = 0; i < matches; ++i) {
1220 proc_infos.GetProcessInfoAtIndex(i).DumpAsTableRow(
1221 ostrm, platform_sp.get(), m_options.show_args,
1228 result.AppendError("invalid args: process list takes only options\n");
1229 result.SetStatus(eReturnStatusFailed);
1232 result.AppendError("no platform is selected\n");
1233 result.SetStatus(eReturnStatusFailed);
1235 return result.Succeeded();
1238 class CommandOptions : public Options {
1241 : Options(), match_info(), show_args(false), verbose(false) {
1242 static llvm::once_flag g_once_flag;
1243 llvm::call_once(g_once_flag, []() {
1244 PosixPlatformCommandOptionValidator *posix_validator =
1245 new PosixPlatformCommandOptionValidator();
1246 for (auto &Option : g_platform_process_list_options) {
1247 switch (Option.short_option) {
1252 Option.validator = posix_validator;
1261 ~CommandOptions() override = default;
1263 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1264 ExecutionContext *execution_context) override {
1266 const int short_option = m_getopt_table[option_idx].val;
1267 bool success = false;
1269 uint32_t id = LLDB_INVALID_PROCESS_ID;
1270 success = !option_arg.getAsInteger(0, id);
1271 switch (short_option) {
1273 match_info.GetProcessInfo().SetProcessID(id);
1275 error.SetErrorStringWithFormat("invalid process ID string: '%s'",
1276 option_arg.str().c_str());
1280 match_info.GetProcessInfo().SetParentProcessID(id);
1282 error.SetErrorStringWithFormat(
1283 "invalid parent process ID string: '%s'",
1284 option_arg.str().c_str());
1288 match_info.GetProcessInfo().SetUserID(success ? id : UINT32_MAX);
1290 error.SetErrorStringWithFormat("invalid user ID string: '%s'",
1291 option_arg.str().c_str());
1295 match_info.GetProcessInfo().SetEffectiveUserID(success ? id
1298 error.SetErrorStringWithFormat(
1299 "invalid effective user ID string: '%s'",
1300 option_arg.str().c_str());
1304 match_info.GetProcessInfo().SetGroupID(success ? id : UINT32_MAX);
1306 error.SetErrorStringWithFormat("invalid group ID string: '%s'",
1307 option_arg.str().c_str());
1311 match_info.GetProcessInfo().SetEffectiveGroupID(success ? id
1314 error.SetErrorStringWithFormat(
1315 "invalid effective group ID string: '%s'",
1316 option_arg.str().c_str());
1320 TargetSP target_sp =
1321 execution_context ? execution_context->GetTargetSP() : TargetSP();
1322 DebuggerSP debugger_sp =
1323 target_sp ? target_sp->GetDebugger().shared_from_this()
1325 PlatformSP platform_sp =
1326 debugger_sp ? debugger_sp->GetPlatformList().GetSelectedPlatform()
1328 match_info.GetProcessInfo().GetArchitecture() =
1329 Platform::GetAugmentedArchSpec(platform_sp.get(), option_arg);
1333 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1334 option_arg, FileSpec::Style::native);
1335 match_info.SetNameMatchType(NameMatch::Equals);
1339 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1340 option_arg, FileSpec::Style::native);
1341 match_info.SetNameMatchType(NameMatch::EndsWith);
1345 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1346 option_arg, FileSpec::Style::native);
1347 match_info.SetNameMatchType(NameMatch::StartsWith);
1351 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1352 option_arg, FileSpec::Style::native);
1353 match_info.SetNameMatchType(NameMatch::Contains);
1357 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1358 option_arg, FileSpec::Style::native);
1359 match_info.SetNameMatchType(NameMatch::RegularExpression);
1371 error.SetErrorStringWithFormat("unrecognized option '%c'",
1379 void OptionParsingStarting(ExecutionContext *execution_context) override {
1385 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1386 return llvm::makeArrayRef(g_platform_process_list_options);
1389 // Instance variables to hold the values for command options.
1391 ProcessInstanceInfoMatch match_info;
1396 CommandOptions m_options;
1399 //----------------------------------------------------------------------
1400 // "platform process info"
1401 //----------------------------------------------------------------------
1402 class CommandObjectPlatformProcessInfo : public CommandObjectParsed {
1404 CommandObjectPlatformProcessInfo(CommandInterpreter &interpreter)
1405 : CommandObjectParsed(
1406 interpreter, "platform process info",
1407 "Get detailed information for one or more process by process ID.",
1408 "platform process info <pid> [<pid> <pid> ...]", 0) {
1409 CommandArgumentEntry arg;
1410 CommandArgumentData pid_args;
1412 // Define the first (and only) variant of this arg.
1413 pid_args.arg_type = eArgTypePid;
1414 pid_args.arg_repetition = eArgRepeatStar;
1416 // There is only one variant this argument could be; put it into the
1418 arg.push_back(pid_args);
1420 // Push the data for the first argument into the m_arguments vector.
1421 m_arguments.push_back(arg);
1424 ~CommandObjectPlatformProcessInfo() override = default;
1427 bool DoExecute(Args &args, CommandReturnObject &result) override {
1428 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1429 PlatformSP platform_sp;
1431 platform_sp = target->GetPlatform();
1435 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform();
1439 const size_t argc = args.GetArgumentCount();
1443 if (platform_sp->IsConnected()) {
1444 Stream &ostrm = result.GetOutputStream();
1445 for (auto &entry : args.entries()) {
1447 if (entry.ref.getAsInteger(0, pid)) {
1448 result.AppendErrorWithFormat("invalid process ID argument '%s'",
1449 entry.ref.str().c_str());
1450 result.SetStatus(eReturnStatusFailed);
1453 ProcessInstanceInfo proc_info;
1454 if (platform_sp->GetProcessInfo(pid, proc_info)) {
1455 ostrm.Printf("Process information for process %" PRIu64 ":\n",
1457 proc_info.Dump(ostrm, platform_sp.get());
1459 ostrm.Printf("error: no process information is available for "
1460 "process %" PRIu64 "\n",
1468 result.AppendErrorWithFormat(
1469 "not connected to '%s'",
1470 platform_sp->GetPluginName().GetCString());
1471 result.SetStatus(eReturnStatusFailed);
1475 result.AppendError("one or more process id(s) must be specified");
1476 result.SetStatus(eReturnStatusFailed);
1479 result.AppendError("no platform is currently selected");
1480 result.SetStatus(eReturnStatusFailed);
1482 return result.Succeeded();
1486 static constexpr OptionDefinition g_platform_process_attach_options[] = {
1488 { LLDB_OPT_SET_ALL, false, "plugin", 'P', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePlugin, "Name of the process plugin you want to use." },
1489 { LLDB_OPT_SET_1, false, "pid", 'p', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePid, "The process ID of an existing process to attach to." },
1490 { LLDB_OPT_SET_2, false, "name", 'n', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeProcessName, "The name of the process to attach to." },
1491 { LLDB_OPT_SET_2, false, "waitfor", 'w', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Wait for the process with <process-name> to launch." },
1495 class CommandObjectPlatformProcessAttach : public CommandObjectParsed {
1497 class CommandOptions : public Options {
1499 CommandOptions() : Options() {
1500 // Keep default values of all options in one place: OptionParsingStarting
1502 OptionParsingStarting(nullptr);
1505 ~CommandOptions() override = default;
1507 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1508 ExecutionContext *execution_context) override {
1510 char short_option = (char)m_getopt_table[option_idx].val;
1511 switch (short_option) {
1513 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
1514 if (option_arg.getAsInteger(0, pid)) {
1515 error.SetErrorStringWithFormat("invalid process ID '%s'",
1516 option_arg.str().c_str());
1518 attach_info.SetProcessID(pid);
1523 attach_info.SetProcessPluginName(option_arg);
1527 attach_info.GetExecutableFile().SetFile(option_arg,
1528 FileSpec::Style::native);
1532 attach_info.SetWaitForLaunch(true);
1536 error.SetErrorStringWithFormat("invalid short option character '%c'",
1543 void OptionParsingStarting(ExecutionContext *execution_context) override {
1544 attach_info.Clear();
1547 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1548 return llvm::makeArrayRef(g_platform_process_attach_options);
1551 bool HandleOptionArgumentCompletion(
1552 CompletionRequest &request, OptionElementVector &opt_element_vector,
1553 int opt_element_index, CommandInterpreter &interpreter) override {
1554 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
1555 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
1557 // We are only completing the name option for now...
1559 if (GetDefinitions()[opt_defs_index].short_option == 'n') {
1560 // Are we in the name?
1562 // Look to see if there is a -P argument provided, and if so use that
1563 // plugin, otherwise use the default plugin.
1565 const char *partial_name = nullptr;
1566 partial_name = request.GetParsedLine().GetArgumentAtIndex(opt_arg_pos);
1568 PlatformSP platform_sp(interpreter.GetPlatform(true));
1570 ProcessInstanceInfoList process_infos;
1571 ProcessInstanceInfoMatch match_info;
1573 match_info.GetProcessInfo().GetExecutableFile().SetFile(
1574 partial_name, FileSpec::Style::native);
1575 match_info.SetNameMatchType(NameMatch::StartsWith);
1577 platform_sp->FindProcesses(match_info, process_infos);
1578 const uint32_t num_matches = process_infos.GetSize();
1579 if (num_matches > 0) {
1580 for (uint32_t i = 0; i < num_matches; ++i) {
1581 request.AddCompletion(llvm::StringRef(
1582 process_infos.GetProcessNameAtIndex(i),
1583 process_infos.GetProcessNameLengthAtIndex(i)));
1592 // Options table: Required for subclasses of Options.
1594 static OptionDefinition g_option_table[];
1596 // Instance variables to hold the values for command options.
1598 ProcessAttachInfo attach_info;
1601 CommandObjectPlatformProcessAttach(CommandInterpreter &interpreter)
1602 : CommandObjectParsed(interpreter, "platform process attach",
1603 "Attach to a process.",
1604 "platform process attach <cmd-options>"),
1607 ~CommandObjectPlatformProcessAttach() override = default;
1609 bool DoExecute(Args &command, CommandReturnObject &result) override {
1610 PlatformSP platform_sp(
1611 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
1614 ProcessSP remote_process_sp = platform_sp->Attach(
1615 m_options.attach_info, m_interpreter.GetDebugger(), nullptr, err);
1617 result.AppendError(err.AsCString());
1618 result.SetStatus(eReturnStatusFailed);
1619 } else if (!remote_process_sp) {
1620 result.AppendError("could not attach: unknown reason");
1621 result.SetStatus(eReturnStatusFailed);
1623 result.SetStatus(eReturnStatusSuccessFinishResult);
1625 result.AppendError("no platform is currently selected");
1626 result.SetStatus(eReturnStatusFailed);
1628 return result.Succeeded();
1631 Options *GetOptions() override { return &m_options; }
1634 CommandOptions m_options;
1637 class CommandObjectPlatformProcess : public CommandObjectMultiword {
1639 //------------------------------------------------------------------
1640 // Constructors and Destructors
1641 //------------------------------------------------------------------
1642 CommandObjectPlatformProcess(CommandInterpreter &interpreter)
1643 : CommandObjectMultiword(interpreter, "platform process",
1644 "Commands to query, launch and attach to "
1645 "processes on the current platform.",
1646 "platform process [attach|launch|list] ...") {
1649 CommandObjectSP(new CommandObjectPlatformProcessAttach(interpreter)));
1652 CommandObjectSP(new CommandObjectPlatformProcessLaunch(interpreter)));
1653 LoadSubCommand("info", CommandObjectSP(new CommandObjectPlatformProcessInfo(
1655 LoadSubCommand("list", CommandObjectSP(new CommandObjectPlatformProcessList(
1659 ~CommandObjectPlatformProcess() override = default;
1662 //------------------------------------------------------------------
1663 // For CommandObjectPlatform only
1664 //------------------------------------------------------------------
1665 DISALLOW_COPY_AND_ASSIGN(CommandObjectPlatformProcess);
1668 //----------------------------------------------------------------------
1670 //----------------------------------------------------------------------
1671 static constexpr OptionDefinition g_platform_shell_options[] = {
1673 { LLDB_OPT_SET_ALL, false, "timeout", 't', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeValue, "Seconds to wait for the remote host to finish running the command." },
1677 class CommandObjectPlatformShell : public CommandObjectRaw {
1679 class CommandOptions : public Options {
1681 CommandOptions() : Options() {}
1683 ~CommandOptions() override = default;
1685 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1686 return llvm::makeArrayRef(g_platform_shell_options);
1689 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1690 ExecutionContext *execution_context) override {
1693 const char short_option = (char)GetDefinitions()[option_idx].short_option;
1695 switch (short_option) {
1697 uint32_t timeout_sec;
1698 if (option_arg.getAsInteger(10, timeout_sec))
1699 error.SetErrorStringWithFormat(
1700 "could not convert \"%s\" to a numeric value.",
1701 option_arg.str().c_str());
1703 timeout = std::chrono::seconds(timeout_sec);
1706 error.SetErrorStringWithFormat("invalid short option character '%c'",
1714 void OptionParsingStarting(ExecutionContext *execution_context) override {}
1716 Timeout<std::micro> timeout = std::chrono::seconds(10);
1719 CommandObjectPlatformShell(CommandInterpreter &interpreter)
1720 : CommandObjectRaw(interpreter, "platform shell",
1721 "Run a shell command on the current platform.",
1722 "platform shell <shell-command>", 0),
1725 ~CommandObjectPlatformShell() override = default;
1727 Options *GetOptions() override { return &m_options; }
1729 bool DoExecute(llvm::StringRef raw_command_line,
1730 CommandReturnObject &result) override {
1731 ExecutionContext exe_ctx = GetCommandInterpreter().GetExecutionContext();
1732 m_options.NotifyOptionParsingStarting(&exe_ctx);
1735 // Print out an usage syntax on an empty command line.
1736 if (raw_command_line.empty()) {
1737 result.GetOutputStream().Printf("%s\n", this->GetSyntax().str().c_str());
1741 OptionsWithRaw args(raw_command_line);
1742 const char *expr = args.GetRawPart().c_str();
1745 if (!ParseOptions(args.GetArgs(), result))
1748 PlatformSP platform_sp(
1749 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
1752 FileSpec working_dir{};
1756 error = (platform_sp->RunShellCommand(expr, working_dir, &status, &signo,
1757 &output, m_options.timeout));
1758 if (!output.empty())
1759 result.GetOutputStream().PutCString(output);
1762 const char *signo_cstr = Host::GetSignalAsCString(signo);
1764 result.GetOutputStream().Printf(
1765 "error: command returned with status %i and signal %s\n",
1766 status, signo_cstr);
1768 result.GetOutputStream().Printf(
1769 "error: command returned with status %i and signal %i\n",
1772 result.GetOutputStream().Printf(
1773 "error: command returned with status %i\n", status);
1776 result.GetOutputStream().Printf(
1777 "error: cannot run remote shell commands without a platform\n");
1778 error.SetErrorString(
1779 "error: cannot run remote shell commands without a platform");
1783 result.AppendError(error.AsCString());
1784 result.SetStatus(eReturnStatusFailed);
1786 result.SetStatus(eReturnStatusSuccessFinishResult);
1791 CommandOptions m_options;
1794 //----------------------------------------------------------------------
1795 // "platform install" - install a target to a remote end
1796 //----------------------------------------------------------------------
1797 class CommandObjectPlatformInstall : public CommandObjectParsed {
1799 CommandObjectPlatformInstall(CommandInterpreter &interpreter)
1800 : CommandObjectParsed(
1801 interpreter, "platform target-install",
1802 "Install a target (bundle or executable file) to the remote end.",
1803 "platform target-install <local-thing> <remote-sandbox>", 0) {}
1805 ~CommandObjectPlatformInstall() override = default;
1807 bool DoExecute(Args &args, CommandReturnObject &result) override {
1808 if (args.GetArgumentCount() != 2) {
1809 result.AppendError("platform target-install takes two arguments");
1810 result.SetStatus(eReturnStatusFailed);
1813 // TODO: move the bulk of this code over to the platform itself
1814 FileSpec src(args.GetArgumentAtIndex(0));
1815 FileSystem::Instance().Resolve(src);
1816 FileSpec dst(args.GetArgumentAtIndex(1));
1817 if (!FileSystem::Instance().Exists(src)) {
1818 result.AppendError("source location does not exist or is not accessible");
1819 result.SetStatus(eReturnStatusFailed);
1822 PlatformSP platform_sp(
1823 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
1825 result.AppendError("no platform currently selected");
1826 result.SetStatus(eReturnStatusFailed);
1830 Status error = platform_sp->Install(src, dst);
1831 if (error.Success()) {
1832 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1834 result.AppendErrorWithFormat("install failed: %s", error.AsCString());
1835 result.SetStatus(eReturnStatusFailed);
1837 return result.Succeeded();
1841 CommandObjectPlatform::CommandObjectPlatform(CommandInterpreter &interpreter)
1842 : CommandObjectMultiword(
1843 interpreter, "platform", "Commands to manage and create platforms.",
1844 "platform [connect|disconnect|info|list|status|select] ...") {
1845 LoadSubCommand("select",
1846 CommandObjectSP(new CommandObjectPlatformSelect(interpreter)));
1847 LoadSubCommand("list",
1848 CommandObjectSP(new CommandObjectPlatformList(interpreter)));
1849 LoadSubCommand("status",
1850 CommandObjectSP(new CommandObjectPlatformStatus(interpreter)));
1851 LoadSubCommand("connect", CommandObjectSP(
1852 new CommandObjectPlatformConnect(interpreter)));
1855 CommandObjectSP(new CommandObjectPlatformDisconnect(interpreter)));
1856 LoadSubCommand("settings", CommandObjectSP(new CommandObjectPlatformSettings(
1858 LoadSubCommand("mkdir",
1859 CommandObjectSP(new CommandObjectPlatformMkDir(interpreter)));
1860 LoadSubCommand("file",
1861 CommandObjectSP(new CommandObjectPlatformFile(interpreter)));
1862 LoadSubCommand("get-file", CommandObjectSP(new CommandObjectPlatformGetFile(
1864 LoadSubCommand("get-size", CommandObjectSP(new CommandObjectPlatformGetSize(
1866 LoadSubCommand("put-file", CommandObjectSP(new CommandObjectPlatformPutFile(
1868 LoadSubCommand("process", CommandObjectSP(
1869 new CommandObjectPlatformProcess(interpreter)));
1870 LoadSubCommand("shell",
1871 CommandObjectSP(new CommandObjectPlatformShell(interpreter)));
1874 CommandObjectSP(new CommandObjectPlatformInstall(interpreter)));
1877 CommandObjectPlatform::~CommandObjectPlatform() = default;