blob: e7b9ab1886c19e8f3f6bc43b66b892353ff9c19e [file] [log] [blame]
Greg Claytonb1888f22011-03-19 01:12:21 +00001//===-- CommandObjectPlatform.cpp -------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Greg Claytonb1888f22011-03-19 01:12:21 +000012#include "CommandObjectPlatform.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Core/DataExtractor.h"
19#include "lldb/Core/Debugger.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000020#include "lldb/Core/Module.h"
Greg Claytonb1888f22011-03-19 01:12:21 +000021#include "lldb/Core/PluginManager.h"
22#include "lldb/Interpreter/Args.h"
23#include "lldb/Interpreter/CommandInterpreter.h"
24#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000025#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Claytonb1888f22011-03-19 01:12:21 +000026#include "lldb/Target/ExecutionContext.h"
27#include "lldb/Target/Platform.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000028#include "lldb/Target/Process.h"
Greg Claytonb1888f22011-03-19 01:12:21 +000029
30using namespace lldb;
31using namespace lldb_private;
32
Greg Clayton143fcc32011-04-13 00:18:08 +000033
Greg Claytonb1888f22011-03-19 01:12:21 +000034//----------------------------------------------------------------------
Greg Claytonabe0fed2011-04-18 08:33:37 +000035// "platform select <platform-name>"
Greg Claytonb1888f22011-03-19 01:12:21 +000036//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +000037class CommandObjectPlatformSelect : public CommandObjectParsed
Greg Claytonb1888f22011-03-19 01:12:21 +000038{
39public:
Greg Clayton143fcc32011-04-13 00:18:08 +000040 CommandObjectPlatformSelect (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +000041 CommandObjectParsed (interpreter,
42 "platform select",
43 "Create a platform if needed and select it as the current platform.",
44 "platform select <platform-name>",
45 0),
Greg Clayton143fcc32011-04-13 00:18:08 +000046 m_option_group (interpreter),
47 m_platform_options (false) // Don't include the "--platform" option by passing false
Greg Claytonb1888f22011-03-19 01:12:21 +000048 {
Greg Clayton5e342f52011-04-13 22:47:15 +000049 m_option_group.Append (&m_platform_options, LLDB_OPT_SET_ALL, 1);
Greg Clayton143fcc32011-04-13 00:18:08 +000050 m_option_group.Finalize();
Greg Claytonb1888f22011-03-19 01:12:21 +000051 }
52
53 virtual
Greg Clayton143fcc32011-04-13 00:18:08 +000054 ~CommandObjectPlatformSelect ()
Greg Claytonb1888f22011-03-19 01:12:21 +000055 {
56 }
57
Jim Inghamda26bd22012-06-08 21:56:10 +000058 virtual int
59 HandleCompletion (Args &input,
60 int &cursor_index,
61 int &cursor_char_position,
62 int match_start_point,
63 int max_return_elements,
64 bool &word_complete,
65 StringList &matches)
66 {
67 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
68 completion_str.erase (cursor_char_position);
69
70 CommandCompletions::PlatformPluginNames (m_interpreter,
71 completion_str.c_str(),
72 match_start_point,
73 max_return_elements,
74 NULL,
75 word_complete,
76 matches);
77 return matches.GetSize();
78 }
79
80 virtual Options *
81 GetOptions ()
82 {
83 return &m_option_group;
84 }
85
86protected:
Greg Claytonb1888f22011-03-19 01:12:21 +000087 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +000088 DoExecute (Args& args, CommandReturnObject &result)
Greg Claytonb1888f22011-03-19 01:12:21 +000089 {
Greg Claytonb1888f22011-03-19 01:12:21 +000090 if (args.GetArgumentCount() == 1)
91 {
Greg Clayton5e342f52011-04-13 22:47:15 +000092 const char *platform_name = args.GetArgumentAtIndex (0);
93 if (platform_name && platform_name[0])
94 {
95 const bool select = true;
Greg Claytonabe0fed2011-04-18 08:33:37 +000096 m_platform_options.SetPlatformName (platform_name);
Greg Clayton5e342f52011-04-13 22:47:15 +000097 Error error;
Greg Claytonb170aee2012-05-08 01:45:38 +000098 ArchSpec platform_arch;
99 PlatformSP platform_sp (m_platform_options.CreatePlatformWithOptions (m_interpreter, ArchSpec(), select, error, platform_arch));
Greg Clayton5e342f52011-04-13 22:47:15 +0000100 if (platform_sp)
101 {
102 platform_sp->GetStatus (result.GetOutputStream());
103 result.SetStatus (eReturnStatusSuccessFinishResult);
104 }
105 else
106 {
107 result.AppendError(error.AsCString());
108 result.SetStatus (eReturnStatusFailed);
109 }
110 }
111 else
112 {
113 result.AppendError ("invalid platform name");
114 result.SetStatus (eReturnStatusFailed);
115 }
Greg Claytonb1888f22011-03-19 01:12:21 +0000116 }
117 else
118 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000119 result.AppendError ("platform create takes a platform name as an argument\n");
Greg Claytonb1888f22011-03-19 01:12:21 +0000120 result.SetStatus (eReturnStatusFailed);
121 }
122 return result.Succeeded();
123 }
Greg Claytonabe0fed2011-04-18 08:33:37 +0000124
Greg Clayton143fcc32011-04-13 00:18:08 +0000125 OptionGroupOptions m_option_group;
Greg Claytonabe0fed2011-04-18 08:33:37 +0000126 OptionGroupPlatform m_platform_options;
Greg Claytonb1888f22011-03-19 01:12:21 +0000127};
128
129//----------------------------------------------------------------------
130// "platform list"
131//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +0000132class CommandObjectPlatformList : public CommandObjectParsed
Greg Claytonb1888f22011-03-19 01:12:21 +0000133{
134public:
135 CommandObjectPlatformList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000136 CommandObjectParsed (interpreter,
137 "platform list",
138 "List all platforms that are available.",
139 NULL,
140 0)
Greg Claytonb1888f22011-03-19 01:12:21 +0000141 {
142 }
143
144 virtual
145 ~CommandObjectPlatformList ()
146 {
147 }
148
Jim Inghamda26bd22012-06-08 21:56:10 +0000149protected:
Greg Claytonb1888f22011-03-19 01:12:21 +0000150 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000151 DoExecute (Args& args, CommandReturnObject &result)
Greg Claytonb1888f22011-03-19 01:12:21 +0000152 {
153 Stream &ostrm = result.GetOutputStream();
154 ostrm.Printf("Available platforms:\n");
155
156 PlatformSP host_platform_sp (Platform::GetDefaultPlatform());
157 ostrm.Printf ("%s: %s\n",
158 host_platform_sp->GetShortPluginName(),
159 host_platform_sp->GetDescription());
160
161 uint32_t idx;
162 for (idx = 0; 1; ++idx)
163 {
164 const char *plugin_name = PluginManager::GetPlatformPluginNameAtIndex (idx);
165 if (plugin_name == NULL)
166 break;
167 const char *plugin_desc = PluginManager::GetPlatformPluginDescriptionAtIndex (idx);
168 if (plugin_desc == NULL)
169 break;
170 ostrm.Printf("%s: %s\n", plugin_name, plugin_desc);
171 }
172
173 if (idx == 0)
174 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000175 result.AppendError ("no platforms are available\n");
Greg Claytonb1888f22011-03-19 01:12:21 +0000176 result.SetStatus (eReturnStatusFailed);
177 }
Johnny Chen08150312011-03-30 21:19:59 +0000178 else
179 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytonb1888f22011-03-19 01:12:21 +0000180 return result.Succeeded();
181 }
182};
183
184//----------------------------------------------------------------------
185// "platform status"
186//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +0000187class CommandObjectPlatformStatus : public CommandObjectParsed
Greg Claytonb1888f22011-03-19 01:12:21 +0000188{
189public:
190 CommandObjectPlatformStatus (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000191 CommandObjectParsed (interpreter,
192 "platform status",
193 "Display status for the currently selected platform.",
194 NULL,
195 0)
Greg Claytonb1888f22011-03-19 01:12:21 +0000196 {
197 }
198
199 virtual
200 ~CommandObjectPlatformStatus ()
201 {
202 }
203
Jim Inghamda26bd22012-06-08 21:56:10 +0000204protected:
Greg Claytonb1888f22011-03-19 01:12:21 +0000205 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000206 DoExecute (Args& args, CommandReturnObject &result)
Greg Claytonb1888f22011-03-19 01:12:21 +0000207 {
208 Stream &ostrm = result.GetOutputStream();
209
Greg Claytonff39f742011-04-01 00:29:43 +0000210 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
211 if (platform_sp)
Greg Claytonb1888f22011-03-19 01:12:21 +0000212 {
Greg Claytonff39f742011-04-01 00:29:43 +0000213 platform_sp->GetStatus (ostrm);
Greg Claytonb1888f22011-03-19 01:12:21 +0000214 result.SetStatus (eReturnStatusSuccessFinishResult);
215 }
216 else
217 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000218 result.AppendError ("no platform us currently selected\n");
Greg Claytonb1888f22011-03-19 01:12:21 +0000219 result.SetStatus (eReturnStatusFailed);
220 }
221 return result.Succeeded();
222 }
223};
224
Greg Claytoncb8977d2011-03-23 00:09:55 +0000225//----------------------------------------------------------------------
226// "platform connect <connect-url>"
227//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +0000228class CommandObjectPlatformConnect : public CommandObjectParsed
Greg Claytoncb8977d2011-03-23 00:09:55 +0000229{
230public:
231 CommandObjectPlatformConnect (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000232 CommandObjectParsed (interpreter,
233 "platform connect",
234 "Connect a platform by name to be the currently selected platform.",
235 "platform connect <connect-url>",
236 0)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000237 {
238 }
239
240 virtual
241 ~CommandObjectPlatformConnect ()
242 {
243 }
244
Jim Inghamda26bd22012-06-08 21:56:10 +0000245protected:
Greg Claytoncb8977d2011-03-23 00:09:55 +0000246 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000247 DoExecute (Args& args, CommandReturnObject &result)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000248 {
249 Stream &ostrm = result.GetOutputStream();
250
Greg Claytonff39f742011-04-01 00:29:43 +0000251 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
252 if (platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000253 {
Greg Claytonff39f742011-04-01 00:29:43 +0000254 Error error (platform_sp->ConnectRemote (args));
Greg Claytoncb8977d2011-03-23 00:09:55 +0000255 if (error.Success())
256 {
Greg Claytonff39f742011-04-01 00:29:43 +0000257 platform_sp->GetStatus (ostrm);
Greg Claytoncb8977d2011-03-23 00:09:55 +0000258 result.SetStatus (eReturnStatusSuccessFinishResult);
259 }
260 else
261 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000262 result.AppendErrorWithFormat ("%s\n", error.AsCString());
Greg Claytoncb8977d2011-03-23 00:09:55 +0000263 result.SetStatus (eReturnStatusFailed);
264 }
265 }
266 else
267 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000268 result.AppendError ("no platform us currently selected\n");
Greg Claytoncb8977d2011-03-23 00:09:55 +0000269 result.SetStatus (eReturnStatusFailed);
270 }
271 return result.Succeeded();
272 }
273};
274
275//----------------------------------------------------------------------
276// "platform disconnect"
277//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +0000278class CommandObjectPlatformDisconnect : public CommandObjectParsed
Greg Claytoncb8977d2011-03-23 00:09:55 +0000279{
280public:
281 CommandObjectPlatformDisconnect (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000282 CommandObjectParsed (interpreter,
283 "platform disconnect",
284 "Disconnect a platform by name to be the currently selected platform.",
285 "platform disconnect",
286 0)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000287 {
288 }
289
290 virtual
291 ~CommandObjectPlatformDisconnect ()
292 {
293 }
294
Jim Inghamda26bd22012-06-08 21:56:10 +0000295protected:
Greg Claytoncb8977d2011-03-23 00:09:55 +0000296 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000297 DoExecute (Args& args, CommandReturnObject &result)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000298 {
Greg Claytonff39f742011-04-01 00:29:43 +0000299 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
300 if (platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000301 {
302 if (args.GetArgumentCount() == 0)
303 {
304 Error error;
305
Greg Claytonff39f742011-04-01 00:29:43 +0000306 if (platform_sp->IsConnected())
Greg Claytoncb8977d2011-03-23 00:09:55 +0000307 {
308 // Cache the instance name if there is one since we are
309 // about to disconnect and the name might go with it.
Greg Claytonff39f742011-04-01 00:29:43 +0000310 const char *hostname_cstr = platform_sp->GetHostname();
Greg Clayton58e26e02011-03-24 04:28:38 +0000311 std::string hostname;
312 if (hostname_cstr)
313 hostname.assign (hostname_cstr);
Greg Claytoncb8977d2011-03-23 00:09:55 +0000314
Greg Claytonff39f742011-04-01 00:29:43 +0000315 error = platform_sp->DisconnectRemote ();
Greg Claytoncb8977d2011-03-23 00:09:55 +0000316 if (error.Success())
317 {
318 Stream &ostrm = result.GetOutputStream();
Greg Clayton58e26e02011-03-24 04:28:38 +0000319 if (hostname.empty())
Greg Claytonff39f742011-04-01 00:29:43 +0000320 ostrm.Printf ("Disconnected from \"%s\"\n", platform_sp->GetShortPluginName());
Greg Claytoncb8977d2011-03-23 00:09:55 +0000321 else
Greg Clayton58e26e02011-03-24 04:28:38 +0000322 ostrm.Printf ("Disconnected from \"%s\"\n", hostname.c_str());
Greg Claytoncb8977d2011-03-23 00:09:55 +0000323 result.SetStatus (eReturnStatusSuccessFinishResult);
324 }
325 else
326 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000327 result.AppendErrorWithFormat ("%s", error.AsCString());
Greg Claytoncb8977d2011-03-23 00:09:55 +0000328 result.SetStatus (eReturnStatusFailed);
329 }
330 }
331 else
332 {
333 // Not connected...
Greg Claytonff39f742011-04-01 00:29:43 +0000334 result.AppendErrorWithFormat ("not connected to '%s'", platform_sp->GetShortPluginName());
Greg Claytoncb8977d2011-03-23 00:09:55 +0000335 result.SetStatus (eReturnStatusFailed);
336 }
337 }
338 else
339 {
340 // Bad args
341 result.AppendError ("\"platform disconnect\" doesn't take any arguments");
342 result.SetStatus (eReturnStatusFailed);
343 }
344 }
345 else
346 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000347 result.AppendError ("no platform is currently selected");
Greg Claytoncb8977d2011-03-23 00:09:55 +0000348 result.SetStatus (eReturnStatusFailed);
349 }
350 return result.Succeeded();
351 }
352};
Greg Claytonb72d0f02011-04-12 05:54:46 +0000353//----------------------------------------------------------------------
354// "platform process launch"
355//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +0000356class CommandObjectPlatformProcessLaunch : public CommandObjectParsed
Greg Claytonb72d0f02011-04-12 05:54:46 +0000357{
358public:
359 CommandObjectPlatformProcessLaunch (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000360 CommandObjectParsed (interpreter,
361 "platform process launch",
362 "Launch a new process on a remote platform.",
363 "platform process launch program",
364 0),
Greg Claytonb72d0f02011-04-12 05:54:46 +0000365 m_options (interpreter)
366 {
367 }
368
369 virtual
370 ~CommandObjectPlatformProcessLaunch ()
371 {
372 }
373
Jim Inghamda26bd22012-06-08 21:56:10 +0000374 virtual Options *
375 GetOptions ()
376 {
377 return &m_options;
378 }
379
380protected:
Greg Claytonb72d0f02011-04-12 05:54:46 +0000381 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000382 DoExecute (Args& args, CommandReturnObject &result)
Greg Claytonb72d0f02011-04-12 05:54:46 +0000383 {
384 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
385
386 if (platform_sp)
387 {
388 Error error;
389 const uint32_t argc = args.GetArgumentCount();
Greg Clayton567e7f32011-09-22 04:58:26 +0000390 Target *target = m_interpreter.GetExecutionContext().GetTargetPtr();
Greg Claytonabb33022011-11-08 02:43:13 +0000391 if (target == NULL)
Greg Claytonb72d0f02011-04-12 05:54:46 +0000392 {
Greg Claytonabb33022011-11-08 02:43:13 +0000393 result.AppendError ("invalid target, create a debug target using the 'target create' command");
394 result.SetStatus (eReturnStatusFailed);
395 return false;
396 }
397
398 Module *exe_module = target->GetExecutableModulePointer();
399 if (exe_module)
400 {
401 m_options.launch_info.GetExecutableFile () = exe_module->GetFileSpec();
402 char exe_path[PATH_MAX];
403 if (m_options.launch_info.GetExecutableFile ().GetPath (exe_path, sizeof(exe_path)))
404 m_options.launch_info.GetArguments().AppendArgument (exe_path);
405 m_options.launch_info.GetArchitecture() = exe_module->GetArchitecture();
Greg Claytonb72d0f02011-04-12 05:54:46 +0000406 }
407
408 if (argc > 0)
409 {
410 if (m_options.launch_info.GetExecutableFile ())
411 {
412 // We already have an executable file, so we will use this
413 // and all arguments to this function are extra arguments
414 m_options.launch_info.GetArguments().AppendArguments (args);
415 }
416 else
417 {
418 // We don't have any file yet, so the first argument is our
419 // executable, and the rest are program arguments
420 const bool first_arg_is_executable = true;
Greg Clayton0c8446c2012-10-17 22:57:12 +0000421 m_options.launch_info.SetArguments (args, first_arg_is_executable);
Greg Claytonb72d0f02011-04-12 05:54:46 +0000422 }
423 }
424
425 if (m_options.launch_info.GetExecutableFile ())
426 {
427 Debugger &debugger = m_interpreter.GetDebugger();
428
429 if (argc == 0)
Greg Clayton73844aa2012-08-22 17:17:09 +0000430 target->GetRunArguments(m_options.launch_info.GetArguments());
Greg Claytonb72d0f02011-04-12 05:54:46 +0000431
432 ProcessSP process_sp (platform_sp->DebugProcess (m_options.launch_info,
433 debugger,
434 target,
435 debugger.GetListener(),
436 error));
437 if (process_sp && process_sp->IsAlive())
438 {
439 result.SetStatus (eReturnStatusSuccessFinishNoResult);
440 return true;
441 }
442
443 if (error.Success())
444 result.AppendError ("process launch failed");
445 else
446 result.AppendError (error.AsCString());
447 result.SetStatus (eReturnStatusFailed);
448 }
449 else
450 {
451 result.AppendError ("'platform process launch' uses the current target file and arguments, or the executable and its arguments can be specified in this command");
452 result.SetStatus (eReturnStatusFailed);
453 return false;
454 }
455 }
456 else
457 {
458 result.AppendError ("no platform is selected\n");
459 }
460 return result.Succeeded();
461 }
462
Greg Claytonb72d0f02011-04-12 05:54:46 +0000463protected:
464 ProcessLaunchCommandOptions m_options;
465};
466
Greg Claytoncb8977d2011-03-23 00:09:55 +0000467
468
Greg Clayton24bc5d92011-03-30 18:16:51 +0000469//----------------------------------------------------------------------
470// "platform process list"
471//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +0000472class CommandObjectPlatformProcessList : public CommandObjectParsed
Greg Clayton24bc5d92011-03-30 18:16:51 +0000473{
474public:
475 CommandObjectPlatformProcessList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000476 CommandObjectParsed (interpreter,
477 "platform process list",
478 "List processes on a remote platform by name, pid, or many other matching attributes.",
479 "platform process list",
480 0),
Greg Claytonf15996e2011-04-07 22:46:35 +0000481 m_options (interpreter)
Greg Clayton24bc5d92011-03-30 18:16:51 +0000482 {
483 }
484
485 virtual
486 ~CommandObjectPlatformProcessList ()
487 {
488 }
489
Jim Inghamda26bd22012-06-08 21:56:10 +0000490 virtual Options *
491 GetOptions ()
492 {
493 return &m_options;
494 }
495
496protected:
Greg Clayton24bc5d92011-03-30 18:16:51 +0000497 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000498 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton24bc5d92011-03-30 18:16:51 +0000499 {
500 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
501
502 if (platform_sp)
503 {
504 Error error;
505 if (args.GetArgumentCount() == 0)
506 {
507
508 if (platform_sp)
509 {
Greg Claytonff39f742011-04-01 00:29:43 +0000510 Stream &ostrm = result.GetOutputStream();
511
Greg Clayton24bc5d92011-03-30 18:16:51 +0000512 lldb::pid_t pid = m_options.match_info.GetProcessInfo().GetProcessID();
513 if (pid != LLDB_INVALID_PROCESS_ID)
514 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000515 ProcessInstanceInfo proc_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000516 if (platform_sp->GetProcessInfo (pid, proc_info))
517 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000518 ProcessInstanceInfo::DumpTableHeader (ostrm, platform_sp.get(), m_options.show_args, m_options.verbose);
519 proc_info.DumpAsTableRow(ostrm, platform_sp.get(), m_options.show_args, m_options.verbose);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000520 result.SetStatus (eReturnStatusSuccessFinishResult);
521 }
522 else
523 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000524 result.AppendErrorWithFormat ("no process found with pid = %" PRIu64 "\n", pid);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000525 result.SetStatus (eReturnStatusFailed);
526 }
527 }
528 else
529 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000530 ProcessInstanceInfoList proc_infos;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000531 const uint32_t matches = platform_sp->FindProcesses (m_options.match_info, proc_infos);
Greg Claytonb72d0f02011-04-12 05:54:46 +0000532 const char *match_desc = NULL;
533 const char *match_name = m_options.match_info.GetProcessInfo().GetName();
534 if (match_name && match_name[0])
535 {
536 switch (m_options.match_info.GetNameMatchType())
537 {
538 case eNameMatchIgnore: break;
539 case eNameMatchEquals: match_desc = "matched"; break;
540 case eNameMatchContains: match_desc = "contained"; break;
541 case eNameMatchStartsWith: match_desc = "started with"; break;
542 case eNameMatchEndsWith: match_desc = "ended with"; break;
543 case eNameMatchRegularExpression: match_desc = "matched the regular expression"; break;
544 }
545 }
546
Greg Clayton24bc5d92011-03-30 18:16:51 +0000547 if (matches == 0)
548 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000549 if (match_desc)
550 result.AppendErrorWithFormat ("no processes were found that %s \"%s\" on the \"%s\" platform\n",
551 match_desc,
552 match_name,
553 platform_sp->GetShortPluginName());
554 else
555 result.AppendErrorWithFormat ("no processes were found on the \"%s\" platform\n", platform_sp->GetShortPluginName());
556 result.SetStatus (eReturnStatusFailed);
557 }
558 else
559 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000560 result.AppendMessageWithFormat ("%u matching process%s found on \"%s\"",
561 matches,
562 matches > 1 ? "es were" : " was",
563 platform_sp->GetName());
564 if (match_desc)
565 result.AppendMessageWithFormat (" whose name %s \"%s\"",
566 match_desc,
567 match_name);
568 result.AppendMessageWithFormat ("\n");
569 ProcessInstanceInfo::DumpTableHeader (ostrm, platform_sp.get(), m_options.show_args, m_options.verbose);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000570 for (uint32_t i=0; i<matches; ++i)
571 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000572 proc_infos.GetProcessInfoAtIndex(i).DumpAsTableRow(ostrm, platform_sp.get(), m_options.show_args, m_options.verbose);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000573 }
574 }
575 }
576 }
577 }
578 else
579 {
580 result.AppendError ("invalid args: process list takes only options\n");
581 result.SetStatus (eReturnStatusFailed);
582 }
583 }
584 else
585 {
586 result.AppendError ("no platform is selected\n");
587 result.SetStatus (eReturnStatusFailed);
588 }
589 return result.Succeeded();
590 }
591
Greg Clayton24bc5d92011-03-30 18:16:51 +0000592 class CommandOptions : public Options
593 {
594 public:
595
Greg Claytonf15996e2011-04-07 22:46:35 +0000596 CommandOptions (CommandInterpreter &interpreter) :
597 Options (interpreter),
Greg Clayton24bc5d92011-03-30 18:16:51 +0000598 match_info ()
599 {
600 }
601
602 virtual
603 ~CommandOptions ()
604 {
605 }
606
607 virtual Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000608 SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Clayton24bc5d92011-03-30 18:16:51 +0000609 {
610 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +0000611 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000612 bool success = false;
613
614 switch (short_option)
615 {
616 case 'p':
617 match_info.GetProcessInfo().SetProcessID (Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success));
618 if (!success)
619 error.SetErrorStringWithFormat("invalid process ID string: '%s'", option_arg);
620 break;
621
622 case 'P':
623 match_info.GetProcessInfo().SetParentProcessID (Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success));
624 if (!success)
625 error.SetErrorStringWithFormat("invalid parent process ID string: '%s'", option_arg);
626 break;
627
628 case 'u':
Greg Claytonb72d0f02011-04-12 05:54:46 +0000629 match_info.GetProcessInfo().SetUserID (Args::StringToUInt32 (option_arg, UINT32_MAX, 0, &success));
Greg Clayton24bc5d92011-03-30 18:16:51 +0000630 if (!success)
631 error.SetErrorStringWithFormat("invalid user ID string: '%s'", option_arg);
632 break;
633
634 case 'U':
635 match_info.GetProcessInfo().SetEffectiveUserID (Args::StringToUInt32 (option_arg, UINT32_MAX, 0, &success));
636 if (!success)
637 error.SetErrorStringWithFormat("invalid effective user ID string: '%s'", option_arg);
638 break;
639
640 case 'g':
Greg Claytonb72d0f02011-04-12 05:54:46 +0000641 match_info.GetProcessInfo().SetGroupID (Args::StringToUInt32 (option_arg, UINT32_MAX, 0, &success));
Greg Clayton24bc5d92011-03-30 18:16:51 +0000642 if (!success)
643 error.SetErrorStringWithFormat("invalid group ID string: '%s'", option_arg);
644 break;
645
646 case 'G':
647 match_info.GetProcessInfo().SetEffectiveGroupID (Args::StringToUInt32 (option_arg, UINT32_MAX, 0, &success));
648 if (!success)
649 error.SetErrorStringWithFormat("invalid effective group ID string: '%s'", option_arg);
650 break;
651
652 case 'a':
Greg Claytonf15996e2011-04-07 22:46:35 +0000653 match_info.GetProcessInfo().GetArchitecture().SetTriple (option_arg, m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform().get());
Greg Clayton24bc5d92011-03-30 18:16:51 +0000654 break;
655
656 case 'n':
Greg Clayton527154d2011-11-15 03:53:30 +0000657 match_info.GetProcessInfo().GetExecutableFile().SetFile (option_arg, false);
Greg Claytonb72d0f02011-04-12 05:54:46 +0000658 match_info.SetNameMatchType (eNameMatchEquals);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000659 break;
660
661 case 'e':
Greg Clayton527154d2011-11-15 03:53:30 +0000662 match_info.GetProcessInfo().GetExecutableFile().SetFile (option_arg, false);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000663 match_info.SetNameMatchType (eNameMatchEndsWith);
664 break;
665
666 case 's':
Greg Clayton527154d2011-11-15 03:53:30 +0000667 match_info.GetProcessInfo().GetExecutableFile().SetFile (option_arg, false);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000668 match_info.SetNameMatchType (eNameMatchStartsWith);
669 break;
670
671 case 'c':
Greg Clayton527154d2011-11-15 03:53:30 +0000672 match_info.GetProcessInfo().GetExecutableFile().SetFile (option_arg, false);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000673 match_info.SetNameMatchType (eNameMatchContains);
674 break;
675
676 case 'r':
Greg Clayton527154d2011-11-15 03:53:30 +0000677 match_info.GetProcessInfo().GetExecutableFile().SetFile (option_arg, false);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000678 match_info.SetNameMatchType (eNameMatchRegularExpression);
679 break;
680
Greg Claytonb72d0f02011-04-12 05:54:46 +0000681 case 'A':
682 show_args = true;
683 break;
684
685 case 'v':
686 verbose = true;
687 break;
688
Greg Clayton24bc5d92011-03-30 18:16:51 +0000689 default:
690 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
691 break;
692 }
693
694 return error;
695 }
696
697 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000698 OptionParsingStarting ()
Greg Clayton24bc5d92011-03-30 18:16:51 +0000699 {
700 match_info.Clear();
Greg Claytonb72d0f02011-04-12 05:54:46 +0000701 show_args = false;
702 verbose = false;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000703 }
704
705 const OptionDefinition*
706 GetDefinitions ()
707 {
708 return g_option_table;
709 }
710
711 // Options table: Required for subclasses of Options.
712
713 static OptionDefinition g_option_table[];
714
715 // Instance variables to hold the values for command options.
716
Greg Claytonb72d0f02011-04-12 05:54:46 +0000717 ProcessInstanceInfoMatch match_info;
718 bool show_args;
719 bool verbose;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000720 };
721 CommandOptions m_options;
722};
723
724OptionDefinition
725CommandObjectPlatformProcessList::CommandOptions::g_option_table[] =
726{
Greg Claytonb72d0f02011-04-12 05:54:46 +0000727{ LLDB_OPT_SET_1, false, "pid" , 'p', required_argument, NULL, 0, eArgTypePid , "List the process info for a specific process ID." },
728{ LLDB_OPT_SET_2, true , "name" , 'n', required_argument, NULL, 0, eArgTypeProcessName , "Find processes with executable basenames that match a string." },
729{ LLDB_OPT_SET_3, true , "ends-with" , 'e', required_argument, NULL, 0, eArgTypeNone , "Find processes with executable basenames that end with a string." },
730{ LLDB_OPT_SET_4, true , "starts-with" , 's', required_argument, NULL, 0, eArgTypeNone , "Find processes with executable basenames that start with a string." },
731{ LLDB_OPT_SET_5, true , "contains" , 'c', required_argument, NULL, 0, eArgTypeNone , "Find processes with executable basenames that contain a string." },
732{ LLDB_OPT_SET_6, true , "regex" , 'r', required_argument, NULL, 0, eArgTypeNone , "Find processes with executable basenames that match a regular expression." },
733{ ~LLDB_OPT_SET_1, false, "parent" , 'P', required_argument, NULL, 0, eArgTypePid , "Find processes that have a matching parent process ID." },
734{ ~LLDB_OPT_SET_1, false, "uid" , 'u', required_argument, NULL, 0, eArgTypeNone , "Find processes that have a matching user ID." },
735{ ~LLDB_OPT_SET_1, false, "euid" , 'U', required_argument, NULL, 0, eArgTypeNone , "Find processes that have a matching effective user ID." },
736{ ~LLDB_OPT_SET_1, false, "gid" , 'g', required_argument, NULL, 0, eArgTypeNone , "Find processes that have a matching group ID." },
737{ ~LLDB_OPT_SET_1, false, "egid" , 'G', required_argument, NULL, 0, eArgTypeNone , "Find processes that have a matching effective group ID." },
738{ ~LLDB_OPT_SET_1, false, "arch" , 'a', required_argument, NULL, 0, eArgTypeArchitecture , "Find processes that have a matching architecture." },
739{ LLDB_OPT_SET_ALL, false, "show-args" , 'A', no_argument , NULL, 0, eArgTypeNone , "Show process arguments instead of the process executable basename." },
740{ LLDB_OPT_SET_ALL, false, "verbose" , 'v', no_argument , NULL, 0, eArgTypeNone , "Enable verbose output." },
741{ 0 , false, NULL , 0 , 0 , NULL, 0, eArgTypeNone , NULL }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000742};
743
Greg Claytonff39f742011-04-01 00:29:43 +0000744//----------------------------------------------------------------------
745// "platform process info"
746//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +0000747class CommandObjectPlatformProcessInfo : public CommandObjectParsed
Greg Claytonff39f742011-04-01 00:29:43 +0000748{
749public:
750 CommandObjectPlatformProcessInfo (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000751 CommandObjectParsed (interpreter,
752 "platform process info",
753 "Get detailed information for one or more process by process ID.",
754 "platform process info <pid> [<pid> <pid> ...]",
755 0)
Greg Claytonff39f742011-04-01 00:29:43 +0000756 {
757 CommandArgumentEntry arg;
758 CommandArgumentData pid_args;
759
760 // Define the first (and only) variant of this arg.
761 pid_args.arg_type = eArgTypePid;
762 pid_args.arg_repetition = eArgRepeatStar;
763
764 // There is only one variant this argument could be; put it into the argument entry.
765 arg.push_back (pid_args);
766
767 // Push the data for the first argument into the m_arguments vector.
768 m_arguments.push_back (arg);
769 }
770
771 virtual
772 ~CommandObjectPlatformProcessInfo ()
773 {
774 }
775
Jim Inghamda26bd22012-06-08 21:56:10 +0000776protected:
Greg Claytonff39f742011-04-01 00:29:43 +0000777 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000778 DoExecute (Args& args, CommandReturnObject &result)
Greg Claytonff39f742011-04-01 00:29:43 +0000779 {
780 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
781 if (platform_sp)
782 {
783 const size_t argc = args.GetArgumentCount();
784 if (argc > 0)
785 {
786 Error error;
787
788 if (platform_sp->IsConnected())
789 {
790 Stream &ostrm = result.GetOutputStream();
791 bool success;
792 for (size_t i=0; i<argc; ++ i)
793 {
794 const char *arg = args.GetArgumentAtIndex(i);
795 lldb::pid_t pid = Args::StringToUInt32 (arg, LLDB_INVALID_PROCESS_ID, 0, &success);
796 if (success)
797 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000798 ProcessInstanceInfo proc_info;
Greg Claytonff39f742011-04-01 00:29:43 +0000799 if (platform_sp->GetProcessInfo (pid, proc_info))
800 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000801 ostrm.Printf ("Process information for process %" PRIu64 ":\n", pid);
Greg Claytonff39f742011-04-01 00:29:43 +0000802 proc_info.Dump (ostrm, platform_sp.get());
803 }
804 else
805 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000806 ostrm.Printf ("error: no process information is available for process %" PRIu64 "\n", pid);
Greg Claytonff39f742011-04-01 00:29:43 +0000807 }
808 ostrm.EOL();
809 }
810 else
811 {
812 result.AppendErrorWithFormat ("invalid process ID argument '%s'", arg);
813 result.SetStatus (eReturnStatusFailed);
814 break;
815 }
816 }
817 }
818 else
819 {
820 // Not connected...
821 result.AppendErrorWithFormat ("not connected to '%s'", platform_sp->GetShortPluginName());
822 result.SetStatus (eReturnStatusFailed);
823 }
824 }
825 else
826 {
Johnny Chen1736fef2011-05-09 19:05:46 +0000827 // No args
828 result.AppendError ("one or more process id(s) must be specified");
Greg Claytonff39f742011-04-01 00:29:43 +0000829 result.SetStatus (eReturnStatusFailed);
830 }
831 }
832 else
833 {
834 result.AppendError ("no platform is currently selected");
835 result.SetStatus (eReturnStatusFailed);
836 }
837 return result.Succeeded();
838 }
839};
840
841
842
843
Greg Clayton24bc5d92011-03-30 18:16:51 +0000844class CommandObjectPlatformProcess : public CommandObjectMultiword
845{
846public:
847 //------------------------------------------------------------------
848 // Constructors and Destructors
849 //------------------------------------------------------------------
850 CommandObjectPlatformProcess (CommandInterpreter &interpreter) :
851 CommandObjectMultiword (interpreter,
852 "platform process",
853 "A set of commands to query, launch and attach to platform processes",
854 "platform process [attach|launch|list] ...")
855 {
856// LoadSubCommand ("attach", CommandObjectSP (new CommandObjectPlatformProcessAttach (interpreter)));
Greg Claytonb72d0f02011-04-12 05:54:46 +0000857 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectPlatformProcessLaunch (interpreter)));
Greg Claytonff39f742011-04-01 00:29:43 +0000858 LoadSubCommand ("info" , CommandObjectSP (new CommandObjectPlatformProcessInfo (interpreter)));
Greg Clayton24bc5d92011-03-30 18:16:51 +0000859 LoadSubCommand ("list" , CommandObjectSP (new CommandObjectPlatformProcessList (interpreter)));
860
861 }
862
863 virtual
864 ~CommandObjectPlatformProcess ()
865 {
866 }
867
868private:
869 //------------------------------------------------------------------
870 // For CommandObjectPlatform only
871 //------------------------------------------------------------------
872 DISALLOW_COPY_AND_ASSIGN (CommandObjectPlatformProcess);
873};
Greg Claytonb1888f22011-03-19 01:12:21 +0000874
Greg Clayton97471182012-04-14 01:42:46 +0000875
Jim Inghamda26bd22012-06-08 21:56:10 +0000876class CommandObjectPlatformShell : public CommandObjectRaw
Greg Clayton97471182012-04-14 01:42:46 +0000877{
878public:
879 CommandObjectPlatformShell (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000880 CommandObjectRaw (interpreter,
881 "platform shell",
882 "Run a shell command on a the selected platform.",
883 "platform shell <shell-command>",
884 0)
Greg Clayton97471182012-04-14 01:42:46 +0000885 {
886 }
887
888 virtual
889 ~CommandObjectPlatformShell ()
890 {
891 }
892
Jim Inghamda26bd22012-06-08 21:56:10 +0000893protected:
Greg Clayton97471182012-04-14 01:42:46 +0000894 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000895 DoExecute (const char *raw_command_line, CommandReturnObject &result)
Greg Clayton97471182012-04-14 01:42:46 +0000896 {
897 // TODO: Implement "Platform::RunShellCommand()" and switch over to using
898 // the current platform when it is in the interface.
899 const char *working_dir = NULL;
900 std::string output;
901 int status = -1;
902 int signo = -1;
903 Error error (Host::RunShellCommand (raw_command_line, working_dir, &status, &signo, &output, 10));
904 if (!output.empty())
905 result.GetOutputStream().PutCString(output.c_str());
906 if (status > 0)
907 {
908 if (signo > 0)
909 {
910 const char *signo_cstr = Host::GetSignalAsCString(signo);
911 if (signo_cstr)
912 result.GetOutputStream().Printf("error: command returned with status %i and signal %s\n", status, signo_cstr);
913 else
914 result.GetOutputStream().Printf("error: command returned with status %i and signal %i\n", status, signo);
915 }
916 else
917 result.GetOutputStream().Printf("error: command returned with status %i\n", status);
918 }
919
920 if (error.Fail())
921 {
922 result.AppendError(error.AsCString());
923 result.SetStatus (eReturnStatusFailed);
924 }
925 else
926 {
927 result.SetStatus (eReturnStatusSuccessFinishResult);
928 }
929 return true;
930 }
Greg Clayton97471182012-04-14 01:42:46 +0000931};
932
Greg Claytonb1888f22011-03-19 01:12:21 +0000933//----------------------------------------------------------------------
934// CommandObjectPlatform constructor
935//----------------------------------------------------------------------
936CommandObjectPlatform::CommandObjectPlatform(CommandInterpreter &interpreter) :
937 CommandObjectMultiword (interpreter,
938 "platform",
939 "A set of commands to manage and create platforms.",
Greg Clayton143fcc32011-04-13 00:18:08 +0000940 "platform [connect|disconnect|info|list|status|select] ...")
Greg Claytonb1888f22011-03-19 01:12:21 +0000941{
Greg Claytonb1888f22011-03-19 01:12:21 +0000942 LoadSubCommand ("select", CommandObjectSP (new CommandObjectPlatformSelect (interpreter)));
Greg Clayton143fcc32011-04-13 00:18:08 +0000943 LoadSubCommand ("list" , CommandObjectSP (new CommandObjectPlatformList (interpreter)));
Greg Claytonb1888f22011-03-19 01:12:21 +0000944 LoadSubCommand ("status", CommandObjectSP (new CommandObjectPlatformStatus (interpreter)));
Greg Claytoncb8977d2011-03-23 00:09:55 +0000945 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectPlatformConnect (interpreter)));
946 LoadSubCommand ("disconnect", CommandObjectSP (new CommandObjectPlatformDisconnect (interpreter)));
Greg Clayton24bc5d92011-03-30 18:16:51 +0000947 LoadSubCommand ("process", CommandObjectSP (new CommandObjectPlatformProcess (interpreter)));
Greg Clayton97471182012-04-14 01:42:46 +0000948 LoadSubCommand ("shell", CommandObjectSP (new CommandObjectPlatformShell (interpreter)));
Greg Claytonb1888f22011-03-19 01:12:21 +0000949}
950
951
952//----------------------------------------------------------------------
953// Destructor
954//----------------------------------------------------------------------
955CommandObjectPlatform::~CommandObjectPlatform()
956{
957}