blob: bdaf67cc81495ea547d4be766f9cee018eb63a20 [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"
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{
Chris Lattner24943d22010-06-08 16:52:24 +000031}
32
33CommandObjectFile::CommandOptions::~CommandOptions ()
34{
35}
36
37lldb::OptionDefinition
38CommandObjectFile::CommandOptions::g_option_table[] =
39{
Jim Ingham34e9a982010-06-15 18:47:14 +000040 { LLDB_OPT_SET_1, false, "arch", 'a', required_argument, NULL, 0, "<arch>", "Specify the architecture to launch."},
Chris Lattner24943d22010-06-08 16:52:24 +000041 { 0, false, NULL, 0, 0, NULL, 0, NULL, NULL }
42};
43
44const lldb::OptionDefinition *
45CommandObjectFile::CommandOptions::GetDefinitions ()
46{
47 return g_option_table;
48}
49
50Error
51CommandObjectFile::CommandOptions::SetOptionValue (int option_idx, const char *option_arg)
52{
53 Error error;
54 char short_option = (char) m_getopt_table[option_idx].val;
55
56 switch (short_option)
57 {
58 case 'a':
59 {
60 ArchSpec option_arch (option_arg);
61 if (option_arch.IsValid())
62 m_arch = option_arch;
63 else
64 error.SetErrorStringWithFormat ("Invalid arch string '%s'.\n", optarg);
65 }
66 break;
67
68 default:
69 error.SetErrorStringWithFormat ("Unrecognized option '%c'.\n", short_option);
70 break;
71 }
72
73 return error;
74}
75
76void
77CommandObjectFile::CommandOptions::ResetOptionValues ()
78{
79 Options::ResetOptionValues();
80 m_arch.Clear();
81}
82
83//-------------------------------------------------------------------------
84// CommandObjectFile
85//-------------------------------------------------------------------------
86
87CommandObjectFile::CommandObjectFile() :
88 CommandObject ("file",
89 "Sets the file to be used as the main executable by the debugger.",
90 "file [<cmd-options>] <filename>")
91{
92}
93
94CommandObjectFile::~CommandObjectFile ()
95{
96}
97
98Options *
99CommandObjectFile::GetOptions ()
100{
101 return &m_options;
102}
103
104bool
105CommandObjectFile::Execute
106(
Greg Clayton63094e02010-06-23 01:19:29 +0000107 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000108 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000109 CommandReturnObject &result
110)
111{
112 const char *file_path = command.GetArgumentAtIndex(0);
113 Timer scoped_timer(__PRETTY_FUNCTION__, "(dbg) file '%s'", file_path);
114 const int argc = command.GetArgumentCount();
115 if (argc == 1)
116 {
117 FileSpec file_spec (file_path);
118
119 if (! file_spec.Exists())
120 {
121 result.AppendErrorWithFormat ("File '%s' does not exist.\n", file_path);
122 result.SetStatus (eReturnStatusFailed);
123 return result.Succeeded();
124 }
125
126 TargetSP target_sp;
127
128 ArchSpec arch;
129 if (m_options.m_arch.IsValid())
130 arch = m_options.m_arch;
131 else
132 {
133 arch = lldb_private::GetDefaultArchitecture ();
134 if (!arch.IsValid())
135 arch = LLDB_ARCH_DEFAULT;
136 }
Greg Clayton63094e02010-06-23 01:19:29 +0000137 Debugger &debugger = interpreter.GetDebugger();
138 Error error = debugger.GetTargetList().CreateTarget (debugger, file_spec, arch, NULL, true, target_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000139
140 if (error.Fail() && !m_options.m_arch.IsValid())
141 {
142 if (arch == LLDB_ARCH_DEFAULT_32BIT)
143 arch = LLDB_ARCH_DEFAULT_64BIT;
144 else
145 arch = LLDB_ARCH_DEFAULT_32BIT;
Greg Clayton63094e02010-06-23 01:19:29 +0000146 error = debugger.GetTargetList().CreateTarget (debugger, file_spec, arch, NULL, true, target_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000147 }
148
149 if (target_sp)
150 {
Greg Clayton63094e02010-06-23 01:19:29 +0000151 debugger.GetTargetList().SetCurrentTarget(target_sp.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000152 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, arch.AsCString());
153 result.SetStatus (eReturnStatusSuccessFinishNoResult);
154 }
155 else
156 {
157 result.AppendError(error.AsCString());
158 result.SetStatus (eReturnStatusFailed);
159 }
160 }
161 else
162 {
163 result.AppendErrorWithFormat("'%s' takes exactly one executable path argument.\n", m_cmd_name.c_str());
164 result.SetStatus (eReturnStatusFailed);
165 }
166 return result.Succeeded();
167
168}