blob: 45538676f2074bf29313d470eac1cd116fedc414 [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;
Greg Claytonb1f44b32013-03-13 18:25:49 +0000223 num_lines = 0;
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 Claytonb1f44b32013-03-13 18:25:49 +0000323 if (m_options.num_lines == 0)
324 m_options.num_lines = 10;
325
Greg Clayton36da2aa2013-01-25 18:06:21 +0000326 const size_t num_modules = m_options.modules.size();
327 if (num_modules > 0)
Jim Ingham338f7532010-08-20 01:17:07 +0000328 {
329 ModuleList matching_modules;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000330 for (size_t i = 0; i < num_modules; ++i)
Jim Ingham338f7532010-08-20 01:17:07 +0000331 {
Greg Clayton444fe992012-02-26 05:51:37 +0000332 FileSpec module_file_spec(m_options.modules[i].c_str(), false);
333 if (module_file_spec)
Jim Ingham338f7532010-08-20 01:17:07 +0000334 {
Greg Clayton444fe992012-02-26 05:51:37 +0000335 ModuleSpec module_spec (module_file_spec);
Jim Ingham338f7532010-08-20 01:17:07 +0000336 matching_modules.Clear();
Greg Clayton444fe992012-02-26 05:51:37 +0000337 target->GetImages().FindModules (module_spec, matching_modules);
Sean Callanan302d78c2012-02-10 22:52:19 +0000338 num_matches += matching_modules.FindFunctions (name, eFunctionNameTypeAuto, include_symbols, include_inlines, append, sc_list);
Jim Ingham338f7532010-08-20 01:17:07 +0000339 }
340 }
341 }
342 else
343 {
Sean Callanan302d78c2012-02-10 22:52:19 +0000344 num_matches = target->GetImages().FindFunctions (name, eFunctionNameTypeAuto, include_symbols, include_inlines, append, sc_list);
Jim Ingham338f7532010-08-20 01:17:07 +0000345 }
346
347 SymbolContext sc;
348
349 if (num_matches == 0)
350 {
351 result.AppendErrorWithFormat("Could not find function named: \"%s\".\n", m_options.symbol_name.c_str());
352 result.SetStatus (eReturnStatusFailed);
353 return false;
354 }
355
356 sc_list.GetContextAtIndex (0, sc);
357 FileSpec start_file;
358 uint32_t start_line;
359 uint32_t end_line;
360 FileSpec end_file;
361 if (sc.function != NULL)
362 {
363 sc.function->GetStartLineSourceInfo (start_file, start_line);
364 if (start_line == 0)
365 {
366 result.AppendErrorWithFormat("Could not find line information for start of function: \"%s\".\n", m_options.symbol_name.c_str());
367 result.SetStatus (eReturnStatusFailed);
368 return false;
369 }
370 sc.function->GetEndLineSourceInfo (end_file, end_line);
371 }
372 else
373 {
374 result.AppendErrorWithFormat("Could not find function info for: \"%s\".\n", m_options.symbol_name.c_str());
375 result.SetStatus (eReturnStatusFailed);
376 return false;
377 }
378
379 if (num_matches > 1)
380 {
381 // This could either be because there are multiple functions of this name, in which case
382 // we'll have to specify this further... Or it could be because there are multiple inlined instances
383 // of one function. So run through the matches and if they all have the same file & line then we can just
384 // list one.
385
386 bool found_multiple = false;
387
388 for (size_t i = 1; i < num_matches; i++)
389 {
390 SymbolContext scratch_sc;
391 sc_list.GetContextAtIndex (i, scratch_sc);
392 if (scratch_sc.function != NULL)
393 {
394 FileSpec scratch_file;
395 uint32_t scratch_line;
396 scratch_sc.function->GetStartLineSourceInfo (scratch_file, scratch_line);
397 if (scratch_file != start_file
398 || scratch_line != start_line)
399 {
400 found_multiple = true;
401 break;
402 }
403 }
404 }
405 if (found_multiple)
406 {
407 StreamString s;
408 for (size_t i = 0; i < num_matches; i++)
409 {
410 SymbolContext scratch_sc;
411 sc_list.GetContextAtIndex (i, scratch_sc);
412 if (scratch_sc.function != NULL)
413 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000414 s.Printf("\n%lu: ", i);
Jim Ingham338f7532010-08-20 01:17:07 +0000415 scratch_sc.function->Dump (&s, true);
416 }
417 }
418 result.AppendErrorWithFormat("Multiple functions found matching: %s: \n%s\n",
419 m_options.symbol_name.c_str(),
420 s.GetData());
421 result.SetStatus (eReturnStatusFailed);
422 return false;
423 }
424 }
425
Jim Ingham338f7532010-08-20 01:17:07 +0000426 // This is a little hacky, but the first line table entry for a function points to the "{" that
427 // starts the function block. It would be nice to actually get the function
428 // declaration in there too. So back up a bit, but not further than what you're going to display.
Greg Claytonb1f44b32013-03-13 18:25:49 +0000429 uint32_t extra_lines;
430 if (m_options.num_lines >= 10)
431 extra_lines = 5;
432 else
433 extra_lines = m_options.num_lines/2;
Jim Ingham338f7532010-08-20 01:17:07 +0000434 uint32_t line_no;
Greg Claytonb1f44b32013-03-13 18:25:49 +0000435 if (start_line <= extra_lines)
Jim Ingham338f7532010-08-20 01:17:07 +0000436 line_no = 1;
437 else
Greg Claytonb1f44b32013-03-13 18:25:49 +0000438 line_no = start_line - extra_lines;
Jim Ingham338f7532010-08-20 01:17:07 +0000439
440 // For fun, if the function is shorter than the number of lines we're supposed to display,
441 // only display the function...
442 if (end_line != 0)
443 {
444 if (m_options.num_lines > end_line - line_no)
Greg Claytonb1f44b32013-03-13 18:25:49 +0000445 m_options.num_lines = end_line - line_no + extra_lines;
Jim Ingham338f7532010-08-20 01:17:07 +0000446 }
447
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000448 char path_buf[PATH_MAX];
449 start_file.GetPath(path_buf, sizeof(path_buf));
Greg Clayton1cee1e62011-04-20 18:52:45 +0000450
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000451 if (m_options.show_bp_locs)
Greg Clayton1cee1e62011-04-20 18:52:45 +0000452 {
453 const bool show_inlines = true;
454 m_breakpoint_locations.Reset (start_file, 0, show_inlines);
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000455 SearchFilter target_search_filter (m_exe_ctx.GetTargetSP());
Greg Clayton1cee1e62011-04-20 18:52:45 +0000456 target_search_filter.Search (m_breakpoint_locations);
457 }
458 else
459 m_breakpoint_locations.Clear();
460
Greg Claytonb1f44b32013-03-13 18:25:49 +0000461 result.AppendMessageWithFormat("File: %s\n", path_buf);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000462 target->GetSourceManager().DisplaySourceLinesWithLineNumbers (start_file,
463 line_no,
464 0,
465 m_options.num_lines,
466 "",
467 &result.GetOutputStream(),
468 GetBreakpointLocations ());
Jim Ingham338f7532010-08-20 01:17:07 +0000469
470 result.SetStatus (eReturnStatusSuccessFinishResult);
471 return true;
472
473 }
Greg Clayton78124112012-12-07 00:19:47 +0000474 else if (m_options.address != LLDB_INVALID_ADDRESS)
475 {
476 SymbolContext sc;
477 Address so_addr;
478 StreamString error_strm;
479
480 if (target->GetSectionLoadList().IsEmpty())
481 {
482 // The target isn't loaded yet, we need to lookup the file address
483 // in all modules
484 const ModuleList &module_list = target->GetImages();
Greg Clayton36da2aa2013-01-25 18:06:21 +0000485 const size_t num_modules = module_list.GetSize();
486 for (size_t i=0; i<num_modules; ++i)
Greg Clayton78124112012-12-07 00:19:47 +0000487 {
488 ModuleSP module_sp (module_list.GetModuleAtIndex(i));
489 if (module_sp && module_sp->ResolveFileAddress(m_options.address, so_addr))
490 {
Greg Claytona7e864c2013-02-23 04:12:47 +0000491 sc.Clear(true);
Greg Clayton78124112012-12-07 00:19:47 +0000492 if (module_sp->ResolveSymbolContextForAddress (so_addr, eSymbolContextEverything, sc) & eSymbolContextLineEntry)
493 sc_list.Append(sc);
494 }
495 }
496
497 if (sc_list.GetSize() == 0)
498 {
499 result.AppendErrorWithFormat("no modules have source information for file address 0x%" PRIx64 ".\n",
500 m_options.address);
501 result.SetStatus (eReturnStatusFailed);
502 return false;
503 }
504 }
505 else
506 {
507 // The target has some things loaded, resolve this address to a
508 // compile unit + file + line and display
509 if (target->GetSectionLoadList().ResolveLoadAddress (m_options.address, so_addr))
510 {
511 ModuleSP module_sp (so_addr.GetModule());
512 if (module_sp)
513 {
Greg Claytona7e864c2013-02-23 04:12:47 +0000514 sc.Clear(true);
Greg Clayton78124112012-12-07 00:19:47 +0000515 if (module_sp->ResolveSymbolContextForAddress (so_addr, eSymbolContextEverything, sc) & eSymbolContextLineEntry)
516 {
517 sc_list.Append(sc);
518 }
519 else
520 {
521 so_addr.Dump(&error_strm, NULL, Address::DumpStyleModuleWithFileAddress);
522 result.AppendErrorWithFormat("address resolves to %s, but there is no line table information available for this address.\n",
523 error_strm.GetData());
524 result.SetStatus (eReturnStatusFailed);
525 return false;
526 }
527 }
528 }
529
530 if (sc_list.GetSize() == 0)
531 {
532 result.AppendErrorWithFormat("no modules contain load address 0x%" PRIx64 ".\n", m_options.address);
533 result.SetStatus (eReturnStatusFailed);
534 return false;
535 }
536 }
537 uint32_t num_matches = sc_list.GetSize();
538 for (uint32_t i=0; i<num_matches; ++i)
539 {
540 sc_list.GetContextAtIndex(i, sc);
541 if (sc.comp_unit)
542 {
543 if (m_options.show_bp_locs)
544 {
545 m_breakpoint_locations.Clear();
546 const bool show_inlines = true;
547 m_breakpoint_locations.Reset (*sc.comp_unit, 0, show_inlines);
548 SearchFilter target_search_filter (target->shared_from_this());
549 target_search_filter.Search (m_breakpoint_locations);
550 }
551
552 bool show_fullpaths = true;
553 bool show_module = true;
554 bool show_inlined_frames = true;
555 sc.DumpStopContext(&result.GetOutputStream(),
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000556 m_exe_ctx.GetBestExecutionContextScope(),
Greg Clayton78124112012-12-07 00:19:47 +0000557 sc.line_entry.range.GetBaseAddress(),
558 show_fullpaths,
559 show_module,
560 show_inlined_frames);
561 result.GetOutputStream().EOL();
Greg Claytonb1f44b32013-03-13 18:25:49 +0000562
563 if (m_options.num_lines == 0)
564 m_options.num_lines = 10;
Greg Clayton78124112012-12-07 00:19:47 +0000565
566 size_t lines_to_back_up = m_options.num_lines >= 10 ? 5 : m_options.num_lines/2;
567
568 target->GetSourceManager().DisplaySourceLinesWithLineNumbers (sc.comp_unit,
569 sc.line_entry.line,
570 lines_to_back_up,
571 m_options.num_lines - lines_to_back_up,
572 "->",
573 &result.GetOutputStream(),
574 GetBreakpointLocations ());
575 result.SetStatus (eReturnStatusSuccessFinishResult);
576 }
577 }
578 }
Jim Ingham338f7532010-08-20 01:17:07 +0000579 else if (m_options.file_name.empty())
Jim Ingham767af882010-07-07 03:36:20 +0000580 {
581 // Last valid source manager context, or the current frame if no
582 // valid last context in source manager.
583 // One little trick here, if you type the exact same list command twice in a row, it is
584 // more likely because you typed it once, then typed it again
585 if (m_options.start_line == 0)
586 {
Jim Inghamc7f18c82011-09-29 20:22:33 +0000587 if (target->GetSourceManager().DisplayMoreWithLineNumbers (&result.GetOutputStream(),
Greg Claytonb1f44b32013-03-13 18:25:49 +0000588 m_options.num_lines,
589 m_options.reverse,
590 GetBreakpointLocations ()))
Chris Lattner24943d22010-06-08 16:52:24 +0000591 {
Chris Lattner24943d22010-06-08 16:52:24 +0000592 result.SetStatus (eReturnStatusSuccessFinishResult);
593 }
Jim Ingham767af882010-07-07 03:36:20 +0000594 }
595 else
596 {
Greg Claytonb1f44b32013-03-13 18:25:49 +0000597 if (m_options.num_lines == 0)
598 m_options.num_lines = 10;
599
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000600 if (m_options.show_bp_locs)
Greg Clayton1cee1e62011-04-20 18:52:45 +0000601 {
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000602 SourceManager::FileSP last_file_sp (target->GetSourceManager().GetLastFile ());
Greg Clayton1cee1e62011-04-20 18:52:45 +0000603 if (last_file_sp)
604 {
605 const bool show_inlines = true;
606 m_breakpoint_locations.Reset (last_file_sp->GetFileSpec(), 0, show_inlines);
Greg Clayton13d24fb2012-01-29 20:56:30 +0000607 SearchFilter target_search_filter (target->shared_from_this());
Greg Clayton1cee1e62011-04-20 18:52:45 +0000608 target_search_filter.Search (m_breakpoint_locations);
609 }
610 }
611 else
612 m_breakpoint_locations.Clear();
613
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000614 if (target->GetSourceManager().DisplaySourceLinesWithLineNumbersUsingLastFile(
Jim Ingham767af882010-07-07 03:36:20 +0000615 m_options.start_line, // Line to display
Greg Claytonb1f44b32013-03-13 18:25:49 +0000616 m_options.num_lines, // Lines after line to
617 UINT32_MAX, // Don't mark "line"
Jim Ingham767af882010-07-07 03:36:20 +0000618 "", // Don't mark "line"
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000619 &result.GetOutputStream(),
620 GetBreakpointLocations ()))
Chris Lattner24943d22010-06-08 16:52:24 +0000621 {
Jim Ingham767af882010-07-07 03:36:20 +0000622 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000623 }
Jim Ingham767af882010-07-07 03:36:20 +0000624
Chris Lattner24943d22010-06-08 16:52:24 +0000625 }
626 }
627 else
628 {
Jim Ingham767af882010-07-07 03:36:20 +0000629 const char *filename = m_options.file_name.c_str();
Jim Ingham767af882010-07-07 03:36:20 +0000630
631 bool check_inlines = false;
632 SymbolContextList sc_list;
Jim Ingham338f7532010-08-20 01:17:07 +0000633 size_t num_matches = 0;
634
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000635 if (m_options.modules.size() > 0)
Jim Ingham767af882010-07-07 03:36:20 +0000636 {
Jim Ingham338f7532010-08-20 01:17:07 +0000637 ModuleList matching_modules;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000638 for (size_t i = 0, e = m_options.modules.size(); i < e; ++i)
Jim Ingham767af882010-07-07 03:36:20 +0000639 {
Greg Clayton444fe992012-02-26 05:51:37 +0000640 FileSpec module_file_spec(m_options.modules[i].c_str(), false);
641 if (module_file_spec)
Jim Ingham767af882010-07-07 03:36:20 +0000642 {
Greg Clayton444fe992012-02-26 05:51:37 +0000643 ModuleSpec module_spec (module_file_spec);
Jim Ingham338f7532010-08-20 01:17:07 +0000644 matching_modules.Clear();
Greg Clayton444fe992012-02-26 05:51:37 +0000645 target->GetImages().FindModules (module_spec, matching_modules);
Jim Ingham338f7532010-08-20 01:17:07 +0000646 num_matches += matching_modules.ResolveSymbolContextForFilePath (filename,
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000647 0,
648 check_inlines,
649 eSymbolContextModule | eSymbolContextCompUnit,
650 sc_list);
Jim Ingham767af882010-07-07 03:36:20 +0000651 }
652 }
653 }
Jim Ingham338f7532010-08-20 01:17:07 +0000654 else
655 {
656 num_matches = target->GetImages().ResolveSymbolContextForFilePath (filename,
657 0,
658 check_inlines,
659 eSymbolContextModule | eSymbolContextCompUnit,
660 sc_list);
661 }
662
663 if (num_matches == 0)
664 {
665 result.AppendErrorWithFormat("Could not find source file \"%s\".\n",
666 m_options.file_name.c_str());
667 result.SetStatus (eReturnStatusFailed);
668 return false;
669 }
670
671 if (num_matches > 1)
672 {
673 SymbolContext sc;
674 bool got_multiple = false;
675 FileSpec *test_cu_spec = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000676
Chris Lattner0f6fa732010-09-08 22:55:31 +0000677 for (unsigned i = 0; i < num_matches; i++)
Jim Ingham338f7532010-08-20 01:17:07 +0000678 {
679 sc_list.GetContextAtIndex(i, sc);
680 if (sc.comp_unit)
681 {
682 if (test_cu_spec)
683 {
684 if (test_cu_spec != static_cast<FileSpec *> (sc.comp_unit))
685 got_multiple = true;
686 break;
687 }
688 else
689 test_cu_spec = sc.comp_unit;
690 }
691 }
692 if (got_multiple)
693 {
694 result.AppendErrorWithFormat("Multiple source files found matching: \"%s.\"\n",
695 m_options.file_name.c_str());
696 result.SetStatus (eReturnStatusFailed);
697 return false;
698 }
699 }
700
701 SymbolContext sc;
702 if (sc_list.GetContextAtIndex(0, sc))
703 {
704 if (sc.comp_unit)
705 {
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000706 if (m_options.show_bp_locs)
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000707 {
708 const bool show_inlines = true;
709 m_breakpoint_locations.Reset (*sc.comp_unit, 0, show_inlines);
Greg Clayton13d24fb2012-01-29 20:56:30 +0000710 SearchFilter target_search_filter (target->shared_from_this());
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000711 target_search_filter.Search (m_breakpoint_locations);
712 }
713 else
714 m_breakpoint_locations.Clear();
715
Greg Claytonb1f44b32013-03-13 18:25:49 +0000716 if (m_options.num_lines == 0)
717 m_options.num_lines = 10;
718
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000719 target->GetSourceManager().DisplaySourceLinesWithLineNumbers (sc.comp_unit,
720 m_options.start_line,
721 0,
722 m_options.num_lines,
723 "",
724 &result.GetOutputStream(),
725 GetBreakpointLocations ());
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000726
Jim Ingham338f7532010-08-20 01:17:07 +0000727 result.SetStatus (eReturnStatusSuccessFinishResult);
728 }
729 else
730 {
731 result.AppendErrorWithFormat("No comp unit found for: \"%s.\"\n",
732 m_options.file_name.c_str());
733 result.SetStatus (eReturnStatusFailed);
734 return false;
735 }
736 }
737 }
Jim Ingham767af882010-07-07 03:36:20 +0000738 return result.Succeeded();
Chris Lattner24943d22010-06-08 16:52:24 +0000739 }
Jim Ingham767af882010-07-07 03:36:20 +0000740
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000741 const SymbolContextList *
742 GetBreakpointLocations ()
743 {
744 if (m_breakpoint_locations.GetFileLineMatches().GetSize() > 0)
745 return &m_breakpoint_locations.GetFileLineMatches();
746 return NULL;
747 }
Jim Ingham767af882010-07-07 03:36:20 +0000748 CommandOptions m_options;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000749 FileLineResolver m_breakpoint_locations;
Jim Ingham12b5aa02013-01-09 03:27:33 +0000750 std::string m_reverse_name;
Jim Ingham767af882010-07-07 03:36:20 +0000751
752};
753
Greg Claytonb3448432011-03-24 21:19:54 +0000754OptionDefinition
Jim Ingham767af882010-07-07 03:36:20 +0000755CommandObjectSourceList::CommandOptions::g_option_table[] =
756{
Greg Clayton78124112012-12-07 00:19:47 +0000757{ LLDB_OPT_SET_ALL, false, "count", 'c', required_argument, NULL, 0, eArgTypeCount, "The number of source lines to display."},
758{ LLDB_OPT_SET_1 |
759 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 +0000760{ 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 +0000761{ LLDB_OPT_SET_1 , false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "The file from which to display source."},
762{ LLDB_OPT_SET_1 , false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "The line number at which to start the display source."},
763{ 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 +0000764{ 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 +0000765{ 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 +0000766{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Jim Ingham767af882010-07-07 03:36:20 +0000767};
768
769#pragma mark CommandObjectMultiwordSource
770
771//-------------------------------------------------------------------------
772// CommandObjectMultiwordSource
773//-------------------------------------------------------------------------
774
775CommandObjectMultiwordSource::CommandObjectMultiwordSource (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +0000776 CommandObjectMultiword (interpreter,
777 "source",
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000778 "A set of commands for accessing source file information",
Jim Ingham767af882010-07-07 03:36:20 +0000779 "source <subcommand> [<subcommand-options>]")
780{
Greg Claytond8b82152013-04-18 19:58:52 +0000781 // "source info" isn't implemented yet...
782 //LoadSubCommand ("info", CommandObjectSP (new CommandObjectSourceInfo (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +0000783 LoadSubCommand ("list", CommandObjectSP (new CommandObjectSourceList (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +0000784}
Jim Ingham767af882010-07-07 03:36:20 +0000785
786CommandObjectMultiwordSource::~CommandObjectMultiwordSource ()
787{
788}
789