Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandObjectSource.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 "CommandObjectSource.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" |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Debugger.h" |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 18 | #include "lldb/Core/FileLineResolver.h" |
| 19 | #include "lldb/Core/SourceManager.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" |
Greg Clayton | 5f54ac3 | 2011-02-08 05:05:52 +0000 | [diff] [blame] | 22 | #include "lldb/Host/FileSpec.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #include "lldb/Target/Process.h" |
| 24 | #include "lldb/Target/TargetList.h" |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 25 | #include "lldb/Interpreter/CommandCompletions.h" |
| 26 | #include "lldb/Interpreter/Options.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace lldb; |
| 29 | using namespace lldb_private; |
| 30 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | //------------------------------------------------------------------------- |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 32 | // CommandObjectSourceInfo |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | //------------------------------------------------------------------------- |
| 34 | |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 35 | class CommandObjectSourceInfo : public CommandObject |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 38 | class CommandOptions : public Options |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | { |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 40 | public: |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 41 | CommandOptions (CommandInterpreter &interpreter) : |
| 42 | Options(interpreter) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | { |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 44 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 45 | |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 46 | ~CommandOptions () |
| 47 | { |
| 48 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 50 | Error |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 51 | SetOptionValue (uint32_t option_idx, const char *option_arg) |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 52 | { |
| 53 | Error error; |
| 54 | const char short_option = g_option_table[option_idx].short_option; |
| 55 | switch (short_option) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | { |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 57 | case 'l': |
| 58 | start_line = Args::StringToUInt32 (option_arg, 0); |
| 59 | if (start_line == 0) |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame^] | 60 | error.SetErrorStringWithFormat("invalid line number: '%s'", option_arg); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 61 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 62 | |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 63 | case 'f': |
| 64 | file_name = option_arg; |
| 65 | break; |
| 66 | |
| 67 | default: |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame^] | 68 | error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 69 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 72 | return error; |
| 73 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 74 | |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 75 | void |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 76 | OptionParsingStarting () |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 77 | { |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 78 | file_spec.Clear(); |
| 79 | file_name.clear(); |
| 80 | start_line = 0; |
| 81 | } |
| 82 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 83 | const OptionDefinition* |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 84 | GetDefinitions () |
| 85 | { |
| 86 | return g_option_table; |
| 87 | } |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 88 | static OptionDefinition g_option_table[]; |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 89 | |
| 90 | // Instance variables to hold the values for command options. |
| 91 | FileSpec file_spec; |
| 92 | std::string file_name; |
| 93 | uint32_t start_line; |
| 94 | |
| 95 | }; |
| 96 | |
| 97 | public: |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 98 | CommandObjectSourceInfo(CommandInterpreter &interpreter) : |
| 99 | CommandObject (interpreter, |
| 100 | "source info", |
| 101 | "Display information about the source lines from the current executable's debug info.", |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 102 | "source info [<cmd-options>]"), |
| 103 | m_options (interpreter) |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 104 | { |
| 105 | } |
| 106 | |
| 107 | ~CommandObjectSourceInfo () |
| 108 | { |
| 109 | } |
| 110 | |
| 111 | |
| 112 | Options * |
| 113 | GetOptions () |
| 114 | { |
| 115 | return &m_options; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | bool |
| 120 | Execute |
| 121 | ( |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 122 | Args& args, |
| 123 | CommandReturnObject &result |
| 124 | ) |
| 125 | { |
| 126 | result.AppendError ("Not yet implemented"); |
| 127 | result.SetStatus (eReturnStatusFailed); |
| 128 | return false; |
| 129 | } |
| 130 | protected: |
| 131 | CommandOptions m_options; |
| 132 | }; |
| 133 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 134 | OptionDefinition |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 135 | CommandObjectSourceInfo::CommandOptions::g_option_table[] = |
| 136 | { |
Caroline Tice | 4d6675c | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 137 | { LLDB_OPT_SET_1, false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "The line number at which to start the display source."}, |
| 138 | { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "The file from which to display source."}, |
| 139 | { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | #pragma mark CommandObjectSourceList |
| 143 | //------------------------------------------------------------------------- |
| 144 | // CommandObjectSourceList |
| 145 | //------------------------------------------------------------------------- |
| 146 | |
| 147 | class CommandObjectSourceList : public CommandObject |
| 148 | { |
| 149 | |
| 150 | class CommandOptions : public Options |
| 151 | { |
| 152 | public: |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 153 | CommandOptions (CommandInterpreter &interpreter) : |
| 154 | Options(interpreter) |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 155 | { |
| 156 | } |
| 157 | |
| 158 | ~CommandOptions () |
| 159 | { |
| 160 | } |
| 161 | |
| 162 | Error |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 163 | SetOptionValue (uint32_t option_idx, const char *option_arg) |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 164 | { |
| 165 | Error error; |
| 166 | const char short_option = g_option_table[option_idx].short_option; |
| 167 | switch (short_option) |
| 168 | { |
| 169 | case 'l': |
| 170 | start_line = Args::StringToUInt32 (option_arg, 0); |
| 171 | if (start_line == 0) |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame^] | 172 | error.SetErrorStringWithFormat("invalid line number: '%s'", option_arg); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 173 | break; |
| 174 | |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 175 | case 'c': |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 176 | num_lines = Args::StringToUInt32 (option_arg, 0); |
| 177 | if (num_lines == 0) |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame^] | 178 | error.SetErrorStringWithFormat("invalid line count: '%s'", option_arg); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 179 | break; |
| 180 | |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 181 | case 'f': |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 182 | file_name = option_arg; |
| 183 | break; |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 184 | |
| 185 | case 'n': |
| 186 | symbol_name = option_arg; |
| 187 | break; |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 188 | |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 189 | case 's': |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 190 | modules.push_back (std::string (option_arg)); |
| 191 | break; |
| 192 | |
| 193 | case 'b': |
| 194 | show_bp_locs = true; |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 195 | break; |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 196 | default: |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame^] | 197 | error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 198 | break; |
| 199 | } |
| 200 | |
| 201 | return error; |
| 202 | } |
| 203 | |
| 204 | void |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 205 | OptionParsingStarting () |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 206 | { |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 207 | file_spec.Clear(); |
| 208 | file_name.clear(); |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 209 | symbol_name.clear(); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 210 | start_line = 0; |
| 211 | num_lines = 10; |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 212 | show_bp_locs = false; |
| 213 | modules.clear(); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 216 | const OptionDefinition* |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 217 | GetDefinitions () |
| 218 | { |
| 219 | return g_option_table; |
| 220 | } |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 221 | static OptionDefinition g_option_table[]; |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 222 | |
| 223 | // Instance variables to hold the values for command options. |
| 224 | FileSpec file_spec; |
| 225 | std::string file_name; |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 226 | std::string symbol_name; |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 227 | uint32_t start_line; |
| 228 | uint32_t num_lines; |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 229 | STLStringArray modules; |
| 230 | bool show_bp_locs; |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | public: |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 234 | CommandObjectSourceList(CommandInterpreter &interpreter) : |
| 235 | CommandObject (interpreter, |
| 236 | "source list", |
| 237 | "Display source code (as specified) based on the current executable's debug info.", |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 238 | NULL), |
| 239 | m_options (interpreter) |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 240 | { |
Caroline Tice | 43b014a | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 241 | CommandArgumentEntry arg; |
| 242 | CommandArgumentData file_arg; |
| 243 | |
| 244 | // Define the first (and only) variant of this arg. |
| 245 | file_arg.arg_type = eArgTypeFilename; |
| 246 | file_arg.arg_repetition = eArgRepeatOptional; |
| 247 | |
| 248 | // There is only one variant this argument could be; put it into the argument entry. |
| 249 | arg.push_back (file_arg); |
| 250 | |
| 251 | // Push the data for the first argument into the m_arguments vector. |
| 252 | m_arguments.push_back (arg); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | ~CommandObjectSourceList () |
| 256 | { |
| 257 | } |
| 258 | |
| 259 | |
| 260 | Options * |
| 261 | GetOptions () |
| 262 | { |
| 263 | return &m_options; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | bool |
| 268 | Execute |
| 269 | ( |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 270 | Args& args, |
| 271 | CommandReturnObject &result |
| 272 | ) |
| 273 | { |
| 274 | const int argc = args.GetArgumentCount(); |
| 275 | |
| 276 | if (argc != 0) |
| 277 | { |
| 278 | result.AppendErrorWithFormat("'%s' takes no arguments, only flags.\n", GetCommandName()); |
| 279 | result.SetStatus (eReturnStatusFailed); |
| 280 | } |
| 281 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 282 | ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 283 | Target *target = exe_ctx.GetTargetPtr(); |
Jim Ingham | fdf24ef | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 284 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 285 | if (target == NULL) |
Jim Ingham | fdf24ef | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 286 | target = m_interpreter.GetDebugger().GetSelectedTarget().get(); |
| 287 | |
| 288 | if (target == NULL) |
| 289 | { |
| 290 | result.AppendError ("invalid target, create a debug target using the 'target create' command"); |
| 291 | result.SetStatus (eReturnStatusFailed); |
| 292 | return false; |
| 293 | } |
| 294 | |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 295 | if (!m_options.symbol_name.empty()) |
| 296 | { |
| 297 | // Displaying the source for a symbol: |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 298 | SymbolContextList sc_list; |
| 299 | ConstString name(m_options.symbol_name.c_str()); |
Greg Clayton | 28d5fcc | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 300 | bool include_symbols = false; |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 301 | bool append = true; |
| 302 | size_t num_matches = 0; |
| 303 | |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 304 | if (m_options.modules.size() > 0) |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 305 | { |
| 306 | ModuleList matching_modules; |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 307 | for (unsigned i = 0, e = m_options.modules.size(); i != e; i++) |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 308 | { |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 309 | FileSpec module_spec(m_options.modules[i].c_str(), false); |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 310 | if (module_spec) |
| 311 | { |
| 312 | matching_modules.Clear(); |
| 313 | target->GetImages().FindModules (&module_spec, NULL, NULL, NULL, matching_modules); |
Greg Clayton | 28d5fcc | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 314 | num_matches += matching_modules.FindFunctions (name, eFunctionNameTypeBase, include_symbols, append, sc_list); |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | } |
| 318 | else |
| 319 | { |
Greg Clayton | 28d5fcc | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 320 | num_matches = target->GetImages().FindFunctions (name, eFunctionNameTypeBase, include_symbols, append, sc_list); |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | SymbolContext sc; |
| 324 | |
| 325 | if (num_matches == 0) |
| 326 | { |
| 327 | result.AppendErrorWithFormat("Could not find function named: \"%s\".\n", m_options.symbol_name.c_str()); |
| 328 | result.SetStatus (eReturnStatusFailed); |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | sc_list.GetContextAtIndex (0, sc); |
| 333 | FileSpec start_file; |
| 334 | uint32_t start_line; |
| 335 | uint32_t end_line; |
| 336 | FileSpec end_file; |
| 337 | if (sc.function != NULL) |
| 338 | { |
| 339 | sc.function->GetStartLineSourceInfo (start_file, start_line); |
| 340 | if (start_line == 0) |
| 341 | { |
| 342 | result.AppendErrorWithFormat("Could not find line information for start of function: \"%s\".\n", m_options.symbol_name.c_str()); |
| 343 | result.SetStatus (eReturnStatusFailed); |
| 344 | return false; |
| 345 | } |
| 346 | sc.function->GetEndLineSourceInfo (end_file, end_line); |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | result.AppendErrorWithFormat("Could not find function info for: \"%s\".\n", m_options.symbol_name.c_str()); |
| 351 | result.SetStatus (eReturnStatusFailed); |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | if (num_matches > 1) |
| 356 | { |
| 357 | // This could either be because there are multiple functions of this name, in which case |
| 358 | // we'll have to specify this further... Or it could be because there are multiple inlined instances |
| 359 | // of one function. So run through the matches and if they all have the same file & line then we can just |
| 360 | // list one. |
| 361 | |
| 362 | bool found_multiple = false; |
| 363 | |
| 364 | for (size_t i = 1; i < num_matches; i++) |
| 365 | { |
| 366 | SymbolContext scratch_sc; |
| 367 | sc_list.GetContextAtIndex (i, scratch_sc); |
| 368 | if (scratch_sc.function != NULL) |
| 369 | { |
| 370 | FileSpec scratch_file; |
| 371 | uint32_t scratch_line; |
| 372 | scratch_sc.function->GetStartLineSourceInfo (scratch_file, scratch_line); |
| 373 | if (scratch_file != start_file |
| 374 | || scratch_line != start_line) |
| 375 | { |
| 376 | found_multiple = true; |
| 377 | break; |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | if (found_multiple) |
| 382 | { |
| 383 | StreamString s; |
| 384 | for (size_t i = 0; i < num_matches; i++) |
| 385 | { |
| 386 | SymbolContext scratch_sc; |
| 387 | sc_list.GetContextAtIndex (i, scratch_sc); |
| 388 | if (scratch_sc.function != NULL) |
| 389 | { |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 390 | s.Printf("\n%lu: ", i); |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 391 | scratch_sc.function->Dump (&s, true); |
| 392 | } |
| 393 | } |
| 394 | result.AppendErrorWithFormat("Multiple functions found matching: %s: \n%s\n", |
| 395 | m_options.symbol_name.c_str(), |
| 396 | s.GetData()); |
| 397 | result.SetStatus (eReturnStatusFailed); |
| 398 | return false; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | |
| 403 | // This is a little hacky, but the first line table entry for a function points to the "{" that |
| 404 | // starts the function block. It would be nice to actually get the function |
| 405 | // declaration in there too. So back up a bit, but not further than what you're going to display. |
| 406 | size_t lines_to_back_up = m_options.num_lines >= 10 ? 5 : m_options.num_lines/2; |
| 407 | uint32_t line_no; |
| 408 | if (start_line <= lines_to_back_up) |
| 409 | line_no = 1; |
| 410 | else |
| 411 | line_no = start_line - lines_to_back_up; |
| 412 | |
| 413 | // For fun, if the function is shorter than the number of lines we're supposed to display, |
| 414 | // only display the function... |
| 415 | if (end_line != 0) |
| 416 | { |
| 417 | if (m_options.num_lines > end_line - line_no) |
| 418 | m_options.num_lines = end_line - line_no; |
| 419 | } |
| 420 | |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 421 | char path_buf[PATH_MAX]; |
| 422 | start_file.GetPath(path_buf, sizeof(path_buf)); |
Greg Clayton | 1cee1e6 | 2011-04-20 18:52:45 +0000 | [diff] [blame] | 423 | |
Jim Ingham | fdf24ef | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 424 | if (m_options.show_bp_locs) |
Greg Clayton | 1cee1e6 | 2011-04-20 18:52:45 +0000 | [diff] [blame] | 425 | { |
| 426 | const bool show_inlines = true; |
| 427 | m_breakpoint_locations.Reset (start_file, 0, show_inlines); |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 428 | SearchFilter target_search_filter (exe_ctx.GetTargetSP()); |
Greg Clayton | 1cee1e6 | 2011-04-20 18:52:45 +0000 | [diff] [blame] | 429 | target_search_filter.Search (m_breakpoint_locations); |
| 430 | } |
| 431 | else |
| 432 | m_breakpoint_locations.Clear(); |
| 433 | |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 434 | result.AppendMessageWithFormat("File: %s.\n", path_buf); |
Jim Ingham | fdf24ef | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 435 | target->GetSourceManager().DisplaySourceLinesWithLineNumbers (start_file, |
| 436 | line_no, |
| 437 | 0, |
| 438 | m_options.num_lines, |
| 439 | "", |
| 440 | &result.GetOutputStream(), |
| 441 | GetBreakpointLocations ()); |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 442 | |
| 443 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 444 | return true; |
| 445 | |
| 446 | } |
| 447 | else if (m_options.file_name.empty()) |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 448 | { |
| 449 | // Last valid source manager context, or the current frame if no |
| 450 | // valid last context in source manager. |
| 451 | // One little trick here, if you type the exact same list command twice in a row, it is |
| 452 | // more likely because you typed it once, then typed it again |
| 453 | if (m_options.start_line == 0) |
| 454 | { |
Jim Ingham | c7f18c8 | 2011-09-29 20:22:33 +0000 | [diff] [blame] | 455 | if (target->GetSourceManager().DisplayMoreWithLineNumbers (&result.GetOutputStream(), |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 456 | GetBreakpointLocations ())) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 457 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 458 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 459 | } |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 460 | } |
| 461 | else |
| 462 | { |
Jim Ingham | fdf24ef | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 463 | if (m_options.show_bp_locs) |
Greg Clayton | 1cee1e6 | 2011-04-20 18:52:45 +0000 | [diff] [blame] | 464 | { |
Jim Ingham | fdf24ef | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 465 | SourceManager::FileSP last_file_sp (target->GetSourceManager().GetLastFile ()); |
Greg Clayton | 1cee1e6 | 2011-04-20 18:52:45 +0000 | [diff] [blame] | 466 | if (last_file_sp) |
| 467 | { |
| 468 | const bool show_inlines = true; |
| 469 | m_breakpoint_locations.Reset (last_file_sp->GetFileSpec(), 0, show_inlines); |
Jim Ingham | fdf24ef | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 470 | SearchFilter target_search_filter (target->GetSP()); |
Greg Clayton | 1cee1e6 | 2011-04-20 18:52:45 +0000 | [diff] [blame] | 471 | target_search_filter.Search (m_breakpoint_locations); |
| 472 | } |
| 473 | } |
| 474 | else |
| 475 | m_breakpoint_locations.Clear(); |
| 476 | |
Jim Ingham | fdf24ef | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 477 | if (target->GetSourceManager().DisplaySourceLinesWithLineNumbersUsingLastFile( |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 478 | m_options.start_line, // Line to display |
| 479 | 0, // Lines before line to display |
| 480 | m_options.num_lines, // Lines after line to display |
| 481 | "", // Don't mark "line" |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 482 | &result.GetOutputStream(), |
| 483 | GetBreakpointLocations ())) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 484 | { |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 485 | result.SetStatus (eReturnStatusSuccessFinishResult); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 486 | } |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 487 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | else |
| 491 | { |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 492 | const char *filename = m_options.file_name.c_str(); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 493 | |
| 494 | bool check_inlines = false; |
| 495 | SymbolContextList sc_list; |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 496 | size_t num_matches = 0; |
| 497 | |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 498 | if (m_options.modules.size() > 0) |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 499 | { |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 500 | ModuleList matching_modules; |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 501 | for (unsigned i = 0, e = m_options.modules.size(); i != e; i++) |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 502 | { |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 503 | FileSpec module_spec(m_options.modules[i].c_str(), false); |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 504 | if (module_spec) |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 505 | { |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 506 | matching_modules.Clear(); |
| 507 | target->GetImages().FindModules (&module_spec, NULL, NULL, NULL, matching_modules); |
| 508 | num_matches += matching_modules.ResolveSymbolContextForFilePath (filename, |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 509 | 0, |
| 510 | check_inlines, |
| 511 | eSymbolContextModule | eSymbolContextCompUnit, |
| 512 | sc_list); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | } |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 516 | else |
| 517 | { |
| 518 | num_matches = target->GetImages().ResolveSymbolContextForFilePath (filename, |
| 519 | 0, |
| 520 | check_inlines, |
| 521 | eSymbolContextModule | eSymbolContextCompUnit, |
| 522 | sc_list); |
| 523 | } |
| 524 | |
| 525 | if (num_matches == 0) |
| 526 | { |
| 527 | result.AppendErrorWithFormat("Could not find source file \"%s\".\n", |
| 528 | m_options.file_name.c_str()); |
| 529 | result.SetStatus (eReturnStatusFailed); |
| 530 | return false; |
| 531 | } |
| 532 | |
| 533 | if (num_matches > 1) |
| 534 | { |
| 535 | SymbolContext sc; |
| 536 | bool got_multiple = false; |
| 537 | FileSpec *test_cu_spec = NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 538 | |
Chris Lattner | 0f6fa73 | 2010-09-08 22:55:31 +0000 | [diff] [blame] | 539 | for (unsigned i = 0; i < num_matches; i++) |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 540 | { |
| 541 | sc_list.GetContextAtIndex(i, sc); |
| 542 | if (sc.comp_unit) |
| 543 | { |
| 544 | if (test_cu_spec) |
| 545 | { |
| 546 | if (test_cu_spec != static_cast<FileSpec *> (sc.comp_unit)) |
| 547 | got_multiple = true; |
| 548 | break; |
| 549 | } |
| 550 | else |
| 551 | test_cu_spec = sc.comp_unit; |
| 552 | } |
| 553 | } |
| 554 | if (got_multiple) |
| 555 | { |
| 556 | result.AppendErrorWithFormat("Multiple source files found matching: \"%s.\"\n", |
| 557 | m_options.file_name.c_str()); |
| 558 | result.SetStatus (eReturnStatusFailed); |
| 559 | return false; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | SymbolContext sc; |
| 564 | if (sc_list.GetContextAtIndex(0, sc)) |
| 565 | { |
| 566 | if (sc.comp_unit) |
| 567 | { |
Jim Ingham | fdf24ef | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 568 | if (m_options.show_bp_locs) |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 569 | { |
| 570 | const bool show_inlines = true; |
| 571 | m_breakpoint_locations.Reset (*sc.comp_unit, 0, show_inlines); |
Jim Ingham | fdf24ef | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 572 | SearchFilter target_search_filter (target->GetSP()); |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 573 | target_search_filter.Search (m_breakpoint_locations); |
| 574 | } |
| 575 | else |
| 576 | m_breakpoint_locations.Clear(); |
| 577 | |
Jim Ingham | fdf24ef | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 578 | target->GetSourceManager().DisplaySourceLinesWithLineNumbers (sc.comp_unit, |
| 579 | m_options.start_line, |
| 580 | 0, |
| 581 | m_options.num_lines, |
| 582 | "", |
| 583 | &result.GetOutputStream(), |
| 584 | GetBreakpointLocations ()); |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 585 | |
Jim Ingham | 338f753 | 2010-08-20 01:17:07 +0000 | [diff] [blame] | 586 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 587 | } |
| 588 | else |
| 589 | { |
| 590 | result.AppendErrorWithFormat("No comp unit found for: \"%s.\"\n", |
| 591 | m_options.file_name.c_str()); |
| 592 | result.SetStatus (eReturnStatusFailed); |
| 593 | return false; |
| 594 | } |
| 595 | } |
| 596 | } |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 597 | return result.Succeeded(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 598 | } |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 599 | |
| 600 | virtual const char *GetRepeatCommand (Args ¤t_command_args, uint32_t index) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 601 | { |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 602 | return m_cmd_name.c_str(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 603 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 604 | |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 605 | protected: |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 606 | const SymbolContextList * |
| 607 | GetBreakpointLocations () |
| 608 | { |
| 609 | if (m_breakpoint_locations.GetFileLineMatches().GetSize() > 0) |
| 610 | return &m_breakpoint_locations.GetFileLineMatches(); |
| 611 | return NULL; |
| 612 | } |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 613 | CommandOptions m_options; |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 614 | FileLineResolver m_breakpoint_locations; |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 615 | |
| 616 | }; |
| 617 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 618 | OptionDefinition |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 619 | CommandObjectSourceList::CommandOptions::g_option_table[] = |
| 620 | { |
Caroline Tice | 4d6675c | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 621 | { LLDB_OPT_SET_ALL, false, "count", 'c', required_argument, NULL, 0, eArgTypeCount, "The number of source lines to display."}, |
| 622 | { LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Look up the source file in the given shared library."}, |
Greg Clayton | 52c8b6e | 2011-04-19 04:19:37 +0000 | [diff] [blame] | 623 | { LLDB_OPT_SET_ALL, false, "show-breakpoints", 'b', no_argument, NULL, 0, eArgTypeNone, "Show the line table locations from the debug information that indicate valid places to set source level breakpoints."}, |
Caroline Tice | 4d6675c | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 624 | { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "The file from which to display source."}, |
| 625 | { LLDB_OPT_SET_1, false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "The line number at which to start the display source."}, |
| 626 | { LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeSymbol, "The name of a function whose source to display."}, |
| 627 | { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 628 | }; |
| 629 | |
| 630 | #pragma mark CommandObjectMultiwordSource |
| 631 | |
| 632 | //------------------------------------------------------------------------- |
| 633 | // CommandObjectMultiwordSource |
| 634 | //------------------------------------------------------------------------- |
| 635 | |
| 636 | CommandObjectMultiwordSource::CommandObjectMultiwordSource (CommandInterpreter &interpreter) : |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 637 | CommandObjectMultiword (interpreter, |
| 638 | "source", |
Caroline Tice | c1ad82e | 2010-09-07 22:38:08 +0000 | [diff] [blame] | 639 | "A set of commands for accessing source file information", |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 640 | "source <subcommand> [<subcommand-options>]") |
| 641 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 642 | LoadSubCommand ("info", CommandObjectSP (new CommandObjectSourceInfo (interpreter))); |
| 643 | LoadSubCommand ("list", CommandObjectSP (new CommandObjectSourceList (interpreter))); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 644 | } |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 645 | |
| 646 | CommandObjectMultiwordSource::~CommandObjectMultiwordSource () |
| 647 | { |
| 648 | } |
| 649 | |