blob: 372baf9e822931be1ac8c481a940efba8b8a0342 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- CommandObjectFile.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 "CommandObjectFile.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Jim Ingham84cdc152010-06-15 19:49:27 +000016#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/Debugger.h"
18#include "lldb/Core/Timer.h"
Greg Clayton63094e02010-06-23 01:19:29 +000019#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Interpreter/CommandInterpreter.h"
21#include "lldb/Interpreter/CommandReturnObject.h"
Jim Ingham802f8b02010-06-30 05:02:46 +000022#include "lldb/Interpreter/CommandCompletions.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Target/Process.h"
24
25using namespace lldb;
26using namespace lldb_private;
27
Greg Clayton143fcc32011-04-13 00:18:08 +000028FileOptionGroup::FileOptionGroup() :
29 m_arch (),
30 m_arch_str ()
Chris Lattner24943d22010-06-08 16:52:24 +000031{
Chris Lattner24943d22010-06-08 16:52:24 +000032}
33
Greg Clayton143fcc32011-04-13 00:18:08 +000034FileOptionGroup::~FileOptionGroup ()
Chris Lattner24943d22010-06-08 16:52:24 +000035{
36}
37
Greg Clayton143fcc32011-04-13 00:18:08 +000038OptionDefinition g_file_option_table[] =
Chris Lattner24943d22010-06-08 16:52:24 +000039{
Greg Clayton143fcc32011-04-13 00:18:08 +000040 { LLDB_OPT_SET_1 , false, "arch" , 'a', required_argument, NULL, 0, eArgTypeArchitecture , "Specify the architecture for the target."},
Chris Lattner24943d22010-06-08 16:52:24 +000041};
Greg Clayton143fcc32011-04-13 00:18:08 +000042const uint32_t k_num_file_options = sizeof(g_file_option_table)/sizeof(OptionDefinition);
43
44uint32_t
45FileOptionGroup::GetNumDefinitions ()
46{
47 return k_num_file_options;
48}
Chris Lattner24943d22010-06-08 16:52:24 +000049
Greg Claytonb3448432011-03-24 21:19:54 +000050const OptionDefinition *
Greg Clayton143fcc32011-04-13 00:18:08 +000051FileOptionGroup::GetDefinitions ()
Chris Lattner24943d22010-06-08 16:52:24 +000052{
Greg Clayton143fcc32011-04-13 00:18:08 +000053 return g_file_option_table;
Chris Lattner24943d22010-06-08 16:52:24 +000054}
55
56Error
Greg Clayton143fcc32011-04-13 00:18:08 +000057FileOptionGroup::SetOptionValue (CommandInterpreter &interpreter,
58 uint32_t option_idx,
59 const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +000060{
61 Error error;
Greg Clayton143fcc32011-04-13 00:18:08 +000062 char short_option = (char) g_file_option_table[option_idx].short_option;
Chris Lattner24943d22010-06-08 16:52:24 +000063
64 switch (short_option)
65 {
66 case 'a':
67 {
Greg Clayton143fcc32011-04-13 00:18:08 +000068 // Save the arch value in case we specify a platform after specifying the arch
69 m_arch_str.assign (option_arg);
70 // Check to see if we already have a platform?
71 m_arch_platform_sp = interpreter.GetPlatform (false);
72 ArchSpec option_arch (option_arg, m_arch_platform_sp.get());
Chris Lattner24943d22010-06-08 16:52:24 +000073 if (option_arch.IsValid())
74 m_arch = option_arch;
75 else
Jim Ingham7a4c8ea2011-03-22 01:53:33 +000076 error.SetErrorStringWithFormat ("Invalid arch string '%s'.\n", option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +000077 }
78 break;
79
80 default:
81 error.SetErrorStringWithFormat ("Unrecognized option '%c'.\n", short_option);
82 break;
83 }
84
85 return error;
86}
87
88void
Greg Clayton143fcc32011-04-13 00:18:08 +000089FileOptionGroup::OptionParsingStarting (CommandInterpreter &interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000090{
Chris Lattner24943d22010-06-08 16:52:24 +000091 m_arch.Clear();
92}
93
Greg Clayton143fcc32011-04-13 00:18:08 +000094Error
95FileOptionGroup::OptionParsingFinished (CommandInterpreter &interpreter)
96{
97 Error error;
98 if (m_arch.IsValid())
99 {
100 PlatformSP curr_platform_sp (interpreter.GetPlatform (false));
101 if (curr_platform_sp.get() != m_arch_platform_sp.get())
102 {
103 ArchSpec option_arch (m_arch_str.c_str(), curr_platform_sp.get());
104 if (option_arch.IsValid())
105 m_arch = option_arch;
106 else
107 error.SetErrorStringWithFormat ("invalid arch '%s' for platform '%s'", m_arch_str.c_str(), curr_platform_sp->GetName());
108 }
109 }
110 return error;
111}
112
Chris Lattner24943d22010-06-08 16:52:24 +0000113//-------------------------------------------------------------------------
114// CommandObjectFile
115//-------------------------------------------------------------------------
116
Greg Clayton238c0a12010-09-18 01:14:36 +0000117CommandObjectFile::CommandObjectFile(CommandInterpreter &interpreter) :
118 CommandObject (interpreter,
119 "file",
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000120 "Set the file to be used as the main executable by the debugger.",
Greg Claytonf15996e2011-04-07 22:46:35 +0000121 NULL),
Greg Clayton143fcc32011-04-13 00:18:08 +0000122 m_option_group (interpreter),
123 m_file_options (),
124 m_platform_options(true) // Do include the "--platform" option in the platform settings by passing true
Chris Lattner24943d22010-06-08 16:52:24 +0000125{
Caroline Tice43b014a2010-10-04 22:28:36 +0000126 CommandArgumentEntry arg;
127 CommandArgumentData file_arg;
128
129 // Define the first (and only) variant of this arg.
130 file_arg.arg_type = eArgTypeFilename;
131 file_arg.arg_repetition = eArgRepeatPlain;
132
133 // There is only one variant this argument could be; put it into the argument entry.
134 arg.push_back (file_arg);
135
136 // Push the data for the first argument into the m_arguments vector.
137 m_arguments.push_back (arg);
Greg Clayton143fcc32011-04-13 00:18:08 +0000138
139 m_option_group.Append (&m_file_options);
140 m_option_group.Append (&m_platform_options);
141 m_option_group.Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000142}
143
144CommandObjectFile::~CommandObjectFile ()
145{
146}
147
148Options *
149CommandObjectFile::GetOptions ()
150{
Greg Clayton143fcc32011-04-13 00:18:08 +0000151 return &m_option_group;
Chris Lattner24943d22010-06-08 16:52:24 +0000152}
153
154bool
155CommandObjectFile::Execute
156(
157 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000158 CommandReturnObject &result
159)
160{
161 const char *file_path = command.GetArgumentAtIndex(0);
Caroline Tice31fbb642010-09-08 22:08:58 +0000162 Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) file '%s'", file_path);
Chris Lattner24943d22010-06-08 16:52:24 +0000163 const int argc = command.GetArgumentCount();
164 if (argc == 1)
165 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000166 FileSpec file_spec (file_path, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000167
Caroline Ticeeddffe92010-09-10 04:48:55 +0000168 if (! file_spec.Exists() && !file_spec.ResolveExecutableLocation())
Chris Lattner24943d22010-06-08 16:52:24 +0000169 {
170 result.AppendErrorWithFormat ("File '%s' does not exist.\n", file_path);
171 result.SetStatus (eReturnStatusFailed);
172 return result.Succeeded();
173 }
174
175 TargetSP target_sp;
176
Greg Clayton238c0a12010-09-18 01:14:36 +0000177 Debugger &debugger = m_interpreter.GetDebugger();
Greg Clayton143fcc32011-04-13 00:18:08 +0000178 Error error = debugger.GetTargetList().CreateTarget (debugger, file_spec, m_file_options.m_arch, true, target_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000179
180 if (target_sp)
181 {
Jim Inghamc8332952010-08-26 21:32:51 +0000182 debugger.GetTargetList().SetSelectedTarget(target_sp.get());
Greg Clayton940b1032011-02-23 00:35:02 +0000183 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, target_sp->GetArchitecture().GetArchitectureName());
Chris Lattner24943d22010-06-08 16:52:24 +0000184 result.SetStatus (eReturnStatusSuccessFinishNoResult);
185 }
186 else
187 {
188 result.AppendError(error.AsCString());
189 result.SetStatus (eReturnStatusFailed);
190 }
191 }
192 else
193 {
194 result.AppendErrorWithFormat("'%s' takes exactly one executable path argument.\n", m_cmd_name.c_str());
195 result.SetStatus (eReturnStatusFailed);
196 }
197 return result.Succeeded();
198
199}
Jim Ingham802f8b02010-06-30 05:02:46 +0000200
201int
Greg Clayton238c0a12010-09-18 01:14:36 +0000202CommandObjectFile::HandleArgumentCompletion
203(
204 Args &input,
205 int &cursor_index,
206 int &cursor_char_position,
207 OptionElementVector &opt_element_vector,
208 int match_start_point,
209 int max_return_elements,
210 bool &word_complete,
211 StringList &matches
212)
Jim Ingham802f8b02010-06-30 05:02:46 +0000213{
Greg Clayton238c0a12010-09-18 01:14:36 +0000214 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
215 completion_str.erase (cursor_char_position);
Jim Ingham802f8b02010-06-30 05:02:46 +0000216
Greg Clayton238c0a12010-09-18 01:14:36 +0000217 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
218 CommandCompletions::eDiskFileCompletion,
219 completion_str.c_str(),
220 match_start_point,
221 max_return_elements,
222 NULL,
223 word_complete,
224 matches);
225 return matches.GetSize();
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000226}