blob: 0beaadf5728451ec2ec64e8c3a106eeaefcbb468 [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
Daniel Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "CommandObjectSource.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
Jim Ingham84cdc152010-06-15 19:49:27 +000018#include "lldb/Interpreter/Args.h"
Greg Clayton63094e02010-06-23 01:19:29 +000019#include "lldb/Core/Debugger.h"
Greg Clayton52c8b6e2011-04-19 04:19:37 +000020#include "lldb/Core/FileLineResolver.h"
Greg Clayton78124112012-12-07 00:19:47 +000021#include "lldb/Core/Module.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000022#include "lldb/Core/ModuleSpec.h"
Greg Clayton52c8b6e2011-04-19 04:19:37 +000023#include "lldb/Core/SourceManager.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Interpreter/CommandInterpreter.h"
25#include "lldb/Interpreter/CommandReturnObject.h"
Greg Clayton5f54ac32011-02-08 05:05:52 +000026#include "lldb/Host/FileSpec.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000027#include "lldb/Symbol/CompileUnit.h"
28#include "lldb/Symbol/Function.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029#include "lldb/Target/Process.h"
30#include "lldb/Target/TargetList.h"
Jim Ingham767af882010-07-07 03:36:20 +000031#include "lldb/Interpreter/CommandCompletions.h"
32#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000033
34using namespace lldb;
35using namespace lldb_private;
36
Chris Lattner24943d22010-06-08 16:52:24 +000037//-------------------------------------------------------------------------
Greg Clayton52c8b6e2011-04-19 04:19:37 +000038// CommandObjectSourceInfo
Chris Lattner24943d22010-06-08 16:52:24 +000039//-------------------------------------------------------------------------
40
Jim Inghamda26bd22012-06-08 21:56:10 +000041class CommandObjectSourceInfo : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +000042{
Chris Lattner24943d22010-06-08 16:52:24 +000043
Jim Ingham767af882010-07-07 03:36:20 +000044 class CommandOptions : public Options
Chris Lattner24943d22010-06-08 16:52:24 +000045 {
Jim Ingham767af882010-07-07 03:36:20 +000046 public:
Greg Claytonf15996e2011-04-07 22:46:35 +000047 CommandOptions (CommandInterpreter &interpreter) :
48 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000049 {
Jim Ingham767af882010-07-07 03:36:20 +000050 }
Chris Lattner24943d22010-06-08 16:52:24 +000051
Jim Ingham767af882010-07-07 03:36:20 +000052 ~CommandOptions ()
53 {
54 }
Chris Lattner24943d22010-06-08 16:52:24 +000055
Jim Ingham767af882010-07-07 03:36:20 +000056 Error
Greg Clayton143fcc32011-04-13 00:18:08 +000057 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Ingham767af882010-07-07 03:36:20 +000058 {
59 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +000060 const int short_option = g_option_table[option_idx].short_option;
Jim Ingham767af882010-07-07 03:36:20 +000061 switch (short_option)
Chris Lattner24943d22010-06-08 16:52:24 +000062 {
Jim Ingham767af882010-07-07 03:36:20 +000063 case 'l':
64 start_line = Args::StringToUInt32 (option_arg, 0);
65 if (start_line == 0)
Greg Clayton9c236732011-10-26 00:56:27 +000066 error.SetErrorStringWithFormat("invalid line number: '%s'", option_arg);
Jim Ingham767af882010-07-07 03:36:20 +000067 break;
Chris Lattner24943d22010-06-08 16:52:24 +000068
Jim Ingham767af882010-07-07 03:36:20 +000069 case 'f':
70 file_name = option_arg;
71 break;
72
73 default:
Greg Clayton9c236732011-10-26 00:56:27 +000074 error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
Jim Ingham767af882010-07-07 03:36:20 +000075 break;
Chris Lattner24943d22010-06-08 16:52:24 +000076 }
77
Jim Ingham767af882010-07-07 03:36:20 +000078 return error;
79 }
Chris Lattner24943d22010-06-08 16:52:24 +000080
Jim Ingham767af882010-07-07 03:36:20 +000081 void
Greg Clayton143fcc32011-04-13 00:18:08 +000082 OptionParsingStarting ()
Jim Ingham767af882010-07-07 03:36:20 +000083 {
Jim Ingham767af882010-07-07 03:36:20 +000084 file_spec.Clear();
85 file_name.clear();
86 start_line = 0;
87 }
88
Greg Claytonb3448432011-03-24 21:19:54 +000089 const OptionDefinition*
Jim Ingham767af882010-07-07 03:36:20 +000090 GetDefinitions ()
91 {
92 return g_option_table;
93 }
Greg Claytonb3448432011-03-24 21:19:54 +000094 static OptionDefinition g_option_table[];
Jim Ingham767af882010-07-07 03:36:20 +000095
96 // Instance variables to hold the values for command options.
97 FileSpec file_spec;
98 std::string file_name;
99 uint32_t start_line;
100
101 };
102
103public:
Greg Clayton238c0a12010-09-18 01:14:36 +0000104 CommandObjectSourceInfo(CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000105 CommandObjectParsed (interpreter,
106 "source info",
107 "Display information about the source lines from the current executable's debug info.",
108 "source info [<cmd-options>]"),
Greg Claytonf15996e2011-04-07 22:46:35 +0000109 m_options (interpreter)
Jim Ingham767af882010-07-07 03:36:20 +0000110 {
111 }
112
113 ~CommandObjectSourceInfo ()
114 {
115 }
116
117
118 Options *
119 GetOptions ()
120 {
121 return &m_options;
122 }
123
Jim Inghamda26bd22012-06-08 21:56:10 +0000124protected:
Jim Ingham767af882010-07-07 03:36:20 +0000125 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000126 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham767af882010-07-07 03:36:20 +0000127 {
128 result.AppendError ("Not yet implemented");
129 result.SetStatus (eReturnStatusFailed);
130 return false;
131 }
Jim Inghamda26bd22012-06-08 21:56:10 +0000132
Jim Ingham767af882010-07-07 03:36:20 +0000133 CommandOptions m_options;
134};
135
Greg Claytonb3448432011-03-24 21:19:54 +0000136OptionDefinition
Jim Ingham767af882010-07-07 03:36:20 +0000137CommandObjectSourceInfo::CommandOptions::g_option_table[] =
138{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000139{ LLDB_OPT_SET_1, false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "The line number at which to start the display source."},
140{ LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "The file from which to display source."},
141{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Jim Ingham767af882010-07-07 03:36:20 +0000142};
143
144#pragma mark CommandObjectSourceList
145//-------------------------------------------------------------------------
146// CommandObjectSourceList
147//-------------------------------------------------------------------------
148
Jim Inghamda26bd22012-06-08 21:56:10 +0000149class CommandObjectSourceList : public CommandObjectParsed
Jim Ingham767af882010-07-07 03:36:20 +0000150{
151
152 class CommandOptions : public Options
153 {
154 public:
Greg Claytonf15996e2011-04-07 22:46:35 +0000155 CommandOptions (CommandInterpreter &interpreter) :
156 Options(interpreter)
Jim Ingham767af882010-07-07 03:36:20 +0000157 {
158 }
159
160 ~CommandOptions ()
161 {
162 }
163
164 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000165 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Ingham767af882010-07-07 03:36:20 +0000166 {
167 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +0000168 const int short_option = g_option_table[option_idx].short_option;
Jim Ingham767af882010-07-07 03:36:20 +0000169 switch (short_option)
170 {
171 case 'l':
172 start_line = Args::StringToUInt32 (option_arg, 0);
173 if (start_line == 0)
Greg Clayton9c236732011-10-26 00:56:27 +0000174 error.SetErrorStringWithFormat("invalid line number: '%s'", option_arg);
Jim Ingham767af882010-07-07 03:36:20 +0000175 break;
176
Jim Ingham338f7532010-08-20 01:17:07 +0000177 case 'c':
Jim Ingham767af882010-07-07 03:36:20 +0000178 num_lines = Args::StringToUInt32 (option_arg, 0);
179 if (num_lines == 0)
Greg Clayton9c236732011-10-26 00:56:27 +0000180 error.SetErrorStringWithFormat("invalid line count: '%s'", option_arg);
Jim Ingham767af882010-07-07 03:36:20 +0000181 break;
182
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000183 case 'f':
Jim Ingham767af882010-07-07 03:36:20 +0000184 file_name = option_arg;
185 break;
Jim Ingham338f7532010-08-20 01:17:07 +0000186
187 case 'n':
188 symbol_name = option_arg;
189 break;
Jim Ingham767af882010-07-07 03:36:20 +0000190
Greg Clayton78124112012-12-07 00:19:47 +0000191 case 'a':
192 {
193 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
194 address = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
195 }
196 break;
Jim Ingham338f7532010-08-20 01:17:07 +0000197 case 's':
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000198 modules.push_back (std::string (option_arg));
199 break;
200
201 case 'b':
202 show_bp_locs = true;
Jim Ingham338f7532010-08-20 01:17:07 +0000203 break;
Jim Ingham12b5aa02013-01-09 03:27:33 +0000204 case 'r':
205 reverse = true;
206 break;
Jim Ingham767af882010-07-07 03:36:20 +0000207 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000208 error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
Jim Ingham767af882010-07-07 03:36:20 +0000209 break;
210 }
211
212 return error;
213 }
214
215 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000216 OptionParsingStarting ()
Jim Ingham767af882010-07-07 03:36:20 +0000217 {
Jim Ingham767af882010-07-07 03:36:20 +0000218 file_spec.Clear();
219 file_name.clear();
Jim Ingham338f7532010-08-20 01:17:07 +0000220 symbol_name.clear();
Greg Clayton78124112012-12-07 00:19:47 +0000221 address = LLDB_INVALID_ADDRESS;
Jim Ingham767af882010-07-07 03:36:20 +0000222 start_line = 0;
223 num_lines = 10;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000224 show_bp_locs = false;
Jim Ingham12b5aa02013-01-09 03:27:33 +0000225 reverse = false;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000226 modules.clear();
Jim Ingham767af882010-07-07 03:36:20 +0000227 }
228
Greg Claytonb3448432011-03-24 21:19:54 +0000229 const OptionDefinition*
Jim Ingham767af882010-07-07 03:36:20 +0000230 GetDefinitions ()
231 {
232 return g_option_table;
233 }
Greg Claytonb3448432011-03-24 21:19:54 +0000234 static OptionDefinition g_option_table[];
Jim Ingham767af882010-07-07 03:36:20 +0000235
236 // Instance variables to hold the values for command options.
237 FileSpec file_spec;
238 std::string file_name;
Jim Ingham338f7532010-08-20 01:17:07 +0000239 std::string symbol_name;
Greg Clayton78124112012-12-07 00:19:47 +0000240 lldb::addr_t address;
Jim Ingham767af882010-07-07 03:36:20 +0000241 uint32_t start_line;
242 uint32_t num_lines;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000243 STLStringArray modules;
244 bool show_bp_locs;
Jim Ingham12b5aa02013-01-09 03:27:33 +0000245 bool reverse;
Jim Ingham767af882010-07-07 03:36:20 +0000246 };
247
248public:
Greg Clayton238c0a12010-09-18 01:14:36 +0000249 CommandObjectSourceList(CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000250 CommandObjectParsed (interpreter,
251 "source list",
252 "Display source code (as specified) based on the current executable's debug info.",
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000253 NULL,
254 eFlagRequiresTarget),
Greg Claytonf15996e2011-04-07 22:46:35 +0000255 m_options (interpreter)
Jim Ingham767af882010-07-07 03:36:20 +0000256 {
257 }
258
259 ~CommandObjectSourceList ()
260 {
261 }
262
263
264 Options *
265 GetOptions ()
266 {
267 return &m_options;
268 }
269
Jim Inghamda26bd22012-06-08 21:56:10 +0000270 virtual const char *
271 GetRepeatCommand (Args &current_command_args, uint32_t index)
Jim Ingham767af882010-07-07 03:36:20 +0000272 {
Jim Ingham12b5aa02013-01-09 03:27:33 +0000273 // This is kind of gross, but the command hasn't been parsed yet so we can't look at the option
274 // values for this invocation... I have to scan the arguments directly.
275 size_t num_args = current_command_args.GetArgumentCount();
276 bool is_reverse = false;
277 for (size_t i = 0 ; i < num_args; i++)
278 {
279 const char *arg = current_command_args.GetArgumentAtIndex(i);
280 if (arg && (strcmp(arg, "-r") == 0 || strcmp(arg, "--reverse") == 0))
281 {
282 is_reverse = true;
283 }
284 }
285 if (is_reverse)
286 {
287 if (m_reverse_name.empty())
288 {
289 m_reverse_name = m_cmd_name;
290 m_reverse_name.append (" -r");
291 }
292 return m_reverse_name.c_str();
293 }
294 else
295 return m_cmd_name.c_str();
Jim Inghamda26bd22012-06-08 21:56:10 +0000296 }
297
298protected:
299 bool
300 DoExecute (Args& command, CommandReturnObject &result)
301 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000302 const size_t argc = command.GetArgumentCount();
Jim Ingham767af882010-07-07 03:36:20 +0000303
304 if (argc != 0)
305 {
306 result.AppendErrorWithFormat("'%s' takes no arguments, only flags.\n", GetCommandName());
307 result.SetStatus (eReturnStatusFailed);
Jim Ingham26e089b2011-11-29 21:21:26 +0000308 return false;
Jim Ingham767af882010-07-07 03:36:20 +0000309 }
310
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000311 Target *target = m_exe_ctx.GetTargetPtr();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000312
Greg Clayton78124112012-12-07 00:19:47 +0000313 SymbolContextList sc_list;
Jim Ingham338f7532010-08-20 01:17:07 +0000314 if (!m_options.symbol_name.empty())
315 {
316 // Displaying the source for a symbol:
Jim Ingham338f7532010-08-20 01:17:07 +0000317 ConstString name(m_options.symbol_name.c_str());
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000318 bool include_symbols = false;
Sean Callanan302d78c2012-02-10 22:52:19 +0000319 bool include_inlines = true;
Jim Ingham338f7532010-08-20 01:17:07 +0000320 bool append = true;
321 size_t num_matches = 0;
322
Greg Clayton36da2aa2013-01-25 18:06:21 +0000323 const size_t num_modules = m_options.modules.size();
324 if (num_modules > 0)
Jim Ingham338f7532010-08-20 01:17:07 +0000325 {
326 ModuleList matching_modules;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000327 for (size_t i = 0; i < num_modules; ++i)
Jim Ingham338f7532010-08-20 01:17:07 +0000328 {
Greg Clayton444fe992012-02-26 05:51:37 +0000329 FileSpec module_file_spec(m_options.modules[i].c_str(), false);
330 if (module_file_spec)
Jim Ingham338f7532010-08-20 01:17:07 +0000331 {
Greg Clayton444fe992012-02-26 05:51:37 +0000332 ModuleSpec module_spec (module_file_spec);
Jim Ingham338f7532010-08-20 01:17:07 +0000333 matching_modules.Clear();
Greg Clayton444fe992012-02-26 05:51:37 +0000334 target->GetImages().FindModules (module_spec, matching_modules);
Sean Callanan302d78c2012-02-10 22:52:19 +0000335 num_matches += matching_modules.FindFunctions (name, eFunctionNameTypeAuto, include_symbols, include_inlines, append, sc_list);
Jim Ingham338f7532010-08-20 01:17:07 +0000336 }
337 }
338 }
339 else
340 {
Sean Callanan302d78c2012-02-10 22:52:19 +0000341 num_matches = target->GetImages().FindFunctions (name, eFunctionNameTypeAuto, include_symbols, include_inlines, append, sc_list);
Jim Ingham338f7532010-08-20 01:17:07 +0000342 }
343
344 SymbolContext sc;
345
346 if (num_matches == 0)
347 {
348 result.AppendErrorWithFormat("Could not find function named: \"%s\".\n", m_options.symbol_name.c_str());
349 result.SetStatus (eReturnStatusFailed);
350 return false;
351 }
352
353 sc_list.GetContextAtIndex (0, sc);
354 FileSpec start_file;
355 uint32_t start_line;
356 uint32_t end_line;
357 FileSpec end_file;
358 if (sc.function != NULL)
359 {
360 sc.function->GetStartLineSourceInfo (start_file, start_line);
361 if (start_line == 0)
362 {
363 result.AppendErrorWithFormat("Could not find line information for start of function: \"%s\".\n", m_options.symbol_name.c_str());
364 result.SetStatus (eReturnStatusFailed);
365 return false;
366 }
367 sc.function->GetEndLineSourceInfo (end_file, end_line);
368 }
369 else
370 {
371 result.AppendErrorWithFormat("Could not find function info for: \"%s\".\n", m_options.symbol_name.c_str());
372 result.SetStatus (eReturnStatusFailed);
373 return false;
374 }
375
376 if (num_matches > 1)
377 {
378 // This could either be because there are multiple functions of this name, in which case
379 // we'll have to specify this further... Or it could be because there are multiple inlined instances
380 // of one function. So run through the matches and if they all have the same file & line then we can just
381 // list one.
382
383 bool found_multiple = false;
384
385 for (size_t i = 1; i < num_matches; i++)
386 {
387 SymbolContext scratch_sc;
388 sc_list.GetContextAtIndex (i, scratch_sc);
389 if (scratch_sc.function != NULL)
390 {
391 FileSpec scratch_file;
392 uint32_t scratch_line;
393 scratch_sc.function->GetStartLineSourceInfo (scratch_file, scratch_line);
394 if (scratch_file != start_file
395 || scratch_line != start_line)
396 {
397 found_multiple = true;
398 break;
399 }
400 }
401 }
402 if (found_multiple)
403 {
404 StreamString s;
405 for (size_t i = 0; i < num_matches; i++)
406 {
407 SymbolContext scratch_sc;
408 sc_list.GetContextAtIndex (i, scratch_sc);
409 if (scratch_sc.function != NULL)
410 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000411 s.Printf("\n%lu: ", i);
Jim Ingham338f7532010-08-20 01:17:07 +0000412 scratch_sc.function->Dump (&s, true);
413 }
414 }
415 result.AppendErrorWithFormat("Multiple functions found matching: %s: \n%s\n",
416 m_options.symbol_name.c_str(),
417 s.GetData());
418 result.SetStatus (eReturnStatusFailed);
419 return false;
420 }
421 }
422
423
424 // This is a little hacky, but the first line table entry for a function points to the "{" that
425 // starts the function block. It would be nice to actually get the function
426 // declaration in there too. So back up a bit, but not further than what you're going to display.
Greg Clayton36da2aa2013-01-25 18:06:21 +0000427 uint32_t lines_to_back_up = m_options.num_lines >= 10 ? 5 : m_options.num_lines/2;
Jim Ingham338f7532010-08-20 01:17:07 +0000428 uint32_t line_no;
429 if (start_line <= lines_to_back_up)
430 line_no = 1;
431 else
432 line_no = start_line - lines_to_back_up;
433
434 // For fun, if the function is shorter than the number of lines we're supposed to display,
435 // only display the function...
436 if (end_line != 0)
437 {
438 if (m_options.num_lines > end_line - line_no)
439 m_options.num_lines = end_line - line_no;
440 }
441
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000442 char path_buf[PATH_MAX];
443 start_file.GetPath(path_buf, sizeof(path_buf));
Greg Clayton1cee1e62011-04-20 18:52:45 +0000444
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000445 if (m_options.show_bp_locs)
Greg Clayton1cee1e62011-04-20 18:52:45 +0000446 {
447 const bool show_inlines = true;
448 m_breakpoint_locations.Reset (start_file, 0, show_inlines);
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000449 SearchFilter target_search_filter (m_exe_ctx.GetTargetSP());
Greg Clayton1cee1e62011-04-20 18:52:45 +0000450 target_search_filter.Search (m_breakpoint_locations);
451 }
452 else
453 m_breakpoint_locations.Clear();
454
Jim Ingham338f7532010-08-20 01:17:07 +0000455 result.AppendMessageWithFormat("File: %s.\n", path_buf);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000456 target->GetSourceManager().DisplaySourceLinesWithLineNumbers (start_file,
457 line_no,
458 0,
459 m_options.num_lines,
460 "",
461 &result.GetOutputStream(),
462 GetBreakpointLocations ());
Jim Ingham338f7532010-08-20 01:17:07 +0000463
464 result.SetStatus (eReturnStatusSuccessFinishResult);
465 return true;
466
467 }
Greg Clayton78124112012-12-07 00:19:47 +0000468 else if (m_options.address != LLDB_INVALID_ADDRESS)
469 {
470 SymbolContext sc;
471 Address so_addr;
472 StreamString error_strm;
473
474 if (target->GetSectionLoadList().IsEmpty())
475 {
476 // The target isn't loaded yet, we need to lookup the file address
477 // in all modules
478 const ModuleList &module_list = target->GetImages();
Greg Clayton36da2aa2013-01-25 18:06:21 +0000479 const size_t num_modules = module_list.GetSize();
480 for (size_t i=0; i<num_modules; ++i)
Greg Clayton78124112012-12-07 00:19:47 +0000481 {
482 ModuleSP module_sp (module_list.GetModuleAtIndex(i));
483 if (module_sp && module_sp->ResolveFileAddress(m_options.address, so_addr))
484 {
485 sc.Clear();
486 if (module_sp->ResolveSymbolContextForAddress (so_addr, eSymbolContextEverything, sc) & eSymbolContextLineEntry)
487 sc_list.Append(sc);
488 }
489 }
490
491 if (sc_list.GetSize() == 0)
492 {
493 result.AppendErrorWithFormat("no modules have source information for file address 0x%" PRIx64 ".\n",
494 m_options.address);
495 result.SetStatus (eReturnStatusFailed);
496 return false;
497 }
498 }
499 else
500 {
501 // The target has some things loaded, resolve this address to a
502 // compile unit + file + line and display
503 if (target->GetSectionLoadList().ResolveLoadAddress (m_options.address, so_addr))
504 {
505 ModuleSP module_sp (so_addr.GetModule());
506 if (module_sp)
507 {
508 sc.Clear();
509 if (module_sp->ResolveSymbolContextForAddress (so_addr, eSymbolContextEverything, sc) & eSymbolContextLineEntry)
510 {
511 sc_list.Append(sc);
512 }
513 else
514 {
515 so_addr.Dump(&error_strm, NULL, Address::DumpStyleModuleWithFileAddress);
516 result.AppendErrorWithFormat("address resolves to %s, but there is no line table information available for this address.\n",
517 error_strm.GetData());
518 result.SetStatus (eReturnStatusFailed);
519 return false;
520 }
521 }
522 }
523
524 if (sc_list.GetSize() == 0)
525 {
526 result.AppendErrorWithFormat("no modules contain load address 0x%" PRIx64 ".\n", m_options.address);
527 result.SetStatus (eReturnStatusFailed);
528 return false;
529 }
530 }
531 uint32_t num_matches = sc_list.GetSize();
532 for (uint32_t i=0; i<num_matches; ++i)
533 {
534 sc_list.GetContextAtIndex(i, sc);
535 if (sc.comp_unit)
536 {
537 if (m_options.show_bp_locs)
538 {
539 m_breakpoint_locations.Clear();
540 const bool show_inlines = true;
541 m_breakpoint_locations.Reset (*sc.comp_unit, 0, show_inlines);
542 SearchFilter target_search_filter (target->shared_from_this());
543 target_search_filter.Search (m_breakpoint_locations);
544 }
545
546 bool show_fullpaths = true;
547 bool show_module = true;
548 bool show_inlined_frames = true;
549 sc.DumpStopContext(&result.GetOutputStream(),
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000550 m_exe_ctx.GetBestExecutionContextScope(),
Greg Clayton78124112012-12-07 00:19:47 +0000551 sc.line_entry.range.GetBaseAddress(),
552 show_fullpaths,
553 show_module,
554 show_inlined_frames);
555 result.GetOutputStream().EOL();
556
557 size_t lines_to_back_up = m_options.num_lines >= 10 ? 5 : m_options.num_lines/2;
558
559 target->GetSourceManager().DisplaySourceLinesWithLineNumbers (sc.comp_unit,
560 sc.line_entry.line,
561 lines_to_back_up,
562 m_options.num_lines - lines_to_back_up,
563 "->",
564 &result.GetOutputStream(),
565 GetBreakpointLocations ());
566 result.SetStatus (eReturnStatusSuccessFinishResult);
567 }
568 }
569 }
Jim Ingham338f7532010-08-20 01:17:07 +0000570 else if (m_options.file_name.empty())
Jim Ingham767af882010-07-07 03:36:20 +0000571 {
572 // Last valid source manager context, or the current frame if no
573 // valid last context in source manager.
574 // One little trick here, if you type the exact same list command twice in a row, it is
575 // more likely because you typed it once, then typed it again
576 if (m_options.start_line == 0)
577 {
Jim Inghamc7f18c82011-09-29 20:22:33 +0000578 if (target->GetSourceManager().DisplayMoreWithLineNumbers (&result.GetOutputStream(),
Jim Ingham12b5aa02013-01-09 03:27:33 +0000579 GetBreakpointLocations (),
580 m_options.reverse))
Chris Lattner24943d22010-06-08 16:52:24 +0000581 {
Chris Lattner24943d22010-06-08 16:52:24 +0000582 result.SetStatus (eReturnStatusSuccessFinishResult);
583 }
Jim Ingham767af882010-07-07 03:36:20 +0000584 }
585 else
586 {
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000587 if (m_options.show_bp_locs)
Greg Clayton1cee1e62011-04-20 18:52:45 +0000588 {
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000589 SourceManager::FileSP last_file_sp (target->GetSourceManager().GetLastFile ());
Greg Clayton1cee1e62011-04-20 18:52:45 +0000590 if (last_file_sp)
591 {
592 const bool show_inlines = true;
593 m_breakpoint_locations.Reset (last_file_sp->GetFileSpec(), 0, show_inlines);
Greg Clayton13d24fb2012-01-29 20:56:30 +0000594 SearchFilter target_search_filter (target->shared_from_this());
Greg Clayton1cee1e62011-04-20 18:52:45 +0000595 target_search_filter.Search (m_breakpoint_locations);
596 }
597 }
598 else
599 m_breakpoint_locations.Clear();
600
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000601 if (target->GetSourceManager().DisplaySourceLinesWithLineNumbersUsingLastFile(
Jim Ingham767af882010-07-07 03:36:20 +0000602 m_options.start_line, // Line to display
603 0, // Lines before line to display
604 m_options.num_lines, // Lines after line to display
605 "", // Don't mark "line"
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000606 &result.GetOutputStream(),
607 GetBreakpointLocations ()))
Chris Lattner24943d22010-06-08 16:52:24 +0000608 {
Jim Ingham767af882010-07-07 03:36:20 +0000609 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000610 }
Jim Ingham767af882010-07-07 03:36:20 +0000611
Chris Lattner24943d22010-06-08 16:52:24 +0000612 }
613 }
614 else
615 {
Jim Ingham767af882010-07-07 03:36:20 +0000616 const char *filename = m_options.file_name.c_str();
Jim Ingham767af882010-07-07 03:36:20 +0000617
618 bool check_inlines = false;
619 SymbolContextList sc_list;
Jim Ingham338f7532010-08-20 01:17:07 +0000620 size_t num_matches = 0;
621
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000622 if (m_options.modules.size() > 0)
Jim Ingham767af882010-07-07 03:36:20 +0000623 {
Jim Ingham338f7532010-08-20 01:17:07 +0000624 ModuleList matching_modules;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000625 for (size_t i = 0, e = m_options.modules.size(); i < e; ++i)
Jim Ingham767af882010-07-07 03:36:20 +0000626 {
Greg Clayton444fe992012-02-26 05:51:37 +0000627 FileSpec module_file_spec(m_options.modules[i].c_str(), false);
628 if (module_file_spec)
Jim Ingham767af882010-07-07 03:36:20 +0000629 {
Greg Clayton444fe992012-02-26 05:51:37 +0000630 ModuleSpec module_spec (module_file_spec);
Jim Ingham338f7532010-08-20 01:17:07 +0000631 matching_modules.Clear();
Greg Clayton444fe992012-02-26 05:51:37 +0000632 target->GetImages().FindModules (module_spec, matching_modules);
Jim Ingham338f7532010-08-20 01:17:07 +0000633 num_matches += matching_modules.ResolveSymbolContextForFilePath (filename,
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000634 0,
635 check_inlines,
636 eSymbolContextModule | eSymbolContextCompUnit,
637 sc_list);
Jim Ingham767af882010-07-07 03:36:20 +0000638 }
639 }
640 }
Jim Ingham338f7532010-08-20 01:17:07 +0000641 else
642 {
643 num_matches = target->GetImages().ResolveSymbolContextForFilePath (filename,
644 0,
645 check_inlines,
646 eSymbolContextModule | eSymbolContextCompUnit,
647 sc_list);
648 }
649
650 if (num_matches == 0)
651 {
652 result.AppendErrorWithFormat("Could not find source file \"%s\".\n",
653 m_options.file_name.c_str());
654 result.SetStatus (eReturnStatusFailed);
655 return false;
656 }
657
658 if (num_matches > 1)
659 {
660 SymbolContext sc;
661 bool got_multiple = false;
662 FileSpec *test_cu_spec = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000663
Chris Lattner0f6fa732010-09-08 22:55:31 +0000664 for (unsigned i = 0; i < num_matches; i++)
Jim Ingham338f7532010-08-20 01:17:07 +0000665 {
666 sc_list.GetContextAtIndex(i, sc);
667 if (sc.comp_unit)
668 {
669 if (test_cu_spec)
670 {
671 if (test_cu_spec != static_cast<FileSpec *> (sc.comp_unit))
672 got_multiple = true;
673 break;
674 }
675 else
676 test_cu_spec = sc.comp_unit;
677 }
678 }
679 if (got_multiple)
680 {
681 result.AppendErrorWithFormat("Multiple source files found matching: \"%s.\"\n",
682 m_options.file_name.c_str());
683 result.SetStatus (eReturnStatusFailed);
684 return false;
685 }
686 }
687
688 SymbolContext sc;
689 if (sc_list.GetContextAtIndex(0, sc))
690 {
691 if (sc.comp_unit)
692 {
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000693 if (m_options.show_bp_locs)
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000694 {
695 const bool show_inlines = true;
696 m_breakpoint_locations.Reset (*sc.comp_unit, 0, show_inlines);
Greg Clayton13d24fb2012-01-29 20:56:30 +0000697 SearchFilter target_search_filter (target->shared_from_this());
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000698 target_search_filter.Search (m_breakpoint_locations);
699 }
700 else
701 m_breakpoint_locations.Clear();
702
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000703 target->GetSourceManager().DisplaySourceLinesWithLineNumbers (sc.comp_unit,
704 m_options.start_line,
705 0,
706 m_options.num_lines,
707 "",
708 &result.GetOutputStream(),
709 GetBreakpointLocations ());
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000710
Jim Ingham338f7532010-08-20 01:17:07 +0000711 result.SetStatus (eReturnStatusSuccessFinishResult);
712 }
713 else
714 {
715 result.AppendErrorWithFormat("No comp unit found for: \"%s.\"\n",
716 m_options.file_name.c_str());
717 result.SetStatus (eReturnStatusFailed);
718 return false;
719 }
720 }
721 }
Jim Ingham767af882010-07-07 03:36:20 +0000722 return result.Succeeded();
Chris Lattner24943d22010-06-08 16:52:24 +0000723 }
Jim Ingham767af882010-07-07 03:36:20 +0000724
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000725 const SymbolContextList *
726 GetBreakpointLocations ()
727 {
728 if (m_breakpoint_locations.GetFileLineMatches().GetSize() > 0)
729 return &m_breakpoint_locations.GetFileLineMatches();
730 return NULL;
731 }
Jim Ingham767af882010-07-07 03:36:20 +0000732 CommandOptions m_options;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000733 FileLineResolver m_breakpoint_locations;
Jim Ingham12b5aa02013-01-09 03:27:33 +0000734 std::string m_reverse_name;
Jim Ingham767af882010-07-07 03:36:20 +0000735
736};
737
Greg Claytonb3448432011-03-24 21:19:54 +0000738OptionDefinition
Jim Ingham767af882010-07-07 03:36:20 +0000739CommandObjectSourceList::CommandOptions::g_option_table[] =
740{
Greg Clayton78124112012-12-07 00:19:47 +0000741{ LLDB_OPT_SET_ALL, false, "count", 'c', required_argument, NULL, 0, eArgTypeCount, "The number of source lines to display."},
742{ LLDB_OPT_SET_1 |
743 LLDB_OPT_SET_2 , 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 +0000744{ 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."},
Greg Clayton78124112012-12-07 00:19:47 +0000745{ LLDB_OPT_SET_1 , false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "The file from which to display source."},
746{ LLDB_OPT_SET_1 , false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "The line number at which to start the display source."},
747{ LLDB_OPT_SET_2 , false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeSymbol, "The name of a function whose source to display."},
Enrico Granata4968ad82013-01-29 01:48:30 +0000748{ LLDB_OPT_SET_3 , false, "address",'a', required_argument, NULL, 0, eArgTypeAddressOrExpression, "Lookup the address and display the source information for the corresponding file and line."},
Jim Ingham12b5aa02013-01-09 03:27:33 +0000749{ LLDB_OPT_SET_4, false, "reverse", 'r', no_argument, NULL, 0, eArgTypeNone, "Reverse the listing to look backwards from the last displayed block of source."},
Caroline Tice4d6675c2010-10-01 19:59:14 +0000750{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Jim Ingham767af882010-07-07 03:36:20 +0000751};
752
753#pragma mark CommandObjectMultiwordSource
754
755//-------------------------------------------------------------------------
756// CommandObjectMultiwordSource
757//-------------------------------------------------------------------------
758
759CommandObjectMultiwordSource::CommandObjectMultiwordSource (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +0000760 CommandObjectMultiword (interpreter,
761 "source",
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000762 "A set of commands for accessing source file information",
Jim Ingham767af882010-07-07 03:36:20 +0000763 "source <subcommand> [<subcommand-options>]")
764{
Greg Clayton238c0a12010-09-18 01:14:36 +0000765 LoadSubCommand ("info", CommandObjectSP (new CommandObjectSourceInfo (interpreter)));
766 LoadSubCommand ("list", CommandObjectSP (new CommandObjectSourceList (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +0000767}
Jim Ingham767af882010-07-07 03:36:20 +0000768
769CommandObjectMultiwordSource::~CommandObjectMultiwordSource ()
770{
771}
772