blob: c0c9f94d07d7a136a1ff48d7cf93931aa061d69d [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"
19#include "lldb/Core/SourceManager.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Interpreter/CommandInterpreter.h"
21#include "lldb/Interpreter/CommandReturnObject.h"
Greg Clayton5f54ac32011-02-08 05:05:52 +000022#include "lldb/Host/FileSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Target/Process.h"
24#include "lldb/Target/TargetList.h"
Jim Ingham767af882010-07-07 03:36:20 +000025#include "lldb/Interpreter/CommandCompletions.h"
26#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027
28using namespace lldb;
29using namespace lldb_private;
30
Chris Lattner24943d22010-06-08 16:52:24 +000031//-------------------------------------------------------------------------
Greg Clayton52c8b6e2011-04-19 04:19:37 +000032// CommandObjectSourceInfo
Chris Lattner24943d22010-06-08 16:52:24 +000033//-------------------------------------------------------------------------
34
Jim Ingham767af882010-07-07 03:36:20 +000035class CommandObjectSourceInfo : public CommandObject
Chris Lattner24943d22010-06-08 16:52:24 +000036{
Chris Lattner24943d22010-06-08 16:52:24 +000037
Jim Ingham767af882010-07-07 03:36:20 +000038 class CommandOptions : public Options
Chris Lattner24943d22010-06-08 16:52:24 +000039 {
Jim Ingham767af882010-07-07 03:36:20 +000040 public:
Greg Claytonf15996e2011-04-07 22:46:35 +000041 CommandOptions (CommandInterpreter &interpreter) :
42 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000043 {
Jim Ingham767af882010-07-07 03:36:20 +000044 }
Chris Lattner24943d22010-06-08 16:52:24 +000045
Jim Ingham767af882010-07-07 03:36:20 +000046 ~CommandOptions ()
47 {
48 }
Chris Lattner24943d22010-06-08 16:52:24 +000049
Jim Ingham767af882010-07-07 03:36:20 +000050 Error
Greg Clayton143fcc32011-04-13 00:18:08 +000051 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Ingham767af882010-07-07 03:36:20 +000052 {
53 Error error;
54 const char short_option = g_option_table[option_idx].short_option;
55 switch (short_option)
Chris Lattner24943d22010-06-08 16:52:24 +000056 {
Jim Ingham767af882010-07-07 03:36:20 +000057 case 'l':
58 start_line = Args::StringToUInt32 (option_arg, 0);
59 if (start_line == 0)
Greg Clayton9c236732011-10-26 00:56:27 +000060 error.SetErrorStringWithFormat("invalid line number: '%s'", option_arg);
Jim Ingham767af882010-07-07 03:36:20 +000061 break;
Chris Lattner24943d22010-06-08 16:52:24 +000062
Jim Ingham767af882010-07-07 03:36:20 +000063 case 'f':
64 file_name = option_arg;
65 break;
66
67 default:
Greg Clayton9c236732011-10-26 00:56:27 +000068 error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
Jim Ingham767af882010-07-07 03:36:20 +000069 break;
Chris Lattner24943d22010-06-08 16:52:24 +000070 }
71
Jim Ingham767af882010-07-07 03:36:20 +000072 return error;
73 }
Chris Lattner24943d22010-06-08 16:52:24 +000074
Jim Ingham767af882010-07-07 03:36:20 +000075 void
Greg Clayton143fcc32011-04-13 00:18:08 +000076 OptionParsingStarting ()
Jim Ingham767af882010-07-07 03:36:20 +000077 {
Jim Ingham767af882010-07-07 03:36:20 +000078 file_spec.Clear();
79 file_name.clear();
80 start_line = 0;
81 }
82
Greg Claytonb3448432011-03-24 21:19:54 +000083 const OptionDefinition*
Jim Ingham767af882010-07-07 03:36:20 +000084 GetDefinitions ()
85 {
86 return g_option_table;
87 }
Greg Claytonb3448432011-03-24 21:19:54 +000088 static OptionDefinition g_option_table[];
Jim Ingham767af882010-07-07 03:36:20 +000089
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
97public:
Greg Clayton238c0a12010-09-18 01:14:36 +000098 CommandObjectSourceInfo(CommandInterpreter &interpreter) :
99 CommandObject (interpreter,
100 "source info",
101 "Display information about the source lines from the current executable's debug info.",
Greg Claytonf15996e2011-04-07 22:46:35 +0000102 "source info [<cmd-options>]"),
103 m_options (interpreter)
Jim Ingham767af882010-07-07 03:36:20 +0000104 {
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 Ingham767af882010-07-07 03:36:20 +0000122 Args& args,
123 CommandReturnObject &result
124 )
125 {
126 result.AppendError ("Not yet implemented");
127 result.SetStatus (eReturnStatusFailed);
128 return false;
129 }
130protected:
131 CommandOptions m_options;
132};
133
Greg Claytonb3448432011-03-24 21:19:54 +0000134OptionDefinition
Jim Ingham767af882010-07-07 03:36:20 +0000135CommandObjectSourceInfo::CommandOptions::g_option_table[] =
136{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000137{ 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 Ingham767af882010-07-07 03:36:20 +0000140};
141
142#pragma mark CommandObjectSourceList
143//-------------------------------------------------------------------------
144// CommandObjectSourceList
145//-------------------------------------------------------------------------
146
147class CommandObjectSourceList : public CommandObject
148{
149
150 class CommandOptions : public Options
151 {
152 public:
Greg Claytonf15996e2011-04-07 22:46:35 +0000153 CommandOptions (CommandInterpreter &interpreter) :
154 Options(interpreter)
Jim Ingham767af882010-07-07 03:36:20 +0000155 {
156 }
157
158 ~CommandOptions ()
159 {
160 }
161
162 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000163 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Ingham767af882010-07-07 03:36:20 +0000164 {
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 Clayton9c236732011-10-26 00:56:27 +0000172 error.SetErrorStringWithFormat("invalid line number: '%s'", option_arg);
Jim Ingham767af882010-07-07 03:36:20 +0000173 break;
174
Jim Ingham338f7532010-08-20 01:17:07 +0000175 case 'c':
Jim Ingham767af882010-07-07 03:36:20 +0000176 num_lines = Args::StringToUInt32 (option_arg, 0);
177 if (num_lines == 0)
Greg Clayton9c236732011-10-26 00:56:27 +0000178 error.SetErrorStringWithFormat("invalid line count: '%s'", option_arg);
Jim Ingham767af882010-07-07 03:36:20 +0000179 break;
180
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000181 case 'f':
Jim Ingham767af882010-07-07 03:36:20 +0000182 file_name = option_arg;
183 break;
Jim Ingham338f7532010-08-20 01:17:07 +0000184
185 case 'n':
186 symbol_name = option_arg;
187 break;
Jim Ingham767af882010-07-07 03:36:20 +0000188
Jim Ingham338f7532010-08-20 01:17:07 +0000189 case 's':
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000190 modules.push_back (std::string (option_arg));
191 break;
192
193 case 'b':
194 show_bp_locs = true;
Jim Ingham338f7532010-08-20 01:17:07 +0000195 break;
Jim Ingham767af882010-07-07 03:36:20 +0000196 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000197 error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
Jim Ingham767af882010-07-07 03:36:20 +0000198 break;
199 }
200
201 return error;
202 }
203
204 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000205 OptionParsingStarting ()
Jim Ingham767af882010-07-07 03:36:20 +0000206 {
Jim Ingham767af882010-07-07 03:36:20 +0000207 file_spec.Clear();
208 file_name.clear();
Jim Ingham338f7532010-08-20 01:17:07 +0000209 symbol_name.clear();
Jim Ingham767af882010-07-07 03:36:20 +0000210 start_line = 0;
211 num_lines = 10;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000212 show_bp_locs = false;
213 modules.clear();
Jim Ingham767af882010-07-07 03:36:20 +0000214 }
215
Greg Claytonb3448432011-03-24 21:19:54 +0000216 const OptionDefinition*
Jim Ingham767af882010-07-07 03:36:20 +0000217 GetDefinitions ()
218 {
219 return g_option_table;
220 }
Greg Claytonb3448432011-03-24 21:19:54 +0000221 static OptionDefinition g_option_table[];
Jim Ingham767af882010-07-07 03:36:20 +0000222
223 // Instance variables to hold the values for command options.
224 FileSpec file_spec;
225 std::string file_name;
Jim Ingham338f7532010-08-20 01:17:07 +0000226 std::string symbol_name;
Jim Ingham767af882010-07-07 03:36:20 +0000227 uint32_t start_line;
228 uint32_t num_lines;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000229 STLStringArray modules;
230 bool show_bp_locs;
Jim Ingham767af882010-07-07 03:36:20 +0000231 };
232
233public:
Greg Clayton238c0a12010-09-18 01:14:36 +0000234 CommandObjectSourceList(CommandInterpreter &interpreter) :
235 CommandObject (interpreter,
236 "source list",
237 "Display source code (as specified) based on the current executable's debug info.",
Greg Claytonf15996e2011-04-07 22:46:35 +0000238 NULL),
239 m_options (interpreter)
Jim Ingham767af882010-07-07 03:36:20 +0000240 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000241 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 Ingham767af882010-07-07 03:36:20 +0000253 }
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 Ingham767af882010-07-07 03:36:20 +0000270 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);
Jim Ingham26e089b2011-11-29 21:21:26 +0000280 return false;
Jim Ingham767af882010-07-07 03:36:20 +0000281 }
282
Greg Claytonb72d0f02011-04-12 05:54:46 +0000283 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +0000284 Target *target = exe_ctx.GetTargetPtr();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000285
Greg Clayton567e7f32011-09-22 04:58:26 +0000286 if (target == NULL)
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000287 target = m_interpreter.GetDebugger().GetSelectedTarget().get();
288
289 if (target == NULL)
290 {
291 result.AppendError ("invalid target, create a debug target using the 'target create' command");
292 result.SetStatus (eReturnStatusFailed);
293 return false;
294 }
295
Jim Ingham338f7532010-08-20 01:17:07 +0000296 if (!m_options.symbol_name.empty())
297 {
298 // Displaying the source for a symbol:
Jim Ingham338f7532010-08-20 01:17:07 +0000299 SymbolContextList sc_list;
300 ConstString name(m_options.symbol_name.c_str());
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000301 bool include_symbols = false;
Sean Callanan302d78c2012-02-10 22:52:19 +0000302 bool include_inlines = true;
Jim Ingham338f7532010-08-20 01:17:07 +0000303 bool append = true;
304 size_t num_matches = 0;
305
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000306 if (m_options.modules.size() > 0)
Jim Ingham338f7532010-08-20 01:17:07 +0000307 {
308 ModuleList matching_modules;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000309 for (unsigned i = 0, e = m_options.modules.size(); i != e; i++)
Jim Ingham338f7532010-08-20 01:17:07 +0000310 {
Greg Clayton444fe992012-02-26 05:51:37 +0000311 FileSpec module_file_spec(m_options.modules[i].c_str(), false);
312 if (module_file_spec)
Jim Ingham338f7532010-08-20 01:17:07 +0000313 {
Greg Clayton444fe992012-02-26 05:51:37 +0000314 ModuleSpec module_spec (module_file_spec);
Jim Ingham338f7532010-08-20 01:17:07 +0000315 matching_modules.Clear();
Greg Clayton444fe992012-02-26 05:51:37 +0000316 target->GetImages().FindModules (module_spec, matching_modules);
Sean Callanan302d78c2012-02-10 22:52:19 +0000317 num_matches += matching_modules.FindFunctions (name, eFunctionNameTypeAuto, include_symbols, include_inlines, append, sc_list);
Jim Ingham338f7532010-08-20 01:17:07 +0000318 }
319 }
320 }
321 else
322 {
Sean Callanan302d78c2012-02-10 22:52:19 +0000323 num_matches = target->GetImages().FindFunctions (name, eFunctionNameTypeAuto, include_symbols, include_inlines, append, sc_list);
Jim Ingham338f7532010-08-20 01:17:07 +0000324 }
325
326 SymbolContext sc;
327
328 if (num_matches == 0)
329 {
330 result.AppendErrorWithFormat("Could not find function named: \"%s\".\n", m_options.symbol_name.c_str());
331 result.SetStatus (eReturnStatusFailed);
332 return false;
333 }
334
335 sc_list.GetContextAtIndex (0, sc);
336 FileSpec start_file;
337 uint32_t start_line;
338 uint32_t end_line;
339 FileSpec end_file;
340 if (sc.function != NULL)
341 {
342 sc.function->GetStartLineSourceInfo (start_file, start_line);
343 if (start_line == 0)
344 {
345 result.AppendErrorWithFormat("Could not find line information for start of function: \"%s\".\n", m_options.symbol_name.c_str());
346 result.SetStatus (eReturnStatusFailed);
347 return false;
348 }
349 sc.function->GetEndLineSourceInfo (end_file, end_line);
350 }
351 else
352 {
353 result.AppendErrorWithFormat("Could not find function info for: \"%s\".\n", m_options.symbol_name.c_str());
354 result.SetStatus (eReturnStatusFailed);
355 return false;
356 }
357
358 if (num_matches > 1)
359 {
360 // This could either be because there are multiple functions of this name, in which case
361 // we'll have to specify this further... Or it could be because there are multiple inlined instances
362 // of one function. So run through the matches and if they all have the same file & line then we can just
363 // list one.
364
365 bool found_multiple = false;
366
367 for (size_t i = 1; i < num_matches; i++)
368 {
369 SymbolContext scratch_sc;
370 sc_list.GetContextAtIndex (i, scratch_sc);
371 if (scratch_sc.function != NULL)
372 {
373 FileSpec scratch_file;
374 uint32_t scratch_line;
375 scratch_sc.function->GetStartLineSourceInfo (scratch_file, scratch_line);
376 if (scratch_file != start_file
377 || scratch_line != start_line)
378 {
379 found_multiple = true;
380 break;
381 }
382 }
383 }
384 if (found_multiple)
385 {
386 StreamString s;
387 for (size_t i = 0; i < num_matches; i++)
388 {
389 SymbolContext scratch_sc;
390 sc_list.GetContextAtIndex (i, scratch_sc);
391 if (scratch_sc.function != NULL)
392 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000393 s.Printf("\n%lu: ", i);
Jim Ingham338f7532010-08-20 01:17:07 +0000394 scratch_sc.function->Dump (&s, true);
395 }
396 }
397 result.AppendErrorWithFormat("Multiple functions found matching: %s: \n%s\n",
398 m_options.symbol_name.c_str(),
399 s.GetData());
400 result.SetStatus (eReturnStatusFailed);
401 return false;
402 }
403 }
404
405
406 // This is a little hacky, but the first line table entry for a function points to the "{" that
407 // starts the function block. It would be nice to actually get the function
408 // declaration in there too. So back up a bit, but not further than what you're going to display.
409 size_t lines_to_back_up = m_options.num_lines >= 10 ? 5 : m_options.num_lines/2;
410 uint32_t line_no;
411 if (start_line <= lines_to_back_up)
412 line_no = 1;
413 else
414 line_no = start_line - lines_to_back_up;
415
416 // For fun, if the function is shorter than the number of lines we're supposed to display,
417 // only display the function...
418 if (end_line != 0)
419 {
420 if (m_options.num_lines > end_line - line_no)
421 m_options.num_lines = end_line - line_no;
422 }
423
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000424 char path_buf[PATH_MAX];
425 start_file.GetPath(path_buf, sizeof(path_buf));
Greg Clayton1cee1e62011-04-20 18:52:45 +0000426
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000427 if (m_options.show_bp_locs)
Greg Clayton1cee1e62011-04-20 18:52:45 +0000428 {
429 const bool show_inlines = true;
430 m_breakpoint_locations.Reset (start_file, 0, show_inlines);
Greg Clayton567e7f32011-09-22 04:58:26 +0000431 SearchFilter target_search_filter (exe_ctx.GetTargetSP());
Greg Clayton1cee1e62011-04-20 18:52:45 +0000432 target_search_filter.Search (m_breakpoint_locations);
433 }
434 else
435 m_breakpoint_locations.Clear();
436
Jim Ingham338f7532010-08-20 01:17:07 +0000437 result.AppendMessageWithFormat("File: %s.\n", path_buf);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000438 target->GetSourceManager().DisplaySourceLinesWithLineNumbers (start_file,
439 line_no,
440 0,
441 m_options.num_lines,
442 "",
443 &result.GetOutputStream(),
444 GetBreakpointLocations ());
Jim Ingham338f7532010-08-20 01:17:07 +0000445
446 result.SetStatus (eReturnStatusSuccessFinishResult);
447 return true;
448
449 }
450 else if (m_options.file_name.empty())
Jim Ingham767af882010-07-07 03:36:20 +0000451 {
452 // Last valid source manager context, or the current frame if no
453 // valid last context in source manager.
454 // One little trick here, if you type the exact same list command twice in a row, it is
455 // more likely because you typed it once, then typed it again
456 if (m_options.start_line == 0)
457 {
Jim Inghamc7f18c82011-09-29 20:22:33 +0000458 if (target->GetSourceManager().DisplayMoreWithLineNumbers (&result.GetOutputStream(),
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000459 GetBreakpointLocations ()))
Chris Lattner24943d22010-06-08 16:52:24 +0000460 {
Chris Lattner24943d22010-06-08 16:52:24 +0000461 result.SetStatus (eReturnStatusSuccessFinishResult);
462 }
Jim Ingham767af882010-07-07 03:36:20 +0000463 }
464 else
465 {
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000466 if (m_options.show_bp_locs)
Greg Clayton1cee1e62011-04-20 18:52:45 +0000467 {
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000468 SourceManager::FileSP last_file_sp (target->GetSourceManager().GetLastFile ());
Greg Clayton1cee1e62011-04-20 18:52:45 +0000469 if (last_file_sp)
470 {
471 const bool show_inlines = true;
472 m_breakpoint_locations.Reset (last_file_sp->GetFileSpec(), 0, show_inlines);
Greg Clayton13d24fb2012-01-29 20:56:30 +0000473 SearchFilter target_search_filter (target->shared_from_this());
Greg Clayton1cee1e62011-04-20 18:52:45 +0000474 target_search_filter.Search (m_breakpoint_locations);
475 }
476 }
477 else
478 m_breakpoint_locations.Clear();
479
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000480 if (target->GetSourceManager().DisplaySourceLinesWithLineNumbersUsingLastFile(
Jim Ingham767af882010-07-07 03:36:20 +0000481 m_options.start_line, // Line to display
482 0, // Lines before line to display
483 m_options.num_lines, // Lines after line to display
484 "", // Don't mark "line"
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000485 &result.GetOutputStream(),
486 GetBreakpointLocations ()))
Chris Lattner24943d22010-06-08 16:52:24 +0000487 {
Jim Ingham767af882010-07-07 03:36:20 +0000488 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000489 }
Jim Ingham767af882010-07-07 03:36:20 +0000490
Chris Lattner24943d22010-06-08 16:52:24 +0000491 }
492 }
493 else
494 {
Jim Ingham767af882010-07-07 03:36:20 +0000495 const char *filename = m_options.file_name.c_str();
Jim Ingham767af882010-07-07 03:36:20 +0000496
497 bool check_inlines = false;
498 SymbolContextList sc_list;
Jim Ingham338f7532010-08-20 01:17:07 +0000499 size_t num_matches = 0;
500
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000501 if (m_options.modules.size() > 0)
Jim Ingham767af882010-07-07 03:36:20 +0000502 {
Jim Ingham338f7532010-08-20 01:17:07 +0000503 ModuleList matching_modules;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000504 for (unsigned i = 0, e = m_options.modules.size(); i != e; i++)
Jim Ingham767af882010-07-07 03:36:20 +0000505 {
Greg Clayton444fe992012-02-26 05:51:37 +0000506 FileSpec module_file_spec(m_options.modules[i].c_str(), false);
507 if (module_file_spec)
Jim Ingham767af882010-07-07 03:36:20 +0000508 {
Greg Clayton444fe992012-02-26 05:51:37 +0000509 ModuleSpec module_spec (module_file_spec);
Jim Ingham338f7532010-08-20 01:17:07 +0000510 matching_modules.Clear();
Greg Clayton444fe992012-02-26 05:51:37 +0000511 target->GetImages().FindModules (module_spec, matching_modules);
Jim Ingham338f7532010-08-20 01:17:07 +0000512 num_matches += matching_modules.ResolveSymbolContextForFilePath (filename,
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000513 0,
514 check_inlines,
515 eSymbolContextModule | eSymbolContextCompUnit,
516 sc_list);
Jim Ingham767af882010-07-07 03:36:20 +0000517 }
518 }
519 }
Jim Ingham338f7532010-08-20 01:17:07 +0000520 else
521 {
522 num_matches = target->GetImages().ResolveSymbolContextForFilePath (filename,
523 0,
524 check_inlines,
525 eSymbolContextModule | eSymbolContextCompUnit,
526 sc_list);
527 }
528
529 if (num_matches == 0)
530 {
531 result.AppendErrorWithFormat("Could not find source file \"%s\".\n",
532 m_options.file_name.c_str());
533 result.SetStatus (eReturnStatusFailed);
534 return false;
535 }
536
537 if (num_matches > 1)
538 {
539 SymbolContext sc;
540 bool got_multiple = false;
541 FileSpec *test_cu_spec = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000542
Chris Lattner0f6fa732010-09-08 22:55:31 +0000543 for (unsigned i = 0; i < num_matches; i++)
Jim Ingham338f7532010-08-20 01:17:07 +0000544 {
545 sc_list.GetContextAtIndex(i, sc);
546 if (sc.comp_unit)
547 {
548 if (test_cu_spec)
549 {
550 if (test_cu_spec != static_cast<FileSpec *> (sc.comp_unit))
551 got_multiple = true;
552 break;
553 }
554 else
555 test_cu_spec = sc.comp_unit;
556 }
557 }
558 if (got_multiple)
559 {
560 result.AppendErrorWithFormat("Multiple source files found matching: \"%s.\"\n",
561 m_options.file_name.c_str());
562 result.SetStatus (eReturnStatusFailed);
563 return false;
564 }
565 }
566
567 SymbolContext sc;
568 if (sc_list.GetContextAtIndex(0, sc))
569 {
570 if (sc.comp_unit)
571 {
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000572 if (m_options.show_bp_locs)
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000573 {
574 const bool show_inlines = true;
575 m_breakpoint_locations.Reset (*sc.comp_unit, 0, show_inlines);
Greg Clayton13d24fb2012-01-29 20:56:30 +0000576 SearchFilter target_search_filter (target->shared_from_this());
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000577 target_search_filter.Search (m_breakpoint_locations);
578 }
579 else
580 m_breakpoint_locations.Clear();
581
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000582 target->GetSourceManager().DisplaySourceLinesWithLineNumbers (sc.comp_unit,
583 m_options.start_line,
584 0,
585 m_options.num_lines,
586 "",
587 &result.GetOutputStream(),
588 GetBreakpointLocations ());
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000589
Jim Ingham338f7532010-08-20 01:17:07 +0000590 result.SetStatus (eReturnStatusSuccessFinishResult);
591 }
592 else
593 {
594 result.AppendErrorWithFormat("No comp unit found for: \"%s.\"\n",
595 m_options.file_name.c_str());
596 result.SetStatus (eReturnStatusFailed);
597 return false;
598 }
599 }
600 }
Jim Ingham767af882010-07-07 03:36:20 +0000601 return result.Succeeded();
Chris Lattner24943d22010-06-08 16:52:24 +0000602 }
Jim Ingham767af882010-07-07 03:36:20 +0000603
604 virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
Chris Lattner24943d22010-06-08 16:52:24 +0000605 {
Jim Ingham767af882010-07-07 03:36:20 +0000606 return m_cmd_name.c_str();
Chris Lattner24943d22010-06-08 16:52:24 +0000607 }
Chris Lattner24943d22010-06-08 16:52:24 +0000608
Jim Ingham767af882010-07-07 03:36:20 +0000609protected:
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000610 const SymbolContextList *
611 GetBreakpointLocations ()
612 {
613 if (m_breakpoint_locations.GetFileLineMatches().GetSize() > 0)
614 return &m_breakpoint_locations.GetFileLineMatches();
615 return NULL;
616 }
Jim Ingham767af882010-07-07 03:36:20 +0000617 CommandOptions m_options;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000618 FileLineResolver m_breakpoint_locations;
Jim Ingham767af882010-07-07 03:36:20 +0000619
620};
621
Greg Claytonb3448432011-03-24 21:19:54 +0000622OptionDefinition
Jim Ingham767af882010-07-07 03:36:20 +0000623CommandObjectSourceList::CommandOptions::g_option_table[] =
624{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000625{ LLDB_OPT_SET_ALL, false, "count", 'c', required_argument, NULL, 0, eArgTypeCount, "The number of source lines to display."},
626{ 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 +0000627{ 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 +0000628{ LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "The file from which to display source."},
629{ LLDB_OPT_SET_1, false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "The line number at which to start the display source."},
630{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeSymbol, "The name of a function whose source to display."},
631{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Jim Ingham767af882010-07-07 03:36:20 +0000632};
633
634#pragma mark CommandObjectMultiwordSource
635
636//-------------------------------------------------------------------------
637// CommandObjectMultiwordSource
638//-------------------------------------------------------------------------
639
640CommandObjectMultiwordSource::CommandObjectMultiwordSource (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +0000641 CommandObjectMultiword (interpreter,
642 "source",
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000643 "A set of commands for accessing source file information",
Jim Ingham767af882010-07-07 03:36:20 +0000644 "source <subcommand> [<subcommand-options>]")
645{
Greg Clayton238c0a12010-09-18 01:14:36 +0000646 LoadSubCommand ("info", CommandObjectSP (new CommandObjectSourceInfo (interpreter)));
647 LoadSubCommand ("list", CommandObjectSP (new CommandObjectSourceList (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +0000648}
Jim Ingham767af882010-07-07 03:36:20 +0000649
650CommandObjectMultiwordSource::~CommandObjectMultiwordSource ()
651{
652}
653