blob: 0294d678822ca9f6614df03e05b659a547212d7b [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
10#include "CommandObjectPlatform.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Core/DataExtractor.h"
17#include "lldb/Core/Debugger.h"
18#include "lldb/Core/PluginManager.h"
19#include "lldb/Interpreter/Args.h"
20#include "lldb/Interpreter/CommandInterpreter.h"
21#include "lldb/Interpreter/CommandReturnObject.h"
22#include "lldb/Target/ExecutionContext.h"
23#include "lldb/Target/Platform.h"
24
25using namespace lldb;
26using namespace lldb_private;
27
28//----------------------------------------------------------------------
29// "platform create <platform-name>"
30//----------------------------------------------------------------------
31class CommandObjectPlatformCreate : public CommandObject
32{
33public:
34 CommandObjectPlatformCreate (CommandInterpreter &interpreter) :
35 CommandObject (interpreter,
36 "platform create",
37 "Create a platform instance by name and select it as the current platform.",
38 "platform create <platform-name>",
39 0)
40 {
41 }
42
43 virtual
44 ~CommandObjectPlatformCreate ()
45 {
46 }
47
48 virtual bool
49 Execute (Args& args, CommandReturnObject &result)
50 {
51 Error error;
52 if (args.GetArgumentCount() == 1)
53 {
54 PlatformSP platform_sp (Platform::Create (args.GetArgumentAtIndex (0), error));
55
56 if (platform_sp)
57 {
58 m_interpreter.GetDebugger().GetPlatformList().Append (platform_sp, true);
59 if (m_options.os_version_major != UINT32_MAX)
60 {
61 platform_sp->SetOSVersion (m_options.os_version_major,
62 m_options.os_version_minor,
63 m_options.os_version_update);
64 }
65
66 platform_sp->GetStatus (result.GetOutputStream());
67 }
68 }
69 else
70 {
Greg Clayton58e26e02011-03-24 04:28:38 +000071 result.AppendError ("command not implemented\n");
Greg Claytonb1888f22011-03-19 01:12:21 +000072 result.SetStatus (eReturnStatusFailed);
73 }
74 return result.Succeeded();
75 }
76
77 virtual Options *
78 GetOptions ()
79 {
80 return &m_options;
81 }
82
83protected:
84
85 class CommandOptions : public Options
86 {
87 public:
88
89 CommandOptions () :
90 os_version_major (UINT32_MAX),
91 os_version_minor (UINT32_MAX),
92 os_version_update (UINT32_MAX)
93 {
94 }
95
96 virtual
97 ~CommandOptions ()
98 {
99 }
100
101 virtual Error
102 SetOptionValue (int option_idx, const char *option_arg)
103 {
104 Error error;
105 char short_option = (char) m_getopt_table[option_idx].val;
106
107 switch (short_option)
108 {
109 case 'v':
110 if (Args::StringToVersion (option_arg,
111 os_version_major,
112 os_version_minor,
113 os_version_update) == option_arg)
114 {
115 error.SetErrorStringWithFormat ("invalid version string '%s'", option_arg);
116 }
117 break;
118
119 default:
120 error.SetErrorStringWithFormat ("Unrecognized option '%c'.\n", short_option);
121 break;
122 }
123
124 return error;
125 }
126
127 void
128 ResetOptionValues ()
129 {
130 os_version_major = UINT32_MAX;
131 os_version_minor = UINT32_MAX;
132 os_version_update = UINT32_MAX;
133 }
134
135 const lldb::OptionDefinition*
136 GetDefinitions ()
137 {
138 return g_option_table;
139 }
140
141 // Options table: Required for subclasses of Options.
142
143 static lldb::OptionDefinition g_option_table[];
144
145 // Instance variables to hold the values for command options.
146
147 uint32_t os_version_major;
148 uint32_t os_version_minor;
149 uint32_t os_version_update;
150 };
151 CommandOptions m_options;
152};
153
154lldb::OptionDefinition
155CommandObjectPlatformCreate::CommandOptions::g_option_table[] =
156{
157 { LLDB_OPT_SET_ALL, false, "sdk-version", 'v', required_argument, NULL, 0, eArgTypeNone, "Specify the initial SDK version to use prior to connecting." },
158 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
159};
160
161//----------------------------------------------------------------------
162// "platform list"
163//----------------------------------------------------------------------
164class CommandObjectPlatformList : public CommandObject
165{
166public:
167 CommandObjectPlatformList (CommandInterpreter &interpreter) :
168 CommandObject (interpreter,
169 "platform list",
170 "List all platforms that are available.",
171 NULL,
172 0)
173 {
174 }
175
176 virtual
177 ~CommandObjectPlatformList ()
178 {
179 }
180
181 virtual bool
182 Execute (Args& args, CommandReturnObject &result)
183 {
184 Stream &ostrm = result.GetOutputStream();
185 ostrm.Printf("Available platforms:\n");
186
187 PlatformSP host_platform_sp (Platform::GetDefaultPlatform());
188 ostrm.Printf ("%s: %s\n",
189 host_platform_sp->GetShortPluginName(),
190 host_platform_sp->GetDescription());
191
192 uint32_t idx;
193 for (idx = 0; 1; ++idx)
194 {
195 const char *plugin_name = PluginManager::GetPlatformPluginNameAtIndex (idx);
196 if (plugin_name == NULL)
197 break;
198 const char *plugin_desc = PluginManager::GetPlatformPluginDescriptionAtIndex (idx);
199 if (plugin_desc == NULL)
200 break;
201 ostrm.Printf("%s: %s\n", plugin_name, plugin_desc);
202 }
203
204 if (idx == 0)
205 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000206 result.AppendError ("no platforms are available\n");
Greg Claytonb1888f22011-03-19 01:12:21 +0000207 result.SetStatus (eReturnStatusFailed);
208 }
209 return result.Succeeded();
210 }
211};
212
213//----------------------------------------------------------------------
214// "platform status"
215//----------------------------------------------------------------------
216class CommandObjectPlatformStatus : public CommandObject
217{
218public:
219 CommandObjectPlatformStatus (CommandInterpreter &interpreter) :
220 CommandObject (interpreter,
221 "platform status",
222 "Display status for the currently selected platform.",
223 NULL,
224 0)
225 {
226 }
227
228 virtual
229 ~CommandObjectPlatformStatus ()
230 {
231 }
232
233 virtual bool
234 Execute (Args& args, CommandReturnObject &result)
235 {
236 Stream &ostrm = result.GetOutputStream();
237
238 PlatformSP selected_platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
239 if (selected_platform_sp)
240 {
241 selected_platform_sp->GetStatus (ostrm);
242 result.SetStatus (eReturnStatusSuccessFinishResult);
243 }
244 else
245 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000246 result.AppendError ("no platform us currently selected\n");
Greg Claytonb1888f22011-03-19 01:12:21 +0000247 result.SetStatus (eReturnStatusFailed);
248 }
249 return result.Succeeded();
250 }
251};
252
253
254//----------------------------------------------------------------------
255// "platform select <platform-name>"
256//----------------------------------------------------------------------
257class CommandObjectPlatformSelect : public CommandObject
258{
259public:
260 CommandObjectPlatformSelect (CommandInterpreter &interpreter) :
261 CommandObject (interpreter,
262 "platform select",
263 "Select a platform by name to be the currently selected platform.",
264 "platform select <platform-name>",
265 0)
266 {
267 }
268
269 virtual
270 ~CommandObjectPlatformSelect ()
271 {
272 }
273
274 virtual bool
275 Execute (Args& args, CommandReturnObject &result)
276 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000277 result.AppendError ("command not implemented\n");
Greg Claytonb1888f22011-03-19 01:12:21 +0000278 result.SetStatus (eReturnStatusFailed);
279 return result.Succeeded();
280 }
281};
282
283
Greg Claytoncb8977d2011-03-23 00:09:55 +0000284//----------------------------------------------------------------------
285// "platform connect <connect-url>"
286//----------------------------------------------------------------------
287class CommandObjectPlatformConnect : public CommandObject
288{
289public:
290 CommandObjectPlatformConnect (CommandInterpreter &interpreter) :
291 CommandObject (interpreter,
292 "platform connect",
293 "Connect a platform by name to be the currently selected platform.",
294 "platform connect <connect-url>",
295 0)
296 {
297 }
298
299 virtual
300 ~CommandObjectPlatformConnect ()
301 {
302 }
303
304 virtual bool
305 Execute (Args& args, CommandReturnObject &result)
306 {
307 Stream &ostrm = result.GetOutputStream();
308
Greg Claytoncb8977d2011-03-23 00:09:55 +0000309 PlatformSP selected_platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
310 if (selected_platform_sp)
311 {
312 Error error (selected_platform_sp->ConnectRemote (args));
313 if (error.Success())
314 {
Greg Claytoncb8977d2011-03-23 00:09:55 +0000315 selected_platform_sp->GetStatus (ostrm);
316 result.SetStatus (eReturnStatusSuccessFinishResult);
317 }
318 else
319 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000320 result.AppendErrorWithFormat ("%s\n", error.AsCString());
Greg Claytoncb8977d2011-03-23 00:09:55 +0000321 result.SetStatus (eReturnStatusFailed);
322 }
323 }
324 else
325 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000326 result.AppendError ("no platform us currently selected\n");
Greg Claytoncb8977d2011-03-23 00:09:55 +0000327 result.SetStatus (eReturnStatusFailed);
328 }
329 return result.Succeeded();
330 }
331};
332
333//----------------------------------------------------------------------
334// "platform disconnect"
335//----------------------------------------------------------------------
336class CommandObjectPlatformDisconnect : public CommandObject
337{
338public:
339 CommandObjectPlatformDisconnect (CommandInterpreter &interpreter) :
340 CommandObject (interpreter,
341 "platform disconnect",
342 "Disconnect a platform by name to be the currently selected platform.",
343 "platform disconnect",
344 0)
345 {
346 }
347
348 virtual
349 ~CommandObjectPlatformDisconnect ()
350 {
351 }
352
353 virtual bool
354 Execute (Args& args, CommandReturnObject &result)
355 {
356 PlatformSP selected_platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
357 if (selected_platform_sp)
358 {
359 if (args.GetArgumentCount() == 0)
360 {
361 Error error;
362
363 if (selected_platform_sp->IsConnected())
364 {
365 // Cache the instance name if there is one since we are
366 // about to disconnect and the name might go with it.
Greg Clayton58e26e02011-03-24 04:28:38 +0000367 const char *hostname_cstr = selected_platform_sp->GetHostname();
368 std::string hostname;
369 if (hostname_cstr)
370 hostname.assign (hostname_cstr);
Greg Claytoncb8977d2011-03-23 00:09:55 +0000371
372 error = selected_platform_sp->DisconnectRemote ();
373 if (error.Success())
374 {
375 Stream &ostrm = result.GetOutputStream();
Greg Clayton58e26e02011-03-24 04:28:38 +0000376 if (hostname.empty())
Greg Claytoncb8977d2011-03-23 00:09:55 +0000377 ostrm.Printf ("Disconnected from \"%s\"\n", selected_platform_sp->GetShortPluginName());
378 else
Greg Clayton58e26e02011-03-24 04:28:38 +0000379 ostrm.Printf ("Disconnected from \"%s\"\n", hostname.c_str());
Greg Claytoncb8977d2011-03-23 00:09:55 +0000380 result.SetStatus (eReturnStatusSuccessFinishResult);
381 }
382 else
383 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000384 result.AppendErrorWithFormat ("%s", error.AsCString());
Greg Claytoncb8977d2011-03-23 00:09:55 +0000385 result.SetStatus (eReturnStatusFailed);
386 }
387 }
388 else
389 {
390 // Not connected...
Greg Clayton58e26e02011-03-24 04:28:38 +0000391 result.AppendErrorWithFormat ("not connected to '%s'", selected_platform_sp->GetShortPluginName());
Greg Claytoncb8977d2011-03-23 00:09:55 +0000392 result.SetStatus (eReturnStatusFailed);
393 }
394 }
395 else
396 {
397 // Bad args
398 result.AppendError ("\"platform disconnect\" doesn't take any arguments");
399 result.SetStatus (eReturnStatusFailed);
400 }
401 }
402 else
403 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000404 result.AppendError ("no platform is currently selected");
Greg Claytoncb8977d2011-03-23 00:09:55 +0000405 result.SetStatus (eReturnStatusFailed);
406 }
407 return result.Succeeded();
408 }
409};
410
411
Greg Claytonb1888f22011-03-19 01:12:21 +0000412
413//----------------------------------------------------------------------
414// CommandObjectPlatform constructor
415//----------------------------------------------------------------------
416CommandObjectPlatform::CommandObjectPlatform(CommandInterpreter &interpreter) :
417 CommandObjectMultiword (interpreter,
418 "platform",
419 "A set of commands to manage and create platforms.",
Greg Claytoncb8977d2011-03-23 00:09:55 +0000420 "platform [connect|create|disconnect|list|status|select] ...")
Greg Claytonb1888f22011-03-19 01:12:21 +0000421{
422 LoadSubCommand ("create", CommandObjectSP (new CommandObjectPlatformCreate (interpreter)));
423 LoadSubCommand ("list" , CommandObjectSP (new CommandObjectPlatformList (interpreter)));
424 LoadSubCommand ("select", CommandObjectSP (new CommandObjectPlatformSelect (interpreter)));
425 LoadSubCommand ("status", CommandObjectSP (new CommandObjectPlatformStatus (interpreter)));
Greg Claytoncb8977d2011-03-23 00:09:55 +0000426 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectPlatformConnect (interpreter)));
427 LoadSubCommand ("disconnect", CommandObjectSP (new CommandObjectPlatformDisconnect (interpreter)));
Greg Claytonb1888f22011-03-19 01:12:21 +0000428}
429
430
431//----------------------------------------------------------------------
432// Destructor
433//----------------------------------------------------------------------
434CommandObjectPlatform::~CommandObjectPlatform()
435{
436}