blob: 563cbe298dc8d838a5ac237adb340a3798f53692 [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() :
Greg Clayton143fcc32011-04-13 00:18:08 +000029 m_arch_str ()
Chris Lattner24943d22010-06-08 16:52:24 +000030{
Chris Lattner24943d22010-06-08 16:52:24 +000031}
32
Greg Clayton143fcc32011-04-13 00:18:08 +000033FileOptionGroup::~FileOptionGroup ()
Chris Lattner24943d22010-06-08 16:52:24 +000034{
35}
36
Greg Clayton143fcc32011-04-13 00:18:08 +000037OptionDefinition g_file_option_table[] =
Chris Lattner24943d22010-06-08 16:52:24 +000038{
Greg Clayton143fcc32011-04-13 00:18:08 +000039 { 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 +000040};
Greg Clayton143fcc32011-04-13 00:18:08 +000041const uint32_t k_num_file_options = sizeof(g_file_option_table)/sizeof(OptionDefinition);
42
43uint32_t
44FileOptionGroup::GetNumDefinitions ()
45{
46 return k_num_file_options;
47}
Chris Lattner24943d22010-06-08 16:52:24 +000048
Greg Claytonb3448432011-03-24 21:19:54 +000049const OptionDefinition *
Greg Clayton143fcc32011-04-13 00:18:08 +000050FileOptionGroup::GetDefinitions ()
Chris Lattner24943d22010-06-08 16:52:24 +000051{
Greg Clayton143fcc32011-04-13 00:18:08 +000052 return g_file_option_table;
Chris Lattner24943d22010-06-08 16:52:24 +000053}
54
Greg Clayton5e342f52011-04-13 22:47:15 +000055bool
56FileOptionGroup::GetArchitecture (Platform *platform, ArchSpec &arch)
57{
58 if (m_arch_str.empty())
59 arch.Clear();
60 else
61 arch.SetTriple(m_arch_str.c_str(), platform);
62 return arch.IsValid();
63}
64
65
Chris Lattner24943d22010-06-08 16:52:24 +000066Error
Greg Clayton143fcc32011-04-13 00:18:08 +000067FileOptionGroup::SetOptionValue (CommandInterpreter &interpreter,
68 uint32_t option_idx,
69 const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +000070{
71 Error error;
Greg Clayton143fcc32011-04-13 00:18:08 +000072 char short_option = (char) g_file_option_table[option_idx].short_option;
Chris Lattner24943d22010-06-08 16:52:24 +000073
74 switch (short_option)
75 {
76 case 'a':
Greg Clayton5e342f52011-04-13 22:47:15 +000077 m_arch_str.assign (option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +000078 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{
Greg Clayton5e342f52011-04-13 22:47:15 +000091 m_arch_str.clear();
Greg Clayton143fcc32011-04-13 00:18:08 +000092}
93
Chris Lattner24943d22010-06-08 16:52:24 +000094//-------------------------------------------------------------------------
95// CommandObjectFile
96//-------------------------------------------------------------------------
97
Greg Clayton238c0a12010-09-18 01:14:36 +000098CommandObjectFile::CommandObjectFile(CommandInterpreter &interpreter) :
99 CommandObject (interpreter,
100 "file",
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000101 "Set the file to be used as the main executable by the debugger.",
Greg Claytonf15996e2011-04-07 22:46:35 +0000102 NULL),
Greg Clayton143fcc32011-04-13 00:18:08 +0000103 m_option_group (interpreter),
104 m_file_options (),
105 m_platform_options(true) // Do include the "--platform" option in the platform settings by passing true
Chris Lattner24943d22010-06-08 16:52:24 +0000106{
Caroline Tice43b014a2010-10-04 22:28:36 +0000107 CommandArgumentEntry arg;
108 CommandArgumentData file_arg;
109
110 // Define the first (and only) variant of this arg.
111 file_arg.arg_type = eArgTypeFilename;
112 file_arg.arg_repetition = eArgRepeatPlain;
113
114 // There is only one variant this argument could be; put it into the argument entry.
115 arg.push_back (file_arg);
116
117 // Push the data for the first argument into the m_arguments vector.
118 m_arguments.push_back (arg);
Greg Clayton143fcc32011-04-13 00:18:08 +0000119
Greg Clayton5e342f52011-04-13 22:47:15 +0000120 m_option_group.Append (&m_file_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
121 m_option_group.Append (&m_platform_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton143fcc32011-04-13 00:18:08 +0000122 m_option_group.Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000123}
124
125CommandObjectFile::~CommandObjectFile ()
126{
127}
128
129Options *
130CommandObjectFile::GetOptions ()
131{
Greg Clayton143fcc32011-04-13 00:18:08 +0000132 return &m_option_group;
Chris Lattner24943d22010-06-08 16:52:24 +0000133}
134
135bool
136CommandObjectFile::Execute
137(
138 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000139 CommandReturnObject &result
140)
141{
142 const char *file_path = command.GetArgumentAtIndex(0);
Caroline Tice31fbb642010-09-08 22:08:58 +0000143 Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) file '%s'", file_path);
Chris Lattner24943d22010-06-08 16:52:24 +0000144 const int argc = command.GetArgumentCount();
145 if (argc == 1)
146 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000147 FileSpec file_spec (file_path, true);
Greg Clayton5e342f52011-04-13 22:47:15 +0000148
149 bool select = true;
150 PlatformSP platform_sp;
151
152 Error error;
153
154 if (!m_platform_options.platform_name.empty())
155 {
156 platform_sp = m_platform_options.CreatePlatformWithOptions(m_interpreter, select, error);
157 if (!platform_sp)
158 {
159 result.AppendError(error.AsCString());
160 result.SetStatus (eReturnStatusFailed);
161 return false;
162 }
163 }
164 ArchSpec file_arch;
165
166 if (!m_file_options.m_arch_str.empty())
167 {
168 if (!platform_sp)
169 platform_sp = m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform();
170 if (!m_file_options.GetArchitecture(platform_sp.get(), file_arch))
171 {
172 result.AppendErrorWithFormat("invalid architecture '%s'", m_file_options.m_arch_str.c_str());
173 result.SetStatus (eReturnStatusFailed);
174 return false;
175 }
176 }
Chris Lattner24943d22010-06-08 16:52:24 +0000177
Caroline Ticeeddffe92010-09-10 04:48:55 +0000178 if (! file_spec.Exists() && !file_spec.ResolveExecutableLocation())
Chris Lattner24943d22010-06-08 16:52:24 +0000179 {
180 result.AppendErrorWithFormat ("File '%s' does not exist.\n", file_path);
181 result.SetStatus (eReturnStatusFailed);
Greg Clayton5e342f52011-04-13 22:47:15 +0000182 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000183 }
184
185 TargetSP target_sp;
Greg Clayton238c0a12010-09-18 01:14:36 +0000186 Debugger &debugger = m_interpreter.GetDebugger();
Greg Clayton5e342f52011-04-13 22:47:15 +0000187 error = debugger.GetTargetList().CreateTarget (debugger, file_spec, file_arch, true, target_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000188
189 if (target_sp)
190 {
Jim Inghamc8332952010-08-26 21:32:51 +0000191 debugger.GetTargetList().SetSelectedTarget(target_sp.get());
Greg Clayton940b1032011-02-23 00:35:02 +0000192 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, target_sp->GetArchitecture().GetArchitectureName());
Chris Lattner24943d22010-06-08 16:52:24 +0000193 result.SetStatus (eReturnStatusSuccessFinishNoResult);
194 }
195 else
196 {
197 result.AppendError(error.AsCString());
198 result.SetStatus (eReturnStatusFailed);
199 }
200 }
201 else
202 {
203 result.AppendErrorWithFormat("'%s' takes exactly one executable path argument.\n", m_cmd_name.c_str());
204 result.SetStatus (eReturnStatusFailed);
205 }
206 return result.Succeeded();
207
208}
Jim Ingham802f8b02010-06-30 05:02:46 +0000209
210int
Greg Clayton238c0a12010-09-18 01:14:36 +0000211CommandObjectFile::HandleArgumentCompletion
212(
213 Args &input,
214 int &cursor_index,
215 int &cursor_char_position,
216 OptionElementVector &opt_element_vector,
217 int match_start_point,
218 int max_return_elements,
219 bool &word_complete,
220 StringList &matches
221)
Jim Ingham802f8b02010-06-30 05:02:46 +0000222{
Greg Clayton238c0a12010-09-18 01:14:36 +0000223 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
224 completion_str.erase (cursor_char_position);
Jim Ingham802f8b02010-06-30 05:02:46 +0000225
Greg Clayton238c0a12010-09-18 01:14:36 +0000226 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
227 CommandCompletions::eDiskFileCompletion,
228 completion_str.c_str(),
229 match_start_point,
230 max_return_elements,
231 NULL,
232 word_complete,
233 matches);
234 return matches.GetSize();
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000235}