blob: 6f1267ebf1d4df308ed9e7872848ed3f3ca6a1cb [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
28CommandObjectFile::CommandOptions::CommandOptions() :
29 Options (),
30 m_arch () // Breakpoint info defaults to brief descriptions
31{
Chris Lattner24943d22010-06-08 16:52:24 +000032}
33
34CommandObjectFile::CommandOptions::~CommandOptions ()
35{
36}
37
Greg Claytonb3448432011-03-24 21:19:54 +000038OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +000039CommandObjectFile::CommandOptions::g_option_table[] =
40{
Caroline Tice4d6675c2010-10-01 19:59:14 +000041 { LLDB_OPT_SET_1, false, "arch", 'a', required_argument, NULL, 0, eArgTypeArchitecture, "Specify the architecture to be used when the process is launched."},
42 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +000043};
44
Greg Claytonb3448432011-03-24 21:19:54 +000045const OptionDefinition *
Chris Lattner24943d22010-06-08 16:52:24 +000046CommandObjectFile::CommandOptions::GetDefinitions ()
47{
48 return g_option_table;
49}
50
51Error
52CommandObjectFile::CommandOptions::SetOptionValue (int option_idx, const char *option_arg)
53{
54 Error error;
55 char short_option = (char) m_getopt_table[option_idx].val;
56
57 switch (short_option)
58 {
59 case 'a':
60 {
61 ArchSpec option_arch (option_arg);
62 if (option_arch.IsValid())
63 m_arch = option_arch;
64 else
Jim Ingham7a4c8ea2011-03-22 01:53:33 +000065 error.SetErrorStringWithFormat ("Invalid arch string '%s'.\n", option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +000066 }
67 break;
68
69 default:
70 error.SetErrorStringWithFormat ("Unrecognized option '%c'.\n", short_option);
71 break;
72 }
73
74 return error;
75}
76
77void
78CommandObjectFile::CommandOptions::ResetOptionValues ()
79{
Chris Lattner24943d22010-06-08 16:52:24 +000080 m_arch.Clear();
81}
82
83//-------------------------------------------------------------------------
84// CommandObjectFile
85//-------------------------------------------------------------------------
86
Greg Clayton238c0a12010-09-18 01:14:36 +000087CommandObjectFile::CommandObjectFile(CommandInterpreter &interpreter) :
88 CommandObject (interpreter,
89 "file",
Caroline Ticec1ad82e2010-09-07 22:38:08 +000090 "Set the file to be used as the main executable by the debugger.",
Caroline Tice43b014a2010-10-04 22:28:36 +000091 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +000092{
Caroline Tice43b014a2010-10-04 22:28:36 +000093 CommandArgumentEntry arg;
94 CommandArgumentData file_arg;
95
96 // Define the first (and only) variant of this arg.
97 file_arg.arg_type = eArgTypeFilename;
98 file_arg.arg_repetition = eArgRepeatPlain;
99
100 // There is only one variant this argument could be; put it into the argument entry.
101 arg.push_back (file_arg);
102
103 // Push the data for the first argument into the m_arguments vector.
104 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000105}
106
107CommandObjectFile::~CommandObjectFile ()
108{
109}
110
111Options *
112CommandObjectFile::GetOptions ()
113{
114 return &m_options;
115}
116
117bool
118CommandObjectFile::Execute
119(
120 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000121 CommandReturnObject &result
122)
123{
124 const char *file_path = command.GetArgumentAtIndex(0);
Caroline Tice31fbb642010-09-08 22:08:58 +0000125 Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) file '%s'", file_path);
Chris Lattner24943d22010-06-08 16:52:24 +0000126 const int argc = command.GetArgumentCount();
127 if (argc == 1)
128 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000129 FileSpec file_spec (file_path, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000130
Caroline Ticeeddffe92010-09-10 04:48:55 +0000131 if (! file_spec.Exists() && !file_spec.ResolveExecutableLocation())
Chris Lattner24943d22010-06-08 16:52:24 +0000132 {
133 result.AppendErrorWithFormat ("File '%s' does not exist.\n", file_path);
134 result.SetStatus (eReturnStatusFailed);
135 return result.Succeeded();
136 }
137
138 TargetSP target_sp;
139
Greg Clayton238c0a12010-09-18 01:14:36 +0000140 Debugger &debugger = m_interpreter.GetDebugger();
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000141 Error error = debugger.GetTargetList().CreateTarget (debugger, file_spec, m_options.m_arch, true, target_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000142
143 if (target_sp)
144 {
Jim Inghamc8332952010-08-26 21:32:51 +0000145 debugger.GetTargetList().SetSelectedTarget(target_sp.get());
Greg Clayton940b1032011-02-23 00:35:02 +0000146 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, target_sp->GetArchitecture().GetArchitectureName());
Chris Lattner24943d22010-06-08 16:52:24 +0000147 result.SetStatus (eReturnStatusSuccessFinishNoResult);
148 }
149 else
150 {
151 result.AppendError(error.AsCString());
152 result.SetStatus (eReturnStatusFailed);
153 }
154 }
155 else
156 {
157 result.AppendErrorWithFormat("'%s' takes exactly one executable path argument.\n", m_cmd_name.c_str());
158 result.SetStatus (eReturnStatusFailed);
159 }
160 return result.Succeeded();
161
162}
Jim Ingham802f8b02010-06-30 05:02:46 +0000163
164int
Greg Clayton238c0a12010-09-18 01:14:36 +0000165CommandObjectFile::HandleArgumentCompletion
166(
167 Args &input,
168 int &cursor_index,
169 int &cursor_char_position,
170 OptionElementVector &opt_element_vector,
171 int match_start_point,
172 int max_return_elements,
173 bool &word_complete,
174 StringList &matches
175)
Jim Ingham802f8b02010-06-30 05:02:46 +0000176{
Greg Clayton238c0a12010-09-18 01:14:36 +0000177 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
178 completion_str.erase (cursor_char_position);
Jim Ingham802f8b02010-06-30 05:02:46 +0000179
Greg Clayton238c0a12010-09-18 01:14:36 +0000180 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
181 CommandCompletions::eDiskFileCompletion,
182 completion_str.c_str(),
183 match_start_point,
184 max_return_elements,
185 NULL,
186 word_complete,
187 matches);
188 return matches.GetSize();
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000189}