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() : |
| 29 | m_arch (), |
| 30 | m_arch_str () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame^] | 34 | FileOptionGroup::~FileOptionGroup () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | { |
| 36 | } |
| 37 | |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame^] | 38 | OptionDefinition g_file_option_table[] = |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | { |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame^] | 40 | { 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] | 41 | }; |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame^] | 42 | const uint32_t k_num_file_options = sizeof(g_file_option_table)/sizeof(OptionDefinition); |
| 43 | |
| 44 | uint32_t |
| 45 | FileOptionGroup::GetNumDefinitions () |
| 46 | { |
| 47 | return k_num_file_options; |
| 48 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 50 | const OptionDefinition * |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame^] | 51 | FileOptionGroup::GetDefinitions () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | { |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame^] | 53 | return g_file_option_table; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | Error |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame^] | 57 | FileOptionGroup::SetOptionValue (CommandInterpreter &interpreter, |
| 58 | uint32_t option_idx, |
| 59 | const char *option_arg) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 60 | { |
| 61 | Error error; |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame^] | 62 | char short_option = (char) g_file_option_table[option_idx].short_option; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 63 | |
| 64 | switch (short_option) |
| 65 | { |
| 66 | case 'a': |
| 67 | { |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame^] | 68 | // Save the arch value in case we specify a platform after specifying the arch |
| 69 | m_arch_str.assign (option_arg); |
| 70 | // Check to see if we already have a platform? |
| 71 | m_arch_platform_sp = interpreter.GetPlatform (false); |
| 72 | ArchSpec option_arch (option_arg, m_arch_platform_sp.get()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | if (option_arch.IsValid()) |
| 74 | m_arch = option_arch; |
| 75 | else |
Jim Ingham | 7a4c8ea | 2011-03-22 01:53:33 +0000 | [diff] [blame] | 76 | error.SetErrorStringWithFormat ("Invalid arch string '%s'.\n", option_arg); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | } |
| 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 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | m_arch.Clear(); |
| 92 | } |
| 93 | |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame^] | 94 | Error |
| 95 | FileOptionGroup::OptionParsingFinished (CommandInterpreter &interpreter) |
| 96 | { |
| 97 | Error error; |
| 98 | if (m_arch.IsValid()) |
| 99 | { |
| 100 | PlatformSP curr_platform_sp (interpreter.GetPlatform (false)); |
| 101 | if (curr_platform_sp.get() != m_arch_platform_sp.get()) |
| 102 | { |
| 103 | ArchSpec option_arch (m_arch_str.c_str(), curr_platform_sp.get()); |
| 104 | if (option_arch.IsValid()) |
| 105 | m_arch = option_arch; |
| 106 | else |
| 107 | error.SetErrorStringWithFormat ("invalid arch '%s' for platform '%s'", m_arch_str.c_str(), curr_platform_sp->GetName()); |
| 108 | } |
| 109 | } |
| 110 | return error; |
| 111 | } |
| 112 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 113 | //------------------------------------------------------------------------- |
| 114 | // CommandObjectFile |
| 115 | //------------------------------------------------------------------------- |
| 116 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 117 | CommandObjectFile::CommandObjectFile(CommandInterpreter &interpreter) : |
| 118 | CommandObject (interpreter, |
| 119 | "file", |
Caroline Tice | c1ad82e | 2010-09-07 22:38:08 +0000 | [diff] [blame] | 120 | "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] | 121 | NULL), |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame^] | 122 | m_option_group (interpreter), |
| 123 | m_file_options (), |
| 124 | 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] | 125 | { |
Caroline Tice | 43b014a | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 126 | CommandArgumentEntry arg; |
| 127 | CommandArgumentData file_arg; |
| 128 | |
| 129 | // Define the first (and only) variant of this arg. |
| 130 | file_arg.arg_type = eArgTypeFilename; |
| 131 | file_arg.arg_repetition = eArgRepeatPlain; |
| 132 | |
| 133 | // There is only one variant this argument could be; put it into the argument entry. |
| 134 | arg.push_back (file_arg); |
| 135 | |
| 136 | // Push the data for the first argument into the m_arguments vector. |
| 137 | m_arguments.push_back (arg); |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame^] | 138 | |
| 139 | m_option_group.Append (&m_file_options); |
| 140 | m_option_group.Append (&m_platform_options); |
| 141 | m_option_group.Finalize(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | CommandObjectFile::~CommandObjectFile () |
| 145 | { |
| 146 | } |
| 147 | |
| 148 | Options * |
| 149 | CommandObjectFile::GetOptions () |
| 150 | { |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame^] | 151 | return &m_option_group; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | bool |
| 155 | CommandObjectFile::Execute |
| 156 | ( |
| 157 | Args& command, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 158 | CommandReturnObject &result |
| 159 | ) |
| 160 | { |
| 161 | const char *file_path = command.GetArgumentAtIndex(0); |
Caroline Tice | 31fbb64 | 2010-09-08 22:08:58 +0000 | [diff] [blame] | 162 | Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) file '%s'", file_path); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 163 | const int argc = command.GetArgumentCount(); |
| 164 | if (argc == 1) |
| 165 | { |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 166 | FileSpec file_spec (file_path, true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 167 | |
Caroline Tice | eddffe9 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 168 | if (! file_spec.Exists() && !file_spec.ResolveExecutableLocation()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 169 | { |
| 170 | result.AppendErrorWithFormat ("File '%s' does not exist.\n", file_path); |
| 171 | result.SetStatus (eReturnStatusFailed); |
| 172 | return result.Succeeded(); |
| 173 | } |
| 174 | |
| 175 | TargetSP target_sp; |
| 176 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 177 | Debugger &debugger = m_interpreter.GetDebugger(); |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame^] | 178 | Error error = debugger.GetTargetList().CreateTarget (debugger, file_spec, m_file_options.m_arch, true, target_sp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 179 | |
| 180 | if (target_sp) |
| 181 | { |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 182 | debugger.GetTargetList().SetSelectedTarget(target_sp.get()); |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 183 | 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] | 184 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | result.AppendError(error.AsCString()); |
| 189 | result.SetStatus (eReturnStatusFailed); |
| 190 | } |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | result.AppendErrorWithFormat("'%s' takes exactly one executable path argument.\n", m_cmd_name.c_str()); |
| 195 | result.SetStatus (eReturnStatusFailed); |
| 196 | } |
| 197 | return result.Succeeded(); |
| 198 | |
| 199 | } |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 200 | |
| 201 | int |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 202 | CommandObjectFile::HandleArgumentCompletion |
| 203 | ( |
| 204 | Args &input, |
| 205 | int &cursor_index, |
| 206 | int &cursor_char_position, |
| 207 | OptionElementVector &opt_element_vector, |
| 208 | int match_start_point, |
| 209 | int max_return_elements, |
| 210 | bool &word_complete, |
| 211 | StringList &matches |
| 212 | ) |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 213 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 214 | std::string completion_str (input.GetArgumentAtIndex(cursor_index)); |
| 215 | completion_str.erase (cursor_char_position); |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 216 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 217 | CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter, |
| 218 | CommandCompletions::eDiskFileCompletion, |
| 219 | completion_str.c_str(), |
| 220 | match_start_point, |
| 221 | max_return_elements, |
| 222 | NULL, |
| 223 | word_complete, |
| 224 | matches); |
| 225 | return matches.GetSize(); |
Caroline Tice | c1ad82e | 2010-09-07 22:38:08 +0000 | [diff] [blame] | 226 | } |