blob: dc6b4efb8dcf3c6e74052ab72ec95f8ba567f0ac [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 {
71 result.AppendError ("command not implemented");
72 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 {
206 result.AppendError ("no platforms are available");
207 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 {
246 result.AppendError ("no platform us currently selected");
247 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 {
277 result.AppendError ("command not implemented");
278 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
309 // Get rid of the "connect" from the args and leave the rest to the platform
310 args.Shift();
311 PlatformSP selected_platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
312 if (selected_platform_sp)
313 {
314 Error error (selected_platform_sp->ConnectRemote (args));
315 if (error.Success())
316 {
317 ostrm.Printf ("Connected to \"%s\"\n", selected_platform_sp->GetInstanceName());
318 selected_platform_sp->GetStatus (ostrm);
319 result.SetStatus (eReturnStatusSuccessFinishResult);
320 }
321 else
322 {
323 result.AppendErrorWithFormat ("connection failed: %s", error.AsCString());
324 result.SetStatus (eReturnStatusFailed);
325 }
326 }
327 else
328 {
329 result.AppendError ("no platform us currently selected");
330 result.SetStatus (eReturnStatusFailed);
331 }
332 return result.Succeeded();
333 }
334};
335
336//----------------------------------------------------------------------
337// "platform disconnect"
338//----------------------------------------------------------------------
339class CommandObjectPlatformDisconnect : public CommandObject
340{
341public:
342 CommandObjectPlatformDisconnect (CommandInterpreter &interpreter) :
343 CommandObject (interpreter,
344 "platform disconnect",
345 "Disconnect a platform by name to be the currently selected platform.",
346 "platform disconnect",
347 0)
348 {
349 }
350
351 virtual
352 ~CommandObjectPlatformDisconnect ()
353 {
354 }
355
356 virtual bool
357 Execute (Args& args, CommandReturnObject &result)
358 {
359 PlatformSP selected_platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
360 if (selected_platform_sp)
361 {
362 if (args.GetArgumentCount() == 0)
363 {
364 Error error;
365
366 if (selected_platform_sp->IsConnected())
367 {
368 // Cache the instance name if there is one since we are
369 // about to disconnect and the name might go with it.
370 const char *instance_name_cstr = selected_platform_sp->GetInstanceName();
371 std::string instance_name;
372 if (instance_name_cstr)
373 instance_name.assign (instance_name_cstr);
374
375 error = selected_platform_sp->DisconnectRemote ();
376 if (error.Success())
377 {
378 Stream &ostrm = result.GetOutputStream();
379 if (instance_name.empty())
380 ostrm.Printf ("Disconnected from \"%s\"\n", selected_platform_sp->GetShortPluginName());
381 else
382 ostrm.Printf ("Disconnected from \"%s\"\n", instance_name.c_str());
383 result.SetStatus (eReturnStatusSuccessFinishResult);
384 }
385 else
386 {
387 result.AppendErrorWithFormat ("disconnect failed: %s", error.AsCString());
388 result.SetStatus (eReturnStatusFailed);
389 }
390 }
391 else
392 {
393 // Not connected...
394 result.AppendError ("not connected.");
395 result.SetStatus (eReturnStatusFailed);
396 }
397 }
398 else
399 {
400 // Bad args
401 result.AppendError ("\"platform disconnect\" doesn't take any arguments");
402 result.SetStatus (eReturnStatusFailed);
403 }
404 }
405 else
406 {
407 result.AppendError ("no platform us currently selected");
408 result.SetStatus (eReturnStatusFailed);
409 }
410 return result.Succeeded();
411 }
412};
413
414
Greg Claytonb1888f22011-03-19 01:12:21 +0000415
416//----------------------------------------------------------------------
417// CommandObjectPlatform constructor
418//----------------------------------------------------------------------
419CommandObjectPlatform::CommandObjectPlatform(CommandInterpreter &interpreter) :
420 CommandObjectMultiword (interpreter,
421 "platform",
422 "A set of commands to manage and create platforms.",
Greg Claytoncb8977d2011-03-23 00:09:55 +0000423 "platform [connect|create|disconnect|list|status|select] ...")
Greg Claytonb1888f22011-03-19 01:12:21 +0000424{
425 LoadSubCommand ("create", CommandObjectSP (new CommandObjectPlatformCreate (interpreter)));
426 LoadSubCommand ("list" , CommandObjectSP (new CommandObjectPlatformList (interpreter)));
427 LoadSubCommand ("select", CommandObjectSP (new CommandObjectPlatformSelect (interpreter)));
428 LoadSubCommand ("status", CommandObjectSP (new CommandObjectPlatformStatus (interpreter)));
Greg Claytoncb8977d2011-03-23 00:09:55 +0000429 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectPlatformConnect (interpreter)));
430 LoadSubCommand ("disconnect", CommandObjectSP (new CommandObjectPlatformDisconnect (interpreter)));
Greg Claytonb1888f22011-03-19 01:12:21 +0000431}
432
433
434//----------------------------------------------------------------------
435// Destructor
436//----------------------------------------------------------------------
437CommandObjectPlatform::~CommandObjectPlatform()
438{
439}