blob: ec58e94d445f6544003b713c75589332e94a2828 [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 Inghamda26bd22012-06-08 21:56:10 +000035class CommandObjectSourceInfo : public CommandObjectParsed
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) :
Jim Inghamda26bd22012-06-08 21:56:10 +000099 CommandObjectParsed (interpreter,
100 "source info",
101 "Display information about the source lines from the current executable's debug info.",
102 "source info [<cmd-options>]"),
Greg Claytonf15996e2011-04-07 22:46:35 +0000103 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
Jim Inghamda26bd22012-06-08 21:56:10 +0000118protected:
Jim Ingham767af882010-07-07 03:36:20 +0000119 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000120 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham767af882010-07-07 03:36:20 +0000121 {
122 result.AppendError ("Not yet implemented");
123 result.SetStatus (eReturnStatusFailed);
124 return false;
125 }
Jim Inghamda26bd22012-06-08 21:56:10 +0000126
Jim Ingham767af882010-07-07 03:36:20 +0000127 CommandOptions m_options;
128};
129
Greg Claytonb3448432011-03-24 21:19:54 +0000130OptionDefinition
Jim Ingham767af882010-07-07 03:36:20 +0000131CommandObjectSourceInfo::CommandOptions::g_option_table[] =
132{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000133{ LLDB_OPT_SET_1, false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "The line number at which to start the display source."},
134{ LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "The file from which to display source."},
135{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Jim Ingham767af882010-07-07 03:36:20 +0000136};
137
138#pragma mark CommandObjectSourceList
139//-------------------------------------------------------------------------
140// CommandObjectSourceList
141//-------------------------------------------------------------------------
142
Jim Inghamda26bd22012-06-08 21:56:10 +0000143class CommandObjectSourceList : public CommandObjectParsed
Jim Ingham767af882010-07-07 03:36:20 +0000144{
145
146 class CommandOptions : public Options
147 {
148 public:
Greg Claytonf15996e2011-04-07 22:46:35 +0000149 CommandOptions (CommandInterpreter &interpreter) :
150 Options(interpreter)
Jim Ingham767af882010-07-07 03:36:20 +0000151 {
152 }
153
154 ~CommandOptions ()
155 {
156 }
157
158 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000159 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Ingham767af882010-07-07 03:36:20 +0000160 {
161 Error error;
162 const char short_option = g_option_table[option_idx].short_option;
163 switch (short_option)
164 {
165 case 'l':
166 start_line = Args::StringToUInt32 (option_arg, 0);
167 if (start_line == 0)
Greg Clayton9c236732011-10-26 00:56:27 +0000168 error.SetErrorStringWithFormat("invalid line number: '%s'", option_arg);
Jim Ingham767af882010-07-07 03:36:20 +0000169 break;
170
Jim Ingham338f7532010-08-20 01:17:07 +0000171 case 'c':
Jim Ingham767af882010-07-07 03:36:20 +0000172 num_lines = Args::StringToUInt32 (option_arg, 0);
173 if (num_lines == 0)
Greg Clayton9c236732011-10-26 00:56:27 +0000174 error.SetErrorStringWithFormat("invalid line count: '%s'", option_arg);
Jim Ingham767af882010-07-07 03:36:20 +0000175 break;
176
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000177 case 'f':
Jim Ingham767af882010-07-07 03:36:20 +0000178 file_name = option_arg;
179 break;
Jim Ingham338f7532010-08-20 01:17:07 +0000180
181 case 'n':
182 symbol_name = option_arg;
183 break;
Jim Ingham767af882010-07-07 03:36:20 +0000184
Jim Ingham338f7532010-08-20 01:17:07 +0000185 case 's':
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000186 modules.push_back (std::string (option_arg));
187 break;
188
189 case 'b':
190 show_bp_locs = true;
Jim Ingham338f7532010-08-20 01:17:07 +0000191 break;
Jim Ingham767af882010-07-07 03:36:20 +0000192 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000193 error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
Jim Ingham767af882010-07-07 03:36:20 +0000194 break;
195 }
196
197 return error;
198 }
199
200 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000201 OptionParsingStarting ()
Jim Ingham767af882010-07-07 03:36:20 +0000202 {
Jim Ingham767af882010-07-07 03:36:20 +0000203 file_spec.Clear();
204 file_name.clear();
Jim Ingham338f7532010-08-20 01:17:07 +0000205 symbol_name.clear();
Jim Ingham767af882010-07-07 03:36:20 +0000206 start_line = 0;
207 num_lines = 10;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000208 show_bp_locs = false;
209 modules.clear();
Jim Ingham767af882010-07-07 03:36:20 +0000210 }
211
Greg Claytonb3448432011-03-24 21:19:54 +0000212 const OptionDefinition*
Jim Ingham767af882010-07-07 03:36:20 +0000213 GetDefinitions ()
214 {
215 return g_option_table;
216 }
Greg Claytonb3448432011-03-24 21:19:54 +0000217 static OptionDefinition g_option_table[];
Jim Ingham767af882010-07-07 03:36:20 +0000218
219 // Instance variables to hold the values for command options.
220 FileSpec file_spec;
221 std::string file_name;
Jim Ingham338f7532010-08-20 01:17:07 +0000222 std::string symbol_name;
Jim Ingham767af882010-07-07 03:36:20 +0000223 uint32_t start_line;
224 uint32_t num_lines;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000225 STLStringArray modules;
226 bool show_bp_locs;
Jim Ingham767af882010-07-07 03:36:20 +0000227 };
228
229public:
Greg Clayton238c0a12010-09-18 01:14:36 +0000230 CommandObjectSourceList(CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000231 CommandObjectParsed (interpreter,
232 "source list",
233 "Display source code (as specified) based on the current executable's debug info.",
234 NULL),
Greg Claytonf15996e2011-04-07 22:46:35 +0000235 m_options (interpreter)
Jim Ingham767af882010-07-07 03:36:20 +0000236 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000237 CommandArgumentEntry arg;
238 CommandArgumentData file_arg;
239
240 // Define the first (and only) variant of this arg.
241 file_arg.arg_type = eArgTypeFilename;
242 file_arg.arg_repetition = eArgRepeatOptional;
243
244 // There is only one variant this argument could be; put it into the argument entry.
245 arg.push_back (file_arg);
246
247 // Push the data for the first argument into the m_arguments vector.
248 m_arguments.push_back (arg);
Jim Ingham767af882010-07-07 03:36:20 +0000249 }
250
251 ~CommandObjectSourceList ()
252 {
253 }
254
255
256 Options *
257 GetOptions ()
258 {
259 return &m_options;
260 }
261
Jim Inghamda26bd22012-06-08 21:56:10 +0000262 virtual const char *
263 GetRepeatCommand (Args &current_command_args, uint32_t index)
Jim Ingham767af882010-07-07 03:36:20 +0000264 {
Jim Inghamda26bd22012-06-08 21:56:10 +0000265 return m_cmd_name.c_str();
266 }
267
268protected:
269 bool
270 DoExecute (Args& command, CommandReturnObject &result)
271 {
272 const int argc = command.GetArgumentCount();
Jim Ingham767af882010-07-07 03:36:20 +0000273
274 if (argc != 0)
275 {
276 result.AppendErrorWithFormat("'%s' takes no arguments, only flags.\n", GetCommandName());
277 result.SetStatus (eReturnStatusFailed);
Jim Ingham26e089b2011-11-29 21:21:26 +0000278 return false;
Jim Ingham767af882010-07-07 03:36:20 +0000279 }
280
Greg Claytonb72d0f02011-04-12 05:54:46 +0000281 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +0000282 Target *target = exe_ctx.GetTargetPtr();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000283
Greg Clayton567e7f32011-09-22 04:58:26 +0000284 if (target == NULL)
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000285 target = m_interpreter.GetDebugger().GetSelectedTarget().get();
286
287 if (target == NULL)
288 {
289 result.AppendError ("invalid target, create a debug target using the 'target create' command");
290 result.SetStatus (eReturnStatusFailed);
291 return false;
292 }
293
Jim Ingham338f7532010-08-20 01:17:07 +0000294 if (!m_options.symbol_name.empty())
295 {
296 // Displaying the source for a symbol:
Jim Ingham338f7532010-08-20 01:17:07 +0000297 SymbolContextList sc_list;
298 ConstString name(m_options.symbol_name.c_str());
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000299 bool include_symbols = false;
Sean Callanan302d78c2012-02-10 22:52:19 +0000300 bool include_inlines = true;
Jim Ingham338f7532010-08-20 01:17:07 +0000301 bool append = true;
302 size_t num_matches = 0;
303
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000304 if (m_options.modules.size() > 0)
Jim Ingham338f7532010-08-20 01:17:07 +0000305 {
306 ModuleList matching_modules;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000307 for (unsigned i = 0, e = m_options.modules.size(); i != e; i++)
Jim Ingham338f7532010-08-20 01:17:07 +0000308 {
Greg Clayton444fe992012-02-26 05:51:37 +0000309 FileSpec module_file_spec(m_options.modules[i].c_str(), false);
310 if (module_file_spec)
Jim Ingham338f7532010-08-20 01:17:07 +0000311 {
Greg Clayton444fe992012-02-26 05:51:37 +0000312 ModuleSpec module_spec (module_file_spec);
Jim Ingham338f7532010-08-20 01:17:07 +0000313 matching_modules.Clear();
Greg Clayton444fe992012-02-26 05:51:37 +0000314 target->GetImages().FindModules (module_spec, matching_modules);
Sean Callanan302d78c2012-02-10 22:52:19 +0000315 num_matches += matching_modules.FindFunctions (name, eFunctionNameTypeAuto, include_symbols, include_inlines, append, sc_list);
Jim Ingham338f7532010-08-20 01:17:07 +0000316 }
317 }
318 }
319 else
320 {
Sean Callanan302d78c2012-02-10 22:52:19 +0000321 num_matches = target->GetImages().FindFunctions (name, eFunctionNameTypeAuto, include_symbols, include_inlines, append, sc_list);
Jim Ingham338f7532010-08-20 01:17:07 +0000322 }
323
324 SymbolContext sc;
325
326 if (num_matches == 0)
327 {
328 result.AppendErrorWithFormat("Could not find function named: \"%s\".\n", m_options.symbol_name.c_str());
329 result.SetStatus (eReturnStatusFailed);
330 return false;
331 }
332
333 sc_list.GetContextAtIndex (0, sc);
334 FileSpec start_file;
335 uint32_t start_line;
336 uint32_t end_line;
337 FileSpec end_file;
338 if (sc.function != NULL)
339 {
340 sc.function->GetStartLineSourceInfo (start_file, start_line);
341 if (start_line == 0)
342 {
343 result.AppendErrorWithFormat("Could not find line information for start of function: \"%s\".\n", m_options.symbol_name.c_str());
344 result.SetStatus (eReturnStatusFailed);
345 return false;
346 }
347 sc.function->GetEndLineSourceInfo (end_file, end_line);
348 }
349 else
350 {
351 result.AppendErrorWithFormat("Could not find function info for: \"%s\".\n", m_options.symbol_name.c_str());
352 result.SetStatus (eReturnStatusFailed);
353 return false;
354 }
355
356 if (num_matches > 1)
357 {
358 // This could either be because there are multiple functions of this name, in which case
359 // we'll have to specify this further... Or it could be because there are multiple inlined instances
360 // of one function. So run through the matches and if they all have the same file & line then we can just
361 // list one.
362
363 bool found_multiple = false;
364
365 for (size_t i = 1; i < num_matches; i++)
366 {
367 SymbolContext scratch_sc;
368 sc_list.GetContextAtIndex (i, scratch_sc);
369 if (scratch_sc.function != NULL)
370 {
371 FileSpec scratch_file;
372 uint32_t scratch_line;
373 scratch_sc.function->GetStartLineSourceInfo (scratch_file, scratch_line);
374 if (scratch_file != start_file
375 || scratch_line != start_line)
376 {
377 found_multiple = true;
378 break;
379 }
380 }
381 }
382 if (found_multiple)
383 {
384 StreamString s;
385 for (size_t i = 0; i < num_matches; i++)
386 {
387 SymbolContext scratch_sc;
388 sc_list.GetContextAtIndex (i, scratch_sc);
389 if (scratch_sc.function != NULL)
390 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000391 s.Printf("\n%lu: ", i);
Jim Ingham338f7532010-08-20 01:17:07 +0000392 scratch_sc.function->Dump (&s, true);
393 }
394 }
395 result.AppendErrorWithFormat("Multiple functions found matching: %s: \n%s\n",
396 m_options.symbol_name.c_str(),
397 s.GetData());
398 result.SetStatus (eReturnStatusFailed);
399 return false;
400 }
401 }
402
403
404 // This is a little hacky, but the first line table entry for a function points to the "{" that
405 // starts the function block. It would be nice to actually get the function
406 // declaration in there too. So back up a bit, but not further than what you're going to display.
407 size_t lines_to_back_up = m_options.num_lines >= 10 ? 5 : m_options.num_lines/2;
408 uint32_t line_no;
409 if (start_line <= lines_to_back_up)
410 line_no = 1;
411 else
412 line_no = start_line - lines_to_back_up;
413
414 // For fun, if the function is shorter than the number of lines we're supposed to display,
415 // only display the function...
416 if (end_line != 0)
417 {
418 if (m_options.num_lines > end_line - line_no)
419 m_options.num_lines = end_line - line_no;
420 }
421
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000422 char path_buf[PATH_MAX];
423 start_file.GetPath(path_buf, sizeof(path_buf));
Greg Clayton1cee1e62011-04-20 18:52:45 +0000424
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000425 if (m_options.show_bp_locs)
Greg Clayton1cee1e62011-04-20 18:52:45 +0000426 {
427 const bool show_inlines = true;
428 m_breakpoint_locations.Reset (start_file, 0, show_inlines);
Greg Clayton567e7f32011-09-22 04:58:26 +0000429 SearchFilter target_search_filter (exe_ctx.GetTargetSP());
Greg Clayton1cee1e62011-04-20 18:52:45 +0000430 target_search_filter.Search (m_breakpoint_locations);
431 }
432 else
433 m_breakpoint_locations.Clear();
434
Jim Ingham338f7532010-08-20 01:17:07 +0000435 result.AppendMessageWithFormat("File: %s.\n", path_buf);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000436 target->GetSourceManager().DisplaySourceLinesWithLineNumbers (start_file,
437 line_no,
438 0,
439 m_options.num_lines,
440 "",
441 &result.GetOutputStream(),
442 GetBreakpointLocations ());
Jim Ingham338f7532010-08-20 01:17:07 +0000443
444 result.SetStatus (eReturnStatusSuccessFinishResult);
445 return true;
446
447 }
448 else if (m_options.file_name.empty())
Jim Ingham767af882010-07-07 03:36:20 +0000449 {
450 // Last valid source manager context, or the current frame if no
451 // valid last context in source manager.
452 // One little trick here, if you type the exact same list command twice in a row, it is
453 // more likely because you typed it once, then typed it again
454 if (m_options.start_line == 0)
455 {
Jim Inghamc7f18c82011-09-29 20:22:33 +0000456 if (target->GetSourceManager().DisplayMoreWithLineNumbers (&result.GetOutputStream(),
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000457 GetBreakpointLocations ()))
Chris Lattner24943d22010-06-08 16:52:24 +0000458 {
Chris Lattner24943d22010-06-08 16:52:24 +0000459 result.SetStatus (eReturnStatusSuccessFinishResult);
460 }
Jim Ingham767af882010-07-07 03:36:20 +0000461 }
462 else
463 {
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000464 if (m_options.show_bp_locs)
Greg Clayton1cee1e62011-04-20 18:52:45 +0000465 {
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000466 SourceManager::FileSP last_file_sp (target->GetSourceManager().GetLastFile ());
Greg Clayton1cee1e62011-04-20 18:52:45 +0000467 if (last_file_sp)
468 {
469 const bool show_inlines = true;
470 m_breakpoint_locations.Reset (last_file_sp->GetFileSpec(), 0, show_inlines);
Greg Clayton13d24fb2012-01-29 20:56:30 +0000471 SearchFilter target_search_filter (target->shared_from_this());
Greg Clayton1cee1e62011-04-20 18:52:45 +0000472 target_search_filter.Search (m_breakpoint_locations);
473 }
474 }
475 else
476 m_breakpoint_locations.Clear();
477
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000478 if (target->GetSourceManager().DisplaySourceLinesWithLineNumbersUsingLastFile(
Jim Ingham767af882010-07-07 03:36:20 +0000479 m_options.start_line, // Line to display
480 0, // Lines before line to display
481 m_options.num_lines, // Lines after line to display
482 "", // Don't mark "line"
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000483 &result.GetOutputStream(),
484 GetBreakpointLocations ()))
Chris Lattner24943d22010-06-08 16:52:24 +0000485 {
Jim Ingham767af882010-07-07 03:36:20 +0000486 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000487 }
Jim Ingham767af882010-07-07 03:36:20 +0000488
Chris Lattner24943d22010-06-08 16:52:24 +0000489 }
490 }
491 else
492 {
Jim Ingham767af882010-07-07 03:36:20 +0000493 const char *filename = m_options.file_name.c_str();
Jim Ingham767af882010-07-07 03:36:20 +0000494
495 bool check_inlines = false;
496 SymbolContextList sc_list;
Jim Ingham338f7532010-08-20 01:17:07 +0000497 size_t num_matches = 0;
498
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000499 if (m_options.modules.size() > 0)
Jim Ingham767af882010-07-07 03:36:20 +0000500 {
Jim Ingham338f7532010-08-20 01:17:07 +0000501 ModuleList matching_modules;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000502 for (unsigned i = 0, e = m_options.modules.size(); i != e; i++)
Jim Ingham767af882010-07-07 03:36:20 +0000503 {
Greg Clayton444fe992012-02-26 05:51:37 +0000504 FileSpec module_file_spec(m_options.modules[i].c_str(), false);
505 if (module_file_spec)
Jim Ingham767af882010-07-07 03:36:20 +0000506 {
Greg Clayton444fe992012-02-26 05:51:37 +0000507 ModuleSpec module_spec (module_file_spec);
Jim Ingham338f7532010-08-20 01:17:07 +0000508 matching_modules.Clear();
Greg Clayton444fe992012-02-26 05:51:37 +0000509 target->GetImages().FindModules (module_spec, matching_modules);
Jim Ingham338f7532010-08-20 01:17:07 +0000510 num_matches += matching_modules.ResolveSymbolContextForFilePath (filename,
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000511 0,
512 check_inlines,
513 eSymbolContextModule | eSymbolContextCompUnit,
514 sc_list);
Jim Ingham767af882010-07-07 03:36:20 +0000515 }
516 }
517 }
Jim Ingham338f7532010-08-20 01:17:07 +0000518 else
519 {
520 num_matches = target->GetImages().ResolveSymbolContextForFilePath (filename,
521 0,
522 check_inlines,
523 eSymbolContextModule | eSymbolContextCompUnit,
524 sc_list);
525 }
526
527 if (num_matches == 0)
528 {
529 result.AppendErrorWithFormat("Could not find source file \"%s\".\n",
530 m_options.file_name.c_str());
531 result.SetStatus (eReturnStatusFailed);
532 return false;
533 }
534
535 if (num_matches > 1)
536 {
537 SymbolContext sc;
538 bool got_multiple = false;
539 FileSpec *test_cu_spec = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000540
Chris Lattner0f6fa732010-09-08 22:55:31 +0000541 for (unsigned i = 0; i < num_matches; i++)
Jim Ingham338f7532010-08-20 01:17:07 +0000542 {
543 sc_list.GetContextAtIndex(i, sc);
544 if (sc.comp_unit)
545 {
546 if (test_cu_spec)
547 {
548 if (test_cu_spec != static_cast<FileSpec *> (sc.comp_unit))
549 got_multiple = true;
550 break;
551 }
552 else
553 test_cu_spec = sc.comp_unit;
554 }
555 }
556 if (got_multiple)
557 {
558 result.AppendErrorWithFormat("Multiple source files found matching: \"%s.\"\n",
559 m_options.file_name.c_str());
560 result.SetStatus (eReturnStatusFailed);
561 return false;
562 }
563 }
564
565 SymbolContext sc;
566 if (sc_list.GetContextAtIndex(0, sc))
567 {
568 if (sc.comp_unit)
569 {
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000570 if (m_options.show_bp_locs)
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000571 {
572 const bool show_inlines = true;
573 m_breakpoint_locations.Reset (*sc.comp_unit, 0, show_inlines);
Greg Clayton13d24fb2012-01-29 20:56:30 +0000574 SearchFilter target_search_filter (target->shared_from_this());
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000575 target_search_filter.Search (m_breakpoint_locations);
576 }
577 else
578 m_breakpoint_locations.Clear();
579
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000580 target->GetSourceManager().DisplaySourceLinesWithLineNumbers (sc.comp_unit,
581 m_options.start_line,
582 0,
583 m_options.num_lines,
584 "",
585 &result.GetOutputStream(),
586 GetBreakpointLocations ());
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000587
Jim Ingham338f7532010-08-20 01:17:07 +0000588 result.SetStatus (eReturnStatusSuccessFinishResult);
589 }
590 else
591 {
592 result.AppendErrorWithFormat("No comp unit found for: \"%s.\"\n",
593 m_options.file_name.c_str());
594 result.SetStatus (eReturnStatusFailed);
595 return false;
596 }
597 }
598 }
Jim Ingham767af882010-07-07 03:36:20 +0000599 return result.Succeeded();
Chris Lattner24943d22010-06-08 16:52:24 +0000600 }
Jim Ingham767af882010-07-07 03:36:20 +0000601
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000602 const SymbolContextList *
603 GetBreakpointLocations ()
604 {
605 if (m_breakpoint_locations.GetFileLineMatches().GetSize() > 0)
606 return &m_breakpoint_locations.GetFileLineMatches();
607 return NULL;
608 }
Jim Ingham767af882010-07-07 03:36:20 +0000609 CommandOptions m_options;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000610 FileLineResolver m_breakpoint_locations;
Jim Ingham767af882010-07-07 03:36:20 +0000611
612};
613
Greg Claytonb3448432011-03-24 21:19:54 +0000614OptionDefinition
Jim Ingham767af882010-07-07 03:36:20 +0000615CommandObjectSourceList::CommandOptions::g_option_table[] =
616{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000617{ LLDB_OPT_SET_ALL, false, "count", 'c', required_argument, NULL, 0, eArgTypeCount, "The number of source lines to display."},
618{ 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 +0000619{ 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 +0000620{ LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "The file from which to display source."},
621{ LLDB_OPT_SET_1, false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "The line number at which to start the display source."},
622{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeSymbol, "The name of a function whose source to display."},
623{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Jim Ingham767af882010-07-07 03:36:20 +0000624};
625
626#pragma mark CommandObjectMultiwordSource
627
628//-------------------------------------------------------------------------
629// CommandObjectMultiwordSource
630//-------------------------------------------------------------------------
631
632CommandObjectMultiwordSource::CommandObjectMultiwordSource (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +0000633 CommandObjectMultiword (interpreter,
634 "source",
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000635 "A set of commands for accessing source file information",
Jim Ingham767af882010-07-07 03:36:20 +0000636 "source <subcommand> [<subcommand-options>]")
637{
Greg Clayton238c0a12010-09-18 01:14:36 +0000638 LoadSubCommand ("info", CommandObjectSP (new CommandObjectSourceInfo (interpreter)));
639 LoadSubCommand ("list", CommandObjectSP (new CommandObjectSourceList (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +0000640}
Jim Ingham767af882010-07-07 03:36:20 +0000641
642CommandObjectMultiwordSource::~CommandObjectMultiwordSource ()
643{
644}
645