blob: de40c6ee7fd70ea37bcbf254c43e7130ec21021d [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
16#include "lldb/Core/Args.h"
17#include "lldb/Core/Debugger.h"
18#include "lldb/Core/Timer.h"
19#include "lldb/Interpreter/CommandContext.h"
20#include "lldb/Interpreter/CommandInterpreter.h"
21#include "lldb/Interpreter/CommandReturnObject.h"
22#include "lldb/Target/Process.h"
23
24using namespace lldb;
25using namespace lldb_private;
26
27CommandObjectFile::CommandOptions::CommandOptions() :
28 Options (),
29 m_arch () // Breakpoint info defaults to brief descriptions
30{
31 BuildValidOptionSets();
32}
33
34CommandObjectFile::CommandOptions::~CommandOptions ()
35{
36}
37
38lldb::OptionDefinition
39CommandObjectFile::CommandOptions::g_option_table[] =
40{
Jim Ingham34e9a982010-06-15 18:47:14 +000041 { LLDB_OPT_SET_1, false, "arch", 'a', required_argument, NULL, 0, "<arch>", "Specify the architecture to launch."},
Chris Lattner24943d22010-06-08 16:52:24 +000042 { 0, false, NULL, 0, 0, NULL, 0, NULL, NULL }
43};
44
45const lldb::OptionDefinition *
46CommandObjectFile::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
65 error.SetErrorStringWithFormat ("Invalid arch string '%s'.\n", optarg);
66 }
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{
80 Options::ResetOptionValues();
81 m_arch.Clear();
82}
83
84//-------------------------------------------------------------------------
85// CommandObjectFile
86//-------------------------------------------------------------------------
87
88CommandObjectFile::CommandObjectFile() :
89 CommandObject ("file",
90 "Sets the file to be used as the main executable by the debugger.",
91 "file [<cmd-options>] <filename>")
92{
93}
94
95CommandObjectFile::~CommandObjectFile ()
96{
97}
98
99Options *
100CommandObjectFile::GetOptions ()
101{
102 return &m_options;
103}
104
105bool
106CommandObjectFile::Execute
107(
108 Args& command,
109 CommandContext *context,
110 CommandInterpreter *interpreter,
111 CommandReturnObject &result
112)
113{
114 const char *file_path = command.GetArgumentAtIndex(0);
115 Timer scoped_timer(__PRETTY_FUNCTION__, "(dbg) file '%s'", file_path);
116 const int argc = command.GetArgumentCount();
117 if (argc == 1)
118 {
119 FileSpec file_spec (file_path);
120
121 if (! file_spec.Exists())
122 {
123 result.AppendErrorWithFormat ("File '%s' does not exist.\n", file_path);
124 result.SetStatus (eReturnStatusFailed);
125 return result.Succeeded();
126 }
127
128 TargetSP target_sp;
129
130 ArchSpec arch;
131 if (m_options.m_arch.IsValid())
132 arch = m_options.m_arch;
133 else
134 {
135 arch = lldb_private::GetDefaultArchitecture ();
136 if (!arch.IsValid())
137 arch = LLDB_ARCH_DEFAULT;
138 }
139
140 Error error = Debugger::GetSharedInstance().GetTargetList().CreateTarget (file_spec, arch, NULL, true, target_sp);
141
142 if (error.Fail() && !m_options.m_arch.IsValid())
143 {
144 if (arch == LLDB_ARCH_DEFAULT_32BIT)
145 arch = LLDB_ARCH_DEFAULT_64BIT;
146 else
147 arch = LLDB_ARCH_DEFAULT_32BIT;
148 error = Debugger::GetSharedInstance().GetTargetList().CreateTarget (file_spec, arch, NULL, true, target_sp);
149 }
150
151 if (target_sp)
152 {
153 Debugger::GetSharedInstance().GetTargetList().SetCurrentTarget(target_sp.get());
154 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, arch.AsCString());
155 result.SetStatus (eReturnStatusSuccessFinishNoResult);
156 }
157 else
158 {
159 result.AppendError(error.AsCString());
160 result.SetStatus (eReturnStatusFailed);
161 }
162 }
163 else
164 {
165 result.AppendErrorWithFormat("'%s' takes exactly one executable path argument.\n", m_cmd_name.c_str());
166 result.SetStatus (eReturnStatusFailed);
167 }
168 return result.Succeeded();
169
170}