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 | { |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 41 | { LLDB_OPT_SET_1, false, "arch", 'a', required_argument, NULL, 0, "<arch>", "Specify the architecture to launch."}, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | { 0, false, NULL, 0, 0, NULL, 0, NULL, NULL } |
| 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 |
| 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 | |
| 77 | void |
| 78 | CommandObjectFile::CommandOptions::ResetOptionValues () |
| 79 | { |
| 80 | Options::ResetOptionValues(); |
| 81 | m_arch.Clear(); |
| 82 | } |
| 83 | |
| 84 | //------------------------------------------------------------------------- |
| 85 | // CommandObjectFile |
| 86 | //------------------------------------------------------------------------- |
| 87 | |
| 88 | CommandObjectFile::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 | |
| 95 | CommandObjectFile::~CommandObjectFile () |
| 96 | { |
| 97 | } |
| 98 | |
| 99 | Options * |
| 100 | CommandObjectFile::GetOptions () |
| 101 | { |
| 102 | return &m_options; |
| 103 | } |
| 104 | |
| 105 | bool |
| 106 | CommandObjectFile::Execute |
| 107 | ( |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 108 | CommandInterpreter &interpreter, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | Args& command, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 110 | CommandReturnObject &result |
| 111 | ) |
| 112 | { |
| 113 | const char *file_path = command.GetArgumentAtIndex(0); |
| 114 | Timer scoped_timer(__PRETTY_FUNCTION__, "(dbg) file '%s'", file_path); |
| 115 | const int argc = command.GetArgumentCount(); |
| 116 | if (argc == 1) |
| 117 | { |
| 118 | FileSpec file_spec (file_path); |
| 119 | |
| 120 | if (! file_spec.Exists()) |
| 121 | { |
| 122 | result.AppendErrorWithFormat ("File '%s' does not exist.\n", file_path); |
| 123 | result.SetStatus (eReturnStatusFailed); |
| 124 | return result.Succeeded(); |
| 125 | } |
| 126 | |
| 127 | TargetSP target_sp; |
| 128 | |
| 129 | ArchSpec arch; |
| 130 | if (m_options.m_arch.IsValid()) |
| 131 | arch = m_options.m_arch; |
| 132 | else |
| 133 | { |
| 134 | arch = lldb_private::GetDefaultArchitecture (); |
| 135 | if (!arch.IsValid()) |
| 136 | arch = LLDB_ARCH_DEFAULT; |
| 137 | } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 138 | Debugger &debugger = interpreter.GetDebugger(); |
| 139 | Error error = debugger.GetTargetList().CreateTarget (debugger, file_spec, arch, NULL, true, target_sp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 140 | |
| 141 | if (error.Fail() && !m_options.m_arch.IsValid()) |
| 142 | { |
| 143 | if (arch == LLDB_ARCH_DEFAULT_32BIT) |
| 144 | arch = LLDB_ARCH_DEFAULT_64BIT; |
| 145 | else |
| 146 | arch = LLDB_ARCH_DEFAULT_32BIT; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 147 | error = debugger.GetTargetList().CreateTarget (debugger, file_spec, arch, NULL, true, target_sp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | if (target_sp) |
| 151 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 152 | debugger.GetTargetList().SetCurrentTarget(target_sp.get()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 153 | result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, arch.AsCString()); |
| 154 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | result.AppendError(error.AsCString()); |
| 159 | result.SetStatus (eReturnStatusFailed); |
| 160 | } |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | result.AppendErrorWithFormat("'%s' takes exactly one executable path argument.\n", m_cmd_name.c_str()); |
| 165 | result.SetStatus (eReturnStatusFailed); |
| 166 | } |
| 167 | return result.Succeeded(); |
| 168 | |
| 169 | } |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 170 | |
| 171 | int |
| 172 | CommandObjectFile::HandleArgumentCompletion (CommandInterpreter &interpreter, |
| 173 | Args &input, |
| 174 | int &cursor_index, |
| 175 | int &cursor_char_position, |
| 176 | OptionElementVector &opt_element_vector, |
| 177 | int match_start_point, |
| 178 | int max_return_elements, |
| 179 | bool &word_complete, |
| 180 | StringList &matches) |
| 181 | { |
| 182 | std::string completion_str (input.GetArgumentAtIndex(cursor_index)); |
| 183 | completion_str.erase (cursor_char_position); |
| 184 | |
| 185 | CommandCompletions::InvokeCommonCompletionCallbacks (interpreter, |
| 186 | CommandCompletions::eDiskFileCompletion, |
| 187 | completion_str.c_str(), |
| 188 | match_start_point, |
| 189 | max_return_elements, |
| 190 | NULL, |
| 191 | word_complete, |
| 192 | matches); |
| 193 | return matches.GetSize(); |
| 194 | |
| 195 | } |