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 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "CommandObjectPlatform.h"
16 // Other libraries and framework includes
18 #include "lldb/Core/DataExtractor.h"
19 #include "lldb/Core/Debugger.h"
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/PluginManager.h"
22 #include "lldb/Interpreter/Args.h"
23 #include "lldb/Interpreter/CommandInterpreter.h"
24 #include "lldb/Interpreter/CommandReturnObject.h"
25 #include "lldb/Interpreter/OptionGroupFile.h"
26 #include "lldb/Interpreter/OptionGroupPlatform.h"
27 #include "lldb/Target/ExecutionContext.h"
28 #include "lldb/Target/Platform.h"
29 #include "lldb/Target/Process.h"
30 #include "lldb/Utility/Utils.h"
33 using namespace lldb_private;
36 ParsePermissionString(const char* permissions)
38 if (strlen(permissions) != 9)
40 bool user_r,user_w,user_x,
41 group_r,group_w,group_x,
42 world_r,world_w,world_x;
44 user_r = (permissions[0] == 'r');
45 user_w = (permissions[1] == 'w');
46 user_x = (permissions[2] == 'x');
48 group_r = (permissions[3] == 'r');
49 group_w = (permissions[4] == 'w');
50 group_x = (permissions[5] == 'x');
52 world_r = (permissions[6] == 'r');
53 world_w = (permissions[7] == 'w');
54 world_x = (permissions[8] == 'x');
56 mode_t user,group,world;
57 user = (user_r ? 4 : 0) | (user_w ? 2 : 0) | (user_x ? 1 : 0);
58 group = (group_r ? 4 : 0) | (group_w ? 2 : 0) | (group_x ? 1 : 0);
59 world = (world_r ? 4 : 0) | (world_w ? 2 : 0) | (world_x ? 1 : 0);
61 return user | group | world;
64 static OptionDefinition
65 g_permissions_options[] =
67 { LLDB_OPT_SET_ALL, false, "permissions-value", 'v', OptionParser::eRequiredArgument, NULL, 0, eArgTypePermissionsNumber , "Give out the numeric value for permissions (e.g. 757)" },
68 { LLDB_OPT_SET_ALL, false, "permissions-string",'s', OptionParser::eRequiredArgument, NULL, 0, eArgTypePermissionsString , "Give out the string value for permissions (e.g. rwxr-xr--)." },
69 { LLDB_OPT_SET_ALL, false, "user-read", 'r', OptionParser::eNoArgument, NULL, 0, eArgTypeNone , "Allow user to read." },
70 { LLDB_OPT_SET_ALL, false, "user-write", 'w', OptionParser::eNoArgument, NULL, 0, eArgTypeNone , "Allow user to write." },
71 { LLDB_OPT_SET_ALL, false, "user-exec", 'x', OptionParser::eNoArgument, NULL, 0, eArgTypeNone , "Allow user to execute." },
73 { LLDB_OPT_SET_ALL, false, "group-read", 'R', OptionParser::eNoArgument, NULL, 0, eArgTypeNone , "Allow group to read." },
74 { LLDB_OPT_SET_ALL, false, "group-write", 'W', OptionParser::eNoArgument, NULL, 0, eArgTypeNone , "Allow group to write." },
75 { LLDB_OPT_SET_ALL, false, "group-exec", 'X', OptionParser::eNoArgument, NULL, 0, eArgTypeNone , "Allow group to execute." },
77 { LLDB_OPT_SET_ALL, false, "world-read", 'd', OptionParser::eNoArgument, NULL, 0, eArgTypeNone , "Allow world to read." },
78 { LLDB_OPT_SET_ALL, false, "world-write", 't', OptionParser::eNoArgument, NULL, 0, eArgTypeNone , "Allow world to write." },
79 { LLDB_OPT_SET_ALL, false, "world-exec", 'e', OptionParser::eNoArgument, NULL, 0, eArgTypeNone , "Allow world to execute." },
83 class OptionPermissions : public lldb_private::OptionGroup
95 virtual lldb_private::Error
96 SetOptionValue (CommandInterpreter &interpreter,
98 const char *option_arg)
101 char short_option = (char) GetDefinitions()[option_idx].short_option;
102 switch (short_option)
107 uint32_t perms = Args::StringToUInt32(option_arg, 777, 8, &ok);
109 error.SetErrorStringWithFormat("invalid value for permissions: %s", option_arg);
111 m_permissions = perms;
116 mode_t perms = ParsePermissionString(option_arg);
117 if (perms == (mode_t)-1)
118 error.SetErrorStringWithFormat("invalid value for permissions: %s", option_arg);
120 m_permissions = perms;
123 m_permissions |= lldb::eFilePermissionsUserRead;
126 m_permissions |= lldb::eFilePermissionsUserWrite;
129 m_permissions |= lldb::eFilePermissionsUserExecute;
132 m_permissions |= lldb::eFilePermissionsGroupRead;
135 m_permissions |= lldb::eFilePermissionsGroupWrite;
138 m_permissions |= lldb::eFilePermissionsGroupExecute;
141 m_permissions |= lldb::eFilePermissionsWorldRead;
144 m_permissions |= lldb::eFilePermissionsWorldWrite;
147 m_permissions |= lldb::eFilePermissionsWorldExecute;
151 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
159 OptionParsingStarting (CommandInterpreter &interpreter)
167 return llvm::array_lengthof(g_permissions_options);
170 const lldb_private::OptionDefinition*
173 return g_permissions_options;
176 // Instance variables to hold the values for command options.
178 uint32_t m_permissions;
180 DISALLOW_COPY_AND_ASSIGN(OptionPermissions);
183 //----------------------------------------------------------------------
184 // "platform select <platform-name>"
185 //----------------------------------------------------------------------
186 class CommandObjectPlatformSelect : public CommandObjectParsed
189 CommandObjectPlatformSelect (CommandInterpreter &interpreter) :
190 CommandObjectParsed (interpreter,
192 "Create a platform if needed and select it as the current platform.",
193 "platform select <platform-name>",
195 m_option_group (interpreter),
196 m_platform_options (false) // Don't include the "--platform" option by passing false
198 m_option_group.Append (&m_platform_options, LLDB_OPT_SET_ALL, 1);
199 m_option_group.Finalize();
203 ~CommandObjectPlatformSelect ()
208 HandleCompletion (Args &input,
210 int &cursor_char_position,
211 int match_start_point,
212 int max_return_elements,
216 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
217 completion_str.erase (cursor_char_position);
219 CommandCompletions::PlatformPluginNames (m_interpreter,
220 completion_str.c_str(),
226 return matches.GetSize();
232 return &m_option_group;
237 DoExecute (Args& args, CommandReturnObject &result)
239 if (args.GetArgumentCount() == 1)
241 const char *platform_name = args.GetArgumentAtIndex (0);
242 if (platform_name && platform_name[0])
244 const bool select = true;
245 m_platform_options.SetPlatformName (platform_name);
247 ArchSpec platform_arch;
248 PlatformSP platform_sp (m_platform_options.CreatePlatformWithOptions (m_interpreter, ArchSpec(), select, error, platform_arch));
251 platform_sp->GetStatus (result.GetOutputStream());
252 result.SetStatus (eReturnStatusSuccessFinishResult);
256 result.AppendError(error.AsCString());
257 result.SetStatus (eReturnStatusFailed);
262 result.AppendError ("invalid platform name");
263 result.SetStatus (eReturnStatusFailed);
268 result.AppendError ("platform create takes a platform name as an argument\n");
269 result.SetStatus (eReturnStatusFailed);
271 return result.Succeeded();
274 OptionGroupOptions m_option_group;
275 OptionGroupPlatform m_platform_options;
278 //----------------------------------------------------------------------
280 //----------------------------------------------------------------------
281 class CommandObjectPlatformList : public CommandObjectParsed
284 CommandObjectPlatformList (CommandInterpreter &interpreter) :
285 CommandObjectParsed (interpreter,
287 "List all platforms that are available.",
294 ~CommandObjectPlatformList ()
300 DoExecute (Args& args, CommandReturnObject &result)
302 Stream &ostrm = result.GetOutputStream();
303 ostrm.Printf("Available platforms:\n");
305 PlatformSP host_platform_sp (Platform::GetDefaultPlatform());
306 ostrm.Printf ("%s: %s\n",
307 host_platform_sp->GetPluginName().GetCString(),
308 host_platform_sp->GetDescription());
311 for (idx = 0; 1; ++idx)
313 const char *plugin_name = PluginManager::GetPlatformPluginNameAtIndex (idx);
314 if (plugin_name == NULL)
316 const char *plugin_desc = PluginManager::GetPlatformPluginDescriptionAtIndex (idx);
317 if (plugin_desc == NULL)
319 ostrm.Printf("%s: %s\n", plugin_name, plugin_desc);
324 result.AppendError ("no platforms are available\n");
325 result.SetStatus (eReturnStatusFailed);
328 result.SetStatus (eReturnStatusSuccessFinishResult);
329 return result.Succeeded();
333 //----------------------------------------------------------------------
335 //----------------------------------------------------------------------
336 class CommandObjectPlatformStatus : public CommandObjectParsed
339 CommandObjectPlatformStatus (CommandInterpreter &interpreter) :
340 CommandObjectParsed (interpreter,
342 "Display status for the currently selected platform.",
349 ~CommandObjectPlatformStatus ()
355 DoExecute (Args& args, CommandReturnObject &result)
357 Stream &ostrm = result.GetOutputStream();
359 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
360 PlatformSP platform_sp;
363 platform_sp = target->GetPlatform();
367 platform_sp = m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform();
371 platform_sp->GetStatus (ostrm);
372 result.SetStatus (eReturnStatusSuccessFinishResult);
376 result.AppendError ("no platform us currently selected\n");
377 result.SetStatus (eReturnStatusFailed);
379 return result.Succeeded();
383 //----------------------------------------------------------------------
384 // "platform connect <connect-url>"
385 //----------------------------------------------------------------------
386 class CommandObjectPlatformConnect : public CommandObjectParsed
389 CommandObjectPlatformConnect (CommandInterpreter &interpreter) :
390 CommandObjectParsed (interpreter,
392 "Connect a platform by name to be the currently selected platform.",
393 "platform connect <connect-url>",
399 ~CommandObjectPlatformConnect ()
405 DoExecute (Args& args, CommandReturnObject &result)
407 Stream &ostrm = result.GetOutputStream();
409 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
412 Error error (platform_sp->ConnectRemote (args));
415 platform_sp->GetStatus (ostrm);
416 result.SetStatus (eReturnStatusSuccessFinishResult);
420 result.AppendErrorWithFormat ("%s\n", error.AsCString());
421 result.SetStatus (eReturnStatusFailed);
426 result.AppendError ("no platform is currently selected\n");
427 result.SetStatus (eReturnStatusFailed);
429 return result.Succeeded();
435 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
436 OptionGroupOptions* m_platform_options = NULL;
439 m_platform_options = platform_sp->GetConnectionOptions(m_interpreter);
440 if (m_platform_options != NULL && !m_platform_options->m_did_finalize)
441 m_platform_options->Finalize();
443 return m_platform_options;
448 //----------------------------------------------------------------------
449 // "platform disconnect"
450 //----------------------------------------------------------------------
451 class CommandObjectPlatformDisconnect : public CommandObjectParsed
454 CommandObjectPlatformDisconnect (CommandInterpreter &interpreter) :
455 CommandObjectParsed (interpreter,
456 "platform disconnect",
457 "Disconnect a platform by name to be the currently selected platform.",
458 "platform disconnect",
464 ~CommandObjectPlatformDisconnect ()
470 DoExecute (Args& args, CommandReturnObject &result)
472 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
475 if (args.GetArgumentCount() == 0)
479 if (platform_sp->IsConnected())
481 // Cache the instance name if there is one since we are
482 // about to disconnect and the name might go with it.
483 const char *hostname_cstr = platform_sp->GetHostname();
484 std::string hostname;
486 hostname.assign (hostname_cstr);
488 error = platform_sp->DisconnectRemote ();
491 Stream &ostrm = result.GetOutputStream();
492 if (hostname.empty())
493 ostrm.Printf ("Disconnected from \"%s\"\n", platform_sp->GetPluginName().GetCString());
495 ostrm.Printf ("Disconnected from \"%s\"\n", hostname.c_str());
496 result.SetStatus (eReturnStatusSuccessFinishResult);
500 result.AppendErrorWithFormat ("%s", error.AsCString());
501 result.SetStatus (eReturnStatusFailed);
507 result.AppendErrorWithFormat ("not connected to '%s'", platform_sp->GetPluginName().GetCString());
508 result.SetStatus (eReturnStatusFailed);
514 result.AppendError ("\"platform disconnect\" doesn't take any arguments");
515 result.SetStatus (eReturnStatusFailed);
520 result.AppendError ("no platform is currently selected");
521 result.SetStatus (eReturnStatusFailed);
523 return result.Succeeded();
527 //----------------------------------------------------------------------
528 // "platform settings"
529 //----------------------------------------------------------------------
530 class CommandObjectPlatformSettings : public CommandObjectParsed
533 CommandObjectPlatformSettings (CommandInterpreter &interpreter) :
534 CommandObjectParsed (interpreter,
536 "Set settings for the current target's platform, or for a platform by name.",
539 m_options (interpreter),
540 m_option_working_dir (LLDB_OPT_SET_1, false, "working-dir", 'w', 0, eArgTypePath, "The working directory for the platform.")
542 m_options.Append (&m_option_working_dir, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
546 ~CommandObjectPlatformSettings ()
552 DoExecute (Args& args, CommandReturnObject &result)
554 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
557 if (m_option_working_dir.GetOptionValue().OptionWasSet())
558 platform_sp->SetWorkingDirectory (ConstString(m_option_working_dir.GetOptionValue().GetCurrentValue().GetPath().c_str()));
562 result.AppendError ("no platform is currently selected");
563 result.SetStatus (eReturnStatusFailed);
565 return result.Succeeded();
571 if (m_options.DidFinalize() == false)
573 m_options.Append(new OptionPermissions());
574 m_options.Finalize();
580 OptionGroupOptions m_options;
581 OptionGroupFile m_option_working_dir;
586 //----------------------------------------------------------------------
588 //----------------------------------------------------------------------
589 class CommandObjectPlatformMkDir : public CommandObjectParsed
592 CommandObjectPlatformMkDir (CommandInterpreter &interpreter) :
593 CommandObjectParsed (interpreter,
595 "Make a new directory on the remote end.",
598 m_options(interpreter)
603 ~CommandObjectPlatformMkDir ()
608 DoExecute (Args& args, CommandReturnObject &result)
610 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
613 std::string cmd_line;
614 args.GetCommandString(cmd_line);
616 const OptionPermissions* options_permissions = (OptionPermissions*)m_options.GetGroupWithOption('r');
617 if (options_permissions)
618 mode = options_permissions->m_permissions;
620 mode = lldb::eFilePermissionsUserRWX | lldb::eFilePermissionsGroupRWX | lldb::eFilePermissionsWorldRX;
621 Error error = platform_sp->MakeDirectory(cmd_line.c_str(), mode);
624 result.SetStatus (eReturnStatusSuccessFinishResult);
628 result.AppendError(error.AsCString());
629 result.SetStatus (eReturnStatusFailed);
634 result.AppendError ("no platform currently selected\n");
635 result.SetStatus (eReturnStatusFailed);
637 return result.Succeeded();
643 if (m_options.DidFinalize() == false)
645 m_options.Append(new OptionPermissions());
646 m_options.Finalize();
650 OptionGroupOptions m_options;
654 //----------------------------------------------------------------------
656 //----------------------------------------------------------------------
657 class CommandObjectPlatformFOpen : public CommandObjectParsed
660 CommandObjectPlatformFOpen (CommandInterpreter &interpreter) :
661 CommandObjectParsed (interpreter,
662 "platform file open",
663 "Open a file on the remote end.",
666 m_options(interpreter)
671 ~CommandObjectPlatformFOpen ()
676 DoExecute (Args& args, CommandReturnObject &result)
678 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
682 std::string cmd_line;
683 args.GetCommandString(cmd_line);
685 const OptionPermissions* options_permissions = (OptionPermissions*)m_options.GetGroupWithOption('r');
686 if (options_permissions)
687 perms = options_permissions->m_permissions;
689 perms = lldb::eFilePermissionsUserRW | lldb::eFilePermissionsGroupRW | lldb::eFilePermissionsWorldRead;
690 lldb::user_id_t fd = platform_sp->OpenFile(FileSpec(cmd_line.c_str(),false),
691 File::eOpenOptionRead | File::eOpenOptionWrite |
692 File::eOpenOptionAppend | File::eOpenOptionCanCreate,
697 result.AppendMessageWithFormat("File Descriptor = %" PRIu64 "\n",fd);
698 result.SetStatus (eReturnStatusSuccessFinishResult);
702 result.AppendError(error.AsCString());
703 result.SetStatus (eReturnStatusFailed);
708 result.AppendError ("no platform currently selected\n");
709 result.SetStatus (eReturnStatusFailed);
711 return result.Succeeded();
716 if (m_options.DidFinalize() == false)
718 m_options.Append(new OptionPermissions());
719 m_options.Finalize();
723 OptionGroupOptions m_options;
726 //----------------------------------------------------------------------
728 //----------------------------------------------------------------------
729 class CommandObjectPlatformFClose : public CommandObjectParsed
732 CommandObjectPlatformFClose (CommandInterpreter &interpreter) :
733 CommandObjectParsed (interpreter,
734 "platform file close",
735 "Close a file on the remote end.",
742 ~CommandObjectPlatformFClose ()
747 DoExecute (Args& args, CommandReturnObject &result)
749 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
752 std::string cmd_line;
753 args.GetCommandString(cmd_line);
754 const lldb::user_id_t fd = Args::StringToUInt64(cmd_line.c_str(), UINT64_MAX);
756 bool success = platform_sp->CloseFile(fd, error);
759 result.AppendMessageWithFormat("file %" PRIu64 " closed.\n", fd);
760 result.SetStatus (eReturnStatusSuccessFinishResult);
764 result.AppendError(error.AsCString());
765 result.SetStatus (eReturnStatusFailed);
770 result.AppendError ("no platform currently selected\n");
771 result.SetStatus (eReturnStatusFailed);
773 return result.Succeeded();
777 //----------------------------------------------------------------------
779 //----------------------------------------------------------------------
780 class CommandObjectPlatformFRead : public CommandObjectParsed
783 CommandObjectPlatformFRead (CommandInterpreter &interpreter) :
784 CommandObjectParsed (interpreter,
785 "platform file read",
786 "Read data from a file on the remote end.",
789 m_options (interpreter)
794 ~CommandObjectPlatformFRead ()
799 DoExecute (Args& args, CommandReturnObject &result)
801 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
804 std::string cmd_line;
805 args.GetCommandString(cmd_line);
806 const lldb::user_id_t fd = Args::StringToUInt64(cmd_line.c_str(), UINT64_MAX);
807 std::string buffer(m_options.m_count,0);
809 uint32_t retcode = platform_sp->ReadFile(fd, m_options.m_offset, &buffer[0], m_options.m_count, error);
810 result.AppendMessageWithFormat("Return = %d\n",retcode);
811 result.AppendMessageWithFormat("Data = \"%s\"\n",buffer.c_str());
812 result.SetStatus (eReturnStatusSuccessFinishResult);
816 result.AppendError ("no platform currently selected\n");
817 result.SetStatus (eReturnStatusFailed);
819 return result.Succeeded();
828 class CommandOptions : public Options
832 CommandOptions (CommandInterpreter &interpreter) :
833 Options (interpreter)
843 SetOptionValue (uint32_t option_idx, const char *option_arg)
846 char short_option = (char) m_getopt_table[option_idx].val;
847 bool success = false;
849 switch (short_option)
852 m_offset = Args::StringToUInt32(option_arg, 0, 0, &success);
854 error.SetErrorStringWithFormat("invalid offset: '%s'", option_arg);
857 m_count = Args::StringToUInt32(option_arg, 0, 0, &success);
859 error.SetErrorStringWithFormat("invalid offset: '%s'", option_arg);
863 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
871 OptionParsingStarting ()
877 const OptionDefinition*
880 return g_option_table;
883 // Options table: Required for subclasses of Options.
885 static OptionDefinition g_option_table[];
887 // Instance variables to hold the values for command options.
892 CommandOptions m_options;
895 CommandObjectPlatformFRead::CommandOptions::g_option_table[] =
897 { LLDB_OPT_SET_1, false, "offset" , 'o', OptionParser::eRequiredArgument, NULL, 0, eArgTypeIndex , "Offset into the file at which to start reading." },
898 { LLDB_OPT_SET_1, false, "count" , 'c', OptionParser::eRequiredArgument, NULL, 0, eArgTypeCount , "Number of bytes to read from the file." },
899 { 0 , false, NULL , 0 , 0 , NULL, 0, eArgTypeNone , NULL }
903 //----------------------------------------------------------------------
905 //----------------------------------------------------------------------
906 class CommandObjectPlatformFWrite : public CommandObjectParsed
909 CommandObjectPlatformFWrite (CommandInterpreter &interpreter) :
910 CommandObjectParsed (interpreter,
911 "platform file write",
912 "Write data to a file on the remote end.",
915 m_options (interpreter)
920 ~CommandObjectPlatformFWrite ()
925 DoExecute (Args& args, CommandReturnObject &result)
927 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
930 std::string cmd_line;
931 args.GetCommandString(cmd_line);
933 const lldb::user_id_t fd = Args::StringToUInt64(cmd_line.c_str(), UINT64_MAX);
934 uint32_t retcode = platform_sp->WriteFile (fd,
936 &m_options.m_data[0],
937 m_options.m_data.size(),
939 result.AppendMessageWithFormat("Return = %d\n",retcode);
940 result.SetStatus (eReturnStatusSuccessFinishResult);
944 result.AppendError ("no platform currently selected\n");
945 result.SetStatus (eReturnStatusFailed);
947 return result.Succeeded();
956 class CommandOptions : public Options
960 CommandOptions (CommandInterpreter &interpreter) :
961 Options (interpreter)
971 SetOptionValue (uint32_t option_idx, const char *option_arg)
974 char short_option = (char) m_getopt_table[option_idx].val;
975 bool success = false;
977 switch (short_option)
980 m_offset = Args::StringToUInt32(option_arg, 0, 0, &success);
982 error.SetErrorStringWithFormat("invalid offset: '%s'", option_arg);
985 m_data.assign(option_arg);
989 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
997 OptionParsingStarting ()
1003 const OptionDefinition*
1006 return g_option_table;
1009 // Options table: Required for subclasses of Options.
1011 static OptionDefinition g_option_table[];
1013 // Instance variables to hold the values for command options.
1018 CommandOptions m_options;
1021 CommandObjectPlatformFWrite::CommandOptions::g_option_table[] =
1023 { LLDB_OPT_SET_1, false, "offset" , 'o', OptionParser::eRequiredArgument, NULL, 0, eArgTypeIndex , "Offset into the file at which to start reading." },
1024 { LLDB_OPT_SET_1, false, "data" , 'd', OptionParser::eRequiredArgument, NULL, 0, eArgTypeValue , "Text to write to the file." },
1025 { 0 , false, NULL , 0 , 0 , NULL, 0, eArgTypeNone , NULL }
1028 class CommandObjectPlatformFile : public CommandObjectMultiword
1031 //------------------------------------------------------------------
1032 // Constructors and Destructors
1033 //------------------------------------------------------------------
1034 CommandObjectPlatformFile (CommandInterpreter &interpreter) :
1035 CommandObjectMultiword (interpreter,
1037 "A set of commands to manage file access through a platform",
1038 "platform file [open|close|read|write] ...")
1040 LoadSubCommand ("open", CommandObjectSP (new CommandObjectPlatformFOpen (interpreter)));
1041 LoadSubCommand ("close", CommandObjectSP (new CommandObjectPlatformFClose (interpreter)));
1042 LoadSubCommand ("read", CommandObjectSP (new CommandObjectPlatformFRead (interpreter)));
1043 LoadSubCommand ("write", CommandObjectSP (new CommandObjectPlatformFWrite (interpreter)));
1047 ~CommandObjectPlatformFile ()
1052 //------------------------------------------------------------------
1053 // For CommandObjectPlatform only
1054 //------------------------------------------------------------------
1055 DISALLOW_COPY_AND_ASSIGN (CommandObjectPlatformFile);
1058 //----------------------------------------------------------------------
1059 // "platform get-file remote-file-path host-file-path"
1060 //----------------------------------------------------------------------
1061 class CommandObjectPlatformGetFile : public CommandObjectParsed
1064 CommandObjectPlatformGetFile (CommandInterpreter &interpreter) :
1065 CommandObjectParsed (interpreter,
1066 "platform get-file",
1067 "Transfer a file from the remote end to the local host.",
1068 "platform get-file <remote-file-spec> <local-file-spec>",
1074 platform get-file /the/remote/file/path /the/local/file/path\n\
1075 # Transfer a file from the remote end with file path /the/remote/file/path to the local host.\n");
1077 CommandArgumentEntry arg1, arg2;
1078 CommandArgumentData file_arg_remote, file_arg_host;
1080 // Define the first (and only) variant of this arg.
1081 file_arg_remote.arg_type = eArgTypeFilename;
1082 file_arg_remote.arg_repetition = eArgRepeatPlain;
1083 // There is only one variant this argument could be; put it into the argument entry.
1084 arg1.push_back (file_arg_remote);
1086 // Define the second (and only) variant of this arg.
1087 file_arg_host.arg_type = eArgTypeFilename;
1088 file_arg_host.arg_repetition = eArgRepeatPlain;
1089 // There is only one variant this argument could be; put it into the argument entry.
1090 arg2.push_back (file_arg_host);
1092 // Push the data for the first and the second arguments into the m_arguments vector.
1093 m_arguments.push_back (arg1);
1094 m_arguments.push_back (arg2);
1098 ~CommandObjectPlatformGetFile ()
1103 DoExecute (Args& args, CommandReturnObject &result)
1105 // If the number of arguments is incorrect, issue an error message.
1106 if (args.GetArgumentCount() != 2)
1108 result.GetErrorStream().Printf("error: required arguments missing; specify both the source and destination file paths\n");
1109 result.SetStatus(eReturnStatusFailed);
1113 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
1116 const char *remote_file_path = args.GetArgumentAtIndex(0);
1117 const char *local_file_path = args.GetArgumentAtIndex(1);
1118 Error error = platform_sp->GetFile(FileSpec(remote_file_path, false),
1119 FileSpec(local_file_path, false));
1120 if (error.Success())
1122 result.AppendMessageWithFormat("successfully get-file from %s (remote) to %s (host)\n",
1123 remote_file_path, local_file_path);
1124 result.SetStatus (eReturnStatusSuccessFinishResult);
1128 result.AppendMessageWithFormat("get-file failed: %s\n", error.AsCString());
1129 result.SetStatus (eReturnStatusFailed);
1134 result.AppendError ("no platform currently selected\n");
1135 result.SetStatus (eReturnStatusFailed);
1137 return result.Succeeded();
1141 //----------------------------------------------------------------------
1142 // "platform get-size remote-file-path"
1143 //----------------------------------------------------------------------
1144 class CommandObjectPlatformGetSize : public CommandObjectParsed
1147 CommandObjectPlatformGetSize (CommandInterpreter &interpreter) :
1148 CommandObjectParsed (interpreter,
1149 "platform get-size",
1150 "Get the file size from the remote end.",
1151 "platform get-size <remote-file-spec>",
1157 platform get-size /the/remote/file/path\n\
1158 # Get the file size from the remote end with path /the/remote/file/path.\n");
1160 CommandArgumentEntry arg1;
1161 CommandArgumentData file_arg_remote;
1163 // Define the first (and only) variant of this arg.
1164 file_arg_remote.arg_type = eArgTypeFilename;
1165 file_arg_remote.arg_repetition = eArgRepeatPlain;
1166 // There is only one variant this argument could be; put it into the argument entry.
1167 arg1.push_back (file_arg_remote);
1169 // Push the data for the first argument into the m_arguments vector.
1170 m_arguments.push_back (arg1);
1174 ~CommandObjectPlatformGetSize ()
1179 DoExecute (Args& args, CommandReturnObject &result)
1181 // If the number of arguments is incorrect, issue an error message.
1182 if (args.GetArgumentCount() != 1)
1184 result.GetErrorStream().Printf("error: required argument missing; specify the source file path as the only argument\n");
1185 result.SetStatus(eReturnStatusFailed);
1189 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
1192 std::string remote_file_path(args.GetArgumentAtIndex(0));
1193 user_id_t size = platform_sp->GetFileSize(FileSpec(remote_file_path.c_str(), false));
1194 if (size != UINT64_MAX)
1196 result.AppendMessageWithFormat("File size of %s (remote): %" PRIu64 "\n", remote_file_path.c_str(), size);
1197 result.SetStatus (eReturnStatusSuccessFinishResult);
1201 result.AppendMessageWithFormat("Eroor getting file size of %s (remote)\n", remote_file_path.c_str());
1202 result.SetStatus (eReturnStatusFailed);
1207 result.AppendError ("no platform currently selected\n");
1208 result.SetStatus (eReturnStatusFailed);
1210 return result.Succeeded();
1214 //----------------------------------------------------------------------
1215 // "platform put-file"
1216 //----------------------------------------------------------------------
1217 class CommandObjectPlatformPutFile : public CommandObjectParsed
1220 CommandObjectPlatformPutFile (CommandInterpreter &interpreter) :
1221 CommandObjectParsed (interpreter,
1222 "platform put-file",
1223 "Transfer a file from this system to the remote end.",
1230 ~CommandObjectPlatformPutFile ()
1235 DoExecute (Args& args, CommandReturnObject &result)
1237 const char* src = args.GetArgumentAtIndex(0);
1238 const char* dst = args.GetArgumentAtIndex(1);
1240 FileSpec src_fs(src, true);
1241 FileSpec dst_fs(dst, false);
1243 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
1246 Error error (platform_sp->PutFile(src_fs, dst_fs));
1247 if (error.Success())
1249 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1253 result.AppendError (error.AsCString());
1254 result.SetStatus (eReturnStatusFailed);
1259 result.AppendError ("no platform currently selected\n");
1260 result.SetStatus (eReturnStatusFailed);
1262 return result.Succeeded();
1266 //----------------------------------------------------------------------
1267 // "platform process launch"
1268 //----------------------------------------------------------------------
1269 class CommandObjectPlatformProcessLaunch : public CommandObjectParsed
1272 CommandObjectPlatformProcessLaunch (CommandInterpreter &interpreter) :
1273 CommandObjectParsed (interpreter,
1274 "platform process launch",
1275 "Launch a new process on a remote platform.",
1276 "platform process launch program",
1277 eFlagRequiresTarget | eFlagTryTargetAPILock),
1278 m_options (interpreter)
1283 ~CommandObjectPlatformProcessLaunch ()
1295 DoExecute (Args& args, CommandReturnObject &result)
1297 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1298 PlatformSP platform_sp;
1301 platform_sp = target->GetPlatform();
1305 platform_sp = m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform();
1311 const size_t argc = args.GetArgumentCount();
1312 Target *target = m_exe_ctx.GetTargetPtr();
1313 Module *exe_module = target->GetExecutableModulePointer();
1316 m_options.launch_info.GetExecutableFile () = exe_module->GetFileSpec();
1317 char exe_path[PATH_MAX];
1318 if (m_options.launch_info.GetExecutableFile ().GetPath (exe_path, sizeof(exe_path)))
1319 m_options.launch_info.GetArguments().AppendArgument (exe_path);
1320 m_options.launch_info.GetArchitecture() = exe_module->GetArchitecture();
1325 if (m_options.launch_info.GetExecutableFile ())
1327 // We already have an executable file, so we will use this
1328 // and all arguments to this function are extra arguments
1329 m_options.launch_info.GetArguments().AppendArguments (args);
1333 // We don't have any file yet, so the first argument is our
1334 // executable, and the rest are program arguments
1335 const bool first_arg_is_executable = true;
1336 m_options.launch_info.SetArguments (args, first_arg_is_executable);
1340 if (m_options.launch_info.GetExecutableFile ())
1342 Debugger &debugger = m_interpreter.GetDebugger();
1345 target->GetRunArguments(m_options.launch_info.GetArguments());
1347 ProcessSP process_sp (platform_sp->DebugProcess (m_options.launch_info,
1350 debugger.GetListener(),
1352 if (process_sp && process_sp->IsAlive())
1354 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1358 if (error.Success())
1359 result.AppendError ("process launch failed");
1361 result.AppendError (error.AsCString());
1362 result.SetStatus (eReturnStatusFailed);
1366 result.AppendError ("'platform process launch' uses the current target file and arguments, or the executable and its arguments can be specified in this command");
1367 result.SetStatus (eReturnStatusFailed);
1373 result.AppendError ("no platform is selected\n");
1375 return result.Succeeded();
1379 ProcessLaunchCommandOptions m_options;
1384 //----------------------------------------------------------------------
1385 // "platform process list"
1386 //----------------------------------------------------------------------
1387 class CommandObjectPlatformProcessList : public CommandObjectParsed
1390 CommandObjectPlatformProcessList (CommandInterpreter &interpreter) :
1391 CommandObjectParsed (interpreter,
1392 "platform process list",
1393 "List processes on a remote platform by name, pid, or many other matching attributes.",
1394 "platform process list",
1396 m_options (interpreter)
1401 ~CommandObjectPlatformProcessList ()
1413 DoExecute (Args& args, CommandReturnObject &result)
1415 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1416 PlatformSP platform_sp;
1419 platform_sp = target->GetPlatform();
1423 platform_sp = m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform();
1429 if (args.GetArgumentCount() == 0)
1434 Stream &ostrm = result.GetOutputStream();
1436 lldb::pid_t pid = m_options.match_info.GetProcessInfo().GetProcessID();
1437 if (pid != LLDB_INVALID_PROCESS_ID)
1439 ProcessInstanceInfo proc_info;
1440 if (platform_sp->GetProcessInfo (pid, proc_info))
1442 ProcessInstanceInfo::DumpTableHeader (ostrm, platform_sp.get(), m_options.show_args, m_options.verbose);
1443 proc_info.DumpAsTableRow(ostrm, platform_sp.get(), m_options.show_args, m_options.verbose);
1444 result.SetStatus (eReturnStatusSuccessFinishResult);
1448 result.AppendErrorWithFormat ("no process found with pid = %" PRIu64 "\n", pid);
1449 result.SetStatus (eReturnStatusFailed);
1454 ProcessInstanceInfoList proc_infos;
1455 const uint32_t matches = platform_sp->FindProcesses (m_options.match_info, proc_infos);
1456 const char *match_desc = NULL;
1457 const char *match_name = m_options.match_info.GetProcessInfo().GetName();
1458 if (match_name && match_name[0])
1460 switch (m_options.match_info.GetNameMatchType())
1462 case eNameMatchIgnore: break;
1463 case eNameMatchEquals: match_desc = "matched"; break;
1464 case eNameMatchContains: match_desc = "contained"; break;
1465 case eNameMatchStartsWith: match_desc = "started with"; break;
1466 case eNameMatchEndsWith: match_desc = "ended with"; break;
1467 case eNameMatchRegularExpression: match_desc = "matched the regular expression"; break;
1474 result.AppendErrorWithFormat ("no processes were found that %s \"%s\" on the \"%s\" platform\n",
1477 platform_sp->GetPluginName().GetCString());
1479 result.AppendErrorWithFormat ("no processes were found on the \"%s\" platform\n", platform_sp->GetPluginName().GetCString());
1480 result.SetStatus (eReturnStatusFailed);
1484 result.AppendMessageWithFormat ("%u matching process%s found on \"%s\"",
1486 matches > 1 ? "es were" : " was",
1487 platform_sp->GetName().GetCString());
1489 result.AppendMessageWithFormat (" whose name %s \"%s\"",
1492 result.AppendMessageWithFormat ("\n");
1493 ProcessInstanceInfo::DumpTableHeader (ostrm, platform_sp.get(), m_options.show_args, m_options.verbose);
1494 for (uint32_t i=0; i<matches; ++i)
1496 proc_infos.GetProcessInfoAtIndex(i).DumpAsTableRow(ostrm, platform_sp.get(), m_options.show_args, m_options.verbose);
1504 result.AppendError ("invalid args: process list takes only options\n");
1505 result.SetStatus (eReturnStatusFailed);
1510 result.AppendError ("no platform is selected\n");
1511 result.SetStatus (eReturnStatusFailed);
1513 return result.Succeeded();
1516 class CommandOptions : public Options
1520 CommandOptions (CommandInterpreter &interpreter) :
1521 Options (interpreter),
1532 SetOptionValue (uint32_t option_idx, const char *option_arg)
1535 const int short_option = m_getopt_table[option_idx].val;
1536 bool success = false;
1538 switch (short_option)
1541 match_info.GetProcessInfo().SetProcessID (Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success));
1543 error.SetErrorStringWithFormat("invalid process ID string: '%s'", option_arg);
1547 match_info.GetProcessInfo().SetParentProcessID (Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success));
1549 error.SetErrorStringWithFormat("invalid parent process ID string: '%s'", option_arg);
1553 match_info.GetProcessInfo().SetUserID (Args::StringToUInt32 (option_arg, UINT32_MAX, 0, &success));
1555 error.SetErrorStringWithFormat("invalid user ID string: '%s'", option_arg);
1559 match_info.GetProcessInfo().SetEffectiveUserID (Args::StringToUInt32 (option_arg, UINT32_MAX, 0, &success));
1561 error.SetErrorStringWithFormat("invalid effective user ID string: '%s'", option_arg);
1565 match_info.GetProcessInfo().SetGroupID (Args::StringToUInt32 (option_arg, UINT32_MAX, 0, &success));
1567 error.SetErrorStringWithFormat("invalid group ID string: '%s'", option_arg);
1571 match_info.GetProcessInfo().SetEffectiveGroupID (Args::StringToUInt32 (option_arg, UINT32_MAX, 0, &success));
1573 error.SetErrorStringWithFormat("invalid effective group ID string: '%s'", option_arg);
1577 match_info.GetProcessInfo().GetArchitecture().SetTriple (option_arg, m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform().get());
1581 match_info.GetProcessInfo().GetExecutableFile().SetFile (option_arg, false);
1582 match_info.SetNameMatchType (eNameMatchEquals);
1586 match_info.GetProcessInfo().GetExecutableFile().SetFile (option_arg, false);
1587 match_info.SetNameMatchType (eNameMatchEndsWith);
1591 match_info.GetProcessInfo().GetExecutableFile().SetFile (option_arg, false);
1592 match_info.SetNameMatchType (eNameMatchStartsWith);
1596 match_info.GetProcessInfo().GetExecutableFile().SetFile (option_arg, false);
1597 match_info.SetNameMatchType (eNameMatchContains);
1601 match_info.GetProcessInfo().GetExecutableFile().SetFile (option_arg, false);
1602 match_info.SetNameMatchType (eNameMatchRegularExpression);
1614 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1622 OptionParsingStarting ()
1629 const OptionDefinition*
1632 return g_option_table;
1635 // Options table: Required for subclasses of Options.
1637 static OptionDefinition g_option_table[];
1639 // Instance variables to hold the values for command options.
1641 ProcessInstanceInfoMatch match_info;
1645 CommandOptions m_options;
1649 CommandObjectPlatformProcessList::CommandOptions::g_option_table[] =
1651 { LLDB_OPT_SET_1 , false, "pid" , 'p', OptionParser::eRequiredArgument, NULL, 0, eArgTypePid , "List the process info for a specific process ID." },
1652 { LLDB_OPT_SET_2 , true , "name" , 'n', OptionParser::eRequiredArgument, NULL, 0, eArgTypeProcessName , "Find processes with executable basenames that match a string." },
1653 { LLDB_OPT_SET_3 , true , "ends-with" , 'e', OptionParser::eRequiredArgument, NULL, 0, eArgTypeProcessName , "Find processes with executable basenames that end with a string." },
1654 { LLDB_OPT_SET_4 , true , "starts-with", 's', OptionParser::eRequiredArgument, NULL, 0, eArgTypeProcessName , "Find processes with executable basenames that start with a string." },
1655 { LLDB_OPT_SET_5 , true , "contains" , 'c', OptionParser::eRequiredArgument, NULL, 0, eArgTypeProcessName , "Find processes with executable basenames that contain a string." },
1656 { LLDB_OPT_SET_6 , true , "regex" , 'r', OptionParser::eRequiredArgument, NULL, 0, eArgTypeRegularExpression, "Find processes with executable basenames that match a regular expression." },
1657 { LLDB_OPT_SET_FROM_TO(2, 6), false, "parent" , 'P', OptionParser::eRequiredArgument, NULL, 0, eArgTypePid , "Find processes that have a matching parent process ID." },
1658 { LLDB_OPT_SET_FROM_TO(2, 6), false, "uid" , 'u', OptionParser::eRequiredArgument, NULL, 0, eArgTypeUnsignedInteger , "Find processes that have a matching user ID." },
1659 { LLDB_OPT_SET_FROM_TO(2, 6), false, "euid" , 'U', OptionParser::eRequiredArgument, NULL, 0, eArgTypeUnsignedInteger , "Find processes that have a matching effective user ID." },
1660 { LLDB_OPT_SET_FROM_TO(2, 6), false, "gid" , 'g', OptionParser::eRequiredArgument, NULL, 0, eArgTypeUnsignedInteger , "Find processes that have a matching group ID." },
1661 { LLDB_OPT_SET_FROM_TO(2, 6), false, "egid" , 'G', OptionParser::eRequiredArgument, NULL, 0, eArgTypeUnsignedInteger , "Find processes that have a matching effective group ID." },
1662 { LLDB_OPT_SET_FROM_TO(2, 6), false, "arch" , 'a', OptionParser::eRequiredArgument, NULL, 0, eArgTypeArchitecture , "Find processes that have a matching architecture." },
1663 { LLDB_OPT_SET_FROM_TO(1, 6), false, "show-args" , 'A', OptionParser::eNoArgument , NULL, 0, eArgTypeNone , "Show process arguments instead of the process executable basename." },
1664 { LLDB_OPT_SET_FROM_TO(1, 6), false, "verbose" , 'v', OptionParser::eNoArgument , NULL, 0, eArgTypeNone , "Enable verbose output." },
1665 { 0 , false, NULL , 0 , 0 , NULL, 0, eArgTypeNone , NULL }
1668 //----------------------------------------------------------------------
1669 // "platform process info"
1670 //----------------------------------------------------------------------
1671 class CommandObjectPlatformProcessInfo : public CommandObjectParsed
1674 CommandObjectPlatformProcessInfo (CommandInterpreter &interpreter) :
1675 CommandObjectParsed (interpreter,
1676 "platform process info",
1677 "Get detailed information for one or more process by process ID.",
1678 "platform process info <pid> [<pid> <pid> ...]",
1681 CommandArgumentEntry arg;
1682 CommandArgumentData pid_args;
1684 // Define the first (and only) variant of this arg.
1685 pid_args.arg_type = eArgTypePid;
1686 pid_args.arg_repetition = eArgRepeatStar;
1688 // There is only one variant this argument could be; put it into the argument entry.
1689 arg.push_back (pid_args);
1691 // Push the data for the first argument into the m_arguments vector.
1692 m_arguments.push_back (arg);
1696 ~CommandObjectPlatformProcessInfo ()
1702 DoExecute (Args& args, CommandReturnObject &result)
1704 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1705 PlatformSP platform_sp;
1708 platform_sp = target->GetPlatform();
1712 platform_sp = m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform();
1717 const size_t argc = args.GetArgumentCount();
1722 if (platform_sp->IsConnected())
1724 Stream &ostrm = result.GetOutputStream();
1726 for (size_t i=0; i<argc; ++ i)
1728 const char *arg = args.GetArgumentAtIndex(i);
1729 lldb::pid_t pid = Args::StringToUInt32 (arg, LLDB_INVALID_PROCESS_ID, 0, &success);
1732 ProcessInstanceInfo proc_info;
1733 if (platform_sp->GetProcessInfo (pid, proc_info))
1735 ostrm.Printf ("Process information for process %" PRIu64 ":\n", pid);
1736 proc_info.Dump (ostrm, platform_sp.get());
1740 ostrm.Printf ("error: no process information is available for process %" PRIu64 "\n", pid);
1746 result.AppendErrorWithFormat ("invalid process ID argument '%s'", arg);
1747 result.SetStatus (eReturnStatusFailed);
1755 result.AppendErrorWithFormat ("not connected to '%s'", platform_sp->GetPluginName().GetCString());
1756 result.SetStatus (eReturnStatusFailed);
1762 result.AppendError ("one or more process id(s) must be specified");
1763 result.SetStatus (eReturnStatusFailed);
1768 result.AppendError ("no platform is currently selected");
1769 result.SetStatus (eReturnStatusFailed);
1771 return result.Succeeded();
1775 class CommandObjectPlatformProcessAttach : public CommandObjectParsed
1779 class CommandOptions : public Options
1783 CommandOptions (CommandInterpreter &interpreter) :
1784 Options(interpreter)
1786 // Keep default values of all options in one place: OptionParsingStarting ()
1787 OptionParsingStarting ();
1795 SetOptionValue (uint32_t option_idx, const char *option_arg)
1798 char short_option = (char) m_getopt_table[option_idx].val;
1799 bool success = false;
1800 switch (short_option)
1804 lldb::pid_t pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
1805 if (!success || pid == LLDB_INVALID_PROCESS_ID)
1807 error.SetErrorStringWithFormat("invalid process ID '%s'", option_arg);
1811 attach_info.SetProcessID (pid);
1817 attach_info.SetProcessPluginName (option_arg);
1821 attach_info.GetExecutableFile().SetFile(option_arg, false);
1825 attach_info.SetWaitForLaunch(true);
1829 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
1836 OptionParsingStarting ()
1838 attach_info.Clear();
1841 const OptionDefinition*
1844 return g_option_table;
1848 HandleOptionArgumentCompletion (Args &input,
1851 OptionElementVector &opt_element_vector,
1852 int opt_element_index,
1853 int match_start_point,
1854 int max_return_elements,
1855 bool &word_complete,
1856 StringList &matches)
1858 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
1859 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
1861 // We are only completing the name option for now...
1863 const OptionDefinition *opt_defs = GetDefinitions();
1864 if (opt_defs[opt_defs_index].short_option == 'n')
1866 // Are we in the name?
1868 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
1869 // use the default plugin.
1871 const char *partial_name = NULL;
1872 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
1874 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
1877 ProcessInstanceInfoList process_infos;
1878 ProcessInstanceInfoMatch match_info;
1881 match_info.GetProcessInfo().GetExecutableFile().SetFile(partial_name, false);
1882 match_info.SetNameMatchType(eNameMatchStartsWith);
1884 platform_sp->FindProcesses (match_info, process_infos);
1885 const uint32_t num_matches = process_infos.GetSize();
1886 if (num_matches > 0)
1888 for (uint32_t i=0; i<num_matches; ++i)
1890 matches.AppendString (process_infos.GetProcessNameAtIndex(i),
1891 process_infos.GetProcessNameLengthAtIndex(i));
1900 // Options table: Required for subclasses of Options.
1902 static OptionDefinition g_option_table[];
1904 // Instance variables to hold the values for command options.
1906 ProcessAttachInfo attach_info;
1909 CommandObjectPlatformProcessAttach (CommandInterpreter &interpreter) :
1910 CommandObjectParsed (interpreter,
1911 "platform process attach",
1912 "Attach to a process.",
1913 "platform process attach <cmd-options>"),
1914 m_options (interpreter)
1918 ~CommandObjectPlatformProcessAttach ()
1923 DoExecute (Args& command,
1924 CommandReturnObject &result)
1926 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
1930 ProcessSP remote_process_sp =
1931 platform_sp->Attach(m_options.attach_info, m_interpreter.GetDebugger(), NULL, m_interpreter.GetDebugger().GetListener(), err);
1934 result.AppendError(err.AsCString());
1935 result.SetStatus (eReturnStatusFailed);
1937 else if (remote_process_sp.get() == NULL)
1939 result.AppendError("could not attach: unknown reason");
1940 result.SetStatus (eReturnStatusFailed);
1943 result.SetStatus (eReturnStatusSuccessFinishResult);
1947 result.AppendError ("no platform is currently selected");
1948 result.SetStatus (eReturnStatusFailed);
1950 return result.Succeeded();
1961 CommandOptions m_options;
1966 CommandObjectPlatformProcessAttach::CommandOptions::g_option_table[] =
1968 { LLDB_OPT_SET_ALL, false, "plugin", 'P', OptionParser::eRequiredArgument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
1969 { LLDB_OPT_SET_1, false, "pid", 'p', OptionParser::eRequiredArgument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
1970 { LLDB_OPT_SET_2, false, "name", 'n', OptionParser::eRequiredArgument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
1971 { LLDB_OPT_SET_2, false, "waitfor",'w', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Wait for the the process with <process-name> to launch."},
1972 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1976 class CommandObjectPlatformProcess : public CommandObjectMultiword
1979 //------------------------------------------------------------------
1980 // Constructors and Destructors
1981 //------------------------------------------------------------------
1982 CommandObjectPlatformProcess (CommandInterpreter &interpreter) :
1983 CommandObjectMultiword (interpreter,
1985 "A set of commands to query, launch and attach to platform processes",
1986 "platform process [attach|launch|list] ...")
1988 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectPlatformProcessAttach (interpreter)));
1989 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectPlatformProcessLaunch (interpreter)));
1990 LoadSubCommand ("info" , CommandObjectSP (new CommandObjectPlatformProcessInfo (interpreter)));
1991 LoadSubCommand ("list" , CommandObjectSP (new CommandObjectPlatformProcessList (interpreter)));
1996 ~CommandObjectPlatformProcess ()
2001 //------------------------------------------------------------------
2002 // For CommandObjectPlatform only
2003 //------------------------------------------------------------------
2004 DISALLOW_COPY_AND_ASSIGN (CommandObjectPlatformProcess);
2007 //----------------------------------------------------------------------
2009 //----------------------------------------------------------------------
2010 class CommandObjectPlatformShell : public CommandObjectRaw
2014 class CommandOptions : public Options
2018 CommandOptions (CommandInterpreter &interpreter) :
2019 Options(interpreter),
2030 GetNumDefinitions ()
2035 virtual const OptionDefinition*
2038 return g_option_table;
2042 SetOptionValue (uint32_t option_idx,
2043 const char *option_value)
2047 const char short_option = (char) g_option_table[option_idx].short_option;
2049 switch (short_option)
2054 timeout = Args::StringToUInt32(option_value, 10, 10, &success);
2056 error.SetErrorStringWithFormat("could not convert \"%s\" to a numeric value.", option_value);
2060 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
2068 OptionParsingStarting ()
2072 // Options table: Required for subclasses of Options.
2074 static OptionDefinition g_option_table[];
2078 CommandObjectPlatformShell (CommandInterpreter &interpreter) :
2079 CommandObjectRaw (interpreter,
2081 "Run a shell command on a the selected platform.",
2082 "platform shell <shell-command>",
2084 m_options(interpreter)
2089 ~CommandObjectPlatformShell ()
2101 DoExecute (const char *raw_command_line, CommandReturnObject &result)
2103 m_options.NotifyOptionParsingStarting();
2105 const char* expr = NULL;
2107 // Print out an usage syntax on an empty command line.
2108 if (raw_command_line[0] == '\0')
2110 result.GetOutputStream().Printf("%s\n", this->GetSyntax());
2114 if (raw_command_line[0] == '-')
2116 // We have some options and these options MUST end with --.
2117 const char *end_options = NULL;
2118 const char *s = raw_command_line;
2121 end_options = ::strstr (s, "--");
2124 end_options += 2; // Get past the "--"
2125 if (::isspace (end_options[0]))
2128 while (::isspace (*expr))
2138 Args args (raw_command_line, end_options - raw_command_line);
2139 if (!ParseOptions (args, result))
2145 expr = raw_command_line;
2147 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
2151 const char *working_dir = NULL;
2155 error = (platform_sp->RunShellCommand (expr, working_dir, &status, &signo, &output, m_options.timeout));
2156 if (!output.empty())
2157 result.GetOutputStream().PutCString(output.c_str());
2162 const char *signo_cstr = Host::GetSignalAsCString(signo);
2164 result.GetOutputStream().Printf("error: command returned with status %i and signal %s\n", status, signo_cstr);
2166 result.GetOutputStream().Printf("error: command returned with status %i and signal %i\n", status, signo);
2169 result.GetOutputStream().Printf("error: command returned with status %i\n", status);
2174 result.GetOutputStream().Printf("error: cannot run remote shell commands without a platform\n");
2175 error.SetErrorString("error: cannot run remote shell commands without a platform");
2180 result.AppendError(error.AsCString());
2181 result.SetStatus (eReturnStatusFailed);
2185 result.SetStatus (eReturnStatusSuccessFinishResult);
2189 CommandOptions m_options;
2193 CommandObjectPlatformShell::CommandOptions::g_option_table[] =
2195 { LLDB_OPT_SET_ALL, false, "timeout", 't', OptionParser::eRequiredArgument, NULL, 0, eArgTypeValue, "Seconds to wait for the remote host to finish running the command."},
2196 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
2200 //----------------------------------------------------------------------
2201 // "platform install" - install a target to a remote end
2202 //----------------------------------------------------------------------
2203 class CommandObjectPlatformInstall : public CommandObjectParsed
2206 CommandObjectPlatformInstall (CommandInterpreter &interpreter) :
2207 CommandObjectParsed (interpreter,
2208 "platform target-install",
2209 "Install a target (bundle or executable file) to the remote end.",
2210 "platform target-install <local-thing> <remote-sandbox>",
2216 ~CommandObjectPlatformInstall ()
2221 DoExecute (Args& args, CommandReturnObject &result)
2223 if (args.GetArgumentCount() != 2)
2225 result.AppendError("platform target-install takes two arguments");
2226 result.SetStatus(eReturnStatusFailed);
2229 // TODO: move the bulk of this code over to the platform itself
2230 FileSpec src(args.GetArgumentAtIndex(0), true);
2231 FileSpec dst(args.GetArgumentAtIndex(1), false);
2232 if (src.Exists() == false)
2234 result.AppendError("source location does not exist or is not accessible");
2235 result.SetStatus(eReturnStatusFailed);
2238 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
2241 result.AppendError ("no platform currently selected");
2242 result.SetStatus (eReturnStatusFailed);
2246 Error error = platform_sp->Install(src, dst);
2247 if (error.Success())
2249 result.SetStatus(eReturnStatusSuccessFinishNoResult);
2253 result.AppendErrorWithFormat("install failed: %s", error.AsCString());
2254 result.SetStatus(eReturnStatusFailed);
2256 return result.Succeeded();
2262 //----------------------------------------------------------------------
2263 // CommandObjectPlatform constructor
2264 //----------------------------------------------------------------------
2265 CommandObjectPlatform::CommandObjectPlatform(CommandInterpreter &interpreter) :
2266 CommandObjectMultiword (interpreter,
2268 "A set of commands to manage and create platforms.",
2269 "platform [connect|disconnect|info|list|status|select] ...")
2271 LoadSubCommand ("select", CommandObjectSP (new CommandObjectPlatformSelect (interpreter)));
2272 LoadSubCommand ("list" , CommandObjectSP (new CommandObjectPlatformList (interpreter)));
2273 LoadSubCommand ("status", CommandObjectSP (new CommandObjectPlatformStatus (interpreter)));
2274 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectPlatformConnect (interpreter)));
2275 LoadSubCommand ("disconnect", CommandObjectSP (new CommandObjectPlatformDisconnect (interpreter)));
2276 LoadSubCommand ("settings", CommandObjectSP (new CommandObjectPlatformSettings (interpreter)));
2277 #ifdef LLDB_CONFIGURATION_DEBUG
2278 LoadSubCommand ("mkdir", CommandObjectSP (new CommandObjectPlatformMkDir (interpreter)));
2279 LoadSubCommand ("file", CommandObjectSP (new CommandObjectPlatformFile (interpreter)));
2280 LoadSubCommand ("get-file", CommandObjectSP (new CommandObjectPlatformGetFile (interpreter)));
2281 LoadSubCommand ("get-size", CommandObjectSP (new CommandObjectPlatformGetSize (interpreter)));
2282 LoadSubCommand ("put-file", CommandObjectSP (new CommandObjectPlatformPutFile (interpreter)));
2284 LoadSubCommand ("process", CommandObjectSP (new CommandObjectPlatformProcess (interpreter)));
2285 LoadSubCommand ("shell", CommandObjectSP (new CommandObjectPlatformShell (interpreter)));
2286 LoadSubCommand ("target-install", CommandObjectSP (new CommandObjectPlatformInstall (interpreter)));
2290 //----------------------------------------------------------------------
2292 //----------------------------------------------------------------------
2293 CommandObjectPlatform::~CommandObjectPlatform()