blob: c4ccf2a2167b8468bd8fc7a43b0e5bc7a45b94b5 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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 Ingham84cdc152010-06-15 19:49:27 +000016#include "lldb/Interpreter/Args.h"
Greg Clayton63094e02010-06-23 01:19:29 +000017#include "lldb/Core/Debugger.h"
Greg Clayton52c8b6e2011-04-19 04:19:37 +000018#include "lldb/Core/FileLineResolver.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000019#include "lldb/Core/ModuleSpec.h"
Greg Clayton52c8b6e2011-04-19 04:19:37 +000020#include "lldb/Core/SourceManager.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Interpreter/CommandInterpreter.h"
22#include "lldb/Interpreter/CommandReturnObject.h"
Greg Clayton5f54ac32011-02-08 05:05:52 +000023#include "lldb/Host/FileSpec.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000024#include "lldb/Symbol/CompileUnit.h"
25#include "lldb/Symbol/Function.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Target/Process.h"
27#include "lldb/Target/TargetList.h"
Jim Ingham767af882010-07-07 03:36:20 +000028#include "lldb/Interpreter/CommandCompletions.h"
29#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030
31using namespace lldb;
32using namespace lldb_private;
33
Chris Lattner24943d22010-06-08 16:52:24 +000034//-------------------------------------------------------------------------
Greg Clayton52c8b6e2011-04-19 04:19:37 +000035// CommandObjectSourceInfo
Chris Lattner24943d22010-06-08 16:52:24 +000036//-------------------------------------------------------------------------
37
Jim Inghamda26bd22012-06-08 21:56:10 +000038class CommandObjectSourceInfo : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +000039{
Chris Lattner24943d22010-06-08 16:52:24 +000040
Jim Ingham767af882010-07-07 03:36:20 +000041 class CommandOptions : public Options
Chris Lattner24943d22010-06-08 16:52:24 +000042 {
Jim Ingham767af882010-07-07 03:36:20 +000043 public:
Greg Claytonf15996e2011-04-07 22:46:35 +000044 CommandOptions (CommandInterpreter &interpreter) :
45 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000046 {
Jim Ingham767af882010-07-07 03:36:20 +000047 }
Chris Lattner24943d22010-06-08 16:52:24 +000048
Jim Ingham767af882010-07-07 03:36:20 +000049 ~CommandOptions ()
50 {
51 }
Chris Lattner24943d22010-06-08 16:52:24 +000052
Jim Ingham767af882010-07-07 03:36:20 +000053 Error
Greg Clayton143fcc32011-04-13 00:18:08 +000054 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Ingham767af882010-07-07 03:36:20 +000055 {
56 Error error;
57 const char short_option = g_option_table[option_idx].short_option;
58 switch (short_option)
Chris Lattner24943d22010-06-08 16:52:24 +000059 {
Jim Ingham767af882010-07-07 03:36:20 +000060 case 'l':
61 start_line = Args::StringToUInt32 (option_arg, 0);
62 if (start_line == 0)
Greg Clayton9c236732011-10-26 00:56:27 +000063 error.SetErrorStringWithFormat("invalid line number: '%s'", option_arg);
Jim Ingham767af882010-07-07 03:36:20 +000064 break;
Chris Lattner24943d22010-06-08 16:52:24 +000065
Jim Ingham767af882010-07-07 03:36:20 +000066 case 'f':
67 file_name = option_arg;
68 break;
69
70 default:
Greg Clayton9c236732011-10-26 00:56:27 +000071 error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
Jim Ingham767af882010-07-07 03:36:20 +000072 break;
Chris Lattner24943d22010-06-08 16:52:24 +000073 }
74
Jim Ingham767af882010-07-07 03:36:20 +000075 return error;
76 }
Chris Lattner24943d22010-06-08 16:52:24 +000077
Jim Ingham767af882010-07-07 03:36:20 +000078 void
Greg Clayton143fcc32011-04-13 00:18:08 +000079 OptionParsingStarting ()
Jim Ingham767af882010-07-07 03:36:20 +000080 {
Jim Ingham767af882010-07-07 03:36:20 +000081 file_spec.Clear();
82 file_name.clear();
83 start_line = 0;
84 }
85
Greg Claytonb3448432011-03-24 21:19:54 +000086 const OptionDefinition*
Jim Ingham767af882010-07-07 03:36:20 +000087 GetDefinitions ()
88 {
89 return g_option_table;
90 }
Greg Claytonb3448432011-03-24 21:19:54 +000091 static OptionDefinition g_option_table[];
Jim Ingham767af882010-07-07 03:36:20 +000092
93 // Instance variables to hold the values for command options.
94 FileSpec file_spec;
95 std::string file_name;
96 uint32_t start_line;
97
98 };
99
100public:
Greg Clayton238c0a12010-09-18 01:14:36 +0000101 CommandObjectSourceInfo(CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000102 CommandObjectParsed (interpreter,
103 "source info",
104 "Display information about the source lines from the current executable's debug info.",
105 "source info [<cmd-options>]"),
Greg Claytonf15996e2011-04-07 22:46:35 +0000106 m_options (interpreter)
Jim Ingham767af882010-07-07 03:36:20 +0000107 {
108 }
109
110 ~CommandObjectSourceInfo ()
111 {
112 }
113
114
115 Options *
116 GetOptions ()
117 {
118 return &m_options;
119 }
120
Jim Inghamda26bd22012-06-08 21:56:10 +0000121protected:
Jim Ingham767af882010-07-07 03:36:20 +0000122 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000123 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham767af882010-07-07 03:36:20 +0000124 {
125 result.AppendError ("Not yet implemented");
126 result.SetStatus (eReturnStatusFailed);
127 return false;
128 }
Jim Inghamda26bd22012-06-08 21:56:10 +0000129
Jim Ingham767af882010-07-07 03:36:20 +0000130 CommandOptions m_options;
131};
132
Greg Claytonb3448432011-03-24 21:19:54 +0000133OptionDefinition
Jim Ingham767af882010-07-07 03:36:20 +0000134CommandObjectSourceInfo::CommandOptions::g_option_table[] =
135{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000136{ LLDB_OPT_SET_1, false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "The line number at which to start the display source."},
137{ LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "The file from which to display source."},
138{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Jim Ingham767af882010-07-07 03:36:20 +0000139};
140
141#pragma mark CommandObjectSourceList
142//-------------------------------------------------------------------------
143// CommandObjectSourceList
144//-------------------------------------------------------------------------
145
Jim Inghamda26bd22012-06-08 21:56:10 +0000146class CommandObjectSourceList : public CommandObjectParsed
Jim Ingham767af882010-07-07 03:36:20 +0000147{
148
149 class CommandOptions : public Options
150 {
151 public:
Greg Claytonf15996e2011-04-07 22:46:35 +0000152 CommandOptions (CommandInterpreter &interpreter) :
153 Options(interpreter)
Jim Ingham767af882010-07-07 03:36:20 +0000154 {
155 }
156
157 ~CommandOptions ()
158 {
159 }
160
161 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000162 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Ingham767af882010-07-07 03:36:20 +0000163 {
164 Error error;
165 const char short_option = g_option_table[option_idx].short_option;
166 switch (short_option)
167 {
168 case 'l':
169 start_line = Args::StringToUInt32 (option_arg, 0);
170 if (start_line == 0)
Greg Clayton9c236732011-10-26 00:56:27 +0000171 error.SetErrorStringWithFormat("invalid line number: '%s'", option_arg);
Jim Ingham767af882010-07-07 03:36:20 +0000172 break;
173
Jim Ingham338f7532010-08-20 01:17:07 +0000174 case 'c':
Jim Ingham767af882010-07-07 03:36:20 +0000175 num_lines = Args::StringToUInt32 (option_arg, 0);
176 if (num_lines == 0)
Greg Clayton9c236732011-10-26 00:56:27 +0000177 error.SetErrorStringWithFormat("invalid line count: '%s'", option_arg);
Jim Ingham767af882010-07-07 03:36:20 +0000178 break;
179
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000180 case 'f':
Jim Ingham767af882010-07-07 03:36:20 +0000181 file_name = option_arg;
182 break;
Jim Ingham338f7532010-08-20 01:17:07 +0000183
184 case 'n':
185 symbol_name = option_arg;
186 break;
Jim Ingham767af882010-07-07 03:36:20 +0000187
Jim Ingham338f7532010-08-20 01:17:07 +0000188 case 's':
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000189 modules.push_back (std::string (option_arg));
190 break;
191
192 case 'b':
193 show_bp_locs = true;
Jim Ingham338f7532010-08-20 01:17:07 +0000194 break;
Jim Ingham767af882010-07-07 03:36:20 +0000195 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000196 error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
Jim Ingham767af882010-07-07 03:36:20 +0000197 break;
198 }
199
200 return error;
201 }
202
203 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000204 OptionParsingStarting ()
Jim Ingham767af882010-07-07 03:36:20 +0000205 {
Jim Ingham767af882010-07-07 03:36:20 +0000206 file_spec.Clear();
207 file_name.clear();
Jim Ingham338f7532010-08-20 01:17:07 +0000208 symbol_name.clear();
Jim Ingham767af882010-07-07 03:36:20 +0000209 start_line = 0;
210 num_lines = 10;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000211 show_bp_locs = false;
212 modules.clear();
Jim Ingham767af882010-07-07 03:36:20 +0000213 }
214
Greg Claytonb3448432011-03-24 21:19:54 +0000215 const OptionDefinition*
Jim Ingham767af882010-07-07 03:36:20 +0000216 GetDefinitions ()
217 {
218 return g_option_table;
219 }
Greg Claytonb3448432011-03-24 21:19:54 +0000220 static OptionDefinition g_option_table[];
Jim Ingham767af882010-07-07 03:36:20 +0000221
222 // Instance variables to hold the values for command options.
223 FileSpec file_spec;
224 std::string file_name;
Jim Ingham338f7532010-08-20 01:17:07 +0000225 std::string symbol_name;
Jim Ingham767af882010-07-07 03:36:20 +0000226 uint32_t start_line;
227 uint32_t num_lines;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000228 STLStringArray modules;
229 bool show_bp_locs;
Jim Ingham767af882010-07-07 03:36:20 +0000230 };
231
232public:
Greg Clayton238c0a12010-09-18 01:14:36 +0000233 CommandObjectSourceList(CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000234 CommandObjectParsed (interpreter,
235 "source list",
236 "Display source code (as specified) based on the current executable's debug info.",
237 NULL),
Greg Claytonf15996e2011-04-07 22:46:35 +0000238 m_options (interpreter)
Jim Ingham767af882010-07-07 03:36:20 +0000239 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000240 CommandArgumentEntry arg;
241 CommandArgumentData file_arg;
242
243 // Define the first (and only) variant of this arg.
244 file_arg.arg_type = eArgTypeFilename;
245 file_arg.arg_repetition = eArgRepeatOptional;
246
247 // There is only one variant this argument could be; put it into the argument entry.
248 arg.push_back (file_arg);
249
250 // Push the data for the first argument into the m_arguments vector.
251 m_arguments.push_back (arg);
Jim Ingham767af882010-07-07 03:36:20 +0000252 }
253
254 ~CommandObjectSourceList ()
255 {
256 }
257
258
259 Options *
260 GetOptions ()
261 {
262 return &m_options;
263 }
264
Jim Inghamda26bd22012-06-08 21:56:10 +0000265 virtual const char *
266 GetRepeatCommand (Args &current_command_args, uint32_t index)
Jim Ingham767af882010-07-07 03:36:20 +0000267 {
Jim Inghamda26bd22012-06-08 21:56:10 +0000268 return m_cmd_name.c_str();
269 }
270
271protected:
272 bool
273 DoExecute (Args& command, CommandReturnObject &result)
274 {
275 const int argc = command.GetArgumentCount();
Jim Ingham767af882010-07-07 03:36:20 +0000276
277 if (argc != 0)
278 {
279 result.AppendErrorWithFormat("'%s' takes no arguments, only flags.\n", GetCommandName());
280 result.SetStatus (eReturnStatusFailed);
Jim Ingham26e089b2011-11-29 21:21:26 +0000281 return false;
Jim Ingham767af882010-07-07 03:36:20 +0000282 }
283
Greg Claytonb72d0f02011-04-12 05:54:46 +0000284 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +0000285 Target *target = exe_ctx.GetTargetPtr();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000286
Greg Clayton567e7f32011-09-22 04:58:26 +0000287 if (target == NULL)
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000288 target = m_interpreter.GetDebugger().GetSelectedTarget().get();
289
290 if (target == NULL)
291 {
292 result.AppendError ("invalid target, create a debug target using the 'target create' command");
293 result.SetStatus (eReturnStatusFailed);
294 return false;
295 }
296
Jim Ingham338f7532010-08-20 01:17:07 +0000297 if (!m_options.symbol_name.empty())
298 {
299 // Displaying the source for a symbol:
Jim Ingham338f7532010-08-20 01:17:07 +0000300 SymbolContextList sc_list;
301 ConstString name(m_options.symbol_name.c_str());
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000302 bool include_symbols = false;
Sean Callanan302d78c2012-02-10 22:52:19 +0000303 bool include_inlines = true;
Jim Ingham338f7532010-08-20 01:17:07 +0000304 bool append = true;
305 size_t num_matches = 0;
306
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000307 if (m_options.modules.size() > 0)
Jim Ingham338f7532010-08-20 01:17:07 +0000308 {
309 ModuleList matching_modules;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000310 for (unsigned i = 0, e = m_options.modules.size(); i != e; i++)
Jim Ingham338f7532010-08-20 01:17:07 +0000311 {
Greg Clayton444fe992012-02-26 05:51:37 +0000312 FileSpec module_file_spec(m_options.modules[i].c_str(), false);
313 if (module_file_spec)
Jim Ingham338f7532010-08-20 01:17:07 +0000314 {
Greg Clayton444fe992012-02-26 05:51:37 +0000315 ModuleSpec module_spec (module_file_spec);
Jim Ingham338f7532010-08-20 01:17:07 +0000316 matching_modules.Clear();
Greg Clayton444fe992012-02-26 05:51:37 +0000317 target->GetImages().FindModules (module_spec, matching_modules);
Sean Callanan302d78c2012-02-10 22:52:19 +0000318 num_matches += matching_modules.FindFunctions (name, eFunctionNameTypeAuto, include_symbols, include_inlines, append, sc_list);
Jim Ingham338f7532010-08-20 01:17:07 +0000319 }
320 }
321 }
322 else
323 {
Sean Callanan302d78c2012-02-10 22:52:19 +0000324 num_matches = target->GetImages().FindFunctions (name, eFunctionNameTypeAuto, include_symbols, include_inlines, append, sc_list);
Jim Ingham338f7532010-08-20 01:17:07 +0000325 }
326
327 SymbolContext sc;
328
329 if (num_matches == 0)
330 {
331 result.AppendErrorWithFormat("Could not find function named: \"%s\".\n", m_options.symbol_name.c_str());
332 result.SetStatus (eReturnStatusFailed);
333 return false;
334 }
335
336 sc_list.GetContextAtIndex (0, sc);
337 FileSpec start_file;
338 uint32_t start_line;
339 uint32_t end_line;
340 FileSpec end_file;
341 if (sc.function != NULL)
342 {
343 sc.function->GetStartLineSourceInfo (start_file, start_line);
344 if (start_line == 0)
345 {
346 result.AppendErrorWithFormat("Could not find line information for start of function: \"%s\".\n", m_options.symbol_name.c_str());
347 result.SetStatus (eReturnStatusFailed);
348 return false;
349 }
350 sc.function->GetEndLineSourceInfo (end_file, end_line);
351 }
352 else
353 {
354 result.AppendErrorWithFormat("Could not find function info for: \"%s\".\n", m_options.symbol_name.c_str());
355 result.SetStatus (eReturnStatusFailed);
356 return false;
357 }
358
359 if (num_matches > 1)
360 {
361 // This could either be because there are multiple functions of this name, in which case
362 // we'll have to specify this further... Or it could be because there are multiple inlined instances
363 // of one function. So run through the matches and if they all have the same file & line then we can just
364 // list one.
365
366 bool found_multiple = false;
367
368 for (size_t i = 1; i < num_matches; i++)
369 {
370 SymbolContext scratch_sc;
371 sc_list.GetContextAtIndex (i, scratch_sc);
372 if (scratch_sc.function != NULL)
373 {
374 FileSpec scratch_file;
375 uint32_t scratch_line;
376 scratch_sc.function->GetStartLineSourceInfo (scratch_file, scratch_line);
377 if (scratch_file != start_file
378 || scratch_line != start_line)
379 {
380 found_multiple = true;
381 break;
382 }
383 }
384 }
385 if (found_multiple)
386 {
387 StreamString s;
388 for (size_t i = 0; i < num_matches; i++)
389 {
390 SymbolContext scratch_sc;
391 sc_list.GetContextAtIndex (i, scratch_sc);
392 if (scratch_sc.function != NULL)
393 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000394 s.Printf("\n%lu: ", i);
Jim Ingham338f7532010-08-20 01:17:07 +0000395 scratch_sc.function->Dump (&s, true);
396 }
397 }
398 result.AppendErrorWithFormat("Multiple functions found matching: %s: \n%s\n",
399 m_options.symbol_name.c_str(),
400 s.GetData());
401 result.SetStatus (eReturnStatusFailed);
402 return false;
403 }
404 }
405
406
407 // This is a little hacky, but the first line table entry for a function points to the "{" that
408 // starts the function block. It would be nice to actually get the function
409 // declaration in there too. So back up a bit, but not further than what you're going to display.
410 size_t lines_to_back_up = m_options.num_lines >= 10 ? 5 : m_options.num_lines/2;
411 uint32_t line_no;
412 if (start_line <= lines_to_back_up)
413 line_no = 1;
414 else
415 line_no = start_line - lines_to_back_up;
416
417 // For fun, if the function is shorter than the number of lines we're supposed to display,
418 // only display the function...
419 if (end_line != 0)
420 {
421 if (m_options.num_lines > end_line - line_no)
422 m_options.num_lines = end_line - line_no;
423 }
424
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000425 char path_buf[PATH_MAX];
426 start_file.GetPath(path_buf, sizeof(path_buf));
Greg Clayton1cee1e62011-04-20 18:52:45 +0000427
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000428 if (m_options.show_bp_locs)
Greg Clayton1cee1e62011-04-20 18:52:45 +0000429 {
430 const bool show_inlines = true;
431 m_breakpoint_locations.Reset (start_file, 0, show_inlines);
Greg Clayton567e7f32011-09-22 04:58:26 +0000432 SearchFilter target_search_filter (exe_ctx.GetTargetSP());
Greg Clayton1cee1e62011-04-20 18:52:45 +0000433 target_search_filter.Search (m_breakpoint_locations);
434 }
435 else
436 m_breakpoint_locations.Clear();
437
Jim Ingham338f7532010-08-20 01:17:07 +0000438 result.AppendMessageWithFormat("File: %s.\n", path_buf);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000439 target->GetSourceManager().DisplaySourceLinesWithLineNumbers (start_file,
440 line_no,
441 0,
442 m_options.num_lines,
443 "",
444 &result.GetOutputStream(),
445 GetBreakpointLocations ());
Jim Ingham338f7532010-08-20 01:17:07 +0000446
447 result.SetStatus (eReturnStatusSuccessFinishResult);
448 return true;
449
450 }
451 else if (m_options.file_name.empty())
Jim Ingham767af882010-07-07 03:36:20 +0000452 {
453 // Last valid source manager context, or the current frame if no
454 // valid last context in source manager.
455 // One little trick here, if you type the exact same list command twice in a row, it is
456 // more likely because you typed it once, then typed it again
457 if (m_options.start_line == 0)
458 {
Jim Inghamc7f18c82011-09-29 20:22:33 +0000459 if (target->GetSourceManager().DisplayMoreWithLineNumbers (&result.GetOutputStream(),
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000460 GetBreakpointLocations ()))
Chris Lattner24943d22010-06-08 16:52:24 +0000461 {
Chris Lattner24943d22010-06-08 16:52:24 +0000462 result.SetStatus (eReturnStatusSuccessFinishResult);
463 }
Jim Ingham767af882010-07-07 03:36:20 +0000464 }
465 else
466 {
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000467 if (m_options.show_bp_locs)
Greg Clayton1cee1e62011-04-20 18:52:45 +0000468 {
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000469 SourceManager::FileSP last_file_sp (target->GetSourceManager().GetLastFile ());
Greg Clayton1cee1e62011-04-20 18:52:45 +0000470 if (last_file_sp)
471 {
472 const bool show_inlines = true;
473 m_breakpoint_locations.Reset (last_file_sp->GetFileSpec(), 0, show_inlines);
Greg Clayton13d24fb2012-01-29 20:56:30 +0000474 SearchFilter target_search_filter (target->shared_from_this());
Greg Clayton1cee1e62011-04-20 18:52:45 +0000475 target_search_filter.Search (m_breakpoint_locations);
476 }
477 }
478 else
479 m_breakpoint_locations.Clear();
480
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000481 if (target->GetSourceManager().DisplaySourceLinesWithLineNumbersUsingLastFile(
Jim Ingham767af882010-07-07 03:36:20 +0000482 m_options.start_line, // Line to display
483 0, // Lines before line to display
484 m_options.num_lines, // Lines after line to display
485 "", // Don't mark "line"
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000486 &result.GetOutputStream(),
487 GetBreakpointLocations ()))
Chris Lattner24943d22010-06-08 16:52:24 +0000488 {
Jim Ingham767af882010-07-07 03:36:20 +0000489 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000490 }
Jim Ingham767af882010-07-07 03:36:20 +0000491
Chris Lattner24943d22010-06-08 16:52:24 +0000492 }
493 }
494 else
495 {
Jim Ingham767af882010-07-07 03:36:20 +0000496 const char *filename = m_options.file_name.c_str();
Jim Ingham767af882010-07-07 03:36:20 +0000497
498 bool check_inlines = false;
499 SymbolContextList sc_list;
Jim Ingham338f7532010-08-20 01:17:07 +0000500 size_t num_matches = 0;
501
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000502 if (m_options.modules.size() > 0)
Jim Ingham767af882010-07-07 03:36:20 +0000503 {
Jim Ingham338f7532010-08-20 01:17:07 +0000504 ModuleList matching_modules;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000505 for (unsigned i = 0, e = m_options.modules.size(); i != e; i++)
Jim Ingham767af882010-07-07 03:36:20 +0000506 {
Greg Clayton444fe992012-02-26 05:51:37 +0000507 FileSpec module_file_spec(m_options.modules[i].c_str(), false);
508 if (module_file_spec)
Jim Ingham767af882010-07-07 03:36:20 +0000509 {
Greg Clayton444fe992012-02-26 05:51:37 +0000510 ModuleSpec module_spec (module_file_spec);
Jim Ingham338f7532010-08-20 01:17:07 +0000511 matching_modules.Clear();
Greg Clayton444fe992012-02-26 05:51:37 +0000512 target->GetImages().FindModules (module_spec, matching_modules);
Jim Ingham338f7532010-08-20 01:17:07 +0000513 num_matches += matching_modules.ResolveSymbolContextForFilePath (filename,
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000514 0,
515 check_inlines,
516 eSymbolContextModule | eSymbolContextCompUnit,
517 sc_list);
Jim Ingham767af882010-07-07 03:36:20 +0000518 }
519 }
520 }
Jim Ingham338f7532010-08-20 01:17:07 +0000521 else
522 {
523 num_matches = target->GetImages().ResolveSymbolContextForFilePath (filename,
524 0,
525 check_inlines,
526 eSymbolContextModule | eSymbolContextCompUnit,
527 sc_list);
528 }
529
530 if (num_matches == 0)
531 {
532 result.AppendErrorWithFormat("Could not find source file \"%s\".\n",
533 m_options.file_name.c_str());
534 result.SetStatus (eReturnStatusFailed);
535 return false;
536 }
537
538 if (num_matches > 1)
539 {
540 SymbolContext sc;
541 bool got_multiple = false;
542 FileSpec *test_cu_spec = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000543
Chris Lattner0f6fa732010-09-08 22:55:31 +0000544 for (unsigned i = 0; i < num_matches; i++)
Jim Ingham338f7532010-08-20 01:17:07 +0000545 {
546 sc_list.GetContextAtIndex(i, sc);
547 if (sc.comp_unit)
548 {
549 if (test_cu_spec)
550 {
551 if (test_cu_spec != static_cast<FileSpec *> (sc.comp_unit))
552 got_multiple = true;
553 break;
554 }
555 else
556 test_cu_spec = sc.comp_unit;
557 }
558 }
559 if (got_multiple)
560 {
561 result.AppendErrorWithFormat("Multiple source files found matching: \"%s.\"\n",
562 m_options.file_name.c_str());
563 result.SetStatus (eReturnStatusFailed);
564 return false;
565 }
566 }
567
568 SymbolContext sc;
569 if (sc_list.GetContextAtIndex(0, sc))
570 {
571 if (sc.comp_unit)
572 {
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000573 if (m_options.show_bp_locs)
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000574 {
575 const bool show_inlines = true;
576 m_breakpoint_locations.Reset (*sc.comp_unit, 0, show_inlines);
Greg Clayton13d24fb2012-01-29 20:56:30 +0000577 SearchFilter target_search_filter (target->shared_from_this());
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000578 target_search_filter.Search (m_breakpoint_locations);
579 }
580 else
581 m_breakpoint_locations.Clear();
582
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000583 target->GetSourceManager().DisplaySourceLinesWithLineNumbers (sc.comp_unit,
584 m_options.start_line,
585 0,
586 m_options.num_lines,
587 "",
588 &result.GetOutputStream(),
589 GetBreakpointLocations ());
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000590
Jim Ingham338f7532010-08-20 01:17:07 +0000591 result.SetStatus (eReturnStatusSuccessFinishResult);
592 }
593 else
594 {
595 result.AppendErrorWithFormat("No comp unit found for: \"%s.\"\n",
596 m_options.file_name.c_str());
597 result.SetStatus (eReturnStatusFailed);
598 return false;
599 }
600 }
601 }
Jim Ingham767af882010-07-07 03:36:20 +0000602 return result.Succeeded();
Chris Lattner24943d22010-06-08 16:52:24 +0000603 }
Jim Ingham767af882010-07-07 03:36:20 +0000604
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000605 const SymbolContextList *
606 GetBreakpointLocations ()
607 {
608 if (m_breakpoint_locations.GetFileLineMatches().GetSize() > 0)
609 return &m_breakpoint_locations.GetFileLineMatches();
610 return NULL;
611 }
Jim Ingham767af882010-07-07 03:36:20 +0000612 CommandOptions m_options;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000613 FileLineResolver m_breakpoint_locations;
Jim Ingham767af882010-07-07 03:36:20 +0000614
615};
616
Greg Claytonb3448432011-03-24 21:19:54 +0000617OptionDefinition
Jim Ingham767af882010-07-07 03:36:20 +0000618CommandObjectSourceList::CommandOptions::g_option_table[] =
619{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000620{ LLDB_OPT_SET_ALL, false, "count", 'c', required_argument, NULL, 0, eArgTypeCount, "The number of source lines to display."},
621{ LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Look up the source file in the given shared library."},
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000622{ 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 Tice4d6675c2010-10-01 19:59:14 +0000623{ LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "The file from which to display source."},
624{ LLDB_OPT_SET_1, false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "The line number at which to start the display source."},
625{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeSymbol, "The name of a function whose source to display."},
626{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Jim Ingham767af882010-07-07 03:36:20 +0000627};
628
629#pragma mark CommandObjectMultiwordSource
630
631//-------------------------------------------------------------------------
632// CommandObjectMultiwordSource
633//-------------------------------------------------------------------------
634
635CommandObjectMultiwordSource::CommandObjectMultiwordSource (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +0000636 CommandObjectMultiword (interpreter,
637 "source",
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000638 "A set of commands for accessing source file information",
Jim Ingham767af882010-07-07 03:36:20 +0000639 "source <subcommand> [<subcommand-options>]")
640{
Greg Clayton238c0a12010-09-18 01:14:36 +0000641 LoadSubCommand ("info", CommandObjectSP (new CommandObjectSourceInfo (interpreter)));
642 LoadSubCommand ("list", CommandObjectSP (new CommandObjectSourceList (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +0000643}
Jim Ingham767af882010-07-07 03:36:20 +0000644
645CommandObjectMultiwordSource::~CommandObjectMultiwordSource ()
646{
647}
648