blob: 03715a0ee4bf5f7b78352598be9da7a181813f40 [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 Ingham767af882010-07-07 03:36:20 +0000204 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000205 error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
Jim Ingham767af882010-07-07 03:36:20 +0000206 break;
207 }
208
209 return error;
210 }
211
212 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000213 OptionParsingStarting ()
Jim Ingham767af882010-07-07 03:36:20 +0000214 {
Jim Ingham767af882010-07-07 03:36:20 +0000215 file_spec.Clear();
216 file_name.clear();
Jim Ingham338f7532010-08-20 01:17:07 +0000217 symbol_name.clear();
Greg Clayton78124112012-12-07 00:19:47 +0000218 address = LLDB_INVALID_ADDRESS;
Jim Ingham767af882010-07-07 03:36:20 +0000219 start_line = 0;
220 num_lines = 10;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000221 show_bp_locs = false;
222 modules.clear();
Jim Ingham767af882010-07-07 03:36:20 +0000223 }
224
Greg Claytonb3448432011-03-24 21:19:54 +0000225 const OptionDefinition*
Jim Ingham767af882010-07-07 03:36:20 +0000226 GetDefinitions ()
227 {
228 return g_option_table;
229 }
Greg Claytonb3448432011-03-24 21:19:54 +0000230 static OptionDefinition g_option_table[];
Jim Ingham767af882010-07-07 03:36:20 +0000231
232 // Instance variables to hold the values for command options.
233 FileSpec file_spec;
234 std::string file_name;
Jim Ingham338f7532010-08-20 01:17:07 +0000235 std::string symbol_name;
Greg Clayton78124112012-12-07 00:19:47 +0000236 lldb::addr_t address;
Jim Ingham767af882010-07-07 03:36:20 +0000237 uint32_t start_line;
238 uint32_t num_lines;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000239 STLStringArray modules;
240 bool show_bp_locs;
Jim Ingham767af882010-07-07 03:36:20 +0000241 };
242
243public:
Greg Clayton238c0a12010-09-18 01:14:36 +0000244 CommandObjectSourceList(CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000245 CommandObjectParsed (interpreter,
246 "source list",
247 "Display source code (as specified) based on the current executable's debug info.",
248 NULL),
Greg Claytonf15996e2011-04-07 22:46:35 +0000249 m_options (interpreter)
Jim Ingham767af882010-07-07 03:36:20 +0000250 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000251 CommandArgumentEntry arg;
252 CommandArgumentData file_arg;
253
254 // Define the first (and only) variant of this arg.
255 file_arg.arg_type = eArgTypeFilename;
256 file_arg.arg_repetition = eArgRepeatOptional;
257
258 // There is only one variant this argument could be; put it into the argument entry.
259 arg.push_back (file_arg);
260
261 // Push the data for the first argument into the m_arguments vector.
262 m_arguments.push_back (arg);
Jim Ingham767af882010-07-07 03:36:20 +0000263 }
264
265 ~CommandObjectSourceList ()
266 {
267 }
268
269
270 Options *
271 GetOptions ()
272 {
273 return &m_options;
274 }
275
Jim Inghamda26bd22012-06-08 21:56:10 +0000276 virtual const char *
277 GetRepeatCommand (Args &current_command_args, uint32_t index)
Jim Ingham767af882010-07-07 03:36:20 +0000278 {
Jim Inghamda26bd22012-06-08 21:56:10 +0000279 return m_cmd_name.c_str();
280 }
281
282protected:
283 bool
284 DoExecute (Args& command, CommandReturnObject &result)
285 {
286 const int argc = command.GetArgumentCount();
Jim Ingham767af882010-07-07 03:36:20 +0000287
288 if (argc != 0)
289 {
290 result.AppendErrorWithFormat("'%s' takes no arguments, only flags.\n", GetCommandName());
291 result.SetStatus (eReturnStatusFailed);
Jim Ingham26e089b2011-11-29 21:21:26 +0000292 return false;
Jim Ingham767af882010-07-07 03:36:20 +0000293 }
294
Greg Claytonb72d0f02011-04-12 05:54:46 +0000295 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +0000296 Target *target = exe_ctx.GetTargetPtr();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000297
Greg Clayton567e7f32011-09-22 04:58:26 +0000298 if (target == NULL)
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000299 target = m_interpreter.GetDebugger().GetSelectedTarget().get();
300
301 if (target == NULL)
302 {
303 result.AppendError ("invalid target, create a debug target using the 'target create' command");
304 result.SetStatus (eReturnStatusFailed);
305 return false;
306 }
307
Greg Clayton78124112012-12-07 00:19:47 +0000308 SymbolContextList sc_list;
Jim Ingham338f7532010-08-20 01:17:07 +0000309 if (!m_options.symbol_name.empty())
310 {
311 // Displaying the source for a symbol:
Jim Ingham338f7532010-08-20 01:17:07 +0000312 ConstString name(m_options.symbol_name.c_str());
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000313 bool include_symbols = false;
Sean Callanan302d78c2012-02-10 22:52:19 +0000314 bool include_inlines = true;
Jim Ingham338f7532010-08-20 01:17:07 +0000315 bool append = true;
316 size_t num_matches = 0;
317
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000318 if (m_options.modules.size() > 0)
Jim Ingham338f7532010-08-20 01:17:07 +0000319 {
320 ModuleList matching_modules;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000321 for (unsigned i = 0, e = m_options.modules.size(); i != e; i++)
Jim Ingham338f7532010-08-20 01:17:07 +0000322 {
Greg Clayton444fe992012-02-26 05:51:37 +0000323 FileSpec module_file_spec(m_options.modules[i].c_str(), false);
324 if (module_file_spec)
Jim Ingham338f7532010-08-20 01:17:07 +0000325 {
Greg Clayton444fe992012-02-26 05:51:37 +0000326 ModuleSpec module_spec (module_file_spec);
Jim Ingham338f7532010-08-20 01:17:07 +0000327 matching_modules.Clear();
Greg Clayton444fe992012-02-26 05:51:37 +0000328 target->GetImages().FindModules (module_spec, matching_modules);
Sean Callanan302d78c2012-02-10 22:52:19 +0000329 num_matches += matching_modules.FindFunctions (name, eFunctionNameTypeAuto, include_symbols, include_inlines, append, sc_list);
Jim Ingham338f7532010-08-20 01:17:07 +0000330 }
331 }
332 }
333 else
334 {
Sean Callanan302d78c2012-02-10 22:52:19 +0000335 num_matches = target->GetImages().FindFunctions (name, eFunctionNameTypeAuto, include_symbols, include_inlines, append, sc_list);
Jim Ingham338f7532010-08-20 01:17:07 +0000336 }
337
338 SymbolContext sc;
339
340 if (num_matches == 0)
341 {
342 result.AppendErrorWithFormat("Could not find function named: \"%s\".\n", m_options.symbol_name.c_str());
343 result.SetStatus (eReturnStatusFailed);
344 return false;
345 }
346
347 sc_list.GetContextAtIndex (0, sc);
348 FileSpec start_file;
349 uint32_t start_line;
350 uint32_t end_line;
351 FileSpec end_file;
352 if (sc.function != NULL)
353 {
354 sc.function->GetStartLineSourceInfo (start_file, start_line);
355 if (start_line == 0)
356 {
357 result.AppendErrorWithFormat("Could not find line information for start of function: \"%s\".\n", m_options.symbol_name.c_str());
358 result.SetStatus (eReturnStatusFailed);
359 return false;
360 }
361 sc.function->GetEndLineSourceInfo (end_file, end_line);
362 }
363 else
364 {
365 result.AppendErrorWithFormat("Could not find function info for: \"%s\".\n", m_options.symbol_name.c_str());
366 result.SetStatus (eReturnStatusFailed);
367 return false;
368 }
369
370 if (num_matches > 1)
371 {
372 // This could either be because there are multiple functions of this name, in which case
373 // we'll have to specify this further... Or it could be because there are multiple inlined instances
374 // of one function. So run through the matches and if they all have the same file & line then we can just
375 // list one.
376
377 bool found_multiple = false;
378
379 for (size_t i = 1; i < num_matches; i++)
380 {
381 SymbolContext scratch_sc;
382 sc_list.GetContextAtIndex (i, scratch_sc);
383 if (scratch_sc.function != NULL)
384 {
385 FileSpec scratch_file;
386 uint32_t scratch_line;
387 scratch_sc.function->GetStartLineSourceInfo (scratch_file, scratch_line);
388 if (scratch_file != start_file
389 || scratch_line != start_line)
390 {
391 found_multiple = true;
392 break;
393 }
394 }
395 }
396 if (found_multiple)
397 {
398 StreamString s;
399 for (size_t i = 0; i < num_matches; i++)
400 {
401 SymbolContext scratch_sc;
402 sc_list.GetContextAtIndex (i, scratch_sc);
403 if (scratch_sc.function != NULL)
404 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000405 s.Printf("\n%lu: ", i);
Jim Ingham338f7532010-08-20 01:17:07 +0000406 scratch_sc.function->Dump (&s, true);
407 }
408 }
409 result.AppendErrorWithFormat("Multiple functions found matching: %s: \n%s\n",
410 m_options.symbol_name.c_str(),
411 s.GetData());
412 result.SetStatus (eReturnStatusFailed);
413 return false;
414 }
415 }
416
417
418 // This is a little hacky, but the first line table entry for a function points to the "{" that
419 // starts the function block. It would be nice to actually get the function
420 // declaration in there too. So back up a bit, but not further than what you're going to display.
421 size_t lines_to_back_up = m_options.num_lines >= 10 ? 5 : m_options.num_lines/2;
422 uint32_t line_no;
423 if (start_line <= lines_to_back_up)
424 line_no = 1;
425 else
426 line_no = start_line - lines_to_back_up;
427
428 // For fun, if the function is shorter than the number of lines we're supposed to display,
429 // only display the function...
430 if (end_line != 0)
431 {
432 if (m_options.num_lines > end_line - line_no)
433 m_options.num_lines = end_line - line_no;
434 }
435
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000436 char path_buf[PATH_MAX];
437 start_file.GetPath(path_buf, sizeof(path_buf));
Greg Clayton1cee1e62011-04-20 18:52:45 +0000438
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000439 if (m_options.show_bp_locs)
Greg Clayton1cee1e62011-04-20 18:52:45 +0000440 {
441 const bool show_inlines = true;
442 m_breakpoint_locations.Reset (start_file, 0, show_inlines);
Greg Clayton567e7f32011-09-22 04:58:26 +0000443 SearchFilter target_search_filter (exe_ctx.GetTargetSP());
Greg Clayton1cee1e62011-04-20 18:52:45 +0000444 target_search_filter.Search (m_breakpoint_locations);
445 }
446 else
447 m_breakpoint_locations.Clear();
448
Jim Ingham338f7532010-08-20 01:17:07 +0000449 result.AppendMessageWithFormat("File: %s.\n", path_buf);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000450 target->GetSourceManager().DisplaySourceLinesWithLineNumbers (start_file,
451 line_no,
452 0,
453 m_options.num_lines,
454 "",
455 &result.GetOutputStream(),
456 GetBreakpointLocations ());
Jim Ingham338f7532010-08-20 01:17:07 +0000457
458 result.SetStatus (eReturnStatusSuccessFinishResult);
459 return true;
460
461 }
Greg Clayton78124112012-12-07 00:19:47 +0000462 else if (m_options.address != LLDB_INVALID_ADDRESS)
463 {
464 SymbolContext sc;
465 Address so_addr;
466 StreamString error_strm;
467
468 if (target->GetSectionLoadList().IsEmpty())
469 {
470 // The target isn't loaded yet, we need to lookup the file address
471 // in all modules
472 const ModuleList &module_list = target->GetImages();
473 const uint32_t num_modules = module_list.GetSize();
474 for (uint32_t i=0; i<num_modules; ++i)
475 {
476 ModuleSP module_sp (module_list.GetModuleAtIndex(i));
477 if (module_sp && module_sp->ResolveFileAddress(m_options.address, so_addr))
478 {
479 sc.Clear();
480 if (module_sp->ResolveSymbolContextForAddress (so_addr, eSymbolContextEverything, sc) & eSymbolContextLineEntry)
481 sc_list.Append(sc);
482 }
483 }
484
485 if (sc_list.GetSize() == 0)
486 {
487 result.AppendErrorWithFormat("no modules have source information for file address 0x%" PRIx64 ".\n",
488 m_options.address);
489 result.SetStatus (eReturnStatusFailed);
490 return false;
491 }
492 }
493 else
494 {
495 // The target has some things loaded, resolve this address to a
496 // compile unit + file + line and display
497 if (target->GetSectionLoadList().ResolveLoadAddress (m_options.address, so_addr))
498 {
499 ModuleSP module_sp (so_addr.GetModule());
500 if (module_sp)
501 {
502 sc.Clear();
503 if (module_sp->ResolveSymbolContextForAddress (so_addr, eSymbolContextEverything, sc) & eSymbolContextLineEntry)
504 {
505 sc_list.Append(sc);
506 }
507 else
508 {
509 so_addr.Dump(&error_strm, NULL, Address::DumpStyleModuleWithFileAddress);
510 result.AppendErrorWithFormat("address resolves to %s, but there is no line table information available for this address.\n",
511 error_strm.GetData());
512 result.SetStatus (eReturnStatusFailed);
513 return false;
514 }
515 }
516 }
517
518 if (sc_list.GetSize() == 0)
519 {
520 result.AppendErrorWithFormat("no modules contain load address 0x%" PRIx64 ".\n", m_options.address);
521 result.SetStatus (eReturnStatusFailed);
522 return false;
523 }
524 }
525 uint32_t num_matches = sc_list.GetSize();
526 for (uint32_t i=0; i<num_matches; ++i)
527 {
528 sc_list.GetContextAtIndex(i, sc);
529 if (sc.comp_unit)
530 {
531 if (m_options.show_bp_locs)
532 {
533 m_breakpoint_locations.Clear();
534 const bool show_inlines = true;
535 m_breakpoint_locations.Reset (*sc.comp_unit, 0, show_inlines);
536 SearchFilter target_search_filter (target->shared_from_this());
537 target_search_filter.Search (m_breakpoint_locations);
538 }
539
540 bool show_fullpaths = true;
541 bool show_module = true;
542 bool show_inlined_frames = true;
543 sc.DumpStopContext(&result.GetOutputStream(),
544 exe_ctx.GetBestExecutionContextScope(),
545 sc.line_entry.range.GetBaseAddress(),
546 show_fullpaths,
547 show_module,
548 show_inlined_frames);
549 result.GetOutputStream().EOL();
550
551 size_t lines_to_back_up = m_options.num_lines >= 10 ? 5 : m_options.num_lines/2;
552
553 target->GetSourceManager().DisplaySourceLinesWithLineNumbers (sc.comp_unit,
554 sc.line_entry.line,
555 lines_to_back_up,
556 m_options.num_lines - lines_to_back_up,
557 "->",
558 &result.GetOutputStream(),
559 GetBreakpointLocations ());
560 result.SetStatus (eReturnStatusSuccessFinishResult);
561 }
562 }
563 }
Jim Ingham338f7532010-08-20 01:17:07 +0000564 else if (m_options.file_name.empty())
Jim Ingham767af882010-07-07 03:36:20 +0000565 {
566 // Last valid source manager context, or the current frame if no
567 // valid last context in source manager.
568 // One little trick here, if you type the exact same list command twice in a row, it is
569 // more likely because you typed it once, then typed it again
570 if (m_options.start_line == 0)
571 {
Jim Inghamc7f18c82011-09-29 20:22:33 +0000572 if (target->GetSourceManager().DisplayMoreWithLineNumbers (&result.GetOutputStream(),
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000573 GetBreakpointLocations ()))
Chris Lattner24943d22010-06-08 16:52:24 +0000574 {
Chris Lattner24943d22010-06-08 16:52:24 +0000575 result.SetStatus (eReturnStatusSuccessFinishResult);
576 }
Jim Ingham767af882010-07-07 03:36:20 +0000577 }
578 else
579 {
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000580 if (m_options.show_bp_locs)
Greg Clayton1cee1e62011-04-20 18:52:45 +0000581 {
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000582 SourceManager::FileSP last_file_sp (target->GetSourceManager().GetLastFile ());
Greg Clayton1cee1e62011-04-20 18:52:45 +0000583 if (last_file_sp)
584 {
585 const bool show_inlines = true;
586 m_breakpoint_locations.Reset (last_file_sp->GetFileSpec(), 0, show_inlines);
Greg Clayton13d24fb2012-01-29 20:56:30 +0000587 SearchFilter target_search_filter (target->shared_from_this());
Greg Clayton1cee1e62011-04-20 18:52:45 +0000588 target_search_filter.Search (m_breakpoint_locations);
589 }
590 }
591 else
592 m_breakpoint_locations.Clear();
593
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000594 if (target->GetSourceManager().DisplaySourceLinesWithLineNumbersUsingLastFile(
Jim Ingham767af882010-07-07 03:36:20 +0000595 m_options.start_line, // Line to display
596 0, // Lines before line to display
597 m_options.num_lines, // Lines after line to display
598 "", // Don't mark "line"
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000599 &result.GetOutputStream(),
600 GetBreakpointLocations ()))
Chris Lattner24943d22010-06-08 16:52:24 +0000601 {
Jim Ingham767af882010-07-07 03:36:20 +0000602 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000603 }
Jim Ingham767af882010-07-07 03:36:20 +0000604
Chris Lattner24943d22010-06-08 16:52:24 +0000605 }
606 }
607 else
608 {
Jim Ingham767af882010-07-07 03:36:20 +0000609 const char *filename = m_options.file_name.c_str();
Jim Ingham767af882010-07-07 03:36:20 +0000610
611 bool check_inlines = false;
612 SymbolContextList sc_list;
Jim Ingham338f7532010-08-20 01:17:07 +0000613 size_t num_matches = 0;
614
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000615 if (m_options.modules.size() > 0)
Jim Ingham767af882010-07-07 03:36:20 +0000616 {
Jim Ingham338f7532010-08-20 01:17:07 +0000617 ModuleList matching_modules;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000618 for (unsigned i = 0, e = m_options.modules.size(); i != e; i++)
Jim Ingham767af882010-07-07 03:36:20 +0000619 {
Greg Clayton444fe992012-02-26 05:51:37 +0000620 FileSpec module_file_spec(m_options.modules[i].c_str(), false);
621 if (module_file_spec)
Jim Ingham767af882010-07-07 03:36:20 +0000622 {
Greg Clayton444fe992012-02-26 05:51:37 +0000623 ModuleSpec module_spec (module_file_spec);
Jim Ingham338f7532010-08-20 01:17:07 +0000624 matching_modules.Clear();
Greg Clayton444fe992012-02-26 05:51:37 +0000625 target->GetImages().FindModules (module_spec, matching_modules);
Jim Ingham338f7532010-08-20 01:17:07 +0000626 num_matches += matching_modules.ResolveSymbolContextForFilePath (filename,
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000627 0,
628 check_inlines,
629 eSymbolContextModule | eSymbolContextCompUnit,
630 sc_list);
Jim Ingham767af882010-07-07 03:36:20 +0000631 }
632 }
633 }
Jim Ingham338f7532010-08-20 01:17:07 +0000634 else
635 {
636 num_matches = target->GetImages().ResolveSymbolContextForFilePath (filename,
637 0,
638 check_inlines,
639 eSymbolContextModule | eSymbolContextCompUnit,
640 sc_list);
641 }
642
643 if (num_matches == 0)
644 {
645 result.AppendErrorWithFormat("Could not find source file \"%s\".\n",
646 m_options.file_name.c_str());
647 result.SetStatus (eReturnStatusFailed);
648 return false;
649 }
650
651 if (num_matches > 1)
652 {
653 SymbolContext sc;
654 bool got_multiple = false;
655 FileSpec *test_cu_spec = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000656
Chris Lattner0f6fa732010-09-08 22:55:31 +0000657 for (unsigned i = 0; i < num_matches; i++)
Jim Ingham338f7532010-08-20 01:17:07 +0000658 {
659 sc_list.GetContextAtIndex(i, sc);
660 if (sc.comp_unit)
661 {
662 if (test_cu_spec)
663 {
664 if (test_cu_spec != static_cast<FileSpec *> (sc.comp_unit))
665 got_multiple = true;
666 break;
667 }
668 else
669 test_cu_spec = sc.comp_unit;
670 }
671 }
672 if (got_multiple)
673 {
674 result.AppendErrorWithFormat("Multiple source files found matching: \"%s.\"\n",
675 m_options.file_name.c_str());
676 result.SetStatus (eReturnStatusFailed);
677 return false;
678 }
679 }
680
681 SymbolContext sc;
682 if (sc_list.GetContextAtIndex(0, sc))
683 {
684 if (sc.comp_unit)
685 {
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000686 if (m_options.show_bp_locs)
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000687 {
688 const bool show_inlines = true;
689 m_breakpoint_locations.Reset (*sc.comp_unit, 0, show_inlines);
Greg Clayton13d24fb2012-01-29 20:56:30 +0000690 SearchFilter target_search_filter (target->shared_from_this());
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000691 target_search_filter.Search (m_breakpoint_locations);
692 }
693 else
694 m_breakpoint_locations.Clear();
695
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000696 target->GetSourceManager().DisplaySourceLinesWithLineNumbers (sc.comp_unit,
697 m_options.start_line,
698 0,
699 m_options.num_lines,
700 "",
701 &result.GetOutputStream(),
702 GetBreakpointLocations ());
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000703
Jim Ingham338f7532010-08-20 01:17:07 +0000704 result.SetStatus (eReturnStatusSuccessFinishResult);
705 }
706 else
707 {
708 result.AppendErrorWithFormat("No comp unit found for: \"%s.\"\n",
709 m_options.file_name.c_str());
710 result.SetStatus (eReturnStatusFailed);
711 return false;
712 }
713 }
714 }
Jim Ingham767af882010-07-07 03:36:20 +0000715 return result.Succeeded();
Chris Lattner24943d22010-06-08 16:52:24 +0000716 }
Jim Ingham767af882010-07-07 03:36:20 +0000717
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000718 const SymbolContextList *
719 GetBreakpointLocations ()
720 {
721 if (m_breakpoint_locations.GetFileLineMatches().GetSize() > 0)
722 return &m_breakpoint_locations.GetFileLineMatches();
723 return NULL;
724 }
Jim Ingham767af882010-07-07 03:36:20 +0000725 CommandOptions m_options;
Greg Clayton52c8b6e2011-04-19 04:19:37 +0000726 FileLineResolver m_breakpoint_locations;
Jim Ingham767af882010-07-07 03:36:20 +0000727
728};
729
Greg Claytonb3448432011-03-24 21:19:54 +0000730OptionDefinition
Jim Ingham767af882010-07-07 03:36:20 +0000731CommandObjectSourceList::CommandOptions::g_option_table[] =
732{
Greg Clayton78124112012-12-07 00:19:47 +0000733{ LLDB_OPT_SET_ALL, false, "count", 'c', required_argument, NULL, 0, eArgTypeCount, "The number of source lines to display."},
734{ LLDB_OPT_SET_1 |
735 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 +0000736{ 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 +0000737{ LLDB_OPT_SET_1 , false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "The file from which to display source."},
738{ LLDB_OPT_SET_1 , false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "The line number at which to start the display source."},
739{ LLDB_OPT_SET_2 , false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeSymbol, "The name of a function whose source to display."},
740{ LLDB_OPT_SET_3 , false, "address",'a', required_argument, NULL, 0, eArgTypeAddress, "Lookup the address and display the source information for the corresponding file and line."},
Caroline Tice4d6675c2010-10-01 19:59:14 +0000741{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Jim Ingham767af882010-07-07 03:36:20 +0000742};
743
744#pragma mark CommandObjectMultiwordSource
745
746//-------------------------------------------------------------------------
747// CommandObjectMultiwordSource
748//-------------------------------------------------------------------------
749
750CommandObjectMultiwordSource::CommandObjectMultiwordSource (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +0000751 CommandObjectMultiword (interpreter,
752 "source",
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000753 "A set of commands for accessing source file information",
Jim Ingham767af882010-07-07 03:36:20 +0000754 "source <subcommand> [<subcommand-options>]")
755{
Greg Clayton238c0a12010-09-18 01:14:36 +0000756 LoadSubCommand ("info", CommandObjectSP (new CommandObjectSourceInfo (interpreter)));
757 LoadSubCommand ("list", CommandObjectSP (new CommandObjectSourceList (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +0000758}
Jim Ingham767af882010-07-07 03:36:20 +0000759
760CommandObjectMultiwordSource::~CommandObjectMultiwordSource ()
761{
762}
763