blob: f442aa6073dc6dc76d3e030af722f8dd63b7017f [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Debugger.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 Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Enrico Granata21dfcd92012-09-28 23:57:51 +000012#include "lldb/API/SBDebugger.h"
13
Greg Clayton4a33d312011-06-23 17:59:56 +000014#include "lldb/Core/Debugger.h"
15
16#include <map>
17
Enrico Granata4becb372011-06-29 22:27:15 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/Type.h"
20
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/lldb-private.h"
22#include "lldb/Core/ConnectionFileDescriptor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/InputReader.h"
Greg Clayton1f746072012-08-29 21:13:06 +000024#include "lldb/Core/Module.h"
Greg Claytone8cd0c92012-10-19 18:02:49 +000025#include "lldb/Core/PluginManager.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000026#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000028#include "lldb/Core/StreamAsynchronousIO.h"
Jim Ingham228063c2012-02-21 02:23:08 +000029#include "lldb/Core/StreamCallback.h"
Greg Clayton1b654882010-09-19 02:33:57 +000030#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000032#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000033#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000034#include "lldb/DataFormatters/DataVisualization.h"
35#include "lldb/DataFormatters/FormatManager.h"
Enrico Granata21dfcd92012-09-28 23:57:51 +000036#include "lldb/Host/DynamicLibrary.h"
Greg Claytona3406612011-02-07 23:24:47 +000037#include "lldb/Host/Terminal.h"
Greg Clayton66111032010-06-23 01:19:29 +000038#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000039#include "lldb/Interpreter/OptionValueSInt64.h"
40#include "lldb/Interpreter/OptionValueString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000041#include "lldb/Symbol/ClangASTContext.h"
42#include "lldb/Symbol/CompileUnit.h"
43#include "lldb/Symbol/Function.h"
44#include "lldb/Symbol/Symbol.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000045#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "lldb/Target/TargetList.h"
47#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000048#include "lldb/Target/RegisterContext.h"
49#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000051#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052
53using namespace lldb;
54using namespace lldb_private;
55
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056
Greg Clayton1b654882010-09-19 02:33:57 +000057static uint32_t g_shared_debugger_refcount = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000058static lldb::user_id_t g_unique_id = 1;
59
Greg Clayton1b654882010-09-19 02:33:57 +000060#pragma mark Static Functions
61
62static Mutex &
63GetDebuggerListMutex ()
64{
65 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
66 return g_mutex;
67}
68
69typedef std::vector<DebuggerSP> DebuggerList;
70
71static DebuggerList &
72GetDebuggerList()
73{
74 // hide the static debugger list inside a singleton accessor to avoid
75 // global init contructors
76 static DebuggerList g_list;
77 return g_list;
78}
Greg Claytone372b982011-11-21 21:44:34 +000079
80OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000081g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000082{
Greg Clayton67cc0632012-08-22 17:17:09 +000083 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
84 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
85 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000086 { 0, NULL, NULL }
87};
88
Greg Clayton67cc0632012-08-22 17:17:09 +000089OptionEnumValueElement
90g_language_enumerators[] =
91{
92 { eScriptLanguageNone, "none", "Disable scripting languages."},
93 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
94 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +000095 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +000096};
Greg Claytone372b982011-11-21 21:44:34 +000097
Greg Clayton67cc0632012-08-22 17:17:09 +000098#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
99#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
100
101#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
102 "{, ${frame.pc}}"\
103 MODULE_WITH_FUNC\
104 FILE_AND_LINE\
Greg Clayton85d0c572013-04-04 20:40:35 +0000105 "{, name = '${thread.name}}"\
106 "{, queue = '${thread.queue}}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000107 "{, stop reason = ${thread.stop-reason}}"\
108 "{\\nReturn value: ${thread.return-value}}"\
109 "\\n"
110
111#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
112 MODULE_WITH_FUNC\
113 FILE_AND_LINE\
114 "\\n"
115
116
117
Greg Clayton754a9362012-08-23 00:22:02 +0000118static PropertyDefinition
119g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000120{
121{ "auto-confirm", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
122{ "frame-format", OptionValue::eTypeString , true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
123{ "notify-void", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Notify the user explicitly if an expression returns void (default: false)." },
Greg Clayton4c054102012-09-01 00:38:36 +0000124{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
Greg Clayton67cc0632012-08-22 17:17:09 +0000125{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
126{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
127{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
128{ "stop-line-count-after", OptionValue::eTypeSInt64 , true, 3 , NULL, NULL, "The number of sources lines to display that come after the current source line when displaying a stopped context." },
129{ "stop-line-count-before", OptionValue::eTypeSInt64 , true, 3 , NULL, NULL, "The number of sources lines to display that come before the current source line when displaying a stopped context." },
130{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
131{ "thread-format", OptionValue::eTypeString , true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
132{ "use-external-editor", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." },
Greg Claytone8cd0c92012-10-19 18:02:49 +0000133
134 { NULL, OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000135};
136
137enum
138{
139 ePropertyAutoConfirm = 0,
140 ePropertyFrameFormat,
141 ePropertyNotiftVoid,
142 ePropertyPrompt,
143 ePropertyScriptLanguage,
144 ePropertyStopDisassemblyCount,
145 ePropertyStopDisassemblyDisplay,
146 ePropertyStopLineCountAfter,
147 ePropertyStopLineCountBefore,
148 ePropertyTerminalWidth,
149 ePropertyThreadFormat,
150 ePropertyUseExternalEditor
151};
152
153//
154//const char *
155//Debugger::GetFrameFormat() const
156//{
157// return m_properties_sp->GetFrameFormat();
158//}
159//const char *
160//Debugger::GetThreadFormat() const
161//{
162// return m_properties_sp->GetThreadFormat();
163//}
164//
Greg Clayton4c054102012-09-01 00:38:36 +0000165
166
167Error
168Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
169 VarSetOperationType op,
170 const char *property_path,
171 const char *value)
172{
173 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
174 if (error.Success())
175 {
176 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
177 {
178 const char *new_prompt = GetPrompt();
179 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
180 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
181 }
182 }
183 return error;
184}
185
Greg Clayton67cc0632012-08-22 17:17:09 +0000186bool
187Debugger::GetAutoConfirm () const
188{
189 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000190 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000191}
192
193const char *
194Debugger::GetFrameFormat() const
195{
196 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000197 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000198}
199
200bool
201Debugger::GetNotifyVoid () const
202{
203 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000204 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000205}
206
207const char *
208Debugger::GetPrompt() const
209{
210 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000211 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000212}
213
214void
215Debugger::SetPrompt(const char *p)
216{
217 const uint32_t idx = ePropertyPrompt;
218 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
219 const char *new_prompt = GetPrompt();
220 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));;
221 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
222}
223
224const char *
225Debugger::GetThreadFormat() const
226{
227 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000228 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000229}
230
231lldb::ScriptLanguage
232Debugger::GetScriptLanguage() const
233{
234 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000235 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000236}
237
238bool
239Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
240{
241 const uint32_t idx = ePropertyScriptLanguage;
242 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
243}
244
245uint32_t
246Debugger::GetTerminalWidth () const
247{
248 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000249 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000250}
251
252bool
253Debugger::SetTerminalWidth (uint32_t term_width)
254{
255 const uint32_t idx = ePropertyTerminalWidth;
256 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
257}
258
259bool
260Debugger::GetUseExternalEditor () const
261{
262 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000263 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000264}
265
266bool
267Debugger::SetUseExternalEditor (bool b)
268{
269 const uint32_t idx = ePropertyUseExternalEditor;
270 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
271}
272
273uint32_t
274Debugger::GetStopSourceLineCount (bool before) const
275{
276 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000277 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000278}
279
280Debugger::StopDisassemblyType
281Debugger::GetStopDisassemblyDisplay () const
282{
283 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000284 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000285}
286
287uint32_t
288Debugger::GetDisassemblyLineCount () const
289{
290 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000291 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000292}
Greg Claytone372b982011-11-21 21:44:34 +0000293
Greg Clayton1b654882010-09-19 02:33:57 +0000294#pragma mark Debugger
295
Greg Clayton67cc0632012-08-22 17:17:09 +0000296//const DebuggerPropertiesSP &
297//Debugger::GetSettings() const
298//{
299// return m_properties_sp;
300//}
301//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000302
Caroline Tice2f88aad2011-01-14 00:29:16 +0000303int
304Debugger::TestDebuggerRefCount ()
305{
306 return g_shared_debugger_refcount;
307}
308
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309void
310Debugger::Initialize ()
311{
Greg Claytonc15f55e2012-03-30 20:53:46 +0000312 if (g_shared_debugger_refcount++ == 0)
Greg Claytondbe54502010-11-19 03:46:01 +0000313 lldb_private::Initialize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314}
315
316void
317Debugger::Terminate ()
318{
Greg Clayton66111032010-06-23 01:19:29 +0000319 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320 {
Greg Clayton66111032010-06-23 01:19:29 +0000321 g_shared_debugger_refcount--;
322 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323 {
Greg Claytondbe54502010-11-19 03:46:01 +0000324 lldb_private::WillTerminate();
325 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000326
327 // Clear our master list of debugger objects
328 Mutex::Locker locker (GetDebuggerListMutex ());
329 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000330 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331 }
332}
333
Caroline Tice20bd37f2011-03-10 22:14:10 +0000334void
335Debugger::SettingsInitialize ()
336{
Greg Clayton6920b522012-08-22 18:39:03 +0000337 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000338}
339
340void
341Debugger::SettingsTerminate ()
342{
Greg Clayton6920b522012-08-22 18:39:03 +0000343 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000344}
345
Enrico Granata21dfcd92012-09-28 23:57:51 +0000346bool
347Debugger::LoadPlugin (const FileSpec& spec)
348{
349 lldb::DynamicLibrarySP dynlib_sp(new lldb_private::DynamicLibrary(spec));
350 lldb::DebuggerSP debugger_sp(shared_from_this());
351 lldb::SBDebugger debugger_sb(debugger_sp);
352 // TODO: mangle this differently for your system - on OSX, the first underscore needs to be removed and the second one stays
353 LLDBCommandPluginInit init_func = dynlib_sp->GetSymbol<LLDBCommandPluginInit>("_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
354 if (!init_func)
355 return false;
356 if (init_func(debugger_sb))
357 {
358 m_loaded_plugins.push_back(dynlib_sp);
359 return true;
360 }
361 return false;
362}
363
364static FileSpec::EnumerateDirectoryResult
365LoadPluginCallback
366(
367 void *baton,
368 FileSpec::FileType file_type,
369 const FileSpec &file_spec
370 )
371{
372 Error error;
373
374 static ConstString g_dylibext("dylib");
375
376 if (!baton)
377 return FileSpec::eEnumerateDirectoryResultQuit;
378
379 Debugger *debugger = (Debugger*)baton;
380
381 // If we have a regular file, a symbolic link or unknown file type, try
382 // and process the file. We must handle unknown as sometimes the directory
383 // enumeration might be enumerating a file system that doesn't have correct
384 // file type information.
385 if (file_type == FileSpec::eFileTypeRegular ||
386 file_type == FileSpec::eFileTypeSymbolicLink ||
387 file_type == FileSpec::eFileTypeUnknown )
388 {
389 FileSpec plugin_file_spec (file_spec);
390 plugin_file_spec.ResolvePath ();
391
392 if (plugin_file_spec.GetFileNameExtension() != g_dylibext)
393 return FileSpec::eEnumerateDirectoryResultNext;
394
395 debugger->LoadPlugin (plugin_file_spec);
396
397 return FileSpec::eEnumerateDirectoryResultNext;
398 }
399
400 else if (file_type == FileSpec::eFileTypeUnknown ||
401 file_type == FileSpec::eFileTypeDirectory ||
402 file_type == FileSpec::eFileTypeSymbolicLink )
403 {
404 // Try and recurse into anything that a directory or symbolic link.
405 // We must also do this for unknown as sometimes the directory enumeration
406 // might be enurating a file system that doesn't have correct file type
407 // information.
408 return FileSpec::eEnumerateDirectoryResultEnter;
409 }
410
411 return FileSpec::eEnumerateDirectoryResultNext;
412}
413
414void
415Debugger::InstanceInitialize ()
416{
417 FileSpec dir_spec;
418 const bool find_directories = true;
419 const bool find_files = true;
420 const bool find_other = true;
421 char dir_path[PATH_MAX];
422 if (Host::GetLLDBPath (ePathTypeLLDBSystemPlugins, dir_spec))
423 {
424 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
425 {
426 FileSpec::EnumerateDirectory (dir_path,
427 find_directories,
428 find_files,
429 find_other,
430 LoadPluginCallback,
431 this);
432 }
433 }
434
435 if (Host::GetLLDBPath (ePathTypeLLDBUserPlugins, dir_spec))
436 {
437 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
438 {
439 FileSpec::EnumerateDirectory (dir_path,
440 find_directories,
441 find_files,
442 find_other,
443 LoadPluginCallback,
444 this);
445 }
446 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000447
448 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000449}
450
Greg Clayton66111032010-06-23 01:19:29 +0000451DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000452Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000453{
Jim Ingham228063c2012-02-21 02:23:08 +0000454 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Claytonc15f55e2012-03-30 20:53:46 +0000455 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000456 {
457 Mutex::Locker locker (GetDebuggerListMutex ());
458 GetDebuggerList().push_back(debugger_sp);
459 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000460 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000461 return debugger_sp;
462}
463
Caroline Ticee02657b2011-01-22 01:02:07 +0000464void
Greg Clayton4d122c42011-09-17 08:33:22 +0000465Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000466{
467 if (debugger_sp.get() == NULL)
468 return;
469
Jim Ingham8314c522011-09-15 21:36:42 +0000470 debugger_sp->Clear();
471
Greg Claytonc15f55e2012-03-30 20:53:46 +0000472 if (g_shared_debugger_refcount > 0)
Caroline Ticee02657b2011-01-22 01:02:07 +0000473 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000474 Mutex::Locker locker (GetDebuggerListMutex ());
475 DebuggerList &debugger_list = GetDebuggerList ();
476 DebuggerList::iterator pos, end = debugger_list.end();
477 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000478 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000479 if ((*pos).get() == debugger_sp.get())
480 {
481 debugger_list.erase (pos);
482 return;
483 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000484 }
485 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000486}
487
Greg Clayton4d122c42011-09-17 08:33:22 +0000488DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000489Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
490{
Greg Clayton4d122c42011-09-17 08:33:22 +0000491 DebuggerSP debugger_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000492 if (g_shared_debugger_refcount > 0)
493 {
494 Mutex::Locker locker (GetDebuggerListMutex ());
495 DebuggerList &debugger_list = GetDebuggerList();
496 DebuggerList::iterator pos, end = debugger_list.end();
497
498 for (pos = debugger_list.begin(); pos != end; ++pos)
499 {
500 if ((*pos).get()->m_instance_name == instance_name)
501 {
502 debugger_sp = *pos;
503 break;
504 }
505 }
506 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000507 return debugger_sp;
508}
Greg Clayton66111032010-06-23 01:19:29 +0000509
510TargetSP
511Debugger::FindTargetWithProcessID (lldb::pid_t pid)
512{
Greg Clayton4d122c42011-09-17 08:33:22 +0000513 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000514 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000515 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000516 Mutex::Locker locker (GetDebuggerListMutex ());
517 DebuggerList &debugger_list = GetDebuggerList();
518 DebuggerList::iterator pos, end = debugger_list.end();
519 for (pos = debugger_list.begin(); pos != end; ++pos)
520 {
521 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
522 if (target_sp)
523 break;
524 }
Greg Clayton66111032010-06-23 01:19:29 +0000525 }
526 return target_sp;
527}
528
Greg Claytone4e45922011-11-16 05:37:56 +0000529TargetSP
530Debugger::FindTargetWithProcess (Process *process)
531{
532 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000533 if (g_shared_debugger_refcount > 0)
Greg Claytone4e45922011-11-16 05:37:56 +0000534 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000535 Mutex::Locker locker (GetDebuggerListMutex ());
536 DebuggerList &debugger_list = GetDebuggerList();
537 DebuggerList::iterator pos, end = debugger_list.end();
538 for (pos = debugger_list.begin(); pos != end; ++pos)
539 {
540 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
541 if (target_sp)
542 break;
543 }
Greg Claytone4e45922011-11-16 05:37:56 +0000544 }
545 return target_sp;
546}
547
Jim Ingham228063c2012-02-21 02:23:08 +0000548Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000549 UserID (g_unique_id++),
Greg Clayton67cc0632012-08-22 17:17:09 +0000550 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
Greg Claytond46c87a2010-12-04 02:39:47 +0000551 m_input_comm("debugger.input"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000552 m_input_file (),
553 m_output_file (),
554 m_error_file (),
Jim Inghamc5917d92012-11-30 20:23:19 +0000555 m_terminal_state (),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000556 m_target_list (*this),
Greg Claytonded470d2011-03-19 01:12:21 +0000557 m_platform_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558 m_listener ("lldb.Debugger"),
Greg Clayton9585fbf2013-03-19 00:20:55 +0000559 m_source_manager_ap(),
Jim Inghame37d6052011-09-13 00:29:56 +0000560 m_source_file_cache(),
Greg Clayton66111032010-06-23 01:19:29 +0000561 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000562 m_input_reader_stack (),
Greg Clayton67cc0632012-08-22 17:17:09 +0000563 m_input_reader_data (),
564 m_instance_name()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565{
Greg Clayton67cc0632012-08-22 17:17:09 +0000566 char instance_cstr[256];
567 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
568 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000569 if (log_callback)
570 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000571 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000572 // Always add our default platform to the platform list
573 PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
574 assert (default_platform_sp.get());
575 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000576
Greg Clayton754a9362012-08-23 00:22:02 +0000577 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000578 m_collection_sp->AppendProperty (ConstString("target"),
579 ConstString("Settings specify to debugging targets."),
580 true,
581 Target::GetGlobalProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000582 if (m_command_interpreter_ap.get())
583 {
584 m_collection_sp->AppendProperty (ConstString("interpreter"),
585 ConstString("Settings specify to the debugger's command interpreter."),
586 true,
587 m_command_interpreter_ap->GetValueProperties());
588 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000589 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
590 term_width->SetMinimumValue(10);
591 term_width->SetMaximumValue(1024);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592}
593
594Debugger::~Debugger ()
595{
Jim Ingham8314c522011-09-15 21:36:42 +0000596 Clear();
597}
598
599void
600Debugger::Clear()
601{
Caroline Tice3d6086f2010-12-20 18:35:50 +0000602 CleanUpInputReaders();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000603 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000604 int num_targets = m_target_list.GetNumTargets();
605 for (int i = 0; i < num_targets; i++)
606 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000607 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
608 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000609 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000610 ProcessSP process_sp (target_sp->GetProcessSP());
611 if (process_sp)
Jim Ingham1fd07052013-02-27 19:13:05 +0000612 process_sp->Finalize();
Greg Claytonccbc08e2012-01-14 17:04:19 +0000613 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000614 }
Greg Clayton66111032010-06-23 01:19:29 +0000615 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000616 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000617
618 // Close the input file _before_ we close the input read communications class
619 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000620 m_terminal_state.Clear();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000621 GetInputFile().Close ();
622 // Now that we have closed m_input_file, we can now tell our input communication
623 // class to close down. Its read thread should quickly exit after we close
624 // the input file handle above.
625 m_input_comm.Clear ();
Jim Ingham8314c522011-09-15 21:36:42 +0000626}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627
628bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000629Debugger::GetCloseInputOnEOF () const
630{
631 return m_input_comm.GetCloseOnEOF();
632}
633
634void
635Debugger::SetCloseInputOnEOF (bool b)
636{
637 m_input_comm.SetCloseOnEOF(b);
638}
639
640bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641Debugger::GetAsyncExecution ()
642{
Greg Clayton66111032010-06-23 01:19:29 +0000643 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000644}
645
646void
647Debugger::SetAsyncExecution (bool async_execution)
648{
Greg Clayton66111032010-06-23 01:19:29 +0000649 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650}
651
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652
653void
654Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
655{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000656 File &in_file = GetInputFile();
657 in_file.SetStream (fh, tranfer_ownership);
658 if (in_file.IsValid() == false)
659 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000660
661 // Disconnect from any old connection if we had one
662 m_input_comm.Disconnect ();
Greg Clayton32720b52012-01-14 20:47:38 +0000663 // Pass false as the second argument to ConnectionFileDescriptor below because
664 // our "in_file" above will already take ownership if requested and we don't
665 // want to objects trying to own and close a file descriptor.
666 m_input_comm.SetConnection (new ConnectionFileDescriptor (in_file.GetDescriptor(), false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000667 m_input_comm.SetReadThreadBytesReceivedCallback (Debugger::DispatchInputCallback, this);
Jim Inghamc5917d92012-11-30 20:23:19 +0000668
669 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
670 SaveInputTerminalState ();
671
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000672 Error error;
673 if (m_input_comm.StartReadThread (&error) == false)
674 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000675 File &err_file = GetErrorFile();
676
677 err_file.Printf ("error: failed to main input read thread: %s", error.AsCString() ? error.AsCString() : "unkown error");
678 exit(1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000679 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000680}
681
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682void
683Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
684{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000685 File &out_file = GetOutputFile();
686 out_file.SetStream (fh, tranfer_ownership);
687 if (out_file.IsValid() == false)
688 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000689
Enrico Granatab5887262012-10-29 21:18:03 +0000690 // do not create the ScriptInterpreter just for setting the output file handle
691 // as the constructor will know how to do the right thing on its own
692 const bool can_create = false;
693 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
694 if (script_interpreter)
695 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696}
697
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000698void
699Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
700{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000701 File &err_file = GetErrorFile();
702 err_file.SetStream (fh, tranfer_ownership);
703 if (err_file.IsValid() == false)
704 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000705}
706
Jim Inghamc5917d92012-11-30 20:23:19 +0000707void
708Debugger::SaveInputTerminalState ()
709{
710 File &in_file = GetInputFile();
711 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
712 m_terminal_state.Save(in_file.GetDescriptor(), true);
713}
714
715void
716Debugger::RestoreInputTerminalState ()
717{
718 m_terminal_state.Restore();
719}
720
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000721ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000722Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000723{
724 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000725 TargetSP target_sp(GetSelectedTarget());
726 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727
728 if (target_sp)
729 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000730 ProcessSP process_sp (target_sp->GetProcessSP());
731 exe_ctx.SetProcessSP (process_sp);
732 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000733 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000734 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
735 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000737 exe_ctx.SetThreadSP (thread_sp);
738 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
739 if (exe_ctx.GetFramePtr() == NULL)
740 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741 }
742 }
743 }
744 return exe_ctx;
745
746}
747
Caroline Ticeb44880c2011-02-10 01:15:13 +0000748InputReaderSP
749Debugger::GetCurrentInputReader ()
750{
751 InputReaderSP reader_sp;
752
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000753 if (!m_input_reader_stack.IsEmpty())
Caroline Ticeb44880c2011-02-10 01:15:13 +0000754 {
755 // Clear any finished readers from the stack
756 while (CheckIfTopInputReaderIsDone()) ;
757
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000758 if (!m_input_reader_stack.IsEmpty())
759 reader_sp = m_input_reader_stack.Top();
Caroline Ticeb44880c2011-02-10 01:15:13 +0000760 }
761
762 return reader_sp;
763}
764
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000765void
766Debugger::DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len)
767{
Caroline Ticeefed6132010-11-19 20:47:54 +0000768 if (bytes_len > 0)
769 ((Debugger *)baton)->DispatchInput ((char *)bytes, bytes_len);
770 else
771 ((Debugger *)baton)->DispatchInputEndOfFile ();
772}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773
774
775void
776Debugger::DispatchInput (const char *bytes, size_t bytes_len)
777{
Caroline Ticeefed6132010-11-19 20:47:54 +0000778 if (bytes == NULL || bytes_len == 0)
779 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000780
781 WriteToDefaultReader (bytes, bytes_len);
782}
783
784void
Caroline Ticeefed6132010-11-19 20:47:54 +0000785Debugger::DispatchInputInterrupt ()
786{
787 m_input_reader_data.clear();
788
Caroline Ticeb44880c2011-02-10 01:15:13 +0000789 InputReaderSP reader_sp (GetCurrentInputReader ());
790 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000791 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000792 reader_sp->Notify (eInputReaderInterrupt);
Caroline Ticeefed6132010-11-19 20:47:54 +0000793
Caroline Ticeb44880c2011-02-10 01:15:13 +0000794 // If notifying the reader of the interrupt finished the reader, we should pop it off the stack.
Caroline Ticeefed6132010-11-19 20:47:54 +0000795 while (CheckIfTopInputReaderIsDone ()) ;
796 }
797}
798
799void
800Debugger::DispatchInputEndOfFile ()
801{
802 m_input_reader_data.clear();
803
Caroline Ticeb44880c2011-02-10 01:15:13 +0000804 InputReaderSP reader_sp (GetCurrentInputReader ());
805 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000806 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000807 reader_sp->Notify (eInputReaderEndOfFile);
Caroline Ticeefed6132010-11-19 20:47:54 +0000808
Caroline Ticeb44880c2011-02-10 01:15:13 +0000809 // If notifying the reader of the end-of-file finished the reader, we should pop it off the stack.
Caroline Ticeefed6132010-11-19 20:47:54 +0000810 while (CheckIfTopInputReaderIsDone ()) ;
811 }
812}
813
814void
Caroline Tice3d6086f2010-12-20 18:35:50 +0000815Debugger::CleanUpInputReaders ()
816{
817 m_input_reader_data.clear();
818
Caroline Ticeb44880c2011-02-10 01:15:13 +0000819 // The bottom input reader should be the main debugger input reader. We do not want to close that one here.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000820 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000821 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000822 InputReaderSP reader_sp (GetCurrentInputReader ());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000823 if (reader_sp)
824 {
825 reader_sp->Notify (eInputReaderEndOfFile);
826 reader_sp->SetIsDone (true);
827 }
828 }
829}
830
831void
Caroline Tice969ed3d2011-05-02 20:41:46 +0000832Debugger::NotifyTopInputReader (InputReaderAction notification)
833{
834 InputReaderSP reader_sp (GetCurrentInputReader());
835 if (reader_sp)
836 {
837 reader_sp->Notify (notification);
838
839 // Flush out any input readers that are done.
840 while (CheckIfTopInputReaderIsDone ())
841 /* Do nothing. */;
842 }
843}
844
Caroline Tice9088b062011-05-09 23:06:58 +0000845bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000846Debugger::InputReaderIsTopReader (const InputReaderSP& reader_sp)
Caroline Tice9088b062011-05-09 23:06:58 +0000847{
Caroline Ticed61c10b2011-06-16 16:27:19 +0000848 InputReaderSP top_reader_sp (GetCurrentInputReader());
Caroline Tice9088b062011-05-09 23:06:58 +0000849
Caroline Ticed61c10b2011-06-16 16:27:19 +0000850 return (reader_sp.get() == top_reader_sp.get());
Caroline Tice9088b062011-05-09 23:06:58 +0000851}
852
853
Caroline Tice969ed3d2011-05-02 20:41:46 +0000854void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000855Debugger::WriteToDefaultReader (const char *bytes, size_t bytes_len)
856{
857 if (bytes && bytes_len)
858 m_input_reader_data.append (bytes, bytes_len);
859
860 if (m_input_reader_data.empty())
861 return;
862
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000863 while (!m_input_reader_stack.IsEmpty() && !m_input_reader_data.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000864 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000865 // Get the input reader from the top of the stack
Caroline Ticeb44880c2011-02-10 01:15:13 +0000866 InputReaderSP reader_sp (GetCurrentInputReader ());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000867 if (!reader_sp)
868 break;
869
Greg Clayton471b31c2010-07-20 22:52:08 +0000870 size_t bytes_handled = reader_sp->HandleRawBytes (m_input_reader_data.c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000871 m_input_reader_data.size());
872 if (bytes_handled)
873 {
874 m_input_reader_data.erase (0, bytes_handled);
875 }
876 else
877 {
878 // No bytes were handled, we might not have reached our
879 // granularity, just return and wait for more data
880 break;
881 }
882 }
883
Caroline Ticeb44880c2011-02-10 01:15:13 +0000884 // Flush out any input readers that are done.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000885 while (CheckIfTopInputReaderIsDone ())
886 /* Do nothing. */;
887
888}
889
890void
891Debugger::PushInputReader (const InputReaderSP& reader_sp)
892{
893 if (!reader_sp)
894 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000895
896 // Deactivate the old top reader
897 InputReaderSP top_reader_sp (GetCurrentInputReader ());
898
899 if (top_reader_sp)
900 top_reader_sp->Notify (eInputReaderDeactivate);
901
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000902 m_input_reader_stack.Push (reader_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000903 reader_sp->Notify (eInputReaderActivate);
904 ActivateInputReader (reader_sp);
905}
906
907bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000908Debugger::PopInputReader (const InputReaderSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000909{
910 bool result = false;
911
912 // The reader on the stop of the stack is done, so let the next
913 // read on the stack referesh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000914 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000915 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000916 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000917 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000918
919 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
920 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000921 m_input_reader_stack.Pop ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000922 reader_sp->Notify (eInputReaderDeactivate);
923 reader_sp->Notify (eInputReaderDone);
924 result = true;
925
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000926 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000927 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000928 reader_sp = m_input_reader_stack.Top();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000929 if (reader_sp)
930 {
931 ActivateInputReader (reader_sp);
932 reader_sp->Notify (eInputReaderReactivate);
933 }
934 }
935 }
936 }
937 return result;
938}
939
940bool
941Debugger::CheckIfTopInputReaderIsDone ()
942{
943 bool result = false;
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000944 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000945 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000946 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000947 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000948
949 if (reader_sp && reader_sp->IsDone())
950 {
951 result = true;
952 PopInputReader (reader_sp);
953 }
954 }
955 return result;
956}
957
958void
959Debugger::ActivateInputReader (const InputReaderSP &reader_sp)
960{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000961 int input_fd = m_input_file.GetFile().GetDescriptor();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000962
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000963 if (input_fd >= 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000964 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000965 Terminal tty(input_fd);
Greg Claytona3406612011-02-07 23:24:47 +0000966
967 tty.SetEcho(reader_sp->GetEcho());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000968
Greg Claytona3406612011-02-07 23:24:47 +0000969 switch (reader_sp->GetGranularity())
970 {
971 case eInputReaderGranularityByte:
972 case eInputReaderGranularityWord:
973 tty.SetCanonical (false);
974 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000975
Greg Claytona3406612011-02-07 23:24:47 +0000976 case eInputReaderGranularityLine:
977 case eInputReaderGranularityAll:
978 tty.SetCanonical (true);
979 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000980
Greg Claytona3406612011-02-07 23:24:47 +0000981 default:
982 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000983 }
984 }
985}
Greg Clayton66111032010-06-23 01:19:29 +0000986
Jim Ingham5b52f0c2011-06-02 23:58:26 +0000987StreamSP
988Debugger::GetAsyncOutputStream ()
989{
990 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
991 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
992}
993
994StreamSP
995Debugger::GetAsyncErrorStream ()
996{
997 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
998 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
999}
1000
Greg Claytonc7bece562013-01-25 18:06:21 +00001001size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001002Debugger::GetNumDebuggers()
1003{
Greg Claytonc15f55e2012-03-30 20:53:46 +00001004 if (g_shared_debugger_refcount > 0)
1005 {
1006 Mutex::Locker locker (GetDebuggerListMutex ());
1007 return GetDebuggerList().size();
1008 }
1009 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001010}
1011
1012lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001013Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001014{
1015 DebuggerSP debugger_sp;
1016
Greg Claytonc15f55e2012-03-30 20:53:46 +00001017 if (g_shared_debugger_refcount > 0)
1018 {
1019 Mutex::Locker locker (GetDebuggerListMutex ());
1020 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001021
Greg Claytonc15f55e2012-03-30 20:53:46 +00001022 if (index < debugger_list.size())
1023 debugger_sp = debugger_list[index];
1024 }
1025
Enrico Granata061858c2012-02-15 02:34:21 +00001026 return debugger_sp;
1027}
1028
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001029DebuggerSP
1030Debugger::FindDebuggerWithID (lldb::user_id_t id)
1031{
Greg Clayton4d122c42011-09-17 08:33:22 +00001032 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001033
Greg Claytonc15f55e2012-03-30 20:53:46 +00001034 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001035 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001036 Mutex::Locker locker (GetDebuggerListMutex ());
1037 DebuggerList &debugger_list = GetDebuggerList();
1038 DebuggerList::iterator pos, end = debugger_list.end();
1039 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001040 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001041 if ((*pos).get()->GetID() == id)
1042 {
1043 debugger_sp = *pos;
1044 break;
1045 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001046 }
1047 }
1048 return debugger_sp;
1049}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001050
Greg Clayton1b654882010-09-19 02:33:57 +00001051static void
1052TestPromptFormats (StackFrame *frame)
1053{
1054 if (frame == NULL)
1055 return;
1056
1057 StreamString s;
1058 const char *prompt_format =
1059 "{addr = '${addr}'\n}"
1060 "{process.id = '${process.id}'\n}"
1061 "{process.name = '${process.name}'\n}"
1062 "{process.file.basename = '${process.file.basename}'\n}"
1063 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1064 "{thread.id = '${thread.id}'\n}"
1065 "{thread.index = '${thread.index}'\n}"
1066 "{thread.name = '${thread.name}'\n}"
1067 "{thread.queue = '${thread.queue}'\n}"
1068 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1069 "{target.arch = '${target.arch}'\n}"
1070 "{module.file.basename = '${module.file.basename}'\n}"
1071 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1072 "{file.basename = '${file.basename}'\n}"
1073 "{file.fullpath = '${file.fullpath}'\n}"
1074 "{frame.index = '${frame.index}'\n}"
1075 "{frame.pc = '${frame.pc}'\n}"
1076 "{frame.sp = '${frame.sp}'\n}"
1077 "{frame.fp = '${frame.fp}'\n}"
1078 "{frame.flags = '${frame.flags}'\n}"
1079 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1080 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1081 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1082 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1083 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1084 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1085 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1086 "{function.id = '${function.id}'\n}"
1087 "{function.name = '${function.name}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001088 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001089 "{function.addr-offset = '${function.addr-offset}'\n}"
1090 "{function.line-offset = '${function.line-offset}'\n}"
1091 "{function.pc-offset = '${function.pc-offset}'\n}"
1092 "{line.file.basename = '${line.file.basename}'\n}"
1093 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1094 "{line.number = '${line.number}'\n}"
1095 "{line.start-addr = '${line.start-addr}'\n}"
1096 "{line.end-addr = '${line.end-addr}'\n}"
1097;
1098
1099 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1100 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001101 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton1b654882010-09-19 02:33:57 +00001102 const char *end = NULL;
1103 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, &end))
1104 {
1105 printf("%s\n", s.GetData());
1106 }
1107 else
1108 {
1109 printf ("error: at '%s'\n", end);
1110 printf ("what we got: %s\n", s.GetData());
1111 }
1112}
1113
Enrico Granata9fc19442011-07-06 02:13:41 +00001114static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001115ScanFormatDescriptor (const char* var_name_begin,
1116 const char* var_name_end,
1117 const char** var_name_final,
1118 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +00001119 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +00001120 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +00001121{
Greg Clayton5160ce52013-03-27 23:08:40 +00001122 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001123 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +00001124 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +00001125 {
1126 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001127 log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +00001128 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +00001129 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001130 else
1131 {
1132 *var_name_final = *percent_position;
1133 char* format_name = new char[var_name_end-*var_name_final]; format_name[var_name_end-*var_name_final-1] = '\0';
1134 memcpy(format_name, *var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +00001135 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001136 log->Printf("ScanFormatDescriptor] parsing %s as a format descriptor", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +00001137 if ( !FormatManager::GetFormatFromCString(format_name,
1138 true,
1139 *custom_format) )
1140 {
Enrico Granatae992a082011-07-22 17:03:19 +00001141 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001142 log->Printf("ScanFormatDescriptor] %s is an unknown format", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +00001143 // if this is an @ sign, print ObjC description
Greg Clayton34132752011-07-06 04:07:21 +00001144 if (*format_name == '@')
Enrico Granata86cc9822012-03-19 22:58:49 +00001145 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
Enrico Granata9fc19442011-07-06 02:13:41 +00001146 // if this is a V, print the value using the default format
Enrico Granatae992a082011-07-22 17:03:19 +00001147 else if (*format_name == 'V')
Enrico Granata86cc9822012-03-19 22:58:49 +00001148 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatad55546b2011-07-22 00:16:08 +00001149 // if this is an L, print the location of the value
Enrico Granatae992a082011-07-22 17:03:19 +00001150 else if (*format_name == 'L')
Enrico Granata86cc9822012-03-19 22:58:49 +00001151 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
Enrico Granatad55546b2011-07-22 00:16:08 +00001152 // if this is an S, print the summary after all
Enrico Granatae992a082011-07-22 17:03:19 +00001153 else if (*format_name == 'S')
Enrico Granata86cc9822012-03-19 22:58:49 +00001154 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Enrico Granata5dfd49c2011-08-04 02:34:29 +00001155 else if (*format_name == '#')
Enrico Granata86cc9822012-03-19 22:58:49 +00001156 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001157 else if (*format_name == 'T')
Enrico Granata86cc9822012-03-19 22:58:49 +00001158 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
Enrico Granatae992a082011-07-22 17:03:19 +00001159 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001160 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +00001161 }
1162 // a good custom format tells us to print the value using it
1163 else
Enrico Granatae992a082011-07-22 17:03:19 +00001164 {
1165 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001166 log->Printf("ScanFormatDescriptor] will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001167 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001168 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001169 delete format_name;
1170 }
Enrico Granatae992a082011-07-22 17:03:19 +00001171 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001172 log->Printf("ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
Enrico Granatae992a082011-07-22 17:03:19 +00001173 *custom_format,
1174 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001175 return true;
1176}
1177
1178static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001179ScanBracketedRange (const char* var_name_begin,
1180 const char* var_name_end,
1181 const char* var_name_final,
1182 const char** open_bracket_position,
1183 const char** separator_position,
1184 const char** close_bracket_position,
1185 const char** var_name_final_if_array_range,
1186 int64_t* index_lower,
1187 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001188{
Greg Clayton5160ce52013-03-27 23:08:40 +00001189 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001190 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001191 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001192 {
1193 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1194 *close_bracket_position = ::strchr(*open_bracket_position,']');
1195 // as usual, we assume that [] will come before %
1196 //printf("trying to expand a []\n");
1197 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001198 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001199 {
Enrico Granatae992a082011-07-22 17:03:19 +00001200 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001201 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001202 *index_lower = 0;
1203 }
1204 else if (*separator_position == NULL || *separator_position > var_name_end)
1205 {
1206 char *end = NULL;
1207 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1208 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001209 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001210 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001211 }
Greg Clayton34132752011-07-06 04:07:21 +00001212 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001213 {
1214 char *end = NULL;
1215 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1216 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001217 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001218 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001219 }
1220 else
Enrico Granatae992a082011-07-22 17:03:19 +00001221 {
1222 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001223 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001224 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001225 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001226 if (*index_lower > *index_higher && *index_higher > 0)
1227 {
Enrico Granatae992a082011-07-22 17:03:19 +00001228 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001229 log->Printf("[ScanBracketedRange] swapping indices");
Greg Claytonc7bece562013-01-25 18:06:21 +00001230 int64_t temp = *index_lower;
Enrico Granata9fc19442011-07-06 02:13:41 +00001231 *index_lower = *index_higher;
1232 *index_higher = temp;
1233 }
1234 }
Enrico Granatae992a082011-07-22 17:03:19 +00001235 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001236 log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001237 return true;
1238}
1239
Enrico Granata9fc19442011-07-06 02:13:41 +00001240static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001241ExpandIndexedExpression (ValueObject* valobj,
Greg Claytonc7bece562013-01-25 18:06:21 +00001242 size_t index,
Enrico Granatadc940732011-08-23 00:32:52 +00001243 StackFrame* frame,
1244 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001245{
Greg Clayton5160ce52013-03-27 23:08:40 +00001246 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001247 const char* ptr_deref_format = "[%d]";
Enrico Granata599171a2013-02-01 23:59:44 +00001248 std::string ptr_deref_buffer(10,0);
1249 ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001250 if (log)
Enrico Granata599171a2013-02-01 23:59:44 +00001251 log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001252 const char* first_unparsed;
1253 ValueObject::GetValueForExpressionPathOptions options;
1254 ValueObject::ExpressionPathEndResultType final_value_type;
1255 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001256 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granata599171a2013-02-01 23:59:44 +00001257 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001258 &first_unparsed,
1259 &reason_to_stop,
1260 &final_value_type,
1261 options,
1262 &what_next);
1263 if (!item)
1264 {
Enrico Granatae992a082011-07-22 17:03:19 +00001265 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001266 log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001267 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001268 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001269 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001270 else
1271 {
Enrico Granatae992a082011-07-22 17:03:19 +00001272 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001273 log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001274 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001275 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001276 }
1277 return item;
1278}
1279
Greg Clayton1b654882010-09-19 02:33:57 +00001280bool
1281Debugger::FormatPrompt
1282(
1283 const char *format,
1284 const SymbolContext *sc,
1285 const ExecutionContext *exe_ctx,
1286 const Address *addr,
1287 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001288 const char **end,
Enrico Granatac482a192011-08-17 22:13:59 +00001289 ValueObject* valobj
Greg Clayton1b654882010-09-19 02:33:57 +00001290)
1291{
Enrico Granatac482a192011-08-17 22:13:59 +00001292 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001293 bool success = true;
1294 const char *p;
Greg Clayton5160ce52013-03-27 23:08:40 +00001295 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Greg Clayton1b654882010-09-19 02:33:57 +00001296 for (p = format; *p != '\0'; ++p)
1297 {
Enrico Granatac482a192011-08-17 22:13:59 +00001298 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001299 {
Enrico Granatac482a192011-08-17 22:13:59 +00001300 valobj = realvalobj;
1301 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001302 }
Greg Clayton1b654882010-09-19 02:33:57 +00001303 size_t non_special_chars = ::strcspn (p, "${}\\");
1304 if (non_special_chars > 0)
1305 {
1306 if (success)
1307 s.Write (p, non_special_chars);
1308 p += non_special_chars;
1309 }
1310
1311 if (*p == '\0')
1312 {
1313 break;
1314 }
1315 else if (*p == '{')
1316 {
1317 // Start a new scope that must have everything it needs if it is to
1318 // to make it into the final output stream "s". If you want to make
1319 // a format that only prints out the function or symbol name if there
1320 // is one in the symbol context you can use:
1321 // "{function =${function.name}}"
1322 // The first '{' starts a new scope that end with the matching '}' at
1323 // the end of the string. The contents "function =${function.name}"
1324 // will then be evaluated and only be output if there is a function
1325 // or symbol with a valid name.
1326 StreamString sub_strm;
1327
1328 ++p; // Skip the '{'
1329
Enrico Granatac482a192011-08-17 22:13:59 +00001330 if (FormatPrompt (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
Greg Clayton1b654882010-09-19 02:33:57 +00001331 {
1332 // The stream had all it needed
1333 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1334 }
1335 if (*p != '}')
1336 {
1337 success = false;
1338 break;
1339 }
1340 }
1341 else if (*p == '}')
1342 {
1343 // End of a enclosing scope
1344 break;
1345 }
1346 else if (*p == '$')
1347 {
1348 // We have a prompt variable to print
1349 ++p;
1350 if (*p == '{')
1351 {
1352 ++p;
1353 const char *var_name_begin = p;
1354 const char *var_name_end = ::strchr (p, '}');
1355
1356 if (var_name_end && var_name_begin < var_name_end)
1357 {
1358 // if we have already failed to parse, skip this variable
1359 if (success)
1360 {
1361 const char *cstr = NULL;
1362 Address format_addr;
1363 bool calculate_format_addr_function_offset = false;
1364 // Set reg_kind and reg_num to invalid values
1365 RegisterKind reg_kind = kNumRegisterKinds;
1366 uint32_t reg_num = LLDB_INVALID_REGNUM;
1367 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001368 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001369 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001370 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001371 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1372 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001373
Greg Clayton1b654882010-09-19 02:33:57 +00001374 // Each variable must set success to true below...
1375 bool var_success = false;
1376 switch (var_name_begin[0])
1377 {
Enrico Granata4becb372011-06-29 22:27:15 +00001378 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001379 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001380 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001381 {
Enrico Granatac482a192011-08-17 22:13:59 +00001382 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001383 break;
1384
Enrico Granatac3e320a2011-08-02 17:27:39 +00001385 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001386 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001387
Enrico Granata6f3533f2011-07-29 19:53:35 +00001388 // check for *var and *svar
1389 if (*var_name_begin == '*')
1390 {
1391 do_deref_pointer = true;
1392 var_name_begin++;
1393 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001394
1395 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001396 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001397
Enrico Granata6f3533f2011-07-29 19:53:35 +00001398 if (*var_name_begin == 's')
1399 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001400 if (!valobj->IsSynthetic())
1401 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001402 if (!valobj)
1403 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001404 var_name_begin++;
1405 }
1406
Enrico Granatac3e320a2011-08-02 17:27:39 +00001407 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001408 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001409
Enrico Granata6f3533f2011-07-29 19:53:35 +00001410 // should be a 'v' by now
1411 if (*var_name_begin != 'v')
1412 break;
1413
Enrico Granatac3e320a2011-08-02 17:27:39 +00001414 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001415 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001416
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001417 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001418 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001419 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001420 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001421 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001422 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001423 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001424 const char* var_name_final = NULL;
1425 const char* var_name_final_if_array_range = NULL;
1426 const char* close_bracket_position = NULL;
1427 int64_t index_lower = -1;
1428 int64_t index_higher = -1;
1429 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001430 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001431 bool was_plain_var = false;
1432 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001433 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001434
Enrico Granatac482a192011-08-17 22:13:59 +00001435 if (!valobj) break;
1436 // simplest case ${var}, just print valobj's value
Greg Clayton34132752011-07-06 04:07:21 +00001437 if (::strncmp (var_name_begin, "var}", strlen("var}")) == 0)
Enrico Granata4becb372011-06-29 22:27:15 +00001438 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001439 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001440 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001441 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001442 }
1443 else if (::strncmp(var_name_begin,"var%",strlen("var%")) == 0)
1444 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001445 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001446 // this is a variable with some custom format applied to it
1447 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001448 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001449 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001450 ScanFormatDescriptor (var_name_begin,
1451 var_name_end,
1452 &var_name_final,
1453 &percent_position,
1454 &custom_format,
1455 &val_obj_display);
1456 }
1457 // this is ${var.something} or multiple .something nested
1458 else if (::strncmp (var_name_begin, "var", strlen("var")) == 0)
1459 {
Enrico Granataa777dc22012-05-08 21:49:57 +00001460 if (::strncmp(var_name_begin, "var[", strlen("var[")) == 0)
1461 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001462 const char* percent_position;
1463 ScanFormatDescriptor (var_name_begin,
1464 var_name_end,
1465 &var_name_final,
1466 &percent_position,
1467 &custom_format,
1468 &val_obj_display);
1469
1470 const char* open_bracket_position;
1471 const char* separator_position;
1472 ScanBracketedRange (var_name_begin,
1473 var_name_end,
1474 var_name_final,
1475 &open_bracket_position,
1476 &separator_position,
1477 &close_bracket_position,
1478 &var_name_final_if_array_range,
1479 &index_lower,
1480 &index_higher);
1481
1482 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001483
Enrico Granata599171a2013-02-01 23:59:44 +00001484 std::string expr_path(var_name_final-var_name_begin-1,0);
1485 memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3);
1486
1487 if (log)
1488 log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str());
1489
1490 target = valobj->GetValueForExpressionPath(expr_path.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001491 &first_unparsed,
1492 &reason_to_stop,
1493 &final_value_type,
1494 options,
1495 &what_next).get();
1496
1497 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001498 {
Enrico Granatae992a082011-07-22 17:03:19 +00001499 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001500 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001501 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001502 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001503 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001504 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001505 else
1506 {
Enrico Granatae992a082011-07-22 17:03:19 +00001507 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001508 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001509 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001510 first_unparsed, reason_to_stop, final_value_type);
1511 }
Enrico Granata4becb372011-06-29 22:27:15 +00001512 }
Greg Clayton34132752011-07-06 04:07:21 +00001513 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001514 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001515
Enrico Granata86cc9822012-03-19 22:58:49 +00001516 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1517 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001518
Enrico Granata86cc9822012-03-19 22:58:49 +00001519 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001520
Enrico Granataa7187d02011-07-06 19:27:11 +00001521 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001522 {
Greg Clayton34132752011-07-06 04:07:21 +00001523 // I have not deref-ed yet, let's do it
1524 // this happens when we are not going through GetValueForVariableExpressionPath
1525 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001526 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001527 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001528 if (error.Fail())
1529 {
1530 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001531 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
Enrico Granatadc940732011-08-23 00:32:52 +00001532 break;
1533 }
Greg Clayton34132752011-07-06 04:07:21 +00001534 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001535 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001536
Enrico Granataa777dc22012-05-08 21:49:57 +00001537 // <rdar://problem/11338654>
1538 // we do not want to use the summary for a bitfield of type T:n
1539 // if we were originally dealing with just a T - that would get
1540 // us into an endless recursion
1541 if (target->IsBitfield() && was_var_indexed)
1542 {
1543 // TODO: check for a (T:n)-specific summary - we should still obey that
1544 StreamString bitfield_name;
1545 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1546 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1547 if (!DataVisualization::GetSummaryForType(type_sp))
1548 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1549 }
1550
Enrico Granata85933ed2011-08-18 16:38:26 +00001551 // TODO use flags for these
Greg Clayton4ef877f2012-12-06 02:33:54 +00001552 bool is_array = ClangASTContext::IsArrayType(target->GetClangType(), NULL, NULL, NULL);
Enrico Granataf4efecd2011-07-12 22:56:10 +00001553 bool is_pointer = ClangASTContext::IsPointerType(target->GetClangType());
Enrico Granata85933ed2011-08-18 16:38:26 +00001554 bool is_aggregate = ClangASTContext::IsAggregateType(target->GetClangType());
Enrico Granataf4efecd2011-07-12 22:56:10 +00001555
Enrico Granata86cc9822012-03-19 22:58:49 +00001556 if ((is_array || is_pointer) && (!is_array_range) && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) // this should be wrong, but there are some exceptions
Enrico Granataf4efecd2011-07-12 22:56:10 +00001557 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001558 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001559 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001560 log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001561
Greg Clayton5088c482013-03-25 21:06:13 +00001562 if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001563 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001564 // try to use the special cases
1565 var_success = target->DumpPrintableRepresentation(str_temp,
1566 val_obj_display,
1567 custom_format);
1568 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001569 log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001570
1571 // should not happen
Greg Clayton5088c482013-03-25 21:06:13 +00001572 if (var_success)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001573 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001574 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001575 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001576 }
1577 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001578 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001579 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001580 {
1581 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1582 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001583 else if (is_pointer) // if pointer, value is the address stored
1584 {
Greg Clayton23f59502012-07-17 03:23:13 +00001585 target->DumpPrintableRepresentation (s,
1586 val_obj_display,
1587 custom_format,
1588 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001589 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001590 var_success = true;
1591 break;
1592 }
1593 }
1594
1595 // if directly trying to print ${var}, and this is an aggregate, display a nice
1596 // type @ location message
1597 if (is_aggregate && was_plain_var)
1598 {
1599 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1600 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001601 break;
1602 }
1603
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001604 // if directly trying to print ${var%V}, and this is an aggregate, do not let the user do it
Enrico Granata86cc9822012-03-19 22:58:49 +00001605 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001606 {
1607 s << "<invalid use of aggregate type>";
1608 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001609 break;
1610 }
Greg Clayton34132752011-07-06 04:07:21 +00001611
1612 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001613 {
1614 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001615 log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001616 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001617 }
Greg Clayton34132752011-07-06 04:07:21 +00001618 else
Enrico Granatae992a082011-07-22 17:03:19 +00001619 {
1620 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001621 log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001622 if (!is_array && !is_pointer)
1623 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001624 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001625 log->Printf("[Debugger::FormatPrompt] handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001626 const char* special_directions = NULL;
1627 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001628 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1629 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001630 ConstString additional_data;
1631 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1632 special_directions_writer.Printf("${%svar%s}",
1633 do_deref_pointer ? "*" : "",
1634 additional_data.GetCString());
1635 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001636 }
1637
1638 // let us display items index_lower thru index_higher of this array
1639 s.PutChar('[');
1640 var_success = true;
1641
1642 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001643 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001644
Greg Claytoncc4d0142012-02-17 07:49:44 +00001645 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001646
Greg Clayton34132752011-07-06 04:07:21 +00001647 for (;index_lower<=index_higher;index_lower++)
1648 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001649 ValueObject* item = ExpandIndexedExpression (target,
1650 index_lower,
1651 exe_ctx->GetFramePtr(),
1652 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001653
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001654 if (!item)
1655 {
Enrico Granatae992a082011-07-22 17:03:19 +00001656 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001657 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001658 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001659 else
1660 {
Enrico Granatae992a082011-07-22 17:03:19 +00001661 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001662 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001663 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001664
Greg Clayton34132752011-07-06 04:07:21 +00001665 if (!special_directions)
1666 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1667 else
1668 var_success &= FormatPrompt(special_directions, sc, exe_ctx, addr, s, NULL, item);
1669
Enrico Granata22c55d12011-08-12 02:00:06 +00001670 if (--max_num_children == 0)
1671 {
1672 s.PutCString(", ...");
1673 break;
1674 }
1675
Greg Clayton34132752011-07-06 04:07:21 +00001676 if (index_lower < index_higher)
1677 s.PutChar(',');
1678 }
1679 s.PutChar(']');
1680 }
Enrico Granata4becb372011-06-29 22:27:15 +00001681 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001682 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001683 case 'a':
1684 if (::strncmp (var_name_begin, "addr}", strlen("addr}")) == 0)
1685 {
1686 if (addr && addr->IsValid())
1687 {
1688 var_success = true;
1689 format_addr = *addr;
1690 }
1691 }
Greg Clayton5a314712011-10-14 07:41:33 +00001692 else if (::strncmp (var_name_begin, "ansi.", strlen("ansi.")) == 0)
1693 {
1694 var_success = true;
1695 var_name_begin += strlen("ansi."); // Skip the "ansi."
1696 if (::strncmp (var_name_begin, "fg.", strlen("fg.")) == 0)
1697 {
1698 var_name_begin += strlen("fg."); // Skip the "fg."
1699 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1700 {
1701 s.Printf ("%s%s%s",
1702 lldb_utility::ansi::k_escape_start,
1703 lldb_utility::ansi::k_fg_black,
1704 lldb_utility::ansi::k_escape_end);
1705 }
1706 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1707 {
1708 s.Printf ("%s%s%s",
1709 lldb_utility::ansi::k_escape_start,
1710 lldb_utility::ansi::k_fg_red,
1711 lldb_utility::ansi::k_escape_end);
1712 }
1713 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1714 {
1715 s.Printf ("%s%s%s",
1716 lldb_utility::ansi::k_escape_start,
1717 lldb_utility::ansi::k_fg_green,
1718 lldb_utility::ansi::k_escape_end);
1719 }
1720 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1721 {
1722 s.Printf ("%s%s%s",
1723 lldb_utility::ansi::k_escape_start,
1724 lldb_utility::ansi::k_fg_yellow,
1725 lldb_utility::ansi::k_escape_end);
1726 }
1727 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1728 {
1729 s.Printf ("%s%s%s",
1730 lldb_utility::ansi::k_escape_start,
1731 lldb_utility::ansi::k_fg_blue,
1732 lldb_utility::ansi::k_escape_end);
1733 }
1734 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1735 {
1736 s.Printf ("%s%s%s",
1737 lldb_utility::ansi::k_escape_start,
1738 lldb_utility::ansi::k_fg_purple,
1739 lldb_utility::ansi::k_escape_end);
1740 }
1741 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1742 {
1743 s.Printf ("%s%s%s",
1744 lldb_utility::ansi::k_escape_start,
1745 lldb_utility::ansi::k_fg_cyan,
1746 lldb_utility::ansi::k_escape_end);
1747 }
1748 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1749 {
1750 s.Printf ("%s%s%s",
1751 lldb_utility::ansi::k_escape_start,
1752 lldb_utility::ansi::k_fg_white,
1753 lldb_utility::ansi::k_escape_end);
1754 }
1755 else
1756 {
1757 var_success = false;
1758 }
1759 }
1760 else if (::strncmp (var_name_begin, "bg.", strlen("bg.")) == 0)
1761 {
1762 var_name_begin += strlen("bg."); // Skip the "bg."
1763 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1764 {
1765 s.Printf ("%s%s%s",
1766 lldb_utility::ansi::k_escape_start,
1767 lldb_utility::ansi::k_bg_black,
1768 lldb_utility::ansi::k_escape_end);
1769 }
1770 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1771 {
1772 s.Printf ("%s%s%s",
1773 lldb_utility::ansi::k_escape_start,
1774 lldb_utility::ansi::k_bg_red,
1775 lldb_utility::ansi::k_escape_end);
1776 }
1777 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1778 {
1779 s.Printf ("%s%s%s",
1780 lldb_utility::ansi::k_escape_start,
1781 lldb_utility::ansi::k_bg_green,
1782 lldb_utility::ansi::k_escape_end);
1783 }
1784 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1785 {
1786 s.Printf ("%s%s%s",
1787 lldb_utility::ansi::k_escape_start,
1788 lldb_utility::ansi::k_bg_yellow,
1789 lldb_utility::ansi::k_escape_end);
1790 }
1791 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1792 {
1793 s.Printf ("%s%s%s",
1794 lldb_utility::ansi::k_escape_start,
1795 lldb_utility::ansi::k_bg_blue,
1796 lldb_utility::ansi::k_escape_end);
1797 }
1798 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1799 {
1800 s.Printf ("%s%s%s",
1801 lldb_utility::ansi::k_escape_start,
1802 lldb_utility::ansi::k_bg_purple,
1803 lldb_utility::ansi::k_escape_end);
1804 }
1805 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1806 {
1807 s.Printf ("%s%s%s",
1808 lldb_utility::ansi::k_escape_start,
1809 lldb_utility::ansi::k_bg_cyan,
1810 lldb_utility::ansi::k_escape_end);
1811 }
1812 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1813 {
1814 s.Printf ("%s%s%s",
1815 lldb_utility::ansi::k_escape_start,
1816 lldb_utility::ansi::k_bg_white,
1817 lldb_utility::ansi::k_escape_end);
1818 }
1819 else
1820 {
1821 var_success = false;
1822 }
1823 }
1824 else if (::strncmp (var_name_begin, "normal}", strlen ("normal}")) == 0)
1825 {
1826 s.Printf ("%s%s%s",
1827 lldb_utility::ansi::k_escape_start,
1828 lldb_utility::ansi::k_ctrl_normal,
1829 lldb_utility::ansi::k_escape_end);
1830 }
1831 else if (::strncmp (var_name_begin, "bold}", strlen("bold}")) == 0)
1832 {
1833 s.Printf ("%s%s%s",
1834 lldb_utility::ansi::k_escape_start,
1835 lldb_utility::ansi::k_ctrl_bold,
1836 lldb_utility::ansi::k_escape_end);
1837 }
1838 else if (::strncmp (var_name_begin, "faint}", strlen("faint}")) == 0)
1839 {
1840 s.Printf ("%s%s%s",
1841 lldb_utility::ansi::k_escape_start,
1842 lldb_utility::ansi::k_ctrl_faint,
1843 lldb_utility::ansi::k_escape_end);
1844 }
1845 else if (::strncmp (var_name_begin, "italic}", strlen("italic}")) == 0)
1846 {
1847 s.Printf ("%s%s%s",
1848 lldb_utility::ansi::k_escape_start,
1849 lldb_utility::ansi::k_ctrl_italic,
1850 lldb_utility::ansi::k_escape_end);
1851 }
1852 else if (::strncmp (var_name_begin, "underline}", strlen("underline}")) == 0)
1853 {
1854 s.Printf ("%s%s%s",
1855 lldb_utility::ansi::k_escape_start,
1856 lldb_utility::ansi::k_ctrl_underline,
1857 lldb_utility::ansi::k_escape_end);
1858 }
1859 else if (::strncmp (var_name_begin, "slow-blink}", strlen("slow-blink}")) == 0)
1860 {
1861 s.Printf ("%s%s%s",
1862 lldb_utility::ansi::k_escape_start,
1863 lldb_utility::ansi::k_ctrl_slow_blink,
1864 lldb_utility::ansi::k_escape_end);
1865 }
1866 else if (::strncmp (var_name_begin, "fast-blink}", strlen("fast-blink}")) == 0)
1867 {
1868 s.Printf ("%s%s%s",
1869 lldb_utility::ansi::k_escape_start,
1870 lldb_utility::ansi::k_ctrl_fast_blink,
1871 lldb_utility::ansi::k_escape_end);
1872 }
1873 else if (::strncmp (var_name_begin, "negative}", strlen("negative}")) == 0)
1874 {
1875 s.Printf ("%s%s%s",
1876 lldb_utility::ansi::k_escape_start,
1877 lldb_utility::ansi::k_ctrl_negative,
1878 lldb_utility::ansi::k_escape_end);
1879 }
1880 else if (::strncmp (var_name_begin, "conceal}", strlen("conceal}")) == 0)
1881 {
1882 s.Printf ("%s%s%s",
1883 lldb_utility::ansi::k_escape_start,
1884 lldb_utility::ansi::k_ctrl_conceal,
1885 lldb_utility::ansi::k_escape_end);
1886
1887 }
1888 else if (::strncmp (var_name_begin, "crossed-out}", strlen("crossed-out}")) == 0)
1889 {
1890 s.Printf ("%s%s%s",
1891 lldb_utility::ansi::k_escape_start,
1892 lldb_utility::ansi::k_ctrl_crossed_out,
1893 lldb_utility::ansi::k_escape_end);
1894 }
1895 else
1896 {
1897 var_success = false;
1898 }
1899 }
Greg Clayton1b654882010-09-19 02:33:57 +00001900 break;
1901
1902 case 'p':
1903 if (::strncmp (var_name_begin, "process.", strlen("process.")) == 0)
1904 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001905 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001906 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001907 Process *process = exe_ctx->GetProcessPtr();
1908 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00001909 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001910 var_name_begin += ::strlen ("process.");
1911 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001912 {
Daniel Malead01b2952012-11-29 21:49:15 +00001913 s.Printf("%" PRIu64, process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001914 var_success = true;
1915 }
1916 else if ((::strncmp (var_name_begin, "name}", strlen("name}")) == 0) ||
1917 (::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) ||
1918 (::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0))
1919 {
1920 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1921 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00001922 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001923 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1924 {
1925 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
1926 var_success = format_file_spec;
1927 }
1928 else
1929 {
1930 format_file_spec = exe_module->GetFileSpec();
1931 var_success = format_file_spec;
1932 }
Greg Clayton1b654882010-09-19 02:33:57 +00001933 }
1934 }
1935 }
Greg Claytonc14ee322011-09-22 04:58:26 +00001936 }
Greg Clayton1b654882010-09-19 02:33:57 +00001937 }
1938 break;
1939
1940 case 't':
1941 if (::strncmp (var_name_begin, "thread.", strlen("thread.")) == 0)
1942 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001943 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001944 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001945 Thread *thread = exe_ctx->GetThreadPtr();
1946 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00001947 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001948 var_name_begin += ::strlen ("thread.");
1949 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001950 {
Daniel Malead01b2952012-11-29 21:49:15 +00001951 s.Printf("0x%4.4" PRIx64, thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001952 var_success = true;
1953 }
1954 else if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1955 {
1956 s.Printf("%u", thread->GetIndexID());
1957 var_success = true;
1958 }
1959 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1960 {
1961 cstr = thread->GetName();
1962 var_success = cstr && cstr[0];
1963 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001964 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00001965 }
1966 else if (::strncmp (var_name_begin, "queue}", strlen("queue}")) == 0)
1967 {
1968 cstr = thread->GetQueueName();
1969 var_success = cstr && cstr[0];
1970 if (var_success)
1971 s.PutCString(cstr);
1972 }
1973 else if (::strncmp (var_name_begin, "stop-reason}", strlen("stop-reason}")) == 0)
1974 {
1975 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001976 if (stop_info_sp && stop_info_sp->IsValid())
Greg Claytonc14ee322011-09-22 04:58:26 +00001977 {
1978 cstr = stop_info_sp->GetDescription();
1979 if (cstr && cstr[0])
1980 {
1981 s.PutCString(cstr);
1982 var_success = true;
1983 }
Greg Clayton1b654882010-09-19 02:33:57 +00001984 }
1985 }
Jim Ingham73ca05a2011-12-17 01:35:57 +00001986 else if (::strncmp (var_name_begin, "return-value}", strlen("return-value}")) == 0)
1987 {
1988 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001989 if (stop_info_sp && stop_info_sp->IsValid())
Jim Ingham73ca05a2011-12-17 01:35:57 +00001990 {
1991 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
1992 if (return_valobj_sp)
1993 {
Enrico Granata9fb5ab52013-03-26 18:04:53 +00001994 ValueObject::DumpValueObject (s, return_valobj_sp.get());
Jim Inghamef651602011-12-22 19:12:40 +00001995 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001996 }
1997 }
1998 }
Greg Clayton1b654882010-09-19 02:33:57 +00001999 }
2000 }
2001 }
2002 else if (::strncmp (var_name_begin, "target.", strlen("target.")) == 0)
2003 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002004 // TODO: hookup properties
2005// if (!target_properties_sp)
2006// {
2007// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2008// if (target)
2009// target_properties_sp = target->GetProperties();
2010// }
2011//
2012// if (target_properties_sp)
2013// {
2014// var_name_begin += ::strlen ("target.");
2015// const char *end_property = strchr(var_name_begin, '}');
2016// if (end_property)
2017// {
2018// ConstString property_name(var_name_begin, end_property - var_name_begin);
2019// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
2020// if (!property_value.empty())
2021// {
2022// s.PutCString (property_value.c_str());
2023// var_success = true;
2024// }
2025// }
2026// }
Greg Clayton0603aa92010-10-04 01:05:56 +00002027 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2028 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00002029 {
Greg Clayton1b654882010-09-19 02:33:57 +00002030 var_name_begin += ::strlen ("target.");
2031 if (::strncmp (var_name_begin, "arch}", strlen("arch}")) == 0)
2032 {
2033 ArchSpec arch (target->GetArchitecture ());
2034 if (arch.IsValid())
2035 {
Greg Clayton64195a22011-02-23 00:35:02 +00002036 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00002037 var_success = true;
2038 }
2039 }
Greg Clayton67cc0632012-08-22 17:17:09 +00002040 }
Greg Clayton1b654882010-09-19 02:33:57 +00002041 }
2042 break;
2043
2044
2045 case 'm':
2046 if (::strncmp (var_name_begin, "module.", strlen("module.")) == 0)
2047 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002048 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00002049 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002050 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00002051 var_name_begin += ::strlen ("module.");
2052
2053 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2054 {
2055 if (module->GetFileSpec())
2056 {
2057 var_name_begin += ::strlen ("file.");
2058
2059 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2060 {
2061 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
2062 var_success = format_file_spec;
2063 }
2064 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2065 {
2066 format_file_spec = module->GetFileSpec();
2067 var_success = format_file_spec;
2068 }
2069 }
2070 }
2071 }
2072 }
2073 break;
2074
2075
2076 case 'f':
2077 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2078 {
2079 if (sc && sc->comp_unit != NULL)
2080 {
2081 var_name_begin += ::strlen ("file.");
2082
2083 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2084 {
2085 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
2086 var_success = format_file_spec;
2087 }
2088 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2089 {
2090 format_file_spec = *sc->comp_unit;
2091 var_success = format_file_spec;
2092 }
2093 }
2094 }
2095 else if (::strncmp (var_name_begin, "frame.", strlen("frame.")) == 0)
2096 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002097 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002098 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002099 StackFrame *frame = exe_ctx->GetFramePtr();
2100 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00002101 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002102 var_name_begin += ::strlen ("frame.");
2103 if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00002104 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002105 s.Printf("%u", frame->GetFrameIndex());
2106 var_success = true;
2107 }
2108 else if (::strncmp (var_name_begin, "pc}", strlen("pc}")) == 0)
2109 {
2110 reg_kind = eRegisterKindGeneric;
2111 reg_num = LLDB_REGNUM_GENERIC_PC;
2112 var_success = true;
2113 }
2114 else if (::strncmp (var_name_begin, "sp}", strlen("sp}")) == 0)
2115 {
2116 reg_kind = eRegisterKindGeneric;
2117 reg_num = LLDB_REGNUM_GENERIC_SP;
2118 var_success = true;
2119 }
2120 else if (::strncmp (var_name_begin, "fp}", strlen("fp}")) == 0)
2121 {
2122 reg_kind = eRegisterKindGeneric;
2123 reg_num = LLDB_REGNUM_GENERIC_FP;
2124 var_success = true;
2125 }
2126 else if (::strncmp (var_name_begin, "flags}", strlen("flags}")) == 0)
2127 {
2128 reg_kind = eRegisterKindGeneric;
2129 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2130 var_success = true;
2131 }
2132 else if (::strncmp (var_name_begin, "reg.", strlen ("reg.")) == 0)
2133 {
2134 reg_ctx = frame->GetRegisterContext().get();
2135 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002136 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002137 var_name_begin += ::strlen ("reg.");
2138 if (var_name_begin < var_name_end)
2139 {
2140 std::string reg_name (var_name_begin, var_name_end);
2141 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2142 if (reg_info)
2143 var_success = true;
2144 }
Greg Clayton1b654882010-09-19 02:33:57 +00002145 }
2146 }
2147 }
2148 }
2149 }
2150 else if (::strncmp (var_name_begin, "function.", strlen("function.")) == 0)
2151 {
2152 if (sc && (sc->function != NULL || sc->symbol != NULL))
2153 {
2154 var_name_begin += ::strlen ("function.");
2155 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
2156 {
2157 if (sc->function)
Daniel Malead01b2952012-11-29 21:49:15 +00002158 s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002159 else
2160 s.Printf("symbol[%u]", sc->symbol->GetID());
2161
2162 var_success = true;
2163 }
2164 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
2165 {
2166 if (sc->function)
2167 cstr = sc->function->GetName().AsCString (NULL);
2168 else if (sc->symbol)
2169 cstr = sc->symbol->GetName().AsCString (NULL);
2170 if (cstr)
2171 {
2172 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002173
2174 if (sc->block)
2175 {
2176 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2177 if (inline_block)
2178 {
2179 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2180 if (inline_info)
2181 {
2182 s.PutCString(" [inlined] ");
2183 inline_info->GetName().Dump(&s);
2184 }
2185 }
2186 }
Greg Clayton1b654882010-09-19 02:33:57 +00002187 var_success = true;
2188 }
2189 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002190 else if (::strncmp (var_name_begin, "name-with-args}", strlen("name-with-args}")) == 0)
2191 {
2192 // Print the function name with arguments in it
2193
2194 if (sc->function)
2195 {
2196 var_success = true;
2197 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2198 cstr = sc->function->GetName().AsCString (NULL);
2199 if (cstr)
2200 {
2201 const InlineFunctionInfo *inline_info = NULL;
2202 VariableListSP variable_list_sp;
2203 bool get_function_vars = true;
2204 if (sc->block)
2205 {
2206 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2207
2208 if (inline_block)
2209 {
2210 get_function_vars = false;
2211 inline_info = sc->block->GetInlinedFunctionInfo();
2212 if (inline_info)
2213 variable_list_sp = inline_block->GetBlockVariableList (true);
2214 }
2215 }
2216
2217 if (get_function_vars)
2218 {
2219 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2220 }
2221
2222 if (inline_info)
2223 {
2224 s.PutCString (cstr);
2225 s.PutCString (" [inlined] ");
2226 cstr = inline_info->GetName().GetCString();
2227 }
2228
2229 VariableList args;
2230 if (variable_list_sp)
2231 {
2232 const size_t num_variables = variable_list_sp->GetSize();
2233 for (size_t var_idx = 0; var_idx < num_variables; ++var_idx)
2234 {
2235 VariableSP var_sp (variable_list_sp->GetVariableAtIndex(var_idx));
2236 if (var_sp->GetScope() == eValueTypeVariableArgument)
2237 args.AddVariable (var_sp);
2238 }
2239
2240 }
2241 if (args.GetSize() > 0)
2242 {
2243 const char *open_paren = strchr (cstr, '(');
2244 const char *close_paren = NULL;
2245 if (open_paren)
Greg Clayton855958c2013-03-26 01:45:43 +00002246 {
2247 if (strncmp(open_paren, "(anonymous namespace)", strlen("(anonymous namespace)")) == 0)
2248 {
2249 open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '(');
2250 if (open_paren)
2251 close_paren = strchr (open_paren, ')');
2252 }
2253 else
2254 close_paren = strchr (open_paren, ')');
2255 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002256
2257 if (open_paren)
2258 s.Write(cstr, open_paren - cstr + 1);
2259 else
2260 {
2261 s.PutCString (cstr);
2262 s.PutChar ('(');
2263 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002264 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002265 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2266 {
2267 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2268 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
2269 const char *var_name = var_value_sp->GetName().GetCString();
2270 const char *var_value = var_value_sp->GetValueAsCString();
Greg Clayton3b188b12012-12-10 22:26:34 +00002271 if (arg_idx > 0)
2272 s.PutCString (", ");
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002273 if (var_value_sp->GetError().Success())
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002274 s.Printf ("%s=%s", var_name, var_value);
Greg Clayton3b188b12012-12-10 22:26:34 +00002275 else
2276 s.Printf ("%s=<unavailable>", var_name);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002277 }
2278
2279 if (close_paren)
2280 s.PutCString (close_paren);
2281 else
2282 s.PutChar(')');
2283
2284 }
2285 else
2286 {
2287 s.PutCString(cstr);
2288 }
2289 }
2290 }
2291 else if (sc->symbol)
2292 {
2293 cstr = sc->symbol->GetName().AsCString (NULL);
2294 if (cstr)
2295 {
2296 s.PutCString(cstr);
2297 var_success = true;
2298 }
2299 }
2300 }
Greg Clayton1b654882010-09-19 02:33:57 +00002301 else if (::strncmp (var_name_begin, "addr-offset}", strlen("addr-offset}")) == 0)
2302 {
2303 var_success = addr != NULL;
2304 if (var_success)
2305 {
2306 format_addr = *addr;
2307 calculate_format_addr_function_offset = true;
2308 }
2309 }
2310 else if (::strncmp (var_name_begin, "line-offset}", strlen("line-offset}")) == 0)
2311 {
2312 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2313 if (var_success)
2314 {
2315 format_addr = sc->line_entry.range.GetBaseAddress();
2316 calculate_format_addr_function_offset = true;
2317 }
2318 }
2319 else if (::strncmp (var_name_begin, "pc-offset}", strlen("pc-offset}")) == 0)
2320 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002321 StackFrame *frame = exe_ctx->GetFramePtr();
2322 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002323 if (var_success)
2324 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002325 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002326 calculate_format_addr_function_offset = true;
2327 }
2328 }
2329 }
2330 }
2331 break;
2332
2333 case 'l':
2334 if (::strncmp (var_name_begin, "line.", strlen("line.")) == 0)
2335 {
2336 if (sc && sc->line_entry.IsValid())
2337 {
2338 var_name_begin += ::strlen ("line.");
2339 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2340 {
2341 var_name_begin += ::strlen ("file.");
2342
2343 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2344 {
2345 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
2346 var_success = format_file_spec;
2347 }
2348 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2349 {
2350 format_file_spec = sc->line_entry.file;
2351 var_success = format_file_spec;
2352 }
2353 }
2354 else if (::strncmp (var_name_begin, "number}", strlen("number}")) == 0)
2355 {
2356 var_success = true;
2357 s.Printf("%u", sc->line_entry.line);
2358 }
2359 else if ((::strncmp (var_name_begin, "start-addr}", strlen("start-addr}")) == 0) ||
2360 (::strncmp (var_name_begin, "end-addr}", strlen("end-addr}")) == 0))
2361 {
2362 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2363 if (var_success)
2364 {
2365 format_addr = sc->line_entry.range.GetBaseAddress();
2366 if (var_name_begin[0] == 'e')
2367 format_addr.Slide (sc->line_entry.range.GetByteSize());
2368 }
2369 }
2370 }
2371 }
2372 break;
2373 }
2374
2375 if (var_success)
2376 {
2377 // If format addr is valid, then we need to print an address
2378 if (reg_num != LLDB_INVALID_REGNUM)
2379 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002380 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002381 // We have a register value to display...
2382 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2383 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002384 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002385 }
2386 else
2387 {
2388 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002389 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002390
2391 if (reg_ctx)
2392 {
2393 if (reg_kind != kNumRegisterKinds)
2394 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2395 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2396 var_success = reg_info != NULL;
2397 }
2398 }
2399 }
2400
2401 if (reg_info != NULL)
2402 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002403 RegisterValue reg_value;
2404 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2405 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002406 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002407 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002408 }
2409 }
2410
2411 if (format_file_spec)
2412 {
2413 s << format_file_spec;
2414 }
2415
2416 // If format addr is valid, then we need to print an address
2417 if (format_addr.IsValid())
2418 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002419 var_success = false;
2420
Greg Clayton1b654882010-09-19 02:33:57 +00002421 if (calculate_format_addr_function_offset)
2422 {
2423 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002424
Greg Clayton0603aa92010-10-04 01:05:56 +00002425 if (sc)
2426 {
2427 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002428 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002429 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00002430 if (sc->block)
2431 {
2432 // Check to make sure we aren't in an inline
2433 // function. If we are, use the inline block
2434 // range that contains "format_addr" since
2435 // blocks can be discontiguous.
2436 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2437 AddressRange inline_range;
2438 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2439 func_addr = inline_range.GetBaseAddress();
2440 }
2441 }
Greg Claytone7612132012-03-07 21:03:09 +00002442 else if (sc->symbol && sc->symbol->ValueIsAddress())
2443 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002444 }
2445
2446 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002447 {
2448 if (func_addr.GetSection() == format_addr.GetSection())
2449 {
2450 addr_t func_file_addr = func_addr.GetFileAddress();
2451 addr_t addr_file_addr = format_addr.GetFileAddress();
2452 if (addr_file_addr > func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002453 s.Printf(" + %" PRIu64, addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002454 else if (addr_file_addr < func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002455 s.Printf(" - %" PRIu64, func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002456 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002457 }
2458 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002459 {
2460 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2461 if (target)
2462 {
2463 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2464 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2465 if (addr_load_addr > func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002466 s.Printf(" + %" PRIu64, addr_load_addr - func_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002467 else if (addr_load_addr < func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002468 s.Printf(" - %" PRIu64, func_load_addr - addr_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002469 var_success = true;
2470 }
2471 }
Greg Clayton1b654882010-09-19 02:33:57 +00002472 }
2473 }
2474 else
2475 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002476 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002477 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002478 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2479 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002480 if (vaddr == LLDB_INVALID_ADDRESS)
2481 vaddr = format_addr.GetFileAddress ();
2482
2483 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002484 {
Greg Clayton514487e2011-02-15 21:59:32 +00002485 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002486 if (addr_width == 0)
2487 addr_width = 16;
Daniel Malead01b2952012-11-29 21:49:15 +00002488 s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002489 var_success = true;
2490 }
Greg Clayton1b654882010-09-19 02:33:57 +00002491 }
2492 }
2493 }
2494
2495 if (var_success == false)
2496 success = false;
2497 }
2498 p = var_name_end;
2499 }
2500 else
2501 break;
2502 }
2503 else
2504 {
2505 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2506 s.PutChar(*p);
2507 }
2508 }
2509 else if (*p == '\\')
2510 {
2511 ++p; // skip the slash
2512 switch (*p)
2513 {
2514 case 'a': s.PutChar ('\a'); break;
2515 case 'b': s.PutChar ('\b'); break;
2516 case 'f': s.PutChar ('\f'); break;
2517 case 'n': s.PutChar ('\n'); break;
2518 case 'r': s.PutChar ('\r'); break;
2519 case 't': s.PutChar ('\t'); break;
2520 case 'v': s.PutChar ('\v'); break;
2521 case '\'': s.PutChar ('\''); break;
2522 case '\\': s.PutChar ('\\'); break;
2523 case '0':
2524 // 1 to 3 octal chars
2525 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002526 // Make a string that can hold onto the initial zero char,
2527 // up to 3 octal digits, and a terminating NULL.
2528 char oct_str[5] = { 0, 0, 0, 0, 0 };
2529
2530 int i;
2531 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2532 oct_str[i] = p[i];
2533
2534 // We don't want to consume the last octal character since
2535 // the main for loop will do this for us, so we advance p by
2536 // one less than i (even if i is zero)
2537 p += i - 1;
2538 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2539 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002540 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002541 s.PutChar((char)octal_value);
Greg Clayton1b654882010-09-19 02:33:57 +00002542 }
Greg Clayton1b654882010-09-19 02:33:57 +00002543 }
2544 break;
2545
2546 case 'x':
2547 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002548 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002549 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002550 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002551
Greg Clayton0603aa92010-10-04 01:05:56 +00002552 // Make a string that can hold onto two hex chars plus a
2553 // NULL terminator
2554 char hex_str[3] = { 0,0,0 };
2555 hex_str[0] = *p;
2556 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002557 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002558 ++p; // Skip the first of the two hex chars
2559 hex_str[1] = *p;
2560 }
2561
2562 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2563 if (hex_value <= UINT8_MAX)
Greg Claytonc7bece562013-01-25 18:06:21 +00002564 s.PutChar ((char)hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002565 }
2566 else
2567 {
2568 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002569 }
2570 break;
2571
2572 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002573 // Just desensitize any other character by just printing what
2574 // came after the '\'
2575 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002576 break;
2577
2578 }
2579
2580 }
2581 }
2582 if (end)
2583 *end = p;
2584 return success;
2585}
2586
Jim Ingham228063c2012-02-21 02:23:08 +00002587void
2588Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2589{
Jim Ingham4f02b222012-02-22 22:49:20 +00002590 // For simplicity's sake, I am not going to deal with how to close down any
2591 // open logging streams, I just redirect everything from here on out to the
2592 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002593 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2594}
2595
2596bool
2597Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2598{
2599 Log::Callbacks log_callbacks;
2600
2601 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002602 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002603 {
2604 log_stream_sp = m_log_callback_stream_sp;
2605 // For now when using the callback mode you always get thread & timestamp.
2606 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2607 }
2608 else if (log_file == NULL || *log_file == '\0')
2609 {
2610 log_stream_sp.reset(new StreamFile(GetOutputFile().GetDescriptor(), false));
2611 }
2612 else
2613 {
2614 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00002615 if (pos != m_log_streams.end())
2616 log_stream_sp = pos->second.lock();
2617 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002618 {
2619 log_stream_sp.reset (new StreamFile (log_file));
2620 m_log_streams[log_file] = log_stream_sp;
2621 }
Jim Ingham228063c2012-02-21 02:23:08 +00002622 }
2623 assert (log_stream_sp.get());
2624
2625 if (log_options == 0)
2626 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2627
2628 if (Log::GetLogChannelCallbacks (channel, log_callbacks))
2629 {
2630 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2631 return true;
2632 }
2633 else
2634 {
2635 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2636 if (log_channel_sp)
2637 {
2638 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2639 {
2640 return true;
2641 }
2642 else
2643 {
2644 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2645 return false;
2646 }
2647 }
2648 else
2649 {
2650 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2651 return false;
2652 }
2653 }
2654 return false;
2655}
2656
Greg Clayton9585fbf2013-03-19 00:20:55 +00002657SourceManager &
2658Debugger::GetSourceManager ()
2659{
2660 if (m_source_manager_ap.get() == NULL)
2661 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
2662 return *m_source_manager_ap;
2663}
2664
2665