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 | |
| 28 | CommandObjectFile::CommandOptions::CommandOptions() : |
| 29 | Options (), |
| 30 | m_arch () // Breakpoint info defaults to brief descriptions |
| 31 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | CommandObjectFile::CommandOptions::~CommandOptions () |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | lldb::OptionDefinition |
| 39 | CommandObjectFile::CommandOptions::g_option_table[] = |
| 40 | { |
Caroline Tice | 4d6675c | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 41 | { 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | const lldb::OptionDefinition * |
| 46 | CommandObjectFile::CommandOptions::GetDefinitions () |
| 47 | { |
| 48 | return g_option_table; |
| 49 | } |
| 50 | |
| 51 | Error |
| 52 | CommandObjectFile::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 Ingham | 7a4c8ea | 2011-03-22 01:53:33 +0000 | [diff] [blame] | 65 | error.SetErrorStringWithFormat ("Invalid arch string '%s'.\n", option_arg); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 66 | } |
| 67 | break; |
| 68 | |
| 69 | default: |
| 70 | error.SetErrorStringWithFormat ("Unrecognized option '%c'.\n", short_option); |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | return error; |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | CommandObjectFile::CommandOptions::ResetOptionValues () |
| 79 | { |
| 80 | Options::ResetOptionValues(); |
| 81 | m_arch.Clear(); |
| 82 | } |
| 83 | |
| 84 | //------------------------------------------------------------------------- |
| 85 | // CommandObjectFile |
| 86 | //------------------------------------------------------------------------- |
| 87 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 88 | CommandObjectFile::CommandObjectFile(CommandInterpreter &interpreter) : |
| 89 | CommandObject (interpreter, |
| 90 | "file", |
Caroline Tice | c1ad82e | 2010-09-07 22:38:08 +0000 | [diff] [blame] | 91 | "Set the file to be used as the main executable by the debugger.", |
Caroline Tice | 43b014a | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 92 | NULL) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 93 | { |
Caroline Tice | 43b014a | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 94 | CommandArgumentEntry arg; |
| 95 | CommandArgumentData file_arg; |
| 96 | |
| 97 | // Define the first (and only) variant of this arg. |
| 98 | file_arg.arg_type = eArgTypeFilename; |
| 99 | file_arg.arg_repetition = eArgRepeatPlain; |
| 100 | |
| 101 | // There is only one variant this argument could be; put it into the argument entry. |
| 102 | arg.push_back (file_arg); |
| 103 | |
| 104 | // Push the data for the first argument into the m_arguments vector. |
| 105 | m_arguments.push_back (arg); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | CommandObjectFile::~CommandObjectFile () |
| 109 | { |
| 110 | } |
| 111 | |
| 112 | Options * |
| 113 | CommandObjectFile::GetOptions () |
| 114 | { |
| 115 | return &m_options; |
| 116 | } |
| 117 | |
| 118 | bool |
| 119 | CommandObjectFile::Execute |
| 120 | ( |
| 121 | Args& command, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 122 | CommandReturnObject &result |
| 123 | ) |
| 124 | { |
| 125 | const char *file_path = command.GetArgumentAtIndex(0); |
Caroline Tice | 31fbb64 | 2010-09-08 22:08:58 +0000 | [diff] [blame] | 126 | Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) file '%s'", file_path); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 127 | const int argc = command.GetArgumentCount(); |
| 128 | if (argc == 1) |
| 129 | { |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 130 | FileSpec file_spec (file_path, true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 131 | |
Caroline Tice | eddffe9 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 132 | if (! file_spec.Exists() && !file_spec.ResolveExecutableLocation()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 133 | { |
| 134 | result.AppendErrorWithFormat ("File '%s' does not exist.\n", file_path); |
| 135 | result.SetStatus (eReturnStatusFailed); |
| 136 | return result.Succeeded(); |
| 137 | } |
| 138 | |
| 139 | TargetSP target_sp; |
| 140 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 141 | Debugger &debugger = m_interpreter.GetDebugger(); |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 142 | Error error = debugger.GetTargetList().CreateTarget (debugger, file_spec, m_options.m_arch, true, target_sp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 143 | |
| 144 | if (target_sp) |
| 145 | { |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 146 | debugger.GetTargetList().SetSelectedTarget(target_sp.get()); |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 147 | 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] | 148 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | result.AppendError(error.AsCString()); |
| 153 | result.SetStatus (eReturnStatusFailed); |
| 154 | } |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | result.AppendErrorWithFormat("'%s' takes exactly one executable path argument.\n", m_cmd_name.c_str()); |
| 159 | result.SetStatus (eReturnStatusFailed); |
| 160 | } |
| 161 | return result.Succeeded(); |
| 162 | |
| 163 | } |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 164 | |
| 165 | int |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 166 | CommandObjectFile::HandleArgumentCompletion |
| 167 | ( |
| 168 | Args &input, |
| 169 | int &cursor_index, |
| 170 | int &cursor_char_position, |
| 171 | OptionElementVector &opt_element_vector, |
| 172 | int match_start_point, |
| 173 | int max_return_elements, |
| 174 | bool &word_complete, |
| 175 | StringList &matches |
| 176 | ) |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 177 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 178 | std::string completion_str (input.GetArgumentAtIndex(cursor_index)); |
| 179 | completion_str.erase (cursor_char_position); |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 180 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 181 | CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter, |
| 182 | CommandCompletions::eDiskFileCompletion, |
| 183 | completion_str.c_str(), |
| 184 | match_start_point, |
| 185 | max_return_elements, |
| 186 | NULL, |
| 187 | word_complete, |
| 188 | matches); |
| 189 | return matches.GetSize(); |
Caroline Tice | c1ad82e | 2010-09-07 22:38:08 +0000 | [diff] [blame] | 190 | } |