1 //===-- 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 
10 #include "lldb/lldb-python.h"
11 
12 #include "lldb/API/SBDebugger.h"
13 
14 #include "lldb/Core/Debugger.h"
15 
16 #include <map>
17 
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/Type.h"
20 
21 #include "lldb/lldb-private.h"
22 #include "lldb/Core/ConnectionFileDescriptor.h"
23 #include "lldb/Core/InputReader.h"
24 #include "lldb/Core/Module.h"
25 #include "lldb/Core/PluginManager.h"
26 #include "lldb/Core/RegisterValue.h"
27 #include "lldb/Core/State.h"
28 #include "lldb/Core/StreamAsynchronousIO.h"
29 #include "lldb/Core/StreamCallback.h"
30 #include "lldb/Core/StreamString.h"
31 #include "lldb/Core/Timer.h"
32 #include "lldb/Core/ValueObject.h"
33 #include "lldb/Core/ValueObjectVariable.h"
34 #include "lldb/DataFormatters/DataVisualization.h"
35 #include "lldb/DataFormatters/FormatManager.h"
36 #include "lldb/Host/DynamicLibrary.h"
37 #include "lldb/Host/Terminal.h"
38 #include "lldb/Interpreter/CommandInterpreter.h"
39 #include "lldb/Interpreter/OptionValueSInt64.h"
40 #include "lldb/Interpreter/OptionValueString.h"
41 #include "lldb/Symbol/ClangASTContext.h"
42 #include "lldb/Symbol/CompileUnit.h"
43 #include "lldb/Symbol/Function.h"
44 #include "lldb/Symbol/Symbol.h"
45 #include "lldb/Symbol/VariableList.h"
46 #include "lldb/Target/TargetList.h"
47 #include "lldb/Target/Process.h"
48 #include "lldb/Target/RegisterContext.h"
49 #include "lldb/Target/StopInfo.h"
50 #include "lldb/Target/Thread.h"
51 #include "lldb/Utility/AnsiTerminal.h"
52 
53 using namespace lldb;
54 using namespace lldb_private;
55 
56 
57 static uint32_t g_shared_debugger_refcount = 0;
58 static lldb::user_id_t g_unique_id = 1;
59 
60 #pragma mark Static Functions
61 
62 static Mutex &
63 GetDebuggerListMutex ()
64 {
65     static Mutex g_mutex(Mutex::eMutexTypeRecursive);
66     return g_mutex;
67 }
68 
69 typedef std::vector<DebuggerSP> DebuggerList;
70 
71 static DebuggerList &
72 GetDebuggerList()
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 }
79 
80 OptionEnumValueElement
81 g_show_disassembly_enum_values[] =
82 {
83     { 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."},
86     { 0, NULL, NULL }
87 };
88 
89 OptionEnumValueElement
90 g_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."},
95     { 0, NULL, NULL }
96 };
97 
98 #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\
105     "{, name = '${thread.name}}"\
106     "{, queue = '${thread.queue}}"\
107     "{, 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 
118 static PropertyDefinition
119 g_properties[] =
120 {
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)." },
124 {   "prompt",                   OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
125 {   "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." },
133 
134     {   NULL,                       OptionValue::eTypeInvalid, true, 0    , NULL, NULL, NULL }
135 };
136 
137 enum
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 //
165 
166 
167 Error
168 Debugger::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 
186 bool
187 Debugger::GetAutoConfirm () const
188 {
189     const uint32_t idx = ePropertyAutoConfirm;
190     return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
191 }
192 
193 const char *
194 Debugger::GetFrameFormat() const
195 {
196     const uint32_t idx = ePropertyFrameFormat;
197     return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
198 }
199 
200 bool
201 Debugger::GetNotifyVoid () const
202 {
203     const uint32_t idx = ePropertyNotiftVoid;
204     return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
205 }
206 
207 const char *
208 Debugger::GetPrompt() const
209 {
210     const uint32_t idx = ePropertyPrompt;
211     return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
212 }
213 
214 void
215 Debugger::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 
224 const char *
225 Debugger::GetThreadFormat() const
226 {
227     const uint32_t idx = ePropertyThreadFormat;
228     return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
229 }
230 
231 lldb::ScriptLanguage
232 Debugger::GetScriptLanguage() const
233 {
234     const uint32_t idx = ePropertyScriptLanguage;
235     return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
236 }
237 
238 bool
239 Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
240 {
241     const uint32_t idx = ePropertyScriptLanguage;
242     return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
243 }
244 
245 uint32_t
246 Debugger::GetTerminalWidth () const
247 {
248     const uint32_t idx = ePropertyTerminalWidth;
249     return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
250 }
251 
252 bool
253 Debugger::SetTerminalWidth (uint32_t term_width)
254 {
255     const uint32_t idx = ePropertyTerminalWidth;
256     return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
257 }
258 
259 bool
260 Debugger::GetUseExternalEditor () const
261 {
262     const uint32_t idx = ePropertyUseExternalEditor;
263     return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
264 }
265 
266 bool
267 Debugger::SetUseExternalEditor (bool b)
268 {
269     const uint32_t idx = ePropertyUseExternalEditor;
270     return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
271 }
272 
273 uint32_t
274 Debugger::GetStopSourceLineCount (bool before) const
275 {
276     const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
277     return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
278 }
279 
280 Debugger::StopDisassemblyType
281 Debugger::GetStopDisassemblyDisplay () const
282 {
283     const uint32_t idx = ePropertyStopDisassemblyDisplay;
284     return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
285 }
286 
287 uint32_t
288 Debugger::GetDisassemblyLineCount () const
289 {
290     const uint32_t idx = ePropertyStopDisassemblyCount;
291     return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
292 }
293 
294 #pragma mark Debugger
295 
296 //const DebuggerPropertiesSP &
297 //Debugger::GetSettings() const
298 //{
299 //    return m_properties_sp;
300 //}
301 //
302 
303 int
304 Debugger::TestDebuggerRefCount ()
305 {
306     return g_shared_debugger_refcount;
307 }
308 
309 void
310 Debugger::Initialize ()
311 {
312     if (g_shared_debugger_refcount++ == 0)
313         lldb_private::Initialize();
314 }
315 
316 void
317 Debugger::Terminate ()
318 {
319     if (g_shared_debugger_refcount > 0)
320     {
321         g_shared_debugger_refcount--;
322         if (g_shared_debugger_refcount == 0)
323         {
324             lldb_private::WillTerminate();
325             lldb_private::Terminate();
326 
327             // Clear our master list of debugger objects
328             Mutex::Locker locker (GetDebuggerListMutex ());
329             GetDebuggerList().clear();
330         }
331     }
332 }
333 
334 void
335 Debugger::SettingsInitialize ()
336 {
337     Target::SettingsInitialize ();
338 }
339 
340 void
341 Debugger::SettingsTerminate ()
342 {
343     Target::SettingsTerminate ();
344 }
345 
346 bool
347 Debugger::LoadPlugin (const FileSpec& spec, Error& error)
348 {
349     lldb::DynamicLibrarySP dynlib_sp(new lldb_private::DynamicLibrary(spec));
350     if (!dynlib_sp || dynlib_sp->IsValid() == false)
351     {
352         if (spec.Exists())
353             error.SetErrorString("this file does not represent a loadable dylib");
354         else
355             error.SetErrorString("no such file");
356         return false;
357     }
358     lldb::DebuggerSP debugger_sp(shared_from_this());
359     lldb::SBDebugger debugger_sb(debugger_sp);
360     // TODO: mangle this differently for your system - on OSX, the first underscore needs to be removed and the second one stays
361     LLDBCommandPluginInit init_func = dynlib_sp->GetSymbol<LLDBCommandPluginInit>("_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
362     if (!init_func)
363     {
364         error.SetErrorString("cannot find the initialization function lldb::PluginInitialize(lldb::SBDebugger)");
365         return false;
366     }
367     if (init_func(debugger_sb))
368     {
369         m_loaded_plugins.push_back(dynlib_sp);
370         return true;
371     }
372     error.SetErrorString("dylib refused to be loaded");
373     return false;
374 }
375 
376 static FileSpec::EnumerateDirectoryResult
377 LoadPluginCallback
378 (
379  void *baton,
380  FileSpec::FileType file_type,
381  const FileSpec &file_spec
382  )
383 {
384     Error error;
385 
386     static ConstString g_dylibext("dylib");
387 
388     if (!baton)
389         return FileSpec::eEnumerateDirectoryResultQuit;
390 
391     Debugger *debugger = (Debugger*)baton;
392 
393     // If we have a regular file, a symbolic link or unknown file type, try
394     // and process the file. We must handle unknown as sometimes the directory
395     // enumeration might be enumerating a file system that doesn't have correct
396     // file type information.
397     if (file_type == FileSpec::eFileTypeRegular         ||
398         file_type == FileSpec::eFileTypeSymbolicLink    ||
399         file_type == FileSpec::eFileTypeUnknown          )
400     {
401         FileSpec plugin_file_spec (file_spec);
402         plugin_file_spec.ResolvePath ();
403 
404         if (plugin_file_spec.GetFileNameExtension() != g_dylibext)
405             return FileSpec::eEnumerateDirectoryResultNext;
406 
407         Error plugin_load_error;
408         debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
409 
410         return FileSpec::eEnumerateDirectoryResultNext;
411     }
412 
413     else if (file_type == FileSpec::eFileTypeUnknown     ||
414         file_type == FileSpec::eFileTypeDirectory   ||
415         file_type == FileSpec::eFileTypeSymbolicLink )
416     {
417         // Try and recurse into anything that a directory or symbolic link.
418         // We must also do this for unknown as sometimes the directory enumeration
419         // might be enurating a file system that doesn't have correct file type
420         // information.
421         return FileSpec::eEnumerateDirectoryResultEnter;
422     }
423 
424     return FileSpec::eEnumerateDirectoryResultNext;
425 }
426 
427 void
428 Debugger::InstanceInitialize ()
429 {
430     FileSpec dir_spec;
431     const bool find_directories = true;
432     const bool find_files = true;
433     const bool find_other = true;
434     char dir_path[PATH_MAX];
435     if (Host::GetLLDBPath (ePathTypeLLDBSystemPlugins, 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     }
447 
448     if (Host::GetLLDBPath (ePathTypeLLDBUserPlugins, dir_spec))
449     {
450         if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
451         {
452             FileSpec::EnumerateDirectory (dir_path,
453                                           find_directories,
454                                           find_files,
455                                           find_other,
456                                           LoadPluginCallback,
457                                           this);
458         }
459     }
460 
461     PluginManager::DebuggerInitialize (*this);
462 }
463 
464 DebuggerSP
465 Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
466 {
467     DebuggerSP debugger_sp (new Debugger(log_callback, baton));
468     if (g_shared_debugger_refcount > 0)
469     {
470         Mutex::Locker locker (GetDebuggerListMutex ());
471         GetDebuggerList().push_back(debugger_sp);
472     }
473     debugger_sp->InstanceInitialize ();
474     return debugger_sp;
475 }
476 
477 void
478 Debugger::Destroy (DebuggerSP &debugger_sp)
479 {
480     if (debugger_sp.get() == NULL)
481         return;
482 
483     debugger_sp->Clear();
484 
485     if (g_shared_debugger_refcount > 0)
486     {
487         Mutex::Locker locker (GetDebuggerListMutex ());
488         DebuggerList &debugger_list = GetDebuggerList ();
489         DebuggerList::iterator pos, end = debugger_list.end();
490         for (pos = debugger_list.begin (); pos != end; ++pos)
491         {
492             if ((*pos).get() == debugger_sp.get())
493             {
494                 debugger_list.erase (pos);
495                 return;
496             }
497         }
498     }
499 }
500 
501 DebuggerSP
502 Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
503 {
504     DebuggerSP debugger_sp;
505     if (g_shared_debugger_refcount > 0)
506     {
507         Mutex::Locker locker (GetDebuggerListMutex ());
508         DebuggerList &debugger_list = GetDebuggerList();
509         DebuggerList::iterator pos, end = debugger_list.end();
510 
511         for (pos = debugger_list.begin(); pos != end; ++pos)
512         {
513             if ((*pos).get()->m_instance_name == instance_name)
514             {
515                 debugger_sp = *pos;
516                 break;
517             }
518         }
519     }
520     return debugger_sp;
521 }
522 
523 TargetSP
524 Debugger::FindTargetWithProcessID (lldb::pid_t pid)
525 {
526     TargetSP target_sp;
527     if (g_shared_debugger_refcount > 0)
528     {
529         Mutex::Locker locker (GetDebuggerListMutex ());
530         DebuggerList &debugger_list = GetDebuggerList();
531         DebuggerList::iterator pos, end = debugger_list.end();
532         for (pos = debugger_list.begin(); pos != end; ++pos)
533         {
534             target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
535             if (target_sp)
536                 break;
537         }
538     }
539     return target_sp;
540 }
541 
542 TargetSP
543 Debugger::FindTargetWithProcess (Process *process)
544 {
545     TargetSP target_sp;
546     if (g_shared_debugger_refcount > 0)
547     {
548         Mutex::Locker locker (GetDebuggerListMutex ());
549         DebuggerList &debugger_list = GetDebuggerList();
550         DebuggerList::iterator pos, end = debugger_list.end();
551         for (pos = debugger_list.begin(); pos != end; ++pos)
552         {
553             target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
554             if (target_sp)
555                 break;
556         }
557     }
558     return target_sp;
559 }
560 
561 Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
562     UserID (g_unique_id++),
563     Properties(OptionValuePropertiesSP(new OptionValueProperties())),
564     m_input_comm("debugger.input"),
565     m_input_file (),
566     m_output_file (),
567     m_error_file (),
568     m_terminal_state (),
569     m_target_list (*this),
570     m_platform_list (),
571     m_listener ("lldb.Debugger"),
572     m_source_manager_ap(),
573     m_source_file_cache(),
574     m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
575     m_input_reader_stack (),
576     m_input_reader_data (),
577     m_instance_name()
578 {
579     char instance_cstr[256];
580     snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
581     m_instance_name.SetCString(instance_cstr);
582     if (log_callback)
583         m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
584     m_command_interpreter_ap->Initialize ();
585     // Always add our default platform to the platform list
586     PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
587     assert (default_platform_sp.get());
588     m_platform_list.Append (default_platform_sp, true);
589 
590     m_collection_sp->Initialize (g_properties);
591     m_collection_sp->AppendProperty (ConstString("target"),
592                                      ConstString("Settings specify to debugging targets."),
593                                      true,
594                                      Target::GetGlobalProperties()->GetValueProperties());
595     if (m_command_interpreter_ap.get())
596     {
597         m_collection_sp->AppendProperty (ConstString("interpreter"),
598                                          ConstString("Settings specify to the debugger's command interpreter."),
599                                          true,
600                                          m_command_interpreter_ap->GetValueProperties());
601     }
602     OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
603     term_width->SetMinimumValue(10);
604     term_width->SetMaximumValue(1024);
605 }
606 
607 Debugger::~Debugger ()
608 {
609     Clear();
610 }
611 
612 void
613 Debugger::Clear()
614 {
615     CleanUpInputReaders();
616     m_listener.Clear();
617     int num_targets = m_target_list.GetNumTargets();
618     for (int i = 0; i < num_targets; i++)
619     {
620         TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
621         if (target_sp)
622         {
623             ProcessSP process_sp (target_sp->GetProcessSP());
624             if (process_sp)
625                 process_sp->Finalize();
626             target_sp->Destroy();
627         }
628     }
629     BroadcasterManager::Clear ();
630 
631     // Close the input file _before_ we close the input read communications class
632     // as it does NOT own the input file, our m_input_file does.
633     m_terminal_state.Clear();
634     GetInputFile().Close ();
635     // Now that we have closed m_input_file, we can now tell our input communication
636     // class to close down. Its read thread should quickly exit after we close
637     // the input file handle above.
638     m_input_comm.Clear ();
639 }
640 
641 bool
642 Debugger::GetCloseInputOnEOF () const
643 {
644     return m_input_comm.GetCloseOnEOF();
645 }
646 
647 void
648 Debugger::SetCloseInputOnEOF (bool b)
649 {
650     m_input_comm.SetCloseOnEOF(b);
651 }
652 
653 bool
654 Debugger::GetAsyncExecution ()
655 {
656     return !m_command_interpreter_ap->GetSynchronous();
657 }
658 
659 void
660 Debugger::SetAsyncExecution (bool async_execution)
661 {
662     m_command_interpreter_ap->SetSynchronous (!async_execution);
663 }
664 
665 
666 void
667 Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
668 {
669     File &in_file = GetInputFile();
670     in_file.SetStream (fh, tranfer_ownership);
671     if (in_file.IsValid() == false)
672         in_file.SetStream (stdin, true);
673 
674     // Disconnect from any old connection if we had one
675     m_input_comm.Disconnect ();
676     // Pass false as the second argument to ConnectionFileDescriptor below because
677     // our "in_file" above will already take ownership if requested and we don't
678     // want to objects trying to own and close a file descriptor.
679     m_input_comm.SetConnection (new ConnectionFileDescriptor (in_file.GetDescriptor(), false));
680     m_input_comm.SetReadThreadBytesReceivedCallback (Debugger::DispatchInputCallback, this);
681 
682     // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
683     SaveInputTerminalState ();
684 
685     Error error;
686     if (m_input_comm.StartReadThread (&error) == false)
687     {
688         File &err_file = GetErrorFile();
689 
690         err_file.Printf ("error: failed to main input read thread: %s", error.AsCString() ? error.AsCString() : "unkown error");
691         exit(1);
692     }
693 }
694 
695 void
696 Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
697 {
698     File &out_file = GetOutputFile();
699     out_file.SetStream (fh, tranfer_ownership);
700     if (out_file.IsValid() == false)
701         out_file.SetStream (stdout, false);
702 
703     // do not create the ScriptInterpreter just for setting the output file handle
704     // as the constructor will know how to do the right thing on its own
705     const bool can_create = false;
706     ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
707     if (script_interpreter)
708         script_interpreter->ResetOutputFileHandle (fh);
709 }
710 
711 void
712 Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
713 {
714     File &err_file = GetErrorFile();
715     err_file.SetStream (fh, tranfer_ownership);
716     if (err_file.IsValid() == false)
717         err_file.SetStream (stderr, false);
718 }
719 
720 void
721 Debugger::SaveInputTerminalState ()
722 {
723     File &in_file = GetInputFile();
724     if (in_file.GetDescriptor() != File::kInvalidDescriptor)
725         m_terminal_state.Save(in_file.GetDescriptor(), true);
726 }
727 
728 void
729 Debugger::RestoreInputTerminalState ()
730 {
731     m_terminal_state.Restore();
732 }
733 
734 ExecutionContext
735 Debugger::GetSelectedExecutionContext ()
736 {
737     ExecutionContext exe_ctx;
738     TargetSP target_sp(GetSelectedTarget());
739     exe_ctx.SetTargetSP (target_sp);
740 
741     if (target_sp)
742     {
743         ProcessSP process_sp (target_sp->GetProcessSP());
744         exe_ctx.SetProcessSP (process_sp);
745         if (process_sp && process_sp->IsRunning() == false)
746         {
747             ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
748             if (thread_sp)
749             {
750                 exe_ctx.SetThreadSP (thread_sp);
751                 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
752                 if (exe_ctx.GetFramePtr() == NULL)
753                     exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
754             }
755         }
756     }
757     return exe_ctx;
758 
759 }
760 
761 InputReaderSP
762 Debugger::GetCurrentInputReader ()
763 {
764     InputReaderSP reader_sp;
765 
766     if (!m_input_reader_stack.IsEmpty())
767     {
768         // Clear any finished readers from the stack
769         while (CheckIfTopInputReaderIsDone()) ;
770 
771         if (!m_input_reader_stack.IsEmpty())
772             reader_sp = m_input_reader_stack.Top();
773     }
774 
775     return reader_sp;
776 }
777 
778 void
779 Debugger::DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len)
780 {
781     if (bytes_len > 0)
782         ((Debugger *)baton)->DispatchInput ((char *)bytes, bytes_len);
783     else
784         ((Debugger *)baton)->DispatchInputEndOfFile ();
785 }
786 
787 
788 void
789 Debugger::DispatchInput (const char *bytes, size_t bytes_len)
790 {
791     if (bytes == NULL || bytes_len == 0)
792         return;
793 
794     WriteToDefaultReader (bytes, bytes_len);
795 }
796 
797 void
798 Debugger::DispatchInputInterrupt ()
799 {
800     m_input_reader_data.clear();
801 
802     InputReaderSP reader_sp (GetCurrentInputReader ());
803     if (reader_sp)
804     {
805         reader_sp->Notify (eInputReaderInterrupt);
806 
807         // If notifying the reader of the interrupt finished the reader, we should pop it off the stack.
808         while (CheckIfTopInputReaderIsDone ()) ;
809     }
810 }
811 
812 void
813 Debugger::DispatchInputEndOfFile ()
814 {
815     m_input_reader_data.clear();
816 
817     InputReaderSP reader_sp (GetCurrentInputReader ());
818     if (reader_sp)
819     {
820         reader_sp->Notify (eInputReaderEndOfFile);
821 
822         // If notifying the reader of the end-of-file finished the reader, we should pop it off the stack.
823         while (CheckIfTopInputReaderIsDone ()) ;
824     }
825 }
826 
827 void
828 Debugger::CleanUpInputReaders ()
829 {
830     m_input_reader_data.clear();
831 
832     // The bottom input reader should be the main debugger input reader.  We do not want to close that one here.
833     while (m_input_reader_stack.GetSize() > 1)
834     {
835         InputReaderSP reader_sp (GetCurrentInputReader ());
836         if (reader_sp)
837         {
838             reader_sp->Notify (eInputReaderEndOfFile);
839             reader_sp->SetIsDone (true);
840         }
841     }
842 }
843 
844 void
845 Debugger::NotifyTopInputReader (InputReaderAction notification)
846 {
847     InputReaderSP reader_sp (GetCurrentInputReader());
848     if (reader_sp)
849 	{
850         reader_sp->Notify (notification);
851 
852         // Flush out any input readers that are done.
853         while (CheckIfTopInputReaderIsDone ())
854             /* Do nothing. */;
855     }
856 }
857 
858 bool
859 Debugger::InputReaderIsTopReader (const InputReaderSP& reader_sp)
860 {
861     InputReaderSP top_reader_sp (GetCurrentInputReader());
862 
863     return (reader_sp.get() == top_reader_sp.get());
864 }
865 
866 
867 void
868 Debugger::WriteToDefaultReader (const char *bytes, size_t bytes_len)
869 {
870     if (bytes && bytes_len)
871         m_input_reader_data.append (bytes, bytes_len);
872 
873     if (m_input_reader_data.empty())
874         return;
875 
876     while (!m_input_reader_stack.IsEmpty() && !m_input_reader_data.empty())
877     {
878         // Get the input reader from the top of the stack
879         InputReaderSP reader_sp (GetCurrentInputReader ());
880         if (!reader_sp)
881             break;
882 
883         size_t bytes_handled = reader_sp->HandleRawBytes (m_input_reader_data.c_str(),
884                                                           m_input_reader_data.size());
885         if (bytes_handled)
886         {
887             m_input_reader_data.erase (0, bytes_handled);
888         }
889         else
890         {
891             // No bytes were handled, we might not have reached our
892             // granularity, just return and wait for more data
893             break;
894         }
895     }
896 
897     // Flush out any input readers that are done.
898     while (CheckIfTopInputReaderIsDone ())
899         /* Do nothing. */;
900 
901 }
902 
903 void
904 Debugger::PushInputReader (const InputReaderSP& reader_sp)
905 {
906     if (!reader_sp)
907         return;
908 
909     // Deactivate the old top reader
910     InputReaderSP top_reader_sp (GetCurrentInputReader ());
911 
912     if (top_reader_sp)
913         top_reader_sp->Notify (eInputReaderDeactivate);
914 
915     m_input_reader_stack.Push (reader_sp);
916     reader_sp->Notify (eInputReaderActivate);
917     ActivateInputReader (reader_sp);
918 }
919 
920 bool
921 Debugger::PopInputReader (const InputReaderSP& pop_reader_sp)
922 {
923     bool result = false;
924 
925     // The reader on the stop of the stack is done, so let the next
926     // read on the stack referesh its prompt and if there is one...
927     if (!m_input_reader_stack.IsEmpty())
928     {
929         // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
930         InputReaderSP reader_sp(m_input_reader_stack.Top());
931 
932         if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
933         {
934             m_input_reader_stack.Pop ();
935             reader_sp->Notify (eInputReaderDeactivate);
936             reader_sp->Notify (eInputReaderDone);
937             result = true;
938 
939             if (!m_input_reader_stack.IsEmpty())
940             {
941                 reader_sp = m_input_reader_stack.Top();
942                 if (reader_sp)
943                 {
944                     ActivateInputReader (reader_sp);
945                     reader_sp->Notify (eInputReaderReactivate);
946                 }
947             }
948         }
949     }
950     return result;
951 }
952 
953 bool
954 Debugger::CheckIfTopInputReaderIsDone ()
955 {
956     bool result = false;
957     if (!m_input_reader_stack.IsEmpty())
958     {
959         // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
960         InputReaderSP reader_sp(m_input_reader_stack.Top());
961 
962         if (reader_sp && reader_sp->IsDone())
963         {
964             result = true;
965             PopInputReader (reader_sp);
966         }
967     }
968     return result;
969 }
970 
971 void
972 Debugger::ActivateInputReader (const InputReaderSP &reader_sp)
973 {
974     int input_fd = m_input_file.GetFile().GetDescriptor();
975 
976     if (input_fd >= 0)
977     {
978         Terminal tty(input_fd);
979 
980         tty.SetEcho(reader_sp->GetEcho());
981 
982         switch (reader_sp->GetGranularity())
983         {
984         case eInputReaderGranularityByte:
985         case eInputReaderGranularityWord:
986             tty.SetCanonical (false);
987             break;
988 
989         case eInputReaderGranularityLine:
990         case eInputReaderGranularityAll:
991             tty.SetCanonical (true);
992             break;
993 
994         default:
995             break;
996         }
997     }
998 }
999 
1000 StreamSP
1001 Debugger::GetAsyncOutputStream ()
1002 {
1003     return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1004                                                CommandInterpreter::eBroadcastBitAsynchronousOutputData));
1005 }
1006 
1007 StreamSP
1008 Debugger::GetAsyncErrorStream ()
1009 {
1010     return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1011                                                CommandInterpreter::eBroadcastBitAsynchronousErrorData));
1012 }
1013 
1014 size_t
1015 Debugger::GetNumDebuggers()
1016 {
1017     if (g_shared_debugger_refcount > 0)
1018     {
1019         Mutex::Locker locker (GetDebuggerListMutex ());
1020         return GetDebuggerList().size();
1021     }
1022     return 0;
1023 }
1024 
1025 lldb::DebuggerSP
1026 Debugger::GetDebuggerAtIndex (size_t index)
1027 {
1028     DebuggerSP debugger_sp;
1029 
1030     if (g_shared_debugger_refcount > 0)
1031     {
1032         Mutex::Locker locker (GetDebuggerListMutex ());
1033         DebuggerList &debugger_list = GetDebuggerList();
1034 
1035         if (index < debugger_list.size())
1036             debugger_sp = debugger_list[index];
1037     }
1038 
1039     return debugger_sp;
1040 }
1041 
1042 DebuggerSP
1043 Debugger::FindDebuggerWithID (lldb::user_id_t id)
1044 {
1045     DebuggerSP debugger_sp;
1046 
1047     if (g_shared_debugger_refcount > 0)
1048     {
1049         Mutex::Locker locker (GetDebuggerListMutex ());
1050         DebuggerList &debugger_list = GetDebuggerList();
1051         DebuggerList::iterator pos, end = debugger_list.end();
1052         for (pos = debugger_list.begin(); pos != end; ++pos)
1053         {
1054             if ((*pos).get()->GetID() == id)
1055             {
1056                 debugger_sp = *pos;
1057                 break;
1058             }
1059         }
1060     }
1061     return debugger_sp;
1062 }
1063 
1064 static void
1065 TestPromptFormats (StackFrame *frame)
1066 {
1067     if (frame == NULL)
1068         return;
1069 
1070     StreamString s;
1071     const char *prompt_format =
1072     "{addr = '${addr}'\n}"
1073     "{process.id = '${process.id}'\n}"
1074     "{process.name = '${process.name}'\n}"
1075     "{process.file.basename = '${process.file.basename}'\n}"
1076     "{process.file.fullpath = '${process.file.fullpath}'\n}"
1077     "{thread.id = '${thread.id}'\n}"
1078     "{thread.index = '${thread.index}'\n}"
1079     "{thread.name = '${thread.name}'\n}"
1080     "{thread.queue = '${thread.queue}'\n}"
1081     "{thread.stop-reason = '${thread.stop-reason}'\n}"
1082     "{target.arch = '${target.arch}'\n}"
1083     "{module.file.basename = '${module.file.basename}'\n}"
1084     "{module.file.fullpath = '${module.file.fullpath}'\n}"
1085     "{file.basename = '${file.basename}'\n}"
1086     "{file.fullpath = '${file.fullpath}'\n}"
1087     "{frame.index = '${frame.index}'\n}"
1088     "{frame.pc = '${frame.pc}'\n}"
1089     "{frame.sp = '${frame.sp}'\n}"
1090     "{frame.fp = '${frame.fp}'\n}"
1091     "{frame.flags = '${frame.flags}'\n}"
1092     "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1093     "{frame.reg.rip = '${frame.reg.rip}'\n}"
1094     "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1095     "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1096     "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1097     "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1098     "{frame.reg.carp = '${frame.reg.carp}'\n}"
1099     "{function.id = '${function.id}'\n}"
1100     "{function.name = '${function.name}'\n}"
1101     "{function.name-with-args = '${function.name-with-args}'\n}"
1102     "{function.addr-offset = '${function.addr-offset}'\n}"
1103     "{function.line-offset = '${function.line-offset}'\n}"
1104     "{function.pc-offset = '${function.pc-offset}'\n}"
1105     "{line.file.basename = '${line.file.basename}'\n}"
1106     "{line.file.fullpath = '${line.file.fullpath}'\n}"
1107     "{line.number = '${line.number}'\n}"
1108     "{line.start-addr = '${line.start-addr}'\n}"
1109     "{line.end-addr = '${line.end-addr}'\n}"
1110 ;
1111 
1112     SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1113     ExecutionContext exe_ctx;
1114     frame->CalculateExecutionContext(exe_ctx);
1115     const char *end = NULL;
1116     if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, &end))
1117     {
1118         printf("%s\n", s.GetData());
1119     }
1120     else
1121     {
1122         printf ("error: at '%s'\n", end);
1123         printf ("what we got: %s\n", s.GetData());
1124     }
1125 }
1126 
1127 static bool
1128 ScanFormatDescriptor (const char* var_name_begin,
1129                       const char* var_name_end,
1130                       const char** var_name_final,
1131                       const char** percent_position,
1132                       Format* custom_format,
1133                       ValueObject::ValueObjectRepresentationStyle* val_obj_display)
1134 {
1135     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
1136     *percent_position = ::strchr(var_name_begin,'%');
1137     if (!*percent_position || *percent_position > var_name_end)
1138     {
1139         if (log)
1140             log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
1141         *var_name_final = var_name_end;
1142     }
1143     else
1144     {
1145         *var_name_final = *percent_position;
1146         char* format_name = new char[var_name_end-*var_name_final]; format_name[var_name_end-*var_name_final-1] = '\0';
1147         memcpy(format_name, *var_name_final+1, var_name_end-*var_name_final-1);
1148         if (log)
1149             log->Printf("ScanFormatDescriptor] parsing %s as a format descriptor", format_name);
1150         if ( !FormatManager::GetFormatFromCString(format_name,
1151                                                   true,
1152                                                   *custom_format) )
1153         {
1154             if (log)
1155                 log->Printf("ScanFormatDescriptor] %s is an unknown format", format_name);
1156             // if this is an @ sign, print ObjC description
1157             if (*format_name == '@')
1158                 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
1159             // if this is a V, print the value using the default format
1160             else if (*format_name == 'V')
1161                 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1162             // if this is an L, print the location of the value
1163             else if (*format_name == 'L')
1164                 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
1165             // if this is an S, print the summary after all
1166             else if (*format_name == 'S')
1167                 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
1168             else if (*format_name == '#')
1169                 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
1170             else if (*format_name == 'T')
1171                 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
1172             else if (log)
1173                 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name);
1174         }
1175         // a good custom format tells us to print the value using it
1176         else
1177         {
1178             if (log)
1179                 log->Printf("ScanFormatDescriptor] will display value for this VO");
1180             *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1181         }
1182         delete format_name;
1183     }
1184     if (log)
1185         log->Printf("ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
1186                     *custom_format,
1187                     *val_obj_display);
1188     return true;
1189 }
1190 
1191 static bool
1192 ScanBracketedRange (const char* var_name_begin,
1193                     const char* var_name_end,
1194                     const char* var_name_final,
1195                     const char** open_bracket_position,
1196                     const char** separator_position,
1197                     const char** close_bracket_position,
1198                     const char** var_name_final_if_array_range,
1199                     int64_t* index_lower,
1200                     int64_t* index_higher)
1201 {
1202     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
1203     *open_bracket_position = ::strchr(var_name_begin,'[');
1204     if (*open_bracket_position && *open_bracket_position < var_name_final)
1205     {
1206         *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1207         *close_bracket_position = ::strchr(*open_bracket_position,']');
1208         // as usual, we assume that [] will come before %
1209         //printf("trying to expand a []\n");
1210         *var_name_final_if_array_range = *open_bracket_position;
1211         if (*close_bracket_position - *open_bracket_position == 1)
1212         {
1213             if (log)
1214                 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
1215             *index_lower = 0;
1216         }
1217         else if (*separator_position == NULL || *separator_position > var_name_end)
1218         {
1219             char *end = NULL;
1220             *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1221             *index_higher = *index_lower;
1222             if (log)
1223                 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower);
1224         }
1225         else if (*close_bracket_position && *close_bracket_position < var_name_end)
1226         {
1227             char *end = NULL;
1228             *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1229             *index_higher = ::strtoul (*separator_position+1, &end, 0);
1230             if (log)
1231                 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher);
1232         }
1233         else
1234         {
1235             if (log)
1236                 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
1237             return false;
1238         }
1239         if (*index_lower > *index_higher && *index_higher > 0)
1240         {
1241             if (log)
1242                 log->Printf("[ScanBracketedRange] swapping indices");
1243             int64_t temp = *index_lower;
1244             *index_lower = *index_higher;
1245             *index_higher = temp;
1246         }
1247     }
1248     else if (log)
1249             log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
1250     return true;
1251 }
1252 
1253 static ValueObjectSP
1254 ExpandIndexedExpression (ValueObject* valobj,
1255                          size_t index,
1256                          StackFrame* frame,
1257                          bool deref_pointer)
1258 {
1259     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
1260     const char* ptr_deref_format = "[%d]";
1261     std::string ptr_deref_buffer(10,0);
1262     ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
1263     if (log)
1264         log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str());
1265     const char* first_unparsed;
1266     ValueObject::GetValueForExpressionPathOptions options;
1267     ValueObject::ExpressionPathEndResultType final_value_type;
1268     ValueObject::ExpressionPathScanEndReason reason_to_stop;
1269     ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
1270     ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(),
1271                                                           &first_unparsed,
1272                                                           &reason_to_stop,
1273                                                           &final_value_type,
1274                                                           options,
1275                                                           &what_next);
1276     if (!item)
1277     {
1278         if (log)
1279             log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
1280                " final_value_type %d",
1281                first_unparsed, reason_to_stop, final_value_type);
1282     }
1283     else
1284     {
1285         if (log)
1286             log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1287                " final_value_type %d",
1288                first_unparsed, reason_to_stop, final_value_type);
1289     }
1290     return item;
1291 }
1292 
1293 bool
1294 Debugger::FormatPrompt
1295 (
1296     const char *format,
1297     const SymbolContext *sc,
1298     const ExecutionContext *exe_ctx,
1299     const Address *addr,
1300     Stream &s,
1301     const char **end,
1302     ValueObject* valobj
1303 )
1304 {
1305     ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
1306     bool success = true;
1307     const char *p;
1308     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
1309     for (p = format; *p != '\0'; ++p)
1310     {
1311         if (realvalobj)
1312         {
1313             valobj = realvalobj;
1314             realvalobj = NULL;
1315         }
1316         size_t non_special_chars = ::strcspn (p, "${}\\");
1317         if (non_special_chars > 0)
1318         {
1319             if (success)
1320                 s.Write (p, non_special_chars);
1321             p += non_special_chars;
1322         }
1323 
1324         if (*p == '\0')
1325         {
1326             break;
1327         }
1328         else if (*p == '{')
1329         {
1330             // Start a new scope that must have everything it needs if it is to
1331             // to make it into the final output stream "s". If you want to make
1332             // a format that only prints out the function or symbol name if there
1333             // is one in the symbol context you can use:
1334             //      "{function =${function.name}}"
1335             // The first '{' starts a new scope that end with the matching '}' at
1336             // the end of the string. The contents "function =${function.name}"
1337             // will then be evaluated and only be output if there is a function
1338             // or symbol with a valid name.
1339             StreamString sub_strm;
1340 
1341             ++p;  // Skip the '{'
1342 
1343             if (FormatPrompt (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
1344             {
1345                 // The stream had all it needed
1346                 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1347             }
1348             if (*p != '}')
1349             {
1350                 success = false;
1351                 break;
1352             }
1353         }
1354         else if (*p == '}')
1355         {
1356             // End of a enclosing scope
1357             break;
1358         }
1359         else if (*p == '$')
1360         {
1361             // We have a prompt variable to print
1362             ++p;
1363             if (*p == '{')
1364             {
1365                 ++p;
1366                 const char *var_name_begin = p;
1367                 const char *var_name_end = ::strchr (p, '}');
1368 
1369                 if (var_name_end && var_name_begin < var_name_end)
1370                 {
1371                     // if we have already failed to parse, skip this variable
1372                     if (success)
1373                     {
1374                         const char *cstr = NULL;
1375                         Address format_addr;
1376                         bool calculate_format_addr_function_offset = false;
1377                         // Set reg_kind and reg_num to invalid values
1378                         RegisterKind reg_kind = kNumRegisterKinds;
1379                         uint32_t reg_num = LLDB_INVALID_REGNUM;
1380                         FileSpec format_file_spec;
1381                         const RegisterInfo *reg_info = NULL;
1382                         RegisterContext *reg_ctx = NULL;
1383                         bool do_deref_pointer = false;
1384                         ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1385                         ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
1386 
1387                         // Each variable must set success to true below...
1388                         bool var_success = false;
1389                         switch (var_name_begin[0])
1390                         {
1391                         case '*':
1392                         case 'v':
1393                         case 's':
1394                             {
1395                                 if (!valobj)
1396                                     break;
1397 
1398                                 if (log)
1399                                     log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
1400 
1401                                 // check for *var and *svar
1402                                 if (*var_name_begin == '*')
1403                                 {
1404                                     do_deref_pointer = true;
1405                                     var_name_begin++;
1406                                 }
1407 
1408                                 if (log)
1409                                     log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
1410 
1411                                 if (*var_name_begin == 's')
1412                                 {
1413                                     if (!valobj->IsSynthetic())
1414                                         valobj = valobj->GetSyntheticValue().get();
1415                                     if (!valobj)
1416                                         break;
1417                                     var_name_begin++;
1418                                 }
1419 
1420                                 if (log)
1421                                     log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
1422 
1423                                 // should be a 'v' by now
1424                                 if (*var_name_begin != 'v')
1425                                     break;
1426 
1427                                 if (log)
1428                                     log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
1429 
1430                                 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
1431                                                                                   ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
1432                                 ValueObject::GetValueForExpressionPathOptions options;
1433                                 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
1434                                 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
1435                                 ValueObject* target = NULL;
1436                                 Format custom_format = eFormatInvalid;
1437                                 const char* var_name_final = NULL;
1438                                 const char* var_name_final_if_array_range = NULL;
1439                                 const char* close_bracket_position = NULL;
1440                                 int64_t index_lower = -1;
1441                                 int64_t index_higher = -1;
1442                                 bool is_array_range = false;
1443                                 const char* first_unparsed;
1444                                 bool was_plain_var = false;
1445                                 bool was_var_format = false;
1446                                 bool was_var_indexed = false;
1447 
1448                                 if (!valobj) break;
1449                                 // simplest case ${var}, just print valobj's value
1450                                 if (::strncmp (var_name_begin, "var}", strlen("var}")) == 0)
1451                                 {
1452                                     was_plain_var = true;
1453                                     target = valobj;
1454                                     val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1455                                 }
1456                                 else if (::strncmp(var_name_begin,"var%",strlen("var%")) == 0)
1457                                 {
1458                                     was_var_format = true;
1459                                     // this is a variable with some custom format applied to it
1460                                     const char* percent_position;
1461                                     target = valobj;
1462                                     val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
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                                     // this is ${var.something} or multiple .something nested
1471                                 else if (::strncmp (var_name_begin, "var", strlen("var")) == 0)
1472                                 {
1473                                     if (::strncmp(var_name_begin, "var[", strlen("var[")) == 0)
1474                                         was_var_indexed = true;
1475                                     const char* percent_position;
1476                                     ScanFormatDescriptor (var_name_begin,
1477                                                           var_name_end,
1478                                                           &var_name_final,
1479                                                           &percent_position,
1480                                                           &custom_format,
1481                                                           &val_obj_display);
1482 
1483                                     const char* open_bracket_position;
1484                                     const char* separator_position;
1485                                     ScanBracketedRange (var_name_begin,
1486                                                         var_name_end,
1487                                                         var_name_final,
1488                                                         &open_bracket_position,
1489                                                         &separator_position,
1490                                                         &close_bracket_position,
1491                                                         &var_name_final_if_array_range,
1492                                                         &index_lower,
1493                                                         &index_higher);
1494 
1495                                     Error error;
1496 
1497                                     std::string expr_path(var_name_final-var_name_begin-1,0);
1498                                     memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3);
1499 
1500                                     if (log)
1501                                         log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str());
1502 
1503                                     target = valobj->GetValueForExpressionPath(expr_path.c_str(),
1504                                                                              &first_unparsed,
1505                                                                              &reason_to_stop,
1506                                                                              &final_value_type,
1507                                                                              options,
1508                                                                              &what_next).get();
1509 
1510                                     if (!target)
1511                                     {
1512                                         if (log)
1513                                             log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
1514                                                " final_value_type %d",
1515                                                first_unparsed, reason_to_stop, final_value_type);
1516                                         break;
1517                                     }
1518                                     else
1519                                     {
1520                                         if (log)
1521                                             log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1522                                                " final_value_type %d",
1523                                                first_unparsed, reason_to_stop, final_value_type);
1524                                     }
1525                                 }
1526                                 else
1527                                     break;
1528 
1529                                 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1530                                                   final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
1531 
1532                                 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
1533 
1534                                 if (do_deref_pointer && !is_array_range)
1535                                 {
1536                                     // I have not deref-ed yet, let's do it
1537                                     // this happens when we are not going through GetValueForVariableExpressionPath
1538                                     // to get to the target ValueObject
1539                                     Error error;
1540                                     target = target->Dereference(error).get();
1541                                     if (error.Fail())
1542                                     {
1543                                         if (log)
1544                                             log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
1545                                         break;
1546                                     }
1547                                     do_deref_pointer = false;
1548                                 }
1549 
1550                                 // <rdar://problem/11338654>
1551                                 // we do not want to use the summary for a bitfield of type T:n
1552                                 // if we were originally dealing with just a T - that would get
1553                                 // us into an endless recursion
1554                                 if (target->IsBitfield() && was_var_indexed)
1555                                 {
1556                                     // TODO: check for a (T:n)-specific summary - we should still obey that
1557                                     StreamString bitfield_name;
1558                                     bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1559                                     lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1560                                     if (!DataVisualization::GetSummaryForType(type_sp))
1561                                         val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1562                                 }
1563 
1564                                 // TODO use flags for these
1565                                 bool is_array = ClangASTContext::IsArrayType(target->GetClangType(), NULL, NULL, NULL);
1566                                 bool is_pointer = ClangASTContext::IsPointerType(target->GetClangType());
1567                                 bool is_aggregate = ClangASTContext::IsAggregateType(target->GetClangType());
1568 
1569                                 if ((is_array || is_pointer) && (!is_array_range) && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) // this should be wrong, but there are some exceptions
1570                                 {
1571                                     StreamString str_temp;
1572                                     if (log)
1573                                         log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
1574 
1575                                     if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format))
1576                                     {
1577                                         // try to use the special cases
1578                                         var_success = target->DumpPrintableRepresentation(str_temp,
1579                                                                                           val_obj_display,
1580                                                                                           custom_format);
1581                                         if (log)
1582                                             log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
1583 
1584                                         // should not happen
1585                                         if (var_success)
1586                                             s << str_temp.GetData();
1587                                         var_success = true;
1588                                         break;
1589                                     }
1590                                     else
1591                                     {
1592                                         if (was_plain_var) // if ${var}
1593                                         {
1594                                             s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1595                                         }
1596                                         else if (is_pointer) // if pointer, value is the address stored
1597                                         {
1598                                             target->DumpPrintableRepresentation (s,
1599                                                                                  val_obj_display,
1600                                                                                  custom_format,
1601                                                                                  ValueObject::ePrintableRepresentationSpecialCasesDisable);
1602                                         }
1603                                         var_success = true;
1604                                         break;
1605                                     }
1606                                 }
1607 
1608                                 // if directly trying to print ${var}, and this is an aggregate, display a nice
1609                                 // type @ location message
1610                                 if (is_aggregate && was_plain_var)
1611                                 {
1612                                     s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1613                                     var_success = true;
1614                                     break;
1615                                 }
1616 
1617                                 // if directly trying to print ${var%V}, and this is an aggregate, do not let the user do it
1618                                 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
1619                                 {
1620                                     s << "<invalid use of aggregate type>";
1621                                     var_success = true;
1622                                     break;
1623                                 }
1624 
1625                                 if (!is_array_range)
1626                                 {
1627                                     if (log)
1628                                         log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
1629                                     var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1630                                 }
1631                                 else
1632                                 {
1633                                     if (log)
1634                                         log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
1635                                     if (!is_array && !is_pointer)
1636                                         break;
1637                                     if (log)
1638                                         log->Printf("[Debugger::FormatPrompt] handle as array");
1639                                     const char* special_directions = NULL;
1640                                     StreamString special_directions_writer;
1641                                     if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1642                                     {
1643                                         ConstString additional_data;
1644                                         additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1645                                         special_directions_writer.Printf("${%svar%s}",
1646                                                                          do_deref_pointer ? "*" : "",
1647                                                                          additional_data.GetCString());
1648                                         special_directions = special_directions_writer.GetData();
1649                                     }
1650 
1651                                     // let us display items index_lower thru index_higher of this array
1652                                     s.PutChar('[');
1653                                     var_success = true;
1654 
1655                                     if (index_higher < 0)
1656                                         index_higher = valobj->GetNumChildren() - 1;
1657 
1658                                     uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
1659 
1660                                     for (;index_lower<=index_higher;index_lower++)
1661                                     {
1662                                         ValueObject* item = ExpandIndexedExpression (target,
1663                                                                                      index_lower,
1664                                                                                      exe_ctx->GetFramePtr(),
1665                                                                                      false).get();
1666 
1667                                         if (!item)
1668                                         {
1669                                             if (log)
1670                                                 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower);
1671                                         }
1672                                         else
1673                                         {
1674                                             if (log)
1675                                                 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
1676                                         }
1677 
1678                                         if (!special_directions)
1679                                             var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1680                                         else
1681                                             var_success &= FormatPrompt(special_directions, sc, exe_ctx, addr, s, NULL, item);
1682 
1683                                         if (--max_num_children == 0)
1684                                         {
1685                                             s.PutCString(", ...");
1686                                             break;
1687                                         }
1688 
1689                                         if (index_lower < index_higher)
1690                                             s.PutChar(',');
1691                                     }
1692                                     s.PutChar(']');
1693                                 }
1694                             }
1695                             break;
1696                         case 'a':
1697                             if (::strncmp (var_name_begin, "addr}", strlen("addr}")) == 0)
1698                             {
1699                                 if (addr && addr->IsValid())
1700                                 {
1701                                     var_success = true;
1702                                     format_addr = *addr;
1703                                 }
1704                             }
1705                             else if (::strncmp (var_name_begin, "ansi.", strlen("ansi.")) == 0)
1706                             {
1707                                 var_success = true;
1708                                 var_name_begin += strlen("ansi."); // Skip the "ansi."
1709                                 if (::strncmp (var_name_begin, "fg.", strlen("fg.")) == 0)
1710                                 {
1711                                     var_name_begin += strlen("fg."); // Skip the "fg."
1712                                     if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1713                                     {
1714                                         s.Printf ("%s%s%s",
1715                                                   lldb_utility::ansi::k_escape_start,
1716                                                   lldb_utility::ansi::k_fg_black,
1717                                                   lldb_utility::ansi::k_escape_end);
1718                                     }
1719                                     else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1720                                     {
1721                                         s.Printf ("%s%s%s",
1722                                                   lldb_utility::ansi::k_escape_start,
1723                                                   lldb_utility::ansi::k_fg_red,
1724                                                   lldb_utility::ansi::k_escape_end);
1725                                     }
1726                                     else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1727                                     {
1728                                         s.Printf ("%s%s%s",
1729                                                   lldb_utility::ansi::k_escape_start,
1730                                                   lldb_utility::ansi::k_fg_green,
1731                                                   lldb_utility::ansi::k_escape_end);
1732                                     }
1733                                     else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1734                                     {
1735                                         s.Printf ("%s%s%s",
1736                                                   lldb_utility::ansi::k_escape_start,
1737                                                   lldb_utility::ansi::k_fg_yellow,
1738                                                   lldb_utility::ansi::k_escape_end);
1739                                     }
1740                                     else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1741                                     {
1742                                         s.Printf ("%s%s%s",
1743                                                   lldb_utility::ansi::k_escape_start,
1744                                                   lldb_utility::ansi::k_fg_blue,
1745                                                   lldb_utility::ansi::k_escape_end);
1746                                     }
1747                                     else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1748                                     {
1749                                         s.Printf ("%s%s%s",
1750                                                   lldb_utility::ansi::k_escape_start,
1751                                                   lldb_utility::ansi::k_fg_purple,
1752                                                   lldb_utility::ansi::k_escape_end);
1753                                     }
1754                                     else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1755                                     {
1756                                         s.Printf ("%s%s%s",
1757                                                   lldb_utility::ansi::k_escape_start,
1758                                                   lldb_utility::ansi::k_fg_cyan,
1759                                                   lldb_utility::ansi::k_escape_end);
1760                                     }
1761                                     else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1762                                     {
1763                                         s.Printf ("%s%s%s",
1764                                                   lldb_utility::ansi::k_escape_start,
1765                                                   lldb_utility::ansi::k_fg_white,
1766                                                   lldb_utility::ansi::k_escape_end);
1767                                     }
1768                                     else
1769                                     {
1770                                         var_success = false;
1771                                     }
1772                                 }
1773                                 else if (::strncmp (var_name_begin, "bg.", strlen("bg.")) == 0)
1774                                 {
1775                                     var_name_begin += strlen("bg."); // Skip the "bg."
1776                                     if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1777                                     {
1778                                         s.Printf ("%s%s%s",
1779                                                   lldb_utility::ansi::k_escape_start,
1780                                                   lldb_utility::ansi::k_bg_black,
1781                                                   lldb_utility::ansi::k_escape_end);
1782                                     }
1783                                     else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1784                                     {
1785                                         s.Printf ("%s%s%s",
1786                                                   lldb_utility::ansi::k_escape_start,
1787                                                   lldb_utility::ansi::k_bg_red,
1788                                                   lldb_utility::ansi::k_escape_end);
1789                                     }
1790                                     else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1791                                     {
1792                                         s.Printf ("%s%s%s",
1793                                                   lldb_utility::ansi::k_escape_start,
1794                                                   lldb_utility::ansi::k_bg_green,
1795                                                   lldb_utility::ansi::k_escape_end);
1796                                     }
1797                                     else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1798                                     {
1799                                         s.Printf ("%s%s%s",
1800                                                   lldb_utility::ansi::k_escape_start,
1801                                                   lldb_utility::ansi::k_bg_yellow,
1802                                                   lldb_utility::ansi::k_escape_end);
1803                                     }
1804                                     else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1805                                     {
1806                                         s.Printf ("%s%s%s",
1807                                                   lldb_utility::ansi::k_escape_start,
1808                                                   lldb_utility::ansi::k_bg_blue,
1809                                                   lldb_utility::ansi::k_escape_end);
1810                                     }
1811                                     else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1812                                     {
1813                                         s.Printf ("%s%s%s",
1814                                                   lldb_utility::ansi::k_escape_start,
1815                                                   lldb_utility::ansi::k_bg_purple,
1816                                                   lldb_utility::ansi::k_escape_end);
1817                                     }
1818                                     else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1819                                     {
1820                                         s.Printf ("%s%s%s",
1821                                                   lldb_utility::ansi::k_escape_start,
1822                                                   lldb_utility::ansi::k_bg_cyan,
1823                                                   lldb_utility::ansi::k_escape_end);
1824                                     }
1825                                     else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1826                                     {
1827                                         s.Printf ("%s%s%s",
1828                                                   lldb_utility::ansi::k_escape_start,
1829                                                   lldb_utility::ansi::k_bg_white,
1830                                                   lldb_utility::ansi::k_escape_end);
1831                                     }
1832                                     else
1833                                     {
1834                                         var_success = false;
1835                                     }
1836                                 }
1837                                 else if (::strncmp (var_name_begin, "normal}", strlen ("normal}")) == 0)
1838                                 {
1839                                     s.Printf ("%s%s%s",
1840                                               lldb_utility::ansi::k_escape_start,
1841                                               lldb_utility::ansi::k_ctrl_normal,
1842                                               lldb_utility::ansi::k_escape_end);
1843                                 }
1844                                 else if (::strncmp (var_name_begin, "bold}", strlen("bold}")) == 0)
1845                                 {
1846                                     s.Printf ("%s%s%s",
1847                                               lldb_utility::ansi::k_escape_start,
1848                                               lldb_utility::ansi::k_ctrl_bold,
1849                                               lldb_utility::ansi::k_escape_end);
1850                                 }
1851                                 else if (::strncmp (var_name_begin, "faint}", strlen("faint}")) == 0)
1852                                 {
1853                                     s.Printf ("%s%s%s",
1854                                               lldb_utility::ansi::k_escape_start,
1855                                               lldb_utility::ansi::k_ctrl_faint,
1856                                               lldb_utility::ansi::k_escape_end);
1857                                 }
1858                                 else if (::strncmp (var_name_begin, "italic}", strlen("italic}")) == 0)
1859                                 {
1860                                     s.Printf ("%s%s%s",
1861                                               lldb_utility::ansi::k_escape_start,
1862                                               lldb_utility::ansi::k_ctrl_italic,
1863                                               lldb_utility::ansi::k_escape_end);
1864                                 }
1865                                 else if (::strncmp (var_name_begin, "underline}", strlen("underline}")) == 0)
1866                                 {
1867                                     s.Printf ("%s%s%s",
1868                                               lldb_utility::ansi::k_escape_start,
1869                                               lldb_utility::ansi::k_ctrl_underline,
1870                                               lldb_utility::ansi::k_escape_end);
1871                                 }
1872                                 else if (::strncmp (var_name_begin, "slow-blink}", strlen("slow-blink}")) == 0)
1873                                 {
1874                                     s.Printf ("%s%s%s",
1875                                               lldb_utility::ansi::k_escape_start,
1876                                               lldb_utility::ansi::k_ctrl_slow_blink,
1877                                               lldb_utility::ansi::k_escape_end);
1878                                 }
1879                                 else if (::strncmp (var_name_begin, "fast-blink}", strlen("fast-blink}")) == 0)
1880                                 {
1881                                     s.Printf ("%s%s%s",
1882                                               lldb_utility::ansi::k_escape_start,
1883                                               lldb_utility::ansi::k_ctrl_fast_blink,
1884                                               lldb_utility::ansi::k_escape_end);
1885                                 }
1886                                 else if (::strncmp (var_name_begin, "negative}", strlen("negative}")) == 0)
1887                                 {
1888                                     s.Printf ("%s%s%s",
1889                                               lldb_utility::ansi::k_escape_start,
1890                                               lldb_utility::ansi::k_ctrl_negative,
1891                                               lldb_utility::ansi::k_escape_end);
1892                                 }
1893                                 else if (::strncmp (var_name_begin, "conceal}", strlen("conceal}")) == 0)
1894                                 {
1895                                     s.Printf ("%s%s%s",
1896                                               lldb_utility::ansi::k_escape_start,
1897                                               lldb_utility::ansi::k_ctrl_conceal,
1898                                               lldb_utility::ansi::k_escape_end);
1899 
1900                                 }
1901                                 else if (::strncmp (var_name_begin, "crossed-out}", strlen("crossed-out}")) == 0)
1902                                 {
1903                                     s.Printf ("%s%s%s",
1904                                               lldb_utility::ansi::k_escape_start,
1905                                               lldb_utility::ansi::k_ctrl_crossed_out,
1906                                               lldb_utility::ansi::k_escape_end);
1907                                 }
1908                                 else
1909                                 {
1910                                     var_success = false;
1911                                 }
1912                             }
1913                             break;
1914 
1915                         case 'p':
1916                             if (::strncmp (var_name_begin, "process.", strlen("process.")) == 0)
1917                             {
1918                                 if (exe_ctx)
1919                                 {
1920                                     Process *process = exe_ctx->GetProcessPtr();
1921                                     if (process)
1922                                     {
1923                                         var_name_begin += ::strlen ("process.");
1924                                         if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1925                                         {
1926                                             s.Printf("%" PRIu64, process->GetID());
1927                                             var_success = true;
1928                                         }
1929                                         else if ((::strncmp (var_name_begin, "name}", strlen("name}")) == 0) ||
1930                                                  (::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) ||
1931                                                  (::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0))
1932                                         {
1933                                             Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1934                                             if (exe_module)
1935                                             {
1936                                                 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1937                                                 {
1938                                                     format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
1939                                                     var_success = format_file_spec;
1940                                                 }
1941                                                 else
1942                                                 {
1943                                                     format_file_spec = exe_module->GetFileSpec();
1944                                                     var_success = format_file_spec;
1945                                                 }
1946                                             }
1947                                         }
1948                                     }
1949                                 }
1950                             }
1951                             break;
1952 
1953                         case 't':
1954                             if (::strncmp (var_name_begin, "thread.", strlen("thread.")) == 0)
1955                             {
1956                                 if (exe_ctx)
1957                                 {
1958                                     Thread *thread = exe_ctx->GetThreadPtr();
1959                                     if (thread)
1960                                     {
1961                                         var_name_begin += ::strlen ("thread.");
1962                                         if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1963                                         {
1964                                             s.Printf("0x%4.4" PRIx64, thread->GetID());
1965                                             var_success = true;
1966                                         }
1967                                         else if (::strncmp (var_name_begin, "protocol_id}", strlen("protocol_id}")) == 0)
1968                                         {
1969                                             s.Printf("0x%4.4" PRIx64, thread->GetProtocolID());
1970                                             var_success = true;
1971                                         }
1972                                         else if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1973                                         {
1974                                             s.Printf("%u", thread->GetIndexID());
1975                                             var_success = true;
1976                                         }
1977                                         else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1978                                         {
1979                                             cstr = thread->GetName();
1980                                             var_success = cstr && cstr[0];
1981                                             if (var_success)
1982                                                 s.PutCString(cstr);
1983                                         }
1984                                         else if (::strncmp (var_name_begin, "queue}", strlen("queue}")) == 0)
1985                                         {
1986                                             cstr = thread->GetQueueName();
1987                                             var_success = cstr && cstr[0];
1988                                             if (var_success)
1989                                                 s.PutCString(cstr);
1990                                         }
1991                                         else if (::strncmp (var_name_begin, "stop-reason}", strlen("stop-reason}")) == 0)
1992                                         {
1993                                             StopInfoSP stop_info_sp = thread->GetStopInfo ();
1994                                             if (stop_info_sp && stop_info_sp->IsValid())
1995                                             {
1996                                                 cstr = stop_info_sp->GetDescription();
1997                                                 if (cstr && cstr[0])
1998                                                 {
1999                                                     s.PutCString(cstr);
2000                                                     var_success = true;
2001                                                 }
2002                                             }
2003                                         }
2004                                         else if (::strncmp (var_name_begin, "return-value}", strlen("return-value}")) == 0)
2005                                         {
2006                                             StopInfoSP stop_info_sp = thread->GetStopInfo ();
2007                                             if (stop_info_sp && stop_info_sp->IsValid())
2008                                             {
2009                                                 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
2010                                                 if (return_valobj_sp)
2011                                                 {
2012                                                     ValueObject::DumpValueObject (s, return_valobj_sp.get());
2013                                                     var_success = true;
2014                                                 }
2015                                             }
2016                                         }
2017                                     }
2018                                 }
2019                             }
2020                             else if (::strncmp (var_name_begin, "target.", strlen("target.")) == 0)
2021                             {
2022                                 // TODO: hookup properties
2023 //                                if (!target_properties_sp)
2024 //                                {
2025 //                                    Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2026 //                                    if (target)
2027 //                                        target_properties_sp = target->GetProperties();
2028 //                                }
2029 //
2030 //                                if (target_properties_sp)
2031 //                                {
2032 //                                    var_name_begin += ::strlen ("target.");
2033 //                                    const char *end_property = strchr(var_name_begin, '}');
2034 //                                    if (end_property)
2035 //                                    {
2036 //                                        ConstString property_name(var_name_begin, end_property - var_name_begin);
2037 //                                        std::string property_value (target_properties_sp->GetPropertyValue(property_name));
2038 //                                        if (!property_value.empty())
2039 //                                        {
2040 //                                            s.PutCString (property_value.c_str());
2041 //                                            var_success = true;
2042 //                                        }
2043 //                                    }
2044 //                                }
2045                                 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2046                                 if (target)
2047                                 {
2048                                     var_name_begin += ::strlen ("target.");
2049                                     if (::strncmp (var_name_begin, "arch}", strlen("arch}")) == 0)
2050                                     {
2051                                         ArchSpec arch (target->GetArchitecture ());
2052                                         if (arch.IsValid())
2053                                         {
2054                                             s.PutCString (arch.GetArchitectureName());
2055                                             var_success = true;
2056                                         }
2057                                     }
2058                                 }
2059                             }
2060                             break;
2061 
2062 
2063                         case 'm':
2064                             if (::strncmp (var_name_begin, "module.", strlen("module.")) == 0)
2065                             {
2066                                 if (sc && sc->module_sp.get())
2067                                 {
2068                                     Module *module = sc->module_sp.get();
2069                                     var_name_begin += ::strlen ("module.");
2070 
2071                                     if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2072                                     {
2073                                         if (module->GetFileSpec())
2074                                         {
2075                                             var_name_begin += ::strlen ("file.");
2076 
2077                                             if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2078                                             {
2079                                                 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
2080                                                 var_success = format_file_spec;
2081                                             }
2082                                             else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2083                                             {
2084                                                 format_file_spec = module->GetFileSpec();
2085                                                 var_success = format_file_spec;
2086                                             }
2087                                         }
2088                                     }
2089                                 }
2090                             }
2091                             break;
2092 
2093 
2094                         case 'f':
2095                             if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2096                             {
2097                                 if (sc && sc->comp_unit != NULL)
2098                                 {
2099                                     var_name_begin += ::strlen ("file.");
2100 
2101                                     if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2102                                     {
2103                                         format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
2104                                         var_success = format_file_spec;
2105                                     }
2106                                     else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2107                                     {
2108                                         format_file_spec = *sc->comp_unit;
2109                                         var_success = format_file_spec;
2110                                     }
2111                                 }
2112                             }
2113                             else if (::strncmp (var_name_begin, "frame.", strlen("frame.")) == 0)
2114                             {
2115                                 if (exe_ctx)
2116                                 {
2117                                     StackFrame *frame = exe_ctx->GetFramePtr();
2118                                     if (frame)
2119                                     {
2120                                         var_name_begin += ::strlen ("frame.");
2121                                         if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
2122                                         {
2123                                             s.Printf("%u", frame->GetFrameIndex());
2124                                             var_success = true;
2125                                         }
2126                                         else if (::strncmp (var_name_begin, "pc}", strlen("pc}")) == 0)
2127                                         {
2128                                             reg_kind = eRegisterKindGeneric;
2129                                             reg_num = LLDB_REGNUM_GENERIC_PC;
2130                                             var_success = true;
2131                                         }
2132                                         else if (::strncmp (var_name_begin, "sp}", strlen("sp}")) == 0)
2133                                         {
2134                                             reg_kind = eRegisterKindGeneric;
2135                                             reg_num = LLDB_REGNUM_GENERIC_SP;
2136                                             var_success = true;
2137                                         }
2138                                         else if (::strncmp (var_name_begin, "fp}", strlen("fp}")) == 0)
2139                                         {
2140                                             reg_kind = eRegisterKindGeneric;
2141                                             reg_num = LLDB_REGNUM_GENERIC_FP;
2142                                             var_success = true;
2143                                         }
2144                                         else if (::strncmp (var_name_begin, "flags}", strlen("flags}")) == 0)
2145                                         {
2146                                             reg_kind = eRegisterKindGeneric;
2147                                             reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2148                                             var_success = true;
2149                                         }
2150                                         else if (::strncmp (var_name_begin, "reg.", strlen ("reg.")) == 0)
2151                                         {
2152                                             reg_ctx = frame->GetRegisterContext().get();
2153                                             if (reg_ctx)
2154                                             {
2155                                                 var_name_begin += ::strlen ("reg.");
2156                                                 if (var_name_begin < var_name_end)
2157                                                 {
2158                                                     std::string reg_name (var_name_begin, var_name_end);
2159                                                     reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2160                                                     if (reg_info)
2161                                                         var_success = true;
2162                                                 }
2163                                             }
2164                                         }
2165                                     }
2166                                 }
2167                             }
2168                             else if (::strncmp (var_name_begin, "function.", strlen("function.")) == 0)
2169                             {
2170                                 if (sc && (sc->function != NULL || sc->symbol != NULL))
2171                                 {
2172                                     var_name_begin += ::strlen ("function.");
2173                                     if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
2174                                     {
2175                                         if (sc->function)
2176                                             s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
2177                                         else
2178                                             s.Printf("symbol[%u]", sc->symbol->GetID());
2179 
2180                                         var_success = true;
2181                                     }
2182                                     else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
2183                                     {
2184                                         if (sc->function)
2185                                             cstr = sc->function->GetName().AsCString (NULL);
2186                                         else if (sc->symbol)
2187                                             cstr = sc->symbol->GetName().AsCString (NULL);
2188                                         if (cstr)
2189                                         {
2190                                             s.PutCString(cstr);
2191 
2192                                             if (sc->block)
2193                                             {
2194                                                 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2195                                                 if (inline_block)
2196                                                 {
2197                                                     const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2198                                                     if (inline_info)
2199                                                     {
2200                                                         s.PutCString(" [inlined] ");
2201                                                         inline_info->GetName().Dump(&s);
2202                                                     }
2203                                                 }
2204                                             }
2205                                             var_success = true;
2206                                         }
2207                                     }
2208                                     else if (::strncmp (var_name_begin, "name-with-args}", strlen("name-with-args}")) == 0)
2209                                     {
2210                                         // Print the function name with arguments in it
2211 
2212                                         if (sc->function)
2213                                         {
2214                                             var_success = true;
2215                                             ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2216                                             cstr = sc->function->GetName().AsCString (NULL);
2217                                             if (cstr)
2218                                             {
2219                                                 const InlineFunctionInfo *inline_info = NULL;
2220                                                 VariableListSP variable_list_sp;
2221                                                 bool get_function_vars = true;
2222                                                 if (sc->block)
2223                                                 {
2224                                                     Block *inline_block = sc->block->GetContainingInlinedBlock ();
2225 
2226                                                     if (inline_block)
2227                                                     {
2228                                                         get_function_vars = false;
2229                                                         inline_info = sc->block->GetInlinedFunctionInfo();
2230                                                         if (inline_info)
2231                                                             variable_list_sp = inline_block->GetBlockVariableList (true);
2232                                                     }
2233                                                 }
2234 
2235                                                 if (get_function_vars)
2236                                                 {
2237                                                     variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2238                                                 }
2239 
2240                                                 if (inline_info)
2241                                                 {
2242                                                     s.PutCString (cstr);
2243                                                     s.PutCString (" [inlined] ");
2244                                                     cstr = inline_info->GetName().GetCString();
2245                                                 }
2246 
2247                                                 VariableList args;
2248                                                 if (variable_list_sp)
2249                                                 {
2250                                                     const size_t num_variables = variable_list_sp->GetSize();
2251                                                     for (size_t var_idx = 0; var_idx < num_variables; ++var_idx)
2252                                                     {
2253                                                         VariableSP var_sp (variable_list_sp->GetVariableAtIndex(var_idx));
2254                                                         if (var_sp->GetScope() == eValueTypeVariableArgument)
2255                                                             args.AddVariable (var_sp);
2256                                                     }
2257 
2258                                                 }
2259                                                 if (args.GetSize() > 0)
2260                                                 {
2261                                                     const char *open_paren = strchr (cstr, '(');
2262                                                     const char *close_paren = NULL;
2263                                                     if (open_paren)
2264                                                     {
2265                                                         if (strncmp(open_paren, "(anonymous namespace)", strlen("(anonymous namespace)")) == 0)
2266                                                         {
2267                                                             open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '(');
2268                                                             if (open_paren)
2269                                                                 close_paren = strchr (open_paren, ')');
2270                                                         }
2271                                                         else
2272                                                             close_paren = strchr (open_paren, ')');
2273                                                     }
2274 
2275                                                     if (open_paren)
2276                                                         s.Write(cstr, open_paren - cstr + 1);
2277                                                     else
2278                                                     {
2279                                                         s.PutCString (cstr);
2280                                                         s.PutChar ('(');
2281                                                     }
2282                                                     const size_t num_args = args.GetSize();
2283                                                     for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2284                                                     {
2285                                                         VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2286                                                         ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
2287                                                         const char *var_name = var_value_sp->GetName().GetCString();
2288                                                         const char *var_value = var_value_sp->GetValueAsCString();
2289                                                         if (arg_idx > 0)
2290                                                             s.PutCString (", ");
2291                                                         if (var_value_sp->GetError().Success())
2292                                                             s.Printf ("%s=%s", var_name, var_value);
2293                                                         else
2294                                                             s.Printf ("%s=<unavailable>", var_name);
2295                                                     }
2296 
2297                                                     if (close_paren)
2298                                                         s.PutCString (close_paren);
2299                                                     else
2300                                                         s.PutChar(')');
2301 
2302                                                 }
2303                                                 else
2304                                                 {
2305                                                     s.PutCString(cstr);
2306                                                 }
2307                                             }
2308                                         }
2309                                         else if (sc->symbol)
2310                                         {
2311                                             cstr = sc->symbol->GetName().AsCString (NULL);
2312                                             if (cstr)
2313                                             {
2314                                                 s.PutCString(cstr);
2315                                                 var_success = true;
2316                                             }
2317                                         }
2318                                     }
2319                                     else if (::strncmp (var_name_begin, "addr-offset}", strlen("addr-offset}")) == 0)
2320                                     {
2321                                         var_success = addr != NULL;
2322                                         if (var_success)
2323                                         {
2324                                             format_addr = *addr;
2325                                             calculate_format_addr_function_offset = true;
2326                                         }
2327                                     }
2328                                     else if (::strncmp (var_name_begin, "line-offset}", strlen("line-offset}")) == 0)
2329                                     {
2330                                         var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2331                                         if (var_success)
2332                                         {
2333                                             format_addr = sc->line_entry.range.GetBaseAddress();
2334                                             calculate_format_addr_function_offset = true;
2335                                         }
2336                                     }
2337                                     else if (::strncmp (var_name_begin, "pc-offset}", strlen("pc-offset}")) == 0)
2338                                     {
2339                                         StackFrame *frame = exe_ctx->GetFramePtr();
2340                                         var_success = frame != NULL;
2341                                         if (var_success)
2342                                         {
2343                                             format_addr = frame->GetFrameCodeAddress();
2344                                             calculate_format_addr_function_offset = true;
2345                                         }
2346                                     }
2347                                 }
2348                             }
2349                             break;
2350 
2351                         case 'l':
2352                             if (::strncmp (var_name_begin, "line.", strlen("line.")) == 0)
2353                             {
2354                                 if (sc && sc->line_entry.IsValid())
2355                                 {
2356                                     var_name_begin += ::strlen ("line.");
2357                                     if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2358                                     {
2359                                         var_name_begin += ::strlen ("file.");
2360 
2361                                         if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2362                                         {
2363                                             format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
2364                                             var_success = format_file_spec;
2365                                         }
2366                                         else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2367                                         {
2368                                             format_file_spec = sc->line_entry.file;
2369                                             var_success = format_file_spec;
2370                                         }
2371                                     }
2372                                     else if (::strncmp (var_name_begin, "number}", strlen("number}")) == 0)
2373                                     {
2374                                         var_success = true;
2375                                         s.Printf("%u", sc->line_entry.line);
2376                                     }
2377                                     else if ((::strncmp (var_name_begin, "start-addr}", strlen("start-addr}")) == 0) ||
2378                                              (::strncmp (var_name_begin, "end-addr}", strlen("end-addr}")) == 0))
2379                                     {
2380                                         var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2381                                         if (var_success)
2382                                         {
2383                                             format_addr = sc->line_entry.range.GetBaseAddress();
2384                                             if (var_name_begin[0] == 'e')
2385                                                 format_addr.Slide (sc->line_entry.range.GetByteSize());
2386                                         }
2387                                     }
2388                                 }
2389                             }
2390                             break;
2391                         }
2392 
2393                         if (var_success)
2394                         {
2395                             // If format addr is valid, then we need to print an address
2396                             if (reg_num != LLDB_INVALID_REGNUM)
2397                             {
2398                                 StackFrame *frame = exe_ctx->GetFramePtr();
2399                                 // We have a register value to display...
2400                                 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2401                                 {
2402                                     format_addr = frame->GetFrameCodeAddress();
2403                                 }
2404                                 else
2405                                 {
2406                                     if (reg_ctx == NULL)
2407                                         reg_ctx = frame->GetRegisterContext().get();
2408 
2409                                     if (reg_ctx)
2410                                     {
2411                                         if (reg_kind != kNumRegisterKinds)
2412                                             reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2413                                         reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2414                                         var_success = reg_info != NULL;
2415                                     }
2416                                 }
2417                             }
2418 
2419                             if (reg_info != NULL)
2420                             {
2421                                 RegisterValue reg_value;
2422                                 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2423                                 if (var_success)
2424                                 {
2425                                     reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
2426                                 }
2427                             }
2428 
2429                             if (format_file_spec)
2430                             {
2431                                 s << format_file_spec;
2432                             }
2433 
2434                             // If format addr is valid, then we need to print an address
2435                             if (format_addr.IsValid())
2436                             {
2437                                 var_success = false;
2438 
2439                                 if (calculate_format_addr_function_offset)
2440                                 {
2441                                     Address func_addr;
2442 
2443                                     if (sc)
2444                                     {
2445                                         if (sc->function)
2446                                         {
2447                                             func_addr = sc->function->GetAddressRange().GetBaseAddress();
2448                                             if (sc->block)
2449                                             {
2450                                                 // Check to make sure we aren't in an inline
2451                                                 // function. If we are, use the inline block
2452                                                 // range that contains "format_addr" since
2453                                                 // blocks can be discontiguous.
2454                                                 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2455                                                 AddressRange inline_range;
2456                                                 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2457                                                     func_addr = inline_range.GetBaseAddress();
2458                                             }
2459                                         }
2460                                         else if (sc->symbol && sc->symbol->ValueIsAddress())
2461                                             func_addr = sc->symbol->GetAddress();
2462                                     }
2463 
2464                                     if (func_addr.IsValid())
2465                                     {
2466                                         if (func_addr.GetSection() == format_addr.GetSection())
2467                                         {
2468                                             addr_t func_file_addr = func_addr.GetFileAddress();
2469                                             addr_t addr_file_addr = format_addr.GetFileAddress();
2470                                             if (addr_file_addr > func_file_addr)
2471                                                 s.Printf(" + %" PRIu64, addr_file_addr - func_file_addr);
2472                                             else if (addr_file_addr < func_file_addr)
2473                                                 s.Printf(" - %" PRIu64, func_file_addr - addr_file_addr);
2474                                             var_success = true;
2475                                         }
2476                                         else
2477                                         {
2478                                             Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2479                                             if (target)
2480                                             {
2481                                                 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2482                                                 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2483                                                 if (addr_load_addr > func_load_addr)
2484                                                     s.Printf(" + %" PRIu64, addr_load_addr - func_load_addr);
2485                                                 else if (addr_load_addr < func_load_addr)
2486                                                     s.Printf(" - %" PRIu64, func_load_addr - addr_load_addr);
2487                                                 var_success = true;
2488                                             }
2489                                         }
2490                                     }
2491                                 }
2492                                 else
2493                                 {
2494                                     Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2495                                     addr_t vaddr = LLDB_INVALID_ADDRESS;
2496                                     if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2497                                         vaddr = format_addr.GetLoadAddress (target);
2498                                     if (vaddr == LLDB_INVALID_ADDRESS)
2499                                         vaddr = format_addr.GetFileAddress ();
2500 
2501                                     if (vaddr != LLDB_INVALID_ADDRESS)
2502                                     {
2503                                         int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
2504                                         if (addr_width == 0)
2505                                             addr_width = 16;
2506                                         s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
2507                                         var_success = true;
2508                                     }
2509                                 }
2510                             }
2511                         }
2512 
2513                         if (var_success == false)
2514                             success = false;
2515                     }
2516                     p = var_name_end;
2517                 }
2518                 else
2519                     break;
2520             }
2521             else
2522             {
2523                 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2524                 s.PutChar(*p);
2525             }
2526         }
2527         else if (*p == '\\')
2528         {
2529             ++p; // skip the slash
2530             switch (*p)
2531             {
2532             case 'a': s.PutChar ('\a'); break;
2533             case 'b': s.PutChar ('\b'); break;
2534             case 'f': s.PutChar ('\f'); break;
2535             case 'n': s.PutChar ('\n'); break;
2536             case 'r': s.PutChar ('\r'); break;
2537             case 't': s.PutChar ('\t'); break;
2538             case 'v': s.PutChar ('\v'); break;
2539             case '\'': s.PutChar ('\''); break;
2540             case '\\': s.PutChar ('\\'); break;
2541             case '0':
2542                 // 1 to 3 octal chars
2543                 {
2544                     // Make a string that can hold onto the initial zero char,
2545                     // up to 3 octal digits, and a terminating NULL.
2546                     char oct_str[5] = { 0, 0, 0, 0, 0 };
2547 
2548                     int i;
2549                     for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2550                         oct_str[i] = p[i];
2551 
2552                     // We don't want to consume the last octal character since
2553                     // the main for loop will do this for us, so we advance p by
2554                     // one less than i (even if i is zero)
2555                     p += i - 1;
2556                     unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2557                     if (octal_value <= UINT8_MAX)
2558                     {
2559                         s.PutChar((char)octal_value);
2560                     }
2561                 }
2562                 break;
2563 
2564             case 'x':
2565                 // hex number in the format
2566                 if (isxdigit(p[1]))
2567                 {
2568                     ++p;    // Skip the 'x'
2569 
2570                     // Make a string that can hold onto two hex chars plus a
2571                     // NULL terminator
2572                     char hex_str[3] = { 0,0,0 };
2573                     hex_str[0] = *p;
2574                     if (isxdigit(p[1]))
2575                     {
2576                         ++p; // Skip the first of the two hex chars
2577                         hex_str[1] = *p;
2578                     }
2579 
2580                     unsigned long hex_value = strtoul (hex_str, NULL, 16);
2581                     if (hex_value <= UINT8_MAX)
2582                         s.PutChar ((char)hex_value);
2583                 }
2584                 else
2585                 {
2586                     s.PutChar('x');
2587                 }
2588                 break;
2589 
2590             default:
2591                 // Just desensitize any other character by just printing what
2592                 // came after the '\'
2593                 s << *p;
2594                 break;
2595 
2596             }
2597 
2598         }
2599     }
2600     if (end)
2601         *end = p;
2602     return success;
2603 }
2604 
2605 void
2606 Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2607 {
2608     // For simplicity's sake, I am not going to deal with how to close down any
2609     // open logging streams, I just redirect everything from here on out to the
2610     // callback.
2611     m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2612 }
2613 
2614 bool
2615 Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2616 {
2617     Log::Callbacks log_callbacks;
2618 
2619     StreamSP log_stream_sp;
2620     if (m_log_callback_stream_sp)
2621     {
2622         log_stream_sp = m_log_callback_stream_sp;
2623         // For now when using the callback mode you always get thread & timestamp.
2624         log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2625     }
2626     else if (log_file == NULL || *log_file == '\0')
2627     {
2628         log_stream_sp.reset(new StreamFile(GetOutputFile().GetDescriptor(), false));
2629     }
2630     else
2631     {
2632         LogStreamMap::iterator pos = m_log_streams.find(log_file);
2633         if (pos != m_log_streams.end())
2634             log_stream_sp = pos->second.lock();
2635         if (!log_stream_sp)
2636         {
2637             log_stream_sp.reset (new StreamFile (log_file));
2638             m_log_streams[log_file] = log_stream_sp;
2639         }
2640     }
2641     assert (log_stream_sp.get());
2642 
2643     if (log_options == 0)
2644         log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2645 
2646     if (Log::GetLogChannelCallbacks (channel, log_callbacks))
2647     {
2648         log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2649         return true;
2650     }
2651     else
2652     {
2653         LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2654         if (log_channel_sp)
2655         {
2656             if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2657             {
2658                 return true;
2659             }
2660             else
2661             {
2662                 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2663                 return false;
2664             }
2665         }
2666         else
2667         {
2668             error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2669             return false;
2670         }
2671     }
2672     return false;
2673 }
2674 
2675 SourceManager &
2676 Debugger::GetSourceManager ()
2677 {
2678     if (m_source_manager_ap.get() == NULL)
2679         m_source_manager_ap.reset (new SourceManager (shared_from_this()));
2680     return *m_source_manager_ap;
2681 }
2682 
2683 
2684