Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 16 | #include "lldb/Interpreter/Args.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Debugger.h" |
| 18 | #include "lldb/Core/Timer.h" |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Debugger.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 21 | #include "lldb/Interpreter/CommandReturnObject.h" |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 22 | #include "lldb/Interpreter/CommandCompletions.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #include "lldb/Target/Process.h" |
| 24 | |
| 25 | using namespace lldb; |
| 26 | using namespace lldb_private; |
| 27 | |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 28 | FileOptionGroup::FileOptionGroup() : |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 29 | m_arch_str () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 33 | FileOptionGroup::~FileOptionGroup () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | { |
| 35 | } |
| 36 | |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 37 | OptionDefinition g_file_option_table[] = |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | { |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 39 | { LLDB_OPT_SET_1 , false, "arch" , 'a', required_argument, NULL, 0, eArgTypeArchitecture , "Specify the architecture for the target."}, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | }; |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 41 | const uint32_t k_num_file_options = sizeof(g_file_option_table)/sizeof(OptionDefinition); |
| 42 | |
| 43 | uint32_t |
| 44 | FileOptionGroup::GetNumDefinitions () |
| 45 | { |
| 46 | return k_num_file_options; |
| 47 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 49 | const OptionDefinition * |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 50 | FileOptionGroup::GetDefinitions () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | { |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 52 | return g_file_option_table; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Greg Clayton | 5e342f5 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 55 | bool |
| 56 | FileOptionGroup::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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 66 | Error |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 67 | FileOptionGroup::SetOptionValue (CommandInterpreter &interpreter, |
| 68 | uint32_t option_idx, |
| 69 | const char *option_arg) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | { |
| 71 | Error error; |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 72 | char short_option = (char) g_file_option_table[option_idx].short_option; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | |
| 74 | switch (short_option) |
| 75 | { |
| 76 | case 'a': |
Greg Clayton | 5e342f5 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 77 | m_arch_str.assign (option_arg); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 78 | break; |
| 79 | |
| 80 | default: |
| 81 | error.SetErrorStringWithFormat ("Unrecognized option '%c'.\n", short_option); |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | return error; |
| 86 | } |
| 87 | |
| 88 | void |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 89 | FileOptionGroup::OptionParsingStarting (CommandInterpreter &interpreter) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 90 | { |
Greg Clayton | 5e342f5 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 91 | m_arch_str.clear(); |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | //------------------------------------------------------------------------- |
| 95 | // CommandObjectFile |
| 96 | //------------------------------------------------------------------------- |
| 97 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 98 | CommandObjectFile::CommandObjectFile(CommandInterpreter &interpreter) : |
| 99 | CommandObject (interpreter, |
| 100 | "file", |
Caroline Tice | c1ad82e | 2010-09-07 22:38:08 +0000 | [diff] [blame] | 101 | "Set the file to be used as the main executable by the debugger.", |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 102 | NULL), |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 103 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | { |
Caroline Tice | 43b014a | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 107 | 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 Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 119 | |
Greg Clayton | 5e342f5 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 120 | 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 Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 122 | m_option_group.Finalize(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | CommandObjectFile::~CommandObjectFile () |
| 126 | { |
| 127 | } |
| 128 | |
| 129 | Options * |
| 130 | CommandObjectFile::GetOptions () |
| 131 | { |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 132 | return &m_option_group; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | bool |
| 136 | CommandObjectFile::Execute |
| 137 | ( |
| 138 | Args& command, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 139 | CommandReturnObject &result |
| 140 | ) |
| 141 | { |
| 142 | const char *file_path = command.GetArgumentAtIndex(0); |
Caroline Tice | 31fbb64 | 2010-09-08 22:08:58 +0000 | [diff] [blame] | 143 | Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) file '%s'", file_path); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 144 | const int argc = command.GetArgumentCount(); |
| 145 | if (argc == 1) |
| 146 | { |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 147 | FileSpec file_spec (file_path, true); |
Greg Clayton | 5e342f5 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 148 | |
| 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 177 | |
Caroline Tice | eddffe9 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 178 | if (! file_spec.Exists() && !file_spec.ResolveExecutableLocation()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 179 | { |
| 180 | result.AppendErrorWithFormat ("File '%s' does not exist.\n", file_path); |
| 181 | result.SetStatus (eReturnStatusFailed); |
Greg Clayton | 5e342f5 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 182 | return false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | TargetSP target_sp; |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 186 | Debugger &debugger = m_interpreter.GetDebugger(); |
Greg Clayton | 5e342f5 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 187 | error = debugger.GetTargetList().CreateTarget (debugger, file_spec, file_arch, true, target_sp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 188 | |
| 189 | if (target_sp) |
| 190 | { |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 191 | debugger.GetTargetList().SetSelectedTarget(target_sp.get()); |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 192 | result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, target_sp->GetArchitecture().GetArchitectureName()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 193 | 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 Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 209 | |
| 210 | int |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 211 | CommandObjectFile::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 Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 222 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 223 | std::string completion_str (input.GetArgumentAtIndex(cursor_index)); |
| 224 | completion_str.erase (cursor_char_position); |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 225 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 226 | 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 Tice | c1ad82e | 2010-09-07 22:38:08 +0000 | [diff] [blame] | 235 | } |